d3e9a86dccd73845d52dbc2d2c00c05935779d2b
[fw/sdcc] / src / port.h
1 /** @file port.h
2     Definitions for what a port must provide.
3     All ports are referenced in SDCCmain.c.
4 */
5 #ifndef PORT_INCLUDE
6 #define PORT_INCLUDE
7
8 #include "SDCCicode.h"
9 #include "SDCCargs.h"
10 #include "SDCCpeeph.h"
11 #include "mcs51/peep.h"
12
13 #define TARGET_ID_MCS51    1
14 #define TARGET_ID_GBZ80    2
15 #define TARGET_ID_Z80      3
16 #define TARGET_ID_AVR      4
17 #define TARGET_ID_DS390    5
18 #define TARGET_ID_PIC      6
19 #define TARGET_ID_PIC16    7
20 #define TARGET_ID_XA51     9
21 #define TARGET_ID_DS400    10
22 #define TARGET_ID_HC08     11
23
24 /* Macro to test the target we are compiling for.
25    Can only be used after SDCCmain has defined the port
26 */
27 #define TARGET_IS_MCS51 (port->id==TARGET_ID_MCS51)
28 #define TARGET_IS_GBZ80 (port->id==TARGET_ID_GBZ80)
29 #define TARGET_IS_Z80   (port->id==TARGET_ID_Z80)
30 #define TARGET_Z80_LIKE ((port->id==TARGET_ID_Z80) || (port->id==TARGET_ID_GBZ80))
31 #define TARGET_IS_AVR   (port->id==TARGET_ID_AVR)
32 #define TARGET_IS_DS390 (port->id==TARGET_ID_DS390)
33 #define TARGET_IS_DS400 (port->id==TARGET_ID_DS400)
34 #define TARGET_IS_PIC   (port->id==TARGET_ID_PIC)
35 #define TARGET_IS_PIC16 (port->id==TARGET_ID_PIC16)
36 #define TARGET_IS_XA51  (port->id==TARGET_ID_XA51)
37 #define TARGET_IS_HC08  (port->id==TARGET_ID_HC08)
38
39 #define MAX_BUILTIN_ARGS        16
40 /* definition of builtin functions */
41 typedef struct builtins
42   {
43     char *name;                         /* name of builtin function */
44     char *rtype;                        /* return type as string : see typefromStr */
45     int  nParms;                        /* number of parms : max 8 */
46     char *parm_types[MAX_BUILTIN_ARGS]; /* each parm type as string : see typeFromStr */
47   } builtins;
48
49 struct ebbIndex;
50
51 /* pragma structure */
52 struct pragma_s
53   {
54     const char *name;
55     int id;
56     char deprecated;
57     int (*func)(int id, const char *name, const char *cp);
58   };
59
60 /* defined in SDCClex.lex */
61 int process_pragma_tbl(const struct pragma_s *pragma_tbl, const char *s);
62
63 /* Processor specific names */
64 typedef struct
65   {
66 /** Unique id for this target */
67     const int id;
68 /** Target name used for -m */
69     const char *target;
70
71 /** Target name string, used for --help */
72     const char *target_name;
73
74 /** Specific processor for the given target family. specified by -p */
75     char *processor;
76
77     struct
78       {
79         /** Pointer to glue function */
80         void (*do_glue)(void);
81         /** TRUE if all types of glue functions should be inserted into
82             the file that also defines main.
83             We dont want this in cases like the z80 where the startup
84             code is provided by a seperate module.
85         */
86         bool glue_up_main;
87         /* OR of MODEL_* */
88         int supported_models;
89         int default_model;
90       }
91     general;
92
93     /* assembler related information */
94     struct
95       {
96         /** Command to run and arguments (eg as-z80) */
97         const char **cmd;
98         /** Alternate macro based form. */
99         const char *mcmd;
100         /** Arguments for debug mode. */
101         const char *debug_opts;
102         /** Arguments for normal assembly mode. */
103         const char *plain_opts;
104         /* print externs as global */
105         int externGlobal;
106         /* assembler file extension */
107         const char *file_ext;
108         /** If non-null will be used to execute the assembler. */
109         void (*do_assemble) (set *);
110       }
111     assembler;
112
113     /* linker related info */
114     struct
115       {
116         /** Command to run (eg link-z80) */
117         const char **cmd;
118         /** Alternate macro based form. */
119         const char *mcmd;
120         /** If non-null will be used to execute the link. */
121         void (*do_link) (void);
122         /** Extension for object files (.rel, .obj, ...) */
123         const char *rel_ext;
124         /** 1 if port needs the .lnk file, 0 otherwise */
125         const int needLinkerScript;
126       }
127     linker;
128
129     struct
130       {
131 /** Default peephole rules */
132         char *default_rules;
133         int (*getSize)(lineNode *line);
134         bitVect * (*getRegsRead)(lineNode *line);
135         bitVect * (*getRegsWritten)(lineNode *line);
136         bool (*deadMove) (const char *reg, lineNode *currPl, lineNode *head);
137       }
138     peep;
139
140 /** Basic type sizes */
141     struct
142       {
143         int char_size;
144         int short_size;
145         unsigned int int_size;
146         int long_size;
147         int ptr_size;       //near
148         int fptr_size;      //far
149         int gptr_size;      //generic
150         int bit_size;
151         int float_size;
152         int max_base_size;
153       }
154     s;
155
156 /** tags for far, near, xstack, code generic pointers */
157     struct
158       {
159         int tag_far;
160         int tag_near;
161         int tag_xstack;
162         int tag_code;
163       }
164     gp_tags;
165
166 /** memory regions related stuff */
167     struct
168       {
169         const char *xstack_name;
170         const char *istack_name;
171         const char *code_name;
172         const char *data_name;
173         const char *idata_name;
174         const char *pdata_name;
175         const char *xdata_name;
176         const char *bit_name;
177         const char *reg_name;
178         const char *static_name;
179         const char *overlay_name;
180         const char *post_static_name;
181         const char *home_name;
182         const char *xidata_name; // initialized xdata
183         const char *xinit_name; // a code copy of xidata
184         const char *const_name; // const data (code or not)
185         const char *cabs_name; // const absolute data (code or not)
186         const char *xabs_name; // absolute xdata/pdata
187         const char *iabs_name; // absolute idata/data
188         struct memmap *default_local_map; // default location for auto vars
189         struct memmap *default_globl_map; // default location for globl vars
190         int code_ro;            /* code space read-only 1=yes */
191       }
192     mem;
193
194     struct
195       {
196           void (*genExtraAreaDeclaration)(FILE *, bool);
197           void (*genExtraAreaLinkOptions)(FILE *);
198       }
199     extraAreas;
200
201     /* stack related information */
202     struct
203       {
204 /** -1 for grows down (z80), +1 for grows up (mcs51) */
205         int direction;
206 /** Extra overhead when calling between banks */
207         int bank_overhead;
208 /** Extra overhead when the function is an ISR */
209         int isr_overhead;
210 /** Standard overhead for a function call */
211         int call_overhead;
212 /** Re-enterant space */
213         int reent_overhead;
214         /** 'banked' call overhead.
215             Mild overlap with bank_overhead */
216         int banked_overhead;
217       }
218     stack;
219
220     struct
221       {
222         /** One more than the smallest
223             mul/div operation the processor can do natively
224             Eg if the processor has an 8 bit mul, native below is 2 */
225         unsigned muldiv;
226         unsigned shift;
227       }
228     support;
229
230     struct
231       {
232         void (*emitDebuggerSymbol) (char *);
233         struct
234           {
235             int (*regNum) (struct regs *);
236             bitVect * cfiSame;
237             bitVect * cfiUndef;
238             int addressSize;
239             int regNumRet;
240             int regNumSP;
241             int regNumBP;
242             int offsetSP;
243           }
244         dwarf;
245       }
246     debugger;
247
248     struct
249       {
250         int maxCount;
251         int sizeofElement;
252         int sizeofMatchJump[3];
253         int sizeofRangeCompare[3];
254         int sizeofSubtract;
255         int sizeofDispatch;
256       }
257     jumptableCost;
258
259 /** Prefix to add to a C function (eg "_") */
260     const char *fun_prefix;
261
262     /** Called once the processor target has been selected.
263         First chance to initalise and set any port specific variables.
264         'port' is set before calling this.  May be NULL.
265     */
266     void (*init) (void);
267 /** Parses one option + its arguments */
268       bool (*parseOption) (int *pargc, char **argv, int *i);
269 /** Optional list of automatically parsed options.  Should be
270     implemented to at least show the help text correctly. */
271     OPTION *poptions;
272 /** Initialise port spectific paths */
273     void (*initPaths)(void);
274 /** Called after all the options have been parsed. */
275     void (*finaliseOptions) (void);
276     /** Called after the port has been selected but before any
277         options are parsed. */
278     void (*setDefaultOptions) (void);
279 /** Does the dirty work. */
280     void (*assignRegisters) (struct ebbIndex *);
281
282     /** Returns the register name of a symbol.
283         Used so that 'regs' can be an incomplete type. */
284     const char *(*getRegName) (struct regs * reg);
285
286     /* list of keywords that are used by this
287        target (used by lexer) */
288     char **keywords;
289
290     /* Write any port specific assembler output. */
291     void (*genAssemblerPreamble) (FILE * of);
292       /* invoked at end assembler file */
293     void (*genAssemblerEnd) (FILE * of);
294
295     /* Write the port specific IVT. If genIVT is NULL or if
296      * it returns zero, default (8051) IVT generation code
297      * will be used.
298      */
299     int (*genIVT) (FILE * of, symbol ** intTable, int intCount);
300
301     void (*genXINIT) (FILE * of);
302
303     /* Write port specific startup code */
304     void (*genInitStartup) (FILE * of);
305
306     /* parameter passing in register related functions */
307     void (*reset_regparms) (void);      /* reset the register count */
308     int (*reg_parm) (struct sym_link *, bool reentrant);        /* will return 1 if can be passed in register */
309
310     /** Process the pragma string 'sz'.  Returns 0 if recognised and
311         processed, 1 otherwise.  May be NULL.
312     */
313     int (*process_pragma) (const char *sz);
314
315     /** Mangles a support function name to reflect the calling model.
316      */
317     char *(*getMangledFunctionName) (char *szOrginial);
318
319     /** Returns true if the port can multiply the two types nativly
320         without using support functions.
321     */
322     bool (*hasNativeMulFor) (iCode *ic, sym_link *left, sym_link *right);
323
324     /** Returns true if the port has implemented certain bit
325         manipulation iCodes (RRC, RLC, SWAP, GETHBIT, GETABIT, GETBYTE, GETWORD)
326     */
327     bool (*hasExtBitOp) (int op, int size);
328
329     /** Returns the relative expense of accessing a particular output
330         storage class. Larger values indicate higher expense.
331     */
332     int (*oclsExpense) (struct memmap *oclass);
333
334     /** If TRUE, then tprintf and !dw will be used for some initalisers
335      */
336     bool use_dw_for_init;
337
338     /** TRUE for targets with little endian byte ordering, FALSE for
339         targets with big endian byte ordering.
340      */
341     bool little_endian;
342
343     /* condition transformations */
344     bool lt_nge;                /* transform (a < b)  to !(a >= b)  */
345     bool gt_nle;                /* transform (a > b)  to !(a <= b)  */
346     bool le_ngt;                /* transform (a <= b) to !(a > b)   */
347     bool ge_nlt;                /* transform (a >= b) to !(a < b)   */
348     bool ne_neq;                /* transform a != b --> ! (a == b)  */
349     bool eq_nne;                /* transform a == b --> ! (a != b)  */
350
351     bool arrayInitializerSuppported;
352     bool (*cseOk) (iCode *ic, iCode *pdic);
353     builtins *builtintable;     /* table of builtin functions */
354     int unqualified_pointer;    /* unqualified pointers type is  */
355     int reset_labelKey  ;       /* reset Label no 1 at the start of a function */
356     int globals_allowed ;       /* global & static locals not allowed ?  0 ONLY TININative*/
357 #define PORT_MAGIC 0xAC32
358 /** Used at runtime to detect if this structure has been completly filled in. */
359     int magic;
360   }
361 PORT;
362
363 extern PORT *port;
364
365 #if !OPT_DISABLE_MCS51
366 extern PORT mcs51_port;
367 #endif
368 #if !OPT_DISABLE_GBZ80
369 extern PORT gbz80_port;
370 #endif
371 #if !OPT_DISABLE_Z80
372 extern PORT z80_port;
373 #endif
374 #if !OPT_DISABLE_AVR
375 extern PORT avr_port;
376 #endif
377 #if !OPT_DISABLE_DS390
378 extern PORT ds390_port;
379 #endif
380 #if !OPT_DISABLE_PIC
381 extern PORT pic_port;
382 #endif
383 #if !OPT_DISABLE_PIC16
384 extern PORT pic16_port;
385 #endif
386 #if !OPT_DISABLE_TININative
387 extern PORT tininative_port;
388 #endif
389 #if !OPT_DISABLE_XA51
390 extern PORT xa51_port;
391 #endif
392 #if !OPT_DISABLE_DS400
393 extern PORT ds400_port;
394 #endif
395 #if !OPT_DISABLE_HC08
396 extern PORT hc08_port;
397 #endif
398
399 #endif /* PORT_INCLUDE*/