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;
84 extern char *pic16_processor_base_name(void);
86 void pic16_pCodeInitRegisters(void);
88 void pic16_assignRegisters (ebbIndex *);
90 static int regParmFlg = 0; /* determine if we can register a parameter */
92 pic16_options_t pic16_options;
94 extern set *includeDirsSet;
95 extern set *dataDirsSet;
96 extern set *libFilesSet;
98 /* Also defined in gen.h, but the #include is commented out */
99 /* for an unknowned reason. - EEP */
100 void pic16_emitDebuggerSymbol (char *);
102 extern void pic16_emitConfigRegs(FILE *of);
103 extern void pic16_emitIDRegs(FILE *of);
110 asm_addTree (&asm_asxxxx_mapping);
111 pic16_pCodeInitRegisters();
116 _pic16_reset_regparm (void)
122 _pic16_regparm (sym_link * l, bool reentrant)
124 /* force all parameters via SEND/RECEIVE */
125 if(0 /*pic16_options.ip_stack*/) {
126 /* for this processor it is simple
127 * can pass only the first parameter in a register */
128 if(regParmFlg)return 0;
130 return 1; //regParmFlg;
132 /* otherwise pass all arguments in registers via SEND/RECEIVE */
139 int initsfpnt=0; /* set to 1 if source provides a pragma for stack
140 * so glue() later emits code to initialize stack/frame pointers */
143 set *sectNames=NULL; /* list of section listed in pragma directives */
144 set *sectSyms=NULL; /* list of symbols set in a specific section */
145 set *wparamList=NULL;
148 /* This is an experimental code for #pragma inline
149 and is temporarily disabled for 2.5.0 release */
150 set *asmInlineMap=NULL;
155 unsigned want_libc: 1;
156 unsigned want_libm: 1;
157 unsigned want_libio: 1;
158 unsigned want_libdebug: 1;
159 } libflags = { 0, 0, 0, 0, 0 };
170 do_pragma(int id, const char *name, const char *cp)
172 struct pragma_token_s token;
176 init_pragma_token(&token);
180 /* #pragma stack [stack-position] [stack-len] */
183 unsigned int stackPos, stackLen;
187 cp = get_pragma_token(cp, &token);
188 if (TOKEN_INT != token.type)
193 stackPos = token.val.int_val;
195 cp = get_pragma_token(cp, &token);
196 if (TOKEN_INT != token.type)
201 stackLen = token.val.int_val;
203 cp = get_pragma_token(cp, &token);
204 if (TOKEN_EOL != token.type)
212 fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
213 filename, lineno, stackLen, stackLen);
216 /* check sanity of stack */
217 if ((stackPos >> 8) != ((stackPos + stackLen - 1) >> 8)) {
218 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
219 filename, lineno, stackPos, stackPos + stackLen - 1);
223 if (stackPos < pic16->acsSplitOfs) {
224 fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
225 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->acsSplitOfs);
228 if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
229 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
230 filename, lineno, stackPos, stackPos + stackLen - 1, 0xF00 + pic16->acsSplitOfs);
233 if (stackPos+stackLen > pic16->RAMsize) {
234 fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
235 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->RAMsize-1);
241 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
242 addSet(&pic16_fix_udata, reg);
244 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
245 addSet(&pic16_fix_udata, reg);
247 sym = newSymbol("stack", 0);
248 sprintf(sym->rname, "_%s", sym->name);
249 addSet(&publics, sym);
251 sym = newSymbol("stack_end", 0);
252 sprintf(sym->rname, "_%s", sym->name);
253 addSet(&publics, sym);
255 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
259 /* #pragma code [symbol] [location] */
264 cp = get_pragma_token(cp, &token);
265 if (TOKEN_STR != token.type)
268 absS = Safe_calloc(1, sizeof(absSym));
269 sprintf(absS->name, "_%s", get_pragma_string(&token));
271 cp = get_pragma_token(cp, &token);
272 if (TOKEN_INT != token.type)
275 //fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno);
279 absS->address = token.val.int_val;
281 cp = get_pragma_token(cp, &token);
282 if (TOKEN_EOL != token.type)
288 if ((absS->address % 2) != 0) {
290 fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
291 filename, lineno, absS->address);
294 addSet(&absSymSet, absS);
295 // fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
296 // __FILE__, __LINE__, symname, absS->address);
300 /* #pragma udata [section-name] [symbol] */
310 cp = get_pragma_token(cp, &token);
311 if (TOKEN_STR == token.type)
312 sectname = Safe_strdup(get_pragma_string(&token));
319 cp = get_pragma_token(cp, &token);
320 if (TOKEN_STR == token.type)
321 symname = get_pragma_string(&token);
324 //fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno);
331 ssym = Safe_calloc(1, sizeof(sectSym));
332 ssym->name = Safe_calloc(1, strlen(symname) + 2);
333 sprintf(ssym->name, "%s%s", port->fun_prefix, symname);
336 addSet(§Syms, ssym);
338 nsym = newSymbol((char *)symname, 0);
339 strcpy(nsym->rname, ssym->name);
342 checkAddSym(&publics, nsym);
346 for (snam = setFirstItem(sectNames);snam;snam=setNextItem(sectNames))
348 if (!strcmp(sectname, snam->name))
357 snam = Safe_calloc(1, sizeof(sectName));
358 snam->name = Safe_strdup(sectname);
359 snam->regsSet = NULL;
361 addSet(§Names, snam);
364 ssym->section = snam;
367 fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
368 ssym->name, snam->name, snam);
371 cp = get_pragma_token(cp, &token);
372 if (TOKEN_STR == token.type)
373 symname = get_pragma_string(&token);
374 else if (TOKEN_EOL == token.type)
387 /* #pragma library library_module */
392 cp = get_pragma_token(cp, &token);
393 if (TOKEN_EOL != token.type)
395 lmodule = get_pragma_string(&token);
398 * c link the C library
399 * math link the math library
400 * io link the IO library
401 * debug link the debug libary
402 * anything else, will link as-is */
404 if(!strcmp(lmodule, "c"))
405 libflags.want_libc = 1;
406 else if(!strcmp(lmodule, "math"))
407 libflags.want_libm = 1;
408 else if(!strcmp(lmodule, "io"))
409 libflags.want_libio = 1;
410 else if(!strcmp(lmodule, "debug"))
411 libflags.want_libdebug = 1;
412 else if(!strcmp(lmodule, "ignore"))
418 fprintf(stderr, "link library %s\n", lmodule);
419 addSetHead(&libFilesSet, (char *)lmodule);
429 cp = get_pragma_token(cp, &token);
430 if (TOKEN_EOL != token.type)
439 /* This is an experimental code for #pragma inline
440 and is temporarily disabled for 2.5.0 release */
443 char *tmp = strtok((char *)NULL, WHITECOMMA);
446 addSet(&asmInlineMap, Safe_strdup( tmp ));
447 tmp = strtok((char *)NULL, WHITECOMMA);
453 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
454 debugf("inline asm: `%s'\n", s);
466 get_pragma_token(cp, &token);
469 werror(W_BAD_PRAGMA_ARGUMENTS, name);
471 free_pragma_token(&token);
475 static struct pragma_s pragma_tbl[] = {
476 { "stack", P_STACK, 0, do_pragma },
477 { "code", P_CODE, 0, do_pragma },
478 { "udata", P_UDATA, 0, do_pragma },
479 { "library", P_LIBRARY, 0, do_pragma },
480 /*{ "inline", P_INLINE, 0, do_pragma }, */
481 { NULL, 0, 0, NULL },
485 _process_pragma(const char *s)
487 return process_pragma_tbl(pragma_tbl, s);
490 #define REP_UDATA "--preplace-udata-with="
492 #define STACK_MODEL "--pstack-model="
493 #define OPT_BANKSEL "--obanksel="
495 #define ALT_ASM "--asm="
496 #define ALT_LINK "--link="
498 #define IVT_LOC "--ivt-loc="
499 #define NO_DEFLIBS "--nodefaultlibs"
500 #define MPLAB_COMPAT "--mplab-comp"
502 #define USE_CRT "--use-crt="
504 #define OFMSG_LRSUPPORT "--flr-support"
506 #define NO_OPTIMIZE_GOTO "--no-optimize-goto"
507 #define OPTIMIZE_CMP "--optimize-cmp"
508 #define OPTIMIZE_DF "--optimize-df"
510 char *alt_asm = NULL;
511 char *alt_link = NULL;
513 int pic16_mplab_comp = 0;
514 extern int pic16_debug_verbose;
515 extern int pic16_ralloc_debug;
516 extern int pic16_pcode_verbose;
518 int pic16_enable_peeps = 0;
520 OPTION pic16_optionsTable[]= {
521 /* code generation options */
522 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
524 { 'y', "--extended", &pic16_options.xinst, "enable Extended Instruction Set/Literal Offset Addressing mode"},
526 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
528 /* optimization options */
529 { 0, OPT_BANKSEL, &pic16_options.opt_banksel, "set banksel optimization level (default=0 no)", CLAT_INTEGER },
530 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
531 { 0, NO_OPTIMIZE_GOTO, NULL, "do NOT use (conditional) BRA instead of GOTO"},
532 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
533 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
535 /* assembling options */
536 { 0, ALT_ASM, &alt_asm, "Use alternative assembler", CLAT_STRING},
537 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
539 /* linking options */
540 { 0, ALT_LINK, &alt_link, "Use alternative linker", CLAT_STRING },
541 { 0, REP_UDATA, &pic16_sectioninfo.at_udata, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr", CLAT_STRING },
542 { 0, IVT_LOC, NULL, "Set address of interrupt vector table."},
543 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
544 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
545 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
547 /* debugging options */
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"},
551 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
552 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
553 { 0, NULL, NULL, NULL}
557 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
560 _pic16_parseOptions (int *pargc, char **argv, int *i)
565 /* TODO: allow port-specific command line options to specify
566 * segment names here.
569 /* check for arguments that have associated an integer variable */
570 while(pic16_optionsTable[j].pparameter) {
571 if(ISOPT( pic16_optionsTable[j].longOpt )) {
572 (*(int *)pic16_optionsTable[j].pparameter)++;
578 if(ISOPT(STACK_MODEL)) {
579 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
580 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
581 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
583 fprintf(stderr, "Unknown stack model: %s", stkmodel);
590 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
591 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
596 pic16_options.no_crt = 0;
597 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
603 if(ISOPT(OFMSG_LRSUPPORT)) {
604 pic16_options.opt_flags |= OF_LR_SUPPORT;
609 if (ISOPT(NO_OPTIMIZE_GOTO)) {
610 pic16_options.opt_flags |= OF_NO_OPTIMIZE_GOTO;
614 if(ISOPT(OPTIMIZE_CMP)) {
615 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
619 if (ISOPT(OPTIMIZE_DF)) {
620 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
628 extern void pic16_init_pic(const char *name);
630 static void _pic16_initPaths(void)
632 set *pic16libDirsSet=NULL;
634 if (!options.nostdlib) {
635 struct dbuf_s pic16libDir;
637 dbuf_init(&pic16libDir, 128);
638 dbuf_makePath(&pic16libDir, LIB_DIR_SUFFIX, "pic16");
639 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, dbuf_c_str(&pic16libDir));
640 dbuf_destroy(&pic16libDir);
641 mergeSets(&pic16libDirsSet, libDirsSet);
642 libDirsSet = pic16libDirsSet;
645 /* now that we have the paths set up... */
646 pic16_init_pic(port->processor);
649 extern set *linkOptionsSet;
650 char *msprintf(hTab *pvals, const char *pformat, ...);
651 int my_system(const char *cmd);
653 /* forward declarations */
654 extern const char *pic16_linkCmd[];
655 extern const char *pic16_asmCmd[];
656 extern set *asmOptionsSet;
658 /* custom function to link objects */
659 static void _pic16_linkEdit(void)
661 hTab *linkValues=NULL;
669 * link command format:
670 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
673 sprintf(lfrm, "{linker} {incdirs} {lflags} -w -r -o {outfile} {user_ofile} {ofiles} {spec_ofiles} {libs}");
675 shash_add(&linkValues, "linker", pic16_linkCmd[0]);
677 mergeSets(&tSet, libDirsSet);
678 mergeSets(&tSet, libPathsSet);
680 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
681 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
683 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
685 if(fullSrcFileName) {
686 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName);
687 // addSetHead(&relFilesSet, Safe_strdup(temp));
688 shash_add(&linkValues, "user_ofile", temp);
691 if(!pic16_options.no_crt)
692 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
694 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
696 if(!libflags.ignore) {
697 if(libflags.want_libc)
698 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
700 if(libflags.want_libm)
701 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
703 if(libflags.want_libio) {
704 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
705 addSet(&libFilesSet, Safe_strdup(temp));
708 if(libflags.want_libdebug)
709 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
712 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
714 lcmd = msprintf(linkValues, lfrm);
716 ret = my_system( lcmd );
726 _pic16_finaliseOptions (void)
728 port->mem.default_local_map = data;
729 port->mem.default_globl_map = data;
731 /* peepholes are disabled for the time being */
734 /* explicit enable peepholes for testing */
735 if (pic16_enable_peeps)
738 options.all_callee_saves = 1; // always callee saves
741 options.float_rent = 1;
742 options.intlong_rent = 1;
745 setMainValue("mcu", pic16->name[2] );
746 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
748 setMainValue("mcu1", pic16->name[1] );
749 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
751 if (!pic16_options.nodefaultlibs)
755 /* now add the library for the device */
756 sprintf(devlib, "libdev%s.lib", pic16->name[1]); /* e.g., libdev18f452.lib */
757 addSet(&libFilesSet, Safe_strdup(devlib));
759 /* add the internal SDCC library */
760 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
763 if (alt_asm && alt_asm[0] != '\0')
765 size_t len = strlen(alt_asm);
766 char *cmd = malloc(len + 3);
769 memcpy(&cmd[1], alt_asm, len);
772 pic16_linkCmd[0] = cmd;
775 if (alt_link && alt_link[0] != '\0')
777 size_t len = strlen(alt_asm);
778 char *cmd = malloc(len + 3);
781 memcpy(&cmd[1], alt_link, len);
784 pic16_linkCmd[0] = cmd;
787 if (!pic16_options.no_crt)
789 pic16_options.omit_ivt = 1;
790 pic16_options.leave_reset = 0;
793 if (options.model == MODEL_SMALL)
794 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
795 else if(options.model == MODEL_LARGE)
799 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
801 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
802 *(strrchr(buf, 'f')) = 'F';
803 addSet(&asmOptionsSet, Safe_strdup(buf));
806 if (STACK_MODEL_LARGE)
808 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
809 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
813 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
814 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
820 _pic16_setDefaultOptions (void)
822 options.stackAuto = 0; /* implicit declaration */
823 /* port is not capable yet to allocate separate registers
824 * dedicated for passing certain parameters */
826 /* initialize to defaults section locations, names and addresses */
827 pic16_sectioninfo.at_udata = "udata";
829 /* set pic16 port options to defaults */
830 pic16_options.no_banksel = 0;
831 pic16_options.opt_banksel = 0;
832 pic16_options.omit_configw = 0;
833 pic16_options.omit_ivt = 0;
834 pic16_options.leave_reset = 0;
835 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
836 pic16_options.ivt_loc = 0x000000;
837 pic16_options.nodefaultlibs = 0;
838 pic16_options.dumpcalltree = 0;
839 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
840 pic16_options.no_crt = 0; /* use crt by default */
841 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
842 pic16_options.gstack = 0;
843 pic16_options.debgen = 0;
847 _pic16_getRegName (struct regs *reg)
856 static char *_pic16_mangleFunctionName(char *sz)
858 // fprintf(stderr, "mangled function name: %s\n", sz);
866 _pic16_genAssemblerPreamble (FILE * of)
868 char *name = pic16_processor_base_name();
872 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
875 fprintf (of, "\tlist\tp=%s\n",&name[1]);
876 if (pic16_mplab_comp) {
877 // provide ACCESS macro used during SFR accesses
878 fprintf (of, "\tinclude <p%s.inc>\n", &name[1]);
881 if(!pic16_options.omit_configw) {
882 pic16_emitConfigRegs(of);
884 pic16_emitIDRegs(of);
887 fprintf (of, "\tradix dec\n");
890 /* Generate interrupt vector table. */
892 _pic16_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts)
895 /* PIC18F family has only two interrupts, the high and the low
896 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
898 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
899 dbuf_printf(oBuf, "; RESET vector\n");
900 dbuf_printf(oBuf, "\tgoto\t__sdcc_gsinit_startup\n");
903 if(!pic16_options.omit_ivt) {
904 dbuf_printf(oBuf, "\tres 4\n");
907 dbuf_printf(oBuf, "; High priority interrupt vector 0x0008\n");
909 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[1]->rname);
910 dbuf_printf(oBuf, "\tres\t12\n");
912 dbuf_printf(oBuf, "\tretfie\n");
913 dbuf_printf(oBuf, "\tres\t14\n");
916 dbuf_printf(oBuf, "; Low priority interrupt vector 0x0018\n");
918 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[2]->rname);
920 dbuf_printf(oBuf, "\tretfie\n");
927 /* return True if the port can handle the type,
928 * False to convert it to function call */
929 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
931 //fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
932 int symL, symR, symRes, sizeL = 0, sizeR = 0, sizeRes = 0;
934 /* left/right are symbols? */
935 symL = IS_SYMOP(IC_LEFT(ic));
936 symR = IS_SYMOP(IC_RIGHT(ic));
937 symRes = IS_SYMOP(IC_RESULT(ic));
939 /* --> then determine their sizes */
940 sizeL = symL ? getSize(OP_SYM_TYPE(IC_LEFT(ic))) : 4;
941 sizeR = symR ? getSize(OP_SYM_TYPE(IC_RIGHT(ic))) : 4;
942 sizeRes = symRes ? getSize(OP_SYM_TYPE(IC_RESULT(ic))) : 4;
944 /* Checks to enable native multiplication.
945 * PICs do not offer native division at all...
948 * ( i) if result is just one byte, use native MUL
949 * (regardless of the operands)
950 * ( ii) if left and right are unsigned 8-bit operands,
952 * (iii) if left or right is a literal in the range of [-128..256)
953 * and the other is an unsigned byte, use native MUL
957 /* use native mult for `*: <?> x <?> --> {u8_t, s8_t}' */
958 if (sizeRes == 1) { return TRUE; }
960 /* use native mult for `u8_t x u8_t --> { u16_t, s16_t }' */
961 if (sizeL == 1 && symL /*&& SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic)))*/) {
964 //printf( "%s: left too large (%u) / signed (%u)\n", __FUNCTION__, sizeL, symL && !SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic))));
967 if (sizeR == 1 && symR /*&& SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic)))*/) {
970 //printf( "%s: right too large (%u) / signed (%u)\n", __FUNCTION__, sizeR, symR && !SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic))));
974 /* also allow literals [-128..256) for left/right operands */
975 if (IS_VALOP(IC_LEFT(ic)))
977 long l = (long) ulFromVal ( OP_VALUE( IC_LEFT(ic) ) );
979 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
980 if (l >= -128 && l < 256)
984 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
987 if (IS_VALOP( IC_RIGHT(ic) ))
989 long l = (long) ulFromVal ( OP_VALUE( IC_RIGHT(ic) ) );
991 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
992 if (l >= -128 && l < 256)
996 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1000 /* use native mult iff left and right are (unsigned) 8-bit operands */
1001 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1004 if (ic->op == '/' || ic->op == '%')
1006 /* We must catch /: {u8_t,s8_t} x {u8_t,s8_t} --> {u8_t,s8_t},
1007 * because SDCC will call 'divuchar' even for u8_t / s8_t.
1008 * Example: 128 / -2 becomes 128 / 254 = 0 != -64... */
1009 if (sizeL == 1 && sizeR == 1) return TRUE;
1011 /* What about literals? */
1012 if (IS_VALOP( IC_LEFT(ic) ))
1014 long l = (long) ulFromVal ( OP_VALUE( IC_LEFT(ic) ) );
1016 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
1017 if (l >= -128 && l < 256)
1021 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1024 if (IS_VALOP( IC_RIGHT(ic) ))
1026 long l = (long) ulFromVal ( OP_VALUE( IC_RIGHT(ic) ) );
1028 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1029 if (l >= -128 && l < 256)
1033 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1036 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1044 /* Do CSE estimation */
1045 static bool cseCostEstimation (iCode *ic, iCode *pdic)
1047 // operand *result = IC_RESULT(ic);
1048 // sym_link *result_type = operandType(result);
1051 /* VR -- this is an adhoc. Put here after conversation
1052 * with Erik Epetrich */
1056 || ic->op == EQ_OP) {
1058 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
1063 /* if it is a pointer then return ok for now */
1064 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
1066 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
1067 so we will cse only if they are local (i.e. both ic & pdic belong to
1068 the same basic block */
1069 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
1070 /* then if they are the same Basic block then ok */
1071 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
1076 /* for others it is cheaper to do the cse */
1082 /* Indicate which extended bit operations this port supports */
1084 hasExtBitOp (int op, int size)
1088 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
1095 /* Indicate the expense of an access to an output storage class */
1097 oclsExpense (struct memmap *oclass)
1099 /* The IN_FARSPACE test is compatible with historical behaviour, */
1100 /* but I don't think it is applicable to PIC. If so, please feel */
1101 /* free to remove this test -- EEP */
1102 if (IN_FARSPACE(oclass))
1108 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
1109 $2 is always the output file.
1110 $3 -L path and -l libraries
1111 $l is the list of extra options that should be there somewhere...
1112 MUST be terminated with a NULL.
1114 const char *pic16_linkCmd[] =
1116 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"","$3", NULL
1121 /** $1 is always the basename.
1122 $2 is always the output file.
1123 $3 varies (nothing currently)
1124 $l is the list of extra options that should be there somewhere...
1125 MUST be terminated with a NULL.
1127 const char *pic16_asmCmd[] =
1129 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1138 "MCU PIC16", /* Target name */
1139 "p18f452", /* Processor */
1142 TRUE, /* Emit glue around main */
1143 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1147 pic16_asmCmd, /* assembler command and arguments */
1148 NULL, /* alternate macro based form */
1149 "-g", /* arguments for debug mode */
1150 NULL, /* arguments for normal mode */
1151 0, /* print externs as global */
1152 ".asm", /* assembler file extension */
1153 NULL /* no do_assemble function */
1156 NULL, // pic16_linkCmd, /* linker command and arguments */
1157 NULL, /* alternate macro based form */
1158 _pic16_linkEdit, //NULL, /* no do_link function */
1159 ".o", /* extension for object files */
1160 0 /* no need for linker file */
1172 3, /* fptr, far pointers (see Microchip) */
1179 /* generic pointer tags */
1188 "XSEG (XDATA)", // xstack
1189 "STACK (DATA)", // istack
1190 "CSEG (CODE)", // code
1191 "DSEG (DATA)", // data
1192 "ISEG (DATA)", // idata
1193 "PSEG (DATA)", // pdata
1194 "XSEG (XDATA)", // xdata
1195 "BSEG (BIT)", // bit
1196 "RSEG (DATA)", // reg
1197 "GSINIT (CODE)", // static
1198 "OSEG (OVR,DATA)", // overlay
1199 "GSFINAL (CODE)", // post static
1200 "HOME (CODE)", // home
1203 "CONST (CODE)", // const_name - const data (code or not)
1204 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
1205 "XABS (ABS,XDATA)", // xabs_name - absolute xdata
1206 "IABS (ABS,DATA)", // iabs_name - absolute data
1207 NULL, // default location for auto vars
1208 NULL, // default location for global vars
1209 1 // code is read only 1=yes
1212 NULL, /* genExtraAreaDeclaration */
1213 NULL /* genExatrAreaLinkOptions */
1216 /* stack related information */
1217 -1, /* -1 stack grows downwards, +1 upwards */
1218 1, /* extra overhead when calling between banks */
1219 4, /* extra overhead when the function is an ISR */
1220 1, /* extra overhead for a function call */
1221 1, /* re-entrant space */
1222 0 /* 'banked' call overhead, mild overlap with bank_overhead */
1224 /* pic16 has an 8 bit mul */
1229 pic16_emitDebuggerSymbol
1232 255/3, /* maxCount */
1233 3, /* sizeofElement */
1234 /* The rest of these costs are bogus. They approximate */
1235 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
1236 {4,4,4}, /* sizeofMatchJump[] */
1237 {0,0,0}, /* sizeofRangeCompare[] */
1238 0, /* sizeofSubtract */
1239 3, /* sizeofDispatch */
1243 _pic16_parseOptions,
1246 _pic16_finaliseOptions,
1247 _pic16_setDefaultOptions,
1248 pic16_assignRegisters,
1251 _pic16_genAssemblerPreamble,
1252 NULL, /* no genAssemblerEnd */
1254 NULL, // _pic16_genXINIT
1255 NULL, /* genInitStartup */
1256 _pic16_reset_regparm,
1258 _process_pragma, /* process a pragma */
1259 _pic16_mangleFunctionName, /* mangles function name */
1261 hasExtBitOp, /* hasExtBitOp */
1262 oclsExpense, /* oclsExpense */
1264 TRUE, /* little endian */
1267 1, /* transform <= to ! > */
1268 1, /* transform >= to ! < */
1269 1, /* transform != to !(a == b) */
1271 FALSE, /* No array initializer support. */
1272 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
1273 NULL, /* no builtin functions */
1274 GPOINTER, /* treat unqualified pointers as "generic" pointers */
1275 1, /* reset labelKey to 1 */
1276 1, /* globals & local static allowed */