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