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[] =
81 pic16_sectioninfo_t pic16_sectioninfo;
86 extern char *pic16_processor_base_name(void);
88 void pic16_pCodeInitRegisters(void);
90 void pic16_assignRegisters (ebbIndex *);
92 static int regParmFlg = 0; /* determine if we can register a parameter */
94 pic16_options_t pic16_options;
96 extern set *includeDirsSet;
97 extern set *dataDirsSet;
98 extern set *libFilesSet;
100 /* Also defined in gen.h, but the #include is commented out */
101 /* for an unknowned reason. - EEP */
102 void pic16_emitDebuggerSymbol (char *);
104 extern void pic16_emitConfigRegs(FILE *of);
105 extern void pic16_emitIDRegs(FILE *of);
112 asm_addTree (&asm_asxxxx_mapping);
113 pic16_pCodeInitRegisters();
118 _pic16_reset_regparm (void)
124 _pic16_regparm (sym_link * l, bool reentrant)
126 /* force all parameters via SEND/RECEIVE */
127 if(0 /*pic16_options.ip_stack*/) {
128 /* for this processor it is simple
129 * can pass only the first parameter in a register */
130 if(regParmFlg)return 0;
132 return 1; //regParmFlg;
134 /* otherwise pass all arguments in registers via SEND/RECEIVE */
141 int initsfpnt=0; /* set to 1 if source provides a pragma for stack
142 * so glue() later emits code to initialize stack/frame pointers */
145 set *sectNames=NULL; /* list of section listed in pragma directives */
146 set *sectSyms=NULL; /* list of symbols set in a specific section */
147 set *wparamList=NULL;
150 /* This is an experimental code for #pragma inline
151 and is temporarily disabled for 2.5.0 release */
152 set *asmInlineMap=NULL;
157 unsigned want_libc: 1;
158 unsigned want_libm: 1;
159 unsigned want_libio: 1;
160 unsigned want_libdebug: 1;
161 } libflags = { 0, 0, 0, 0, 0 };
165 _process_pragma(const char *sz)
167 static const char *WHITE = " \t\n";
168 static const char *WHITECOMMA = " \t\n,";
169 char *ptr = strtok((char *)sz, WHITE);
171 /* #pragma maxram [maxram] */
172 if (startsWith (ptr, "maxram")) {
173 char *maxRAM = strtok((char *)NULL, WHITE);
175 if (maxRAM != (char *)NULL) {
179 maxRAMVal = constVal(maxRAM);
180 maxRAMaddress = (int)floatFromVal(maxRAMVal);
181 pic16_setMaxRAM(maxRAMaddress);
187 /* #pragma stack [stack-position] [stack-len] */
188 if(startsWith(ptr, "stack")) {
189 char *stackPosS = strtok((char *)NULL, WHITE);
190 char *stackLenS = strtok((char *)NULL, WHITE);
197 fprintf (stderr, "%s:%d: #pragma stack [stack-pos] [stack-length] -- stack-position missing\n", filename, lineno-1);
199 return 0; /* considered an error */
202 stackPosVal = constVal( stackPosS );
203 stackPos = (unsigned int)floatFromVal( stackPosVal );
206 stackLenVal = constVal( stackLenS );
207 stackLen = (unsigned int)floatFromVal( stackLenVal );
212 fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
213 filename, lineno-1, stackLen, stackLen);
216 /* check sanity of stack */
217 if ((stackPos >> 8) != ((stackPos+stackLen-1) >> 8)) {
218 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
219 filename,lineno-1, stackPos, stackPos+stackLen-1);
223 if (stackPos < pic16->acsSplitOfs) {
224 fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
225 filename, lineno-1, stackPos, stackPos+stackLen-1, pic16->acsSplitOfs);
228 if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
229 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
230 filename, lineno-1, stackPos, stackPos+stackLen-1, 0xF00 + pic16->acsSplitOfs);
233 if (stackPos+stackLen > pic16->RAMsize) {
234 fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
235 filename, lineno-1, stackPos, stackPos+stackLen-1, pic16->RAMsize-1);
237 return 1; /* considered an error, but this reports "invalid pragma stack"... */
241 reg=newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
242 addSet(&pic16_fix_udata, reg);
244 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
245 addSet(&pic16_fix_udata, reg);
247 sym = newSymbol("stack", 0);
248 sprintf(sym->rname, "_%s", sym->name);
249 addSet(&publics, sym);
251 sym = newSymbol("stack_end", 0);
252 sprintf(sym->rname, "_%s", sym->name);
253 addSet(&publics, sym);
255 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
260 /* #pragma code [symbol] [location] */
261 if(startsWith(ptr, "code")) {
262 char *symname = strtok((char *)NULL, WHITE);
263 char *location = strtok((char *)NULL, WHITE);
267 if (!symname || !location) {
268 fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno-1);
270 return 1; /* considered an error, but this reports "invalid pragma code"... */
273 absS = Safe_calloc(1, sizeof(absSym));
274 sprintf(absS->name, "_%s", symname);
276 addr = constVal( location );
277 absS->address = (unsigned int)floatFromVal( addr );
279 if((absS->address % 2) != 0) {
281 fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
282 filename, lineno-1, absS->address);
285 addSet(&absSymSet, absS);
286 // fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
287 // __FILE__, __LINE__, symname, absS->address);
292 /* #pragma udata [section-name] [symbol] */
293 if(startsWith(ptr, "udata")) {
294 char *sectname = strtok((char *)NULL, WHITE);
295 char *symname = strtok((char *)NULL, WHITE);
301 if (!symname || !sectname) {
302 fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno-1);
304 return 1; /* considered an error, but this reports "invalid pragma code"... */
308 ssym = Safe_calloc(1, sizeof(sectSym));
309 ssym->name = Safe_calloc(1, strlen(symname)+2);
310 sprintf(ssym->name, "%s%s", port->fun_prefix, symname);
313 addSet(§Syms, ssym);
315 nsym = newSymbol(symname, 0);
316 strcpy(nsym->rname, ssym->name);
319 checkAddSym(&publics, nsym);
323 for(snam=setFirstItem(sectNames);snam;snam=setNextItem(sectNames)) {
324 if(!strcmp(sectname, snam->name)){ found=1; break; }
328 snam = Safe_calloc(1, sizeof(sectName));
329 snam->name = Safe_strdup( sectname );
330 snam->regsSet = NULL;
332 addSet(§Names, snam);
335 ssym->section = snam;
338 fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
339 ssym->name, snam->name, snam);
342 symname = strtok((char *)NULL, WHITE);
348 /* #pragma wparam function1[, function2[,...]] */
349 if(startsWith(ptr, "wparam")) {
350 char *fname = strtok((char *)NULL, WHITECOMMA);
354 fprintf(stderr, "PIC16 Warning: `%s' wparam pragma is obsolete. use function attribute `wparam' instead.\n", fname);
355 addSet(&wparamList, Safe_strdup(fname));
357 // debugf("passing with WREG to %s\n", fname);
358 fname = strtok((char *)NULL, WHITECOMMA);
364 /* #pragma library library_module */
365 if(startsWith(ptr, "library")) {
366 char *lmodule = strtok((char *)NULL, WHITE);
370 * c link the C library
371 * math link the math library
372 * io link the IO library
373 * debug link the debug libary
374 * anything else, will link as-is */
376 if(!strcmp(lmodule, "c"))libflags.want_libc = 1;
377 else if(!strcmp(lmodule, "math"))libflags.want_libm = 1;
378 else if(!strcmp(lmodule, "io"))libflags.want_libio = 1;
379 else if(!strcmp(lmodule, "debug"))libflags.want_libdebug = 1;
380 else if(!strcmp(lmodule, "ignore"))libflags.ignore = 1;
382 if(!libflags.ignore) {
383 fprintf(stderr, "link library %s\n", lmodule);
384 addSetHead(&libFilesSet, lmodule);
393 /* This is an experimental code for #pragma inline
394 and is temporarily disabled for 2.5.0 release */
395 if(startsWith(ptr, "inline")) {
396 char *tmp = strtok((char *)NULL, WHITECOMMA);
399 addSet(&asmInlineMap, Safe_strdup( tmp ));
400 tmp = strtok((char *)NULL, WHITECOMMA);
406 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
407 debugf("inline asm: `%s'\n", s);
418 #define REP_UDATA "--preplace-udata-with="
420 #define STACK_MODEL "--pstack-model="
421 #define OPT_BANKSEL "--obanksel="
423 #define ALT_ASM "--asm="
424 #define ALT_LINK "--link="
426 #define IVT_LOC "--ivt-loc="
427 #define NO_DEFLIBS "--nodefaultlibs"
428 #define MPLAB_COMPAT "--mplab-comp"
430 #define NL_OPT "--nl="
431 #define USE_CRT "--use-crt="
433 #define OFMSG_LRSUPPORT "--flr-support"
435 #define OPTIMIZE_GOTO "--optimize-goto"
436 #define OPTIMIZE_CMP "--optimize-cmp"
437 #define OPTIMIZE_DF "--optimize-df"
442 int pic16_mplab_comp=0;
443 extern int pic16_debug_verbose;
444 extern int pic16_ralloc_debug;
445 extern int pic16_pcode_verbose;
448 int pic16_enable_peeps=0;
449 int pic16_nl=0; /* 0 for LF, 1 for CRLF */
451 OPTION pic16_optionsTable[]= {
452 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
453 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
454 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
455 // { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
456 // { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
457 // { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
458 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
460 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
461 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
462 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
464 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
466 { 0, ALT_ASM, NULL, "Use alternative assembler"},
467 { 0, ALT_LINK, NULL, "Use alternative linker"},
469 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
470 { 0, IVT_LOC, NULL, "<nnnn> interrupt vector table location"},
471 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
472 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
473 { 0, "--fstack", &pic16_fstack, "enable stack optimizations"},
474 { 0, NL_OPT, NULL, "new line, \"lf\" or \"crlf\""},
475 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
476 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
477 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
478 { 0, OPTIMIZE_GOTO, NULL, "try to use (conditional) BRA instead of GOTO"},
479 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
480 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
481 { 0, "--num-func-alloc-regs", &pic16_options.CATregs, "dump number of temporary registers allocated for each function"},
483 { 'y', "--extended", &xinst, "enable Extended Instruction Set/Literal Offset Addressing mode"},
485 { 0, NULL, NULL, NULL}
489 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
491 extern char *getStringArg(const char *, char **, int *, int);
492 extern int getIntArg(const char *, char **, int *, int);
495 _pic16_parseOptions (int *pargc, char **argv, int *i)
500 /* TODO: allow port-specific command line options to specify
501 * segment names here.
504 /* check for arguments that have associated an integer variable */
505 while(pic16_optionsTable[j].pparameter) {
506 if(ISOPT( pic16_optionsTable[j].longOpt )) {
507 (*pic16_optionsTable[j].pparameter)++;
513 if(ISOPT(STACK_MODEL)) {
514 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
515 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
516 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
518 fprintf(stderr, "Unknown stack model: %s", stkmodel);
524 if(ISOPT(OPT_BANKSEL)) {
525 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
529 if(ISOPT(REP_UDATA)) {
530 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
535 alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
539 if(ISOPT(ALT_LINK)) {
540 alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
545 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
546 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
553 tmp = Safe_strdup( getStringArg(NL_OPT, argv, i, *pargc) );
554 if(!STRCASECMP(tmp, "lf"))pic16_nl = 0;
555 else if(!STRCASECMP(tmp, "crlf"))pic16_nl = 1;
557 fprintf(stderr, "invalid termination character id\n");
564 pic16_options.no_crt = 0;
565 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
571 if(ISOPT(OFMSG_LRSUPPORT)) {
572 pic16_options.opt_flags |= OF_LR_SUPPORT;
577 if (ISOPT(OPTIMIZE_GOTO)) {
578 pic16_options.opt_flags |= OF_OPTIMIZE_GOTO;
582 if(ISOPT(OPTIMIZE_CMP)) {
583 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
587 if (ISOPT(OPTIMIZE_DF)) {
588 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
596 extern set *userIncDirsSet;
598 static void _pic16_initPaths(void)
600 char pic16incDir[512];
601 char pic16libDir[512];
602 set *pic16incDirsSet=NULL;
603 set *pic16libDirsSet=NULL;
606 setMainValue("mcu", pic16->name[2] );
607 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
609 setMainValue("mcu1", pic16->name[1] );
610 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
612 sprintf(pic16incDir, "%s%cpic16", INCLUDE_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
613 sprintf(pic16libDir, "%s%cpic16", LIB_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
616 if(!options.nostdinc) {
617 /* setup pic16 include directory */
618 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
619 includeDirsSet = pic16incDirsSet;
620 // mergeSets(&includeDirsSet, pic16incDirsSet);
622 /* pic16 port should not search to the SDCC standard include directories,
623 * so add here the deleted include dirs that user has issued in command line */
624 mergeSets(&pic16incDirsSet, userIncDirsSet);
626 if(!options.nostdlib) {
627 /* setup pic16 library directory */
628 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
629 libDirsSet = pic16libDirsSet;
630 // mergeSets(&libDirsSet, pic16libDirsSet);
633 if(!pic16_options.nodefaultlibs) {
634 /* now add the library for the device */
635 sprintf(devlib, "%s.lib", pic16->name[2]);
636 addSet(&libFilesSet, Safe_strdup(devlib));
638 /* add the internal SDCC library */
639 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
643 extern set *linkOptionsSet;
644 char *msprintf(hTab *pvals, const char *pformat, ...);
645 int my_system(const char *cmd);
647 /* forward declarations */
648 extern const char *pic16_linkCmd[];
649 extern const char *pic16_asmCmd[];
650 extern set *asmOptionsSet;
652 /* custom function to link objects */
653 static void _pic16_linkEdit(void)
655 hTab *linkValues=NULL;
663 * link command format:
664 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
667 sprintf(lfrm, "{linker} {incdirs} {lflags} -w -r -o {outfile} {user_ofile} {ofiles} {spec_ofiles} {libs}");
669 shash_add(&linkValues, "linker", pic16_linkCmd[0]);
671 mergeSets(&tSet, libDirsSet);
672 mergeSets(&tSet, libPathsSet);
674 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
675 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
677 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
679 if(fullSrcFileName) {
680 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName);
681 // addSetHead(&relFilesSet, Safe_strdup(temp));
682 shash_add(&linkValues, "user_ofile", temp);
685 if(!pic16_options.no_crt)
686 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
688 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
690 if(!libflags.ignore) {
691 if(libflags.want_libc)
692 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
694 if(libflags.want_libm)
695 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
697 if(libflags.want_libio) {
698 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
699 addSet(&libFilesSet, Safe_strdup(temp));
702 if(libflags.want_libdebug)
703 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
706 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
708 lcmd = msprintf(linkValues, lfrm);
710 ret = my_system( lcmd );
720 _pic16_finaliseOptions (void)
722 port->mem.default_local_map = data;
723 port->mem.default_globl_map = data;
725 /* peepholes are disabled for the time being */
728 /* explicit enable peepholes for testing */
729 if(pic16_enable_peeps)
732 options.all_callee_saves = 1; // always callee saves
735 options.float_rent = 1;
736 options.intlong_rent = 1;
740 if(alt_asm && strlen(alt_asm))
741 pic16_asmCmd[0] = alt_asm;
743 if(alt_link && strlen(alt_link))
744 pic16_linkCmd[0] = alt_link;
746 if(!pic16_options.no_crt) {
747 pic16_options.omit_ivt = 1;
748 pic16_options.leave_reset = 0;
751 if(options.model == MODEL_SMALL)
752 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
754 if(options.model == MODEL_LARGE)
755 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
760 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
761 *(strrchr(buf, 'f')) = 'F';
762 addSet(&asmOptionsSet, Safe_strdup( buf ));
765 if(STACK_MODEL_LARGE) {
766 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
767 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
769 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
770 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
776 if (options.model == MODEL_LARGE)
778 port->mem.default_local_map = xdata;
779 port->mem.default_globl_map = xdata;
783 port->mem.default_local_map = data;
784 port->mem.default_globl_map = data;
787 if (options.stack10bit)
789 if (options.model != MODEL_FLAT24)
792 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
793 fprintf (stderr, "\t10 bit stack mode disabled.\n");
794 options.stack10bit = 0;
798 /* Fixup the memory map for the stack; it is now in
799 * far space and requires a FPOINTER to access it.
802 istack->ptrType = FPOINTER;
809 _pic16_setDefaultOptions (void)
811 options.stackAuto = 0; /* implicit declaration */
812 /* port is not capable yet to allocate separate registers
813 * dedicated for passing certain parameters */
815 /* initialize to defaults section locations, names and addresses */
816 pic16_sectioninfo.at_udata = "udata";
818 /* set pic16 port options to defaults */
819 pic16_options.no_banksel = 0;
820 pic16_options.opt_banksel = 0;
821 pic16_options.omit_configw = 0;
822 pic16_options.omit_ivt = 0;
823 pic16_options.leave_reset = 0;
824 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
825 pic16_options.ivt_loc = 0x000000;
826 pic16_options.nodefaultlibs = 0;
827 pic16_options.dumpcalltree = 0;
828 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
829 pic16_options.no_crt = 0; /* use crt by default */
830 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
831 pic16_options.gstack = 0;
832 pic16_options.debgen = 0;
833 pic16_options.CATregs = 0;
837 _pic16_getRegName (struct regs *reg)
846 static char *_pic16_mangleFunctionName(char *sz)
848 // fprintf(stderr, "mangled function name: %s\n", sz);
856 _pic16_genAssemblerPreamble (FILE * of)
858 char *name = pic16_processor_base_name();
862 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
865 fprintf (of, "\tlist\tp=%s\n",&name[1]);
866 if (pic16_mplab_comp) {
867 // provide ACCESS macro used during SFR accesses
868 fprintf (of, "\tinclude <p%s.inc>\n", &name[1]);
871 if(!pic16_options.omit_configw) {
872 pic16_emitConfigRegs(of);
874 pic16_emitIDRegs(of);
877 fprintf (of, "\tradix dec\n");
880 /* Generate interrupt vector table. */
882 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
885 /* PIC18F family has only two interrupts, the high and the low
886 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
888 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
889 fprintf(of, "; RESET vector\n");
890 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
893 if(!pic16_options.omit_ivt) {
894 fprintf(of, "\tres 4\n");
897 fprintf(of, "; High priority interrupt vector 0x0008\n");
899 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
900 fprintf(of, "\tres\t12\n");
902 fprintf(of, "\tretfie\n");
903 fprintf(of, "\tres\t14\n");
906 fprintf(of, "; Low priority interrupt vector 0x0018\n");
908 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
910 fprintf(of, "\tretfie\n");
917 /* return True if the port can handle the type,
918 * False to convert it to function call */
919 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
921 //fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
922 int symL, symR, symRes, sizeL = 0, sizeR = 0, sizeRes = 0;
924 /* left/right are symbols? */
925 symL = IS_SYMOP(IC_LEFT(ic));
926 symR = IS_SYMOP(IC_RIGHT(ic));
927 symRes = IS_SYMOP(IC_RESULT(ic));
929 /* --> then determine their sizes */
930 sizeL = symL ? getSize(OP_SYM_TYPE(IC_LEFT(ic))) : 4;
931 sizeR = symR ? getSize(OP_SYM_TYPE(IC_RIGHT(ic))) : 4;
932 sizeRes = symRes ? getSize(OP_SYM_TYPE(IC_RESULT(ic))) : 4;
934 /* Checks to enable native multiplication.
935 * PICs do not offer native division at all...
938 * ( i) if result is just one byte, use native MUL
939 * (regardless of the operands)
940 * ( ii) if left and right are unsigned 8-bit operands,
942 * (iii) if left or right is a literal in the range of [-128..256)
943 * and the other is an unsigned byte, use native MUL
947 /* use native mult for `*: <?> x <?> --> {u8_t, s8_t}' */
948 if (sizeRes == 1) { return TRUE; }
950 /* use native mult for `u8_t x u8_t --> { u16_t, s16_t }' */
951 if (sizeL == 1 && symL /*&& SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic)))*/) {
954 //printf( "%s: left too large (%u) / signed (%u)\n", __FUNCTION__, sizeL, symL && !SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic))));
957 if (sizeR == 1 && symR /*&& SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic)))*/) {
960 //printf( "%s: right too large (%u) / signed (%u)\n", __FUNCTION__, sizeR, symR && !SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic))));
964 /* also allow literals [-128..256) for left/right operands */
965 if (IS_VALOP(IC_LEFT(ic)))
967 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
969 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
970 if (l >= -128 && l < 256)
974 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
977 if (IS_VALOP( IC_RIGHT(ic) ))
979 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
981 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
982 if (l >= -128 && l < 256)
986 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
990 /* use native mult iff left and right are (unsigned) 8-bit operands */
991 if (sizeL == 1 && sizeR == 1) { return TRUE; }
994 if (ic->op == '/' || ic->op == '%')
996 /* We must catch /: {u8_t,s8_t} x {u8_t,s8_t} --> {u8_t,s8_t},
997 * because SDCC will call 'divuchar' even for u8_t / s8_t.
998 * Example: 128 / -2 becomes 128 / 254 = 0 != -64... */
999 if (sizeL == 1 && sizeR == 1) return TRUE;
1001 /* What about literals? */
1002 if (IS_VALOP( IC_LEFT(ic) ))
1004 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1006 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1007 if (l >= -128 && l < 256)
1011 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1014 if (IS_VALOP( IC_RIGHT(ic) ))
1016 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1018 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1019 if (l >= -128 && l < 256)
1023 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1026 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1034 /* Do CSE estimation */
1035 static bool cseCostEstimation (iCode *ic, iCode *pdic)
1037 // operand *result = IC_RESULT(ic);
1038 // sym_link *result_type = operandType(result);
1041 /* VR -- this is an adhoc. Put here after conversation
1042 * with Erik Epetrich */
1046 || ic->op == EQ_OP) {
1048 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
1053 /* if it is a pointer then return ok for now */
1054 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
1056 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
1057 so we will cse only if they are local (i.e. both ic & pdic belong to
1058 the same basic block */
1059 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
1060 /* then if they are the same Basic block then ok */
1061 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
1066 /* for others it is cheaper to do the cse */
1072 /* Indicate which extended bit operations this port supports */
1074 hasExtBitOp (int op, int size)
1078 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
1085 /* Indicate the expense of an access to an output storage class */
1087 oclsExpense (struct memmap *oclass)
1089 /* The IN_FARSPACE test is compatible with historical behaviour, */
1090 /* but I don't think it is applicable to PIC. If so, please feel */
1091 /* free to remove this test -- EEP */
1092 if (IN_FARSPACE(oclass))
1098 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
1099 $2 is always the output file.
1100 $3 -L path and -l libraries
1101 $l is the list of extra options that should be there somewhere...
1102 MUST be terminated with a NULL.
1104 const char *pic16_linkCmd[] =
1106 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"","$3", NULL
1111 /** $1 is always the basename.
1112 $2 is always the output file.
1113 $3 varies (nothing currently)
1114 $l is the list of extra options that should be there somewhere...
1115 MUST be terminated with a NULL.
1117 const char *pic16_asmCmd[] =
1119 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1128 "MCU PIC16", /* Target name */
1129 "p18f452", /* Processor */
1132 TRUE, /* Emit glue around main */
1133 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1137 pic16_asmCmd, /* assembler command and arguments */
1138 NULL, /* alternate macro based form */
1139 "-g", /* arguments for debug mode */
1140 NULL, /* arguments for normal mode */
1141 0, /* print externs as global */
1142 ".asm", /* assembler file extension */
1143 NULL /* no do_assemble function */
1146 NULL, // pic16_linkCmd, /* linker command and arguments */
1147 NULL, /* alternate macro based form */
1148 _pic16_linkEdit, //NULL, /* no do_link function */
1149 ".o", /* extension for object files */
1150 0 /* no need for linker file */
1162 3, /* fptr, far pointers (see Microchip) */
1169 /* generic pointer tags */
1178 "XSEG (XDATA)", // xstack
1179 "STACK (DATA)", // istack
1180 "CSEG (CODE)", // code
1181 "DSEG (DATA)", // data
1182 "ISEG (DATA)", // idata
1183 "PSEG (DATA)", // pdata
1184 "XSEG (XDATA)", // xdata
1185 "BSEG (BIT)", // bit
1186 "RSEG (DATA)", // reg
1187 "GSINIT (CODE)", // static
1188 "OSEG (OVR,DATA)", // overlay
1189 "GSFINAL (CODE)", // post static
1190 "HOME (CODE)", // home
1193 "CONST (CODE)", // const_name - const data (code or not)
1194 NULL, // default location for auto vars
1195 NULL, // default location for global vars
1196 1 // code is read only 1=yes
1199 NULL, /* genExtraAreaDeclaration */
1200 NULL /* genExatrAreaLinkOptions */
1203 /* stack related information */
1204 -1, /* -1 stack grows downwards, +1 upwards */
1205 1, /* extra overhead when calling between banks */
1206 4, /* extra overhead when the function is an ISR */
1207 1, /* extra overhead for a function call */
1208 1, /* re-entrant space */
1209 0 /* 'banked' call overhead, mild overlap with bank_overhead */
1211 /* pic16 has an 8 bit mul */
1216 pic16_emitDebuggerSymbol
1219 255/3, /* maxCount */
1220 3, /* sizeofElement */
1221 /* The rest of these costs are bogus. They approximate */
1222 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
1223 {4,4,4}, /* sizeofMatchJump[] */
1224 {0,0,0}, /* sizeofRangeCompare[] */
1225 0, /* sizeofSubtract */
1226 3, /* sizeofDispatch */
1230 _pic16_parseOptions,
1233 _pic16_finaliseOptions,
1234 _pic16_setDefaultOptions,
1235 pic16_assignRegisters,
1238 _pic16_genAssemblerPreamble,
1239 NULL, /* no genAssemblerEnd */
1241 NULL, // _pic16_genXINIT
1242 NULL, /* genInitStartup */
1243 _pic16_reset_regparm,
1245 _process_pragma, /* process a pragma */
1246 _pic16_mangleFunctionName, /* mangles function name */
1248 hasExtBitOp, /* hasExtBitOp */
1249 oclsExpense, /* oclsExpense */
1251 TRUE, /* little endian */
1254 1, /* transform <= to ! > */
1255 1, /* transform >= to ! < */
1256 1, /* transform != to !(a == b) */
1258 FALSE, /* No array initializer support. */
1259 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
1260 NULL, /* no builtin functions */
1261 GPOINTER, /* treat unqualified pointers as "generic" pointers */
1262 1, /* reset labelKey to 1 */
1263 1, /* globals & local static allowed */