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[] =
80 pic16_sectioninfo_t pic16_sectioninfo;
85 extern char *pic16_processor_base_name(void);
87 void pic16_pCodeInitRegisters(void);
89 void pic16_assignRegisters (ebbIndex *);
91 static int regParmFlg = 0; /* determine if we can register a parameter */
93 pic16_options_t pic16_options;
95 extern set *includeDirsSet;
96 extern set *dataDirsSet;
97 extern set *libFilesSet;
99 /* Also defined in gen.h, but the #include is commented out */
100 /* for an unknowned reason. - EEP */
101 void pic16_emitDebuggerSymbol (char *);
103 extern void pic16_emitConfigRegs(FILE *of);
104 extern void pic16_emitIDRegs(FILE *of);
111 asm_addTree (&asm_asxxxx_mapping);
112 pic16_pCodeInitRegisters();
117 _pic16_reset_regparm (void)
123 _pic16_regparm (sym_link * l, bool reentrant)
125 /* force all parameters via SEND/RECEIVE */
126 if(0 /*pic16_options.ip_stack*/) {
127 /* for this processor it is simple
128 * can pass only the first parameter in a register */
129 if(regParmFlg)return 0;
131 return 1; //regParmFlg;
133 /* otherwise pass all arguments in registers via SEND/RECEIVE */
140 int initsfpnt=0; /* set to 1 if source provides a pragma for stack
141 * so glue() later emits code to initialize stack/frame pointers */
144 set *sectNames=NULL; /* list of section listed in pragma directives */
145 set *sectSyms=NULL; /* list of symbols set in a specific section */
146 set *wparamList=NULL;
149 /* This is an experimental code for #pragma inline
150 and is temporarily disabled for 2.5.0 release */
151 set *asmInlineMap=NULL;
156 unsigned want_libc: 1;
157 unsigned want_libm: 1;
158 unsigned want_libio: 1;
159 unsigned want_libdebug: 1;
160 } libflags = { 0, 0, 0, 0, 0 };
172 do_pragma(int id, const char *name, const char *cp)
174 struct pragma_token_s token;
178 init_pragma_token(&token);
182 /* #pragma maxram [maxram] */
187 cp = get_pragma_token(cp, &token);
188 if (TOKEN_INT == token.type)
189 max_ram = token.val.int_val;
196 cp = get_pragma_token(cp, &token);
197 if (TOKEN_EOL != token.type)
203 pic16_setMaxRAM(max_ram);
207 /* #pragma stack [stack-position] [stack-len] */
210 unsigned int stackPos, stackLen;
214 cp = get_pragma_token(cp, &token);
215 if (TOKEN_INT != token.type)
220 stackPos = token.val.int_val;
222 cp = get_pragma_token(cp, &token);
223 if (TOKEN_INT != token.type)
228 stackLen = token.val.int_val;
230 cp = get_pragma_token(cp, &token);
231 if (TOKEN_EOL != token.type)
239 fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
240 filename, lineno, stackLen, stackLen);
243 /* check sanity of stack */
244 if ((stackPos >> 8) != ((stackPos + stackLen - 1) >> 8)) {
245 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
246 filename, lineno, stackPos, stackPos + stackLen - 1);
250 if (stackPos < pic16->acsSplitOfs) {
251 fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
252 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->acsSplitOfs);
255 if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
256 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
257 filename, lineno, stackPos, stackPos + stackLen - 1, 0xF00 + pic16->acsSplitOfs);
260 if (stackPos+stackLen > pic16->RAMsize) {
261 fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
262 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->RAMsize-1);
268 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
269 addSet(&pic16_fix_udata, reg);
271 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
272 addSet(&pic16_fix_udata, reg);
274 sym = newSymbol("stack", 0);
275 sprintf(sym->rname, "_%s", sym->name);
276 addSet(&publics, sym);
278 sym = newSymbol("stack_end", 0);
279 sprintf(sym->rname, "_%s", sym->name);
280 addSet(&publics, sym);
282 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
286 /* #pragma code [symbol] [location] */
291 cp = get_pragma_token(cp, &token);
292 if (TOKEN_STR != token.type)
295 absS = Safe_calloc(1, sizeof(absSym));
296 sprintf(absS->name, "_%s", get_pragma_string(&token));
298 cp = get_pragma_token(cp, &token);
299 if (TOKEN_INT != token.type)
302 //fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno);
306 absS->address = token.val.int_val;
308 cp = get_pragma_token(cp, &token);
309 if (TOKEN_EOL != token.type)
315 if ((absS->address % 2) != 0) {
317 fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
318 filename, lineno, absS->address);
321 addSet(&absSymSet, absS);
322 // fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
323 // __FILE__, __LINE__, symname, absS->address);
327 /* #pragma udata [section-name] [symbol] */
337 cp = get_pragma_token(cp, &token);
338 if (TOKEN_EOL == token.type)
341 sectname = Safe_strdup(get_pragma_string(&token));
343 cp = get_pragma_token(cp, &token);
344 if (TOKEN_EOL == token.type)
347 //fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno);
351 symname = get_pragma_string(&token);
353 cp = get_pragma_token(cp, &token);
354 if (TOKEN_EOL != token.type)
361 ssym = Safe_calloc(1, sizeof(sectSym));
362 ssym->name = Safe_calloc(1, strlen(symname) + 2);
363 sprintf(ssym->name, "%s%s", port->fun_prefix, symname);
366 addSet(§Syms, ssym);
368 nsym = newSymbol((char *)symname, 0);
369 strcpy(nsym->rname, ssym->name);
372 checkAddSym(&publics, nsym);
376 for (snam = setFirstItem(sectNames);snam;snam=setNextItem(sectNames)) {
377 if (!strcmp(sectname, snam->name)){ found=1; break; }
381 snam = Safe_calloc(1, sizeof(sectName));
382 snam->name = Safe_strdup(sectname);
383 snam->regsSet = NULL;
385 addSet(§Names, snam);
388 ssym->section = snam;
391 fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
392 ssym->name, snam->name, snam);
395 cp = get_pragma_token(cp, &token);
396 symname = (TOKEN_EOL != token.type) ? get_pragma_string(&token) : NULL;
403 /* #pragma library library_module */
408 cp = get_pragma_token(cp, &token);
409 if (TOKEN_EOL != token.type)
411 lmodule = get_pragma_string(&token);
414 * c link the C library
415 * math link the math library
416 * io link the IO library
417 * debug link the debug libary
418 * anything else, will link as-is */
420 if(!strcmp(lmodule, "c"))
421 libflags.want_libc = 1;
422 else if(!strcmp(lmodule, "math"))
423 libflags.want_libm = 1;
424 else if(!strcmp(lmodule, "io"))
425 libflags.want_libio = 1;
426 else if(!strcmp(lmodule, "debug"))
427 libflags.want_libdebug = 1;
428 else if(!strcmp(lmodule, "ignore"))
434 fprintf(stderr, "link library %s\n", lmodule);
435 addSetHead(&libFilesSet, (char *)lmodule);
445 cp = get_pragma_token(cp, &token);
446 if (TOKEN_EOL != token.type)
455 /* This is an experimental code for #pragma inline
456 and is temporarily disabled for 2.5.0 release */
459 char *tmp = strtok((char *)NULL, WHITECOMMA);
462 addSet(&asmInlineMap, Safe_strdup( tmp ));
463 tmp = strtok((char *)NULL, WHITECOMMA);
469 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
470 debugf("inline asm: `%s'\n", s);
482 get_pragma_token(cp, &token);
485 werror(W_BAD_PRAGMA_ARGUMENTS, name);
487 free_pragma_token(&token);
491 static struct pragma_s pragma_tbl[] = {
492 { "maxram", P_MAXRAM, 0, do_pragma },
493 { "stack", P_STACK, 0, do_pragma },
494 { "code", P_CODE, 0, do_pragma },
495 { "udata", P_UDATA, 0, do_pragma },
496 { "library", P_LIBRARY, 0, do_pragma },
497 /*{ "inline", P_INLINE, 0, do_pragma }, */
498 { NULL, 0, 0, NULL },
502 _process_pragma(const char *s)
504 return process_pragma_tbl(pragma_tbl, s);
507 #define REP_UDATA "--preplace-udata-with="
509 #define STACK_MODEL "--pstack-model="
510 #define OPT_BANKSEL "--obanksel="
512 #define ALT_ASM "--asm="
513 #define ALT_LINK "--link="
515 #define IVT_LOC "--ivt-loc="
516 #define NO_DEFLIBS "--nodefaultlibs"
517 #define MPLAB_COMPAT "--mplab-comp"
519 #define NL_OPT "--nl="
520 #define USE_CRT "--use-crt="
522 #define OFMSG_LRSUPPORT "--flr-support"
524 #define OPTIMIZE_GOTO "--optimize-goto"
525 #define OPTIMIZE_CMP "--optimize-cmp"
526 #define OPTIMIZE_DF "--optimize-df"
531 int pic16_mplab_comp=0;
532 extern int pic16_debug_verbose;
533 extern int pic16_ralloc_debug;
534 extern int pic16_pcode_verbose;
537 int pic16_enable_peeps=0;
538 int pic16_nl=0; /* 0 for LF, 1 for CRLF */
540 OPTION pic16_optionsTable[]= {
541 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
542 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
543 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
544 // { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
545 // { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
546 // { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
547 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
549 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
550 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
551 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
553 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
555 { 0, ALT_ASM, NULL, "Use alternative assembler"},
556 { 0, ALT_LINK, NULL, "Use alternative linker"},
558 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
559 { 0, IVT_LOC, NULL, "<nnnn> interrupt vector table location"},
560 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
561 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
562 { 0, "--fstack", &pic16_fstack, "enable stack optimizations"},
563 { 0, NL_OPT, NULL, "new line, \"lf\" or \"crlf\""},
564 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
565 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
566 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
567 { 0, OPTIMIZE_GOTO, NULL, "try to use (conditional) BRA instead of GOTO"},
568 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
569 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
570 { 0, "--num-func-alloc-regs", &pic16_options.CATregs, "dump number of temporary registers allocated for each function"},
572 { 'y', "--extended", &xinst, "enable Extended Instruction Set/Literal Offset Addressing mode"},
574 { 0, NULL, NULL, NULL}
578 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
580 extern char *getStringArg(const char *, char **, int *, int);
581 extern int getIntArg(const char *, char **, int *, int);
584 _pic16_parseOptions (int *pargc, char **argv, int *i)
589 /* TODO: allow port-specific command line options to specify
590 * segment names here.
593 /* check for arguments that have associated an integer variable */
594 while(pic16_optionsTable[j].pparameter) {
595 if(ISOPT( pic16_optionsTable[j].longOpt )) {
596 (*pic16_optionsTable[j].pparameter)++;
602 if(ISOPT(STACK_MODEL)) {
603 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
604 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
605 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
607 fprintf(stderr, "Unknown stack model: %s", stkmodel);
613 if(ISOPT(OPT_BANKSEL)) {
614 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
618 if(ISOPT(REP_UDATA)) {
619 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
624 alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
628 if(ISOPT(ALT_LINK)) {
629 alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
634 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
635 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
642 tmp = Safe_strdup( getStringArg(NL_OPT, argv, i, *pargc) );
643 if(!STRCASECMP(tmp, "lf"))pic16_nl = 0;
644 else if(!STRCASECMP(tmp, "crlf"))pic16_nl = 1;
646 fprintf(stderr, "invalid termination character id\n");
653 pic16_options.no_crt = 0;
654 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
660 if(ISOPT(OFMSG_LRSUPPORT)) {
661 pic16_options.opt_flags |= OF_LR_SUPPORT;
666 if (ISOPT(OPTIMIZE_GOTO)) {
667 pic16_options.opt_flags |= OF_OPTIMIZE_GOTO;
671 if(ISOPT(OPTIMIZE_CMP)) {
672 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
676 if (ISOPT(OPTIMIZE_DF)) {
677 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
685 extern set *userIncDirsSet;
687 static void _pic16_initPaths(void)
689 char pic16incDir[512];
690 char pic16libDir[512];
691 set *pic16incDirsSet=NULL;
692 set *pic16libDirsSet=NULL;
695 setMainValue("mcu", pic16->name[2] );
696 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
698 setMainValue("mcu1", pic16->name[1] );
699 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
701 sprintf(pic16incDir, "%s%cpic16", INCLUDE_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
702 sprintf(pic16libDir, "%s%cpic16", LIB_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
705 if(!options.nostdinc) {
706 /* setup pic16 include directory */
707 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
708 includeDirsSet = pic16incDirsSet;
709 // mergeSets(&includeDirsSet, pic16incDirsSet);
711 /* pic16 port should not search to the SDCC standard include directories,
712 * so add here the deleted include dirs that user has issued in command line */
713 mergeSets(&pic16incDirsSet, userIncDirsSet);
715 if(!options.nostdlib) {
716 /* setup pic16 library directory */
717 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
718 libDirsSet = pic16libDirsSet;
719 // mergeSets(&libDirsSet, pic16libDirsSet);
722 if(!pic16_options.nodefaultlibs) {
723 /* now add the library for the device */
724 sprintf(devlib, "%s.lib", pic16->name[2]);
725 addSet(&libFilesSet, Safe_strdup(devlib));
727 /* add the internal SDCC library */
728 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
732 extern set *linkOptionsSet;
733 char *msprintf(hTab *pvals, const char *pformat, ...);
734 int my_system(const char *cmd);
736 /* forward declarations */
737 extern const char *pic16_linkCmd[];
738 extern const char *pic16_asmCmd[];
739 extern set *asmOptionsSet;
741 /* custom function to link objects */
742 static void _pic16_linkEdit(void)
744 hTab *linkValues=NULL;
752 * link command format:
753 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
756 sprintf(lfrm, "{linker} {incdirs} {lflags} -w -r -o {outfile} {user_ofile} {ofiles} {spec_ofiles} {libs}");
758 shash_add(&linkValues, "linker", pic16_linkCmd[0]);
760 mergeSets(&tSet, libDirsSet);
761 mergeSets(&tSet, libPathsSet);
763 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
764 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
766 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
768 if(fullSrcFileName) {
769 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName);
770 // addSetHead(&relFilesSet, Safe_strdup(temp));
771 shash_add(&linkValues, "user_ofile", temp);
774 if(!pic16_options.no_crt)
775 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
777 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
779 if(!libflags.ignore) {
780 if(libflags.want_libc)
781 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
783 if(libflags.want_libm)
784 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
786 if(libflags.want_libio) {
787 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
788 addSet(&libFilesSet, Safe_strdup(temp));
791 if(libflags.want_libdebug)
792 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
795 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
797 lcmd = msprintf(linkValues, lfrm);
799 ret = my_system( lcmd );
809 _pic16_finaliseOptions (void)
811 port->mem.default_local_map = data;
812 port->mem.default_globl_map = data;
814 /* peepholes are disabled for the time being */
817 /* explicit enable peepholes for testing */
818 if(pic16_enable_peeps)
821 options.all_callee_saves = 1; // always callee saves
824 options.float_rent = 1;
825 options.intlong_rent = 1;
829 if(alt_asm && strlen(alt_asm))
830 pic16_asmCmd[0] = alt_asm;
832 if(alt_link && strlen(alt_link))
833 pic16_linkCmd[0] = alt_link;
835 if(!pic16_options.no_crt) {
836 pic16_options.omit_ivt = 1;
837 pic16_options.leave_reset = 0;
840 if(options.model == MODEL_SMALL)
841 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
843 if(options.model == MODEL_LARGE)
844 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
849 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
850 *(strrchr(buf, 'f')) = 'F';
851 addSet(&asmOptionsSet, Safe_strdup( buf ));
854 if(STACK_MODEL_LARGE) {
855 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
856 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
858 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
859 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
865 if (options.model == MODEL_LARGE)
867 port->mem.default_local_map = xdata;
868 port->mem.default_globl_map = xdata;
872 port->mem.default_local_map = data;
873 port->mem.default_globl_map = data;
876 if (options.stack10bit)
878 if (options.model != MODEL_FLAT24)
881 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
882 fprintf (stderr, "\t10 bit stack mode disabled.\n");
883 options.stack10bit = 0;
887 /* Fixup the memory map for the stack; it is now in
888 * far space and requires a FPOINTER to access it.
891 istack->ptrType = FPOINTER;
898 _pic16_setDefaultOptions (void)
900 options.stackAuto = 0; /* implicit declaration */
901 /* port is not capable yet to allocate separate registers
902 * dedicated for passing certain parameters */
904 /* initialize to defaults section locations, names and addresses */
905 pic16_sectioninfo.at_udata = "udata";
907 /* set pic16 port options to defaults */
908 pic16_options.no_banksel = 0;
909 pic16_options.opt_banksel = 0;
910 pic16_options.omit_configw = 0;
911 pic16_options.omit_ivt = 0;
912 pic16_options.leave_reset = 0;
913 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
914 pic16_options.ivt_loc = 0x000000;
915 pic16_options.nodefaultlibs = 0;
916 pic16_options.dumpcalltree = 0;
917 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
918 pic16_options.no_crt = 0; /* use crt by default */
919 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
920 pic16_options.gstack = 0;
921 pic16_options.debgen = 0;
922 pic16_options.CATregs = 0;
926 _pic16_getRegName (struct regs *reg)
935 static char *_pic16_mangleFunctionName(char *sz)
937 // fprintf(stderr, "mangled function name: %s\n", sz);
945 _pic16_genAssemblerPreamble (FILE * of)
947 char *name = pic16_processor_base_name();
951 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
954 fprintf (of, "\tlist\tp=%s\n",&name[1]);
955 if (pic16_mplab_comp) {
956 // provide ACCESS macro used during SFR accesses
957 fprintf (of, "\tinclude <p%s.inc>\n", &name[1]);
960 if(!pic16_options.omit_configw) {
961 pic16_emitConfigRegs(of);
963 pic16_emitIDRegs(of);
966 fprintf (of, "\tradix dec\n");
969 /* Generate interrupt vector table. */
971 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
974 /* PIC18F family has only two interrupts, the high and the low
975 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
977 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
978 fprintf(of, "; RESET vector\n");
979 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
982 if(!pic16_options.omit_ivt) {
983 fprintf(of, "\tres 4\n");
986 fprintf(of, "; High priority interrupt vector 0x0008\n");
988 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
989 fprintf(of, "\tres\t12\n");
991 fprintf(of, "\tretfie\n");
992 fprintf(of, "\tres\t14\n");
995 fprintf(of, "; Low priority interrupt vector 0x0018\n");
997 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
999 fprintf(of, "\tretfie\n");
1006 /* return True if the port can handle the type,
1007 * False to convert it to function call */
1008 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
1010 //fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
1011 int symL, symR, symRes, sizeL = 0, sizeR = 0, sizeRes = 0;
1013 /* left/right are symbols? */
1014 symL = IS_SYMOP(IC_LEFT(ic));
1015 symR = IS_SYMOP(IC_RIGHT(ic));
1016 symRes = IS_SYMOP(IC_RESULT(ic));
1018 /* --> then determine their sizes */
1019 sizeL = symL ? getSize(OP_SYM_TYPE(IC_LEFT(ic))) : 4;
1020 sizeR = symR ? getSize(OP_SYM_TYPE(IC_RIGHT(ic))) : 4;
1021 sizeRes = symRes ? getSize(OP_SYM_TYPE(IC_RESULT(ic))) : 4;
1023 /* Checks to enable native multiplication.
1024 * PICs do not offer native division at all...
1027 * ( i) if result is just one byte, use native MUL
1028 * (regardless of the operands)
1029 * ( ii) if left and right are unsigned 8-bit operands,
1031 * (iii) if left or right is a literal in the range of [-128..256)
1032 * and the other is an unsigned byte, use native MUL
1036 /* use native mult for `*: <?> x <?> --> {u8_t, s8_t}' */
1037 if (sizeRes == 1) { return TRUE; }
1039 /* use native mult for `u8_t x u8_t --> { u16_t, s16_t }' */
1040 if (sizeL == 1 && symL /*&& SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic)))*/) {
1043 //printf( "%s: left too large (%u) / signed (%u)\n", __FUNCTION__, sizeL, symL && !SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic))));
1046 if (sizeR == 1 && symR /*&& SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic)))*/) {
1049 //printf( "%s: right too large (%u) / signed (%u)\n", __FUNCTION__, sizeR, symR && !SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic))));
1053 /* also allow literals [-128..256) for left/right operands */
1054 if (IS_VALOP(IC_LEFT(ic)))
1056 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1058 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1059 if (l >= -128 && l < 256)
1063 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1066 if (IS_VALOP( IC_RIGHT(ic) ))
1068 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1070 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1071 if (l >= -128 && l < 256)
1075 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1079 /* use native mult iff left and right are (unsigned) 8-bit operands */
1080 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1083 if (ic->op == '/' || ic->op == '%')
1085 /* We must catch /: {u8_t,s8_t} x {u8_t,s8_t} --> {u8_t,s8_t},
1086 * because SDCC will call 'divuchar' even for u8_t / s8_t.
1087 * Example: 128 / -2 becomes 128 / 254 = 0 != -64... */
1088 if (sizeL == 1 && sizeR == 1) return TRUE;
1090 /* What about literals? */
1091 if (IS_VALOP( IC_LEFT(ic) ))
1093 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1095 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1096 if (l >= -128 && l < 256)
1100 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1103 if (IS_VALOP( IC_RIGHT(ic) ))
1105 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1107 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1108 if (l >= -128 && l < 256)
1112 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1115 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1123 /* Do CSE estimation */
1124 static bool cseCostEstimation (iCode *ic, iCode *pdic)
1126 // operand *result = IC_RESULT(ic);
1127 // sym_link *result_type = operandType(result);
1130 /* VR -- this is an adhoc. Put here after conversation
1131 * with Erik Epetrich */
1135 || ic->op == EQ_OP) {
1137 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
1142 /* if it is a pointer then return ok for now */
1143 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
1145 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
1146 so we will cse only if they are local (i.e. both ic & pdic belong to
1147 the same basic block */
1148 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
1149 /* then if they are the same Basic block then ok */
1150 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
1155 /* for others it is cheaper to do the cse */
1161 /* Indicate which extended bit operations this port supports */
1163 hasExtBitOp (int op, int size)
1167 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
1174 /* Indicate the expense of an access to an output storage class */
1176 oclsExpense (struct memmap *oclass)
1178 /* The IN_FARSPACE test is compatible with historical behaviour, */
1179 /* but I don't think it is applicable to PIC. If so, please feel */
1180 /* free to remove this test -- EEP */
1181 if (IN_FARSPACE(oclass))
1187 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
1188 $2 is always the output file.
1189 $3 -L path and -l libraries
1190 $l is the list of extra options that should be there somewhere...
1191 MUST be terminated with a NULL.
1193 const char *pic16_linkCmd[] =
1195 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"","$3", NULL
1200 /** $1 is always the basename.
1201 $2 is always the output file.
1202 $3 varies (nothing currently)
1203 $l is the list of extra options that should be there somewhere...
1204 MUST be terminated with a NULL.
1206 const char *pic16_asmCmd[] =
1208 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1217 "MCU PIC16", /* Target name */
1218 "p18f452", /* Processor */
1221 TRUE, /* Emit glue around main */
1222 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1226 pic16_asmCmd, /* assembler command and arguments */
1227 NULL, /* alternate macro based form */
1228 "-g", /* arguments for debug mode */
1229 NULL, /* arguments for normal mode */
1230 0, /* print externs as global */
1231 ".asm", /* assembler file extension */
1232 NULL /* no do_assemble function */
1235 NULL, // pic16_linkCmd, /* linker command and arguments */
1236 NULL, /* alternate macro based form */
1237 _pic16_linkEdit, //NULL, /* no do_link function */
1238 ".o", /* extension for object files */
1239 0 /* no need for linker file */
1251 3, /* fptr, far pointers (see Microchip) */
1258 /* generic pointer tags */
1267 "XSEG (XDATA)", // xstack
1268 "STACK (DATA)", // istack
1269 "CSEG (CODE)", // code
1270 "DSEG (DATA)", // data
1271 "ISEG (DATA)", // idata
1272 "PSEG (DATA)", // pdata
1273 "XSEG (XDATA)", // xdata
1274 "BSEG (BIT)", // bit
1275 "RSEG (DATA)", // reg
1276 "GSINIT (CODE)", // static
1277 "OSEG (OVR,DATA)", // overlay
1278 "GSFINAL (CODE)", // post static
1279 "HOME (CODE)", // home
1282 "CONST (CODE)", // const_name - const data (code or not)
1283 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
1284 NULL, // default location for auto vars
1285 NULL, // default location for global vars
1286 1 // code is read only 1=yes
1289 NULL, /* genExtraAreaDeclaration */
1290 NULL /* genExatrAreaLinkOptions */
1293 /* stack related information */
1294 -1, /* -1 stack grows downwards, +1 upwards */
1295 1, /* extra overhead when calling between banks */
1296 4, /* extra overhead when the function is an ISR */
1297 1, /* extra overhead for a function call */
1298 1, /* re-entrant space */
1299 0 /* 'banked' call overhead, mild overlap with bank_overhead */
1301 /* pic16 has an 8 bit mul */
1306 pic16_emitDebuggerSymbol
1309 255/3, /* maxCount */
1310 3, /* sizeofElement */
1311 /* The rest of these costs are bogus. They approximate */
1312 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
1313 {4,4,4}, /* sizeofMatchJump[] */
1314 {0,0,0}, /* sizeofRangeCompare[] */
1315 0, /* sizeofSubtract */
1316 3, /* sizeofDispatch */
1320 _pic16_parseOptions,
1323 _pic16_finaliseOptions,
1324 _pic16_setDefaultOptions,
1325 pic16_assignRegisters,
1328 _pic16_genAssemblerPreamble,
1329 NULL, /* no genAssemblerEnd */
1331 NULL, // _pic16_genXINIT
1332 NULL, /* genInitStartup */
1333 _pic16_reset_regparm,
1335 _process_pragma, /* process a pragma */
1336 _pic16_mangleFunctionName, /* mangles function name */
1338 hasExtBitOp, /* hasExtBitOp */
1339 oclsExpense, /* oclsExpense */
1341 TRUE, /* little endian */
1344 1, /* transform <= to ! > */
1345 1, /* transform >= to ! < */
1346 1, /* transform != to !(a == b) */
1348 FALSE, /* No array initializer support. */
1349 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
1350 NULL, /* no builtin functions */
1351 GPOINTER, /* treat unqualified pointers as "generic" pointers */
1352 1, /* reset labelKey to 1 */
1353 1, /* globals & local static allowed */