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