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 -------------------------------------------------------------------------*/
35 #include "dbuf_string.h"
38 static char _defaultRules[] =
43 /* list of key words used by pic16 */
44 static char *_pic16_keywords[] =
82 pic16_sectioninfo_t pic16_sectioninfo;
87 extern char *pic16_processor_base_name(void);
89 void pic16_pCodeInitRegisters(void);
91 void pic16_assignRegisters (ebbIndex *);
93 static int regParmFlg = 0; /* determine if we can register a parameter */
95 pic16_options_t pic16_options;
97 extern set *includeDirsSet;
98 extern set *dataDirsSet;
99 extern set *libFilesSet;
101 /* Also defined in gen.h, but the #include is commented out */
102 /* for an unknowned reason. - EEP */
103 void pic16_emitDebuggerSymbol (char *);
105 extern void pic16_emitConfigRegs(FILE *of);
106 extern void pic16_emitIDRegs(FILE *of);
113 asm_addTree (&asm_asxxxx_mapping);
114 pic16_pCodeInitRegisters();
119 _pic16_reset_regparm (void)
125 _pic16_regparm (sym_link * l, bool reentrant)
127 /* force all parameters via SEND/RECEIVE */
128 if(0 /*pic16_options.ip_stack*/) {
129 /* for this processor it is simple
130 * can pass only the first parameter in a register */
131 if(regParmFlg)return 0;
133 return 1; //regParmFlg;
135 /* otherwise pass all arguments in registers via SEND/RECEIVE */
142 int initsfpnt=0; /* set to 1 if source provides a pragma for stack
143 * so glue() later emits code to initialize stack/frame pointers */
146 set *sectNames=NULL; /* list of section listed in pragma directives */
147 set *sectSyms=NULL; /* list of symbols set in a specific section */
148 set *wparamList=NULL;
151 /* This is an experimental code for #pragma inline
152 and is temporarily disabled for 2.5.0 release */
153 set *asmInlineMap=NULL;
158 unsigned want_libc: 1;
159 unsigned want_libm: 1;
160 unsigned want_libio: 1;
161 unsigned want_libdebug: 1;
162 } libflags = { 0, 0, 0, 0, 0 };
174 do_pragma(int id, const char *name, const char *cp)
176 struct pragma_token_s token;
180 init_pragma_token(&token);
184 /* #pragma maxram [maxram] */
189 cp = get_pragma_token(cp, &token);
190 if (TOKEN_INT == token.type)
191 max_ram = token.val.int_val;
198 cp = get_pragma_token(cp, &token);
199 if (TOKEN_EOL != token.type)
205 pic16_setMaxRAM(max_ram);
209 /* #pragma stack [stack-position] [stack-len] */
212 unsigned int stackPos, stackLen;
216 cp = get_pragma_token(cp, &token);
217 if (TOKEN_INT != token.type)
222 stackPos = token.val.int_val;
224 cp = get_pragma_token(cp, &token);
225 if (TOKEN_INT != token.type)
230 stackLen = token.val.int_val;
232 cp = get_pragma_token(cp, &token);
233 if (TOKEN_EOL != token.type)
241 fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
242 filename, lineno, stackLen, stackLen);
245 /* check sanity of stack */
246 if ((stackPos >> 8) != ((stackPos + stackLen - 1) >> 8)) {
247 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
248 filename, lineno, stackPos, stackPos + stackLen - 1);
252 if (stackPos < pic16->acsSplitOfs) {
253 fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
254 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->acsSplitOfs);
257 if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
258 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
259 filename, lineno, stackPos, stackPos + stackLen - 1, 0xF00 + pic16->acsSplitOfs);
262 if (stackPos+stackLen > pic16->RAMsize) {
263 fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
264 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->RAMsize-1);
270 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
271 addSet(&pic16_fix_udata, reg);
273 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
274 addSet(&pic16_fix_udata, reg);
276 sym = newSymbol("stack", 0);
277 sprintf(sym->rname, "_%s", sym->name);
278 addSet(&publics, sym);
280 sym = newSymbol("stack_end", 0);
281 sprintf(sym->rname, "_%s", sym->name);
282 addSet(&publics, sym);
284 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
288 /* #pragma code [symbol] [location] */
293 cp = get_pragma_token(cp, &token);
294 if (TOKEN_STR != token.type)
297 absS = Safe_calloc(1, sizeof(absSym));
298 sprintf(absS->name, "_%s", get_pragma_string(&token));
300 cp = get_pragma_token(cp, &token);
301 if (TOKEN_INT != token.type)
304 //fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno);
308 absS->address = token.val.int_val;
310 cp = get_pragma_token(cp, &token);
311 if (TOKEN_EOL != token.type)
317 if ((absS->address % 2) != 0) {
319 fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
320 filename, lineno, absS->address);
323 addSet(&absSymSet, absS);
324 // fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
325 // __FILE__, __LINE__, symname, absS->address);
329 /* #pragma udata [section-name] [symbol] */
339 cp = get_pragma_token(cp, &token);
340 if (TOKEN_STR == token.type)
341 sectname = Safe_strdup(get_pragma_string(&token));
348 cp = get_pragma_token(cp, &token);
349 if (TOKEN_STR == token.type)
350 symname = get_pragma_string(&token);
353 //fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno);
360 ssym = Safe_calloc(1, sizeof(sectSym));
361 ssym->name = Safe_calloc(1, strlen(symname) + 2);
362 sprintf(ssym->name, "%s%s", port->fun_prefix, symname);
365 addSet(§Syms, ssym);
367 nsym = newSymbol((char *)symname, 0);
368 strcpy(nsym->rname, ssym->name);
371 checkAddSym(&publics, nsym);
375 for (snam = setFirstItem(sectNames);snam;snam=setNextItem(sectNames))
377 if (!strcmp(sectname, snam->name))
386 snam = Safe_calloc(1, sizeof(sectName));
387 snam->name = Safe_strdup(sectname);
388 snam->regsSet = NULL;
390 addSet(§Names, snam);
393 ssym->section = snam;
396 fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
397 ssym->name, snam->name, snam);
400 cp = get_pragma_token(cp, &token);
401 if (TOKEN_STR == token.type)
402 symname = get_pragma_string(&token);
403 else if (TOKEN_EOL == token.type)
416 /* #pragma library library_module */
421 cp = get_pragma_token(cp, &token);
422 if (TOKEN_EOL != token.type)
424 lmodule = get_pragma_string(&token);
427 * c link the C library
428 * math link the math library
429 * io link the IO library
430 * debug link the debug libary
431 * anything else, will link as-is */
433 if(!strcmp(lmodule, "c"))
434 libflags.want_libc = 1;
435 else if(!strcmp(lmodule, "math"))
436 libflags.want_libm = 1;
437 else if(!strcmp(lmodule, "io"))
438 libflags.want_libio = 1;
439 else if(!strcmp(lmodule, "debug"))
440 libflags.want_libdebug = 1;
441 else if(!strcmp(lmodule, "ignore"))
447 fprintf(stderr, "link library %s\n", lmodule);
448 addSetHead(&libFilesSet, (char *)lmodule);
458 cp = get_pragma_token(cp, &token);
459 if (TOKEN_EOL != token.type)
468 /* This is an experimental code for #pragma inline
469 and is temporarily disabled for 2.5.0 release */
472 char *tmp = strtok((char *)NULL, WHITECOMMA);
475 addSet(&asmInlineMap, Safe_strdup( tmp ));
476 tmp = strtok((char *)NULL, WHITECOMMA);
482 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
483 debugf("inline asm: `%s'\n", s);
495 get_pragma_token(cp, &token);
498 werror(W_BAD_PRAGMA_ARGUMENTS, name);
500 free_pragma_token(&token);
504 static struct pragma_s pragma_tbl[] = {
505 { "maxram", P_MAXRAM, 0, do_pragma },
506 { "stack", P_STACK, 0, do_pragma },
507 { "code", P_CODE, 0, do_pragma },
508 { "udata", P_UDATA, 0, do_pragma },
509 { "library", P_LIBRARY, 0, do_pragma },
510 /*{ "inline", P_INLINE, 0, do_pragma }, */
511 { NULL, 0, 0, NULL },
515 _process_pragma(const char *s)
517 return process_pragma_tbl(pragma_tbl, s);
520 #define REP_UDATA "--preplace-udata-with="
522 #define STACK_MODEL "--pstack-model="
523 #define OPT_BANKSEL "--obanksel="
525 #define ALT_ASM "--asm="
526 #define ALT_LINK "--link="
528 #define IVT_LOC "--ivt-loc="
529 #define NO_DEFLIBS "--nodefaultlibs"
530 #define MPLAB_COMPAT "--mplab-comp"
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;
548 int pic16_enable_peeps=0;
550 OPTION pic16_optionsTable[]= {
551 /* code generation options */
552 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
554 { 'y', "--extended", &xinst, "enable Extended Instruction Set/Literal Offset Addressing mode"},
556 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
558 /* optimization options */
559 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
560 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
561 { 0, OPTIMIZE_GOTO, NULL, "try to use (conditional) BRA instead of GOTO"},
562 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
563 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
565 /* assembling options */
566 { 0, ALT_ASM, NULL, "Use alternative assembler"},
567 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
569 /* linking options */
570 { 0, ALT_LINK, NULL, "Use alternative linker"},
571 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
572 { 0, IVT_LOC, NULL, "Set address of interrupt vector table."},
573 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
574 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
575 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
577 /* debugging options */
578 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
579 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
580 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
581 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
582 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
583 { 0, NULL, NULL, NULL}
587 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
590 _pic16_parseOptions (int *pargc, char **argv, int *i)
595 /* TODO: allow port-specific command line options to specify
596 * segment names here.
599 /* check for arguments that have associated an integer variable */
600 while(pic16_optionsTable[j].pparameter) {
601 if(ISOPT( pic16_optionsTable[j].longOpt )) {
602 (*pic16_optionsTable[j].pparameter)++;
608 if(ISOPT(STACK_MODEL)) {
609 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
610 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
611 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
613 fprintf(stderr, "Unknown stack model: %s", stkmodel);
619 if(ISOPT(OPT_BANKSEL)) {
620 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
624 if(ISOPT(REP_UDATA)) {
625 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
630 alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
634 if(ISOPT(ALT_LINK)) {
635 alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
640 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
641 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
646 pic16_options.no_crt = 0;
647 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
653 if(ISOPT(OFMSG_LRSUPPORT)) {
654 pic16_options.opt_flags |= OF_LR_SUPPORT;
659 if (ISOPT(OPTIMIZE_GOTO)) {
660 pic16_options.opt_flags |= OF_OPTIMIZE_GOTO;
664 if(ISOPT(OPTIMIZE_CMP)) {
665 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
669 if (ISOPT(OPTIMIZE_DF)) {
670 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
678 extern set *userIncDirsSet;
680 static void _pic16_initPaths(void)
682 set *pic16incDirsSet=NULL;
683 set *pic16libDirsSet=NULL;
686 setMainValue("mcu", pic16->name[2] );
687 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
689 setMainValue("mcu1", pic16->name[1] );
690 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
692 if(!options.nostdinc) {
693 struct dbuf_s pic16incDir;
695 dbuf_init(&pic16incDir, 128);
696 dbuf_makePath(&pic16incDir, INCLUDE_DIR_SUFFIX, "pic16");
698 /* setup pic16 include directory */
699 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, dbuf_c_str(&pic16incDir));
700 dbuf_destroy(&pic16incDir);
701 includeDirsSet = pic16incDirsSet;
702 // mergeSets(&includeDirsSet, pic16incDirsSet);
704 /* pic16 port should not search to the SDCC standard include directories,
705 * so add here the deleted include dirs that user has issued in command line */
706 mergeSets(&pic16incDirsSet, userIncDirsSet);
708 if(!options.nostdlib) {
709 struct dbuf_s pic16libDir;
711 dbuf_init(&pic16libDir, 128);
712 dbuf_makePath(&pic16libDir, LIB_DIR_SUFFIX, "pic16");
713 /* setup pic16 library directory */
714 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, dbuf_c_str(&pic16libDir));
715 dbuf_destroy(&pic16libDir);
716 libDirsSet = pic16libDirsSet;
717 // mergeSets(&libDirsSet, pic16libDirsSet);
720 if(!pic16_options.nodefaultlibs) {
721 /* now add the library for the device */
722 sprintf(devlib, "%s.lib", pic16->name[2]);
723 addSet(&libFilesSet, Safe_strdup(devlib));
725 /* add the internal SDCC library */
726 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
730 extern set *linkOptionsSet;
731 char *msprintf(hTab *pvals, const char *pformat, ...);
732 int my_system(const char *cmd);
734 /* forward declarations */
735 extern const char *pic16_linkCmd[];
736 extern const char *pic16_asmCmd[];
737 extern set *asmOptionsSet;
739 /* custom function to link objects */
740 static void _pic16_linkEdit(void)
742 hTab *linkValues=NULL;
750 * link command format:
751 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
754 sprintf(lfrm, "{linker} {incdirs} {lflags} -w -r -o {outfile} {user_ofile} {ofiles} {spec_ofiles} {libs}");
756 shash_add(&linkValues, "linker", pic16_linkCmd[0]);
758 mergeSets(&tSet, libDirsSet);
759 mergeSets(&tSet, libPathsSet);
761 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
762 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
764 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
766 if(fullSrcFileName) {
767 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName);
768 // addSetHead(&relFilesSet, Safe_strdup(temp));
769 shash_add(&linkValues, "user_ofile", temp);
772 if(!pic16_options.no_crt)
773 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
775 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
777 if(!libflags.ignore) {
778 if(libflags.want_libc)
779 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
781 if(libflags.want_libm)
782 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
784 if(libflags.want_libio) {
785 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
786 addSet(&libFilesSet, Safe_strdup(temp));
789 if(libflags.want_libdebug)
790 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
793 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
795 lcmd = msprintf(linkValues, lfrm);
797 ret = my_system( lcmd );
807 _pic16_finaliseOptions (void)
809 port->mem.default_local_map = data;
810 port->mem.default_globl_map = data;
812 /* peepholes are disabled for the time being */
815 /* explicit enable peepholes for testing */
816 if(pic16_enable_peeps)
819 options.all_callee_saves = 1; // always callee saves
822 options.float_rent = 1;
823 options.intlong_rent = 1;
827 if(alt_asm && strlen(alt_asm))
828 pic16_asmCmd[0] = alt_asm;
830 if(alt_link && strlen(alt_link))
831 pic16_linkCmd[0] = alt_link;
833 if(!pic16_options.no_crt) {
834 pic16_options.omit_ivt = 1;
835 pic16_options.leave_reset = 0;
838 if(options.model == MODEL_SMALL)
839 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
841 if(options.model == MODEL_LARGE)
842 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
847 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
848 *(strrchr(buf, 'f')) = 'F';
849 addSet(&asmOptionsSet, Safe_strdup( buf ));
852 if(STACK_MODEL_LARGE) {
853 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
854 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
856 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
857 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
863 if (options.model == MODEL_LARGE)
865 port->mem.default_local_map = xdata;
866 port->mem.default_globl_map = xdata;
870 port->mem.default_local_map = data;
871 port->mem.default_globl_map = data;
874 if (options.stack10bit)
876 if (options.model != MODEL_FLAT24)
879 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
880 fprintf (stderr, "\t10 bit stack mode disabled.\n");
881 options.stack10bit = 0;
885 /* Fixup the memory map for the stack; it is now in
886 * far space and requires a FPOINTER to access it.
889 istack->ptrType = FPOINTER;
896 _pic16_setDefaultOptions (void)
898 options.stackAuto = 0; /* implicit declaration */
899 /* port is not capable yet to allocate separate registers
900 * dedicated for passing certain parameters */
902 /* initialize to defaults section locations, names and addresses */
903 pic16_sectioninfo.at_udata = "udata";
905 /* set pic16 port options to defaults */
906 pic16_options.no_banksel = 0;
907 pic16_options.opt_banksel = 0;
908 pic16_options.omit_configw = 0;
909 pic16_options.omit_ivt = 0;
910 pic16_options.leave_reset = 0;
911 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
912 pic16_options.ivt_loc = 0x000000;
913 pic16_options.nodefaultlibs = 0;
914 pic16_options.dumpcalltree = 0;
915 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
916 pic16_options.no_crt = 0; /* use crt by default */
917 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
918 pic16_options.gstack = 0;
919 pic16_options.debgen = 0;
923 _pic16_getRegName (struct regs *reg)
932 static char *_pic16_mangleFunctionName(char *sz)
934 // fprintf(stderr, "mangled function name: %s\n", sz);
942 _pic16_genAssemblerPreamble (FILE * of)
944 char *name = pic16_processor_base_name();
948 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
951 fprintf (of, "\tlist\tp=%s\n",&name[1]);
952 if (pic16_mplab_comp) {
953 // provide ACCESS macro used during SFR accesses
954 fprintf (of, "\tinclude <p%s.inc>\n", &name[1]);
957 if(!pic16_options.omit_configw) {
958 pic16_emitConfigRegs(of);
960 pic16_emitIDRegs(of);
963 fprintf (of, "\tradix dec\n");
966 /* Generate interrupt vector table. */
968 _pic16_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts)
971 /* PIC18F family has only two interrupts, the high and the low
972 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
974 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
975 dbuf_printf(oBuf, "; RESET vector\n");
976 dbuf_printf(oBuf, "\tgoto\t__sdcc_gsinit_startup\n");
979 if(!pic16_options.omit_ivt) {
980 dbuf_printf(oBuf, "\tres 4\n");
983 dbuf_printf(oBuf, "; High priority interrupt vector 0x0008\n");
985 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[1]->rname);
986 dbuf_printf(oBuf, "\tres\t12\n");
988 dbuf_printf(oBuf, "\tretfie\n");
989 dbuf_printf(oBuf, "\tres\t14\n");
992 dbuf_printf(oBuf, "; Low priority interrupt vector 0x0018\n");
994 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[2]->rname);
996 dbuf_printf(oBuf, "\tretfie\n");
1003 /* return True if the port can handle the type,
1004 * False to convert it to function call */
1005 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
1007 //fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
1008 int symL, symR, symRes, sizeL = 0, sizeR = 0, sizeRes = 0;
1010 /* left/right are symbols? */
1011 symL = IS_SYMOP(IC_LEFT(ic));
1012 symR = IS_SYMOP(IC_RIGHT(ic));
1013 symRes = IS_SYMOP(IC_RESULT(ic));
1015 /* --> then determine their sizes */
1016 sizeL = symL ? getSize(OP_SYM_TYPE(IC_LEFT(ic))) : 4;
1017 sizeR = symR ? getSize(OP_SYM_TYPE(IC_RIGHT(ic))) : 4;
1018 sizeRes = symRes ? getSize(OP_SYM_TYPE(IC_RESULT(ic))) : 4;
1020 /* Checks to enable native multiplication.
1021 * PICs do not offer native division at all...
1024 * ( i) if result is just one byte, use native MUL
1025 * (regardless of the operands)
1026 * ( ii) if left and right are unsigned 8-bit operands,
1028 * (iii) if left or right is a literal in the range of [-128..256)
1029 * and the other is an unsigned byte, use native MUL
1033 /* use native mult for `*: <?> x <?> --> {u8_t, s8_t}' */
1034 if (sizeRes == 1) { return TRUE; }
1036 /* use native mult for `u8_t x u8_t --> { u16_t, s16_t }' */
1037 if (sizeL == 1 && symL /*&& SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic)))*/) {
1040 //printf( "%s: left too large (%u) / signed (%u)\n", __FUNCTION__, sizeL, symL && !SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic))));
1043 if (sizeR == 1 && symR /*&& SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic)))*/) {
1046 //printf( "%s: right too large (%u) / signed (%u)\n", __FUNCTION__, sizeR, symR && !SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic))));
1050 /* also allow literals [-128..256) for left/right operands */
1051 if (IS_VALOP(IC_LEFT(ic)))
1053 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1055 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1056 if (l >= -128 && l < 256)
1060 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1063 if (IS_VALOP( IC_RIGHT(ic) ))
1065 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1067 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1068 if (l >= -128 && l < 256)
1072 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1076 /* use native mult iff left and right are (unsigned) 8-bit operands */
1077 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1080 if (ic->op == '/' || ic->op == '%')
1082 /* We must catch /: {u8_t,s8_t} x {u8_t,s8_t} --> {u8_t,s8_t},
1083 * because SDCC will call 'divuchar' even for u8_t / s8_t.
1084 * Example: 128 / -2 becomes 128 / 254 = 0 != -64... */
1085 if (sizeL == 1 && sizeR == 1) return TRUE;
1087 /* What about literals? */
1088 if (IS_VALOP( IC_LEFT(ic) ))
1090 long l = (long)floatFromVal( OP_VALUE( IC_LEFT(ic) ) );
1092 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1093 if (l >= -128 && l < 256)
1097 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1100 if (IS_VALOP( IC_RIGHT(ic) ))
1102 long l = (long)floatFromVal( OP_VALUE( IC_RIGHT(ic) ) );
1104 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1105 if (l >= -128 && l < 256)
1109 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1112 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1120 /* Do CSE estimation */
1121 static bool cseCostEstimation (iCode *ic, iCode *pdic)
1123 // operand *result = IC_RESULT(ic);
1124 // sym_link *result_type = operandType(result);
1127 /* VR -- this is an adhoc. Put here after conversation
1128 * with Erik Epetrich */
1132 || ic->op == EQ_OP) {
1134 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
1139 /* if it is a pointer then return ok for now */
1140 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
1142 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
1143 so we will cse only if they are local (i.e. both ic & pdic belong to
1144 the same basic block */
1145 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
1146 /* then if they are the same Basic block then ok */
1147 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
1152 /* for others it is cheaper to do the cse */
1158 /* Indicate which extended bit operations this port supports */
1160 hasExtBitOp (int op, int size)
1164 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
1171 /* Indicate the expense of an access to an output storage class */
1173 oclsExpense (struct memmap *oclass)
1175 /* The IN_FARSPACE test is compatible with historical behaviour, */
1176 /* but I don't think it is applicable to PIC. If so, please feel */
1177 /* free to remove this test -- EEP */
1178 if (IN_FARSPACE(oclass))
1184 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
1185 $2 is always the output file.
1186 $3 -L path and -l libraries
1187 $l is the list of extra options that should be there somewhere...
1188 MUST be terminated with a NULL.
1190 const char *pic16_linkCmd[] =
1192 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"","$3", NULL
1197 /** $1 is always the basename.
1198 $2 is always the output file.
1199 $3 varies (nothing currently)
1200 $l is the list of extra options that should be there somewhere...
1201 MUST be terminated with a NULL.
1203 const char *pic16_asmCmd[] =
1205 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1214 "MCU PIC16", /* Target name */
1215 "p18f452", /* Processor */
1218 TRUE, /* Emit glue around main */
1219 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1223 pic16_asmCmd, /* assembler command and arguments */
1224 NULL, /* alternate macro based form */
1225 "-g", /* arguments for debug mode */
1226 NULL, /* arguments for normal mode */
1227 0, /* print externs as global */
1228 ".asm", /* assembler file extension */
1229 NULL /* no do_assemble function */
1232 NULL, // pic16_linkCmd, /* linker command and arguments */
1233 NULL, /* alternate macro based form */
1234 _pic16_linkEdit, //NULL, /* no do_link function */
1235 ".o", /* extension for object files */
1236 0 /* no need for linker file */
1248 3, /* fptr, far pointers (see Microchip) */
1255 /* generic pointer tags */
1264 "XSEG (XDATA)", // xstack
1265 "STACK (DATA)", // istack
1266 "CSEG (CODE)", // code
1267 "DSEG (DATA)", // data
1268 "ISEG (DATA)", // idata
1269 "PSEG (DATA)", // pdata
1270 "XSEG (XDATA)", // xdata
1271 "BSEG (BIT)", // bit
1272 "RSEG (DATA)", // reg
1273 "GSINIT (CODE)", // static
1274 "OSEG (OVR,DATA)", // overlay
1275 "GSFINAL (CODE)", // post static
1276 "HOME (CODE)", // home
1279 "CONST (CODE)", // const_name - const data (code or not)
1280 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
1281 "XABS (ABS,XDATA)", // xabs_name - absolute xdata
1282 "IABS (ABS,DATA)", // iabs_name - absolute data
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 */