2 mcs51 specific general functions.
4 Note that mlh prepended _mcs51_ on the static functions. Makes
5 it easier to set a breakpoint using the debugger.
11 #include "dbuf_string.h"
12 #include "../SDCCutil.h"
14 static char _defaultRules[] =
19 #define OPTION_STACK_SIZE "--stack-size"
21 static OPTION _mcs51_options[] =
23 { 0, OPTION_STACK_SIZE, &options.stack_size, "Tells the linker to allocate this space for stack", CLAT_INTEGER },
24 { 0, "--parms-in-bank1", &options.parms_in_bank1, "use Bank1 for parameter passing"},
25 { 0, "--pack-iram", NULL, "Tells the linker to pack variables in internal ram (default)"},
26 { 0, "--no-pack-iram", &options.no_pack_iram, "Tells the linker not to pack variables in internal ram"},
30 /* list of key words used by msc51 */
31 static char *_mcs51_keywords[] =
65 void mcs51_assignRegisters (ebbIndex *);
67 static int regParmFlg = 0; /* determine if we can register a parameter */
68 static int regBitParmFlg = 0; /* determine if we can register a bit parameter */
73 asm_addTree (&asm_asxxxx_mapping);
77 _mcs51_reset_regparm (void)
84 _mcs51_regparm (sym_link * l, bool reentrant)
86 if (IS_SPEC(l) && (SPEC_NOUN(l) == V_BIT)) {
87 /* bit parameters go to b0 thru b7 */
88 if (reentrant && (regBitParmFlg < 8)) {
90 return 12 + regBitParmFlg;
94 if (options.parms_in_bank1 == 0) {
95 /* simple can pass only the first parameter in a register */
102 int size = getSize(l);
105 /* first one goes the usual way to DPTR */
106 if (regParmFlg == 0) {
110 /* second one onwards goes to RB1_0 thru RB1_7 */
111 remain = regParmFlg - 4;
112 if (size > (8 - remain)) {
117 return regParmFlg - size + 1;
122 _mcs51_parseOptions (int *pargc, char **argv, int *i)
124 /* TODO: allow port-specific command line options to specify
125 * segment names here.
131 _mcs51_finaliseOptions (void)
133 if (options.noXinitOpt) {
137 switch (options.model)
140 port->mem.default_local_map = data;
141 port->mem.default_globl_map = data;
142 port->s.gptr_size = 3;
145 port->mem.default_local_map = pdata;
146 port->mem.default_globl_map = pdata;
147 port->s.gptr_size = 3;
150 port->mem.default_local_map = xdata;
151 port->mem.default_globl_map = xdata;
152 port->s.gptr_size = 3;
155 port->mem.default_local_map = data;
156 port->mem.default_globl_map = data;
160 if (options.parms_in_bank1) {
161 addSet(&preArgvSet, Safe_strdup("-DSDCC_PARMS_IN_BANK1"));
166 _mcs51_setDefaultOptions (void)
171 _mcs51_getRegName (struct regs *reg)
179 _mcs51_genAssemblerPreamble (FILE * of)
181 if (options.parms_in_bank1) {
183 for (i=0; i < 8 ; i++ )
184 fprintf (of,"b1_%d = 0x%x \n",i,8+i);
188 /* Generate interrupt vector table. */
190 _mcs51_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts)
194 dbuf_printf (oBuf, "\tljmp\t__sdcc_gsinit_startup\n");
196 /* now for the other interrupts */
197 for (i = 0; i < maxInterrupts; i++)
201 dbuf_printf (oBuf, "\tljmp\t%s\n", interrupts[i]->rname);
202 if ( i != maxInterrupts - 1 )
203 dbuf_printf (oBuf, "\t.ds\t5\n");
207 dbuf_printf (oBuf, "\treti\n");
208 if ( i != maxInterrupts - 1 )
209 dbuf_printf (oBuf, "\t.ds\t7\n");
216 _mcs51_genExtraAreas(FILE *of, bool hasMain)
218 tfprintf (of, "\t!area\n", HOME_NAME);
219 tfprintf (of, "\t!area\n", "GSINIT0 (CODE)");
220 tfprintf (of, "\t!area\n", "GSINIT1 (CODE)");
221 tfprintf (of, "\t!area\n", "GSINIT2 (CODE)");
222 tfprintf (of, "\t!area\n", "GSINIT3 (CODE)");
223 tfprintf (of, "\t!area\n", "GSINIT4 (CODE)");
224 tfprintf (of, "\t!area\n", "GSINIT5 (CODE)");
225 tfprintf (of, "\t!area\n", STATIC_NAME);
226 tfprintf (of, "\t!area\n", port->mem.post_static_name);
227 tfprintf (of, "\t!area\n", CODE_NAME);
231 _mcs51_genInitStartup (FILE *of)
233 tfprintf (of, "\t!global\n", "__sdcc_gsinit_startup");
234 tfprintf (of, "\t!global\n", "__sdcc_program_startup");
235 tfprintf (of, "\t!global\n", "__start__stack");
237 if (options.useXstack)
239 tfprintf (of, "\t!global\n", "__sdcc_init_xstack");
240 tfprintf (of, "\t!global\n", "__start__xstack");
243 // if the port can copy the XINIT segment to XISEG
249 if (!getenv("SDCC_NOGENRAMCLEAR"))
250 tfprintf (of, "\t!global\n", "__mcs51_genRAMCLEAR");
254 /* Generate code to copy XINIT to XISEG */
255 static void _mcs51_genXINIT (FILE * of) {
256 tfprintf (of, "\t!global\n", "__mcs51_genXINIT");
258 if (!getenv("SDCC_NOGENRAMCLEAR"))
259 tfprintf (of, "\t!global\n", "__mcs51_genXRAMCLEAR");
263 /* Do CSE estimation */
264 static bool cseCostEstimation (iCode *ic, iCode *pdic)
266 operand *result = IC_RESULT(ic);
267 sym_link *result_type = operandType(result);
269 /* if it is a pointer then return ok for now */
270 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
272 /* if bitwise | add & subtract then no since mcs51 is pretty good at it
273 so we will cse only if they are local (i.e. both ic & pdic belong to
274 the same basic block */
275 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
276 /* then if they are the same Basic block then ok */
277 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
281 /* for others it is cheaper to do the cse */
285 /* Indicate which extended bit operations this port supports */
287 hasExtBitOp (int op, int size)
295 || (op == SWAP && size <= 2)
302 /* Indicate the expense of an access to an output storage class */
304 oclsExpense (struct memmap *oclass)
306 if (IN_FARSPACE(oclass))
315 instructionSize(char *inst, char *op1, char *op2)
317 #define ISINST(s) (strncmp(inst, (s), sizeof(s)-1) == 0)
318 #define IS_A(s) (*(s) == 'a' && *(s+1) == '\0')
319 #define IS_C(s) (*(s) == 'c' && *(s+1) == '\0')
320 #define IS_Rn(s) (*(s) == 'r' && *(s+1) >= '0' && *(s+1) <= '7')
321 #define IS_atRi(s) (*(s) == '@' && *(s+1) == 'r')
323 /* Based on the current (2003-08-22) code generation for the
324 small library, the top instruction probability is:
335 /* mov, push, & pop are the 69% of the cases. Check them first! */
338 if (*(inst+3)=='x') return 1; /* movx */
339 if (*(inst+3)=='c') return 1; /* movc */
340 if (IS_C (op1) || IS_C (op2)) return 2;
343 if (IS_Rn (op2) || IS_atRi (op2)) return 1;
346 if (IS_Rn(op1) || IS_atRi(op1))
348 if (IS_A(op2)) return 1;
351 if (strcmp (op1, "dptr") == 0) return 3;
352 if (IS_A (op2) || IS_Rn (op2) || IS_atRi (op2)) return 2;
356 if (ISINST ("push")) return 2;
357 if (ISINST ("pop")) return 2;
359 if (ISINST ("lcall")) return 3;
360 if (ISINST ("ret")) return 1;
361 if (ISINST ("ljmp")) return 3;
362 if (ISINST ("sjmp")) return 2;
363 if (ISINST ("rlc")) return 1;
364 if (ISINST ("rrc")) return 1;
365 if (ISINST ("rl")) return 1;
366 if (ISINST ("rr")) return 1;
367 if (ISINST ("swap")) return 1;
368 if (ISINST ("jc")) return 2;
369 if (ISINST ("jnc")) return 2;
370 if (ISINST ("jb")) return 3;
371 if (ISINST ("jnb")) return 3;
372 if (ISINST ("jbc")) return 3;
373 if (ISINST ("jmp")) return 1; // always jmp @a+dptr
374 if (ISINST ("jz")) return 2;
375 if (ISINST ("jnz")) return 2;
376 if (ISINST ("cjne")) return 3;
377 if (ISINST ("mul")) return 1;
378 if (ISINST ("div")) return 1;
379 if (ISINST ("da")) return 1;
380 if (ISINST ("xchd")) return 1;
381 if (ISINST ("reti")) return 1;
382 if (ISINST ("nop")) return 1;
383 if (ISINST ("acall")) return 2;
384 if (ISINST ("ajmp")) return 2;
387 if (ISINST ("add") || ISINST ("addc") || ISINST ("subb") || ISINST ("xch"))
389 if (IS_Rn(op2) || IS_atRi(op2)) return 1;
392 if (ISINST ("inc") || ISINST ("dec"))
394 if (IS_A(op1) || IS_Rn(op1) || IS_atRi(op1)) return 1;
395 if (strcmp(op1, "dptr") == 0) return 1;
398 if (ISINST ("anl") || ISINST ("orl") || ISINST ("xrl"))
400 if (IS_C(op1)) return 2;
403 if (IS_Rn(op2) || IS_atRi(op2)) return 1;
408 if (IS_A(op2)) return 2;
412 if (ISINST ("clr") || ISINST ("setb") || ISINST ("cpl"))
414 if (IS_A(op1) || IS_C(op1)) return 1;
419 if (IS_Rn(op1)) return 2;
423 /* If the instruction is unrecognized, we shouldn't try to optimize. */
424 /* Return a large value to discourage optimization. */
429 newAsmLineNode (void)
433 aln = Safe_alloc ( sizeof (asmLineNode));
435 aln->regsRead = NULL;
436 aln->regsWritten = NULL;
442 typedef struct mcs51operanddata
450 static mcs51operanddata mcs51operandDataTable[] =
453 {"ab", A_IDX, B_IDX},
467 {"dph", DPH_IDX, -1},
468 {"dpl", DPL_IDX, -1},
469 {"dptr", DPL_IDX, DPH_IDX},
474 {"psw", CND_IDX, -1},
486 mcs51operandCompare (const void *key, const void *member)
488 return strcmp((const char *)key, ((mcs51operanddata *)member)->name);
492 updateOpRW (asmLineNode *aln, char *op, char *optype)
494 mcs51operanddata *opdat;
497 dot = strchr(op, '.');
501 opdat = bsearch (op, mcs51operandDataTable,
502 sizeof(mcs51operandDataTable)/sizeof(mcs51operanddata),
503 sizeof(mcs51operanddata), mcs51operandCompare);
505 if (opdat && strchr(optype,'r'))
507 if (opdat->regIdx1 >= 0)
508 aln->regsRead = bitVectSetBit (aln->regsRead, opdat->regIdx1);
509 if (opdat->regIdx2 >= 0)
510 aln->regsRead = bitVectSetBit (aln->regsRead, opdat->regIdx2);
512 if (opdat && strchr(optype,'w'))
514 if (opdat->regIdx1 >= 0)
515 aln->regsWritten = bitVectSetBit (aln->regsWritten, opdat->regIdx1);
516 if (opdat->regIdx2 >= 0)
517 aln->regsWritten = bitVectSetBit (aln->regsWritten, opdat->regIdx2);
521 if (!strcmp(op, "@r0"))
522 aln->regsRead = bitVectSetBit (aln->regsRead, R0_IDX);
523 if (!strcmp(op, "@r1"))
524 aln->regsRead = bitVectSetBit (aln->regsRead, R1_IDX);
525 if (strstr(op, "dptr"))
527 aln->regsRead = bitVectSetBit (aln->regsRead, DPL_IDX);
528 aln->regsRead = bitVectSetBit (aln->regsRead, DPH_IDX);
530 if (strstr(op, "a+"))
531 aln->regsRead = bitVectSetBit (aln->regsRead, A_IDX);
535 typedef struct mcs51opcodedata
545 static mcs51opcodedata mcs51opcodeDataTable[] =
547 {"acall","j", "", "", ""},
548 {"add", "", "w", "rw", "r"},
549 {"addc", "", "rw", "rw", "r"},
550 {"ajmp", "j", "", "", ""},
551 {"anl", "", "", "rw", "r"},
552 {"cjne", "j", "w", "r", "r"},
553 {"clr", "", "", "w", ""},
554 {"cpl", "", "", "rw", ""},
555 {"da", "", "rw", "rw", ""},
556 {"dec", "", "", "rw", ""},
557 {"div", "", "w", "rw", ""},
558 {"djnz", "j", "", "rw", ""},
559 {"inc", "", "", "rw", ""},
560 {"jb", "j", "", "r", ""},
561 {"jbc", "j", "", "rw", ""},
562 {"jc", "j", "", "", ""},
563 {"jmp", "j", "", "", ""},
564 {"jnb", "j", "", "r", ""},
565 {"jnc", "j", "", "", ""},
566 {"jnz", "j", "", "", ""},
567 {"jz", "j", "", "", ""},
568 {"lcall","j", "", "", ""},
569 {"ljmp", "j", "", "", ""},
570 {"mov", "", "", "w", "r"},
571 {"movc", "", "", "w", "r"},
572 {"movx", "", "", "w", "r"},
573 {"mul", "", "w", "rw", ""},
574 {"nop", "", "", "", ""},
575 {"orl", "", "", "rw", "r"},
576 {"pop", "", "", "w", ""},
577 {"push", "", "", "r", ""},
578 {"ret", "j", "", "", ""},
579 {"reti", "j", "", "", ""},
580 {"rl", "", "", "rw", ""},
581 {"rlc", "", "rw", "rw", ""},
582 {"rr", "", "", "rw", ""},
583 {"rrc", "", "rw", "rw", ""},
584 {"setb", "", "", "w", ""},
585 {"sjmp", "j", "", "", ""},
586 {"subb", "", "rw", "rw", "r"},
587 {"swap", "", "", "rw", ""},
588 {"xch", "", "", "rw", "rw"},
589 {"xchd", "", "", "rw", "rw"},
590 {"xrl", "", "", "rw", "r"},
594 mcs51opcodeCompare (const void *key, const void *member)
596 return strcmp((const char *)key, ((mcs51opcodedata *)member)->name);
600 asmLineNodeFromLineNode (lineNode *ln)
602 asmLineNode *aln = newAsmLineNode();
603 char *op, op1[256], op2[256];
607 mcs51opcodedata *opdat;
611 while (*p && isspace(*p)) p++;
612 for (op = inst, opsize=1; *p; p++)
614 if (isspace(*p) || *p == ';' || *p == ':' || *p == '=')
617 if (opsize < sizeof(inst))
618 *op++ = tolower(*p), opsize++;
622 if (*p == ';' || *p == ':' || *p == '=')
625 while (*p && isspace(*p)) p++;
629 for (op = op1, opsize=1; *p && *p != ','; p++)
631 if (!isspace(*p) && opsize < sizeof(op1))
632 *op++ = tolower(*p), opsize++;
637 for (op = op2, opsize=1; *p && *p != ','; p++)
639 if (!isspace(*p) && opsize < sizeof(op2))
640 *op++ = tolower(*p), opsize++;
644 aln->size = instructionSize(inst, op1, op2);
646 aln->regsRead = newBitVect (END_IDX);
647 aln->regsWritten = newBitVect (END_IDX);
649 opdat = bsearch (inst, mcs51opcodeDataTable,
650 sizeof(mcs51opcodeDataTable)/sizeof(mcs51opcodedata),
651 sizeof(mcs51opcodedata), mcs51opcodeCompare);
655 updateOpRW (aln, op1, opdat->op1type);
656 updateOpRW (aln, op2, opdat->op2type);
657 if (strchr(opdat->pswtype,'r'))
658 aln->regsRead = bitVectSetBit (aln->regsRead, CND_IDX);
659 if (strchr(opdat->pswtype,'w'))
660 aln->regsWritten = bitVectSetBit (aln->regsWritten, CND_IDX);
667 getInstructionSize (lineNode *line)
670 line->aln = asmLineNodeFromLineNode (line);
672 return line->aln->size;
676 getRegsRead (lineNode *line)
679 line->aln = asmLineNodeFromLineNode (line);
681 return line->aln->regsRead;
685 getRegsWritten (lineNode *line)
688 line->aln = asmLineNodeFromLineNode (line);
690 return line->aln->regsWritten;
694 /** $1 is always the basename.
695 $2 is always the output file.
697 $l is the list of extra options that should be there somewhere...
698 MUST be terminated with a NULL.
700 static const char *_linkCmd[] =
702 "aslink", "-nf", "\"$1\"", NULL
705 /* $3 is replaced by assembler.debug_opts resp. port->assembler.plain_opts */
706 static const char *_asmCmd[] =
708 "asx8051", "$l", "$3", "\"$1.asm\"", NULL
716 "MCU 8051", /* Target name */
717 NULL, /* Processor name */
720 TRUE, /* Emit glue around main */
721 MODEL_SMALL | MODEL_MEDIUM | MODEL_LARGE,
727 "-plosgffc", /* Options with debug */
728 "-plosgff", /* Options without debug */
731 NULL /* no do_assemble function */
748 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
749 1, 2, 2, 4, 1, 2, 3, 1, 4, 4
751 /* tags for generic pointers */
752 { 0x00, 0x40, 0x60, 0x80 }, /* far, near, xstack, code */
754 "XSTK (PAG,XDATA)", // xstack_name
755 "STACK (DATA)", // istack_name
756 "CSEG (CODE)", // code_name
757 "DSEG (DATA)", // data_name
758 "ISEG (DATA)", // idata_name
759 "PSEG (PAG,XDATA)", // pdata_name
760 "XSEG (XDATA)", // xdata_name
761 "BSEG (BIT)", // bit_name
762 "RSEG (DATA)", // reg_name
763 "GSINIT (CODE)", // static_name
764 "OSEG (OVR,DATA)", // overlay_name
765 "GSFINAL (CODE)", // post_static_name
766 "HOME (CODE)", // home_name
767 "XISEG (XDATA)", // xidata_name - initialized xdata initialized xdata
768 "XINIT (CODE)", // xinit_name - a code copy of xiseg
769 "CONST (CODE)", // const_name - const data (code or not)
770 "CABS (ABS,CODE)", // cabs_name - const absolute data (code or not)
771 "XABS (ABS,XDATA)", // xabs_name - absolute xdata/pdata
772 "IABS (ABS,DATA)", // iabs_name - absolute idata/data
777 { _mcs51_genExtraAreas, NULL },
779 +1, /* direction (+1 = stack grows up) */
780 0, /* bank_overhead (switch between register banks) */
781 4, /* isr_overhead */
782 1, /* call_overhead (2 for return address - 1 for pre-incrementing push */
783 1, /* reent_overhead */
784 0 /* banked_overhead (switch between code banks) */
787 /* mcs51 has an 8 bit mul */
791 mcs51_emitDebuggerSymbol
795 2, /* sizeofElement */
796 {6,9,15}, /* sizeofMatchJump[] */
797 {9,18,36}, /* sizeofRangeCompare[] */
798 4, /* sizeofSubtract */
799 6, /* sizeofDispatch */
806 _mcs51_finaliseOptions,
807 _mcs51_setDefaultOptions,
808 mcs51_assignRegisters,
811 _mcs51_genAssemblerPreamble,
812 NULL, /* no genAssemblerEnd */
815 _mcs51_genInitStartup,
816 _mcs51_reset_regparm,
821 hasExtBitOp, /* hasExtBitOp */
822 oclsExpense, /* oclsExpense */
824 TRUE, /* little endian */
827 1, /* transform <= to ! > */
828 1, /* transform >= to ! < */
829 1, /* transform != to !(a == b) */
831 FALSE, /* No array initializer support. */
833 NULL, /* no builtin functions */
834 GPOINTER, /* treat unqualified pointers as "generic" pointers */
835 1, /* reset labelKey to 1 */
836 1, /* globals & local static allowed */