2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011 - 2016 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
26 int perm_verb_flag; // Permanently verbose, interactive mode
27 int list_flag; // "-l" listing flag on command line
28 int list_pag = 1; // Enable listing pagination by default
29 int verb_flag; // Be verbose about what's going on
30 int m6502; // 1, assembling 6502 code
31 int as68_flag; // as68 kludge mode
32 int glob_flag; // Assume undefined symbols are global
33 int lsym_flag; // Include local symbols in object file
34 int sbra_flag; // Warn about possible short branches
35 int prg_flag; // !=0, produce .PRG executable (2=symbols)
36 int legacy_flag; // Do stuff like insert code in RISC assembler
37 int obj_format; // Object format flag
38 int debug; // [1..9] Enable debugging levels
39 int err_flag; // '-e' specified
40 int err_fd; // File to write error messages to
41 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
42 int dsp56001; // Assembling DSP 56001 code
43 int list_fd; // File to write listing to
44 int regbank; // RISC register bank
45 int segpadsize; // Segment padding size
46 int endian; // Host processor endianess (0 = LE, 1 = BE)
47 char * objfname; // Object filename pointer
48 char * firstfname; // First source filename
49 char * cmdlnexec; // Executable name, pointer to ARGV[0]
50 char * searchpath; // Search path for include files
51 char defname[] = "noname.o"; // Default output filename
52 int optim_flags[OPT_COUNT]; // Specific optimisations on/off matrix
53 int activecpu=CPU_68000; // Active 68k CPU (68000 by default)
54 int activefpu=FPU_NONE; // Active FPU (none by default)
57 // Manipulate file extension.
59 // 'name' must be large enough to hold any possible filename. If 'stripp' is
60 // nonzero, any old extension is removed. If the file does not already have an
61 // extension, 'extension' is appended to the filename.
63 char * fext(char * name, char * extension, int stripp)
67 // Find beginning of "real" name (strip off path)
68 char * beg = strrchr(name, SLASHCHAR);
73 // Clobber any old extension, if requested
76 for(s=beg; *s && *s!='.'; ++s)
82 if (strrchr(beg, '.') == NULL)
83 strcat(beg, extension);
90 // Return 'item'nth element of semicolon-seperated pathnames specified in the
91 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
92 // nth path doesn't exist.
94 // ['item' ranges from 0 to N-1, where N = #elements in search path]
96 int nthpath(char * env_var, int itemno, char * buf)
98 char * s = searchpath;
107 while (*s != EOS && *s++ != ';')
113 while (*s != EOS && *s != ';')
123 // Display command line help
125 void DisplayHelp(void)
128 " %s [options] srcfile\n"
131 " -? or -h Display usage information\n"
132 " -dsymbol[=value] Define symbol\n"
133 " -e[errorfile] Send error messages to file, not stdout\n"
134 " -f[format] Output object file format\n"
135 " a: ALCYON (use this for ST)\n"
136 " b: BSD (use this for Jaguar)\n"
138 " x: com/exe/xex (Atari 800)\n"
139 " -i[path] Directory to search for include files\n"
140 " -l[filename] Create an output listing file\n"
141 " -l*[filename] Create an output listing file without pagination\n"
142 " -n Don't do things behind your back in RISC assembler\n"
143 " -o file Output file name\n"
144 " +o[value] Turn a specific optimisation on\n"
145 " Available optimisation values and default settings:\n"
146 " o0: Absolute long adddresses to word (on)\n"
147 " o1: move.l #x,dn/an to moveq (on)\n"
148 " o2: Word branches to short (on)\n"
149 " o3: Outer displacement 0(an) to (an) (off)\n"
150 " o4: lea size(An),An to addq #size,An (off)\n"
151 " o5: Absolute long base displacement to word (off)\n"
152 " ~o[value] Turn a specific optimisation off\n"
153 " +oall Turn all optimisations on\n"
154 " ~oall Turn all optimisations off\n"
155 " -p Create an ST .prg (without symbols)\n"
156 " -ps Create an ST .prg (with symbols)\n"
158 " -r[size] Pad segments to boundary size specified\n"
159 " w: word (2 bytes, default alignment)\n"
160 " l: long (4 bytes)\n"
161 " p: phrase (8 bytes)\n"
162 " d: double phrase (16 bytes)\n"
163 " q: quad phrase (32 bytes)\n"
164 " -s Warn about possible short branches\n"
165 " and applied optimisations\n"
166 " -u Force referenced and undefined symbols global\n"
167 " -v Set verbose mode\n"
168 " -x Turn on debugging mode\n"
169 " -y[pagelen] Set page line length (default: 61)\n"
175 // Display version information
177 void DisplayVersion(void)
180 " _ __ _ __ ___ __ _ ___ \n"
181 "| '__| '_ ` _ \\ / _` |/ __|\n"
182 "| | | | | | | | (_| | (__ \n"
183 "|_| |_| |_| |_|\\__,_|\\___|\n"
184 "\nReboot's Macro Assembler\n"
185 "Copyright (C) 199x Landon Dyer, 2011-2017 Reboot\n"
186 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
191 // Parse optimisation options
193 int ParseOptimization(char * optstring)
197 if (*optstring == '+')
199 else if (*optstring != '~')
202 if ((optstring[2] == 'a' || optstring[2] == 'A')
203 && (optstring[3] == 'l' || optstring[3] == 'L')
204 && (optstring[4] == 'l' || optstring[4] == 'L'))
206 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
209 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
211 int opt_no = atoi(&optstring[2]);
213 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
215 optim_flags[opt_no] = onoff;
229 // Process command line arguments and do an assembly
231 int Process(int argc, char ** argv)
233 int argno; // Argument number
234 SYM * sy; // Pointer to a symbol record
235 char * s; // String pointer
236 int fd; // File descriptor
237 char fnbuf[FNSIZ]; // Filename buffer
240 errcnt = 0; // Initialise error count
241 listing = 0; // Initialise listing level
242 list_flag = 0; // Initialise listing flag
243 verb_flag = perm_verb_flag; // Initialise verbose flag
244 as68_flag = 0; // Initialise as68 kludge mode
245 glob_flag = 0; // Initialise .globl flag
246 sbra_flag = 0; // Initialise short branch flag
247 debug = 0; // Initialise debug flag
248 searchpath = NULL; // Initialise search path
249 objfname = NULL; // Initialise object filename
250 list_fname = NULL; // Initialise listing filename
251 err_fname = NULL; // Initialise error filename
252 obj_format = BSD; // Initialise object format
253 firstfname = NULL; // Initialise first filename
254 err_fd = ERROUT; // Initialise error file descriptor
255 err_flag = 0; // Initialise error flag
256 rgpu = 0; // Initialise GPU assembly flag
257 rdsp = 0; // Initialise DSP assembly flag
258 lsym_flag = 1; // Include local symbols in object file
259 regbank = BANK_N; // No RISC register bank specified
260 orgactive = 0; // Not in RISC org section
261 orgwarning = 0; // No ORG warning issued
262 segpadsize = 2; // Initialise segment padding size
263 m6502 = 0; // 6502 mode off by default
265 // Initialise modules
266 InitSymbolTable(); // Symbol table
267 InitTokenizer(); // Tokenizer
268 InitLineProcessor(); // Line processor
269 InitExpression(); // Expression analyzer
270 InitSection(); // Section manager / code generator
271 InitMark(); // Mark tape-recorder
272 InitMacro(); // Macro processor
273 InitListing(); // Listing generator
274 Init6502(); // 6502 assembler
276 // Process command line arguments and assemble source files
277 for(argno=0; argno<argc; argno++)
279 if (*argv[argno] == '-')
281 switch (argv[argno][1])
283 case 'd': // Define symbol
285 for(s=argv[argno]+2; *s!=EOS;)
294 if (argv[argno][2] == EOS)
296 printf("-d: empty symbol\n");
301 sy = lookup(argv[argno] + 2, 0, 0);
305 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
309 sy->sattr = DEFINED | EQUATED | ABS;
310 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
312 case 'e': // Redirect error message output
314 err_fname = argv[argno] + 2;
316 case 'f': // -f<format>
318 switch (argv[argno][2])
321 case 'a': // -fa = Alcyon [the default]
325 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
329 case 'e': // -fe = ELF
333 case 'x': // -fx = COM/EXE/XEX
338 printf("-f: unknown object format specified\n");
343 case 'g': // Debugging flag
345 printf("Debugging flag (-g) not yet implemented\n");
347 case 'i': // Set directory search path
349 searchpath = argv[argno] + 2;
351 case 'l': // Produce listing file
353 if (*(argv[argno] + 2) == '*')
355 list_fname = argv[argno] + 3;
356 list_pag = 0; // Special case - turn off pagination
360 list_fname = argv[argno] + 2;
367 case 'o': // Direct object file output
369 if (argv[argno][2] != EOS)
370 objfname = argv[argno] + 2;
375 printf("Missing argument to -o");
380 objfname = argv[argno];
384 case 'p': /* -p: generate ".PRG" executable output */
387 * -p .PRG generation w/o symbols
388 * -ps .PRG generation with symbols
390 switch (argv[argno][2])
402 printf("-p: syntax error\n");
407 // Enforce Alcyon object format - kind of silly
408 // to ask for .prg output without it!
411 case 'r': // Pad seg to requested boundary size
413 switch(argv[argno][2])
415 case 'w': case 'W': segpadsize = 2; break;
416 case 'l': case 'L': segpadsize = 4; break;
417 case 'p': case 'P': segpadsize = 8; break;
418 case 'd': case 'D': segpadsize = 16; break;
419 case 'q': case 'Q': segpadsize = 32; break;
420 default: segpadsize = 2; break; // Effective autoeven();
423 case 's': // Warn about possible short branches
427 case 'u': // Make undefined symbols .globl
431 case 'v': // Verbose flag
439 case 'x': // Turn on debugging
442 printf("~ Debugging ON\n");
444 case 'y': // -y<pagelen>
446 pagelen = atoi(argv[argno] + 2);
450 printf("-y: bad page length\n");
456 case EOS: // Input is stdin
457 if (firstfname == NULL) // Kludge first filename
458 firstfname = defname;
460 include(0, "(stdin)");
463 case 'h': // Display command line usage
470 case 'n': // Turn off legacy mode
473 printf("Legacy mode OFF\n");
477 printf("Unknown switch: %s\n\n", argv[argno]);
483 else if (*argv[argno] == '+' || *argv[argno] == '~')
485 if (ParseOptimization(argv[argno]) != OK)
488 printf("Unknown switch: %s\n\n", argv[argno]);
496 // Record first filename.
497 if (firstfname == NULL)
498 firstfname = argv[argno];
500 strcpy(fnbuf, argv[argno]);
501 fext(fnbuf, ".s", 0);
506 printf("Cannot open: %s\n", fnbuf);
516 // Wind-up processing;
517 // o save current section (no more code generation)
518 // o do auto-even of all sections (or boundary alignment as requested
520 // o save the last pc value for 6502 (if we used 6502 at all) and
521 // detemine if the last section contains anything
522 // o determine name of object file:
523 // - "foo.o" for linkable output;
524 // - "foo.prg" for GEMDOS executable (-p flag).
527 for(i=TEXT; i<=BSS; i<<=1)
533 case 2: d_even(); break;
534 case 4: d_long(); break;
535 case 8: d_phrase(); break;
536 case 16: d_dphrase(); break;
537 case 32: d_qphrase(); break;
543 SwitchSection(M6502);
545 if (sloc != currentorg[0])
547 currentorg[1] = sloc;
551 if (objfname == NULL)
553 if (firstfname == NULL)
554 firstfname = defname;
556 strcpy(fnbuf, firstfname);
557 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
561 // With one pass finished, go back and:
562 // (1) run through all the fixups and resolve forward references;
563 // (1.5) ensure that remaining fixups can be handled by the linker
564 // (`lo68' format, extended (postfix) format....)
565 // (2) generate the output file image and symbol table;
566 // (3) generate relocation information from left-over fixups.
567 ResolveAllFixups(); // Do all fixups
568 StopMark(); // Stop mark tape-recorder
572 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
577 s = (prg_flag ? "executable" : "object");
578 printf("[Writing %s file: %s]\n", s, objfname);
591 printf("[Wrapping-up listing file]\n");
601 DEBUG dump_everything();
608 // Determine processor endianess
610 int GetEndianess(void)
613 char * p = (char *)&i;
623 // Application entry point
625 int main(int argc, char ** argv)
627 perm_verb_flag = 0; // Clobber "permanent" verbose flag
628 legacy_flag = 1; // Default is legacy mode on (:-P)
630 // Set legacy optimisation flags to on
631 // and everything else to off
632 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
633 optim_flags[OPT_ABS_SHORT] =
634 optim_flags[OPT_MOVEL_MOVEQ] =
635 optim_flags[OPT_BSR_BCC_S] = 1;
637 cmdlnexec = argv[0]; // Obtain executable name
639 endian = GetEndianess(); // Get processor endianess
641 // If commands were passed in, process them
643 return Process(argc - 1, argv + 1);