* doc/sdccman.lyx: updated SDCC version,
[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 "pcode.h"
34 //#include "gen.h"
35
36
37 static char _defaultRules[] =
38 {
39 #include "peeph.rul"
40 };
41
42 /* list of key words used by pic16 */
43 static char *_pic16_keywords[] =
44 {
45   "at",
46   "bit",
47   "code",
48   "critical",
49   "register",
50   "data",
51   "far",
52   "idata",
53   "interrupt",
54   "near",
55   "pdata",
56   "reentrant",
57   "sfr",
58   "sbit",
59   "using",
60   "xdata",
61   "_data",
62   "_code",
63   "_generic",
64   "_near",
65   "_xdata",
66   "_pdata",
67   "_idata",
68   "_naked",
69   NULL
70 };
71
72
73 pic16_sectioninfo_t pic16_sectioninfo;
74
75
76 extern char *pic16_processor_base_name(void);
77
78 void  pic16_pCodeInitRegisters(void);
79
80 void pic16_assignRegisters (eBBlock ** ebbs, int count);
81
82 static int regParmFlg = 0;      /* determine if we can register a parameter */
83
84 pic16_options_t pic16_options;
85
86 extern set *includeDirsSet;
87 extern set *dataDirsSet;
88 extern set *libFilesSet;
89
90 /* Also defined in gen.h, but the #include is commented out */
91 /* for an unknowned reason. - EEP */
92 void pic16_emitDebuggerSymbol (char *);
93  
94 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
95 extern void pic16_emitConfigRegs(FILE *of);
96 extern void pic16_emitIDRegs(FILE *of);
97
98
99
100 static void
101 _pic16_init (void)
102 {
103   asm_addTree (&asm_asxxxx_mapping);
104   pic16_pCodeInitRegisters();
105   maxInterrupts = 2;
106 }
107
108 static void
109 _pic16_reset_regparm (void)
110 {
111   regParmFlg = 0;
112 }
113
114 static int
115 _pic16_regparm (sym_link * l)
116 {
117   /* force all parameters via SEND/RECEIVE */
118   if(0 /*pic16_options.ip_stack*/) {
119     /* for this processor it is simple
120      * can pass only the first parameter in a register */
121     if(regParmFlg)return 0;
122       regParmFlg++;
123       return 1; //regParmFlg;
124   } else {
125     /* otherwise pass all arguments in registers via SEND/RECEIVE */
126     regParmFlg++;// = 1;
127     return regParmFlg;
128   }
129 }
130
131
132 int initsfpnt=0;                /* set to 1 if source provides a pragma for stack
133                                  * so glue() later emits code to initialize stack/frame pointers */
134 set *absSymSet;
135
136 set *sectNames=NULL;                    /* list of section listed in pragma directives */
137 set *sectSyms=NULL;                     /* list of symbols set in a specific section */
138
139
140 static int
141 _process_pragma(const char *sz)
142 {
143   static const char *WHITE = " \t\n";
144   
145   char  *ptr = strtok((char *)sz, WHITE);
146
147         /* #pragma maxram [maxram] */
148         if (startsWith (ptr, "maxram")) {
149           char *maxRAM = strtok((char *)NULL, WHITE);
150
151                 if (maxRAM != (char *)NULL) {
152                   int maxRAMaddress;
153                   value *maxRAMVal;
154
155                         maxRAMVal = constVal(maxRAM);
156                         maxRAMaddress = (int)floatFromVal(maxRAMVal);
157                         pic16_setMaxRAM(maxRAMaddress);
158                 }
159
160           return 0;
161         }
162         
163         /* #pragma stack [stack-position] [stack-len] */
164         if(startsWith(ptr, "stack")) {
165           char *stackPosS = strtok((char *)NULL, WHITE);
166           char *stackLenS = strtok((char *)NULL, WHITE);
167           value *stackPosVal;
168           value *stackLenVal;
169           regs *reg;
170           symbol *sym;
171
172                 stackPosVal = constVal( stackPosS );
173                 stackPos = (unsigned int)floatFromVal( stackPosVal );
174
175                 
176                 if(stackLenS) {
177                         stackLenVal = constVal( stackLenS );
178                         stackLen = (unsigned int)floatFromVal( stackLenVal );
179                 }
180
181                 if(stackLen < 1) {
182                         stackLen = 64;
183                         fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
184                                       filename, lineno-1, stackLen, stackLen);
185                         
186 //                      fprintf(stderr, "%s:%d setting stack to default size %d\n", __FILE__, __LINE__, stackLen);
187                 }
188
189 //              fprintf(stderr, "Initializing stack pointer at 0x%x len 0x%x\n", stackPos, stackLen);
190                                 
191                 reg=newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
192                 addSet(&pic16_fix_udata, reg);
193                 
194                 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
195                 addSet(&pic16_fix_udata, reg);
196
197                 sym = newSymbol("stack", 0);
198                 sprintf(sym->rname, "_%s", sym->name);
199                 addSet(&publics, sym);
200
201                 sym = newSymbol("stack_end", 0);
202                 sprintf(sym->rname, "_%s", sym->name);
203                 addSet(&publics, sym);
204                 
205                 initsfpnt = 1;          // force glue() to initialize stack/frame pointers */
206
207           return 0;
208         }
209         
210         /* #pragma code [symbol] [location] */
211         if(startsWith(ptr, "code")) {
212           char *symname = strtok((char *)NULL, WHITE);
213           char *location = strtok((char *)NULL, WHITE);
214           absSym *absS;
215           value *addr;
216
217                 absS = Safe_calloc(1, sizeof(absSym));
218                 sprintf(absS->name, "_%s", symname);
219                 
220                 addr = constVal( location );
221                 absS->address = (unsigned int)floatFromVal( addr );
222
223                 if((absS->address % 2) != 0) {
224                   absS->address--;
225                   fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
226                                       filename, lineno-1, absS->address);
227                 }
228
229                 addSet(&absSymSet, absS);
230 //              fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
231 //                      __FILE__, __LINE__, symname, absS->address);
232
233           return 0;
234         }
235
236         /* #pragma udata [section-name] [symbol] */
237         if(startsWith(ptr, "udata")) {
238           char *sectname = strtok((char *)NULL, WHITE);
239           char *symname = strtok((char *)NULL, WHITE);
240           symbol *nsym;
241           sectSym *ssym;
242           sectName *snam;
243           int found=0;
244           
245                 while(symname) {
246                         ssym = Safe_calloc(1, sizeof(sectSyms));
247                         ssym->name = Safe_calloc(1, strlen(symname)+2);
248                         sprintf(ssym->name, "_%s", symname);
249                         ssym->reg = NULL;
250
251                         addSet(&sectSyms, ssym);
252
253                         nsym = newSymbol(symname, 0);
254                         strcpy(nsym->rname, ssym->name);
255                         checkAddSym(&publics, nsym);
256
257                         found = 0;
258                         for(snam=setFirstItem(sectNames);snam;snam=setNextItem(sectNames)) {
259                                 if(!strcmp(sectname, snam->name)){ found=1; break; }
260                         }
261                         
262                         if(!found) {
263                                 snam = Safe_calloc(1, sizeof(sectNames));
264                                 snam->name = Safe_strdup( sectname );
265                                 snam->regsSet = NULL;
266                                 
267                                 addSet(&sectNames, snam);
268                         }
269                         
270                         ssym->section = snam;
271                                 
272 //                      fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
273 //                              ssym->name, snam->name, snam);
274
275                         symname = strtok((char *)NULL, WHITE);
276                 }
277
278           return 0;
279         }
280         
281   return 1;
282 }
283
284 #define REP_UDATA       "--preplace-udata-with="
285
286 #define STACK_MODEL     "--pstack-model="
287 #define OPT_BANKSEL     "--obanksel="
288
289 #define ALT_ASM         "--asm="
290 #define ALT_LINK        "--link="
291
292 #define IVT_LOC         "--ivt-loc"
293 #define NO_DEFLIBS      "--nodefaultlibs"
294 #define MPLAB_COMPAT    "--mplab-comp"
295
296 #define NL_OPT          "--nl="
297 #define USE_CRT         "--use-crt="
298
299 #define OFMSG_LRSUPPORT "--flr-support"
300
301 char *alt_asm=NULL;
302 char *alt_link=NULL;
303
304 int pic16_mplab_comp=0;
305 extern int pic16_debug_verbose;
306 extern int pic16_ralloc_debug;
307 extern int pic16_pcode_verbose;
308
309 int pic16_fstack=0;
310 int pic16_enable_peeps=0;
311 int pic16_nl=0;                 /* 0 for LF, 1 for CRLF */
312
313 OPTION pic16_optionsTable[]= {
314         { 0,    NO_DEFLIBS,             &pic16_options.nodefaultlibs,   "do not link default libraries when linking"},
315         { 0,    "--pno-banksel",        &pic16_options.no_banksel,      "do not generate BANKSEL assembler directives"},
316         { 0,    OPT_BANKSEL,            NULL,                           "set banksel optimization level (default=0 no)"},
317         { 0,    "--pomit-config-words", &pic16_options.omit_configw,    "omit the generation of configuration words"},
318         { 0,    "--pomit-ivt",          &pic16_options.omit_ivt,        "omit the generation of the Interrupt Vector Table"},
319         { 0,    "--pleave-reset-vector",&pic16_options.leave_reset,     "when omitting IVT leave RESET vector"},
320         { 0,    STACK_MODEL,            NULL,                           "use stack model 'small' (default) or 'large'"},
321
322         { 0,    "--debug-xtra",         &pic16_debug_verbose,   "show more debug info in assembly output"},
323         { 0,    "--debug-ralloc",       &pic16_ralloc_debug,    "dump register allocator debug file *.d"},
324         { 0,    "--pcode-verbose",      &pic16_pcode_verbose,   "dump pcode related info"},
325                 
326         { 0,    REP_UDATA,      NULL,   "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
327
328         { 0,    ALT_ASM,        NULL,   "Use alternative assembler"},
329         { 0,    ALT_LINK,       NULL,   "Use alternative linker"},
330
331         { 0,    "--denable-peeps",      &pic16_enable_peeps,    "explicit enable of peepholes"},
332         { 0,    IVT_LOC,        NULL,   "<nnnn> interrupt vector table location"},
333         { 0,    "--calltree",           &pic16_options.dumpcalltree,    "dump call tree in .calltree file"},
334         { 0,    MPLAB_COMPAT,           &pic16_mplab_comp,      "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
335         { 0,    "--fstack",             &pic16_fstack,          "enable stack optimizations"},
336         { 0,    NL_OPT,         NULL,                           "new line, \"lf\" or \"crlf\""},
337         { 0,    USE_CRT,        NULL,   "use <crt-o> run-time initialization module"},
338         { 0,    "--no-crt",     &pic16_options.no_crt,  "do not link any default run-time initialization module"},
339         { 0,    "--gstack",     &pic16_options.gstack,  "trace stack pointer push/pop to overflow"},
340         { 0,    OFMSG_LRSUPPORT,        NULL,           "use support functions for local register store/restore"},
341         { 0,    NULL,           NULL,   NULL}
342         };
343
344
345 #define ISOPT(str)      !strncmp(argv[ *i ], str, strlen(str) )
346
347 extern char *getStringArg(const char *,  char **, int *, int);
348 extern int getIntArg(const char *, char **, int *, int);
349
350 static bool
351 _pic16_parseOptions (int *pargc, char **argv, int *i)
352 {
353   int j=0;
354   char *stkmodel;
355   
356   /* TODO: allow port-specific command line options to specify
357    * segment names here.
358    */
359   
360     /* check for arguments that have associated an integer variable */
361     while(pic16_optionsTable[j].pparameter) {
362       if(ISOPT( pic16_optionsTable[j].longOpt )) {
363         (*pic16_optionsTable[j].pparameter)++;
364         return TRUE;
365       }
366       j++;
367     }
368
369     if(ISOPT(STACK_MODEL)) {
370       stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
371       if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
372       else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
373       else {
374         fprintf(stderr, "Unknown stack model: %s", stkmodel);
375         exit(-1);
376       }
377       return TRUE;
378     }
379
380     if(ISOPT(OPT_BANKSEL)) {
381       pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
382       return TRUE;
383     }
384
385     if(ISOPT(REP_UDATA)) {
386       pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
387       return TRUE;
388     }
389         
390     if(ISOPT(ALT_ASM)) {
391       alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
392       return TRUE;
393     }
394         
395     if(ISOPT(ALT_LINK)) {
396       alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
397       return TRUE;
398     }
399
400     if(ISOPT(IVT_LOC)) {
401       pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
402       return TRUE;
403     }
404         
405     if(ISOPT(NL_OPT)) {
406       char *tmp;
407             
408         tmp = Safe_strdup( getStringArg(NL_OPT, argv, i, *pargc) );
409         if(!STRCASECMP(tmp, "lf"))pic16_nl = 0;
410         else if(!STRCASECMP(tmp, "crlf"))pic16_nl = 1;
411         else {
412           fprintf(stderr, "invalid termination character id\n");
413           exit(-1);
414         }
415         return TRUE;
416     }
417
418     if(ISOPT(USE_CRT)) {
419       pic16_options.no_crt = 0;
420       pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
421
422       return TRUE;
423     }
424
425     if(ISOPT(OFMSG_LRSUPPORT)) {
426       pic16_options.opt_flags |= OF_LR_SUPPORT;
427       return TRUE;
428     }
429         
430   return FALSE;
431 }
432
433 extern set *userIncDirsSet;
434
435 static void _pic16_initPaths(void)
436 {
437   char pic16incDir[512];
438   char pic16libDir[512];
439   set *pic16incDirsSet=NULL;
440   set *pic16libDirsSet=NULL;
441   char devlib[512];
442
443     setMainValue("mcu", pic16->name[2] );
444     addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
445
446     sprintf(pic16incDir, "%s%cpic16", INCLUDE_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
447     sprintf(pic16libDir, "%s%cpic16", LIB_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
448
449
450     if(!options.nostdinc) {
451       /* setup pic16 include directory */
452       pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
453       includeDirsSet = pic16incDirsSet;
454 //      mergeSets(&includeDirsSet, pic16incDirsSet);
455     }
456     /* pic16 port should not search to the SDCC standard include directories,
457      * so add here the deleted include dirs that user has issued in command line */
458     mergeSets(&pic16incDirsSet, userIncDirsSet);
459
460     if(!options.nostdlib) {
461       /* setup pic16 library directory */
462       pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
463       libDirsSet = pic16libDirsSet;
464 //      mergeSets(&libDirsSet, pic16libDirsSet);
465     }
466
467     if(!pic16_options.nodefaultlibs) {
468       /* now add the library for the device */
469       sprintf(devlib, "%s.lib", pic16->name[2]);
470       addSet(&libFilesSet, Safe_strdup(devlib));
471
472       /* add the internal SDCC library */
473       addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
474     }
475 }
476
477 extern set *linkOptionsSet;
478 char *msprintf(hTab *pvals, const char *pformat, ...);
479 int my_system(const char *cmd);
480
481 /* custom function to link objects */
482 static void _pic16_linkEdit(void)
483 {
484   hTab *linkValues=NULL;
485   char lfrm[256];
486   char *lcmd;
487   char temp[128];
488   set *tSet=NULL;
489   int ret;
490   
491         /*
492          * link command format:
493          * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
494          *
495          */
496          
497         sprintf(lfrm, "{linker} {incdirs} {lflags} -o {outfile} {user_ofile} {spec_ofiles} {ofiles} {libs}");
498                  
499         shash_add(&linkValues, "linker", "gplink");
500
501         mergeSets(&tSet, libDirsSet);
502         mergeSets(&tSet, libPathsSet);
503         
504         shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
505         shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
506   
507         shash_add(&linkValues, "outfile", dstFileName);
508
509         if(fullSrcFileName) {
510                 sprintf(temp, "%s.o", dstFileName);
511 //              addSetHead(&relFilesSet, Safe_strdup(temp));
512                 shash_add(&linkValues, "user_ofile", temp);
513         }
514
515         if(!pic16_options.no_crt)
516           shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
517
518         shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
519         shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
520         
521         lcmd = msprintf(linkValues, lfrm);
522          
523         ret = my_system( lcmd );
524          
525         Safe_free( lcmd );
526          
527         if(ret)
528                 exit(1);
529 }
530
531
532 /* forward declarations */
533 extern const char *pic16_linkCmd[];
534 extern const char *pic16_asmCmd[];
535
536 static void
537 _pic16_finaliseOptions (void)
538 {
539     port->mem.default_local_map = data;
540     port->mem.default_globl_map = data;
541
542     /* peepholes are disabled for the time being */
543     options.nopeep = 1;
544
545     /* explicit enable peepholes for testing */
546     if(pic16_enable_peeps)
547       options.nopeep = 0;
548
549     options.all_callee_saves = 1;               // always callee saves
550
551 #if 0
552     options.float_rent = 1;
553     options.intlong_rent = 1;
554 #endif
555         
556
557     if(alt_asm && strlen(alt_asm))
558       pic16_asmCmd[0] = alt_asm;
559         
560     if(alt_link && strlen(alt_link))
561       pic16_linkCmd[0] = alt_link;
562         
563     if(!pic16_options.no_crt) {
564       pic16_options.omit_ivt = 1;
565       pic16_options.leave_reset = 0;
566     }
567 }
568
569
570 #if 0
571   if (options.model == MODEL_LARGE)
572     {
573       port->mem.default_local_map = xdata;
574       port->mem.default_globl_map = xdata;
575     }
576   else
577     {
578       port->mem.default_local_map = data;
579       port->mem.default_globl_map = data;
580     }
581
582   if (options.stack10bit)
583     {
584       if (options.model != MODEL_FLAT24)
585         {
586           fprintf (stderr,
587                    "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
588           fprintf (stderr, "\t10 bit stack mode disabled.\n");
589           options.stack10bit = 0;
590         }
591       else
592         {
593           /* Fixup the memory map for the stack; it is now in
594            * far space and requires a FPOINTER to access it.
595            */
596           istack->fmap = 1;
597           istack->ptrType = FPOINTER;
598         }
599     }
600 #endif
601
602
603 static void
604 _pic16_setDefaultOptions (void)
605 {
606   options.stackAuto = 0;                /* implicit declaration */
607   /* port is not capable yet to allocate separate registers 
608    * dedicated for passing certain parameters */
609   
610   /* initialize to defaults section locations, names and addresses */
611   pic16_sectioninfo.at_udata    = "udata";
612
613   /* set pic16 port options to defaults */
614   pic16_options.no_banksel = 0;
615   pic16_options.opt_banksel = 0;
616   pic16_options.omit_configw = 0;
617   pic16_options.omit_ivt = 0;
618   pic16_options.leave_reset = 0;
619   pic16_options.stack_model = 0;                        /* 0 for 'small', 1 for 'large' */
620   pic16_options.ivt_loc = 0x000000;
621   pic16_options.nodefaultlibs = 0;
622   pic16_options.dumpcalltree = 0;
623   pic16_options.crt_name = "crt0i.o";           /* the default crt to link */
624   pic16_options.no_crt = 0;                     /* use crt by default */
625   pic16_options.ip_stack = 1;           /* set to 1 to enable ipop/ipush for stack */
626   pic16_options.gstack = 0;
627 }
628
629 static const char *
630 _pic16_getRegName (struct regs *reg)
631 {
632   if (reg)
633     return reg->name;
634   return "err";
635 }
636
637
638 #if 1
639 static  char *_pic16_mangleFunctionName(char *sz)
640 {
641 //      fprintf(stderr, "mangled function name: %s\n", sz);
642
643   return sz;
644 }
645 #endif
646
647
648 static void
649 _pic16_genAssemblerPreamble (FILE * of)
650 {
651   char *name = pic16_processor_base_name();
652
653         if(!name) {
654                 name = "p18f452";
655                 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
656         }
657
658         fprintf (of, "\tlist\tp=%s\n",&name[1]);
659
660         if(!pic16_options.omit_configw) {
661                 pic16_emitConfigRegs(of);
662                 fprintf(of, "\n");
663                 pic16_emitIDRegs(of);
664         }
665         
666   fprintf (of, "\tradix dec\n");
667 }
668
669 /* Generate interrupt vector table. */
670 static int
671 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
672 {
673 #if 1
674         /* PIC18F family has only two interrupts, the high and the low
675          * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
676
677         if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
678                 fprintf(of, "; RESET vector\n");
679                 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
680         }
681         
682         if(!pic16_options.omit_ivt) {
683                 fprintf(of, "\tres 4\n");
684
685
686                 fprintf(of, "; High priority interrupt vector 0x0008\n");
687                 if(interrupts[1]) {
688                         fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
689                         fprintf(of, "\tres\t12\n"); 
690                 } else {
691                         fprintf(of, "\tretfie\n");
692                         fprintf(of, "\tres\t14\n");
693                 }
694
695                 fprintf(of, "; Low priority interrupt vector 0x0018\n");
696                 if(interrupts[2]) {
697                         fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
698                 } else {
699                         fprintf(of, "\tretfie\n");
700                 }
701         }
702 #endif
703   return TRUE;
704 }
705
706 /* return True if the port can handle the type,
707  * False to convert it to function call */
708 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
709 {
710 //      fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
711
712 #if 1
713         /* multiplication is fixed */
714         /* support mul for char/int/long */
715         if((ic->op == '*')
716           && (getSize(OP_SYMBOL(IC_LEFT(ic))->type ) < 2))return TRUE;
717 #endif
718
719 #if 0
720         /* support div for char/int/long */
721         if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) < 0)
722                 && (ic->op == '/'))return TRUE;
723 #endif
724         
725   return FALSE;
726 }
727
728
729 #if 0
730 /* Do CSE estimation */
731 static bool cseCostEstimation (iCode *ic, iCode *pdic)
732 {
733 //    operand *result = IC_RESULT(ic);
734 //    sym_link *result_type = operandType(result);
735
736
737         /* VR -- this is an adhoc. Put here after conversation
738          * with Erik Epetrich */
739
740         if(ic->op == '<'
741                 || ic->op == '>'
742                 || ic->op == EQ_OP) {
743
744                 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
745           return 0;
746         }
747
748 #if 0
749     /* if it is a pointer then return ok for now */
750     if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
751
752     /* if bitwise | add & subtract then no since mcs51 is pretty good at it
753        so we will cse only if they are local (i.e. both ic & pdic belong to
754        the same basic block */
755     if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
756         /* then if they are the same Basic block then ok */
757         if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
758         else return 0;
759     }
760 #endif
761
762     /* for others it is cheaper to do the cse */
763     return 1;
764 }
765 #endif
766
767
768 /* Indicate which extended bit operations this port supports */
769 static bool
770 hasExtBitOp (int op, int size)
771 {
772   if (op == RRC
773       || op == RLC
774       /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
775      )
776     return TRUE;
777   else
778     return FALSE;
779 }
780
781 /* Indicate the expense of an access to an output storage class */
782 static int
783 oclsExpense (struct memmap *oclass)
784 {
785   /* The IN_FARSPACE test is compatible with historical behaviour, */
786   /* but I don't think it is applicable to PIC. If so, please feel */
787   /* free to remove this test -- EEP */
788   if (IN_FARSPACE(oclass))
789     return 1;
790     
791   return 0;
792 }
793
794 /** $1 is the input object file (PIC16 specific)        // >>always the basename<<.
795     $2 is always the output file.
796     $3 -L path and -l libraries
797     $l is the list of extra options that should be there somewhere...
798     MUST be terminated with a NULL.
799 */
800 const char *pic16_linkCmd[] =
801 {
802   "gplink", "$l", "-o \"$2\"", "\"$1\"","$3", NULL
803 };
804
805
806
807 /** $1 is always the basename.
808     $2 is always the output file.
809     $3 varies (nothing currently)
810     $l is the list of extra options that should be there somewhere...
811     MUST be terminated with a NULL.
812 */
813 const char *pic16_asmCmd[] =
814 {
815   "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
816
817 };
818
819 /* Globals */
820 PORT pic16_port =
821 {
822   TARGET_ID_PIC16,
823   "pic16",
824   "MCU PIC16",                  /* Target name */
825   "p18f452",                    /* Processor */
826   {
827     pic16glue,
828     TRUE,                       /* Emit glue around main */
829     MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
830     MODEL_SMALL
831   },
832   {
833     pic16_asmCmd,               /* assembler command and arguments */
834     NULL,                       /* alternate macro based form */
835     "-g",                       /* arguments for debug mode */
836     NULL,                       /* arguments for normal mode */
837     0,                          /* print externs as global */
838     ".asm",                     /* assembler file extension */
839     NULL                        /* no do_assemble function */
840   },
841   {
842     NULL,                       //    pic16_linkCmd,            /* linker command and arguments */
843     NULL,                       /* alternate macro based form */
844     _pic16_linkEdit,            //NULL,                 /* no do_link function */
845     ".o",                       /* extension for object files */
846     0                           /* no need for linker file */
847   },
848   {
849     _defaultRules
850   },
851   {
852         /* Sizes */
853     1,          /* char */
854     2,          /* short */
855     2,          /* int */
856     4,          /* long */
857     2,          /* ptr */
858     3,          /* fptr, far pointers (see Microchip) */
859     3,          /* gptr */
860     1,          /* bit */
861     4,          /* float */
862     4           /* max */
863   },
864   {
865     "XSEG    (XDATA)",          // xstack
866     "STACK   (DATA)",           // istack
867     "CSEG    (CODE)",           // code
868     "DSEG    (DATA)",           // data
869     "ISEG    (DATA)",           // idata
870     NULL,                                       // pdata
871     "XSEG    (XDATA)",          // xdata
872     "BSEG    (BIT)",            // bit
873     "RSEG    (DATA)",           // reg
874     "GSINIT  (CODE)",           // static
875     "OSEG    (OVR,DATA)",       // overlay
876     "GSFINAL (CODE)",           // post static
877     "HOME        (CODE)",       // home
878     NULL,                       // xidata
879     NULL,                       // xinit
880     NULL,                       // default location for auto vars
881     NULL,                       // default location for global vars
882     1                           // code is read only 1=yes
883   },
884   {
885     NULL,               /* genExtraAreaDeclaration */
886     NULL                /* genExatrAreaLinkOptions */
887   },
888   {
889         /* stack related information */
890     -1,                 /* -1 stack grows downwards, +1 upwards */
891     1,                  /* extra overhead when calling between banks */
892     4,                  /* extra overhead when the function is an ISR */
893     1,                  /* extra overhead for a function call */
894     1,                  /* re-entrant space */
895     0                   /* 'banked' call overhead, mild overlap with bank_overhead */
896   },
897     /* pic16 has an 8 bit mul */
898   {
899      0, -1
900   },
901   {
902     pic16_emitDebuggerSymbol
903   },
904   {
905     255/3,      /* maxCount */
906     3,          /* sizeofElement */
907     /* The rest of these costs are bogus. They approximate */
908     /* the behavior of src/SDCCicode.c 1.207 and earlier.  */
909     {4,4,4},    /* sizeofMatchJump[] */
910     {0,0,0},    /* sizeofRangeCompare[] */
911     0,          /* sizeofSubtract */
912     3,          /* sizeofDispatch */
913   },
914   "_",
915   _pic16_init,
916   _pic16_parseOptions,
917   pic16_optionsTable,
918   _pic16_initPaths,
919   _pic16_finaliseOptions,
920   _pic16_setDefaultOptions,
921   pic16_assignRegisters,
922   _pic16_getRegName,
923   _pic16_keywords,
924   _pic16_genAssemblerPreamble,
925   NULL,                         /* no genAssemblerEnd */
926   _pic16_genIVT,
927   NULL, // _pic16_genXINIT
928   NULL,                         /* genInitStartup */
929   _pic16_reset_regparm,
930   _pic16_regparm,
931   _process_pragma,                              /* process a pragma */
932   _pic16_mangleFunctionName,                            /* mangles function name */
933   _hasNativeMulFor,
934   hasExtBitOp,                  /* hasExtBitOp */
935   oclsExpense,                  /* oclsExpense */
936   FALSE,                        
937   TRUE,                         /* little endian */
938   0,                            /* leave lt */
939   0,                            /* leave gt */
940   1,                            /* transform <= to ! > */
941   1,                            /* transform >= to ! < */
942   1,                            /* transform != to !(a == b) */
943   0,                            /* leave == */
944   FALSE,                        /* No array initializer support. */
945   0,    //cseCostEstimation,            /* !!!no CSE cost estimation yet */
946   NULL,                         /* no builtin functions */
947   GPOINTER,                     /* treat unqualified pointers as "generic" pointers */
948   1,                            /* reset labelKey to 1 */
949   1,                            /* globals & local static allowed */
950   PORT_MAGIC
951 };