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 = Safe_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 = Safe_strdup (buffer);
235 sprintf (buffer, "DATA_%u", bank);
236 gbz80_port.mem.data_name = Safe_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}{sep}{port} -l{port}.lib");
275 setMainValue ("z80crt0", "{libdir}{sep}{port}{sep}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.float_rent = 1;
324 options.noRegParams = 1;
325 /* Default code and data locations. */
326 options.code_loc = 0x200;
330 options.data_loc = 0xC000;
334 options.data_loc = 0x8000;
337 optimize.global_cse = 1;
342 optimize.loopInvariant = 1;
343 optimize.loopInduction = 1;
349 policy is the function policy
350 params is the parameter format
355 r is 'r' for reentrant, 's' for static functions
356 s is 'c' for callee saves, 'r' for caller saves
357 p is 'p' for profiling on, 'x' for profiling off
359 rr - reentrant, caller saves
361 A combination of register short names and s to signify stack variables.
363 bds - first two args appear in BC and DE, the rest on the stack
364 s - all arguments are on the stack.
367 _mangleSupportFunctionName(char *original)
371 sprintf(buffer, "%s_rr%s_%s", original,
372 options.profile ? "f" : "x",
373 options.noRegParams ? "s" : "bds"
376 return Safe_strdup(buffer);
380 _getRegName (struct regs *reg)
391 _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
393 sym_link *test = NULL;
401 if ( IS_LITERAL (left))
404 val = OP_VALUE (IC_LEFT (ic));
406 else if ( IS_LITERAL (right))
409 val = OP_VALUE (IC_RIGHT (ic));
416 if ( getSize (test) <= 2)
425 "{bindir}{sep}link-{port} -n -c -- {z80bases} -m -j" \
427 " {z80extralibfiles} {z80extralibpaths}" \
428 " {z80outputtypeflag} {srcfilename}{z80outext}" \
430 " {srcfilename}{objext}" \
434 "{bindir}{sep}as-{port} -plosgff {srcfilename}{objext} {srcfilename}{asmext}"
441 "Zilog Z80", /* Target name */
444 MODEL_MEDIUM | MODEL_SMALL,
450 "-plosgff", /* Options with debug */
451 "-plosgff", /* Options without debug */
465 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
466 1, 2, 2, 4, 2, 2, 2, 1, 4, 4
488 /* Z80 has no native mul/div commands */
500 0, /* no assembler preamble */
501 0, /* no local IVT generation code */
505 _mangleSupportFunctionName,
510 1, /* transform <= to ! > */
511 1, /* transform >= to ! < */
512 1, /* transform != to !(a == b) */
514 TRUE, /* Array initializer support. */
515 0, /* no CSE cost estimation yet */
524 "Gameboy Z80-like", /* Target name */
527 MODEL_MEDIUM | MODEL_SMALL,
533 "-plosgff", /* Options with debug */
534 "-plosgff", /* Options without debug */
548 /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
549 1, 2, 2, 4, 2, 2, 2, 1, 4, 4
571 /* gbZ80 has no native mul/div commands */
583 0, /* no assembler preamble */
584 0, /* no local IVT generation code */
588 _mangleSupportFunctionName,
593 1, /* transform <= to ! > */
594 1, /* transform >= to ! < */
595 1, /* transform != to !(a == b) */
597 TRUE, /* Array initializer support. */
598 0, /* no CSE cost estimation yet */