xa51, work in progress
[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 CSEG\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, "\tmov\tr7,#0x%04x\n", options.stack_loc);
154     fprintf (of, "\tcall\t_external_startup\n");
155     _xa51_genXINIT(of);
156     fprintf (of, "\tcall\t_main\n");
157     fprintf (of, "\treset\t;main should not return\n");
158   }
159 }
160
161 /* dummy linker for now */
162 void xa_link(void) {
163 }
164
165 /* Do CSE estimation */
166 static bool cseCostEstimation (iCode *ic, iCode *pdic)
167 {
168     operand *result = IC_RESULT(ic);
169     sym_link *result_type = operandType(result);
170
171     /* if it is a pointer then return ok for now */
172     if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
173     
174     /* if bitwise | add & subtract then no since xa51 is pretty good at it 
175        so we will cse only if they are local (i.e. both ic & pdic belong to
176        the same basic block */
177     if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
178         /* then if they are the same Basic block then ok */
179         if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
180         else return 0;
181     }
182         
183     /* for others it is cheaper to do the cse */
184     return 1;
185 }
186
187 /** $1 is always the basename.
188     $2 is always the output file.
189     $3 varies
190     $l is the list of extra options that should be there somewhere...
191     MUST be terminated with a NULL.
192 */
193 static const char *_linkCmd[] =
194 {
195   "xa_link", "", "$1", NULL
196 };
197
198 /* $3 is replaced by assembler.debug_opts resp. port->assembler.plain_opts */
199 static const char *_asmCmd[] =
200 {
201   "xa_rasm", "$l", "$3", "$1.xa", NULL
202 };
203
204 /* Globals */
205 PORT xa51_port =
206 {
207   TARGET_ID_XA51,
208   "xa51",
209   "MCU 80C51XA",                /* Target name */
210   {
211     FALSE,                      /* Emit glue around main */
212     MODEL_PAGE0,
213     MODEL_PAGE0
214   },
215   {
216     _asmCmd,
217     NULL,
218     "",                         /* Options with debug */
219     "",                         /* Options without debug */
220     0,
221     ".xa",
222     NULL                        /* no do_assemble function */
223   },
224   {
225     _linkCmd,
226     NULL,
227     NULL,
228     ".rel"
229   },
230   {
231     _defaultRules
232   },
233   {
234         /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
235     1, 2, 2, 4, 2, 2, 3, 1, 4, 4
236   },
237   {
238     "XSEG    (XDATA)",
239     "STACK   (XDATA)",
240     "CSEG    (CODE)",
241     "DSEG    (DATA)",
242     NULL, //"ISEG    (DATA)",
243     "XSEG    (XDATA)",
244     "BSEG    (BIT)",
245     NULL, //"RSEG    (DATA)",
246     "GSINIT  (CODE)",
247     NULL, //"OSEG    (OVR,XDATA)",
248     "GSFINAL (CODE)",
249     "HOME    (CODE)",
250     "XISEG   (XDATA)", // initialized xdata
251     "XINIT   (CODE)", // a code copy of xiseg
252     NULL, // default local map
253     NULL, // default global map
254     1
255   },
256   {
257     -1, // stack grows down
258     0, // bank overhead NUY
259     4, // isr overhead, page zero mode
260     2, // function call overhead, page zero mode
261     0, // reentrant overhead NUY
262     0 // banked overhead NUY
263   },
264     /* xa51 has an 16 bit mul */
265   {
266     2, -2
267   },
268   "_",
269   _xa51_init,
270   _xa51_parseOptions,
271   _xa51_finaliseOptions,
272   _xa51_setDefaultOptions,
273   xa51_assignRegisters,
274   _xa51_getRegName,
275   _xa51_keywords,
276   _xa51_genAssemblerPreamble,
277   _xa51_genAssemblerEnd,
278   _xa51_genIVT,
279   _xa51_genXINIT,
280   _xa51_reset_regparm,
281   _xa51_regparm,
282   NULL, // process_pragma()
283   NULL, // getMangledFunctionName()
284   NULL, // hasNativeMulFor()
285   TRUE, // use_dw_for_init
286   0,                            /* leave lt */
287   0,                            /* leave gt */
288   0,                            /* transform <= to ! > */
289   0,                            /* transform >= to ! < */
290   0,                            /* transform != to !(a == b) */
291   0,                            /* leave == */
292   FALSE,                        /* No array initializer support. */
293   cseCostEstimation,
294   NULL,                         /* no builtin functions */
295   GPOINTER,                     /* treat unqualified pointers as "generic" pointers */
296   1,                            /* reset labelKey to 1 */
297   1,                            /* globals & local static allowed */
298   PORT_MAGIC
299 };