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