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[] =
70 pic16_sectioninfo_t pic16_sectioninfo;
73 extern char *pic16_processor_base_name(void);
75 void pic16_pCodeInitRegisters(void);
77 void pic16_assignRegisters (eBBlock ** ebbs, int count);
79 static int regParmFlg = 0; /* determine if we can register a parameter */
81 pic16_options_t pic16_options;
86 asm_addTree (&asm_asxxxx_mapping);
87 pic16_pCodeInitRegisters();
91 /* set pic16 port options to defaults */
92 pic16_options.gen_banksel = 0;
93 pic16_options.opt_banksel = 0;
94 pic16_options.omit_configw = 0;
95 pic16_options.omit_ivt = 0;
96 pic16_options.leave_reset = 0;
97 pic16_options.enable_stack = 0;
98 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
102 _pic16_reset_regparm ()
108 _pic16_regparm (sym_link * l)
110 /* for this processor it is simple
111 can pass only the first parameter in a register */
121 _process_pragma(const char *sz)
123 static const char *WHITE = " \t";
124 char *ptr = strtok((char *)sz, WHITE);
126 if (startsWith (ptr, "maxram")) {
127 char *maxRAM = strtok((char *)NULL, WHITE);
129 if (maxRAM != (char *)NULL) {
133 maxRAMVal = constVal(maxRAM);
134 maxRAMaddress = (int)floatFromVal(maxRAMVal);
135 pic16_setMaxRAM(maxRAMaddress);
139 if(startsWith(ptr, "stack")) {
140 char *stackPosS = strtok((char *)NULL, WHITE);
143 // fprintf(stderr, "Initializing stack pointer to 0x%x\n", (int)floatFromVal(constVal(stackPos)));
144 stackPosVal = constVal( stackPosS );
145 stackPos = (unsigned int)floatFromVal( stackPosVal );
151 #define REP_UDATA "--preplace-udata-with="
152 #define REP_UDATAACS "--preplace-udata-acs-with="
153 #define REP_UDATAOVR "--preplace-udata-ovr-with="
154 #define REP_UDATASHR "--preplace-udata-sht-with="
156 #define NAME_CODE "--psection-code-name="
157 #define NAME_IDATA "--psection-idata-name="
158 #define NAME_UDATA "--psection-udata-name="
159 #define NAME_UDATAACS "--psection-udata-acs-name="
160 #define NAME_UDATAOVR "--psection-udata-ovr-name="
161 #define NAME_UDATASHR "--psection-udata-shr-name="
163 #define ADDR_CODE "--psection-code-addr="
164 #define ADDR_IDATA "--psection-idata-addr="
165 #define ADDR_UDATA "--psection-udata-addr="
166 #define ADDR_UDATAACS "--psection-udata-acs-addr="
167 #define ADDR_UDATAOVR "--psection-udata-ovr-addr="
168 #define ADDR_UDATASHR "--psection-udata-shr-addr="
170 #define STACK_MODEL "--pstack-model="
171 #define OPT_BANKSEL "--obanksel="
174 extern int pic16_debug_verbose;
176 OPTION pic16_optionsTable[]= {
177 { 0, "--pgen-banksel", &pic16_options.gen_banksel, "generate BANKSEL assembler directives"},
178 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
179 { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
180 { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
181 { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
182 { 0, "--penable-stack", &pic16_options.enable_stack, "enable the use of stack"},
183 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
185 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
186 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
189 /* these may not be in any use -- VR */
190 { 0, AT_UDATAACS, NULL, "Emit udata_acs variables at another section"},
191 { 0, AT_UDATAOVR, NULL, "Emit udata_ovr variables at another section"},
192 { 0, AT_UDATASHR, NULL, "Emit udata_shr variables at another section"},
196 /* commented out for the time being -- VR */
197 { 0, NAME_CODE, NULL, "Set code section name[,address]"},
198 { 0, NAME_IDATA, NULL, "Set idata section name[,address]"},
199 { 0, NAME_UDATA, NULL, "Set udata section name[,address]"},
200 { 0, NAME_UDATAACS, NULL, "Set udata_acs section name[,address]"},
201 { 0, NAME_UDATAOVR, NULL, "Set udata_ovr section name[,address]"},
202 { 0, NAME_UDATASHR, NULL, "Set udata_shr section name[,address]"},
204 { 0, ADDR_CODE, NULL, "Set code section address"},
205 { 0, ADDR_IDATA, NULL, "Set idata section address"},
206 { 0, ADDR_UDATA, NULL, "Set udata section address"},
207 { 0, ADDR_UDATAACS, NULL, "Set udata_acs section address"},
208 { 0, ADDR_UDATAOVR, NULL, "Set udata_ovr section address"},
209 { 0, ADDR_UDATASHR, NULL, "Set udata_shr section address"},
212 { 0, NULL, NULL, NULL}
216 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
218 extern char *getStringArg(const char *, char **, int *, int);
219 extern int getIntArg(const char *, char **, int *, int);
222 _pic16_parseOptions (int *pargc, char **argv, int *i)
227 /* TODO: allow port-specific command line options to specify
228 * segment names here.
230 /* check for arguments that have associated an integer variable */
231 while(pic16_optionsTable[j].pparameter) {
232 if(ISOPT( pic16_optionsTable[j].longOpt )) {
233 (*pic16_optionsTable[j].pparameter)++;
240 if(ISOPT(STACK_MODEL)) {
241 switch(*getStringArg(STACK_MODEL, argv, i, *pargc)) {
242 case 's': pic16_options.stack_model = 0; break;
243 case 'l': pic16_options.stack_model = 1; break;
246 fprintf(stderr, "Unknown stack model");
252 if(ISOPT(OPT_BANKSEL)) {
253 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
257 if(ISOPT(REP_UDATA)) {
258 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
263 if(ISOPT(AT_UDATAACS)) {
264 pic16_sectioninfo.at_udataacs = Safe_strdup( getStringArg(AT_UDATAACS, argv, i, *pargc));
268 if(ISOPT(AT_UDATAOVR)) {
269 pic16_sectioninfo.at_udataovr = Safe_strdup( getStringArg(AT_UDATAOVR, argv, i, *pargc));
273 if(ISOPT(AT_UDATASHR)) {
274 pic16_sectioninfo.at_udatashr = Safe_strdup( getStringArg(AT_UDATASHR, argv, i, *pargc));
280 if(ISOPT(ADDR_CODE)) {
281 pic16_sectioninfo.addr_code = getIntArg(ADDR_CODE, argv, i, *pargc);
285 if(ISOPT(ADDR_IDATA)) {
286 pic16_sectioninfo.addr_idata = getIntArg(ADDR_IDATA, argv, i, *pargc);
290 if(ISOPT(ADDR_UDATA)) {
291 pic16_sectioninfo.addr_udata = getIntArg(ADDR_UDATA, argv, i, *pargc);
295 if(ISOPT(ADDR_UDATAACS)) {
296 pic16_sectioninfo.addr_udataacs = getIntArg(ADDR_UDATAACS, argv, i, *pargc);
300 if(ISOPT(ADDR_UDATAOVR)) {
301 pic16_sectioninfo.addr_udataovr = getIntArg(ADDR_UDATAOVR, argv, i, *pargc);
305 if(ISOPT(ADDR_UDATASHR)) {
306 pic16_sectioninfo.addr_udatashr = getIntArg(ADDR_UDATASHR, argv, i, *pargc);
312 if(ISOPT(NAME_CODE)) {
313 setParseWithComma(&tset, getStringArg(NAME_CODE, argv, i, *pargc));
314 if(elementsInSet(tset) > 0)
315 pic16_sectioninfo.name_code = Safe_strdup( (char *)indexSet(tset, 0));
316 if(elementsInSet(tset) > 1)
317 pic16_sectioninfo.addr_code = atoi( (char *)indexSet(tset, 1));
321 if(ISOPT(NAME_IDATA)) {
322 setParseWithComma(&tset, getStringArg(NAME_IDATA, argv, i, *pargc));
323 if(elementsInSet(tset) > 0)
324 pic16_sectioninfo.name_idata = Safe_strdup( (char *)indexSet(tset, 0));
325 if(elementsInSet(tset) > 1)
326 pic16_sectioninfo.addr_idata = atoi( (char *)indexSet(tset, 1));
330 if(ISOPT(NAME_UDATA)) {
331 setParseWithComma(&tset, getStringArg(NAME_UDATA, argv, i, *pargc));
332 if(elementsInSet(tset) > 0)
333 pic16_sectioninfo.name_udata = Safe_strdup( (char *)indexSet(tset, 0));
334 if(elementsInSet(tset) > 1)
335 pic16_sectioninfo.addr_udata = atoi( (char *)indexSet(tset, 1));
339 if(ISOPT(NAME_UDATAACS)) {
340 setParseWithComma(&tset, getStringArg(NAME_UDATAACS, argv, i, *pargc));
341 if(elementsInSet(tset) > 0)
342 pic16_sectioninfo.name_udataacs = Safe_strdup( (char *)indexSet(tset, 0));
343 if(elementsInSet(tset) > 1)
344 pic16_sectioninfo.addr_udataacs = atoi( (char *)indexSet(tset, 1));
348 if(ISOPT(NAME_UDATAOVR)) {
349 setParseWithComma(&tset, getStringArg(NAME_UDATAOVR, argv, i, *pargc));
350 if(elementsInSet(tset) > 0)
351 pic16_sectioninfo.name_udataovr = Safe_strdup( (char *)indexSet(tset, 0));
352 if(elementsInSet(tset) > 1)
353 pic16_sectioninfo.addr_udataovr = atoi( (char *)indexSet(tset, 1));
357 if(ISOPT(NAME_UDATASHR)) {
358 setParseWithComma(&tset, getStringArg(NAME_UDATASHR, argv, i, *pargc));
359 if(elementsInSet(tset) > 0)
360 pic16_sectioninfo.name_udatashr = Safe_strdup( (char *)indexSet(tset, 0));
361 if(elementsInSet(tset) > 1)
362 pic16_sectioninfo.addr_udatashr = atoi( (char *)indexSet(tset, 1));
373 _pic16_finaliseOptions (void)
375 port->mem.default_local_map = data;
376 port->mem.default_globl_map = data;
378 setMainValue("mcu", pic16_processor_base_name() );
379 addSet(&preArgvSet, Safe_strdup("-DMCU={mcu}"));
383 /* all the rest is commented ifdef'd out */
385 /* Hack-o-matic: if we are using the flat24 model,
386 * adjust pointer sizes.
388 if (options.model == MODEL_FLAT24)
391 fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
392 "for DS80C390 support. This code generator is "
393 "badly out of date and probably broken.\n");
395 port->s.fptr_size = 3;
396 port->s.gptr_size = 4;
397 port->stack.isr_overhead++; /* Will save dpx on ISR entry. */
399 port->stack.call_overhead++; /* This acounts for the extra byte
400 * of return addres on the stack.
401 * but is ugly. There must be a
405 fReturn = fReturn390;
409 if (options.model == MODEL_LARGE)
411 port->mem.default_local_map = xdata;
412 port->mem.default_globl_map = xdata;
416 port->mem.default_local_map = data;
417 port->mem.default_globl_map = data;
420 if (options.stack10bit)
422 if (options.model != MODEL_FLAT24)
425 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
426 fprintf (stderr, "\t10 bit stack mode disabled.\n");
427 options.stack10bit = 0;
431 /* Fixup the memory map for the stack; it is now in
432 * far space and requires a FPOINTER to access it.
435 istack->ptrType = FPOINTER;
442 _pic16_setDefaultOptions (void)
444 /* initialize to defaults section locations, names and addresses */
445 pic16_sectioninfo.at_udata = "udata";
446 pic16_sectioninfo.at_udataacs = "udata_acs";
447 pic16_sectioninfo.at_udataovr = "udata_ovr";
448 pic16_sectioninfo.at_udatashr = "udata_shr";
450 /* initialize to nothing, so let linker decide about their names */
451 pic16_sectioninfo.name_code =
452 pic16_sectioninfo.name_idata =
453 pic16_sectioninfo.name_udata =
454 pic16_sectioninfo.name_udataacs =
455 pic16_sectioninfo.name_udataovr =
456 pic16_sectioninfo.name_udatashr = "";
458 /* initialize to -1, so let linker decide about their address */
459 pic16_sectioninfo.addr_code =
460 pic16_sectioninfo.addr_idata =
461 pic16_sectioninfo.addr_udata =
462 pic16_sectioninfo.addr_udataacs =
463 pic16_sectioninfo.addr_udataovr =
464 pic16_sectioninfo.addr_udatashr = -1;
468 _pic16_getRegName (struct regs *reg)
477 static char *_pic16_mangleFunctionName(char *sz)
479 fprintf(stderr, "mangled function name: %s\n", sz);
487 _pic16_genAssemblerPreamble (FILE * of)
489 char *name = pic16_processor_base_name();
493 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
496 fprintf (of, "\tlist\tp=%s\n",&name[1]);
498 if(!pic16_options.omit_configw) {
499 fprintf (of, "\t__config 0x%x,0x%x\n", 0x300001, pic16_getConfigWord(0x300001));
500 fprintf (of, "\t__config 0x%x,0x%x\n", 0x300002, pic16_getConfigWord(0x300002));
501 fprintf (of, "\t__config 0x%x,0x%x\n", 0x300003, pic16_getConfigWord(0x300003));
502 fprintf (of, "\t__config 0x%x,0x%x\n", 0x300005, pic16_getConfigWord(0x300005));
503 fprintf (of, "\t__config 0x%x,0x%x\n", 0x300006, pic16_getConfigWord(0x300006));
504 fprintf (of, "\t__config 0x%x,0x%x\n", 0x300008, pic16_getConfigWord(0x300008));
505 fprintf (of, "\t__config 0x%x,0x%x\n", 0x300009, pic16_getConfigWord(0x300009));
506 fprintf (of, "\t__config 0x%x,0x%x\n", 0x30000a, pic16_getConfigWord(0x30000a));
507 fprintf (of, "\t__config 0x%x,0x%x\n", 0x30000b, pic16_getConfigWord(0x30000b));
508 fprintf (of, "\t__config 0x%x,0x%x\n", 0x30000c, pic16_getConfigWord(0x30000c));
509 fprintf (of, "\t__config 0x%x,0x%x\n", 0x30000d, pic16_getConfigWord(0x30000d));
512 fprintf (of, "\tradix dec\n");
515 /* Generate interrupt vector table. */
517 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
519 /* PIC18F family has only two interrupts, the high and the low
520 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
522 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
523 fprintf(of, "; RESET vector\n");
524 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
527 if(!pic16_options.omit_ivt) {
528 fprintf(of, "\tres 4\n");
531 fprintf(of, "; High priority interrupt vector 0x0008\n");
533 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
534 fprintf(of, "\tres\t12\n");
536 fprintf(of, "\tretfie\n");
537 fprintf(of, "\tres\t14\n");
540 fprintf(of, "; Low priority interrupt vector 0x0018\n");
542 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
544 fprintf(of, "\tretfie\n");
552 _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
554 // sym_link *test = NULL;
557 fprintf(stderr,"checking for native mult\n");
566 if ( IS_LITERAL (left))
568 fprintf(stderr,"left is lit\n");
570 val = OP_VALUE (IC_LEFT (ic));
572 else if ( IS_LITERAL (right))
574 fprintf(stderr,"right is lit\n");
576 val = OP_VALUE (IC_RIGHT (ic));
580 fprintf(stderr,"oops, neither is lit so no\n");
584 if ( getSize (test) <= 2)
586 fprintf(stderr,"yep\n");
589 fprintf(stderr,"nope\n");
595 /* Indicate which extended bit operations this port supports */
597 hasExtBitOp (int op, int size)
601 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
608 /* Indicate the expense of an access to an output storage class */
610 oclsExpense (struct memmap *oclass)
612 /* The IN_FARSPACE test is compatible with historical behaviour, */
613 /* but I don't think it is applicable to PIC. If so, please feel */
614 /* free to remove this test -- EEP */
615 if (IN_FARSPACE(oclass))
621 /** $1 is always the basename.
622 $2 is always the output file.
624 $l is the list of extra options that should be there somewhere...
625 MUST be terminated with a NULL.
627 static const char *_linkCmd[] =
629 "gplink", "\"$1.o\"", "-o $1", "$l", NULL
632 /* Sigh. This really is not good. For now, I recommend:
633 * sdcc -S -mpic16 file.c
634 * the -S option does not compile or link
636 static const char *_asmCmd[] =
638 "gpasm", "-c -I/usr/local/share/gputils/header", "\"$1.asm\"", NULL
647 "MCU PIC16", /* Target name */
648 "p18f452", /* Processor */
651 TRUE, /* Emit glue around main */
652 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
656 _asmCmd, /* assembler command and arguments */
657 NULL, /* alternate macro based form */
658 NULL, /* arguments for debug mode */
659 NULL, /* arguments for normal mode */
660 0, /* print externs as global */
661 ".asm", /* assembler file extension */
662 NULL /* no do_assemble function */
665 _linkCmd, /* linker command and arguments */
666 NULL, /* alternate macro based form */
667 NULL, /* no do_link function */
668 ".rel" /* extension for object files */
680 2, /* fptr, this should be changed to 3 for far pointers (see Microchip) */
687 "XSEG (XDATA)", // xstack
688 "STACK (DATA)", // istack
689 "CSEG (CODE)", // code
690 "DSEG (DATA)", // data
691 "ISEG (DATA)", // idata
692 "XSEG (XDATA)", // xdata
694 "RSEG (DATA)", // reg
695 "GSINIT (CODE)", // static
696 "OSEG (OVR,DATA)", // overlay
697 "GSFINAL (CODE)", // post static
698 "HOME (CODE)", // home
701 NULL, // default location for auto vars
702 NULL, // default location for global vars
703 1 // code is read only 1=yes
706 NULL, /* genExtraAreaDeclaration */
707 NULL /* genExatrAreaLinkOptions */
710 /* stack related information */
711 -1, /* -1 stack grows downwards, +1 upwards */
712 1, /* extra overhead when calling between banks */
713 4, /* extra overhead when the function is an ISR */
714 1, /* extra overhead for a function call */
715 1, /* re-entrant space */
716 0 /* 'banked' call overhead, mild overlap with bank_overhead */
718 /* pic16 has an 8 bit mul */
726 _pic16_finaliseOptions,
727 _pic16_setDefaultOptions,
728 pic16_assignRegisters,
731 _pic16_genAssemblerPreamble,
732 NULL, /* no genAssemblerEnd */
734 NULL, // _pic16_genXINIT
735 _pic16_reset_regparm,
737 _process_pragma, /* process a pragma */
738 NULL, //_pic16_mangleFunctionName, /* mangles function name */
740 hasExtBitOp, /* hasExtBitOp */
741 oclsExpense, /* oclsExpense */
743 TRUE, /* little endian */
746 1, /* transform <= to ! > */
747 1, /* transform >= to ! < */
748 1, /* transform != to !(a == b) */
750 FALSE, /* No array initializer support. */
751 0, /* no CSE cost estimation yet */
752 NULL, /* no builtin functions */
753 GPOINTER, /* treat unqualified pointers as "generic" pointers */
754 1, /* reset labelKey to 1 */
755 1, /* globals & local static allowed */