1 /*-------------------------------------------------------------------------
3 main.c - pic16 specific general functions.
5 Written by - Scott Dattalo scott@dattalo.com
6 Ported to PIC16 by - Martin Dubuc m.debuc@rogers.com
8 Note that mlh prepended _pic16_ on the static functions. Makes
9 it easier to set a breakpoint using the debugger.
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
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.
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 -------------------------------------------------------------------------*/
37 static char _defaultRules[] =
42 /* list of key words used by pic16 */
43 static char *_pic16_keywords[] =
72 pic16_sectioninfo_t pic16_sectioninfo;
75 extern char *pic16_processor_base_name(void);
77 void pic16_pCodeInitRegisters(void);
79 void pic16_assignRegisters (eBBlock ** ebbs, int count);
81 static int regParmFlg = 0; /* determine if we can register a parameter */
83 pic16_options_t pic16_options;
85 extern set *includeDirsSet;
86 extern set *dataDirsSet;
87 extern set *libFilesSet;
89 /* Also defined in gen.h, but the #include is commented out */
90 /* for an unknowned reason. - EEP */
91 void pic16_emitDebuggerSymbol (char *);
98 char pic16incDir[512];
99 char pic16libDir[512];
100 set *pic16incDirsSet;
101 set *pic16libDirsSet;
105 asm_addTree (&asm_asxxxx_mapping);
106 pic16_pCodeInitRegisters();
109 /* set pic16 port options to defaults */
110 pic16_options.no_banksel = 0;
111 pic16_options.opt_banksel = 0;
112 pic16_options.omit_configw = 0;
113 pic16_options.omit_ivt = 0;
114 pic16_options.leave_reset = 0;
115 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
119 setMainValue("mcu", pic16->name[2] );
120 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
122 sprintf(pic16incDir, "%s/pic16", INCLUDE_DIR_SUFFIX);
123 sprintf(pic16libDir, "%s/pic16", LIB_DIR_SUFFIX);
125 if(!options.nostdinc) {
126 /* setup pic16 include directory */
127 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
128 mergeSets(&includeDirsSet, pic16incDirsSet);
131 if(!options.nostdlib) {
132 /* setup pic16 library directory */
133 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
134 mergeSets(&libDirsSet, pic16libDirsSet);
136 /* now add the library for the device */
137 sprintf(devlib, "%s.lib", pic16->name[2]);
138 addSet(&libFilesSet, Safe_strdup(devlib));
144 _pic16_reset_regparm ()
150 _pic16_regparm (sym_link * l)
152 /* for this processor it is simple
153 can pass only the first parameter in a register */
162 int initsfpnt=0; /* set to 1 if source provides a pragma for stack
163 * so glue() later emits code to initialize stack/frame pointers */
167 _process_pragma(const char *sz)
169 static const char *WHITE = " \t";
171 char *ptr = strtok((char *)sz, WHITE);
173 if (startsWith (ptr, "maxram")) {
174 char *maxRAM = strtok((char *)NULL, WHITE);
176 if (maxRAM != (char *)NULL) {
180 maxRAMVal = constVal(maxRAM);
181 maxRAMaddress = (int)floatFromVal(maxRAMVal);
182 pic16_setMaxRAM(maxRAMaddress);
186 if(startsWith(ptr, "stack")) {
187 char *stackPosS = strtok((char *)NULL, WHITE);
191 // fprintf(stderr, "Initializing stack pointer to 0x%x\n", (int)floatFromVal(constVal(stackPos)));
192 stackPosVal = constVal( stackPosS );
193 stackPos = (unsigned int)floatFromVal( stackPosVal );
195 reg=newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "stack", 1, 0, NULL);
196 addSet(&pic16_fix_udata, reg);
198 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
203 if(startsWith(ptr, "code")) {
204 char *symname = strtok((char *)NULL, WHITE);
205 char *location = strtok((char *)NULL, WHITE);
209 absS = Safe_calloc(1, sizeof(absSym));
210 absS->name = Safe_strdup( symname );
211 addr = constVal( location );
212 absS->address = (unsigned int)floatFromVal( addr );
214 addSet(&absSymSet, absS);
215 fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
216 __FILE__, __LINE__, symname, absS->address);
224 #define REP_UDATA "--preplace-udata-with="
226 #define STACK_MODEL "--pstack-model="
227 #define OPT_BANKSEL "--obanksel="
230 extern int pic16_debug_verbose;
231 extern int pic16_ralloc_debug;
232 extern int pic16_pcode_verbose;
234 OPTION pic16_optionsTable[]= {
235 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
236 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
237 { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
238 { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
239 { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
240 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
242 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
243 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
244 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
246 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
248 { 0, NULL, NULL, NULL}
252 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
254 extern char *getStringArg(const char *, char **, int *, int);
255 extern int getIntArg(const char *, char **, int *, int);
258 _pic16_parseOptions (int *pargc, char **argv, int *i)
263 /* TODO: allow port-specific command line options to specify
264 * segment names here.
266 /* check for arguments that have associated an integer variable */
267 while(pic16_optionsTable[j].pparameter) {
268 if(ISOPT( pic16_optionsTable[j].longOpt )) {
269 (*pic16_optionsTable[j].pparameter)++;
276 if(ISOPT(STACK_MODEL)) {
277 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
278 if(STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
279 else if(STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
281 fprintf(stderr, "Unknown stack model: %s", stkmodel);
287 if(ISOPT(OPT_BANKSEL)) {
288 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
292 if(ISOPT(REP_UDATA)) {
293 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
300 static void _pic16_initPaths(void)
303 char pic16incDir[512];
304 char pic16libDir[512];
305 set *pic16incDirsSet;
306 set *pic16libDirsSet;
311 setMainValue("mcu", pic16->name[2] );
312 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
314 sprintf(pic16incDir, "%s/pic16", INCLUDE_DIR_SUFFIX);
315 sprintf(pic16libDir, "%s/pic16", LIB_DIR_SUFFIX);
317 if(!options.nostdinc) {
318 /* setup pic16 include directory */
319 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
320 mergeSets(&includeDirsSet, pic16incDirsSet);
323 if(!options.nostdlib) {
324 /* setup pic16 library directory */
325 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
326 mergeSets(&libDirsSet, pic16libDirsSet);
328 /* now add the library for the device */
329 sprintf(devlib, "%s.lib", pic16->name[2]);
330 addSet(&libFilesSet, Safe_strdup(devlib));
337 _pic16_finaliseOptions (void)
340 char pic16incDir[512];
341 char pic16libDir[512];
342 set *pic16incDirsSet;
343 set *pic16libDirsSet;
346 port->mem.default_local_map = data;
347 port->mem.default_globl_map = data;
349 options.all_callee_saves = 1; // always callee saves
350 // options.float_rent = 1;
351 // options.intlong_rent = 1;
354 setMainValue("mcu", pic16->name[2] );
355 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
357 sprintf(pic16incDir, "%s/pic16", INCLUDE_DIR_SUFFIX);
358 sprintf(pic16libDir, "%s/pic16", LIB_DIR_SUFFIX);
360 if(!options.nostdinc) {
361 /* setup pic16 include directory */
362 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
363 mergeSets(&includeDirsSet, pic16incDirsSet);
366 if(!options.nostdlib) {
367 /* setup pic16 library directory */
368 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
369 mergeSets(&libDirsSet, pic16libDirsSet);
371 /* now add the library for the device */
372 sprintf(devlib, "%s.lib", pic16->name[2]);
373 addSet(&libFilesSet, Safe_strdup(devlib));
379 /* all the rest is commented ifdef'd out */
381 /* Hack-o-matic: if we are using the flat24 model,
382 * adjust pointer sizes.
384 if (options.model == MODEL_FLAT24)
387 fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
388 "for DS80C390 support. This code generator is "
389 "badly out of date and probably broken.\n");
391 port->s.fptr_size = 3;
392 port->s.gptr_size = 4;
393 port->stack.isr_overhead++; /* Will save dpx on ISR entry. */
395 port->stack.call_overhead++; /* This acounts for the extra byte
396 * of return addres on the stack.
397 * but is ugly. There must be a
401 fReturn = fReturn390;
405 if (options.model == MODEL_LARGE)
407 port->mem.default_local_map = xdata;
408 port->mem.default_globl_map = xdata;
412 port->mem.default_local_map = data;
413 port->mem.default_globl_map = data;
416 if (options.stack10bit)
418 if (options.model != MODEL_FLAT24)
421 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
422 fprintf (stderr, "\t10 bit stack mode disabled.\n");
423 options.stack10bit = 0;
427 /* Fixup the memory map for the stack; it is now in
428 * far space and requires a FPOINTER to access it.
431 istack->ptrType = FPOINTER;
438 _pic16_setDefaultOptions (void)
440 /* initialize to defaults section locations, names and addresses */
441 pic16_sectioninfo.at_udata = "udata";
442 pic16_sectioninfo.at_udataacs = "udata_acs";
443 pic16_sectioninfo.at_udataovr = "udata_ovr";
444 pic16_sectioninfo.at_udatashr = "udata_shr";
446 /* initialize to nothing, so let linker decide about their names */
447 pic16_sectioninfo.name_code =
448 pic16_sectioninfo.name_idata =
449 pic16_sectioninfo.name_udata =
450 pic16_sectioninfo.name_udataacs =
451 pic16_sectioninfo.name_udataovr =
452 pic16_sectioninfo.name_udatashr = "";
454 /* initialize to -1, so let linker decide about their address */
455 pic16_sectioninfo.addr_code =
456 pic16_sectioninfo.addr_idata =
457 pic16_sectioninfo.addr_udata =
458 pic16_sectioninfo.addr_udataacs =
459 pic16_sectioninfo.addr_udataovr =
460 pic16_sectioninfo.addr_udatashr = -1;
464 /* set pic16 port options to defaults */
465 pic16_options.no_banksel = 0;
466 pic16_options.opt_banksel = 0;
467 pic16_options.omit_configw = 0;
468 pic16_options.omit_ivt = 0;
469 pic16_options.leave_reset = 0;
470 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
474 _pic16_getRegName (struct regs *reg)
483 static char *_pic16_mangleFunctionName(char *sz)
485 // fprintf(stderr, "mangled function name: %s\n", sz);
493 _pic16_genAssemblerPreamble (FILE * of)
495 char *name = pic16_processor_base_name();
499 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
502 fprintf (of, "\tlist\tp=%s\n",&name[1]);
504 if(!pic16_options.omit_configw) {
505 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300001, pic16_getConfigWord(0x300001));
506 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300002, pic16_getConfigWord(0x300002));
507 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300003, pic16_getConfigWord(0x300003));
508 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300005, pic16_getConfigWord(0x300005));
509 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300006, pic16_getConfigWord(0x300006));
510 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300008, pic16_getConfigWord(0x300008));
511 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300009, pic16_getConfigWord(0x300009));
512 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000a, pic16_getConfigWord(0x30000a));
513 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000b, pic16_getConfigWord(0x30000b));
514 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000c, pic16_getConfigWord(0x30000c));
515 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000d, pic16_getConfigWord(0x30000d));
518 fprintf (of, "\tradix dec\n");
521 /* Generate interrupt vector table. */
523 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
526 /* PIC18F family has only two interrupts, the high and the low
527 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
529 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
530 fprintf(of, "; RESET vector\n");
531 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
534 if(!pic16_options.omit_ivt) {
535 fprintf(of, "\tres 4\n");
538 fprintf(of, "; High priority interrupt vector 0x0008\n");
540 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
541 fprintf(of, "\tres\t12\n");
543 fprintf(of, "\tretfie\n");
544 fprintf(of, "\tres\t14\n");
547 fprintf(of, "; Low priority interrupt vector 0x0018\n");
549 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
551 fprintf(of, "\tretfie\n");
558 /* return True if the port can handle the type,
559 * False to convert it to function call */
560 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
562 // fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
564 /* support mul for char/int {/long} */
565 if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) < 2)
566 && (ic->op == '*'))return TRUE;
568 /* support div for char/int/long */
569 if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) < 0)
570 && (ic->op == '/'))return TRUE;
577 /* Do CSE estimation */
578 static bool cseCostEstimation (iCode *ic, iCode *pdic)
580 // operand *result = IC_RESULT(ic);
581 // sym_link *result_type = operandType(result);
584 /* VR -- this is an adhoc. Put here after conversation
585 * with Erik Epetrich */
589 || ic->op == EQ_OP) {
591 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
596 /* if it is a pointer then return ok for now */
597 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
599 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
600 so we will cse only if they are local (i.e. both ic & pdic belong to
601 the same basic block */
602 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
603 /* then if they are the same Basic block then ok */
604 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
609 /* for others it is cheaper to do the cse */
615 /* Indicate which extended bit operations this port supports */
617 hasExtBitOp (int op, int size)
621 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
628 /* Indicate the expense of an access to an output storage class */
630 oclsExpense (struct memmap *oclass)
632 /* The IN_FARSPACE test is compatible with historical behaviour, */
633 /* but I don't think it is applicable to PIC. If so, please feel */
634 /* free to remove this test -- EEP */
635 if (IN_FARSPACE(oclass))
641 /** $1 is always the basename.
642 $2 is always the output file.
643 $3 -L path and -l libraries
644 $l is the list of extra options that should be there somewhere...
645 MUST be terminated with a NULL.
647 static const char *_linkCmd[] =
649 "gplink", "$3", "\"$1.o\"", "-o $1", "$l", NULL
654 /** $1 is always the basename.
655 $2 is always the output file.
656 $3 varies (nothing currently)
657 $l is the list of extra options that should be there somewhere...
658 MUST be terminated with a NULL.
660 static const char *_asmCmd[] =
662 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", NULL
671 "MCU PIC16", /* Target name */
672 "p18f452", /* Processor */
675 TRUE, /* Emit glue around main */
676 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
680 _asmCmd, /* assembler command and arguments */
681 NULL, /* alternate macro based form */
682 "-g", /* arguments for debug mode */
683 NULL, /* arguments for normal mode */
684 0, /* print externs as global */
685 ".asm", /* assembler file extension */
686 NULL /* no do_assemble function */
689 _linkCmd, /* linker command and arguments */
690 NULL, /* alternate macro based form */
691 NULL, /* no do_link function */
692 ".o", /* extension for object files */
693 0 /* no need for linker file */
705 3, /* fptr, far pointers (see Microchip) */
712 "XSEG (XDATA)", // xstack
713 "STACK (DATA)", // istack
714 "CSEG (CODE)", // code
715 "DSEG (DATA)", // data
716 "ISEG (DATA)", // idata
717 "XSEG (XDATA)", // xdata
719 "RSEG (DATA)", // reg
720 "GSINIT (CODE)", // static
721 "OSEG (OVR,DATA)", // overlay
722 "GSFINAL (CODE)", // post static
723 "HOME (CODE)", // home
726 NULL, // default location for auto vars
727 NULL, // default location for global vars
728 1 // code is read only 1=yes
731 NULL, /* genExtraAreaDeclaration */
732 NULL /* genExatrAreaLinkOptions */
735 /* stack related information */
736 -1, /* -1 stack grows downwards, +1 upwards */
737 1, /* extra overhead when calling between banks */
738 4, /* extra overhead when the function is an ISR */
739 1, /* extra overhead for a function call */
740 1, /* re-entrant space */
741 0 /* 'banked' call overhead, mild overlap with bank_overhead */
743 /* pic16 has an 8 bit mul */
748 pic16_emitDebuggerSymbol
755 _pic16_finaliseOptions,
756 _pic16_setDefaultOptions,
757 pic16_assignRegisters,
760 _pic16_genAssemblerPreamble,
761 NULL, /* no genAssemblerEnd */
763 NULL, // _pic16_genXINIT
764 NULL, /* genInitStartup */
765 _pic16_reset_regparm,
767 _process_pragma, /* process a pragma */
768 _pic16_mangleFunctionName, /* mangles function name */
770 hasExtBitOp, /* hasExtBitOp */
771 oclsExpense, /* oclsExpense */
773 TRUE, /* little endian */
776 1, /* transform <= to ! > */
777 1, /* transform >= to ! < */
778 1, /* transform != to !(a == b) */
780 FALSE, /* No array initializer support. */
781 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
782 NULL, /* no builtin functions */
783 GPOINTER, /* treat unqualified pointers as "generic" pointers */
784 1, /* reset labelKey to 1 */
785 1, /* globals & local static allowed */