2 hc08 specific general functions.
4 Note that mlh prepended _hc08_ on the static functions. Makes
5 it easier to set a breakpoint using the debugger.
11 #include "../SDCCutil.h"
13 void copyFile(FILE *dest, FILE *src);
14 extern char * iComments2;
15 extern DEBUGFILE dwarf2DebugFile;
16 extern int dwarf2FinalizeFile(FILE *);
18 static char _defaultRules[] =
23 /* list of key words used by msc51 */
24 static char *_hc08_keywords[] =
54 void hc08_assignRegisters (ebbIndex *);
56 static int regParmFlg = 0; /* determine if we can register a parameter */
61 asm_addTree (&asm_asxxxx_mapping);
65 _hc08_reset_regparm (void)
71 _hc08_regparm (sym_link * l)
73 int size = getSize(l);
75 /* If they fit completely, the first two bytes of parameters can go */
76 /* into A and X, otherwise, they go on the stack. Examples: */
77 /* foo(char p1) A <- p1 */
78 /* foo(char p1, char p2) A <- p1, X <- p2 */
79 /* foo(char p1, char p2, char p3) A <- p1, X <- p2, stack <- p3 */
80 /* foo(int p1) XA <- p1 */
81 /* foo(long p1) stack <- p1 */
82 /* foo(char p1, int p2) A <- p1, stack <- p2 */
83 /* foo(int p1, char p2) XA <- p1, stack <- p2 */
88 if ((regParmFlg+size)>2)
95 return 1+regParmFlg-size;
99 _hc08_parseOptions (int *pargc, char **argv, int *i)
101 if (!strcmp (argv[*i], "--out-fmt-elf"))
104 debugFile = &dwarf2DebugFile;
111 static OPTION _hc08_options[] =
113 { 0, "--out-fmt-elf", NULL, "Output executable in ELF format" },
118 _hc08_finaliseOptions (void)
120 if (options.noXinitOpt) {
124 if (options.model == MODEL_LARGE) {
125 port->mem.default_local_map = xdata;
126 port->mem.default_globl_map = xdata;
130 port->mem.default_local_map = data;
131 port->mem.default_globl_map = data;
134 istack->ptrType = FPOINTER;
138 _hc08_setDefaultOptions (void)
140 options.code_loc = 0x8000;
141 options.data_loc = 0x80;
142 options.xdata_loc = 0; /* 0 means immediately following data */
143 options.stack_loc = 0x7fff;
144 options.out_fmt = 1; /* use motorola S19 output */
146 options.ommitFramePtr = 1; /* no frame pointer (we use SP */
147 /* offsets instead) */
152 _hc08_getRegName (struct regs *reg)
160 _hc08_genAssemblerPreamble (FILE * of)
164 symbol *mainExists=newSymbol("main", 0);
167 fprintf (of, "\t.area %s\n",port->mem.code_name);
168 fprintf (of, "\t.area GSINIT0 (CODE)\n");
169 fprintf (of, "\t.area %s\n",port->mem.static_name);
170 fprintf (of, "\t.area %s\n",port->mem.post_static_name);
171 fprintf (of, "\t.area %s\n",port->mem.xinit_name);
172 fprintf (of, "\t.area %s\n",port->mem.data_name);
173 fprintf (of, "\t.area %s\n",port->mem.overlay_name);
174 fprintf (of, "\t.area %s\n",port->mem.bit_name);
175 fprintf (of, "\t.area %s\n",port->mem.xdata_name);
176 fprintf (of, "\t.area %s\n",port->mem.xidata_name);
178 if ((mainExists=findSymWithLevel(SymbolTab, mainExists)))
180 // generate interrupt vector table
181 fprintf (of, "\t.area\tCODEIVT (ABS)\n");
183 for (i=maxInterrupts;i>0;i--)
189 fprintf (of, "\t.org\t0x%04x\n", (0xfffe - (i * 2)));
192 fprintf (of, "\t.dw\t%s\n", interrupts[i]->rname);
198 fprintf (of, "\t.org\t0xfffe\n");
199 fprintf (of, "\t.dw\t%s", "__sdcc_gs_init_startup\n\n");
201 fprintf (of, "\t.area GSINIT0\n");
202 fprintf (of, "__sdcc_gs_init_startup:\n");
203 if (options.stack_loc)
205 fprintf (of, "\tldhx\t#0x%04x\n", options.stack_loc+1);
206 fprintf (of, "\ttxs\n");
209 fprintf (of, "\trsp\n");
210 fprintf (of, "\tjsr\t__sdcc_external_startup\n");
211 fprintf (of, "\tbeq\t__sdcc_init_data\n");
212 fprintf (of, "\tjmp\t__sdcc_program_startup\n");
213 fprintf (of, "__sdcc_init_data:\n");
215 fprintf (of, "; _hc08_genXINIT() start\n");
216 fprintf (of, " ldhx #0\n");
217 fprintf (of, "00001$:\n");
218 fprintf (of, " cphx #l_XINIT\n");
219 fprintf (of, " beq 00002$\n");
220 fprintf (of, " lda s_XINIT,x\n");
221 fprintf (of, " sta s_XISEG,x\n");
222 fprintf (of, " aix #1\n");
223 fprintf (of, " bra 00001$\n");
224 fprintf (of, "00002$:\n");
225 fprintf (of, "; _hc08_genXINIT() end\n");
227 fprintf (of, "\t.area GSFINAL\n");
228 fprintf (of, "\tjmp\t__sdcc_program_startup\n\n");
230 fprintf (of, "\t.area CSEG\n");
231 fprintf (of, "__sdcc_program_startup:\n");
232 fprintf (of, "\tjsr\t_main\n");
233 fprintf (of, "\tbra\t.\n");
239 _hc08_genAssemblerEnd (FILE * of)
241 if (options.out_fmt == 2 && options.debug)
243 dwarf2FinalizeFile (of);
248 _hc08_genExtraAreas (FILE * asmFile, bool mainExists)
250 fprintf (asmFile, "%s", iComments2);
251 fprintf (asmFile, "; extended address mode data\n");
252 fprintf (asmFile, "%s", iComments2);
253 copyFile (asmFile, xdata->oFile);
257 /* Generate interrupt vector table. */
259 _hc08_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
263 fprintf (of, "\t.area\tCODEIVT (ABS)\n");
264 fprintf (of, "\t.org\t0x%04x\n",
265 (0xfffe - (maxInterrupts * 2)));
267 for (i=maxInterrupts;i>0;i--)
270 fprintf (of, "\t.dw\t%s\n", interrupts[i]->rname);
272 fprintf (of, "\t.dw\t0xffff\n");
274 fprintf (of, "\t.dw\t%s", "__sdcc_gs_init_startup\n");
279 /* Generate code to copy XINIT to XISEG */
280 static void _hc08_genXINIT (FILE * of) {
281 fprintf (of, "; _hc08_genXINIT() start\n");
282 fprintf (of, "; _hc08_genXINIT() end\n");
286 /* Do CSE estimation */
287 static bool cseCostEstimation (iCode *ic, iCode *pdic)
289 operand *result = IC_RESULT(ic);
290 sym_link *result_type = operandType(result);
292 return 0; /* disable CSE */
294 /* if it is a pointer then return ok for now */
295 if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
297 if (ic->op == ADDRESS_OF)
300 /* if bitwise | add & subtract then no since hc08 is pretty good at it
301 so we will cse only if they are local (i.e. both ic & pdic belong to
302 the same basic block */
303 if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
304 /* then if they are the same Basic block then ok */
305 if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
309 /* for others it is cheaper to do the cse */
314 /* Indicate which extended bit operations this port supports */
316 hasExtBitOp (int op, int size)
321 || (op == SWAP && size <= 2)
328 /* Indicate the expense of an access to an output storage class */
330 oclsExpense (struct memmap *oclass)
332 /* The hc08's addressing modes allow access to all storage classes */
333 /* inexpensively (<=0) */
335 if (IN_DIRSPACE (oclass)) /* direct addressing mode is fastest */
337 if (IN_FARSPACE (oclass)) /* extended addressing mode is almost at fast */
339 if (oclass == istack) /* stack is the slowest, but still faster than */
340 return 0; /* trying to copy to a temp location elsewhere */
342 return 0; /* anything we missed */
346 /*----------------------------------------------------------------------*/
347 /* hc08_dwarfRegNum - return the DWARF register number for a register. */
348 /* These are defined for the HC08 in "Motorola 8- and 16-bit Embedded */
349 /* Application Binary Interface (M8/16EABI)" */
350 /*----------------------------------------------------------------------*/
352 hc08_dwarfRegNum (regs * reg)
356 case A_IDX: return 0;
357 case H_IDX: return 1;
358 case X_IDX: return 2;
359 case CND_IDX: return 17;
360 case SP_IDX: return 15;
367 /** $1 is always the basename.
368 $2 is always the output file.
370 $l is the list of extra options that should be there somewhere...
371 MUST be terminated with a NULL.
373 static const char *_linkCmd[] =
375 "link-hc08", "-nf", "\"$1\"", NULL
378 /* $3 is replaced by assembler.debug_opts resp. port->assembler.plain_opts */
379 static const char *_asmCmd[] =
381 "as-hc08", "$l", "$3", "\"$1.asm\"", NULL
389 "HC08", /* Target name */
390 NULL, /* Processor name */
393 FALSE, /* Emit glue around main */
394 MODEL_SMALL | MODEL_LARGE,
400 "-plosgffc", /* Options with debug */
401 "-plosgff", /* Options without debug */
404 NULL /* no do_assemble function */
417 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
418 1, 2, 2, 4, 2, 2, 2, 1, 4, 4
434 "XISEG", // initialized xdata
435 "XINIT", // a code copy of xiseg
440 { _hc08_genExtraAreas,
443 -1, /* direction (-1 = stack grows down) */
444 0, /* bank_overhead (switch between register banks) */
445 4, /* isr_overhead */
446 2, /* call_overhead */
447 0, /* reent_overhead */
448 0 /* banked_overhead (switch between code banks) */
450 /* hc08 has an 8 bit mul */
455 hc08_emitDebuggerSymbol,
469 2, /* sizeofElement */
470 {8,16,32}, /* sizeofMatchJump[] */
471 {8,16,32}, /* sizeofRangeCompare[] */
472 5, /* sizeofSubtract */
473 10, /* sizeofDispatch */
480 _hc08_finaliseOptions,
481 _hc08_setDefaultOptions,
482 hc08_assignRegisters,
485 _hc08_genAssemblerPreamble,
486 _hc08_genAssemblerEnd, /* no genAssemblerEnd */
489 NULL, /* genInitStartup */
492 NULL, /* process_pragma */
493 NULL, /* getMangledFunctionName */
494 NULL, /* hasNativeMulFor */
495 hasExtBitOp, /* hasExtBitOp */
496 oclsExpense, /* oclsExpense */
497 TRUE, /* use_dw_for_init */
498 FALSE, /* little endian */
501 1, /* transform <= to ! > */
502 1, /* transform >= to ! < */
503 1, /* transform != to !(a == b) */
505 FALSE, /* No array initializer support. */
507 NULL, /* no builtin functions */
508 GPOINTER, /* treat unqualified pointers as "generic" pointers */
509 1, /* reset labelKey to 1 */
510 1, /* globals & local static allowed */