2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2017 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 prg_extend; // !=0, output extended .PRG symbols
37 int legacy_flag; // Do stuff like insert code in RISC assembler
38 int obj_format; // Object format flag
39 int debug; // [1..9] Enable debugging levels
40 int err_flag; // '-e' specified
41 int err_fd; // File to write error messages to
42 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
43 int dsp56001; // Assembling DSP 56001 code
44 int list_fd; // File to write listing to
45 int regbank; // RISC register bank
46 int segpadsize; // Segment padding size
47 int endian; // Host processor endianess (0 = LE, 1 = BE)
48 char * objfname; // Object filename pointer
49 char * firstfname; // First source filename
50 char * cmdlnexec; // Executable name, pointer to ARGV[0]
51 char * searchpath; // Search path for include files
52 char defname[] = "noname.o"; // Default output filename
53 int optim_flags[OPT_COUNT]; // Specific optimisations on/off matrix
54 int activecpu = CPU_68000; // Active 68k CPU (68000 by default)
55 int activefpu = FPU_NONE; // Active FPU (none by default)
59 // Manipulate file extension.
61 // 'name' must be large enough to hold any possible filename. If 'stripp' is
62 // nonzero, any old extension is removed. If the file does not already have an
63 // extension, 'extension' is appended to the filename.
65 char * fext(char * name, char * extension, int stripp)
69 // Find beginning of "real" name (strip off path)
70 char * beg = strrchr(name, SLASHCHAR);
75 // Clobber any old extension, if requested
78 for(s=beg; *s && *s!='.'; ++s)
84 if (strrchr(beg, '.') == NULL)
85 strcat(beg, extension);
92 // Return 'item'nth element of semicolon-seperated pathnames specified in the
93 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
94 // nth path doesn't exist.
96 // ['item' ranges from 0 to N-1, where N = #elements in search path]
98 int nthpath(char * env_var, int itemno, char * buf)
100 char * s = searchpath;
109 while (*s != EOS && *s++ != ';')
115 while (*s != EOS && *s != ';')
125 // Display command line help
127 void DisplayHelp(void)
130 " %s [options] srcfile\n"
133 " -? or -h Display usage information\n"
134 " -dsymbol[=value] Define symbol (with optional value, default=0)\n"
135 " -e[errorfile] Send error messages to file, not stdout\n"
136 " -f[format] Output object file format\n"
137 " a: ALCYON (use this for ST)\n"
138 " b: BSD (use this for Jaguar)\n"
140 " x: com/exe/xex (Atari 800)\n"
141 " -i[path] Directory to search for include files\n"
142 " -l[filename] Create an output listing file\n"
143 " -l*[filename] Create an output listing file without pagination\n"
144 " -m[cpu] Select default CPU. Available options:\n"
153 " -n Don't do things behind your back in RISC assembler\n"
154 " -o file Output file name\n"
155 " +o[value] Turn a specific optimisation on\n"
156 " Available optimisation values and default settings:\n"
157 " o0: Absolute long adddresses to word (on)\n"
158 " o1: move.l #x,dn/an to moveq (on)\n"
159 " o2: Word branches to short (on)\n"
160 " o3: Outer displacement 0(an) to (an) (off)\n"
161 " o4: lea size(An),An to addq #size,An (off)\n"
162 " o5: Absolute long base displacement to word (off)\n"
163 " o6: Null branches to NOP (off)\n"
164 " o7: clr.l Dx to moveq #0,Dx (off)\n"
165 " o8: adda.w/l #x,Dy to addq.w/l #x,Dy (off)\n"
166 " o9: adda.w/l #x,Dy to lea x(Dy),Dy (off)\n"
167 " ~o[value] Turn a specific optimisation off\n"
168 " +oall Turn all optimisations on\n"
169 " ~oall Turn all optimisations off\n"
170 " -p Create an ST .prg (without symbols)\n"
171 " -ps Create an ST .prg (with symbols)\n"
172 " -px Create an ST .prg (with exsymbols)\n"
174 " -r[size] Pad segments to boundary size specified\n"
175 " w: word (2 bytes, default alignment)\n"
176 " l: long (4 bytes)\n"
177 " p: phrase (8 bytes)\n"
178 " d: double phrase (16 bytes)\n"
179 " q: quad phrase (32 bytes)\n"
180 " -s Warn about possible short branches\n"
181 " and applied optimisations\n"
182 " -u Force referenced and undefined symbols global\n"
183 " -v Set verbose mode\n"
184 " -x Turn on debugging mode\n"
185 " -y[pagelen] Set page line length (default: 61)\n"
191 // Display version information
193 void DisplayVersion(void)
196 " _ __ _ __ ___ __ _ ___ \n"
197 "| '__| '_ ` _ \\ / _` |/ __|\n"
198 "| | | | | | | | (_| | (__ \n"
199 "|_| |_| |_| |_|\\__,_|\\___|\n"
200 "\nReboot's Macro Assembler\n"
201 "Copyright (C) 199x Landon Dyer, 2011-2017 Reboot\n"
202 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
207 // Parse optimisation options
209 int ParseOptimization(char * optstring)
213 if (*optstring == '+')
215 else if (*optstring != '~')
218 if ((optstring[2] == 'a' || optstring[2] == 'A')
219 && (optstring[3] == 'l' || optstring[3] == 'L')
220 && (optstring[4] == 'l' || optstring[4] == 'L'))
222 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
225 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
227 int opt_no = atoi(&optstring[2]);
229 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
231 optim_flags[opt_no] = onoff;
245 // Process command line arguments and do an assembly
247 int Process(int argc, char ** argv)
249 int argno; // Argument number
250 SYM * sy; // Pointer to a symbol record
251 char * s; // String pointer
252 int fd; // File descriptor
253 char fnbuf[FNSIZ]; // Filename buffer
256 errcnt = 0; // Initialise error count
257 listing = 0; // Initialise listing level
258 list_flag = 0; // Initialise listing flag
259 verb_flag = perm_verb_flag; // Initialise verbose flag
260 as68_flag = 0; // Initialise as68 kludge mode
261 glob_flag = 0; // Initialise .globl flag
262 sbra_flag = 0; // Initialise short branch flag
263 debug = 0; // Initialise debug flag
264 searchpath = NULL; // Initialise search path
265 objfname = NULL; // Initialise object filename
266 list_fname = NULL; // Initialise listing filename
267 err_fname = NULL; // Initialise error filename
268 obj_format = BSD; // Initialise object format
269 firstfname = NULL; // Initialise first filename
270 err_fd = ERROUT; // Initialise error file descriptor
271 err_flag = 0; // Initialise error flag
272 rgpu = 0; // Initialise GPU assembly flag
273 rdsp = 0; // Initialise DSP assembly flag
274 lsym_flag = 1; // Include local symbols in object file
275 regbank = BANK_N; // No RISC register bank specified
276 orgactive = 0; // Not in RISC org section
277 orgwarning = 0; // No ORG warning issued
278 segpadsize = 2; // Initialise segment padding size
279 m6502 = 0; // 6502 mode off by default
281 // Initialise modules
282 InitSymbolTable(); // Symbol table
283 InitTokenizer(); // Tokenizer
284 InitLineProcessor(); // Line processor
285 InitExpression(); // Expression analyzer
286 InitSection(); // Section manager / code generator
287 InitMark(); // Mark tape-recorder
288 InitMacro(); // Macro processor
289 InitListing(); // Listing generator
290 Init6502(); // 6502 assembler
292 // Process command line arguments and assemble source files
293 for(argno=0; argno<argc; argno++)
295 if (*argv[argno] == '-')
297 switch (argv[argno][1])
299 case 'd': // Define symbol
301 for(s=argv[argno]+2; *s!=EOS;)
310 if (argv[argno][2] == EOS)
312 printf("-d: empty symbol\n");
317 sy = lookup(argv[argno] + 2, 0, 0);
321 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
325 sy->sattr = DEFINED | EQUATED | ABS;
326 sy->svalue = (*s ? (uint32_t)atoi(s) : 0);
328 case 'e': // Redirect error message output
330 err_fname = argv[argno] + 2;
332 case 'f': // -f<format>
334 switch (argv[argno][2])
337 case 'a': // -fa = Alcyon [the default]
341 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
345 case 'e': // -fe = ELF
349 case 'x': // -fx = COM/EXE/XEX
354 printf("-f: unknown object format specified\n");
359 case 'g': // Debugging flag
361 printf("Debugging flag (-g) not yet implemented\n");
363 case 'i': // Set directory search path
365 searchpath = argv[argno] + 2;
367 case 'l': // Produce listing file
369 if (*(argv[argno] + 2) == '*')
371 list_fname = argv[argno] + 3;
372 list_pag = 0; // Special case - turn off pagination
376 list_fname = argv[argno] + 2;
385 if (strcmp(argv[argno] + 2, "68000") == 0)
387 else if (strcmp(argv[argno] + 2, "68020") == 0)
389 else if (strcmp(argv[argno] + 2, "68030") == 0)
391 else if (strcmp(argv[argno] + 2, "68040") == 0)
393 else if (strcmp(argv[argno] + 2, "68060") == 0)
395 else if (strcmp(argv[argno] + 2, "68881") == 0)
397 else if (strcmp(argv[argno] + 2, "68882") == 0)
399 else if (strcmp(argv[argno] + 2, "56001") == 0)
401 else if (strcmp(argv[argno] + 2, "6502") == 0)
403 else if (strcmp(argv[argno] + 2, "tom") == 0)
405 else if (strcmp(argv[argno] + 2, "jerry") == 0)
409 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
414 case 'o': // Direct object file output
416 if (argv[argno][2] != EOS)
417 objfname = argv[argno] + 2;
422 printf("Missing argument to -o");
427 objfname = argv[argno];
431 case 'p': /* -p: generate ".PRG" executable output */
434 * -p .PRG generation w/o symbols
435 * -ps .PRG generation with symbols
436 * -px .PRG generation with extended symbols
438 switch (argv[argno][2])
455 printf("-p: syntax error\n");
460 // Enforce Alcyon object format - kind of silly
461 // to ask for .prg output without it!
464 case 'r': // Pad seg to requested boundary size
466 switch(argv[argno][2])
468 case 'w': case 'W': segpadsize = 2; break;
469 case 'l': case 'L': segpadsize = 4; break;
470 case 'p': case 'P': segpadsize = 8; break;
471 case 'd': case 'D': segpadsize = 16; break;
472 case 'q': case 'Q': segpadsize = 32; break;
473 default: segpadsize = 2; break; // Effective autoeven();
476 case 's': // Warn about possible short branches
480 case 'u': // Make undefined symbols .globl
484 case 'v': // Verbose flag
492 case 'x': // Turn on debugging
495 printf("~ Debugging ON\n");
497 case 'y': // -y<pagelen>
499 pagelen = atoi(argv[argno] + 2);
503 printf("-y: bad page length\n");
509 case EOS: // Input is stdin
510 if (firstfname == NULL) // Kludge first filename
511 firstfname = defname;
513 include(0, "(stdin)");
516 case 'h': // Display command line usage
523 case 'n': // Turn off legacy mode
526 printf("Legacy mode OFF\n");
530 printf("Unknown switch: %s\n\n", argv[argno]);
536 else if (*argv[argno] == '+' || *argv[argno] == '~')
538 if (ParseOptimization(argv[argno]) != OK)
541 printf("Unknown switch: %s\n\n", argv[argno]);
549 // Record first filename.
550 if (firstfname == NULL)
551 firstfname = argv[argno];
553 strcpy(fnbuf, argv[argno]);
554 fext(fnbuf, ".s", 0);
559 printf("Cannot open: %s\n", fnbuf);
569 // Wind-up processing;
570 // o save current section (no more code generation)
571 // o do auto-even of all sections (or boundary alignment as requested
573 // o save the last pc value for 6502 (if we used 6502 at all) and
574 // detemine if the last section contains anything
575 // o determine name of object file:
576 // - "foo.o" for linkable output;
577 // - "foo.prg" for GEMDOS executable (-p flag).
580 for(i=TEXT; i<=BSS; i<<=1)
586 case 2: d_even(); break;
587 case 4: d_long(); break;
588 case 8: d_phrase(); break;
589 case 16: d_dphrase(); break;
590 case 32: d_qphrase(); break;
596 SwitchSection(M6502);
598 if (sloc != currentorg[0])
600 currentorg[1] = sloc;
604 if (objfname == NULL)
606 if (firstfname == NULL)
607 firstfname = defname;
609 strcpy(fnbuf, firstfname);
610 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
614 // With one pass finished, go back and:
615 // (1) run through all the fixups and resolve forward references;
616 // (1.5) ensure that remaining fixups can be handled by the linker
617 // (`lo68' format, extended (postfix) format....)
618 // (2) generate the output file image and symbol table;
619 // (3) generate relocation information from left-over fixups.
620 ResolveAllFixups(); // Do all fixups
621 StopMark(); // Stop mark tape-recorder
625 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
630 s = (prg_flag ? "executable" : "object");
631 printf("[Writing %s file: %s]\n", s, objfname);
644 printf("[Wrapping-up listing file]\n");
654 DEBUG dump_everything();
661 // Determine processor endianess
663 int GetEndianess(void)
666 char * p = (char *)&i;
676 // Application entry point
678 int main(int argc, char ** argv)
680 perm_verb_flag = 0; // Clobber "permanent" verbose flag
681 legacy_flag = 1; // Default is legacy mode on (:-P)
683 // Set legacy optimisation flags to on and everything else to off
684 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
685 optim_flags[OPT_ABS_SHORT] =
686 optim_flags[OPT_MOVEL_MOVEQ] =
687 optim_flags[OPT_BSR_BCC_S] = 1;
689 cmdlnexec = argv[0]; // Obtain executable name
690 endian = GetEndianess(); // Get processor endianess
692 // If commands were passed in, process them
694 return Process(argc - 1, argv + 1);