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));
402 _pic16_finaliseOptions (void)
404 port->mem.default_local_map = data;
405 port->mem.default_globl_map = data;
407 options.all_callee_saves = 1; // always callee saves
409 setMainValue("mcu", pic16_processor_base_name() );
410 addSet(&preArgvSet, Safe_strdup("-DMCU={mcu}"));
414 /* all the rest is commented ifdef'd out */
416 /* Hack-o-matic: if we are using the flat24 model,
417 * adjust pointer sizes.
419 if (options.model == MODEL_FLAT24)
422 fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
423 "for DS80C390 support. This code generator is "
424 "badly out of date and probably broken.\n");
426 port->s.fptr_size = 3;
427 port->s.gptr_size = 4;
428 port->stack.isr_overhead++; /* Will save dpx on ISR entry. */
430 port->stack.call_overhead++; /* This acounts for the extra byte
431 * of return addres on the stack.
432 * but is ugly. There must be a
436 fReturn = fReturn390;
440 if (options.model == MODEL_LARGE)
442 port->mem.default_local_map = xdata;
443 port->mem.default_globl_map = xdata;
447 port->mem.default_local_map = data;
448 port->mem.default_globl_map = data;
451 if (options.stack10bit)
453 if (options.model != MODEL_FLAT24)
456 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
457 fprintf (stderr, "\t10 bit stack mode disabled.\n");
458 options.stack10bit = 0;
462 /* Fixup the memory map for the stack; it is now in
463 * far space and requires a FPOINTER to access it.
466 istack->ptrType = FPOINTER;
473 _pic16_setDefaultOptions (void)
475 /* initialize to defaults section locations, names and addresses */
476 pic16_sectioninfo.at_udata = "udata";
477 pic16_sectioninfo.at_udataacs = "udata_acs";
478 pic16_sectioninfo.at_udataovr = "udata_ovr";
479 pic16_sectioninfo.at_udatashr = "udata_shr";
481 /* initialize to nothing, so let linker decide about their names */
482 pic16_sectioninfo.name_code =
483 pic16_sectioninfo.name_idata =
484 pic16_sectioninfo.name_udata =
485 pic16_sectioninfo.name_udataacs =
486 pic16_sectioninfo.name_udataovr =
487 pic16_sectioninfo.name_udatashr = "";
489 /* initialize to -1, so let linker decide about their address */
490 pic16_sectioninfo.addr_code =
491 pic16_sectioninfo.addr_idata =
492 pic16_sectioninfo.addr_udata =
493 pic16_sectioninfo.addr_udataacs =
494 pic16_sectioninfo.addr_udataovr =
495 pic16_sectioninfo.addr_udatashr = -1;
499 _pic16_getRegName (struct regs *reg)
508 static char *_pic16_mangleFunctionName(char *sz)
510 // fprintf(stderr, "mangled function name: %s\n", sz);
518 _pic16_genAssemblerPreamble (FILE * of)
520 char *name = pic16_processor_base_name();
524 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
527 fprintf (of, "\tlist\tp=%s\n",&name[1]);
529 if(!pic16_options.omit_configw) {
530 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300001, pic16_getConfigWord(0x300001));
531 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300002, pic16_getConfigWord(0x300002));
532 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300003, pic16_getConfigWord(0x300003));
533 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300005, pic16_getConfigWord(0x300005));
534 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300006, pic16_getConfigWord(0x300006));
535 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300008, pic16_getConfigWord(0x300008));
536 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x300009, pic16_getConfigWord(0x300009));
537 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000a, pic16_getConfigWord(0x30000a));
538 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000b, pic16_getConfigWord(0x30000b));
539 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000c, pic16_getConfigWord(0x30000c));
540 fprintf (of, "\t__config 0x%x,0x%hhx\n", 0x30000d, pic16_getConfigWord(0x30000d));
543 fprintf (of, "\tradix dec\n");
546 /* Generate interrupt vector table. */
548 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
550 /* PIC18F family has only two interrupts, the high and the low
551 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
553 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
554 fprintf(of, "; RESET vector\n");
555 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
558 if(!pic16_options.omit_ivt) {
559 fprintf(of, "\tres 4\n");
562 fprintf(of, "; High priority interrupt vector 0x0008\n");
564 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
565 fprintf(of, "\tres\t12\n");
567 fprintf(of, "\tretfie\n");
568 fprintf(of, "\tres\t14\n");
571 fprintf(of, "; Low priority interrupt vector 0x0018\n");
573 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
575 fprintf(of, "\tretfie\n");
583 _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
585 // sym_link *test = NULL;
588 fprintf(stderr,"checking for native mult\n");
597 if ( IS_LITERAL (left))
599 fprintf(stderr,"left is lit\n");
601 val = OP_VALUE (IC_LEFT (ic));
603 else if ( IS_LITERAL (right))
605 fprintf(stderr,"right is lit\n");
607 val = OP_VALUE (IC_RIGHT (ic));
611 fprintf(stderr,"oops, neither is lit so no\n");
615 if ( getSize (test) <= 2)
617 fprintf(stderr,"yep\n");
620 fprintf(stderr,"nope\n");
626 /* Indicate which extended bit operations this port supports */
628 hasExtBitOp (int op, int size)
632 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
639 /* Indicate the expense of an access to an output storage class */
641 oclsExpense (struct memmap *oclass)
643 /* The IN_FARSPACE test is compatible with historical behaviour, */
644 /* but I don't think it is applicable to PIC. If so, please feel */
645 /* free to remove this test -- EEP */
646 if (IN_FARSPACE(oclass))
652 /** $1 is always the basename.
653 $2 is always the output file.
655 $l is the list of extra options that should be there somewhere...
656 MUST be terminated with a NULL.
658 static const char *_linkCmd[] =
660 "gplink", "\"$1.o\"", "-o $1", "$l", NULL
663 /* Sigh. This really is not good. For now, I recommend:
664 * sdcc -S -mpic16 file.c
665 * the -S option does not compile or link
667 static const char *_asmCmd[] =
669 "gpasm", "$l", "-c", "\"$1.asm\"", NULL
678 "MCU PIC16", /* Target name */
679 "p18f452", /* Processor */
682 TRUE, /* Emit glue around main */
683 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
687 _asmCmd, /* assembler command and arguments */
688 NULL, /* alternate macro based form */
689 NULL, /* arguments for debug mode */
690 NULL, /* arguments for normal mode */
691 0, /* print externs as global */
692 ".asm", /* assembler file extension */
693 NULL /* no do_assemble function */
696 _linkCmd, /* linker command and arguments */
697 NULL, /* alternate macro based form */
698 NULL, /* no do_link function */
699 ".o", /* extension for object files */
700 0 /* no need for linker file */
712 3, /* fptr, far pointers (see Microchip) */
719 "XSEG (XDATA)", // xstack
720 "STACK (DATA)", // istack
721 "CSEG (CODE)", // code
722 "DSEG (DATA)", // data
723 "ISEG (DATA)", // idata
724 "XSEG (XDATA)", // xdata
726 "RSEG (DATA)", // reg
727 "GSINIT (CODE)", // static
728 "OSEG (OVR,DATA)", // overlay
729 "GSFINAL (CODE)", // post static
730 "HOME (CODE)", // home
733 NULL, // default location for auto vars
734 NULL, // default location for global vars
735 1 // code is read only 1=yes
738 NULL, /* genExtraAreaDeclaration */
739 NULL /* genExatrAreaLinkOptions */
742 /* stack related information */
743 -1, /* -1 stack grows downwards, +1 upwards */
744 1, /* extra overhead when calling between banks */
745 4, /* extra overhead when the function is an ISR */
746 1, /* extra overhead for a function call */
747 1, /* re-entrant space */
748 0 /* 'banked' call overhead, mild overlap with bank_overhead */
750 /* pic16 has an 8 bit mul */
758 _pic16_finaliseOptions,
759 _pic16_setDefaultOptions,
760 pic16_assignRegisters,
763 _pic16_genAssemblerPreamble,
764 NULL, /* no genAssemblerEnd */
766 NULL, // _pic16_genXINIT
767 _pic16_reset_regparm,
769 _process_pragma, /* process a pragma */
770 _pic16_mangleFunctionName, /* mangles function name */
772 hasExtBitOp, /* hasExtBitOp */
773 oclsExpense, /* oclsExpense */
775 TRUE, /* little endian */
778 1, /* transform <= to ! > */
779 1, /* transform >= to ! < */
780 1, /* transform != to !(a == b) */
782 FALSE, /* No array initializer support. */
783 0, /* no CSE cost estimation yet */
784 NULL, /* no builtin functions */
785 GPOINTER, /* treat unqualified pointers as "generic" pointers */
786 1, /* reset labelKey to 1 */
787 1, /* globals & local static allowed */