added seperate segments for initialized data
[fw/sdcc] / src / mcs51 / main.c
1 /** @file main.c
2     mcs51 specific general functions.
3
4     Note that mlh prepended _mcs51_ 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 msc51 */
18 static char *_mcs51_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   NULL
44 };
45
46
47 void mcs51_assignRegisters (eBBlock ** ebbs, int count);
48
49 static int regParmFlg = 0;      /* determine if we can register a parameter */
50
51 static void
52 _mcs51_init (void)
53 {
54   asm_addTree (&asm_asxxxx_mapping);
55 }
56
57 static void
58 _mcs51_reset_regparm ()
59 {
60   regParmFlg = 0;
61 }
62
63 static int
64 _mcs51_regparm (sym_link * l)
65 {
66   /* for this processor it is simple
67      can pass only the first parameter in a register */
68   if (regParmFlg)
69     return 0;
70
71   regParmFlg = 1;
72   return 1;
73 }
74
75 static bool
76 _mcs51_parseOptions (int *pargc, char **argv, int *i)
77 {
78   /* TODO: allow port-specific command line options to specify
79    * segment names here.
80    */
81   return FALSE;
82 }
83
84 static void
85 _mcs51_finaliseOptions (void)
86 {
87   if (options.model == MODEL_LARGE) {
88       port->mem.default_local_map = xdata;
89       port->mem.default_globl_map = xdata;
90     }
91   else
92     {
93       port->mem.default_local_map = data;
94       port->mem.default_globl_map = data;
95     }
96 }
97
98 static void
99 _mcs51_setDefaultOptions (void)
100 {
101 }
102
103 static const char *
104 _mcs51_getRegName (struct regs *reg)
105 {
106   if (reg)
107     return reg->name;
108   return "err";
109 }
110
111 static void
112 _mcs51_genAssemblerPreamble (FILE * of)
113 {
114 }
115
116 /* Generate interrupt vector table. */
117 static int
118 _mcs51_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
119 {
120   return FALSE;
121 }
122
123 /* Generate code to copy XINIT to XISEG */
124 static void _mcs51_genXINIT (FILE * of) {
125   fprintf (of, ";       _mcs51_genXINIT() start\n");
126   fprintf (of, "        mov     a,#s_XINIT\n");
127   fprintf (of, "        add     a,#l_XINIT\n");
128   fprintf (of, "        mov     r1,a\n");
129   fprintf (of, "        mov     a,#s_XINIT>>8\n");
130   fprintf (of, "        addc    a,#l_XINIT>>8\n");
131   fprintf (of, "        mov     r2,a\n");
132   fprintf (of, "        mov     dptr,#s_XINIT\n");
133   fprintf (of, "        mov     r0,#s_XISEG\n");
134   fprintf (of, "        mov     p2,#(s_XISEG >> 8)\n");
135   fprintf (of, "00001$: clr     a\n");
136   fprintf (of, "        movc    a,@a+dptr\n");
137   fprintf (of, "        movx    @r0,a\n");
138   fprintf (of, "        inc     dptr\n");
139   fprintf (of, "        inc     r0\n");
140   fprintf (of, "        cjne    r0,#0,00002$\n");
141   fprintf (of, "        inc     p2\n");
142   fprintf (of, "00002$: mov     a,dpl\n");
143   fprintf (of, "        cjne    a,ar1,00001$\n");
144   fprintf (of, "        mov     a,dph\n");
145   fprintf (of, "        cjne    a,ar2,00001$\n");
146   fprintf (of, "        mov     p2,#0xFF\n");
147   fprintf (of, ";       _mcs51_genXINIT() end\n");
148 }
149
150
151 /* Do CSE estimation */
152 static bool cseCostEstimation (iCode *ic, iCode *pdic)
153 {
154     operand *result = IC_RESULT(ic);
155     sym_link *result_type = operandType(result);
156
157     /* if it is a pointer then return ok for now */
158     if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
159     
160     /* if bitwise | add & subtract then no since mcs51 is pretty good at it 
161        so we will cse only if they are local (i.e. both ic & pdic belong to
162        the same basic block */
163     if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
164         /* then if they are the same Basic block then ok */
165         if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
166         else return 0;
167     }
168         
169     /* for others it is cheaper to do the cse */
170     return 1;
171 }
172
173 /** $1 is always the basename.
174     $2 is always the output file.
175     $3 varies
176     $l is the list of extra options that should be there somewhere...
177     MUST be terminated with a NULL.
178 */
179 static const char *_linkCmd[] =
180 {
181   "{bindir}{sep}aslink", "-nf", "$1", NULL
182 };
183
184 /* $3 is replaced by assembler.debug_opts resp. port->assembler.plain_opts */
185 static const char *_asmCmd[] =
186 {
187   "asx8051", "$l", "$3", "$1.asm", NULL
188 };
189
190 /* Globals */
191 PORT mcs51_port =
192 {
193   TARGET_ID_MCS51,
194   "mcs51",
195   "MCU 8051",                   /* Target name */
196   {
197     TRUE,                       /* Emit glue around main */
198     MODEL_SMALL | MODEL_LARGE,
199     MODEL_SMALL
200   },
201   {
202     _asmCmd,
203     NULL,
204     "-plosgffc",                /* Options with debug */
205     "-plosgff",                 /* Options without debug */
206     0,
207     ".asm"
208   },
209   {
210     _linkCmd,
211     NULL,
212     NULL,
213     ".rel"
214   },
215   {
216     _defaultRules
217   },
218   {
219         /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
220     1, 2, 2, 4, 1, 2, 3, 1, 4, 4
221   },
222   {
223     "XSEG    (XDATA)",
224     "STACK   (DATA)",
225     "CSEG    (CODE)",
226     "DSEG    (DATA)",
227     "ISEG    (DATA)",
228     "XSEG    (XDATA)",
229     "BSEG    (BIT)",
230     "RSEG    (DATA)",
231     /* "GSINIT  (CODE)", */ "CSEG    (CODE)",
232     "OSEG    (OVR,DATA)",
233     "GSFINAL (CODE)",
234     "HOME    (CODE)",
235     "XISEG   (XDATA)", // initialized xdata
236     "XINIT   (CODE)", // a code copy of xiseg
237     NULL,
238     NULL,
239     1
240   },
241   {
242     +1, 1, 4, 1, 1, 0
243   },
244     /* mcs51 has an 8 bit mul */
245   {
246     1, -1
247   },
248   "_",
249   _mcs51_init,
250   _mcs51_parseOptions,
251   _mcs51_finaliseOptions,
252   _mcs51_setDefaultOptions,
253   mcs51_assignRegisters,
254   _mcs51_getRegName,
255   _mcs51_keywords,
256   _mcs51_genAssemblerPreamble,
257   _mcs51_genIVT,
258   _mcs51_genXINIT,
259   _mcs51_reset_regparm,
260   _mcs51_regparm,
261   NULL,
262   NULL,
263   NULL,
264   FALSE,
265   0,                            /* leave lt */
266   0,                            /* leave gt */
267   1,                            /* transform <= to ! > */
268   1,                            /* transform >= to ! < */
269   1,                            /* transform != to !(a == b) */
270   0,                            /* leave == */
271   FALSE,                        /* No array initializer support. */
272   cseCostEstimation,
273   NULL,                         /* no builtin functions */
274   PORT_MAGIC
275 };