d2693844c5d80b2f9098e354be20ab48e83956b7
[fw/sdcc] / src / pic / main.c
1 /** @file main.c
2     pic14 specific general functions.
3
4     Note that mlh prepended _pic14_ 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
13 static char _defaultRules[] =
14 {
15 #include "peeph.rul"
16 };
17
18 /* list of key words used by msc51 */
19 static char *_pic14_keywords[] =
20 {
21   "at",
22   "bit",
23   "code",
24   "critical",
25   "data",
26   "far",
27   "idata",
28   "interrupt",
29   "near",
30   "pdata",
31   "reentrant",
32   "sfr",
33   "sbit",
34   "using",
35   "xdata",
36   "_data",
37   "_code",
38   "_generic",
39   "_near",
40   "_xdata",
41   "_pdata",
42   "_idata",
43   NULL
44 };
45
46 void  pCodeInitRegisters(void);
47
48 void pic14_assignRegisters (eBBlock ** ebbs, int count);
49
50 static int regParmFlg = 0;      /* determine if we can register a parameter */
51
52 static void
53 _pic14_init (void)
54 {
55   asm_addTree (&asm_asxxxx_mapping);
56   pCodeInitRegisters();
57 }
58
59 static void
60 _pic14_reset_regparm ()
61 {
62   regParmFlg = 0;
63 }
64
65 static int
66 _pic14_regparm (sym_link * l)
67 {
68   /* for this processor it is simple
69      can pass only the first parameter in a register */
70   //if (regParmFlg)
71   //  return 0;
72
73   regParmFlg++;// = 1;
74   return 1;
75 }
76
77 static bool
78 _pic14_parseOptions (int *pargc, char **argv, int *i)
79 {
80   /* TODO: allow port-specific command line options to specify
81    * segment names here.
82    */
83   return FALSE;
84 }
85
86 static void
87 _pic14_finaliseOptions (void)
88 {
89
90       port->mem.default_local_map = data;
91       port->mem.default_globl_map = data;
92 #if 0
93   /* Hack-o-matic: if we are using the flat24 model,
94    * adjust pointer sizes.
95    */
96   if (options.model == MODEL_FLAT24)
97     {
98
99       fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
100                "for DS80C390 support. This code generator is "
101                "badly out of date and probably broken.\n");
102
103       port->s.fptr_size = 3;
104       port->s.gptr_size = 4;
105       port->stack.isr_overhead++;       /* Will save dpx on ISR entry. */
106 #if 1
107       port->stack.call_overhead++;      /* This acounts for the extra byte 
108                                          * of return addres on the stack.
109                                          * but is ugly. There must be a 
110                                          * better way.
111                                          */
112 #endif
113       fReturn = fReturn390;
114       fReturnSize = 5;
115     }
116
117   if (options.model == MODEL_LARGE)
118     {
119       port->mem.default_local_map = xdata;
120       port->mem.default_globl_map = xdata;
121     }
122   else
123     {
124       port->mem.default_local_map = data;
125       port->mem.default_globl_map = data;
126     }
127
128   if (options.stack10bit)
129     {
130       if (options.model != MODEL_FLAT24)
131         {
132           fprintf (stderr,
133                    "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
134           fprintf (stderr, "\t10 bit stack mode disabled.\n");
135           options.stack10bit = 0;
136         }
137       else
138         {
139           /* Fixup the memory map for the stack; it is now in
140            * far space and requires a FPOINTER to access it.
141            */
142           istack->fmap = 1;
143           istack->ptrType = FPOINTER;
144         }
145     }
146 #endif
147 }
148
149 static void
150 _pic14_setDefaultOptions (void)
151 {
152 }
153
154 static const char *
155 _pic14_getRegName (struct regs *reg)
156 {
157   if (reg)
158     return reg->name;
159   return "err";
160 }
161
162 extern char *processor_base_name(void);
163
164 static void
165 _pic14_genAssemblerPreamble (FILE * of)
166 {
167   char * name = processor_base_name();
168
169   if(!name) {
170
171     name = "p16f877";
172     fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
173   }
174
175   fprintf (of, "\tlist\tp=%s\n",&name[1]);
176   fprintf (of, "\t__config _wdt_off\n");
177   fprintf (of, "\ninclude \"%s.inc\"\n",name);
178 }
179
180 /* Generate interrupt vector table. */
181 static int
182 _pic14_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
183 {
184   int i;
185
186   if (options.model != MODEL_FLAT24)
187     {
188       /* Let the default code handle it. */
189       return FALSE;
190     }
191
192   fprintf (of, "\t;ajmp\t__sdcc_gsinit_startup\n");
193
194   /* now for the other interrupts */
195   for (i = 0; i < maxInterrupts; i++)
196     {
197       if (interrupts[i])
198         {
199           fprintf (of, "\t;ljmp\t%s\n\t.ds\t4\n", interrupts[i]->rname);
200         }
201       else
202         {
203           fprintf (of, "\t;reti\n\t.ds\t7\n");
204         }
205     }
206
207   return TRUE;
208 }
209
210 /** $1 is always the basename.
211     $2 is always the output file.
212     $3 varies
213     $l is the list of extra options that should be there somewhere...
214     MUST be terminated with a NULL.
215 */
216 static const char *_linkCmd[] =
217 {
218   "aslink", "-nf", "$1", NULL
219 };
220
221 /* Sigh. This really is not good. For now, I recommend:
222  * sdcc -S -mpic14 file.c
223  * the -S option does not compile or link
224  */
225 static const char *_asmCmd[] =
226 {
227   "gpasm", "-c  -I /usr/local/share/gpasm/header", "$1.asm", NULL
228
229 };
230
231 /* Globals */
232 PORT pic_port =
233 {
234   TARGET_ID_PIC,
235   "pic14",
236   "MCU pic",                    /* Target name */
237   NULL,                         /* Processor */
238   {
239     TRUE,                       /* Emit glue around main */
240     MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
241     MODEL_SMALL
242   },
243   {
244     _asmCmd,
245     NULL,
246     NULL,
247     NULL,
248         //"-plosgffc",          /* Options with debug */
249         //"-plosgff",           /* Options without debug */
250     0,
251     ".asm",
252     NULL                        /* no do_assemble function */
253   },
254   {
255     _linkCmd,
256     NULL,
257     NULL,
258     ".rel"
259   },
260   {
261     _defaultRules
262   },
263   {
264         /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
265     1, 2, 2, 4, 1, 2, 1, 1, 4, 4
266         /* TSD - I changed the size of gptr from 3 to 1. However, it should be
267            2 so that we can accomodate the PIC's with 4 register banks (like the
268            16f877)
269          */
270   },
271   {
272     "XSEG    (XDATA)",
273     "STACK   (DATA)",
274     "CSEG    (CODE)",
275     "DSEG    (DATA)",
276     "ISEG    (DATA)",
277     "XSEG    (XDATA)",
278     "BSEG    (BIT)",
279     "RSEG    (DATA)",
280     "GSINIT  (CODE)",
281     "OSEG    (OVR,DATA)",
282     "GSFINAL (CODE)",
283     "HOME        (CODE)",
284     NULL, // xidata
285     NULL, // xinit
286     NULL,
287     NULL,
288     1
289   },
290   {
291     +1, 1, 4, 1, 1, 0
292   },
293     /* pic14 has an 8 bit mul */
294   {
295     1, -1
296   },
297   "_",
298   _pic14_init,
299   _pic14_parseOptions,
300   _pic14_finaliseOptions,
301   _pic14_setDefaultOptions,
302   pic14_assignRegisters,
303   _pic14_getRegName,
304   _pic14_keywords,
305   _pic14_genAssemblerPreamble,
306   NULL,                         /* no genAssemblerEnd */
307   _pic14_genIVT,
308   NULL, // _pic14_genXINIT
309   _pic14_reset_regparm,
310   _pic14_regparm,
311   NULL,
312   NULL,
313   NULL,
314   FALSE,
315   0,                            /* leave lt */
316   0,                            /* leave gt */
317   1,                            /* transform <= to ! > */
318   1,                            /* transform >= to ! < */
319   1,                            /* transform != to !(a == b) */
320   0,                            /* leave == */
321   FALSE,                        /* No array initializer support. */
322   0,                            /* no CSE cost estimation yet */
323   NULL,                         /* no builtin functions */
324   GPOINTER,                     /* treat unqualified pointers as "generic" pointers */
325   1,                            /* reset labelKey to 1 */
326   1,                            /* globals & local static allowed */
327   PORT_MAGIC
328 };