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