2 pic14 specific general functions.
4 Note that mlh prepended _pic14_ on the static functions. Makes
5 it easier to set a breakpoint using the debugger.
12 #include "SDCCmacro.h"
19 static char _defaultRules[] =
25 /* list of key words used by pic14 */
26 static char *_pic14_keywords[] =
53 pic14_options_t pic14_options;
55 #define ARG_STACKLOC "--stack-loc"
56 #define ARG_STACKSIZ "--stack-siz"
58 extern int debug_verbose; /* from pcode.c */
59 static OPTION _pic14_poptions[] = {
60 { 0 , "--debug-xtra", &debug_verbose, "show more debug info in assembly output" },
61 { 0 , "--no-pcode-opt", &pic14_options.disable_df, "disable (slightly faulty) optimization on pCode" },
62 { 0 , ARG_STACKLOC, &pic14_options.stackLocation, "sets the lowest address of the argument passing stack" },
63 { 0 , ARG_STACKSIZ, &pic14_options.stackSize, "sets the size if the argument passing stack (default: 16, minimum: 4)" },
64 { 0 , NULL, NULL, NULL }
67 void pCodeInitRegisters(void);
69 void pic14_assignRegisters (ebbIndex *);
71 /* Also defined in gen.h, but the #include is commented out */
72 /* for an unknowned reason. - EEP */
73 void pic14_emitDebuggerSymbol (char *);
75 static int regParmFlg = 0; /* determine if we can register a parameter */
80 asm_addTree (&asm_asxxxx_mapping);
81 memset (&pic14_options, 0, sizeof (pic14_options));
85 _pic14_reset_regparm (void)
91 _pic14_regparm (sym_link * l, bool reentrant)
93 /* for this processor it is simple
94 can pass only the first parameter in a register */
103 _process_pragma(const char *sz)
105 static const char *WHITE = " \t";
106 char *ptr = strtok((char *)sz, WHITE);
108 if (startsWith (ptr, "memmap"))
115 start = strtok((char *)NULL, WHITE);
116 end = strtok((char *)NULL, WHITE);
117 type = strtok((char *)NULL, WHITE);
118 alias = strtok((char *)NULL, WHITE);
120 if (start != (char *)NULL
121 && end != (char *)NULL
122 && type != (char *)NULL) {
123 value *startVal = constVal(start);
124 value *endVal = constVal(end);
128 if (alias == (char *)NULL) {
129 aliasVal = constVal(0);
131 aliasVal = constVal(alias);
134 r.start_address = (int)floatFromVal(startVal);
135 r.end_address = (int)floatFromVal(endVal);
136 r.alias = (int)floatFromVal(aliasVal);
137 r.bank = (r.start_address >> 7) & 3;
139 if (strcmp(type, "RAM") == 0) {
141 } else if (strcmp(type, "SFR") == 0) {
149 } else if (startsWith (ptr, "maxram")) {
150 // not used any more - comes from device config file pic14devices.txt instead
156 extern char *udata_section_name;
159 _pic14_parseOptions (int *pargc, char **argv, int *i)
164 /* TODO: allow port-specific command line options to specify
165 * segment names here.
168 /* This is a temporary hack, to solve problems with some processors
169 * that do not have udata section. It will be changed when a more
170 * robust solution is figured out -- VR 27-11-2003 FIXME
172 strcpy(buf, "--udata-section-name");
173 if(!strncmp(buf, argv[ *i ], strlen(buf))) {
174 if(strlen(argv[ *i ]) <= strlen(buf)+1) {
175 fprintf(stderr, "WARNING: no `%s' entered\n", buf+2);
178 udata_section_name = strdup( strchr(argv[*i], '=') + 1 );
183 len = strlen(ARG_STACKLOC);
184 if (!strncmp(ARG_STACKLOC, argv[ *i ], len)) {
185 if (argv[*i][len] != '=' || argv[*i][len+1] == 0) {
186 printf("ERROR: no number entered for %s=num\n", ARG_STACKLOC);
189 // extract number from "--stack-loc=0x70"
190 pic14_options.stackLocation = strtol(argv[ *i ] + len + 1, NULL, 0);
193 printf("ERROR: Could not parse number after %s=num\n", ARG_STACKLOC);
199 len = strlen(ARG_STACKLOC);
200 if (!strncmp(ARG_STACKSIZ, argv[*i], len)) {
201 if (argv[*i][len] != '=' || argv[*i][len+1] == 0) {
202 printf("ERROR: no number entered for %s=num\n", ARG_STACKSIZ);
205 // extract number from "--stack-size=16"
206 pic14_options.stackSize = strtol(argv[ *i ] + len + 1, NULL, 0);
208 printf("ERROR: Could not parse number after %s=num\n", ARG_STACKLOC);
217 extern set *dataDirsSet;
218 extern set *includeDirsSet;
219 /* pic14 port uses include/pic and lib/pic instead of
220 * include/pic14 and lib/pic14 as indicated by SDCCmain.c's
221 * setIncludePaths routine. */
223 _pic14_initPaths (void)
229 if (options.nostdinc)
232 tempSet = appendStrSet(dataDirsSet, NULL, INCLUDE_DIR_SUFFIX DIR_SEPARATOR_STRING "pic");
233 mergeSets(&includeDirsSet, tempSet);
235 if ((p = getenv(SDCC_INCLUDE_NAME)) != NULL)
237 addSetHead(&includeDirsSet, p);
238 p2=Safe_alloc(strlen(p)+strlen(DIR_SEPARATOR_STRING)+strlen("pic")+1);
242 strcat(p2, DIR_SEPARATOR_STRING);
244 addSetHead(&includeDirsSet, p2);
250 _pic14_finaliseOptions (void)
252 pCodeInitRegisters();
254 port->mem.default_local_map = data;
255 port->mem.default_globl_map = data;
257 /* Hack-o-matic: if we are using the flat24 model,
258 * adjust pointer sizes.
260 if (options.model == MODEL_FLAT24)
263 fprintf (stderr, "*** WARNING: you should use the '-mds390' option "
264 "for DS80C390 support. This code generator is "
265 "badly out of date and probably broken.\n");
267 port->s.fptr_size = 3;
268 port->s.gptr_size = 4;
269 port->stack.isr_overhead++; /* Will save dpx on ISR entry. */
271 port->stack.call_overhead++; /* This acounts for the extra byte
272 * of return addres on the stack.
273 * but is ugly. There must be a
277 fReturn = fReturn390;
281 if (options.model == MODEL_LARGE)
283 port->mem.default_local_map = xdata;
284 port->mem.default_globl_map = xdata;
288 port->mem.default_local_map = data;
289 port->mem.default_globl_map = data;
292 if (options.stack10bit)
294 if (options.model != MODEL_FLAT24)
297 "*** warning: 10 bit stack mode is only supported in flat24 model.\n");
298 fprintf (stderr, "\t10 bit stack mode disabled.\n");
299 options.stack10bit = 0;
303 /* Fixup the memory map for the stack; it is now in
304 * far space and requires a FPOINTER to access it.
307 istack->ptrType = FPOINTER;
314 _pic14_setDefaultOptions (void)
319 _pic14_getRegName (struct regs *reg)
326 extern char *processor_base_name(void);
329 _pic14_genAssemblerPreamble (FILE * of)
331 char * name = processor_base_name();
336 fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
339 fprintf (of, "\tlist\tp=%s\n",name);
340 fprintf (of, "\tradix dec\n");
341 fprintf (of, "\tinclude \"p%s.inc\"\n",name);
344 /* Generate interrupt vector table. */
346 _pic14_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
350 if (options.model != MODEL_FLAT24)
352 /* Let the default code handle it. */
356 fprintf (of, "\t;ajmp\t__sdcc_gsinit_startup\n");
358 /* now for the other interrupts */
359 for (i = 0; i < maxInterrupts; i++)
363 fprintf (of, "\t;ljmp\t%s\n\t.ds\t4\n", interrupts[i]->rname);
367 fprintf (of, "\t;reti\n\t.ds\t7\n");
375 _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
378 sym_link *test = NULL;
382 //fprintf(stderr,"checking for native mult\n");
389 /* multiply chars in-place */
390 if (getSize(left) == 1 && getSize(right) == 1)
393 /* use library functions for more complex maths */
397 if ( IS_LITERAL (left))
399 fprintf(stderr,"left is lit\n");
401 val = OP_VALUE (IC_LEFT (ic));
403 else if ( IS_LITERAL (right))
405 fprintf(stderr,"right is lit\n");
407 val = OP_VALUE (IC_RIGHT (ic));
411 fprintf(stderr,"oops, neither is lit so no\n");
415 if ( getSize (test) <= 2)
417 fprintf(stderr,"yep\n");
420 fprintf(stderr,"nope\n");
426 /* Indicate which extended bit operations this port supports */
428 hasExtBitOp (int op, int size)
432 /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
439 /* Indicate the expense of an access to an output storage class */
441 oclsExpense (struct memmap *oclass)
443 /* The IN_FARSPACE test is compatible with historical behaviour, */
444 /* but I don't think it is applicable to PIC. If so, please feel */
445 /* free to remove this test -- EEP */
446 if (IN_FARSPACE(oclass))
452 /** $1 is always the basename.
453 $2 is always the output file.
455 $l is the list of extra options that should be there somewhere...
456 MUST be terminated with a NULL.
458 static const char *_linkCmd[] =
460 "gplink", "$l", "-w", "-r", "-o \"$2\"", "\"$1\"", "$3", NULL
463 static const char *_asmCmd[] =
465 "gpasm", "$l", "-c", "\"$1.asm\"", NULL
469 extern set *libFilesSet;
470 extern set *libDirsSet;
471 extern set *libPathsSet;
472 extern set *includeDirsSet;
473 extern set *userIncDirsSet;
474 extern set *dataDirsSetSet;
475 extern set *relFilesSet;
476 extern set *linkOptionsSet;
478 static void _pic14_do_link (void)
480 hTab *linkValues=NULL;
489 * link command format:
490 * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
494 sprintf(lfrm, "{linker} {incdirs} {sysincdirs} {lflags} -w -r -o {outfile} {user_ofile} {spec_ofiles} {ofiles} {libs}");
496 shash_add(&linkValues, "linker", "gplink");
498 /* LIBRARY SEARCH DIRS */
499 mergeSets(&tSet, libPathsSet);
500 mergeSets(&tSet, libDirsSet);
501 shash_add(&linkValues, "incdirs", joinStrSet(appendStrSet(tSet, "-I\"", "\"")));
503 SNPRINTF (&temp[0], 128, "%cpic\"", DIR_SEPARATOR_CHAR);
504 joinStrSet(appendStrSet(libDirsSet, "-I\"", &temp[0]));
505 shash_add(&linkValues, "sysincdirs", joinStrSet(appendStrSet(libDirsSet, "-I\"", &temp[0])));
507 shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
509 shash_add(&linkValues, "outfile", fullDstFileName ? fullDstFileName : dstFileName);
511 if(fullSrcFileName) {
512 sprintf(temp, "%s.o", fullDstFileName ? fullDstFileName : dstFileName );
513 shash_add(&linkValues, "user_ofile", temp);
516 shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
519 procName = processor_base_name();
524 addSet(&libFilesSet, "libsdcc.lib");
525 SNPRINTF(&temp[0], 128, "pic%s.lib", procName);
526 addSet(&libFilesSet, temp);
527 shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
529 lcmd = msprintf(linkValues, lfrm);
531 ret = my_system( lcmd );
544 "MCU pic", /* Target name */
548 TRUE, /* Emit glue around main */
549 MODEL_SMALL | MODEL_LARGE | MODEL_FLAT24,
557 //"-plosgffc", /* Options with debug */
558 //"-plosgff", /* Options without debug */
561 NULL /* no do_assemble function */
566 _pic14_do_link, /* own do link function */
574 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
575 1, 2, 2, 4, 2, 2, 3, 1, 4, 4
576 /* TSD - I changed the size of gptr from 3 to 1. However, it should be
577 2 so that we can accomodate the PIC's with 4 register banks (like the
581 /* tags for generic pointers */
582 { 0x00, 0x00, 0x00, 0x80 }, /* far, near, xstack, code */
599 "CONST (CODE)", // const_name - const data (code or not)
600 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
603 1 // code is read only
609 /* pic14 has an 8 bit mul */
614 pic14_emitDebuggerSymbol
617 255/3, /* maxCount */
618 3, /* sizeofElement */
619 /* The rest of these costs are bogus. They approximate */
620 /* the behavior of src/SDCCicode.c 1.207 and earlier. */
621 {4,4,4}, /* sizeofMatchJump[] */
622 {0,0,0}, /* sizeofRangeCompare[] */
623 0, /* sizeofSubtract */
624 3, /* sizeofDispatch */
631 _pic14_finaliseOptions,
632 _pic14_setDefaultOptions,
633 pic14_assignRegisters,
636 _pic14_genAssemblerPreamble,
637 NULL, /* no genAssemblerEnd */
639 NULL, // _pic14_genXINIT
640 NULL, /* genInitStartup */
641 _pic14_reset_regparm,
643 _process_pragma, /* process a pragma */
646 hasExtBitOp, /* hasExtBitOp */
647 oclsExpense, /* oclsExpense */
649 // TRUE, /* little endian */
650 FALSE, /* little endian - PIC code enumlates big endian */
653 1, /* transform <= to ! > */
654 1, /* transform >= to ! < */
655 1, /* transform != to !(a == b) */
657 FALSE, /* No array initializer support. */
658 0, /* no CSE cost estimation yet */
659 NULL, /* no builtin functions */
660 GPOINTER, /* treat unqualified pointers as "generic" pointers */
661 1, /* reset labelKey to 1 */
662 1, /* globals & local static allowed */