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 -------------------------------------------------------------------------*/
36 static char _defaultRules[] =
41 /* list of key words used by pic16 */
42 static char *_pic16_keywords[] =
71 pic16_sectioninfo_t pic16_sectioninfo;
74 extern char *pic16_processor_base_name(void);
76 void pic16_pCodeInitRegisters(void);
78 void pic16_assignRegisters (eBBlock ** ebbs, int count);
80 static int regParmFlg = 0; /* determine if we can register a parameter */
82 pic16_options_t pic16_options;
87 asm_addTree (&asm_asxxxx_mapping);
88 pic16_pCodeInitRegisters();
92 /* set pic16 port options to defaults */
93 pic16_options.gen_banksel = 0;
94 pic16_options.opt_banksel = 0;
95 pic16_options.omit_configw = 0;
96 pic16_options.omit_ivt = 0;
97 pic16_options.leave_reset = 0;
98 pic16_options.enable_stack = 0;
99 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
103 _pic16_reset_regparm ()
109 _pic16_regparm (sym_link * l)
111 /* for this processor it is simple
112 can pass only the first parameter in a register */
124 _process_pragma(const char *sz)
126 static const char *WHITE = " \t";
128 char *ptr = strtok((char *)sz, WHITE);
130 if (startsWith (ptr, "maxram")) {
131 char *maxRAM = strtok((char *)NULL, WHITE);
133 if (maxRAM != (char *)NULL) {
137 maxRAMVal = constVal(maxRAM);
138 maxRAMaddress = (int)floatFromVal(maxRAMVal);
139 pic16_setMaxRAM(maxRAMaddress);
143 if(startsWith(ptr, "stack")) {
144 char *stackPosS = strtok((char *)NULL, WHITE);
147 // fprintf(stderr, "Initializing stack pointer to 0x%x\n", (int)floatFromVal(constVal(stackPos)));
148 stackPosVal = constVal( stackPosS );
149 stackPos = (unsigned int)floatFromVal( stackPosVal );
154 if(startsWith(ptr, "code")) {
155 char *symname = strtok((char *)NULL, WHITE);
156 char *location = strtok((char *)NULL, WHITE);
160 absS = Safe_calloc(1, sizeof(absSym));
161 absS->name = Safe_strdup( symname );
162 addr = constVal( location );
163 absS->address = (unsigned int)floatFromVal( addr );
165 addSet(&absSymSet, absS);
166 fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
167 __FILE__, __LINE__, symname, absS->address);
175 #define REP_UDATA "--preplace-udata-with="
176 #define REP_UDATAACS "--preplace-udata-acs-with="
177 #define REP_UDATAOVR "--preplace-udata-ovr-with="
178 #define REP_UDATASHR "--preplace-udata-sht-with="
180 #define NAME_CODE "--psection-code-name="
181 #define NAME_IDATA "--psection-idata-name="
182 #define NAME_UDATA "--psection-udata-name="
183 #define NAME_UDATAACS "--psection-udata-acs-name="
184 #define NAME_UDATAOVR "--psection-udata-ovr-name="
185 #define NAME_UDATASHR "--psection-udata-shr-name="
187 #define ADDR_CODE "--psection-code-addr="
188 #define ADDR_IDATA "--psection-idata-addr="
189 #define ADDR_UDATA "--psection-udata-addr="
190 #define ADDR_UDATAACS "--psection-udata-acs-addr="
191 #define ADDR_UDATAOVR "--psection-udata-ovr-addr="
192 #define ADDR_UDATASHR "--psection-udata-shr-addr="
194 #define STACK_MODEL "--pstack-model="
195 #define OPT_BANKSEL "--obanksel="
198 extern int pic16_debug_verbose;
199 extern int pic16_ralloc_debug;
200 extern int pic16_pcode_verbose;
202 OPTION pic16_optionsTable[]= {
203 { 0, "--pgen-banksel", &pic16_options.gen_banksel, "generate BANKSEL assembler directives"},
204 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
205 { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
206 { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
207 { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
208 { 0, "--penable-stack", &pic16_options.enable_stack, "enable the use of stack"},
209 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
211 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
212 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
213 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
215 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
218 /* these may not be in any use -- VR */
219 { 0, AT_UDATAACS, NULL, "Emit udata_acs variables at another section"},
220 { 0, AT_UDATAOVR, NULL, "Emit udata_ovr variables at another section"},
221 { 0, AT_UDATASHR, NULL, "Emit udata_shr variables at another section"},
225 /* commented out for the time being -- VR */
226 { 0, NAME_CODE, NULL, "Set code section name[,address]"},
227 { 0, NAME_IDATA, NULL, "Set idata section name[,address]"},
228 { 0, NAME_UDATA, NULL, "Set udata section name[,address]"},
229 { 0, NAME_UDATAACS, NULL, "Set udata_acs section name[,address]"},
230 { 0, NAME_UDATAOVR, NULL, "Set udata_ovr section name[,address]"},
231 { 0, NAME_UDATASHR, NULL, "Set udata_shr section name[,address]"},
233 { 0, ADDR_CODE, NULL, "Set code section address"},
234 { 0, ADDR_IDATA, NULL, "Set idata section address"},
235 { 0, ADDR_UDATA, NULL, "Set udata section address"},
236 { 0, ADDR_UDATAACS, NULL, "Set udata_acs section address"},
237 { 0, ADDR_UDATAOVR, NULL, "Set udata_ovr section address"},
238 { 0, ADDR_UDATASHR, NULL, "Set udata_shr section address"},
241 { 0, NULL, NULL, NULL}
245 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
247 extern char *getStringArg(const char *, char **, int *, int);
248 extern int getIntArg(const char *, char **, int *, int);
251 _pic16_parseOptions (int *pargc, char **argv, int *i)
256 /* TODO: allow port-specific command line options to specify
257 * segment names here.
259 /* check for arguments that have associated an integer variable */
260 while(pic16_optionsTable[j].pparameter) {
261 if(ISOPT( pic16_optionsTable[j].longOpt )) {
262 (*pic16_optionsTable[j].pparameter)++;
269 if(ISOPT(STACK_MODEL)) {
270 switch(*getStringArg(STACK_MODEL, argv, i, *pargc)) {
271 case 's': pic16_options.stack_model = 0; break;
272 case 'l': pic16_options.stack_model = 1; break;
275 fprintf(stderr, "Unknown stack model");
281 if(ISOPT(OPT_BANKSEL)) {
282 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
286 if(ISOPT(REP_UDATA)) {
287 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
292 if(ISOPT(AT_UDATAACS)) {
293 pic16_sectioninfo.at_udataacs = Safe_strdup( getStringArg(AT_UDATAACS, argv, i, *pargc));
297 if(ISOPT(AT_UDATAOVR)) {
298 pic16_sectioninfo.at_udataovr = Safe_strdup( getStringArg(AT_UDATAOVR, argv, i, *pargc));
302 if(ISOPT(AT_UDATASHR)) {
303 pic16_sectioninfo.at_udatashr = Safe_strdup( getStringArg(AT_UDATASHR, argv, i, *pargc));
309 if(ISOPT(ADDR_CODE)) {
310 pic16_sectioninfo.addr_code = getIntArg(ADDR_CODE, argv, i, *pargc);
314 if(ISOPT(ADDR_IDATA)) {
315 pic16_sectioninfo.addr_idata = getIntArg(ADDR_IDATA, argv, i, *pargc);
319 if(ISOPT(ADDR_UDATA)) {
320 pic16_sectioninfo.addr_udata = getIntArg(ADDR_UDATA, argv, i, *pargc);
324 if(ISOPT(ADDR_UDATAACS)) {
325 pic16_sectioninfo.addr_udataacs = getIntArg(ADDR_UDATAACS, argv, i, *pargc);
329 if(ISOPT(ADDR_UDATAOVR)) {
330 pic16_sectioninfo.addr_udataovr = getIntArg(ADDR_UDATAOVR, argv, i, *pargc);
334 if(ISOPT(ADDR_UDATASHR)) {
335 pic16_sectioninfo.addr_udatashr = getIntArg(ADDR_UDATASHR, argv, i, *pargc);
341 if(ISOPT(NAME_CODE)) {
342 setParseWithComma(&tset, getStringArg(NAME_CODE, argv, i, *pargc));
343 if(elementsInSet(tset) > 0)
344 pic16_sectioninfo.name_code = Safe_strdup( (char *)indexSet(tset, 0));
345 if(elementsInSet(tset) > 1)
346 pic16_sectioninfo.addr_code = atoi( (char *)indexSet(tset, 1));
350 if(ISOPT(NAME_IDATA)) {
351 setParseWithComma(&tset, getStringArg(NAME_IDATA, argv, i, *pargc));
352 if(elementsInSet(tset) > 0)
353 pic16_sectioninfo.name_idata = Safe_strdup( (char *)indexSet(tset, 0));
354 if(elementsInSet(tset) > 1)
355 pic16_sectioninfo.addr_idata = atoi( (char *)indexSet(tset, 1));
359 if(ISOPT(NAME_UDATA)) {
360 setParseWithComma(&tset, getStringArg(NAME_UDATA, argv, i, *pargc));
361 if(elementsInSet(tset) > 0)
362 pic16_sectioninfo.name_udata = Safe_strdup( (char *)indexSet(tset, 0));
363 if(elementsInSet(tset) > 1)
364 pic16_sectioninfo.addr_udata = atoi( (char *)indexSet(tset, 1));
368 if(ISOPT(NAME_UDATAACS)) {
369 setParseWithComma(&tset, getStringArg(NAME_UDATAACS, argv, i, *pargc));
370 if(elementsInSet(tset) > 0)
371 pic16_sectioninfo.name_udataacs = Safe_strdup( (char *)indexSet(tset, 0));
372 if(elementsInSet(tset) > 1)
373 pic16_sectioninfo.addr_udataacs = atoi( (char *)indexSet(tset, 1));
377 if(ISOPT(NAME_UDATAOVR)) {
378 setParseWithComma(&tset, getStringArg(NAME_UDATAOVR, argv, i, *pargc));
379 if(elementsInSet(tset) > 0)
380 pic16_sectioninfo.name_udataovr = Safe_strdup( (char *)indexSet(tset, 0));
381 if(elementsInSet(tset) > 1)
382 pic16_sectioninfo.addr_udataovr = atoi( (char *)indexSet(tset, 1));
386 if(ISOPT(NAME_UDATASHR)) {
387 setParseWithComma(&tset, getStringArg(NAME_UDATASHR, argv, i, *pargc));
388 if(elementsInSet(tset) > 0)
389 pic16_sectioninfo.name_udatashr = Safe_strdup( (char *)indexSet(tset, 0));
390 if(elementsInSet(tset) > 1)
391 pic16_sectioninfo.addr_udatashr = atoi( (char *)indexSet(tset, 1));
401 extern set *includeDirsSet;
402 extern set *dataDirsSet;
403 extern set *libFilesSet;
406 _pic16_finaliseOptions (void)
408 char pic16incDir[512];
409 char pic16libDir[512];
410 set *pic16incDirsSet;
411 set *pic16libDirsSet;
414 port->mem.default_local_map = data;
415 port->mem.default_globl_map = data;
417 options.all_callee_saves = 1; // always callee saves
419 setMainValue("mcu", pic16_processor_base_name() );
420 addSet(&preArgvSet, Safe_strdup("-DMCU={mcu}"));
422 sprintf(pic16incDir, "%s/pic16", INCLUDE_DIR_SUFFIX);
423 sprintf(pic16libDir, "%s/pic16", LIB_DIR_SUFFIX);
425 if(!options.nostdinc) {
426 /* setup pic16 include directory */
427 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
428 mergeSets(&includeDirsSet, pic16incDirsSet);
431 if(!options.nostdlib) {
432 /* setup pic16 library directory */
433 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
434 mergeSets(&libDirsSet, pic16libDirsSet);
436 /* now add the library for the device */
437 sprintf(devlib, "%s.lib", pic16->name[2]);
438 addSet(&libFilesSet, Safe_strdup(devlib));
443 /* all the rest is commented ifdef'd out */
445 /* Hack-o-matic: if we are using the flat24 model,
446 * adjust pointer sizes.
448 if (options.model == MODEL_FLAT24)
451 fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
452 "for DS80C390 support. This code generator is "
453 "badly out of date and probably broken.\n");
455 port->s.fptr_size = 3;
456 port->s.gptr_size = 4;
457 port->stack.isr_overhead++; /* Will save dpx on ISR entry. */
459 port->stack.call_overhead++; /* This acounts for the extra byte
460 * of return addres on the stack.
461 * but is ugly. There must be a
465 fReturn = fReturn390;
469 if (options.model == MODEL_LARGE)
471 port->mem.default_local_map = xdata;
472 port->mem.default_globl_map = xdata;
476 port->mem.default_local_map = data;
477 port->mem.default_globl_map = data;
480 if (options.stack10bit)
482 if (options.model != MODEL_FLAT24)
485 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
486 fprintf (stderr, "\t10 bit stack mode disabled.\n");
487 options.stack10bit = 0;
491 /* Fixup the memory map for the stack; it is now in
492 * far space and requires a FPOINTER to access it.
495 istack->ptrType = FPOINTER;
502 _pic16_setDefaultOptions (void)
504 /* initialize to defaults section locations, names and addresses */
505 pic16_sectioninfo.at_udata = "udata";
506 pic16_sectioninfo.at_udataacs = "udata_acs";
507 pic16_sectioninfo.at_udataovr = "udata_ovr";
508 pic16_sectioninfo.at_udatashr = "udata_shr";
510 /* initialize to nothing, so let linker decide about their names */
511 pic16_sectioninfo.name_code =
512 pic16_sectioninfo.name_idata =
513 pic16_sectioninfo.name_udata =
514 pic16_sectioninfo.name_udataacs =
515 pic16_sectioninfo.name_udataovr =
516 pic16_sectioninfo.name_udatashr = "";
518 /* initialize to -1, so let linker decide about their address */
519 pic16_sectioninfo.addr_code =
520 pic16_sectioninfo.addr_idata =
521 pic16_sectioninfo.addr_udata =
522 pic16_sectioninfo.addr_udataacs =
523 pic16_sectioninfo.addr_udataovr =
524 pic16_sectioninfo.addr_udatashr = -1;
528 _pic16_getRegName (struct regs *reg)
537 static char *_pic16_mangleFunctionName(char *sz)
539 // fprintf(stderr, "mangled function name: %s\n", sz);
547 _pic16_genAssemblerPreamble (FILE * of)
549 char *name = pic16_processor_base_name();
553 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
556 fprintf (of, "\tlist\tp=%s\n",&name[1]);
558 if(!pic16_options.omit_configw) {
559 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300001, pic16_getConfigWord(0x300001));
560 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300002, pic16_getConfigWord(0x300002));
561 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300003, pic16_getConfigWord(0x300003));
562 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300005, pic16_getConfigWord(0x300005));
563 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300006, pic16_getConfigWord(0x300006));
564 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300008, pic16_getConfigWord(0x300008));
565 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300009, pic16_getConfigWord(0x300009));
566 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000a, pic16_getConfigWord(0x30000a));
567 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000b, pic16_getConfigWord(0x30000b));
568 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000c, pic16_getConfigWord(0x30000c));
569 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000d, pic16_getConfigWord(0x30000d));
572 fprintf (of, "\tradix dec\n");
575 /* Generate interrupt vector table. */
577 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
579 /* PIC18F family has only two interrupts, the high and the low
580 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
582 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
583 fprintf(of, "; RESET vector\n");
584 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
587 if(!pic16_options.omit_ivt) {
588 fprintf(of, "\tres 4\n");
591 fprintf(of, "; High priority interrupt vector 0x0008\n");
593 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
594 fprintf(of, "\tres\t12\n");
596 fprintf(of, "\tretfie\n");
597 fprintf(of, "\tres\t14\n");
600 fprintf(of, "; Low priority interrupt vector 0x0018\n");
602 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
604 fprintf(of, "\tretfie\n");
612 _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
614 // sym_link *test = NULL;
617 fprintf(stderr,"checking for native mult\n");
626 if ( IS_LITERAL (left))
628 fprintf(stderr,"left is lit\n");
630 val = OP_VALUE (IC_LEFT (ic));
632 else if ( IS_LITERAL (right))
634 fprintf(stderr,"right is lit\n");
636 val = OP_VALUE (IC_RIGHT (ic));
640 fprintf(stderr,"oops, neither is lit so no\n");
644 if ( getSize (test) <= 2)
646 fprintf(stderr,"yep\n");
649 fprintf(stderr,"nope\n");
655 /* Indicate which extended bit operations this port supports */
657 hasExtBitOp (int op, int size)
661 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
668 /* Indicate the expense of an access to an output storage class */
670 oclsExpense (struct memmap *oclass)
672 /* The IN_FARSPACE test is compatible with historical behaviour, */
673 /* but I don't think it is applicable to PIC. If so, please feel */
674 /* free to remove this test -- EEP */
675 if (IN_FARSPACE(oclass))
681 /** $1 is always the basename.
682 $2 is always the output file.
683 $3 -L path and -l libraries
684 $l is the list of extra options that should be there somewhere...
685 MUST be terminated with a NULL.
687 static const char *_linkCmd[] =
689 "gplink", "$3", "\"$1.o\"", "-o $1", "$l", NULL
694 /** $1 is always the basename.
695 $2 is always the output file.
696 $3 varies (nothing currently)
697 $l is the list of extra options that should be there somewhere...
698 MUST be terminated with a NULL.
700 static const char *_asmCmd[] =
702 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", NULL
711 "MCU PIC16", /* Target name */
712 "p18f452", /* Processor */
715 TRUE, /* Emit glue around main */
716 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
720 _asmCmd, /* assembler command and arguments */
721 NULL, /* alternate macro based form */
722 NULL, /* arguments for debug mode */
723 NULL, /* arguments for normal mode */
724 0, /* print externs as global */
725 ".asm", /* assembler file extension */
726 NULL /* no do_assemble function */
729 _linkCmd, /* linker command and arguments */
730 NULL, /* alternate macro based form */
731 NULL, /* no do_link function */
732 ".o", /* extension for object files */
733 0 /* no need for linker file */
745 3, /* fptr, far pointers (see Microchip) */
752 "XSEG (XDATA)", // xstack
753 "STACK (DATA)", // istack
754 "CSEG (CODE)", // code
755 "DSEG (DATA)", // data
756 "ISEG (DATA)", // idata
757 "XSEG (XDATA)", // xdata
759 "RSEG (DATA)", // reg
760 "GSINIT (CODE)", // static
761 "OSEG (OVR,DATA)", // overlay
762 "GSFINAL (CODE)", // post static
763 "HOME (CODE)", // home
766 NULL, // default location for auto vars
767 NULL, // default location for global vars
768 1 // code is read only 1=yes
771 NULL, /* genExtraAreaDeclaration */
772 NULL /* genExatrAreaLinkOptions */
775 /* stack related information */
776 -1, /* -1 stack grows downwards, +1 upwards */
777 1, /* extra overhead when calling between banks */
778 4, /* extra overhead when the function is an ISR */
779 1, /* extra overhead for a function call */
780 1, /* re-entrant space */
781 0 /* 'banked' call overhead, mild overlap with bank_overhead */
783 /* pic16 has an 8 bit mul */
791 _pic16_finaliseOptions,
792 _pic16_setDefaultOptions,
793 pic16_assignRegisters,
796 _pic16_genAssemblerPreamble,
797 NULL, /* no genAssemblerEnd */
799 NULL, // _pic16_genXINIT
800 _pic16_reset_regparm,
802 _process_pragma, /* process a pragma */
803 _pic16_mangleFunctionName, /* mangles function name */
805 hasExtBitOp, /* hasExtBitOp */
806 oclsExpense, /* oclsExpense */
808 TRUE, /* little endian */
811 1, /* transform <= to ! > */
812 1, /* transform >= to ! < */
813 1, /* transform != to !(a == b) */
815 FALSE, /* No array initializer support. */
816 0, /* no CSE cost estimation yet */
817 NULL, /* no builtin functions */
818 GPOINTER, /* treat unqualified pointers as "generic" pointers */
819 1, /* reset labelKey to 1 */
820 1, /* globals & local static allowed */