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 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)
58 // Manipulate file extension.
60 // 'name' must be large enough to hold any possible filename. If 'stripp' is
61 // nonzero, any old extension is removed. If the file does not already have an
62 // extension, 'extension' is appended to the filename.
64 char * fext(char * name, char * extension, int stripp)
68 // Find beginning of "real" name (strip off path)
69 char * beg = strrchr(name, SLASHCHAR);
74 // Clobber any old extension, if requested
77 for(s=beg; *s && *s!='.'; ++s)
83 if (strrchr(beg, '.') == NULL)
84 strcat(beg, extension);
91 // Return 'item'nth element of semicolon-seperated pathnames specified in the
92 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
93 // nth path doesn't exist.
95 // ['item' ranges from 0 to N-1, where N = #elements in search path]
97 int nthpath(char * env_var, int itemno, char * buf)
99 char * s = searchpath;
108 while (*s != EOS && *s++ != ';')
114 while (*s != EOS && *s != ';')
124 // Display command line help
126 void DisplayHelp(void)
129 " %s [options] srcfile\n"
132 " -? or -h Display usage information\n"
133 " -dsymbol[=value] Define symbol (with optional value, default=0)\n"
134 " -e[errorfile] Send error messages to file, not stdout\n"
135 " -f[format] Output object file format\n"
136 " a: ALCYON (use this for ST)\n"
137 " b: BSD (use this for Jaguar)\n"
139 " x: com/exe/xex (Atari 800)\n"
140 " -i[path] Directory to search for include files\n"
141 " -l[filename] Create an output listing file\n"
142 " -l*[filename] Create an output listing file without pagination\n"
143 " -m[cpu] Select default CPU. Available options:\n"
152 " -n Don't do things behind your back in RISC assembler\n"
153 " -o file Output file name\n"
154 " +o[value] Turn a specific optimisation on\n"
155 " Available optimisation values and default settings:\n"
156 " o0: Absolute long adddresses to word (on)\n"
157 " o1: move.l #x,dn/an to moveq (on)\n"
158 " o2: Word branches to short (on)\n"
159 " o3: Outer displacement 0(an) to (an) (off)\n"
160 " o4: lea size(An),An to addq #size,An (off)\n"
161 " o5: Absolute long base displacement to word (off)\n"
162 " o6: Null branches to NOP (off)\n"
163 " o7: clr.l Dx to moveq #0,Dx (off)\n"
164 " ~o[value] Turn a specific optimisation off\n"
165 " +oall Turn all optimisations on\n"
166 " ~oall Turn all optimisations off\n"
167 " -p Create an ST .prg (without symbols)\n"
168 " -ps Create an ST .prg (with symbols)\n"
170 " -r[size] Pad segments to boundary size specified\n"
171 " w: word (2 bytes, default alignment)\n"
172 " l: long (4 bytes)\n"
173 " p: phrase (8 bytes)\n"
174 " d: double phrase (16 bytes)\n"
175 " q: quad phrase (32 bytes)\n"
176 " -s Warn about possible short branches\n"
177 " and applied optimisations\n"
178 " -u Force referenced and undefined symbols global\n"
179 " -v Set verbose mode\n"
180 " -x Turn on debugging mode\n"
181 " -y[pagelen] Set page line length (default: 61)\n"
187 // Display version information
189 void DisplayVersion(void)
192 " _ __ _ __ ___ __ _ ___ \n"
193 "| '__| '_ ` _ \\ / _` |/ __|\n"
194 "| | | | | | | | (_| | (__ \n"
195 "|_| |_| |_| |_|\\__,_|\\___|\n"
196 "\nReboot's Macro Assembler\n"
197 "Copyright (C) 199x Landon Dyer, 2011-2017 Reboot\n"
198 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
203 // Parse optimisation options
205 int ParseOptimization(char * optstring)
209 if (*optstring == '+')
211 else if (*optstring != '~')
214 if ((optstring[2] == 'a' || optstring[2] == 'A')
215 && (optstring[3] == 'l' || optstring[3] == 'L')
216 && (optstring[4] == 'l' || optstring[4] == 'L'))
218 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
221 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
223 int opt_no = atoi(&optstring[2]);
225 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
227 optim_flags[opt_no] = onoff;
241 // Process command line arguments and do an assembly
243 int Process(int argc, char ** argv)
245 int argno; // Argument number
246 SYM * sy; // Pointer to a symbol record
247 char * s; // String pointer
248 int fd; // File descriptor
249 char fnbuf[FNSIZ]; // Filename buffer
252 errcnt = 0; // Initialise error count
253 listing = 0; // Initialise listing level
254 list_flag = 0; // Initialise listing flag
255 verb_flag = perm_verb_flag; // Initialise verbose flag
256 as68_flag = 0; // Initialise as68 kludge mode
257 glob_flag = 0; // Initialise .globl flag
258 sbra_flag = 0; // Initialise short branch flag
259 debug = 0; // Initialise debug flag
260 searchpath = NULL; // Initialise search path
261 objfname = NULL; // Initialise object filename
262 list_fname = NULL; // Initialise listing filename
263 err_fname = NULL; // Initialise error filename
264 obj_format = BSD; // Initialise object format
265 firstfname = NULL; // Initialise first filename
266 err_fd = ERROUT; // Initialise error file descriptor
267 err_flag = 0; // Initialise error flag
268 rgpu = 0; // Initialise GPU assembly flag
269 rdsp = 0; // Initialise DSP assembly flag
270 lsym_flag = 1; // Include local symbols in object file
271 regbank = BANK_N; // No RISC register bank specified
272 orgactive = 0; // Not in RISC org section
273 orgwarning = 0; // No ORG warning issued
274 segpadsize = 2; // Initialise segment padding size
275 m6502 = 0; // 6502 mode off by default
277 // Initialise modules
278 InitSymbolTable(); // Symbol table
279 InitTokenizer(); // Tokenizer
280 InitLineProcessor(); // Line processor
281 InitExpression(); // Expression analyzer
282 InitSection(); // Section manager / code generator
283 InitMark(); // Mark tape-recorder
284 InitMacro(); // Macro processor
285 InitListing(); // Listing generator
286 Init6502(); // 6502 assembler
288 // Process command line arguments and assemble source files
289 for(argno=0; argno<argc; argno++)
291 if (*argv[argno] == '-')
293 switch (argv[argno][1])
295 case 'd': // Define symbol
297 for(s=argv[argno]+2; *s!=EOS;)
306 if (argv[argno][2] == EOS)
308 printf("-d: empty symbol\n");
313 sy = lookup(argv[argno] + 2, 0, 0);
317 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
321 sy->sattr = DEFINED | EQUATED | ABS;
322 sy->svalue = (*s ? (uint32_t)atoi(s) : 0);
324 case 'e': // Redirect error message output
326 err_fname = argv[argno] + 2;
328 case 'f': // -f<format>
330 switch (argv[argno][2])
333 case 'a': // -fa = Alcyon [the default]
337 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
341 case 'e': // -fe = ELF
345 case 'x': // -fx = COM/EXE/XEX
350 printf("-f: unknown object format specified\n");
355 case 'g': // Debugging flag
357 printf("Debugging flag (-g) not yet implemented\n");
359 case 'i': // Set directory search path
361 searchpath = argv[argno] + 2;
363 case 'l': // Produce listing file
365 if (*(argv[argno] + 2) == '*')
367 list_fname = argv[argno] + 3;
368 list_pag = 0; // Special case - turn off pagination
372 list_fname = argv[argno] + 2;
381 if (strcmp(argv[argno] + 2, "68000") == 0)
383 else if (strcmp(argv[argno] + 2, "68020") == 0)
385 else if (strcmp(argv[argno] + 2, "68030") == 0)
387 else if (strcmp(argv[argno] + 2, "68040") == 0)
389 else if (strcmp(argv[argno] + 2, "68060") == 0)
391 else if (strcmp(argv[argno] + 2, "68881") == 0)
393 else if (strcmp(argv[argno] + 2, "68882") == 0)
395 else if (strcmp(argv[argno] + 2, "56001") == 0)
397 else if (strcmp(argv[argno] + 2, "6502") == 0)
399 else if (strcmp(argv[argno] + 2, "tom") == 0)
401 else if (strcmp(argv[argno] + 2, "jerry") == 0)
405 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
410 case 'o': // Direct object file output
412 if (argv[argno][2] != EOS)
413 objfname = argv[argno] + 2;
418 printf("Missing argument to -o");
423 objfname = argv[argno];
427 case 'p': /* -p: generate ".PRG" executable output */
430 * -p .PRG generation w/o symbols
431 * -ps .PRG generation with symbols
433 switch (argv[argno][2])
445 printf("-p: syntax error\n");
450 // Enforce Alcyon object format - kind of silly
451 // to ask for .prg output without it!
454 case 'r': // Pad seg to requested boundary size
456 switch(argv[argno][2])
458 case 'w': case 'W': segpadsize = 2; break;
459 case 'l': case 'L': segpadsize = 4; break;
460 case 'p': case 'P': segpadsize = 8; break;
461 case 'd': case 'D': segpadsize = 16; break;
462 case 'q': case 'Q': segpadsize = 32; break;
463 default: segpadsize = 2; break; // Effective autoeven();
466 case 's': // Warn about possible short branches
470 case 'u': // Make undefined symbols .globl
474 case 'v': // Verbose flag
482 case 'x': // Turn on debugging
485 printf("~ Debugging ON\n");
487 case 'y': // -y<pagelen>
489 pagelen = atoi(argv[argno] + 2);
493 printf("-y: bad page length\n");
499 case EOS: // Input is stdin
500 if (firstfname == NULL) // Kludge first filename
501 firstfname = defname;
503 include(0, "(stdin)");
506 case 'h': // Display command line usage
513 case 'n': // Turn off legacy mode
516 printf("Legacy mode OFF\n");
520 printf("Unknown switch: %s\n\n", argv[argno]);
526 else if (*argv[argno] == '+' || *argv[argno] == '~')
528 if (ParseOptimization(argv[argno]) != OK)
531 printf("Unknown switch: %s\n\n", argv[argno]);
539 // Record first filename.
540 if (firstfname == NULL)
541 firstfname = argv[argno];
543 strcpy(fnbuf, argv[argno]);
544 fext(fnbuf, ".s", 0);
549 printf("Cannot open: %s\n", fnbuf);
559 // Wind-up processing;
560 // o save current section (no more code generation)
561 // o do auto-even of all sections (or boundary alignment as requested
563 // o save the last pc value for 6502 (if we used 6502 at all) and
564 // detemine if the last section contains anything
565 // o determine name of object file:
566 // - "foo.o" for linkable output;
567 // - "foo.prg" for GEMDOS executable (-p flag).
570 for(i=TEXT; i<=BSS; i<<=1)
576 case 2: d_even(); break;
577 case 4: d_long(); break;
578 case 8: d_phrase(); break;
579 case 16: d_dphrase(); break;
580 case 32: d_qphrase(); break;
586 SwitchSection(M6502);
588 if (sloc != currentorg[0])
590 currentorg[1] = sloc;
594 if (objfname == NULL)
596 if (firstfname == NULL)
597 firstfname = defname;
599 strcpy(fnbuf, firstfname);
600 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
604 // With one pass finished, go back and:
605 // (1) run through all the fixups and resolve forward references;
606 // (1.5) ensure that remaining fixups can be handled by the linker
607 // (`lo68' format, extended (postfix) format....)
608 // (2) generate the output file image and symbol table;
609 // (3) generate relocation information from left-over fixups.
610 ResolveAllFixups(); // Do all fixups
611 StopMark(); // Stop mark tape-recorder
615 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
620 s = (prg_flag ? "executable" : "object");
621 printf("[Writing %s file: %s]\n", s, objfname);
634 printf("[Wrapping-up listing file]\n");
644 DEBUG dump_everything();
651 // Determine processor endianess
653 int GetEndianess(void)
656 char * p = (char *)&i;
666 // Application entry point
668 int main(int argc, char ** argv)
670 perm_verb_flag = 0; // Clobber "permanent" verbose flag
671 legacy_flag = 1; // Default is legacy mode on (:-P)
673 // Set legacy optimisation flags to on and everything else to off
674 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
675 optim_flags[OPT_ABS_SHORT] =
676 optim_flags[OPT_MOVEL_MOVEQ] =
677 optim_flags[OPT_BSR_BCC_S] = 1;
679 cmdlnexec = argv[0]; // Obtain executable name
680 endian = GetEndianess(); // Get processor endianess
682 // If commands were passed in, process them
684 return Process(argc - 1, argv + 1);