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 -------------------------------------------------------------------------*/
34 #include "dbuf_string.h"
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 };
173 do_pragma(int id, const char *name, const char *cp)
175 struct pragma_token_s token;
179 init_pragma_token(&token);
183 /* #pragma maxram [maxram] */
188 cp = get_pragma_token(cp, &token);
189 if (TOKEN_INT == token.type)
190 max_ram = token.val.int_val;
197 cp = get_pragma_token(cp, &token);
198 if (TOKEN_EOL != token.type)
204 pic16_setMaxRAM(max_ram);
208 /* #pragma stack [stack-position] [stack-len] */
211 unsigned int stackPos, stackLen;
215 cp = get_pragma_token(cp, &token);
216 if (TOKEN_INT != token.type)
221 stackPos = token.val.int_val;
223 cp = get_pragma_token(cp, &token);
224 if (TOKEN_INT != token.type)
229 stackLen = token.val.int_val;
231 cp = get_pragma_token(cp, &token);
232 if (TOKEN_EOL != token.type)
240 fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
241 filename, lineno, stackLen, stackLen);
244 /* check sanity of stack */
245 if ((stackPos >> 8) != ((stackPos + stackLen - 1) >> 8)) {
246 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
247 filename, lineno, stackPos, stackPos + stackLen - 1);
251 if (stackPos < pic16->acsSplitOfs) {
252 fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
253 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->acsSplitOfs);
256 if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
257 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
258 filename, lineno, stackPos, stackPos + stackLen - 1, 0xF00 + pic16->acsSplitOfs);
261 if (stackPos+stackLen > pic16->RAMsize) {
262 fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
263 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->RAMsize-1);
269 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
270 addSet(&pic16_fix_udata, reg);
272 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
273 addSet(&pic16_fix_udata, reg);
275 sym = newSymbol("stack", 0);
276 sprintf(sym->rname, "_%s", sym->name);
277 addSet(&publics, sym);
279 sym = newSymbol("stack_end", 0);
280 sprintf(sym->rname, "_%s", sym->name);
281 addSet(&publics, sym);
283 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
287 /* #pragma code [symbol] [location] */
292 cp = get_pragma_token(cp, &token);
293 if (TOKEN_STR != token.type)
296 absS = Safe_calloc(1, sizeof(absSym));
297 sprintf(absS->name, "_%s", get_pragma_string(&token));
299 cp = get_pragma_token(cp, &token);
300 if (TOKEN_INT != token.type)
303 //fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno);
307 absS->address = token.val.int_val;
309 cp = get_pragma_token(cp, &token);
310 if (TOKEN_EOL != token.type)
316 if ((absS->address % 2) != 0) {
318 fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
319 filename, lineno, absS->address);
322 addSet(&absSymSet, absS);
323 // fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
324 // __FILE__, __LINE__, symname, absS->address);
328 /* #pragma udata [section-name] [symbol] */
338 cp = get_pragma_token(cp, &token);
339 if (TOKEN_STR == token.type)
340 sectname = Safe_strdup(get_pragma_string(&token));
347 cp = get_pragma_token(cp, &token);
348 if (TOKEN_STR == token.type)
349 symname = get_pragma_string(&token);
352 //fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno);
359 ssym = Safe_calloc(1, sizeof(sectSym));
360 ssym->name = Safe_calloc(1, strlen(symname) + 2);
361 sprintf(ssym->name, "%s%s", port->fun_prefix, symname);
364 addSet(§Syms, ssym);
366 nsym = newSymbol((char *)symname, 0);
367 strcpy(nsym->rname, ssym->name);
370 checkAddSym(&publics, nsym);
374 for (snam = setFirstItem(sectNames);snam;snam=setNextItem(sectNames))
376 if (!strcmp(sectname, snam->name))
385 snam = Safe_calloc(1, sizeof(sectName));
386 snam->name = Safe_strdup(sectname);
387 snam->regsSet = NULL;
389 addSet(§Names, snam);
392 ssym->section = snam;
395 fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
396 ssym->name, snam->name, snam);
399 cp = get_pragma_token(cp, &token);
400 if (TOKEN_STR == token.type)
401 symname = get_pragma_string(&token);
402 else if (TOKEN_EOL == token.type)
415 /* #pragma library library_module */
420 cp = get_pragma_token(cp, &token);
421 if (TOKEN_EOL != token.type)
423 lmodule = get_pragma_string(&token);
426 * c link the C library
427 * math link the math library
428 * io link the IO library
429 * debug link the debug libary
430 * anything else, will link as-is */
432 if(!strcmp(lmodule, "c"))
433 libflags.want_libc = 1;
434 else if(!strcmp(lmodule, "math"))
435 libflags.want_libm = 1;
436 else if(!strcmp(lmodule, "io"))
437 libflags.want_libio = 1;
438 else if(!strcmp(lmodule, "debug"))
439 libflags.want_libdebug = 1;
440 else if(!strcmp(lmodule, "ignore"))
446 fprintf(stderr, "link library %s\n", lmodule);
447 addSetHead(&libFilesSet, (char *)lmodule);
457 cp = get_pragma_token(cp, &token);
458 if (TOKEN_EOL != token.type)
467 /* This is an experimental code for #pragma inline
468 and is temporarily disabled for 2.5.0 release */
471 char *tmp = strtok((char *)NULL, WHITECOMMA);
474 addSet(&asmInlineMap, Safe_strdup( tmp ));
475 tmp = strtok((char *)NULL, WHITECOMMA);
481 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
482 debugf("inline asm: `%s'\n", s);
494 get_pragma_token(cp, &token);
497 werror(W_BAD_PRAGMA_ARGUMENTS, name);
499 free_pragma_token(&token);
503 static struct pragma_s pragma_tbl[] = {
504 { "maxram", P_MAXRAM, 0, do_pragma },
505 { "stack", P_STACK, 0, do_pragma },
506 { "code", P_CODE, 0, do_pragma },
507 { "udata", P_UDATA, 0, do_pragma },
508 { "library", P_LIBRARY, 0, do_pragma },
509 /*{ "inline", P_INLINE, 0, do_pragma }, */
510 { NULL, 0, 0, NULL },
514 _process_pragma(const char *s)
516 return process_pragma_tbl(pragma_tbl, s);
519 #define REP_UDATA "--preplace-udata-with="
521 #define STACK_MODEL "--pstack-model="
522 #define OPT_BANKSEL "--obanksel="
524 #define ALT_ASM "--asm="
525 #define ALT_LINK "--link="
527 #define IVT_LOC "--ivt-loc="
528 #define NO_DEFLIBS "--nodefaultlibs"
529 #define MPLAB_COMPAT "--mplab-comp"
531 #define NL_OPT "--nl="
532 #define USE_CRT "--use-crt="
534 #define OFMSG_LRSUPPORT "--flr-support"
536 #define OPTIMIZE_GOTO "--optimize-goto"
537 #define OPTIMIZE_CMP "--optimize-cmp"
538 #define OPTIMIZE_DF "--optimize-df"
543 int pic16_mplab_comp=0;
544 extern int pic16_debug_verbose;
545 extern int pic16_ralloc_debug;
546 extern int pic16_pcode_verbose;
549 int pic16_enable_peeps=0;
550 int pic16_nl=0; /* 0 for LF, 1 for CRLF */
552 OPTION pic16_optionsTable[]= {
553 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
554 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
555 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
556 // { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
557 // { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
558 // { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
559 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
561 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
562 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
563 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
565 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
567 { 0, ALT_ASM, NULL, "Use alternative assembler"},
568 { 0, ALT_LINK, NULL, "Use alternative linker"},
570 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
571 { 0, IVT_LOC, NULL, "<nnnn> interrupt vector table location"},
572 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
573 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
574 { 0, "--fstack", &pic16_fstack, "enable stack optimizations"},
575 { 0, NL_OPT, NULL, "new line, \"lf\" or \"crlf\""},
576 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
577 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
578 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
579 { 0, OPTIMIZE_GOTO, NULL, "try to use (conditional) BRA instead of GOTO"},
580 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
581 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
582 { 0, "--num-func-alloc-regs", &pic16_options.CATregs, "dump number of temporary registers allocated for each function"},
584 { 'y', "--extended", &xinst, "enable Extended Instruction Set/Literal Offset Addressing mode"},
586 { 0, NULL, NULL, NULL}
590 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
592 extern char *getStringArg(const char *, char **, int *, int);
593 extern int getIntArg(const char *, char **, int *, int);
596 _pic16_parseOptions (int *pargc, char **argv, int *i)
601 /* TODO: allow port-specific command line options to specify
602 * segment names here.
605 /* check for arguments that have associated an integer variable */
606 while(pic16_optionsTable[j].pparameter) {
607 if(ISOPT( pic16_optionsTable[j].longOpt )) {
608 (*pic16_optionsTable[j].pparameter)++;
614 if(ISOPT(STACK_MODEL)) {
615 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
616 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
617 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
619 fprintf(stderr, "Unknown stack model: %s", stkmodel);
625 if(ISOPT(OPT_BANKSEL)) {
626 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
630 if(ISOPT(REP_UDATA)) {
631 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
636 alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
640 if(ISOPT(ALT_LINK)) {
641 alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
646 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
647 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
654 tmp = Safe_strdup( getStringArg(NL_OPT, argv, i, *pargc) );
655 if(!STRCASECMP(tmp, "lf"))pic16_nl = 0;
656 else if(!STRCASECMP(tmp, "crlf"))pic16_nl = 1;
658 fprintf(stderr, "invalid termination character id\n");
665 pic16_options.no_crt = 0;
666 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
672 if(ISOPT(OFMSG_LRSUPPORT)) {
673 pic16_options.opt_flags |= OF_LR_SUPPORT;
678 if (ISOPT(OPTIMIZE_GOTO)) {
679 pic16_options.opt_flags |= OF_OPTIMIZE_GOTO;
683 if(ISOPT(OPTIMIZE_CMP)) {
684 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
688 if (ISOPT(OPTIMIZE_DF)) {
689 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
697 extern set *userIncDirsSet;
699 static void _pic16_initPaths(void)
701 set *pic16incDirsSet=NULL;
702 set *pic16libDirsSet=NULL;
705 setMainValue("mcu", pic16->name[2] );
706 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
708 setMainValue("mcu1", pic16->name[1] );
709 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
711 if(!options.nostdinc) {
712 struct dbuf_s pic16incDir;
714 dbuf_init(&pic16incDir, 128);
715 dbuf_makePath(&pic16incDir, INCLUDE_DIR_SUFFIX, "pic16");
717 /* setup pic16 include directory */
718 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, dbuf_c_str(&pic16incDir));
719 dbuf_destroy(&pic16incDir);
720 includeDirsSet = pic16incDirsSet;
721 // mergeSets(&includeDirsSet, pic16incDirsSet);
723 /* pic16 port should not search to the SDCC standard include directories,
724 * so add here the deleted include dirs that user has issued in command line */
725 mergeSets(&pic16incDirsSet, userIncDirsSet);
727 if(!options.nostdlib) {
728 struct dbuf_s pic16libDir;
730 dbuf_init(&pic16libDir, 128);
731 dbuf_makePath(&pic16libDir, LIB_DIR_SUFFIX, "pic16");
732 /* setup pic16 library directory */
733 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, dbuf_c_str(&pic16libDir));
734 dbuf_destroy(&pic16libDir);
735 libDirsSet = pic16libDirsSet;
736 // mergeSets(&libDirsSet, pic16libDirsSet);
739 if(!pic16_options.nodefaultlibs) {
740 /* now add the library for the device */
741 sprintf(devlib, "%s.lib", pic16->name[2]);
742 addSet(&libFilesSet, Safe_strdup(devlib));
744 /* add the internal SDCC library */
745 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
749 extern set *linkOptionsSet;
750 char *msprintf(hTab *pvals, const char *pformat, ...);
751 int my_system(const char *cmd);
753 /* forward declarations */
754 extern const char *pic16_linkCmd[];
755 extern const char *pic16_asmCmd[];
756 extern set *asmOptionsSet;
758 /* custom function to link objects */
759 static void _pic16_linkEdit(void)
761 hTab *linkValues=NULL;
769 * link command format:
770 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
773 sprintf(lfrm, "{linker} {incdirs} {lflags} -w -r -o {outfile} {user_ofile} {ofiles} {spec_ofiles} {libs}");
775 shash_add(&linkValues, "linker", pic16_linkCmd[0]);
777 mergeSets(&tSet, libDirsSet);
778 mergeSets(&tSet, libPathsSet);
780 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
781 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
783 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
785 if(fullSrcFileName) {
786 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName);
787 // addSetHead(&relFilesSet, Safe_strdup(temp));
788 shash_add(&linkValues, "user_ofile", temp);
791 if(!pic16_options.no_crt)
792 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
794 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
796 if(!libflags.ignore) {
797 if(libflags.want_libc)
798 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
800 if(libflags.want_libm)
801 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
803 if(libflags.want_libio) {
804 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
805 addSet(&libFilesSet, Safe_strdup(temp));
808 if(libflags.want_libdebug)
809 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
812 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
814 lcmd = msprintf(linkValues, lfrm);
816 ret = my_system( lcmd );
826 _pic16_finaliseOptions (void)
828 port->mem.default_local_map = data;
829 port->mem.default_globl_map = data;
831 /* peepholes are disabled for the time being */
834 /* explicit enable peepholes for testing */
835 if(pic16_enable_peeps)
838 options.all_callee_saves = 1; // always callee saves
841 options.float_rent = 1;
842 options.intlong_rent = 1;
846 if(alt_asm && strlen(alt_asm))
847 pic16_asmCmd[0] = alt_asm;
849 if(alt_link && strlen(alt_link))
850 pic16_linkCmd[0] = alt_link;
852 if(!pic16_options.no_crt) {
853 pic16_options.omit_ivt = 1;
854 pic16_options.leave_reset = 0;
857 if(options.model == MODEL_SMALL)
858 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
860 if(options.model == MODEL_LARGE)
861 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
866 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
867 *(strrchr(buf, 'f')) = 'F';
868 addSet(&asmOptionsSet, Safe_strdup( buf ));
871 if(STACK_MODEL_LARGE) {
872 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
873 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
875 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
876 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
882 if (options.model == MODEL_LARGE)
884 port->mem.default_local_map = xdata;
885 port->mem.default_globl_map = xdata;
889 port->mem.default_local_map = data;
890 port->mem.default_globl_map = data;
893 if (options.stack10bit)
895 if (options.model != MODEL_FLAT24)
898 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
899 fprintf (stderr, "\t10 bit stack mode disabled.\n");
900 options.stack10bit = 0;
904 /* Fixup the memory map for the stack; it is now in
905 * far space and requires a FPOINTER to access it.
908 istack->ptrType = FPOINTER;
915 _pic16_setDefaultOptions (void)
917 options.stackAuto = 0; /* implicit declaration */
918 /* port is not capable yet to allocate separate registers
919 * dedicated for passing certain parameters */
921 /* initialize to defaults section locations, names and addresses */
922 pic16_sectioninfo.at_udata = "udata";
924 /* set pic16 port options to defaults */
925 pic16_options.no_banksel = 0;
926 pic16_options.opt_banksel = 0;
927 pic16_options.omit_configw = 0;
928 pic16_options.omit_ivt = 0;
929 pic16_options.leave_reset = 0;
930 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
931 pic16_options.ivt_loc = 0x000000;
932 pic16_options.nodefaultlibs = 0;
933 pic16_options.dumpcalltree = 0;
934 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
935 pic16_options.no_crt = 0; /* use crt by default */
936 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
937 pic16_options.gstack = 0;
938 pic16_options.debgen = 0;
939 pic16_options.CATregs = 0;
943 _pic16_getRegName (struct regs *reg)
952 static char *_pic16_mangleFunctionName(char *sz)
954 // fprintf(stderr, "mangled function name: %s\n", sz);
962 _pic16_genAssemblerPreamble (FILE * of)
964 char *name = pic16_processor_base_name();
968 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
971 fprintf (of, "\tlist\tp=%s\n",&name[1]);
972 if (pic16_mplab_comp) {
973 // provide ACCESS macro used during SFR accesses
974 fprintf (of, "\tinclude <p%s.inc>\n", &name[1]);
977 if(!pic16_options.omit_configw) {
978 pic16_emitConfigRegs(of);
980 pic16_emitIDRegs(of);
983 fprintf (of, "\tradix dec\n");
986 /* Generate interrupt vector table. */
988 _pic16_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts)
991 /* PIC18F family has only two interrupts, the high and the low
992 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
994 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
995 dbuf_printf(oBuf, "; RESET vector\n");
996 dbuf_printf(oBuf, "\tgoto\t__sdcc_gsinit_startup\n");
999 if(!pic16_options.omit_ivt) {
1000 dbuf_printf(oBuf, "\tres 4\n");
1003 dbuf_printf(oBuf, "; High priority interrupt vector 0x0008\n");
1005 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[1]->rname);
1006 dbuf_printf(oBuf, "\tres\t12\n");
1008 dbuf_printf(oBuf, "\tretfie\n");
1009 dbuf_printf(oBuf, "\tres\t14\n");
1012 dbuf_printf(oBuf, "; Low priority interrupt vector 0x0018\n");
1014 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[2]->rname);
1016 dbuf_printf(oBuf, "\tretfie\n");
1023 /* return True if the port can handle the type,
1024 * False to convert it to function call */
1025 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
1027 //fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
1028 int symL, symR, symRes, sizeL = 0, sizeR = 0, sizeRes = 0;
1030 /* left/right are symbols? */
1031 symL = IS_SYMOP(IC_LEFT(ic));
1032 symR = IS_SYMOP(IC_RIGHT(ic));
1033 symRes = IS_SYMOP(IC_RESULT(ic));
1035 /* --> then determine their sizes */
1036 sizeL = symL ? getSize(OP_SYM_TYPE(IC_LEFT(ic))) : 4;
1037 sizeR = symR ? getSize(OP_SYM_TYPE(IC_RIGHT(ic))) : 4;
1038 sizeRes = symRes ? getSize(OP_SYM_TYPE(IC_RESULT(ic))) : 4;
1040 /* Checks to enable native multiplication.
1041 * PICs do not offer native division at all...
1044 * ( i) if result is just one byte, use native MUL
1045 * (regardless of the operands)
1046 * ( ii) if left and right are unsigned 8-bit operands,
1048 * (iii) if left or right is a literal in the range of [-128..256)
1049 * and the other is an unsigned byte, use native MUL
1053 /* use native mult for `*: <?> x <?> --> {u8_t, s8_t}' */
1054 if (sizeRes == 1) { return TRUE; }
1056 /* use native mult for `u8_t x u8_t --> { u16_t, s16_t }' */
1057 if (sizeL == 1 && symL /*&& SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic)))*/) {
1060 //printf( "%s: left too large (%u) / signed (%u)\n", __FUNCTION__, sizeL, symL && !SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic))));
1063 if (sizeR == 1 && symR /*&& SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic)))*/) {
1066 //printf( "%s: right too large (%u) / signed (%u)\n", __FUNCTION__, sizeR, symR && !SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic))));
1070 /* also allow literals [-128..256) for left/right operands */
1071 if (IS_VALOP(IC_LEFT(ic)))
1073 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1075 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1076 if (l >= -128 && l < 256)
1080 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1083 if (IS_VALOP( IC_RIGHT(ic) ))
1085 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1087 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1088 if (l >= -128 && l < 256)
1092 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1096 /* use native mult iff left and right are (unsigned) 8-bit operands */
1097 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1100 if (ic->op == '/' || ic->op == '%')
1102 /* We must catch /: {u8_t,s8_t} x {u8_t,s8_t} --> {u8_t,s8_t},
1103 * because SDCC will call 'divuchar' even for u8_t / s8_t.
1104 * Example: 128 / -2 becomes 128 / 254 = 0 != -64... */
1105 if (sizeL == 1 && sizeR == 1) return TRUE;
1107 /* What about literals? */
1108 if (IS_VALOP( IC_LEFT(ic) ))
1110 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1112 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1113 if (l >= -128 && l < 256)
1117 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1120 if (IS_VALOP( IC_RIGHT(ic) ))
1122 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1124 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1125 if (l >= -128 && l < 256)
1129 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1132 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1140 /* Do CSE estimation */
1141 static bool cseCostEstimation (iCode *ic, iCode *pdic)
1143 // operand *result = IC_RESULT(ic);
1144 // sym_link *result_type = operandType(result);
1147 /* VR -- this is an adhoc. Put here after conversation
1148 * with Erik Epetrich */
1152 || ic->op == EQ_OP) {
1154 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
1159 /* if it is a pointer then return ok for now */
1160 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
1162 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
1163 so we will cse only if they are local (i.e. both ic & pdic belong to
1164 the same basic block */
1165 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
1166 /* then if they are the same Basic block then ok */
1167 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
1172 /* for others it is cheaper to do the cse */
1178 /* Indicate which extended bit operations this port supports */
1180 hasExtBitOp (int op, int size)
1184 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
1191 /* Indicate the expense of an access to an output storage class */
1193 oclsExpense (struct memmap *oclass)
1195 /* The IN_FARSPACE test is compatible with historical behaviour, */
1196 /* but I don't think it is applicable to PIC. If so, please feel */
1197 /* free to remove this test -- EEP */
1198 if (IN_FARSPACE(oclass))
1204 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
1205 $2 is always the output file.
1206 $3 -L path and -l libraries
1207 $l is the list of extra options that should be there somewhere...
1208 MUST be terminated with a NULL.
1210 const char *pic16_linkCmd[] =
1212 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"","$3", NULL
1217 /** $1 is always the basename.
1218 $2 is always the output file.
1219 $3 varies (nothing currently)
1220 $l is the list of extra options that should be there somewhere...
1221 MUST be terminated with a NULL.
1223 const char *pic16_asmCmd[] =
1225 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1234 "MCU PIC16", /* Target name */
1235 "p18f452", /* Processor */
1238 TRUE, /* Emit glue around main */
1239 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1243 pic16_asmCmd, /* assembler command and arguments */
1244 NULL, /* alternate macro based form */
1245 "-g", /* arguments for debug mode */
1246 NULL, /* arguments for normal mode */
1247 0, /* print externs as global */
1248 ".asm", /* assembler file extension */
1249 NULL /* no do_assemble function */
1252 NULL, // pic16_linkCmd, /* linker command and arguments */
1253 NULL, /* alternate macro based form */
1254 _pic16_linkEdit, //NULL, /* no do_link function */
1255 ".o", /* extension for object files */
1256 0 /* no need for linker file */
1268 3, /* fptr, far pointers (see Microchip) */
1275 /* generic pointer tags */
1284 "XSEG (XDATA)", // xstack
1285 "STACK (DATA)", // istack
1286 "CSEG (CODE)", // code
1287 "DSEG (DATA)", // data
1288 "ISEG (DATA)", // idata
1289 "PSEG (DATA)", // pdata
1290 "XSEG (XDATA)", // xdata
1291 "BSEG (BIT)", // bit
1292 "RSEG (DATA)", // reg
1293 "GSINIT (CODE)", // static
1294 "OSEG (OVR,DATA)", // overlay
1295 "GSFINAL (CODE)", // post static
1296 "HOME (CODE)", // home
1299 "CONST (CODE)", // const_name - const data (code or not)
1300 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
1301 "XABS (ABS,XDATA)", // xabs_name - absolute xdata
1302 "IABS (ABS,DATA)", // iabs_name - absolute data
1303 NULL, // default location for auto vars
1304 NULL, // default location for global vars
1305 1 // code is read only 1=yes
1308 NULL, /* genExtraAreaDeclaration */
1309 NULL /* genExatrAreaLinkOptions */
1312 /* stack related information */
1313 -1, /* -1 stack grows downwards, +1 upwards */
1314 1, /* extra overhead when calling between banks */
1315 4, /* extra overhead when the function is an ISR */
1316 1, /* extra overhead for a function call */
1317 1, /* re-entrant space */
1318 0 /* 'banked' call overhead, mild overlap with bank_overhead */
1320 /* pic16 has an 8 bit mul */
1325 pic16_emitDebuggerSymbol
1328 255/3, /* maxCount */
1329 3, /* sizeofElement */
1330 /* The rest of these costs are bogus. They approximate */
1331 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
1332 {4,4,4}, /* sizeofMatchJump[] */
1333 {0,0,0}, /* sizeofRangeCompare[] */
1334 0, /* sizeofSubtract */
1335 3, /* sizeofDispatch */
1339 _pic16_parseOptions,
1342 _pic16_finaliseOptions,
1343 _pic16_setDefaultOptions,
1344 pic16_assignRegisters,
1347 _pic16_genAssemblerPreamble,
1348 NULL, /* no genAssemblerEnd */
1350 NULL, // _pic16_genXINIT
1351 NULL, /* genInitStartup */
1352 _pic16_reset_regparm,
1354 _process_pragma, /* process a pragma */
1355 _pic16_mangleFunctionName, /* mangles function name */
1357 hasExtBitOp, /* hasExtBitOp */
1358 oclsExpense, /* oclsExpense */
1360 TRUE, /* little endian */
1363 1, /* transform <= to ! > */
1364 1, /* transform >= to ! < */
1365 1, /* transform != to !(a == b) */
1367 FALSE, /* No array initializer support. */
1368 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
1369 NULL, /* no builtin functions */
1370 GPOINTER, /* treat unqualified pointers as "generic" pointers */
1371 1, /* reset labelKey to 1 */
1372 1, /* globals & local static allowed */