1 /*-------------------------------------------------------------------------
3 main.c - pic16 specific general functions.
5 Written by - Scott Dattalo scott@dattalo.com
6 Ported to PIC16 by - Martin Dubuc m.debuc@rogers.com
8 Note that mlh prepended _pic16_ on the static functions. Makes
9 it easier to set a breakpoint using the debugger.
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by the
14 Free Software Foundation; either version 2, or (at your option) any
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 -------------------------------------------------------------------------*/
37 static char _defaultRules[] =
42 /* list of key words used by pic16 */
43 static char *_pic16_keywords[] =
76 pic16_sectioninfo_t pic16_sectioninfo;
79 extern char *pic16_processor_base_name(void);
81 void pic16_pCodeInitRegisters(void);
83 void pic16_assignRegisters (ebbIndex *);
85 static int regParmFlg = 0; /* determine if we can register a parameter */
87 pic16_options_t pic16_options;
89 extern set *includeDirsSet;
90 extern set *dataDirsSet;
91 extern set *libFilesSet;
93 /* Also defined in gen.h, but the #include is commented out */
94 /* for an unknowned reason. - EEP */
95 void pic16_emitDebuggerSymbol (char *);
97 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
98 extern void pic16_emitConfigRegs(FILE *of);
99 extern void pic16_emitIDRegs(FILE *of);
106 asm_addTree (&asm_asxxxx_mapping);
107 pic16_pCodeInitRegisters();
112 _pic16_reset_regparm (void)
118 _pic16_regparm (sym_link * l)
120 /* force all parameters via SEND/RECEIVE */
121 if(0 /*pic16_options.ip_stack*/) {
122 /* for this processor it is simple
123 * can pass only the first parameter in a register */
124 if(regParmFlg)return 0;
126 return 1; //regParmFlg;
128 /* otherwise pass all arguments in registers via SEND/RECEIVE */
135 int initsfpnt=0; /* set to 1 if source provides a pragma for stack
136 * so glue() later emits code to initialize stack/frame pointers */
139 set *sectNames=NULL; /* list of section listed in pragma directives */
140 set *sectSyms=NULL; /* list of symbols set in a specific section */
141 set *wparamList=NULL;
144 /* This is an experimental code for #pragma inline
145 and is temporarily disabled for 2.5.0 release */
146 set *asmInlineMap=NULL;
151 unsigned want_libc: 1;
152 unsigned want_libm: 1;
153 unsigned want_libio: 1;
154 unsigned want_libdebug: 1;
155 } libflags = { 0, 0, 0, 0, 0 };
159 _process_pragma(const char *sz)
161 static const char *WHITE = " \t\n";
162 static const char *WHITECOMMA = " \t\n,";
163 char *ptr = strtok((char *)sz, WHITE);
165 /* #pragma maxram [maxram] */
166 if (startsWith (ptr, "maxram")) {
167 char *maxRAM = strtok((char *)NULL, WHITE);
169 if (maxRAM != (char *)NULL) {
173 maxRAMVal = constVal(maxRAM);
174 maxRAMaddress = (int)floatFromVal(maxRAMVal);
175 pic16_setMaxRAM(maxRAMaddress);
181 /* #pragma stack [stack-position] [stack-len] */
182 if(startsWith(ptr, "stack")) {
183 char *stackPosS = strtok((char *)NULL, WHITE);
184 char *stackLenS = strtok((char *)NULL, WHITE);
191 fprintf (stderr, "%s:%d: #pragma stack [stack-pos] [stack-length] -- stack-position missing\n", filename, lineno-1);
193 return 0; /* considered an error */
196 stackPosVal = constVal( stackPosS );
197 stackPos = (unsigned int)floatFromVal( stackPosVal );
200 stackLenVal = constVal( stackLenS );
201 stackLen = (unsigned int)floatFromVal( stackLenVal );
206 fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
207 filename, lineno-1, stackLen, stackLen);
209 // fprintf(stderr, "%s:%d setting stack to default size %d\n", __FILE__, __LINE__, stackLen);
212 // fprintf(stderr, "Initializing stack pointer at 0x%x len 0x%x\n", stackPos, stackLen);
214 /* check sanity of stack */
215 if ((stackPos >> 8) != ((stackPos+stackLen) >> 8)) {
216 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
217 filename,lineno-1, stackPos, stackPos+stackLen-1);
221 if (stackPos < pic16->acsSplitOfs) {
222 fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
223 filename, lineno-1, stackPos, stackPos+stackLen-1, pic16->acsSplitOfs);
226 if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
227 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
228 filename, lineno-1, stackPos, stackPos+stackLen-1, 0xF00 + pic16->acsSplitOfs);
231 if (stackPos+stackLen > pic16->RAMsize) {
232 fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
233 filename, lineno-1, stackPos, stackPos+stackLen-1, pic16->RAMsize-1);
235 return 1; /* considered an error, but this reports "invalid pragma stack"... */
239 reg=newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
240 addSet(&pic16_fix_udata, reg);
242 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
243 addSet(&pic16_fix_udata, reg);
245 sym = newSymbol("stack", 0);
246 sprintf(sym->rname, "_%s", sym->name);
247 addSet(&publics, sym);
249 sym = newSymbol("stack_end", 0);
250 sprintf(sym->rname, "_%s", sym->name);
251 addSet(&publics, sym);
253 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
258 /* #pragma code [symbol] [location] */
259 if(startsWith(ptr, "code")) {
260 char *symname = strtok((char *)NULL, WHITE);
261 char *location = strtok((char *)NULL, WHITE);
265 if (!symname || !location) {
266 fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno-1);
268 return 1; /* considered an error, but this reports "invalid pragma code"... */
271 absS = Safe_calloc(1, sizeof(absSym));
272 sprintf(absS->name, "_%s", symname);
274 addr = constVal( location );
275 absS->address = (unsigned int)floatFromVal( addr );
277 if((absS->address % 2) != 0) {
279 fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
280 filename, lineno-1, absS->address);
283 addSet(&absSymSet, absS);
284 // fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
285 // __FILE__, __LINE__, symname, absS->address);
290 /* #pragma udata [section-name] [symbol] */
291 if(startsWith(ptr, "udata")) {
292 char *sectname = strtok((char *)NULL, WHITE);
293 char *symname = strtok((char *)NULL, WHITE);
299 if (!symname || !sectname) {
300 fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno-1);
302 return 1; /* considered an error, but this reports "invalid pragma code"... */
306 ssym = Safe_calloc(1, sizeof(sectSym));
307 ssym->name = Safe_calloc(1, strlen(symname)+2);
308 sprintf(ssym->name, "%s%s", port->fun_prefix, symname);
311 addSet(§Syms, ssym);
313 nsym = newSymbol(symname, 0);
314 strcpy(nsym->rname, ssym->name);
317 checkAddSym(&publics, nsym);
321 for(snam=setFirstItem(sectNames);snam;snam=setNextItem(sectNames)) {
322 if(!strcmp(sectname, snam->name)){ found=1; break; }
326 snam = Safe_calloc(1, sizeof(sectName));
327 snam->name = Safe_strdup( sectname );
328 snam->regsSet = NULL;
330 addSet(§Names, snam);
333 ssym->section = snam;
336 fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
337 ssym->name, snam->name, snam);
340 symname = strtok((char *)NULL, WHITE);
346 /* #pragma wparam function1[, function2[,...]] */
347 if(startsWith(ptr, "wparam")) {
348 char *fname = strtok((char *)NULL, WHITECOMMA);
352 fprintf(stderr, "PIC16 Warning: `%s' wparam pragma is obsolete. use function attribute `wparam' instead.\n", fname);
353 addSet(&wparamList, Safe_strdup(fname));
355 // debugf("passing with WREG to %s\n", fname);
356 fname = strtok((char *)NULL, WHITECOMMA);
362 /* #pragma library library_module */
363 if(startsWith(ptr, "library")) {
364 char *lmodule = strtok((char *)NULL, WHITE);
368 * c link the C library
369 * math link the math library
370 * io link the IO library
371 * debug link the debug libary
372 * anything else, will link as-is */
374 if(!strcmp(lmodule, "c"))libflags.want_libc = 1;
375 else if(!strcmp(lmodule, "math"))libflags.want_libm = 1;
376 else if(!strcmp(lmodule, "io"))libflags.want_libio = 1;
377 else if(!strcmp(lmodule, "debug"))libflags.want_libdebug = 1;
378 else if(!strcmp(lmodule, "ignore"))libflags.ignore = 1;
380 if(!libflags.ignore) {
381 fprintf(stderr, "link library %s\n", lmodule);
382 addSetHead(&libFilesSet, lmodule);
391 /* This is an experimental code for #pragma inline
392 and is temporarily disabled for 2.5.0 release */
393 if(startsWith(ptr, "inline")) {
394 char *tmp = strtok((char *)NULL, WHITECOMMA);
397 addSet(&asmInlineMap, Safe_strdup( tmp ));
398 tmp = strtok((char *)NULL, WHITECOMMA);
404 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
405 debugf("inline asm: `%s'\n", s);
416 #define REP_UDATA "--preplace-udata-with="
418 #define STACK_MODEL "--pstack-model="
419 #define OPT_BANKSEL "--obanksel="
421 #define ALT_ASM "--asm="
422 #define ALT_LINK "--link="
424 #define IVT_LOC "--ivt-loc="
425 #define NO_DEFLIBS "--nodefaultlibs"
426 #define MPLAB_COMPAT "--mplab-comp"
428 #define NL_OPT "--nl="
429 #define USE_CRT "--use-crt="
431 #define OFMSG_LRSUPPORT "--flr-support"
433 #define OPTIMIZE_GOTO "--optimize-goto"
434 #define OPTIMIZE_CMP "--optimize-cmp"
435 #define OPTIMIZE_DF "--optimize-df"
440 int pic16_mplab_comp=0;
441 extern int pic16_debug_verbose;
442 extern int pic16_ralloc_debug;
443 extern int pic16_pcode_verbose;
446 int pic16_enable_peeps=0;
447 int pic16_nl=0; /* 0 for LF, 1 for CRLF */
449 OPTION pic16_optionsTable[]= {
450 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
451 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
452 { 0, OPT_BANKSEL, NULL, "set banksel optimization level (default=0 no)"},
453 // { 0, "--pomit-config-words", &pic16_options.omit_configw, "omit the generation of configuration words"},
454 // { 0, "--pomit-ivt", &pic16_options.omit_ivt, "omit the generation of the Interrupt Vector Table"},
455 // { 0, "--pleave-reset-vector",&pic16_options.leave_reset, "when omitting IVT leave RESET vector"},
456 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
458 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
459 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
460 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
462 { 0, REP_UDATA, NULL, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
464 { 0, ALT_ASM, NULL, "Use alternative assembler"},
465 { 0, ALT_LINK, NULL, "Use alternative linker"},
467 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
468 { 0, IVT_LOC, NULL, "<nnnn> interrupt vector table location"},
469 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
470 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
471 { 0, "--fstack", &pic16_fstack, "enable stack optimizations"},
472 { 0, NL_OPT, NULL, "new line, \"lf\" or \"crlf\""},
473 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
474 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
475 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
476 { 0, OPTIMIZE_GOTO, NULL, "try to use (conditional) BRA instead of GOTO"},
477 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
478 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
479 { 0, NULL, NULL, NULL}
483 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
485 extern char *getStringArg(const char *, char **, int *, int);
486 extern int getIntArg(const char *, char **, int *, int);
489 _pic16_parseOptions (int *pargc, char **argv, int *i)
494 /* TODO: allow port-specific command line options to specify
495 * segment names here.
498 /* check for arguments that have associated an integer variable */
499 while(pic16_optionsTable[j].pparameter) {
500 if(ISOPT( pic16_optionsTable[j].longOpt )) {
501 (*pic16_optionsTable[j].pparameter)++;
507 if(ISOPT(STACK_MODEL)) {
508 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
509 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
510 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
512 fprintf(stderr, "Unknown stack model: %s", stkmodel);
518 if(ISOPT(OPT_BANKSEL)) {
519 pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
523 if(ISOPT(REP_UDATA)) {
524 pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
529 alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
533 if(ISOPT(ALT_LINK)) {
534 alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
539 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
540 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
547 tmp = Safe_strdup( getStringArg(NL_OPT, argv, i, *pargc) );
548 if(!STRCASECMP(tmp, "lf"))pic16_nl = 0;
549 else if(!STRCASECMP(tmp, "crlf"))pic16_nl = 1;
551 fprintf(stderr, "invalid termination character id\n");
558 pic16_options.no_crt = 0;
559 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
565 if(ISOPT(OFMSG_LRSUPPORT)) {
566 pic16_options.opt_flags |= OF_LR_SUPPORT;
571 if (ISOPT(OPTIMIZE_GOTO)) {
572 pic16_options.opt_flags |= OF_OPTIMIZE_GOTO;
576 if(ISOPT(OPTIMIZE_CMP)) {
577 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
581 if (ISOPT(OPTIMIZE_DF)) {
582 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
590 extern set *userIncDirsSet;
592 static void _pic16_initPaths(void)
594 char pic16incDir[512];
595 char pic16libDir[512];
596 set *pic16incDirsSet=NULL;
597 set *pic16libDirsSet=NULL;
600 setMainValue("mcu", pic16->name[2] );
601 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
603 setMainValue("mcu1", pic16->name[1] );
604 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
606 sprintf(pic16incDir, "%s%cpic16", INCLUDE_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
607 sprintf(pic16libDir, "%s%cpic16", LIB_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
610 if(!options.nostdinc) {
611 /* setup pic16 include directory */
612 pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
613 includeDirsSet = pic16incDirsSet;
614 // mergeSets(&includeDirsSet, pic16incDirsSet);
616 /* pic16 port should not search to the SDCC standard include directories,
617 * so add here the deleted include dirs that user has issued in command line */
618 mergeSets(&pic16incDirsSet, userIncDirsSet);
620 if(!options.nostdlib) {
621 /* setup pic16 library directory */
622 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
623 libDirsSet = pic16libDirsSet;
624 // mergeSets(&libDirsSet, pic16libDirsSet);
627 if(!pic16_options.nodefaultlibs) {
628 /* now add the library for the device */
629 sprintf(devlib, "%s.lib", pic16->name[2]);
630 addSet(&libFilesSet, Safe_strdup(devlib));
632 /* add the internal SDCC library */
633 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
637 extern set *linkOptionsSet;
638 char *msprintf(hTab *pvals, const char *pformat, ...);
639 int my_system(const char *cmd);
641 /* custom function to link objects */
642 static void _pic16_linkEdit(void)
644 hTab *linkValues=NULL;
652 * link command format:
653 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
657 sprintf(lfrm, "{linker} {incdirs} {lflags} -o {outfile} {user_ofile} {spec_ofiles} {ofiles} {libs}");
659 shash_add(&linkValues, "linker", "gplink");
661 mergeSets(&tSet, libDirsSet);
662 mergeSets(&tSet, libPathsSet);
664 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
665 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
667 shash_add(&linkValues, "outfile", dstFileName);
669 if(fullSrcFileName) {
670 sprintf(temp, "%s.o", dstFileName);
671 // addSetHead(&relFilesSet, Safe_strdup(temp));
672 shash_add(&linkValues, "user_ofile", temp);
675 if(!pic16_options.no_crt)
676 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
678 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
680 if(!libflags.ignore) {
681 if(libflags.want_libc)
682 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
684 if(libflags.want_libm)
685 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
687 if(libflags.want_libio) {
688 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
689 addSet(&libFilesSet, Safe_strdup(temp));
692 if(libflags.want_libdebug)
693 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
696 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
698 lcmd = msprintf(linkValues, lfrm);
700 ret = my_system( lcmd );
709 /* forward declarations */
710 extern const char *pic16_linkCmd[];
711 extern const char *pic16_asmCmd[];
712 extern set *asmOptionsSet;
715 _pic16_finaliseOptions (void)
717 port->mem.default_local_map = data;
718 port->mem.default_globl_map = data;
720 /* peepholes are disabled for the time being */
723 /* explicit enable peepholes for testing */
724 if(pic16_enable_peeps)
727 options.all_callee_saves = 1; // always callee saves
730 options.float_rent = 1;
731 options.intlong_rent = 1;
735 if(alt_asm && strlen(alt_asm))
736 pic16_asmCmd[0] = alt_asm;
738 if(alt_link && strlen(alt_link))
739 pic16_linkCmd[0] = alt_link;
741 if(!pic16_options.no_crt) {
742 pic16_options.omit_ivt = 1;
743 pic16_options.leave_reset = 0;
746 if(options.model == MODEL_SMALL)
747 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
749 if(options.model == MODEL_LARGE)
750 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
755 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
756 *(strrchr(buf, 'f')) = 'F';
757 addSet(&asmOptionsSet, Safe_strdup( buf ));
760 if(STACK_MODEL_LARGE) {
761 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
762 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
764 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
765 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
771 if (options.model == MODEL_LARGE)
773 port->mem.default_local_map = xdata;
774 port->mem.default_globl_map = xdata;
778 port->mem.default_local_map = data;
779 port->mem.default_globl_map = data;
782 if (options.stack10bit)
784 if (options.model != MODEL_FLAT24)
787 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
788 fprintf (stderr, "\t10 bit stack mode disabled.\n");
789 options.stack10bit = 0;
793 /* Fixup the memory map for the stack; it is now in
794 * far space and requires a FPOINTER to access it.
797 istack->ptrType = FPOINTER;
804 _pic16_setDefaultOptions (void)
806 options.stackAuto = 0; /* implicit declaration */
807 /* port is not capable yet to allocate separate registers
808 * dedicated for passing certain parameters */
810 /* initialize to defaults section locations, names and addresses */
811 pic16_sectioninfo.at_udata = "udata";
813 /* set pic16 port options to defaults */
814 pic16_options.no_banksel = 0;
815 pic16_options.opt_banksel = 0;
816 pic16_options.omit_configw = 0;
817 pic16_options.omit_ivt = 0;
818 pic16_options.leave_reset = 0;
819 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
820 pic16_options.ivt_loc = 0x000000;
821 pic16_options.nodefaultlibs = 0;
822 pic16_options.dumpcalltree = 0;
823 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
824 pic16_options.no_crt = 0; /* use crt by default */
825 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
826 pic16_options.gstack = 0;
827 pic16_options.debgen = 0;
831 _pic16_getRegName (struct regs *reg)
840 static char *_pic16_mangleFunctionName(char *sz)
842 // fprintf(stderr, "mangled function name: %s\n", sz);
850 _pic16_genAssemblerPreamble (FILE * of)
852 char *name = pic16_processor_base_name();
856 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
859 fprintf (of, "\tlist\tp=%s\n",&name[1]);
861 if(!pic16_options.omit_configw) {
862 pic16_emitConfigRegs(of);
864 pic16_emitIDRegs(of);
867 fprintf (of, "\tradix dec\n");
870 /* Generate interrupt vector table. */
872 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
875 /* PIC18F family has only two interrupts, the high and the low
876 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
878 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
879 fprintf(of, "; RESET vector\n");
880 fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
883 if(!pic16_options.omit_ivt) {
884 fprintf(of, "\tres 4\n");
887 fprintf(of, "; High priority interrupt vector 0x0008\n");
889 fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
890 fprintf(of, "\tres\t12\n");
892 fprintf(of, "\tretfie\n");
893 fprintf(of, "\tres\t14\n");
896 fprintf(of, "; Low priority interrupt vector 0x0018\n");
898 fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
900 fprintf(of, "\tretfie\n");
907 /* return True if the port can handle the type,
908 * False to convert it to function call */
909 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
911 // fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
914 /* multiplication is fixed */
915 /* support mul for char/int/long */
917 && (getSize(OP_SYMBOL(IC_LEFT(ic))->type ) < 2))return TRUE;
921 /* support div for char/int/long */
922 if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) < 0)
923 && (ic->op == '/'))return TRUE;
931 /* Do CSE estimation */
932 static bool cseCostEstimation (iCode *ic, iCode *pdic)
934 // operand *result = IC_RESULT(ic);
935 // sym_link *result_type = operandType(result);
938 /* VR -- this is an adhoc. Put here after conversation
939 * with Erik Epetrich */
943 || ic->op == EQ_OP) {
945 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
950 /* if it is a pointer then return ok for now */
951 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
953 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
954 so we will cse only if they are local (i.e. both ic & pdic belong to
955 the same basic block */
956 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
957 /* then if they are the same Basic block then ok */
958 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
963 /* for others it is cheaper to do the cse */
969 /* Indicate which extended bit operations this port supports */
971 hasExtBitOp (int op, int size)
975 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
982 /* Indicate the expense of an access to an output storage class */
984 oclsExpense (struct memmap *oclass)
986 /* The IN_FARSPACE test is compatible with historical behaviour, */
987 /* but I don't think it is applicable to PIC. If so, please feel */
988 /* free to remove this test -- EEP */
989 if (IN_FARSPACE(oclass))
995 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
996 $2 is always the output file.
997 $3 -L path and -l libraries
998 $l is the list of extra options that should be there somewhere...
999 MUST be terminated with a NULL.
1001 const char *pic16_linkCmd[] =
1003 "gplink", "$l", "-o \"$2\"", "\"$1\"","$3", NULL
1008 /** $1 is always the basename.
1009 $2 is always the output file.
1010 $3 varies (nothing currently)
1011 $l is the list of extra options that should be there somewhere...
1012 MUST be terminated with a NULL.
1014 const char *pic16_asmCmd[] =
1016 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1025 "MCU PIC16", /* Target name */
1026 "p18f452", /* Processor */
1029 TRUE, /* Emit glue around main */
1030 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1034 pic16_asmCmd, /* assembler command and arguments */
1035 NULL, /* alternate macro based form */
1036 "-g", /* arguments for debug mode */
1037 NULL, /* arguments for normal mode */
1038 0, /* print externs as global */
1039 ".asm", /* assembler file extension */
1040 NULL /* no do_assemble function */
1043 NULL, // pic16_linkCmd, /* linker command and arguments */
1044 NULL, /* alternate macro based form */
1045 _pic16_linkEdit, //NULL, /* no do_link function */
1046 ".o", /* extension for object files */
1047 0 /* no need for linker file */
1059 3, /* fptr, far pointers (see Microchip) */
1066 "XSEG (XDATA)", // xstack
1067 "STACK (DATA)", // istack
1068 "CSEG (CODE)", // code
1069 "DSEG (DATA)", // data
1070 "ISEG (DATA)", // idata
1071 "PSEG (DATA)", // pdata
1072 "XSEG (XDATA)", // xdata
1073 "BSEG (BIT)", // bit
1074 "RSEG (DATA)", // reg
1075 "GSINIT (CODE)", // static
1076 "OSEG (OVR,DATA)", // overlay
1077 "GSFINAL (CODE)", // post static
1078 "HOME (CODE)", // home
1081 NULL, // default location for auto vars
1082 NULL, // default location for global vars
1083 1 // code is read only 1=yes
1086 NULL, /* genExtraAreaDeclaration */
1087 NULL /* genExatrAreaLinkOptions */
1090 /* stack related information */
1091 -1, /* -1 stack grows downwards, +1 upwards */
1092 1, /* extra overhead when calling between banks */
1093 4, /* extra overhead when the function is an ISR */
1094 1, /* extra overhead for a function call */
1095 1, /* re-entrant space */
1096 0 /* 'banked' call overhead, mild overlap with bank_overhead */
1098 /* pic16 has an 8 bit mul */
1103 pic16_emitDebuggerSymbol
1106 255/3, /* maxCount */
1107 3, /* sizeofElement */
1108 /* The rest of these costs are bogus. They approximate */
1109 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
1110 {4,4,4}, /* sizeofMatchJump[] */
1111 {0,0,0}, /* sizeofRangeCompare[] */
1112 0, /* sizeofSubtract */
1113 3, /* sizeofDispatch */
1117 _pic16_parseOptions,
1120 _pic16_finaliseOptions,
1121 _pic16_setDefaultOptions,
1122 pic16_assignRegisters,
1125 _pic16_genAssemblerPreamble,
1126 NULL, /* no genAssemblerEnd */
1128 NULL, // _pic16_genXINIT
1129 NULL, /* genInitStartup */
1130 _pic16_reset_regparm,
1132 _process_pragma, /* process a pragma */
1133 _pic16_mangleFunctionName, /* mangles function name */
1135 hasExtBitOp, /* hasExtBitOp */
1136 oclsExpense, /* oclsExpense */
1138 TRUE, /* little endian */
1141 1, /* transform <= to ! > */
1142 1, /* transform >= to ! < */
1143 1, /* transform != to !(a == b) */
1145 FALSE, /* No array initializer support. */
1146 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
1147 NULL, /* no builtin functions */
1148 GPOINTER, /* treat unqualified pointers as "generic" pointers */
1149 1, /* reset labelKey to 1 */
1150 1, /* globals & local static allowed */