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 };
173 do_pragma(int id, const char *name, const char *cp)
175 struct pragma_token_s token;
179 init_pragma_token(&token);
183 /* #pragma stack [stack-position] [stack-len] */
186 unsigned int stackPos, stackLen;
190 cp = get_pragma_token(cp, &token);
191 if (TOKEN_INT != token.type)
196 stackPos = token.val.int_val;
198 cp = get_pragma_token(cp, &token);
199 if (TOKEN_INT != token.type)
204 stackLen = token.val.int_val;
206 cp = get_pragma_token(cp, &token);
207 if (TOKEN_EOL != token.type)
215 fprintf(stderr, "%s:%d: warning: setting stack to default size %d (0x%04x)\n",
216 filename, lineno, stackLen, stackLen);
219 /* check sanity of stack */
220 if ((stackPos >> 8) != ((stackPos + stackLen - 1) >> 8)) {
221 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
222 filename, lineno, stackPos, stackPos + stackLen - 1);
226 if (stackPos < pic16->acsSplitOfs) {
227 fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
228 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->acsSplitOfs);
231 if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
232 fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
233 filename, lineno, stackPos, stackPos + stackLen - 1, 0xF00 + pic16->acsSplitOfs);
236 if (stackPos+stackLen > pic16->RAMsize) {
237 fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
238 filename, lineno, stackPos, stackPos + stackLen - 1, pic16->RAMsize-1);
244 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
245 addSet(&pic16_fix_udata, reg);
247 reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
248 addSet(&pic16_fix_udata, reg);
250 sym = newSymbol("stack", 0);
251 sprintf(sym->rname, "_%s", sym->name);
252 addSet(&publics, sym);
254 sym = newSymbol("stack_end", 0);
255 sprintf(sym->rname, "_%s", sym->name);
256 addSet(&publics, sym);
258 initsfpnt = 1; // force glue() to initialize stack/frame pointers */
262 /* #pragma code [symbol] [location] */
267 cp = get_pragma_token(cp, &token);
268 if (TOKEN_STR != token.type)
271 absS = Safe_calloc(1, sizeof(absSym));
272 sprintf(absS->name, "_%s", get_pragma_string(&token));
274 cp = get_pragma_token(cp, &token);
275 if (TOKEN_INT != token.type)
278 //fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno);
282 absS->address = token.val.int_val;
284 cp = get_pragma_token(cp, &token);
285 if (TOKEN_EOL != token.type)
291 if ((absS->address % 2) != 0) {
293 fprintf(stderr, "%s:%d: warning: code memory locations should be word aligned, will locate to 0x%06x instead\n",
294 filename, lineno, absS->address);
297 addSet(&absSymSet, absS);
298 // fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
299 // __FILE__, __LINE__, symname, absS->address);
303 /* #pragma udata [section-name] [symbol] */
313 cp = get_pragma_token(cp, &token);
314 if (TOKEN_STR == token.type)
315 sectname = Safe_strdup(get_pragma_string(&token));
322 cp = get_pragma_token(cp, &token);
323 if (TOKEN_STR == token.type)
324 symname = get_pragma_string(&token);
327 //fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno);
334 ssym = Safe_calloc(1, sizeof(sectSym));
335 ssym->name = Safe_calloc(1, strlen(symname) + 2);
336 sprintf(ssym->name, "%s%s", port->fun_prefix, symname);
339 addSet(§Syms, ssym);
341 nsym = newSymbol((char *)symname, 0);
342 strcpy(nsym->rname, ssym->name);
345 checkAddSym(&publics, nsym);
349 for (snam = setFirstItem(sectNames);snam;snam=setNextItem(sectNames))
351 if (!strcmp(sectname, snam->name))
360 snam = Safe_calloc(1, sizeof(sectName));
361 snam->name = Safe_strdup(sectname);
362 snam->regsSet = NULL;
364 addSet(§Names, snam);
367 ssym->section = snam;
370 fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
371 ssym->name, snam->name, snam);
374 cp = get_pragma_token(cp, &token);
375 if (TOKEN_STR == token.type)
376 symname = get_pragma_string(&token);
377 else if (TOKEN_EOL == token.type)
390 /* #pragma library library_module */
395 cp = get_pragma_token(cp, &token);
396 if (TOKEN_EOL != token.type)
398 lmodule = get_pragma_string(&token);
401 * c link the C library
402 * math link the math library
403 * io link the IO library
404 * debug link the debug libary
405 * anything else, will link as-is */
407 if(!strcmp(lmodule, "c"))
408 libflags.want_libc = 1;
409 else if(!strcmp(lmodule, "math"))
410 libflags.want_libm = 1;
411 else if(!strcmp(lmodule, "io"))
412 libflags.want_libio = 1;
413 else if(!strcmp(lmodule, "debug"))
414 libflags.want_libdebug = 1;
415 else if(!strcmp(lmodule, "ignore"))
421 fprintf(stderr, "link library %s\n", lmodule);
422 addSetHead(&libFilesSet, (char *)lmodule);
432 cp = get_pragma_token(cp, &token);
433 if (TOKEN_EOL != token.type)
442 /* This is an experimental code for #pragma inline
443 and is temporarily disabled for 2.5.0 release */
446 char *tmp = strtok((char *)NULL, WHITECOMMA);
449 addSet(&asmInlineMap, Safe_strdup( tmp ));
450 tmp = strtok((char *)NULL, WHITECOMMA);
456 for(s = setFirstItem(asmInlineMap); s ; s = setNextItem(asmInlineMap)) {
457 debugf("inline asm: `%s'\n", s);
469 get_pragma_token(cp, &token);
472 werror(W_BAD_PRAGMA_ARGUMENTS, name);
474 free_pragma_token(&token);
478 static struct pragma_s pragma_tbl[] = {
479 { "stack", P_STACK, 0, do_pragma },
480 { "code", P_CODE, 0, do_pragma },
481 { "udata", P_UDATA, 0, do_pragma },
482 { "library", P_LIBRARY, 0, do_pragma },
483 /*{ "inline", P_INLINE, 0, do_pragma }, */
484 { NULL, 0, 0, NULL },
488 _process_pragma(const char *s)
490 return process_pragma_tbl(pragma_tbl, s);
493 #define REP_UDATA "--preplace-udata-with="
495 #define STACK_MODEL "--pstack-model="
496 #define OPT_BANKSEL "--obanksel="
498 #define ALT_ASM "--asm="
499 #define ALT_LINK "--link="
501 #define IVT_LOC "--ivt-loc="
502 #define NO_DEFLIBS "--nodefaultlibs"
503 #define MPLAB_COMPAT "--mplab-comp"
505 #define USE_CRT "--use-crt="
507 #define OFMSG_LRSUPPORT "--flr-support"
509 #define OPTIMIZE_GOTO "--optimize-goto"
510 #define OPTIMIZE_CMP "--optimize-cmp"
511 #define OPTIMIZE_DF "--optimize-df"
516 int pic16_mplab_comp=0;
517 extern int pic16_debug_verbose;
518 extern int pic16_ralloc_debug;
519 extern int pic16_pcode_verbose;
521 int pic16_enable_peeps=0;
523 OPTION pic16_optionsTable[]= {
524 /* code generation options */
525 { 0, STACK_MODEL, NULL, "use stack model 'small' (default) or 'large'"},
527 { 'y', "--extended", &xinst, "enable Extended Instruction Set/Literal Offset Addressing mode"},
529 { 0, "--pno-banksel", &pic16_options.no_banksel, "do not generate BANKSEL assembler directives"},
531 /* optimization options */
532 { 0, OPT_BANKSEL, &pic16_options.opt_banksel, "set banksel optimization level (default=0 no)", CLAT_INTEGER },
533 { 0, "--denable-peeps", &pic16_enable_peeps, "explicit enable of peepholes"},
534 { 0, OPTIMIZE_GOTO, NULL, "try to use (conditional) BRA instead of GOTO"},
535 { 0, OPTIMIZE_CMP, NULL, "try to optimize some compares"},
536 { 0, OPTIMIZE_DF, NULL, "thoroughly analyze data flow (memory and time intensive!)"},
538 /* assembling options */
539 { 0, ALT_ASM, &alt_asm, "Use alternative assembler", CLAT_STRING},
540 { 0, MPLAB_COMPAT, &pic16_mplab_comp, "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
542 /* linking options */
543 { 0, ALT_LINK, &alt_link, "Use alternative linker", CLAT_STRING },
544 { 0, REP_UDATA, &pic16_sectioninfo.at_udata, "Place udata variables at another section: udata_acs, udata_ovr, udata_shr", CLAT_STRING },
545 { 0, IVT_LOC, NULL, "Set address of interrupt vector table."},
546 { 0, NO_DEFLIBS, &pic16_options.nodefaultlibs, "do not link default libraries when linking"},
547 { 0, USE_CRT, NULL, "use <crt-o> run-time initialization module"},
548 { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"},
550 /* debugging options */
551 { 0, "--debug-xtra", &pic16_debug_verbose, "show more debug info in assembly output"},
552 { 0, "--debug-ralloc", &pic16_ralloc_debug, "dump register allocator debug file *.d"},
553 { 0, "--pcode-verbose", &pic16_pcode_verbose, "dump pcode related info"},
554 { 0, "--calltree", &pic16_options.dumpcalltree, "dump call tree in .calltree file"},
555 { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"},
556 { 0, NULL, NULL, NULL}
560 #define ISOPT(str) !strncmp(argv[ *i ], str, strlen(str) )
563 _pic16_parseOptions (int *pargc, char **argv, int *i)
568 /* TODO: allow port-specific command line options to specify
569 * segment names here.
572 /* check for arguments that have associated an integer variable */
573 while(pic16_optionsTable[j].pparameter) {
574 if(ISOPT( pic16_optionsTable[j].longOpt )) {
575 (*(int *)pic16_optionsTable[j].pparameter)++;
581 if(ISOPT(STACK_MODEL)) {
582 stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
583 if(!STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
584 else if(!STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
586 fprintf(stderr, "Unknown stack model: %s", stkmodel);
593 pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
594 fprintf(stderr, "%s:%d setting interrupt vector addresses 0x%x\n", __FILE__, __LINE__, pic16_options.ivt_loc);
599 pic16_options.no_crt = 0;
600 pic16_options.crt_name = Safe_strdup( getStringArg(USE_CRT, argv, i, *pargc) );
606 if(ISOPT(OFMSG_LRSUPPORT)) {
607 pic16_options.opt_flags |= OF_LR_SUPPORT;
612 if (ISOPT(OPTIMIZE_GOTO)) {
613 pic16_options.opt_flags |= OF_OPTIMIZE_GOTO;
617 if(ISOPT(OPTIMIZE_CMP)) {
618 pic16_options.opt_flags |= OF_OPTIMIZE_CMP;
622 if (ISOPT(OPTIMIZE_DF)) {
623 pic16_options.opt_flags |= OF_OPTIMIZE_DF;
631 extern void pic16_init_pic(const char *name);
633 static void _pic16_initPaths(void)
635 set *pic16libDirsSet=NULL;
637 if (!options.nostdlib) {
638 struct dbuf_s pic16libDir;
640 dbuf_init(&pic16libDir, 128);
641 dbuf_makePath(&pic16libDir, LIB_DIR_SUFFIX, "pic16");
642 pic16libDirsSet = appendStrSet(dataDirsSet, NULL, dbuf_c_str(&pic16libDir));
643 dbuf_destroy(&pic16libDir);
644 mergeSets(&pic16libDirsSet, libDirsSet);
645 libDirsSet = pic16libDirsSet;
648 /* now that we have the paths set up... */
649 pic16_init_pic(port->processor);
652 extern set *linkOptionsSet;
653 char *msprintf(hTab *pvals, const char *pformat, ...);
654 int my_system(const char *cmd);
656 /* forward declarations */
657 extern const char *pic16_linkCmd[];
658 extern const char *pic16_asmCmd[];
659 extern set *asmOptionsSet;
661 /* custom function to link objects */
662 static void _pic16_linkEdit(void)
664 hTab *linkValues=NULL;
672 * link command format:
673 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
676 sprintf(lfrm, "{linker} {incdirs} {lflags} -w -r -o {outfile} {user_ofile} {ofiles} {spec_ofiles} {libs}");
678 shash_add(&linkValues, "linker", pic16_linkCmd[0]);
680 mergeSets(&tSet, libDirsSet);
681 mergeSets(&tSet, libPathsSet);
683 shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
684 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
686 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
688 if(fullSrcFileName) {
689 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName);
690 // addSetHead(&relFilesSet, Safe_strdup(temp));
691 shash_add(&linkValues, "user_ofile", temp);
694 if(!pic16_options.no_crt)
695 shash_add(&linkValues, "spec_ofiles", pic16_options.crt_name);
697 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
699 if(!libflags.ignore) {
700 if(libflags.want_libc)
701 addSet(&libFilesSet, Safe_strdup("libc18f.lib"));
703 if(libflags.want_libm)
704 addSet(&libFilesSet, Safe_strdup("libm18f.lib"));
706 if(libflags.want_libio) {
707 sprintf(temp, "libio%s.lib", pic16->name[1]); /* build libio18f452.lib name */
708 addSet(&libFilesSet, Safe_strdup(temp));
711 if(libflags.want_libdebug)
712 addSet(&libFilesSet, Safe_strdup("libdebug.lib"));
715 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
717 lcmd = msprintf(linkValues, lfrm);
719 ret = my_system( lcmd );
729 _pic16_finaliseOptions (void)
733 port->mem.default_local_map = data;
734 port->mem.default_globl_map = data;
736 /* peepholes are disabled for the time being */
739 /* explicit enable peepholes for testing */
740 if(pic16_enable_peeps)
743 options.all_callee_saves = 1; // always callee saves
746 options.float_rent = 1;
747 options.intlong_rent = 1;
750 setMainValue("mcu", pic16->name[2] );
751 addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
753 setMainValue("mcu1", pic16->name[1] );
754 addSet(&preArgvSet, Safe_strdup("-D__{mcu1}"));
756 if(!pic16_options.nodefaultlibs) {
757 /* now add the library for the device */
758 sprintf(devlib, "%s.lib", pic16->name[2]);
759 addSet(&libFilesSet, Safe_strdup(devlib));
761 /* add the internal SDCC library */
762 addSet(&libFilesSet, Safe_strdup( "libsdcc.lib" ));
765 if(alt_asm && strlen(alt_asm))
766 pic16_asmCmd[0] = alt_asm;
768 if(alt_link && strlen(alt_link))
769 pic16_linkCmd[0] = alt_link;
771 if(!pic16_options.no_crt) {
772 pic16_options.omit_ivt = 1;
773 pic16_options.leave_reset = 0;
776 if(options.model == MODEL_SMALL)
777 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
779 if(options.model == MODEL_LARGE)
780 addSet(&asmOptionsSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
785 sprintf(buf, "-D%s -D__%s", pic16->name[2], pic16->name[1]);
786 *(strrchr(buf, 'f')) = 'F';
787 addSet(&asmOptionsSet, Safe_strdup( buf ));
790 if(STACK_MODEL_LARGE) {
791 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
792 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_LARGE"));
794 addSet(&preArgvSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
795 addSet(&asmOptionsSet, Safe_strdup("-DSTACK_MODEL_SMALL"));
801 _pic16_setDefaultOptions (void)
803 options.stackAuto = 0; /* implicit declaration */
804 /* port is not capable yet to allocate separate registers
805 * dedicated for passing certain parameters */
807 /* initialize to defaults section locations, names and addresses */
808 pic16_sectioninfo.at_udata = "udata";
810 /* set pic16 port options to defaults */
811 pic16_options.no_banksel = 0;
812 pic16_options.opt_banksel = 0;
813 pic16_options.omit_configw = 0;
814 pic16_options.omit_ivt = 0;
815 pic16_options.leave_reset = 0;
816 pic16_options.stack_model = 0; /* 0 for 'small', 1 for 'large' */
817 pic16_options.ivt_loc = 0x000000;
818 pic16_options.nodefaultlibs = 0;
819 pic16_options.dumpcalltree = 0;
820 pic16_options.crt_name = "crt0i.o"; /* the default crt to link */
821 pic16_options.no_crt = 0; /* use crt by default */
822 pic16_options.ip_stack = 1; /* set to 1 to enable ipop/ipush for stack */
823 pic16_options.gstack = 0;
824 pic16_options.debgen = 0;
828 _pic16_getRegName (struct regs *reg)
837 static char *_pic16_mangleFunctionName(char *sz)
839 // fprintf(stderr, "mangled function name: %s\n", sz);
847 _pic16_genAssemblerPreamble (FILE * of)
849 char *name = pic16_processor_base_name();
853 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
856 fprintf (of, "\tlist\tp=%s\n",&name[1]);
857 if (pic16_mplab_comp) {
858 // provide ACCESS macro used during SFR accesses
859 fprintf (of, "\tinclude <p%s.inc>\n", &name[1]);
862 if(!pic16_options.omit_configw) {
863 pic16_emitConfigRegs(of);
865 pic16_emitIDRegs(of);
868 fprintf (of, "\tradix dec\n");
871 /* Generate interrupt vector table. */
873 _pic16_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts)
876 /* PIC18F family has only two interrupts, the high and the low
877 * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
879 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
880 dbuf_printf(oBuf, "; RESET vector\n");
881 dbuf_printf(oBuf, "\tgoto\t__sdcc_gsinit_startup\n");
884 if(!pic16_options.omit_ivt) {
885 dbuf_printf(oBuf, "\tres 4\n");
888 dbuf_printf(oBuf, "; High priority interrupt vector 0x0008\n");
890 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[1]->rname);
891 dbuf_printf(oBuf, "\tres\t12\n");
893 dbuf_printf(oBuf, "\tretfie\n");
894 dbuf_printf(oBuf, "\tres\t14\n");
897 dbuf_printf(oBuf, "; Low priority interrupt vector 0x0018\n");
899 dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[2]->rname);
901 dbuf_printf(oBuf, "\tretfie\n");
908 /* return True if the port can handle the type,
909 * False to convert it to function call */
910 static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
912 //fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
913 int symL, symR, symRes, sizeL = 0, sizeR = 0, sizeRes = 0;
915 /* left/right are symbols? */
916 symL = IS_SYMOP(IC_LEFT(ic));
917 symR = IS_SYMOP(IC_RIGHT(ic));
918 symRes = IS_SYMOP(IC_RESULT(ic));
920 /* --> then determine their sizes */
921 sizeL = symL ? getSize(OP_SYM_TYPE(IC_LEFT(ic))) : 4;
922 sizeR = symR ? getSize(OP_SYM_TYPE(IC_RIGHT(ic))) : 4;
923 sizeRes = symRes ? getSize(OP_SYM_TYPE(IC_RESULT(ic))) : 4;
925 /* Checks to enable native multiplication.
926 * PICs do not offer native division at all...
929 * ( i) if result is just one byte, use native MUL
930 * (regardless of the operands)
931 * ( ii) if left and right are unsigned 8-bit operands,
933 * (iii) if left or right is a literal in the range of [-128..256)
934 * and the other is an unsigned byte, use native MUL
938 /* use native mult for `*: <?> x <?> --> {u8_t, s8_t}' */
939 if (sizeRes == 1) { return TRUE; }
941 /* use native mult for `u8_t x u8_t --> { u16_t, s16_t }' */
942 if (sizeL == 1 && symL /*&& SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic)))*/) {
945 //printf( "%s: left too large (%u) / signed (%u)\n", __FUNCTION__, sizeL, symL && !SPEC_USIGN(OP_SYM_TYPE(IC_LEFT(ic))));
948 if (sizeR == 1 && symR /*&& SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic)))*/) {
951 //printf( "%s: right too large (%u) / signed (%u)\n", __FUNCTION__, sizeR, symR && !SPEC_USIGN(OP_SYM_TYPE(IC_RIGHT(ic))));
955 /* also allow literals [-128..256) for left/right operands */
956 if (IS_VALOP(IC_LEFT(ic)))
958 long l = (long) ulFromVal ( OP_VALUE( IC_LEFT(ic) ) );
960 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
961 if (l >= -128 && l < 256)
965 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
968 if (IS_VALOP( IC_RIGHT(ic) ))
970 long l = (long) ulFromVal ( OP_VALUE( IC_RIGHT(ic) ) );
972 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
973 if (l >= -128 && l < 256)
977 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
981 /* use native mult iff left and right are (unsigned) 8-bit operands */
982 if (sizeL == 1 && sizeR == 1) { return TRUE; }
985 if (ic->op == '/' || ic->op == '%')
987 /* We must catch /: {u8_t,s8_t} x {u8_t,s8_t} --> {u8_t,s8_t},
988 * because SDCC will call 'divuchar' even for u8_t / s8_t.
989 * Example: 128 / -2 becomes 128 / 254 = 0 != -64... */
990 if (sizeL == 1 && sizeR == 1) return TRUE;
992 /* What about literals? */
993 if (IS_VALOP( IC_LEFT(ic) ))
995 long l = (long) ulFromVal ( OP_VALUE( IC_LEFT(ic) ) );
997 //printf( "%s: val(left) = %ld\n", __FUNCTION__, l );
998 if (l >= -128 && l < 256)
1002 //printf( "%s: left value %ld outside [-128..256)\n", __FUNCTION__, l );
1005 if (IS_VALOP( IC_RIGHT(ic) ))
1007 long l = (long) ulFromVal ( OP_VALUE( IC_RIGHT(ic) ) );
1009 //printf( "%s: val(right) = %ld\n", __FUNCTION__, l );
1010 if (l >= -128 && l < 256)
1014 //printf( "%s: right value %ld outside [-128..256)\n", __FUNCTION__, l );
1017 if (sizeL == 1 && sizeR == 1) { return TRUE; }
1025 /* Do CSE estimation */
1026 static bool cseCostEstimation (iCode *ic, iCode *pdic)
1028 // operand *result = IC_RESULT(ic);
1029 // sym_link *result_type = operandType(result);
1032 /* VR -- this is an adhoc. Put here after conversation
1033 * with Erik Epetrich */
1037 || ic->op == EQ_OP) {
1039 fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
1044 /* if it is a pointer then return ok for now */
1045 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
1047 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
1048 so we will cse only if they are local (i.e. both ic & pdic belong to
1049 the same basic block */
1050 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
1051 /* then if they are the same Basic block then ok */
1052 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
1057 /* for others it is cheaper to do the cse */
1063 /* Indicate which extended bit operations this port supports */
1065 hasExtBitOp (int op, int size)
1069 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
1076 /* Indicate the expense of an access to an output storage class */
1078 oclsExpense (struct memmap *oclass)
1080 /* The IN_FARSPACE test is compatible with historical behaviour, */
1081 /* but I don't think it is applicable to PIC. If so, please feel */
1082 /* free to remove this test -- EEP */
1083 if (IN_FARSPACE(oclass))
1089 /** $1 is the input object file (PIC16 specific) // >>always the basename<<.
1090 $2 is always the output file.
1091 $3 -L path and -l libraries
1092 $l is the list of extra options that should be there somewhere...
1093 MUST be terminated with a NULL.
1095 const char *pic16_linkCmd[] =
1097 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"","$3", NULL
1102 /** $1 is always the basename.
1103 $2 is always the output file.
1104 $3 varies (nothing currently)
1105 $l is the list of extra options that should be there somewhere...
1106 MUST be terminated with a NULL.
1108 const char *pic16_asmCmd[] =
1110 "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
1119 "MCU PIC16", /* Target name */
1120 "p18f452", /* Processor */
1123 TRUE, /* Emit glue around main */
1124 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
1128 pic16_asmCmd, /* assembler command and arguments */
1129 NULL, /* alternate macro based form */
1130 "-g", /* arguments for debug mode */
1131 NULL, /* arguments for normal mode */
1132 0, /* print externs as global */
1133 ".asm", /* assembler file extension */
1134 NULL /* no do_assemble function */
1137 NULL, // pic16_linkCmd, /* linker command and arguments */
1138 NULL, /* alternate macro based form */
1139 _pic16_linkEdit, //NULL, /* no do_link function */
1140 ".o", /* extension for object files */
1141 0 /* no need for linker file */
1153 3, /* fptr, far pointers (see Microchip) */
1160 /* generic pointer tags */
1169 "XSEG (XDATA)", // xstack
1170 "STACK (DATA)", // istack
1171 "CSEG (CODE)", // code
1172 "DSEG (DATA)", // data
1173 "ISEG (DATA)", // idata
1174 "PSEG (DATA)", // pdata
1175 "XSEG (XDATA)", // xdata
1176 "BSEG (BIT)", // bit
1177 "RSEG (DATA)", // reg
1178 "GSINIT (CODE)", // static
1179 "OSEG (OVR,DATA)", // overlay
1180 "GSFINAL (CODE)", // post static
1181 "HOME (CODE)", // home
1184 "CONST (CODE)", // const_name - const data (code or not)
1185 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
1186 "XABS (ABS,XDATA)", // xabs_name - absolute xdata
1187 "IABS (ABS,DATA)", // iabs_name - absolute data
1188 NULL, // default location for auto vars
1189 NULL, // default location for global vars
1190 1 // code is read only 1=yes
1193 NULL, /* genExtraAreaDeclaration */
1194 NULL /* genExatrAreaLinkOptions */
1197 /* stack related information */
1198 -1, /* -1 stack grows downwards, +1 upwards */
1199 1, /* extra overhead when calling between banks */
1200 4, /* extra overhead when the function is an ISR */
1201 1, /* extra overhead for a function call */
1202 1, /* re-entrant space */
1203 0 /* 'banked' call overhead, mild overlap with bank_overhead */
1205 /* pic16 has an 8 bit mul */
1210 pic16_emitDebuggerSymbol
1213 255/3, /* maxCount */
1214 3, /* sizeofElement */
1215 /* The rest of these costs are bogus. They approximate */
1216 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
1217 {4,4,4}, /* sizeofMatchJump[] */
1218 {0,0,0}, /* sizeofRangeCompare[] */
1219 0, /* sizeofSubtract */
1220 3, /* sizeofDispatch */
1224 _pic16_parseOptions,
1227 _pic16_finaliseOptions,
1228 _pic16_setDefaultOptions,
1229 pic16_assignRegisters,
1232 _pic16_genAssemblerPreamble,
1233 NULL, /* no genAssemblerEnd */
1235 NULL, // _pic16_genXINIT
1236 NULL, /* genInitStartup */
1237 _pic16_reset_regparm,
1239 _process_pragma, /* process a pragma */
1240 _pic16_mangleFunctionName, /* mangles function name */
1242 hasExtBitOp, /* hasExtBitOp */
1243 oclsExpense, /* oclsExpense */
1245 TRUE, /* little endian */
1248 1, /* transform <= to ! > */
1249 1, /* transform >= to ! < */
1250 1, /* transform != to !(a == b) */
1252 FALSE, /* No array initializer support. */
1253 0, //cseCostEstimation, /* !!!no CSE cost estimation yet */
1254 NULL, /* no builtin functions */
1255 GPOINTER, /* treat unqualified pointers as "generic" pointers */
1256 1, /* reset labelKey to 1 */
1257 1, /* globals & local static allowed */