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