* device/lib/z80/printf.c (sprintf): Added.
[fw/sdcc] / src / xa51 / main.c
1 /* @file main.c
2    xa51 specific general functions.
3 */
4
5 #include "common.h"
6 #include "main.h"
7 #include "ralloc.h"
8 #include "gen.h"
9
10 static char _defaultRules[] =
11 {
12 #include "peeph.rul"
13 };
14
15 /* list of key words used by xa51 */
16 static char *_xa51_keywords[] =
17 {
18   "at",
19   "bit",
20   "code",
21   "critical",
22   "data",
23   "far",
24   //"idata",
25   "interrupt",
26   "near",
27   //"pdata",
28   "reentrant",
29   "sfr",
30   "sbit",
31   "using",
32   "xdata",
33   //"_data",
34   //"_code",
35   //"_generic",
36   //"_near",
37   //"_xdata",
38   //"_pdata",
39   //"_idata",
40   "_naked",
41   //"_overlay",
42   NULL
43 };
44
45 extern int rewinds;
46 void   _xa51_genAssemblerEnd () {
47   //fprintf (stderr, "Did %d rewind%c for c-line in asm comments\n", rewinds,
48   //rewinds==1 ? '\0' : 's');
49 }
50
51 void xa51_assignRegisters (eBBlock ** ebbs, int count);
52
53 static int regParmFlg = 0;      /* determine if we can register a parameter */
54
55 static void
56 _xa51_init (void)
57 {
58   asm_addTree (&asm_xa_asm_mapping);
59 }
60
61 static void
62 _xa51_reset_regparm ()
63 {
64   regParmFlg = 0;
65 }
66
67 static int
68 _xa51_regparm (sym_link * l)
69 {
70   return 0; // for now
71   /* for this processor it is simple
72      can pass only the first parameter in a register */
73   if (regParmFlg)
74     return 0;
75
76   regParmFlg = 1;
77   return 1;
78 }
79
80 static bool
81 _xa51_parseOptions (int *pargc, char **argv, int *i)
82 {
83   /* TODO: allow port-specific command line options to specify
84    * segment names here.
85    */
86   return FALSE;
87 }
88
89 static void
90 _xa51_finaliseOptions (void)
91 {
92   port->mem.default_local_map = istack;
93   port->mem.default_globl_map = xdata;
94   if (options.model!=MODEL_PAGE0) {
95     fprintf (stderr, "-mxa51 only supports --model-page0\n");
96     exit (1);
97   }
98 }
99
100 static void
101 _xa51_setDefaultOptions (void)
102 {
103   options.stackAuto=1;
104   options.intlong_rent=1;
105   options.float_rent=1;
106   options.stack_loc=0x100;
107   options.data_loc=0;
108 }
109
110 static const char *
111 _xa51_getRegName (struct regs *reg)
112 {
113   if (reg)
114     return reg->name;
115   return "err";
116 }
117
118 /* Generate interrupt vector table. */
119 static int
120 _xa51_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
121 {
122   return TRUE;
123 }
124
125 /* Generate code to copy XINIT to XISEG */
126 static void _xa51_genXINIT (FILE * of) {
127   fprintf (of, ";       _xa51_genXINIT() start\n");
128   fprintf (of, "        mov     r0,#l_XINIT\n");
129   fprintf (of, "        beq     00002$\n");
130   fprintf (of, "        mov     r1,#s_XINIT\n");
131   fprintf (of, "        mov     r2,#s_XISEG\n");
132   fprintf (of, "00001$: movc    r3l,[r1+]\n");
133   fprintf (of, "        mov     [r2+],r3l\n");
134   fprintf (of, "        djnz    r0,00001$\n");
135   fprintf (of, "00002$:\n");
136   fprintf (of, ";       _xa51_genXINIT() end\n");
137 }
138
139 static void
140 _xa51_genAssemblerPreamble (FILE * of)
141 {
142   symbol *mainExists=newSymbol("main", 0);
143   mainExists->block=0;
144
145   if ((mainExists=findSymWithLevel(SymbolTab, mainExists))) {
146     fprintf (of, "\t.area GSINIT\t(CODE)\n");
147     fprintf (of, "__interrupt_vect:\n");
148     fprintf (of, "\t.dw\t0x8f00\n");
149     fprintf (of, "\t.dw\t__sdcc_gsinit_startup\n");
150     fprintf (of, "\n");
151     fprintf (of, "__sdcc_gsinit_startup:\n");
152     fprintf (of, ";\tmov.b\t_SCR,#0x01\t; page zero mode\n");
153     fprintf (of, "\t.db 0x96,0x48,0x40,0x01\n");
154     fprintf (of, "\tmov\tr7,#0x%04x\n", options.stack_loc);
155     fprintf (of, "\tcall\t_external_startup\n");
156     _xa51_genXINIT(of);
157     fprintf (of, "\t.area CSEG\t(CODE)\n");
158     fprintf (of, "\tcall\t_main\n");
159     fprintf (of, "\treset\t;main should not return\n");
160   }
161 }
162
163 /* dummy linker for now */
164 void xa_link(void) {
165 }
166
167 /* Do CSE estimation */
168 static bool cseCostEstimation (iCode *ic, iCode *pdic)
169 {
170     operand *result = IC_RESULT(ic);
171     sym_link *result_type = operandType(result);
172
173     /* if it is a pointer then return ok for now */
174     if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
175     
176     /* if bitwise | add & subtract then no since xa51 is pretty good at it 
177        so we will cse only if they are local (i.e. both ic & pdic belong to
178        the same basic block */
179     if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
180         /* then if they are the same Basic block then ok */
181         if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
182         else return 0;
183     }
184         
185     /* for others it is cheaper to do the cse */
186     return 1;
187 }
188
189 /** $1 is always the basename.
190     $2 is always the output file.
191     $3 varies
192     $l is the list of extra options that should be there somewhere...
193     MUST be terminated with a NULL.
194 */
195 static const char *_linkCmd[] =
196 {
197   "xa_link", "", "$1", NULL
198 };
199
200 /* $3 is replaced by assembler.debug_opts resp. port->assembler.plain_opts */
201 static const char *_asmCmd[] =
202 {
203   "xa_rasm", "$l", "$3", "$1.asm", NULL
204 };
205
206 /* Globals */
207 PORT xa51_port =
208 {
209   TARGET_ID_XA51,
210   "xa51",
211   "MCU 80C51XA",                /* Target name */
212   NULL,                         /* Processor name */
213   {
214     FALSE,                      /* Emit glue around main */
215     MODEL_PAGE0,
216     MODEL_PAGE0
217   },
218   {
219     _asmCmd,
220     NULL,
221     "",                         /* Options with debug */
222     "",                         /* Options without debug */
223     0,
224     ".asm",
225     NULL                        /* no do_assemble function */
226   },
227   {
228     _linkCmd,
229     NULL,
230     NULL,
231     ".rel"
232   },
233   {
234     _defaultRules
235   },
236   {
237         /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
238     1, 2, 2, 4, 2, 2, 3, 1, 4, 4
239   },
240   {
241     "XSEG    (XDATA)",
242     "STACK   (XDATA)",
243     "CSEG    (CODE)",
244     "DSEG    (DATA)",
245     NULL, //"ISEG    (DATA)",
246     "XSEG    (XDATA)",
247     "BSEG    (BIT)",
248     NULL, //"RSEG    (DATA)",
249     "GSINIT  (CODE)",
250     NULL, //"OSEG    (OVR,XDATA)",
251     "GSFINAL (CODE)",
252     "HOME    (CODE)",
253     "XISEG   (XDATA)", // initialized xdata
254     "XINIT   (CODE)", // a code copy of xiseg
255     NULL, // default local map
256     NULL, // default global map
257     1
258   },
259   {
260     -1, // stack grows down
261     0, // bank overhead NUY
262     4, // isr overhead, page zero mode
263     2, // function call overhead, page zero mode
264     0, // reentrant overhead NUY
265     0 // banked overhead NUY
266   },
267     /* xa51 has an 16 bit mul */
268   {
269     2, -2
270   },
271   "_",
272   _xa51_init,
273   _xa51_parseOptions,
274   NULL,
275   _xa51_finaliseOptions,
276   _xa51_setDefaultOptions,
277   xa51_assignRegisters,
278   _xa51_getRegName,
279   _xa51_keywords,
280   _xa51_genAssemblerPreamble,
281   _xa51_genAssemblerEnd,
282   _xa51_genIVT,
283   _xa51_genXINIT,
284   _xa51_reset_regparm,
285   _xa51_regparm,
286   NULL, // process_pragma()
287   NULL, // getMangledFunctionName()
288   NULL, // hasNativeMulFor()
289   TRUE, // use_dw_for_init
290   0,                            /* leave lt */
291   0,                            /* leave gt */
292   1,                            /* transform <= to ! > */
293   1,                            /* transform >= to ! < */
294   1,                            /* transform != to !(a == b) */
295   0,                            /* leave == */
296   FALSE,                        /* No array initializer support. */
297   cseCostEstimation,
298   NULL,                         /* no builtin functions */
299   GPOINTER,                     /* treat unqualified pointers as "generic" pointers */
300   1,                            /* reset labelKey to 1 */
301   1,                            /* globals & local static allowed */
302   PORT_MAGIC
303 };