1 /*-------------------------------------------------------------------------
2 main.c - Z80 specific definitions.
4 Michael Hope <michaelh@juju.net.nz> 2001
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
30 static char _z80_defaultRules[] =
33 #include "peeph-z80.rul"
36 static char _gbz80_defaultRules[] =
39 #include "peeph-gbz80.rul"
56 /* determine if we can register a parameter */
61 static char *_keywords[] =
69 extern PORT gbz80_port;
74 static builtins _z80_builtins[] = {
76 { "__builtin_strcpy", "v", 2, {"cg*", "cg*" } },
77 { "__builtin_memcpy", "cg*", 3, {"cg*", "cg*", "ui" } },
79 { NULL , NULL,0, {NULL}}
85 z80_opts.sub = SUB_Z80;
86 asm_addTree (&_asxxxx_z80);
92 z80_opts.sub = SUB_GBZ80;
102 _reg_parm (sym_link * l)
104 if (options.noRegParams)
110 if (_G.regParams == 2)
123 _process_pragma (const char *sz)
125 if (startsWith (sz, "bank="))
128 strcpy (buffer, sz + 5);
130 if (isdigit (buffer[0]))
134 else if (!strcmp (buffer, "BASE"))
136 strcpy (buffer, "HOME");
138 if (isdigit (buffer[0]))
140 /* Arg was a bank number. Handle in an ASM independent
143 strcpy (num, sz + 5);
148 case ASM_TYPE_ASXXXX:
149 sprintf (buffer, "CODE_%s", num);
152 sprintf (buffer, "CODE,BANK[%s]", num);
155 /* PENDING: what to use for ISAS? */
156 sprintf (buffer, "CODE,BANK(%s)", num);
162 gbz80_port.mem.code_name = Safe_strdup (buffer);
163 code->sname = gbz80_port.mem.code_name;
169 static const char *_gbz80_rgbasmCmd[] =
171 "rgbasm", "-o$1.o", "$1.asm", NULL
174 static const char *_gbz80_rgblinkCmd[] =
176 "xlink", "-tg", "-n$1.sym", "-m$1.map", "-zFF", "$1.lnk", NULL
180 _gbz80_rgblink (void)
190 /* first we need to create the <filename>.lnk file */
191 sprintf (scratchFileName, "%s.lnk", sz);
192 if (!(lnkfile = fopen (scratchFileName, "w")))
194 werror (E_FILE_OPEN_ERR, scratchFileName);
198 fprintf (lnkfile, "[Objects]\n");
201 fprintf (lnkfile, "%s.o\n", sz);
203 for (i = 0; i < nrelFiles; i++)
204 fprintf (lnkfile, "%s\n", relFiles[i]);
206 fprintf (lnkfile, "\n[Libraries]\n");
207 /* additional libraries if any */
208 for (i = 0; i < nlibFiles; i++)
209 fprintf (lnkfile, "%s\n", libFiles[i]);
212 fprintf (lnkfile, "\n[Output]\n" "%s.gb", sz);
216 buildCmdLine (buffer,port->linker.cmd, sz, NULL, NULL, NULL);
217 /* call the linker */
218 if (my_system (buffer))
220 perror ("Cannot exec linker");
226 _parseOptions (int *pargc, char **argv, int *i)
228 if (argv[*i][0] == '-')
230 if (argv[*i][1] == 'b' && IS_GB)
232 int bank = atoi (argv[*i] + 3);
238 sprintf (buffer, "CODE_%u", bank);
239 gbz80_port.mem.code_name = Safe_strdup (buffer);
243 sprintf (buffer, "DATA_%u", bank);
244 gbz80_port.mem.data_name = Safe_strdup (buffer);
248 else if (!strncmp (argv[*i], "--asm=", 6))
250 if (!strcmp (argv[*i], "--asm=rgbds"))
252 asm_addTree (&_rgbds_gb);
253 gbz80_port.assembler.cmd = _gbz80_rgbasmCmd;
254 gbz80_port.linker.cmd = _gbz80_rgblinkCmd;
255 gbz80_port.linker.do_link = _gbz80_rgblink;
256 _G.asmType = ASM_TYPE_RGBDS;
259 else if (!strcmp (argv[*i], "--asm=asxxxx"))
261 _G.asmType = ASM_TYPE_ASXXXX;
264 else if (!strcmp (argv[*i], "--asm=isas"))
266 asm_addTree (&_isas_gb);
267 /* Munge the function prefix */
268 gbz80_port.fun_prefix = "";
269 _G.asmType = ASM_TYPE_ISAS;
280 if (options.nostdlib == FALSE)
282 setMainValue ("z80libspec", "-k{libdir}{sep}{port} -l{port}.lib");
283 setMainValue ("z80crt0", "{libdir}{sep}{port}{sep}crt0{objext}");
287 setMainValue ("z80libspec", "");
288 setMainValue ("z80crt0", "");
291 setMainValue ("z80extralibfiles", joinn (libFiles, nlibFiles));
292 setMainValue ("z80extralibpaths", joinn (libPaths, nlibPaths));
296 setMainValue ("z80outputtypeflag", "-z");
297 setMainValue ("z80outext", ".gb");
301 setMainValue ("z80outputtypeflag", "-i");
302 setMainValue ("z80outext", ".ihx");
305 setMainValue ("z80extraobj", joinn (relFiles, nrelFiles));
307 sprintf (buffer, "-b_CODE=0x%04X -b_DATA=0x%04X", options.code_loc, options.data_loc);
308 setMainValue ("z80bases", buffer);
312 _finaliseOptions (void)
314 port->mem.default_local_map = data;
315 port->mem.default_globl_map = data;
316 if (_G.asmType == ASM_TYPE_ASXXXX && IS_GB)
317 asm_addTree (&_asxxxx_gb);
323 _setDefaultOptions (void)
325 options.genericPtr = 1; /* default on */
327 options.stackAuto = 1;
328 options.mainreturn = 1;
329 /* first the options part */
330 options.intlong_rent = 1;
331 options.float_rent = 1;
332 options.noRegParams = 1;
333 /* Default code and data locations. */
334 options.code_loc = 0x200;
338 options.data_loc = 0xC000;
342 options.data_loc = 0x8000;
345 optimize.global_cse = 1;
350 optimize.loopInvariant = 1;
351 optimize.loopInduction = 1;
357 policy is the function policy
358 params is the parameter format
363 r is 'r' for reentrant, 's' for static functions
364 s is 'c' for callee saves, 'r' for caller saves
365 p is 'p' for profiling on, 'x' for profiling off
367 rr - reentrant, caller saves
369 A combination of register short names and s to signify stack variables.
371 bds - first two args appear in BC and DE, the rest on the stack
372 s - all arguments are on the stack.
375 _mangleSupportFunctionName(char *original)
379 sprintf(buffer, "%s_rr%s_%s", original,
380 options.profile ? "f" : "x",
381 options.noRegParams ? "s" : "bds"
384 return Safe_strdup(buffer);
388 _getRegName (struct regs *reg)
399 _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
401 sym_link *test = NULL;
409 if ( IS_LITERAL (left))
412 val = OP_VALUE (IC_LEFT (ic));
414 else if ( IS_LITERAL (right))
417 val = OP_VALUE (IC_RIGHT (ic));
424 if ( getSize (test) <= 2)
433 "{bindir}{sep}link-{port} -n -c -- {z80bases} -m -j" \
435 " {z80extralibfiles} {z80extralibpaths}" \
436 " {z80outputtypeflag} {srcfilename}{z80outext}" \
438 " {srcfilename}{objext}" \
442 "{bindir}{sep}as-{port} -plosgff {srcfilename}{objext} {srcfilename}{asmext}"
449 "Zilog Z80", /* Target name */
452 MODEL_MEDIUM | MODEL_SMALL,
458 "-plosgff", /* Options with debug */
459 "-plosgff", /* Options without debug */
473 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
474 1, 2, 2, 4, 2, 2, 2, 1, 4, 4
498 /* Z80 has no native mul/div commands */
510 0, /* no assembler preamble */
511 0, /* no local IVT generation code */
512 0, /* no genXINIT code */
516 _mangleSupportFunctionName,
521 1, /* transform <= to ! > */
522 1, /* transform >= to ! < */
523 1, /* transform != to !(a == b) */
525 TRUE, /* Array initializer support. */
526 0, /* no CSE cost estimation yet */
527 _z80_builtins, /* no builtin functions */
536 "Gameboy Z80-like", /* Target name */
539 MODEL_MEDIUM | MODEL_SMALL,
545 "-plosgff", /* Options with debug */
546 "-plosgff", /* Options without debug */
560 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
561 1, 2, 2, 4, 2, 2, 2, 1, 4, 4
585 /* gbZ80 has no native mul/div commands */
597 0, /* no assembler preamble */
598 0, /* no local IVT generation code */
599 0, /* no genXINIT code */
603 _mangleSupportFunctionName,
608 1, /* transform <= to ! > */
609 1, /* transform >= to ! < */
610 1, /* transform != to !(a == b) */
612 TRUE, /* Array initializer support. */
613 0, /* no CSE cost estimation yet */
614 NULL, /* no builtin functions */