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 *);
93 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
94 extern void pic16_emitConfigRegs(FILE *of);
102 asm_addTree (&asm_asxxxx_mapping);
103 pic16_pCodeInitRegisters();
106 /* set pic16 port options to defaults */
107 pic16_options.no_banksel = 0;
108 pic16_options.opt_banksel = 0;
109 pic16_options.omit_configw = 0;
110 pic16_options.omit_ivt = 0;
111 pic16_options.leave_reset = 0;
112 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
113 pic16_options.ivt_loc = 0x000000; /* default location of interrupt vectors */
114 pic16_options.nodefaultlibs = 0; /* link default libraries */
118 _pic16_reset_regparm ()
124 _pic16_regparm (sym_link * l)
126 /* for this processor it is simple
127 can pass only the first parameter in a register */
136 int initsfpnt=0; /* set to 1 if source provides a pragma for stack
137 * so glue() later emits code to initialize stack/frame pointers */
141 _process_pragma(const char *sz)
143 static const char *WHITE = " \t";
145 char *ptr = strtok((char *)sz, WHITE);
147 if (startsWith (ptr, "maxram")) {
148 char *maxRAM = strtok((char *)NULL, WHITE);
150 if (maxRAM != (char *)NULL) {
154 maxRAMVal = constVal(maxRAM);
155 maxRAMaddress = (int)floatFromVal(maxRAMVal);
156 pic16_setMaxRAM(maxRAMaddress);
160 if(startsWith(ptr, "stack")) {
161 char *stackPosS = strtok((char *)NULL, WHITE);
166 // fprintf(stderr, "Initializing stack pointer to 0x%x\n", (int)floatFromVal(constVal(stackPos)));
167 stackPosVal = constVal( stackPosS );
168 stackPos = (unsigned int)floatFromVal( stackPosVal );
170 reg=newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", 1, 0, NULL);
171 addSet(&pic16_fix_udata, reg);
173 sym = newSymbol("stack", 0);
174 sprintf(sym->rname, "_%s", sym->name);
175 addSet(&publics, sym);
177 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
182 if(startsWith(ptr, "code")) {
183 char *symname = strtok((char *)NULL, WHITE);
184 char *location = strtok((char *)NULL, WHITE);
188 absS = Safe_calloc(1, sizeof(absSym));
189 absS->name = Safe_strdup( symname );
190 addr = constVal( location );
191 absS->address = (unsigned int)floatFromVal( addr );
193 addSet(&absSymSet, absS);
194 fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
195 __FILE__, __LINE__, symname, absS->address);
203 #define REP_UDATA "--preplace-udata-with="
205 #define STACK_MODEL "--pstack-model="
206 #define OPT_BANKSEL "--obanksel="
208 #define ALT_ASM "--asm="
209 #define ALT_LINK "--link="
211 #define IVT_LOC "--ivt-loc"
212 #define NO_DEFLIBS "--nodefaultlibs"
218 extern int pic16_debug_verbose;
219 extern int pic16_ralloc_debug;
220 extern int pic16_pcode_verbose;
222 int pic16_enable_peeps=0;
224 OPTION pic16_optionsTable[]= {
225 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
226 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
227 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
228 { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
229 { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
230 { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
231 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
233 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
234 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
235 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
237 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
239 { 0, ALT_ASM, NULL, "Use alternative assembler"},
240 { 0, ALT_LINK, NULL, "Use alternative linker"},
242 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
243 { 0, IVT_LOC, NULL, "<nnnn> interrupt vector table location"},
244 { 0, NULL, NULL, NULL}
248 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
250 extern char *getStringArg(const char *, char **, int *, int);
251 extern int getIntArg(const char *, char **, int *, int);
254 _pic16_parseOptions (int *pargc, char **argv, int *i)
259 /* TODO: allow port-specific command line options to specify
260 * segment names here.
262 /* check for arguments that have associated an integer variable */
263 while(pic16_optionsTable[j].pparameter) {
264 if(ISOPT( pic16_optionsTable[j].longOpt )) {
265 (*pic16_optionsTable[j].pparameter)++;
272 if(ISOPT(STACK_MODEL)) {
273 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
274 if(STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
275 else if(STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
277 fprintf(stderr, "Unknown stack model: %s", stkmodel);
283 if(ISOPT(OPT_BANKSEL)) {
284 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
288 if(ISOPT(REP_UDATA)) {
289 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
294 alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
298 if(ISOPT(ALT_LINK)) {
299 alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
304 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
311 static void _pic16_initPaths(void)
313 char pic16incDir[512];
314 char pic16libDir[512];
315 set *pic16incDirsSet;
316 set *pic16libDirsSet;
319 setMainValue("mcu", pic16->name[2] );
320 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
322 sprintf(pic16incDir, "%s%cpic16", INCLUDE_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
323 sprintf(pic16libDir, "%s%cpic16", LIB_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
325 if(!options.nostdinc) {
326 /* setup pic16 include directory */
327 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
328 mergeSets(&includeDirsSet, pic16incDirsSet);
331 if(!options.nostdlib) {
332 /* setup pic16 library directory */
333 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
334 mergeSets(&libDirsSet, pic16libDirsSet);
336 if(!pic16_options.nodefaultlibs) {
337 /* now add the library for the device */
338 sprintf(devlib, "%s.lib", pic16->name[2]);
339 addSet(&libFilesSet, Safe_strdup(devlib));
345 /* forward declarations */
346 extern const char *pic16_linkCmd[];
347 extern const char *pic16_asmCmd[];
350 _pic16_finaliseOptions (void)
352 port->mem.default_local_map = data;
353 port->mem.default_globl_map = data;
355 /* peepholes are disabled for the time being */
358 /* explicit enable peepholes for testing */
359 if(pic16_enable_peeps)
362 options.all_callee_saves = 1; // always callee saves
363 // options.float_rent = 1;
364 // options.intlong_rent = 1;
367 if(alt_asm && strlen(alt_asm))
368 pic16_asmCmd[0] = alt_asm;
370 if(alt_link && strlen(alt_link))
371 pic16_linkCmd[0] = alt_link;
375 /* all the rest is commented ifdef'd out */
377 /* Hack-o-matic: if we are using the flat24 model,
378 * adjust pointer sizes.
380 if (options.model == MODEL_FLAT24)
383 fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
384 "for DS80C390 support. This code generator is "
385 "badly out of date and probably broken.\n");
387 port->s.fptr_size = 3;
388 port->s.gptr_size = 4;
389 port->stack.isr_overhead++; /* Will save dpx on ISR entry. */
391 port->stack.call_overhead++; /* This acounts for the extra byte
392 * of return addres on the stack.
393 * but is ugly. There must be a
397 fReturn = fReturn390;
401 if (options.model == MODEL_LARGE)
403 port->mem.default_local_map = xdata;
404 port->mem.default_globl_map = xdata;
408 port->mem.default_local_map = data;
409 port->mem.default_globl_map = data;
412 if (options.stack10bit)
414 if (options.model != MODEL_FLAT24)
417 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
418 fprintf (stderr, "\t10 bit stack mode disabled.\n");
419 options.stack10bit = 0;
423 /* Fixup the memory map for the stack; it is now in
424 * far space and requires a FPOINTER to access it.
427 istack->ptrType = FPOINTER;
434 _pic16_setDefaultOptions (void)
436 /* initialize to defaults section locations, names and addresses */
437 pic16_sectioninfo.at_udata = "udata";
439 /* set pic16 port options to defaults */
440 pic16_options.no_banksel = 0;
441 pic16_options.opt_banksel = 0;
442 pic16_options.omit_configw = 0;
443 pic16_options.omit_ivt = 0;
444 pic16_options.leave_reset = 0;
445 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
446 pic16_options.ivt_loc = 0x000000;
447 pic16_options.nodefaultlibs = 0;
451 _pic16_getRegName (struct regs *reg)
460 static char *_pic16_mangleFunctionName(char *sz)
462 // fprintf(stderr, "mangled function name: %s\n", sz);
470 _pic16_genAssemblerPreamble (FILE * of)
472 char *name = pic16_processor_base_name();
476 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
479 fprintf (of, "\tlist\tp=%s\n",&name[1]);
481 if(!pic16_options.omit_configw) {
482 pic16_emitConfigRegs(of);
484 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300001, pic16_getConfigWord(0x300001));
485 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300002, pic16_getConfigWord(0x300002));
486 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300003, pic16_getConfigWord(0x300003));
487 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300005, pic16_getConfigWord(0x300005));
488 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300006, pic16_getConfigWord(0x300006));
489 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300008, pic16_getConfigWord(0x300008));
490 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300009, pic16_getConfigWord(0x300009));
491 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000a, pic16_getConfigWord(0x30000a));
492 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000b, pic16_getConfigWord(0x30000b));
493 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000c, pic16_getConfigWord(0x30000c));
494 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000d, pic16_getConfigWord(0x30000d));
498 fprintf (of, "\tradix dec\n");
501 /* Generate interrupt vector table. */
503 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
506 /* PIC18F family has only two interrupts, the high and the low
507 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
509 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
510 fprintf(of, "; RESET vector\n");
511 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
514 if(!pic16_options.omit_ivt) {
515 fprintf(of, "\tres 4\n");
518 fprintf(of, "; High priority interrupt vector 0x0008\n");
520 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
521 fprintf(of, "\tres\t12\n");
523 fprintf(of, "\tretfie\n");
524 fprintf(of, "\tres\t14\n");
527 fprintf(of, "; Low priority interrupt vector 0x0018\n");
529 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
531 fprintf(of, "\tretfie\n");
538 /* return True if the port can handle the type,
539 * False to convert it to function call */
540 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
542 // fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
545 /* multiplication is fixed */
546 /* support mul for char/int/long */
547 if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) <= 4)
548 && (ic->op == '*'))return TRUE;
552 /* support div for char/int/long */
553 if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) <= 0)
554 && (ic->op == '/'))return TRUE;
562 /* Do CSE estimation */
563 static bool cseCostEstimation (iCode *ic, iCode *pdic)
565 // operand *result = IC_RESULT(ic);
566 // sym_link *result_type = operandType(result);
569 /* VR -- this is an adhoc. Put here after conversation
570 * with Erik Epetrich */
574 || ic->op == EQ_OP) {
576 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
581 /* if it is a pointer then return ok for now */
582 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
584 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
585 so we will cse only if they are local (i.e. both ic & pdic belong to
586 the same basic block */
587 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
588 /* then if they are the same Basic block then ok */
589 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
594 /* for others it is cheaper to do the cse */
600 /* Indicate which extended bit operations this port supports */
602 hasExtBitOp (int op, int size)
606 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
613 /* Indicate the expense of an access to an output storage class */
615 oclsExpense (struct memmap *oclass)
617 /* The IN_FARSPACE test is compatible with historical behaviour, */
618 /* but I don't think it is applicable to PIC. If so, please feel */
619 /* free to remove this test -- EEP */
620 if (IN_FARSPACE(oclass))
626 /** $1 is always the basename.
627 $2 is always the output file.
628 $3 -L path and -l libraries
629 $l is the list of extra options that should be there somewhere...
630 MUST be terminated with a NULL.
632 const char *pic16_linkCmd[] =
634 "gplink", "$3", "\"$1.o\"", "-o \"$2\"", "$l", NULL
639 /** $1 is always the basename.
640 $2 is always the output file.
641 $3 varies (nothing currently)
642 $l is the list of extra options that should be there somewhere...
643 MUST be terminated with a NULL.
645 const char *pic16_asmCmd[] =
647 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
656 "MCU PIC16", /* Target name */
657 "p18f452", /* Processor */
660 TRUE, /* Emit glue around main */
661 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
665 pic16_asmCmd, /* assembler command and arguments */
666 NULL, /* alternate macro based form */
667 "-g", /* arguments for debug mode */
668 NULL, /* arguments for normal mode */
669 0, /* print externs as global */
670 ".asm", /* assembler file extension */
671 NULL /* no do_assemble function */
674 pic16_linkCmd, /* linker command and arguments */
675 NULL, /* alternate macro based form */
676 NULL, /* no do_link function */
677 ".o", /* extension for object files */
678 0 /* no need for linker file */
690 3, /* fptr, far pointers (see Microchip) */
697 "XSEG (XDATA)", // xstack
698 "STACK (DATA)", // istack
699 "CSEG (CODE)", // code
700 "DSEG (DATA)", // data
701 "ISEG (DATA)", // idata
702 "XSEG (XDATA)", // xdata
704 "RSEG (DATA)", // reg
705 "GSINIT (CODE)", // static
706 "OSEG (OVR,DATA)", // overlay
707 "GSFINAL (CODE)", // post static
708 "HOME (CODE)", // home
711 NULL, // default location for auto vars
712 NULL, // default location for global vars
713 1 // code is read only 1=yes
716 NULL, /* genExtraAreaDeclaration */
717 NULL /* genExatrAreaLinkOptions */
720 /* stack related information */
721 -1, /* -1 stack grows downwards, +1 upwards */
722 1, /* extra overhead when calling between banks */
723 4, /* extra overhead when the function is an ISR */
724 1, /* extra overhead for a function call */
725 1, /* re-entrant space */
726 0 /* 'banked' call overhead, mild overlap with bank_overhead */
728 /* pic16 has an 8 bit mul */
733 pic16_emitDebuggerSymbol
740 _pic16_finaliseOptions,
741 _pic16_setDefaultOptions,
742 pic16_assignRegisters,
745 _pic16_genAssemblerPreamble,
746 NULL, /* no genAssemblerEnd */
748 NULL, // _pic16_genXINIT
749 NULL, /* genInitStartup */
750 _pic16_reset_regparm,
752 _process_pragma, /* process a pragma */
753 _pic16_mangleFunctionName, /* mangles function name */
755 hasExtBitOp, /* hasExtBitOp */
756 oclsExpense, /* oclsExpense */
758 TRUE, /* little endian */
761 1, /* transform <= to ! > */
762 1, /* transform >= to ! < */
763 1, /* transform != to !(a == b) */
765 FALSE, /* No array initializer support. */
766 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
767 NULL, /* no builtin functions */
768 GPOINTER, /* treat unqualified pointers as "generic" pointers */
769 1, /* reset labelKey to 1 */
770 1, /* globals & local static allowed */