2004-01-13 Vangelis Rokas <vrokas@otenet.gr>
[fw/sdcc] / src / pic16 / main.c
1 /*-------------------------------------------------------------------------
2
3   main.c - pic16 specific general functions.
4
5    Written by - Scott Dattalo scott@dattalo.com
6    Ported to PIC16 by - Martin Dubuc m.debuc@rogers.com
7     
8    Note that mlh prepended _pic16_ on the static functions.  Makes
9    it easier to set a breakpoint using the debugger.
10
11
12    This program is free software; you can redistribute it and/or modify it
13    under the terms of the GNU General Public License as published by the
14    Free Software Foundation; either version 2, or (at your option) any
15    later version.
16    
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21    
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 -------------------------------------------------------------------------*/
26
27 #include "common.h"
28 #include "main.h"
29 #include "ralloc.h"
30 #include "device.h"
31 #include "SDCCutil.h"
32 #include "glue.h"
33 //#include "gen.h"
34
35
36 static char _defaultRules[] =
37 {
38 #include "peeph.rul"
39 };
40
41 /* list of key words used by pic16 */
42 static char *_pic16_keywords[] =
43 {
44   "at",
45   "bit",
46   "code",
47   "critical",
48   "data",
49   "far",
50   "idata",
51   "interrupt",
52   "near",
53   "pdata",
54   "reentrant",
55   "sfr",
56   "sbit",
57   "using",
58   "xdata",
59   "_data",
60   "_code",
61   "_generic",
62   "_near",
63   "_xdata",
64   "_pdata",
65   "_idata",
66   "_naked",
67   NULL
68 };
69
70
71 pic16_sectioninfo_t pic16_sectioninfo;
72
73
74 extern char *pic16_processor_base_name(void);
75
76 void  pic16_pCodeInitRegisters(void);
77
78 void pic16_assignRegisters (eBBlock ** ebbs, int count);
79
80 static int regParmFlg = 0;      /* determine if we can register a parameter */
81
82 pic16_options_t pic16_options;
83
84 static void
85 _pic16_init (void)
86 {
87         asm_addTree (&asm_asxxxx_mapping);
88         pic16_pCodeInitRegisters();
89         maxInterrupts = 2;
90         
91
92         /* set pic16 port options to defaults */
93         pic16_options.gen_banksel = 0;
94         pic16_options.opt_banksel = 0;
95         pic16_options.omit_configw = 0;
96         pic16_options.omit_ivt = 0;
97         pic16_options.leave_reset = 0;
98         pic16_options.enable_stack = 0;
99         pic16_options.stack_model = 0;                  /* 0 for 'small', 1 for 'large' */
100 }
101
102 static void
103 _pic16_reset_regparm ()
104 {
105   regParmFlg = 0;
106 }
107
108 static int
109 _pic16_regparm (sym_link * l)
110 {
111   /* for this processor it is simple
112      can pass only the first parameter in a register */
113   //if (regParmFlg)
114   //  return 0;
115
116   regParmFlg++;// = 1;
117   return 1;
118 }
119
120
121 set *absSymSet;
122
123 static int
124 _process_pragma(const char *sz)
125 {
126   static const char *WHITE = " \t";
127   
128   char  *ptr = strtok((char *)sz, WHITE);
129
130         if (startsWith (ptr, "maxram")) {
131           char *maxRAM = strtok((char *)NULL, WHITE);
132
133                 if (maxRAM != (char *)NULL) {
134                   int maxRAMaddress;
135                   value *maxRAMVal;
136
137                         maxRAMVal = constVal(maxRAM);
138                         maxRAMaddress = (int)floatFromVal(maxRAMVal);
139                         pic16_setMaxRAM(maxRAMaddress);
140                 }
141         }
142         
143         if(startsWith(ptr, "stack")) {
144           char *stackPosS = strtok((char *)NULL, WHITE);
145           value *stackPosVal;
146
147 //              fprintf(stderr, "Initializing stack pointer to 0x%x\n", (int)floatFromVal(constVal(stackPos)));
148                 stackPosVal = constVal( stackPosS );
149                 stackPos = (unsigned int)floatFromVal( stackPosVal );
150
151           return 0;
152         }
153         
154         if(startsWith(ptr, "code")) {
155           char *symname = strtok((char *)NULL, WHITE);
156           char *location = strtok((char *)NULL, WHITE);
157           absSym *absS;
158           value *addr;
159
160                 absS = Safe_calloc(1, sizeof(absSym));
161                 absS->name = Safe_strdup( symname );
162                 addr = constVal( location );
163                 absS->address = (unsigned int)floatFromVal( addr );
164
165                 addSet(&absSymSet, absS);
166                 fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
167                         __FILE__, __LINE__, symname, absS->address);
168
169           return 0;
170         }         
171
172   return 1;
173 }
174
175 #define REP_UDATA       "--preplace-udata-with="
176 #define REP_UDATAACS    "--preplace-udata-acs-with="
177 #define REP_UDATAOVR    "--preplace-udata-ovr-with="
178 #define REP_UDATASHR    "--preplace-udata-sht-with="
179
180 #define NAME_CODE       "--psection-code-name="
181 #define NAME_IDATA      "--psection-idata-name="
182 #define NAME_UDATA      "--psection-udata-name="
183 #define NAME_UDATAACS   "--psection-udata-acs-name="
184 #define NAME_UDATAOVR   "--psection-udata-ovr-name="
185 #define NAME_UDATASHR   "--psection-udata-shr-name="
186
187 #define ADDR_CODE       "--psection-code-addr="
188 #define ADDR_IDATA      "--psection-idata-addr="
189 #define ADDR_UDATA      "--psection-udata-addr="
190 #define ADDR_UDATAACS   "--psection-udata-acs-addr="
191 #define ADDR_UDATAOVR   "--psection-udata-ovr-addr="
192 #define ADDR_UDATASHR   "--psection-udata-shr-addr="
193
194 #define STACK_MODEL     "--pstack-model="
195 #define OPT_BANKSEL     "--obanksel="
196
197
198 extern int pic16_debug_verbose;
199 extern int pic16_ralloc_debug;
200 extern int pic16_pcode_verbose;
201
202 OPTION pic16_optionsTable[]= {
203         { 0,    "--pgen-banksel",       &pic16_options.gen_banksel,     "generate BANKSEL assembler directives"},
204         { 0,    OPT_BANKSEL,            NULL,                           "set banksel optimization level (default=0 no)"},
205         { 0,    "--pomit-config-words", &pic16_options.omit_configw,    "omit the generation of configuration words"},
206         { 0,    "--pomit-ivt",          &pic16_options.omit_ivt,        "omit the generation of the Interrupt Vector Table"},
207         { 0,    "--pleave-reset-vector",&pic16_options.leave_reset,     "when omitting IVT leave RESET vector"},
208         { 0,    "--penable-stack",      &pic16_options.enable_stack,    "enable the use of stack"},
209         { 0,    STACK_MODEL,    NULL,   "use stack model 'small' (default) or 'large'"},
210
211         { 0,    "--debug-xtra",         &pic16_debug_verbose,   "show more debug info in assembly output"},
212         { 0,    "--debug-ralloc",       &pic16_ralloc_debug,    "dump register allocator debug file *.d"},
213         { 0,    "--pcode-verbose",      &pic16_pcode_verbose,   "dump pcode related info"},
214                 
215         { 0,    REP_UDATA,      NULL,   "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
216
217 #if 0
218         /* these may not be in any use -- VR */
219         { 0,    AT_UDATAACS,    NULL,   "Emit udata_acs variables at another section"},
220         { 0,    AT_UDATAOVR,    NULL,   "Emit udata_ovr variables at another section"},
221         { 0,    AT_UDATASHR,    NULL,   "Emit udata_shr variables at another section"},
222 #endif
223
224 #if 0
225         /* commented out for the time being -- VR */
226         { 0,    NAME_CODE,      NULL,   "Set code section name[,address]"},
227         { 0,    NAME_IDATA,     NULL,   "Set idata section name[,address]"},
228         { 0,    NAME_UDATA,     NULL,   "Set udata section name[,address]"},
229         { 0,    NAME_UDATAACS,  NULL,   "Set udata_acs section name[,address]"},
230         { 0,    NAME_UDATAOVR,  NULL,   "Set udata_ovr section name[,address]"},
231         { 0,    NAME_UDATASHR,  NULL,   "Set udata_shr section name[,address]"},
232         
233         { 0,    ADDR_CODE,      NULL,   "Set code section address"},
234         { 0,    ADDR_IDATA,     NULL,   "Set idata section address"},
235         { 0,    ADDR_UDATA,     NULL,   "Set udata section address"},
236         { 0,    ADDR_UDATAACS,  NULL,   "Set udata_acs section address"},
237         { 0,    ADDR_UDATAOVR,  NULL,   "Set udata_ovr section address"},
238         { 0,    ADDR_UDATASHR,  NULL,   "Set udata_shr section address"},
239 #endif
240
241         { 0,    NULL,           NULL,   NULL}
242         };
243
244
245 #define ISOPT(str)      !strncmp(argv[ *i ], str, strlen(str) )
246
247 extern char *getStringArg(const char *,  char **, int *, int);
248 extern int getIntArg(const char *, char **, int *, int);
249
250 static bool
251 _pic16_parseOptions (int *pargc, char **argv, int *i)
252 {
253   int j=0;
254 //  set *tset;
255
256   /* TODO: allow port-specific command line options to specify
257    * segment names here.
258    */
259         /* check for arguments that have associated an integer variable */
260         while(pic16_optionsTable[j].pparameter) {
261                 if(ISOPT( pic16_optionsTable[j].longOpt )) {
262                         (*pic16_optionsTable[j].pparameter)++;
263                         return TRUE;
264                 }
265                 j++;
266         }
267
268
269         if(ISOPT(STACK_MODEL)) {
270                 switch(*getStringArg(STACK_MODEL, argv, i, *pargc)) {
271                         case 's': pic16_options.stack_model = 0; break;
272                         case 'l': pic16_options.stack_model = 1; break;
273                         
274                         default:
275                                 fprintf(stderr, "Unknown stack model");
276                                 exit(-1);
277                 }
278                 return TRUE;
279         }
280
281         if(ISOPT(OPT_BANKSEL)) {
282                 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
283                 return TRUE;
284         }
285
286         if(ISOPT(REP_UDATA)) {
287                 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
288                 return TRUE;
289         }
290         
291 /*
292         if(ISOPT(AT_UDATAACS)) {
293                 pic16_sectioninfo.at_udataacs = Safe_strdup( getStringArg(AT_UDATAACS, argv, i, *pargc));
294                 return TRUE;
295         }
296
297         if(ISOPT(AT_UDATAOVR)) {
298                 pic16_sectioninfo.at_udataovr = Safe_strdup( getStringArg(AT_UDATAOVR, argv, i, *pargc));
299                 return TRUE;
300         }
301         
302         if(ISOPT(AT_UDATASHR)) {
303                 pic16_sectioninfo.at_udatashr = Safe_strdup( getStringArg(AT_UDATASHR, argv, i, *pargc));
304                 return TRUE;
305         }
306 */
307
308 #if 0
309         if(ISOPT(ADDR_CODE)) {
310                 pic16_sectioninfo.addr_code = getIntArg(ADDR_CODE, argv, i, *pargc);
311                 return TRUE;
312         }
313         
314         if(ISOPT(ADDR_IDATA)) {
315                 pic16_sectioninfo.addr_idata = getIntArg(ADDR_IDATA, argv, i, *pargc);
316                 return TRUE;
317         }
318         
319         if(ISOPT(ADDR_UDATA)) {
320                 pic16_sectioninfo.addr_udata = getIntArg(ADDR_UDATA, argv, i, *pargc);
321                 return TRUE;
322         }
323         
324         if(ISOPT(ADDR_UDATAACS)) {
325                 pic16_sectioninfo.addr_udataacs = getIntArg(ADDR_UDATAACS, argv, i, *pargc);
326                 return TRUE;
327         }
328         
329         if(ISOPT(ADDR_UDATAOVR)) {
330                 pic16_sectioninfo.addr_udataovr = getIntArg(ADDR_UDATAOVR, argv, i, *pargc);
331                 return TRUE;
332         }
333         
334         if(ISOPT(ADDR_UDATASHR)) {
335                 pic16_sectioninfo.addr_udatashr = getIntArg(ADDR_UDATASHR, argv, i, *pargc);
336                 return TRUE;
337         }
338
339         tset = newSet();
340         
341         if(ISOPT(NAME_CODE)) {
342                 setParseWithComma(&tset, getStringArg(NAME_CODE, argv, i, *pargc));
343                 if(elementsInSet(tset) > 0)
344                         pic16_sectioninfo.name_code = Safe_strdup( (char *)indexSet(tset, 0));
345                 if(elementsInSet(tset) > 1)
346                         pic16_sectioninfo.addr_code = atoi( (char *)indexSet(tset, 1));
347                 return TRUE;
348         }
349
350         if(ISOPT(NAME_IDATA)) {
351                 setParseWithComma(&tset, getStringArg(NAME_IDATA, argv, i, *pargc));
352                 if(elementsInSet(tset) > 0)
353                         pic16_sectioninfo.name_idata = Safe_strdup( (char *)indexSet(tset, 0));
354                 if(elementsInSet(tset) > 1)
355                         pic16_sectioninfo.addr_idata = atoi( (char *)indexSet(tset, 1));
356                 return TRUE;
357         }
358
359         if(ISOPT(NAME_UDATA)) {
360                 setParseWithComma(&tset, getStringArg(NAME_UDATA, argv, i, *pargc));
361                 if(elementsInSet(tset) > 0)
362                         pic16_sectioninfo.name_udata = Safe_strdup( (char *)indexSet(tset, 0));
363                 if(elementsInSet(tset) > 1)
364                         pic16_sectioninfo.addr_udata = atoi( (char *)indexSet(tset, 1));
365                 return TRUE;
366         }
367
368         if(ISOPT(NAME_UDATAACS)) {
369                 setParseWithComma(&tset, getStringArg(NAME_UDATAACS, argv, i, *pargc));
370                 if(elementsInSet(tset) > 0)
371                         pic16_sectioninfo.name_udataacs = Safe_strdup( (char *)indexSet(tset, 0));
372                 if(elementsInSet(tset) > 1)
373                         pic16_sectioninfo.addr_udataacs = atoi( (char *)indexSet(tset, 1));
374                 return TRUE;
375         }
376
377         if(ISOPT(NAME_UDATAOVR)) {
378                 setParseWithComma(&tset, getStringArg(NAME_UDATAOVR, argv, i, *pargc));
379                 if(elementsInSet(tset) > 0)
380                         pic16_sectioninfo.name_udataovr = Safe_strdup( (char *)indexSet(tset, 0));
381                 if(elementsInSet(tset) > 1)
382                         pic16_sectioninfo.addr_udataovr = atoi( (char *)indexSet(tset, 1));
383                 return TRUE;
384         }
385
386         if(ISOPT(NAME_UDATASHR)) {
387                 setParseWithComma(&tset, getStringArg(NAME_UDATASHR, argv, i, *pargc));
388                 if(elementsInSet(tset) > 0)
389                         pic16_sectioninfo.name_udatashr = Safe_strdup( (char *)indexSet(tset, 0));
390                 if(elementsInSet(tset) > 1)
391                         pic16_sectioninfo.addr_udatashr = atoi( (char *)indexSet(tset, 1));
392                 return TRUE;
393         }
394
395         deleteSet( &tset );
396 #endif
397
398   return FALSE;
399 }
400
401 static void
402 _pic16_finaliseOptions (void)
403 {
404         port->mem.default_local_map = data;
405         port->mem.default_globl_map = data;
406
407         options.all_callee_saves = 1;           // always callee saves
408
409         setMainValue("mcu", pic16_processor_base_name() );
410         addSet(&preArgvSet, Safe_strdup("-DMCU={mcu}"));
411 }
412
413
414 /* all the rest is commented ifdef'd out */
415 #if 0
416   /* Hack-o-matic: if we are using the flat24 model,
417    * adjust pointer sizes.
418    */
419   if (options.model == MODEL_FLAT24)
420     {
421
422       fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
423                "for DS80C390 support. This code generator is "
424                "badly out of date and probably broken.\n");
425
426       port->s.fptr_size = 3;
427       port->s.gptr_size = 4;
428       port->stack.isr_overhead++;       /* Will save dpx on ISR entry. */
429 #if 1
430       port->stack.call_overhead++;      /* This acounts for the extra byte 
431                                          * of return addres on the stack.
432                                          * but is ugly. There must be a 
433                                          * better way.
434                                          */
435 #endif
436       fReturn = fReturn390;
437       fReturnSize = 5;
438     }
439
440   if (options.model == MODEL_LARGE)
441     {
442       port->mem.default_local_map = xdata;
443       port->mem.default_globl_map = xdata;
444     }
445   else
446     {
447       port->mem.default_local_map = data;
448       port->mem.default_globl_map = data;
449     }
450
451   if (options.stack10bit)
452     {
453       if (options.model != MODEL_FLAT24)
454         {
455           fprintf (stderr,
456                    "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
457           fprintf (stderr, "\t10 bit stack mode disabled.\n");
458           options.stack10bit = 0;
459         }
460       else
461         {
462           /* Fixup the memory map for the stack; it is now in
463            * far space and requires a FPOINTER to access it.
464            */
465           istack->fmap = 1;
466           istack->ptrType = FPOINTER;
467         }
468     }
469 #endif
470
471
472 static void
473 _pic16_setDefaultOptions (void)
474 {
475         /* initialize to defaults section locations, names and addresses */
476         pic16_sectioninfo.at_udata      = "udata";
477         pic16_sectioninfo.at_udataacs   = "udata_acs";
478         pic16_sectioninfo.at_udataovr   = "udata_ovr";
479         pic16_sectioninfo.at_udatashr   = "udata_shr";
480
481         /* initialize to nothing, so let linker decide about their names */
482         pic16_sectioninfo.name_code     =
483         pic16_sectioninfo.name_idata    =
484         pic16_sectioninfo.name_udata    =
485         pic16_sectioninfo.name_udataacs =
486         pic16_sectioninfo.name_udataovr =
487         pic16_sectioninfo.name_udatashr = "";
488
489         /* initialize to -1, so let linker decide about their address */
490         pic16_sectioninfo.addr_code     =
491         pic16_sectioninfo.addr_idata    =
492         pic16_sectioninfo.addr_udata    =
493         pic16_sectioninfo.addr_udataacs =
494         pic16_sectioninfo.addr_udataovr =
495         pic16_sectioninfo.addr_udatashr = -1;
496 }
497
498 static const char *
499 _pic16_getRegName (struct regs *reg)
500 {
501   if (reg)
502     return reg->name;
503   return "err";
504 }
505
506
507 #if 1
508 static  char *_pic16_mangleFunctionName(char *sz)
509 {
510 //      fprintf(stderr, "mangled function name: %s\n", sz);
511
512   return sz;
513 }
514 #endif
515
516
517 static void
518 _pic16_genAssemblerPreamble (FILE * of)
519 {
520   char *name = pic16_processor_base_name();
521
522         if(!name) {
523                 name = "p18f452";
524                 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
525         }
526
527         fprintf (of, "\tlist\tp=%s\n",&name[1]);
528
529         if(!pic16_options.omit_configw) {
530                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300001, pic16_getConfigWord(0x300001));
531                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300002, pic16_getConfigWord(0x300002));
532                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300003, pic16_getConfigWord(0x300003));
533                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300005, pic16_getConfigWord(0x300005));
534                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300006, pic16_getConfigWord(0x300006));
535                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300008, pic16_getConfigWord(0x300008));
536                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300009, pic16_getConfigWord(0x300009));
537                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000a, pic16_getConfigWord(0x30000a));
538                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000b, pic16_getConfigWord(0x30000b));
539                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000c, pic16_getConfigWord(0x30000c));
540                 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000d, pic16_getConfigWord(0x30000d));
541         }
542         
543   fprintf (of, "\tradix dec\n");
544 }
545
546 /* Generate interrupt vector table. */
547 static int
548 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
549 {
550         /* PIC18F family has only two interrupts, the high and the low
551          * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
552
553         if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
554                 fprintf(of, "; RESET vector\n");
555                 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
556         }
557         
558         if(!pic16_options.omit_ivt) {
559                 fprintf(of, "\tres 4\n");
560
561
562                 fprintf(of, "; High priority interrupt vector 0x0008\n");
563                 if(interrupts[1]) {
564                         fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
565                         fprintf(of, "\tres\t12\n"); 
566                 } else {
567                         fprintf(of, "\tretfie\n");
568                         fprintf(of, "\tres\t14\n");
569                 }
570
571                 fprintf(of, "; Low priority interrupt vector 0x0018\n");
572                 if(interrupts[2]) {
573                         fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
574                 } else {
575                         fprintf(of, "\tretfie\n");
576                 }
577         }
578
579   return TRUE;
580 }
581
582 static bool
583 _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
584 {
585   //  sym_link *test = NULL;
586   //  value *val;
587
588   fprintf(stderr,"checking for native mult\n");
589
590   if ( ic->op != '*')
591     {
592       return FALSE;
593     }
594
595   return TRUE;
596 /*
597   if ( IS_LITERAL (left))
598     {
599       fprintf(stderr,"left is lit\n");
600       test = left;
601       val = OP_VALUE (IC_LEFT (ic));
602     }
603   else if ( IS_LITERAL (right))
604     {
605       fprintf(stderr,"right is lit\n");
606       test = left;
607       val = OP_VALUE (IC_RIGHT (ic));
608     }
609   else
610     {
611       fprintf(stderr,"oops, neither is lit so no\n");
612       return FALSE;
613     }
614
615   if ( getSize (test) <= 2)
616     {
617       fprintf(stderr,"yep\n");
618       return TRUE;
619     }
620   fprintf(stderr,"nope\n");
621
622   return FALSE;
623 */
624 }
625
626 /* Indicate which extended bit operations this port supports */
627 static bool
628 hasExtBitOp (int op, int size)
629 {
630   if (op == RRC
631       || op == RLC
632       /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
633      )
634     return TRUE;
635   else
636     return FALSE;
637 }
638
639 /* Indicate the expense of an access to an output storage class */
640 static int
641 oclsExpense (struct memmap *oclass)
642 {
643   /* The IN_FARSPACE test is compatible with historical behaviour, */
644   /* but I don't think it is applicable to PIC. If so, please feel */
645   /* free to remove this test -- EEP */
646   if (IN_FARSPACE(oclass))
647     return 1;
648     
649   return 0;
650 }
651
652 /** $1 is always the basename.
653     $2 is always the output file.
654     $3 varies
655     $l is the list of extra options that should be there somewhere...
656     MUST be terminated with a NULL.
657 */
658 static const char *_linkCmd[] =
659 {
660   "gplink", "\"$1.o\"", "-o $1", "$l", NULL
661 };
662
663 /* Sigh. This really is not good. For now, I recommend:
664  * sdcc -S -mpic16 file.c
665  * the -S option does not compile or link
666  */
667 static const char *_asmCmd[] =
668 {
669   "gpasm", "$l", "-c", "\"$1.asm\"", NULL
670
671 };
672
673 /* Globals */
674 PORT pic16_port =
675 {
676   TARGET_ID_PIC16,
677   "pic16",
678   "MCU PIC16",                  /* Target name */
679   "p18f452",                    /* Processor */
680   {
681     pic16glue,
682     TRUE,                       /* Emit glue around main */
683     MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
684     MODEL_SMALL
685   },
686   {
687     _asmCmd,                    /* assembler command and arguments */
688     NULL,                       /* alternate macro based form */
689     NULL,                       /* arguments for debug mode */
690     NULL,                       /* arguments for normal mode */
691     0,                          /* print externs as global */
692     ".asm",                     /* assembler file extension */
693     NULL                        /* no do_assemble function */
694   },
695   {
696     _linkCmd,                   /* linker command and arguments */
697     NULL,                       /* alternate macro based form */
698     NULL,                       /* no do_link function */
699     ".o",                       /* extension for object files */
700     0                           /* no need for linker file */
701   },
702   {
703     _defaultRules
704   },
705   {
706         /* Sizes */
707     1,          /* char */
708     2,          /* short */
709     2,          /* int */
710     4,          /* long */
711     2,          /* ptr */
712     3,          /* fptr, far pointers (see Microchip) */
713     2,          /* gptr */
714     1,          /* bit */
715     4,          /* float */
716     4           /* max */
717   },
718   {
719     "XSEG    (XDATA)",          // xstack
720     "STACK   (DATA)",           // istack
721     "CSEG    (CODE)",           // code
722     "DSEG    (DATA)",           // data
723     "ISEG    (DATA)",           // idata
724     "XSEG    (XDATA)",          // xdata
725     "BSEG    (BIT)",            // bit
726     "RSEG    (DATA)",           // reg
727     "GSINIT  (CODE)",           // static
728     "OSEG    (OVR,DATA)",       // overlay
729     "GSFINAL (CODE)",           // post static
730     "HOME        (CODE)",       // home
731     NULL,                       // xidata
732     NULL,                       // xinit
733     NULL,                       // default location for auto vars
734     NULL,                       // default location for global vars
735     1                           // code is read only 1=yes
736   },
737   {
738     NULL,               /* genExtraAreaDeclaration */
739     NULL                /* genExatrAreaLinkOptions */
740   },
741   {
742         /* stack related information */
743     -1,                 /* -1 stack grows downwards, +1 upwards */
744     1,                  /* extra overhead when calling between banks */
745     4,                  /* extra overhead when the function is an ISR */
746     1,                  /* extra overhead for a function call */
747     1,                  /* re-entrant space */
748     0                   /* 'banked' call overhead, mild overlap with bank_overhead */
749   },
750     /* pic16 has an 8 bit mul */
751   {
752     1, -1
753   },
754   "_",
755   _pic16_init,
756   _pic16_parseOptions,
757   pic16_optionsTable,
758   _pic16_finaliseOptions,
759   _pic16_setDefaultOptions,
760   pic16_assignRegisters,
761   _pic16_getRegName,
762   _pic16_keywords,
763   _pic16_genAssemblerPreamble,
764   NULL,                         /* no genAssemblerEnd */
765   _pic16_genIVT,
766   NULL, // _pic16_genXINIT
767   _pic16_reset_regparm,
768   _pic16_regparm,
769   _process_pragma,                              /* process a pragma */
770   _pic16_mangleFunctionName,                            /* mangles function name */
771   _hasNativeMulFor,
772   hasExtBitOp,                  /* hasExtBitOp */
773   oclsExpense,                  /* oclsExpense */
774   FALSE,                        
775   TRUE,                         /* little endian */
776   0,                            /* leave lt */
777   0,                            /* leave gt */
778   1,                            /* transform <= to ! > */
779   1,                            /* transform >= to ! < */
780   1,                            /* transform != to !(a == b) */
781   0,                            /* leave == */
782   FALSE,                        /* No array initializer support. */
783   0,                            /* no CSE cost estimation yet */
784   NULL,                         /* no builtin functions */
785   GPOINTER,                     /* treat unqualified pointers as "generic" pointers */
786   1,                            /* reset labelKey to 1 */
787   1,                            /* globals & local static allowed */
788   PORT_MAGIC
789 };