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