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"))libflags.want_libc = 1;
421 else if(!strcmp(lmodule, "math"))
422 libflags.want_libm = 1;
423 else if(!strcmp(lmodule, "io"))
424 libflags.want_libio = 1;
425 else if(!strcmp(lmodule, "debug"))
426 libflags.want_libdebug = 1;
427 else if(!strcmp(lmodule, "ignore"))
433 fprintf(stderr, "link library %s\n", lmodule);
434 addSetHead(&libFilesSet, (char *)lmodule);
444 cp = get_pragma_token(cp, &token);
445 if (TOKEN_EOL != token.type)
454 /* This is an experimental code for #pragma inline
455 and is temporarily disabled for 2.5.0 release */
458 char *tmp = strtok((char *)NULL, WHITECOMMA);
461 addSet(&asmInlineMap, Safe_strdup( tmp ));
462 tmp = strtok((char *)NULL, WHITECOMMA);
468 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
469 debugf("inline asm: `%s'\n", s);
481 get_pragma_token(cp, &token);
484 werror(W_BAD_PRAGMA_ARGUMENTS, name);
486 free_pragma_token(&token);
490 static struct pragma_s pragma_tbl[] = {
491 { "maxram", P_MAXRAM, 0, do_pragma },
492 { "stack", P_STACK, 0, do_pragma },
493 { "code", P_CODE, 0, do_pragma },
494 { "udata", P_UDATA, 0, do_pragma },
495 { "library", P_LIBRARY, 0, do_pragma },
496 /*{ "inline", P_INLINE, 0, do_pragma }, */
497 { NULL, 0, 0, NULL },
501 _process_pragma(const char *s)
503 return process_pragma_tbl(pragma_tbl, s);
506 #define REP_UDATA "--preplace-udata-with="
508 #define STACK_MODEL "--pstack-model="
509 #define OPT_BANKSEL "--obanksel="
511 #define ALT_ASM "--asm="
512 #define ALT_LINK "--link="
514 #define IVT_LOC "--ivt-loc="
515 #define NO_DEFLIBS "--nodefaultlibs"
516 #define MPLAB_COMPAT "--mplab-comp"
518 #define NL_OPT "--nl="
519 #define USE_CRT "--use-crt="
521 #define OFMSG_LRSUPPORT "--flr-support"
523 #define OPTIMIZE_GOTO "--optimize-goto"
524 #define OPTIMIZE_CMP "--optimize-cmp"
525 #define OPTIMIZE_DF "--optimize-df"
530 int pic16_mplab_comp=0;
531 extern int pic16_debug_verbose;
532 extern int pic16_ralloc_debug;
533 extern int pic16_pcode_verbose;
536 int pic16_enable_peeps=0;
537 int pic16_nl=0; /* 0 for LF, 1 for CRLF */
539 OPTION pic16_optionsTable[]= {
540 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
541 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
542 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
543 // { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
544 // { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
545 // { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
546 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
548 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
549 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
550 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
552 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
554 { 0, ALT_ASM, NULL, "Use alternative assembler"},
555 { 0, ALT_LINK, NULL, "Use alternative linker"},
557 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
558 { 0, IVT_LOC, NULL, "<nnnn> interrupt vector table location"},
559 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
560 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
561 { 0, "--fstack", &pic16_fstack, "enable stack optimizations"},
562 { 0, NL_OPT, NULL, "new line, \"lf\" or \"crlf\""},
563 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
564 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
565 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
566 { 0, OPTIMIZE_GOTO, NULL, "try to use (conditional) BRA instead of GOTO"},
567 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
568 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
569 { 0, "--num-func-alloc-regs", &pic16_options.CATregs, "dump number of temporary registers allocated for each function"},
571 { 'y', "--extended", &xinst, "enable Extended Instruction Set/Literal Offset Addressing mode"},
573 { 0, NULL, NULL, NULL}
577 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
579 extern char *getStringArg(const char *, char **, int *, int);
580 extern int getIntArg(const char *, char **, int *, int);
583 _pic16_parseOptions (int *pargc, char **argv, int *i)
588 /* TODO: allow port-specific command line options to specify
589 * segment names here.
592 /* check for arguments that have associated an integer variable */
593 while(pic16_optionsTable[j].pparameter) {
594 if(ISOPT( pic16_optionsTable[j].longOpt )) {
595 (*pic16_optionsTable[j].pparameter)++;
601 if(ISOPT(STACK_MODEL)) {
602 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
603 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
604 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
606 fprintf(stderr, "Unknown stack model: %s", stkmodel);
612 if(ISOPT(OPT_BANKSEL)) {
613 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
617 if(ISOPT(REP_UDATA)) {
618 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
623 alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
627 if(ISOPT(ALT_LINK)) {
628 alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
633 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
634 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
641 tmp = Safe_strdup( getStringArg(NL_OPT, argv, i, *pargc) );
642 if(!STRCASECMP(tmp, "lf"))pic16_nl = 0;
643 else if(!STRCASECMP(tmp, "crlf"))pic16_nl = 1;
645 fprintf(stderr, "invalid termination character id\n");
652 pic16_options.no_crt = 0;
653 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
659 if(ISOPT(OFMSG_LRSUPPORT)) {
660 pic16_options.opt_flags |= OF_LR_SUPPORT;
665 if (ISOPT(OPTIMIZE_GOTO)) {
666 pic16_options.opt_flags |= OF_OPTIMIZE_GOTO;
670 if(ISOPT(OPTIMIZE_CMP)) {
671 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
675 if (ISOPT(OPTIMIZE_DF)) {
676 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
684 extern set *userIncDirsSet;
686 static void _pic16_initPaths(void)
688 char pic16incDir[512];
689 char pic16libDir[512];
690 set *pic16incDirsSet=NULL;
691 set *pic16libDirsSet=NULL;
694 setMainValue("mcu", pic16->name[2] );
695 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
697 setMainValue("mcu1", pic16->name[1] );
698 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
700 sprintf(pic16incDir, "%s%cpic16", INCLUDE_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
701 sprintf(pic16libDir, "%s%cpic16", LIB_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
704 if(!options.nostdinc) {
705 /* setup pic16 include directory */
706 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
707 includeDirsSet = pic16incDirsSet;
708 // mergeSets(&includeDirsSet, pic16incDirsSet);
710 /* pic16 port should not search to the SDCC standard include directories,
711 * so add here the deleted include dirs that user has issued in command line */
712 mergeSets(&pic16incDirsSet, userIncDirsSet);
714 if(!options.nostdlib) {
715 /* setup pic16 library directory */
716 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
717 libDirsSet = pic16libDirsSet;
718 // mergeSets(&libDirsSet, pic16libDirsSet);
721 if(!pic16_options.nodefaultlibs) {
722 /* now add the library for the device */
723 sprintf(devlib, "%s.lib", pic16->name[2]);
724 addSet(&libFilesSet, Safe_strdup(devlib));
726 /* add the internal SDCC library */
727 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
731 extern set *linkOptionsSet;
732 char *msprintf(hTab *pvals, const char *pformat, ...);
733 int my_system(const char *cmd);
735 /* forward declarations */
736 extern const char *pic16_linkCmd[];
737 extern const char *pic16_asmCmd[];
738 extern set *asmOptionsSet;
740 /* custom function to link objects */
741 static void _pic16_linkEdit(void)
743 hTab *linkValues=NULL;
751 * link command format:
752 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
755 sprintf(lfrm, "{linker} {incdirs} {lflags} -w -r -o {outfile} {user_ofile} {ofiles} {spec_ofiles} {libs}");
757 shash_add(&linkValues, "linker", pic16_linkCmd[0]);
759 mergeSets(&tSet, libDirsSet);
760 mergeSets(&tSet, libPathsSet);
762 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
763 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
765 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
767 if(fullSrcFileName) {
768 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName);
769 // addSetHead(&relFilesSet, Safe_strdup(temp));
770 shash_add(&linkValues, "user_ofile", temp);
773 if(!pic16_options.no_crt)
774 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
776 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
778 if(!libflags.ignore) {
779 if(libflags.want_libc)
780 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
782 if(libflags.want_libm)
783 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
785 if(libflags.want_libio) {
786 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
787 addSet(&libFilesSet, Safe_strdup(temp));
790 if(libflags.want_libdebug)
791 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
794 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
796 lcmd = msprintf(linkValues, lfrm);
798 ret = my_system( lcmd );
808 _pic16_finaliseOptions (void)
810 port->mem.default_local_map = data;
811 port->mem.default_globl_map = data;
813 /* peepholes are disabled for the time being */
816 /* explicit enable peepholes for testing */
817 if(pic16_enable_peeps)
820 options.all_callee_saves = 1; // always callee saves
823 options.float_rent = 1;
824 options.intlong_rent = 1;
828 if(alt_asm && strlen(alt_asm))
829 pic16_asmCmd[0] = alt_asm;
831 if(alt_link && strlen(alt_link))
832 pic16_linkCmd[0] = alt_link;
834 if(!pic16_options.no_crt) {
835 pic16_options.omit_ivt = 1;
836 pic16_options.leave_reset = 0;
839 if(options.model == MODEL_SMALL)
840 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
842 if(options.model == MODEL_LARGE)
843 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
848 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
849 *(strrchr(buf, 'f')) = 'F';
850 addSet(&asmOptionsSet, Safe_strdup( buf ));
853 if(STACK_MODEL_LARGE) {
854 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
855 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
857 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
858 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
864 if (options.model == MODEL_LARGE)
866 port->mem.default_local_map = xdata;
867 port->mem.default_globl_map = xdata;
871 port->mem.default_local_map = data;
872 port->mem.default_globl_map = data;
875 if (options.stack10bit)
877 if (options.model != MODEL_FLAT24)
880 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
881 fprintf (stderr, "\t10 bit stack mode disabled.\n");
882 options.stack10bit = 0;
886 /* Fixup the memory map for the stack; it is now in
887 * far space and requires a FPOINTER to access it.
890 istack->ptrType = FPOINTER;
897 _pic16_setDefaultOptions (void)
899 options.stackAuto = 0; /* implicit declaration */
900 /* port is not capable yet to allocate separate registers
901 * dedicated for passing certain parameters */
903 /* initialize to defaults section locations, names and addresses */
904 pic16_sectioninfo.at_udata = "udata";
906 /* set pic16 port options to defaults */
907 pic16_options.no_banksel = 0;
908 pic16_options.opt_banksel = 0;
909 pic16_options.omit_configw = 0;
910 pic16_options.omit_ivt = 0;
911 pic16_options.leave_reset = 0;
912 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
913 pic16_options.ivt_loc = 0x000000;
914 pic16_options.nodefaultlibs = 0;
915 pic16_options.dumpcalltree = 0;
916 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
917 pic16_options.no_crt = 0; /* use crt by default */
918 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
919 pic16_options.gstack = 0;
920 pic16_options.debgen = 0;
921 pic16_options.CATregs = 0;
925 _pic16_getRegName (struct regs *reg)
934 static char *_pic16_mangleFunctionName(char *sz)
936 // fprintf(stderr, "mangled function name: %s\n", sz);
944 _pic16_genAssemblerPreamble (FILE * of)
946 char *name = pic16_processor_base_name();
950 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
953 fprintf (of, "\tlist\tp=%s\n",&name[1]);
954 if (pic16_mplab_comp) {
955 // provide ACCESS macro used during SFR accesses
956 fprintf (of, "\tinclude <p%s.inc>\n", &name[1]);
959 if(!pic16_options.omit_configw) {
960 pic16_emitConfigRegs(of);
962 pic16_emitIDRegs(of);
965 fprintf (of, "\tradix dec\n");
968 /* Generate interrupt vector table. */
970 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
973 /* PIC18F family has only two interrupts, the high and the low
974 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
976 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
977 fprintf(of, "; RESET vector\n");
978 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
981 if(!pic16_options.omit_ivt) {
982 fprintf(of, "\tres 4\n");
985 fprintf(of, "; High priority interrupt vector 0x0008\n");
987 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
988 fprintf(of, "\tres\t12\n");
990 fprintf(of, "\tretfie\n");
991 fprintf(of, "\tres\t14\n");
994 fprintf(of, "; Low priority interrupt vector 0x0018\n");
996 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
998 fprintf(of, "\tretfie\n");
1005 /* return True if the port can handle the type,
1006 * False to convert it to function call */
1007 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
1009 //fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
1010 int symL, symR, symRes, sizeL = 0, sizeR = 0, sizeRes = 0;
1012 /* left/right are symbols? */
1013 symL = IS_SYMOP(IC_LEFT(ic));
1014 symR = IS_SYMOP(IC_RIGHT(ic));
1015 symRes = IS_SYMOP(IC_RESULT(ic));
1017 /* --> then determine their sizes */
1018 sizeL = symL ? getSize(OP_SYM_TYPE(IC_LEFT(ic))) : 4;
1019 sizeR = symR ? getSize(OP_SYM_TYPE(IC_RIGHT(ic))) : 4;
1020 sizeRes = symRes ? getSize(OP_SYM_TYPE(IC_RESULT(ic))) : 4;
1022 /* Checks to enable native multiplication.
1023 * PICs do not offer native division at all...
1026 * ( i) if result is just one byte, use native MUL
1027 * (regardless of the operands)
1028 * ( ii) if left and right are unsigned 8-bit operands,
1030 * (iii) if left or right is a literal in the range of [-128..256)
1031 * and the other is an unsigned byte, use native MUL
1035 /* use native mult for `*: <?> x <?> --> {u8_t, s8_t}' */
1036 if (sizeRes == 1) { return TRUE; }
1038 /* use native mult for `u8_t x u8_t --> { u16_t, s16_t }' */
1039 if (sizeL == 1 && symL /*&& SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic)))*/) {
1042 //printf( "%s: left too large (%u) / signed (%u)\n", __FUNCTION__, sizeL, symL && !SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic))));
1045 if (sizeR == 1 && symR /*&& SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic)))*/) {
1048 //printf( "%s: right too large (%u) / signed (%u)\n", __FUNCTION__, sizeR, symR && !SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic))));
1052 /* also allow literals [-128..256) for left/right operands */
1053 if (IS_VALOP(IC_LEFT(ic)))
1055 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1057 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1058 if (l >= -128 && l < 256)
1062 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1065 if (IS_VALOP( IC_RIGHT(ic) ))
1067 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1069 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1070 if (l >= -128 && l < 256)
1074 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1078 /* use native mult iff left and right are (unsigned) 8-bit operands */
1079 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1082 if (ic->op == '/' || ic->op == '%')
1084 /* We must catch /: {u8_t,s8_t} x {u8_t,s8_t} --> {u8_t,s8_t},
1085 * because SDCC will call 'divuchar' even for u8_t / s8_t.
1086 * Example: 128 / -2 becomes 128 / 254 = 0 != -64... */
1087 if (sizeL == 1 && sizeR == 1) return TRUE;
1089 /* What about literals? */
1090 if (IS_VALOP( IC_LEFT(ic) ))
1092 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1094 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1095 if (l >= -128 && l < 256)
1099 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1102 if (IS_VALOP( IC_RIGHT(ic) ))
1104 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1106 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1107 if (l >= -128 && l < 256)
1111 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1114 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1122 /* Do CSE estimation */
1123 static bool cseCostEstimation (iCode *ic, iCode *pdic)
1125 // operand *result = IC_RESULT(ic);
1126 // sym_link *result_type = operandType(result);
1129 /* VR -- this is an adhoc. Put here after conversation
1130 * with Erik Epetrich */
1134 || ic->op == EQ_OP) {
1136 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
1141 /* if it is a pointer then return ok for now */
1142 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
1144 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
1145 so we will cse only if they are local (i.e. both ic & pdic belong to
1146 the same basic block */
1147 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
1148 /* then if they are the same Basic block then ok */
1149 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
1154 /* for others it is cheaper to do the cse */
1160 /* Indicate which extended bit operations this port supports */
1162 hasExtBitOp (int op, int size)
1166 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
1173 /* Indicate the expense of an access to an output storage class */
1175 oclsExpense (struct memmap *oclass)
1177 /* The IN_FARSPACE test is compatible with historical behaviour, */
1178 /* but I don't think it is applicable to PIC. If so, please feel */
1179 /* free to remove this test -- EEP */
1180 if (IN_FARSPACE(oclass))
1186 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
1187 $2 is always the output file.
1188 $3 -L path and -l libraries
1189 $l is the list of extra options that should be there somewhere...
1190 MUST be terminated with a NULL.
1192 const char *pic16_linkCmd[] =
1194 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"","$3", NULL
1199 /** $1 is always the basename.
1200 $2 is always the output file.
1201 $3 varies (nothing currently)
1202 $l is the list of extra options that should be there somewhere...
1203 MUST be terminated with a NULL.
1205 const char *pic16_asmCmd[] =
1207 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1216 "MCU PIC16", /* Target name */
1217 "p18f452", /* Processor */
1220 TRUE, /* Emit glue around main */
1221 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1225 pic16_asmCmd, /* assembler command and arguments */
1226 NULL, /* alternate macro based form */
1227 "-g", /* arguments for debug mode */
1228 NULL, /* arguments for normal mode */
1229 0, /* print externs as global */
1230 ".asm", /* assembler file extension */
1231 NULL /* no do_assemble function */
1234 NULL, // pic16_linkCmd, /* linker command and arguments */
1235 NULL, /* alternate macro based form */
1236 _pic16_linkEdit, //NULL, /* no do_link function */
1237 ".o", /* extension for object files */
1238 0 /* no need for linker file */
1250 3, /* fptr, far pointers (see Microchip) */
1257 /* generic pointer tags */
1266 "XSEG (XDATA)", // xstack
1267 "STACK (DATA)", // istack
1268 "CSEG (CODE)", // code
1269 "DSEG (DATA)", // data
1270 "ISEG (DATA)", // idata
1271 "PSEG (DATA)", // pdata
1272 "XSEG (XDATA)", // xdata
1273 "BSEG (BIT)", // bit
1274 "RSEG (DATA)", // reg
1275 "GSINIT (CODE)", // static
1276 "OSEG (OVR,DATA)", // overlay
1277 "GSFINAL (CODE)", // post static
1278 "HOME (CODE)", // home
1281 "CONST (CODE)", // const_name - const data (code or not)
1282 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
1283 NULL, // default location for auto vars
1284 NULL, // default location for global vars
1285 1 // code is read only 1=yes
1288 NULL, /* genExtraAreaDeclaration */
1289 NULL /* genExatrAreaLinkOptions */
1292 /* stack related information */
1293 -1, /* -1 stack grows downwards, +1 upwards */
1294 1, /* extra overhead when calling between banks */
1295 4, /* extra overhead when the function is an ISR */
1296 1, /* extra overhead for a function call */
1297 1, /* re-entrant space */
1298 0 /* 'banked' call overhead, mild overlap with bank_overhead */
1300 /* pic16 has an 8 bit mul */
1305 pic16_emitDebuggerSymbol
1308 255/3, /* maxCount */
1309 3, /* sizeofElement */
1310 /* The rest of these costs are bogus. They approximate */
1311 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
1312 {4,4,4}, /* sizeofMatchJump[] */
1313 {0,0,0}, /* sizeofRangeCompare[] */
1314 0, /* sizeofSubtract */
1315 3, /* sizeofDispatch */
1319 _pic16_parseOptions,
1322 _pic16_finaliseOptions,
1323 _pic16_setDefaultOptions,
1324 pic16_assignRegisters,
1327 _pic16_genAssemblerPreamble,
1328 NULL, /* no genAssemblerEnd */
1330 NULL, // _pic16_genXINIT
1331 NULL, /* genInitStartup */
1332 _pic16_reset_regparm,
1334 _process_pragma, /* process a pragma */
1335 _pic16_mangleFunctionName, /* mangles function name */
1337 hasExtBitOp, /* hasExtBitOp */
1338 oclsExpense, /* oclsExpense */
1340 TRUE, /* little endian */
1343 1, /* transform <= to ! > */
1344 1, /* transform >= to ! < */
1345 1, /* transform != to !(a == b) */
1347 FALSE, /* No array initializer support. */
1348 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
1349 NULL, /* no builtin functions */
1350 GPOINTER, /* treat unqualified pointers as "generic" pointers */
1351 1, /* reset labelKey to 1 */
1352 1, /* globals & local static allowed */