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;
77 z80_opts.sub = SUB_Z80;
78 asm_addTree (&_asxxxx_z80);
84 z80_opts.sub = SUB_GBZ80;
94 _reg_parm (sym_link * l)
96 if (options.noRegParams)
102 if (_G.regParams == 2)
115 _process_pragma (const char *sz)
117 if (startsWith (sz, "bank="))
120 strcpy (buffer, sz + 5);
122 if (isdigit (buffer[0]))
126 else if (!strcmp (buffer, "BASE"))
128 strcpy (buffer, "HOME");
130 if (isdigit (buffer[0]))
132 /* Arg was a bank number. Handle in an ASM independent
135 strcpy (num, sz + 5);
140 case ASM_TYPE_ASXXXX:
141 sprintf (buffer, "CODE_%s", num);
144 sprintf (buffer, "CODE,BANK[%s]", num);
147 /* PENDING: what to use for ISAS? */
148 sprintf (buffer, "CODE,BANK(%s)", num);
154 gbz80_port.mem.code_name = gc_strdup (buffer);
155 code->sname = gbz80_port.mem.code_name;
161 static const char *_gbz80_rgbasmCmd[] =
163 "rgbasm", "-o$1.o", "$1.asm", NULL
166 static const char *_gbz80_rgblinkCmd[] =
168 "xlink", "-tg", "-n$1.sym", "-m$1.map", "-zFF", "$1.lnk", NULL
172 _gbz80_rgblink (void)
182 /* first we need to create the <filename>.lnk file */
183 sprintf (scratchFileName, "%s.lnk", sz);
184 if (!(lnkfile = fopen (scratchFileName, "w")))
186 werror (E_FILE_OPEN_ERR, scratchFileName);
190 fprintf (lnkfile, "[Objects]\n");
193 fprintf (lnkfile, "%s.o\n", sz);
195 for (i = 0; i < nrelFiles; i++)
196 fprintf (lnkfile, "%s\n", relFiles[i]);
198 fprintf (lnkfile, "\n[Libraries]\n");
199 /* additional libraries if any */
200 for (i = 0; i < nlibFiles; i++)
201 fprintf (lnkfile, "%s\n", libFiles[i]);
204 fprintf (lnkfile, "\n[Output]\n" "%s.gb", sz);
208 buildCmdLine (buffer,port->linker.cmd, sz, NULL, NULL, NULL);
209 /* call the linker */
210 if (my_system (buffer))
212 perror ("Cannot exec linker");
218 _parseOptions (int *pargc, char **argv, int *i)
220 if (argv[*i][0] == '-')
222 if (argv[*i][1] == 'b' && IS_GB)
224 int bank = atoi (argv[*i] + 3);
230 sprintf (buffer, "CODE_%u", bank);
231 gbz80_port.mem.code_name = gc_strdup (buffer);
235 sprintf (buffer, "DATA_%u", bank);
236 gbz80_port.mem.data_name = gc_strdup (buffer);
240 else if (!strncmp (argv[*i], "--asm=", 6))
242 if (!strcmp (argv[*i], "--asm=rgbds"))
244 asm_addTree (&_rgbds_gb);
245 gbz80_port.assembler.cmd = _gbz80_rgbasmCmd;
246 gbz80_port.linker.cmd = _gbz80_rgblinkCmd;
247 gbz80_port.linker.do_link = _gbz80_rgblink;
248 _G.asmType = ASM_TYPE_RGBDS;
251 else if (!strcmp (argv[*i], "--asm=asxxxx"))
253 _G.asmType = ASM_TYPE_ASXXXX;
256 else if (!strcmp (argv[*i], "--asm=isas"))
258 asm_addTree (&_isas_gb);
259 /* Munge the function prefix */
260 gbz80_port.fun_prefix = "";
261 _G.asmType = ASM_TYPE_ISAS;
272 if (options.nostdlib == FALSE)
274 setMainValue ("z80libspec", "-k{libdir}/{port} -l{port}.lib");
275 setMainValue ("z80crt0", "{libdir}/{port}/crt0{objext}");
279 setMainValue ("z80libspec", "");
280 setMainValue ("z80crt0", "");
283 setMainValue ("z80extralibfiles", joinn (libFiles, nlibFiles));
284 setMainValue ("z80extralibpaths", joinn (libPaths, nlibPaths));
288 setMainValue ("z80outputtypeflag", "-z");
289 setMainValue ("z80outext", ".gb");
293 setMainValue ("z80outputtypeflag", "-i");
294 setMainValue ("z80outext", ".ihx");
297 setMainValue ("z80extraobj", joinn (relFiles, nrelFiles));
299 sprintf (buffer, "-b_CODE=0x%04X -b_DATA=0x%04X", options.code_loc, options.data_loc);
300 setMainValue ("z80bases", buffer);
304 _finaliseOptions (void)
306 port->mem.default_local_map = data;
307 port->mem.default_globl_map = data;
308 if (_G.asmType == ASM_TYPE_ASXXXX && IS_GB)
309 asm_addTree (&_asxxxx_gb);
315 _setDefaultOptions (void)
317 options.genericPtr = 1; /* default on */
319 options.stackAuto = 1;
320 options.mainreturn = 1;
321 /* first the options part */
322 options.intlong_rent = 1;
323 options.noRegParams = 1;
324 /* Default code and data locations. */
325 options.code_loc = 0x200;
329 options.data_loc = 0xC000;
333 options.data_loc = 0x8000;
336 optimize.global_cse = 1;
341 optimize.loopInvariant = 1;
342 optimize.loopInduction = 1;
348 policy is the function policy
349 params is the parameter format
354 r is 'r' for reentrant, 's' for static functions
355 s is 'c' for callee saves, 'r' for caller saves
356 p is 'p' for profiling on, 'x' for profiling off
358 rr - reentrant, caller saves
360 A combination of register short names and s to signify stack variables.
362 bds - first two args appear in BC and DE, the rest on the stack
363 s - all arguments are on the stack.
366 _mangleSupportFunctionName(char *original)
370 sprintf(buffer, "%s_rr%s_%s", original,
371 options.profile ? "f" : "x",
372 options.noRegParams ? "s" : "bds"
375 return gc_strdup(buffer);
379 _getRegName (struct regs *reg)
390 "{bindir}/link-{port} -n -c -- {z80bases} -m -j" \
392 " {z80extralibfiles} {z80extralibpaths}" \
393 " {z80outputtypeflag} {srcfilename}{z80outext}" \
395 " {srcfilename}{objext}" \
399 "{bindir}/as-{port} -plosgff {srcfilename}{objext} {srcfilename}{asmext}"
406 "Zilog Z80", /* Target name */
409 MODEL_MEDIUM | MODEL_SMALL,
415 "-plosgff", /* Options with debug */
416 "-plosgff", /* Options without debug */
430 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
431 1, 2, 2, 4, 2, 2, 2, 1, 4, 4
453 /* Z80 has no native mul/div commands */
465 0, /* no assembler preamble */
466 0, /* no local IVT generation code */
470 _mangleSupportFunctionName,
474 1, /* transform <= to ! > */
475 1, /* transform >= to ! < */
476 1, /* transform != to !(a == b) */
478 TRUE, /* Array initializer support. */
487 "Gameboy Z80-like", /* Target name */
490 MODEL_MEDIUM | MODEL_SMALL,
496 "-plosgff", /* Options with debug */
497 "-plosgff", /* Options without debug */
511 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
512 1, 2, 2, 4, 2, 2, 2, 1, 4, 4
534 /* gbZ80 has no native mul/div commands */
546 0, /* no assembler preamble */
547 0, /* no local IVT generation code */
551 _mangleSupportFunctionName,
555 1, /* transform <= to ! > */
556 1, /* transform >= to ! < */
557 1, /* transform != to !(a == b) */
559 FALSE, /* No array initializer support. */