2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2018 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 robjproc; // Assembling Jaguar Object Processor code
44 int dsp56001; // Assembling DSP 56001 code
45 int list_fd; // File to write listing to
46 int regbank; // RISC register bank
47 int segpadsize; // Segment padding size
48 int endian; // Host processor endianess (0 = LE, 1 = BE)
49 char * objfname; // Object filename pointer
50 char * firstfname; // First source filename
51 char * cmdlnexec; // Executable name, pointer to ARGV[0]
52 char * searchpath; // Search path for include files
53 char defname[] = "noname.o"; // Default output filename
54 int optim_flags[OPT_COUNT]; // Specific optimisations on/off matrix
55 int activecpu = CPU_68000; // Active 68k CPU (68000 by default)
56 int activefpu = FPU_NONE; // Active FPU (none by default)
60 // Convert a string to uppercase
62 void strtoupper(char * s)
70 // Manipulate file extension.
72 // 'name' must be large enough to hold any possible filename. If 'stripp' is
73 // nonzero, any old extension is removed. If the file does not already have an
74 // extension, 'extension' is appended to the filename.
76 char * fext(char * name, char * extension, int stripp)
80 // Find beginning of "real" name (strip off path)
81 char * beg = strrchr(name, SLASHCHAR);
86 // Clobber any old extension, if requested
89 for(s=beg; *s && *s!='.'; ++s)
95 if (strrchr(beg, '.') == NULL)
96 strcat(beg, extension);
103 // Return 'item'nth element of semicolon-seperated pathnames specified in the
104 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
105 // nth path doesn't exist.
107 // ['item' ranges from 0 to N-1, where N = #elements in search path]
109 int nthpath(char * env_var, int itemno, char * buf)
111 char * s = searchpath;
120 while (*s != EOS && *s++ != ';')
126 while (*s != EOS && *s != ';')
136 // Display command line help
138 void DisplayHelp(void)
141 " %s [options] srcfile\n"
144 " -? or -h Display usage information\n"
145 " -dsymbol[=value] Define symbol (with optional value, default=0)\n"
146 " -e[errorfile] Send error messages to file, not stdout\n"
147 " -f[format] Output object file format\n"
148 " a: ALCYON (use this for ST)\n"
149 " b: BSD (use this for Jaguar)\n"
151 " x: com/exe/xex (Atari 800)\n"
152 " -i[path] Directory to search for include files\n"
153 " -l[filename] Create an output listing file\n"
154 " -l*[filename] Create an output listing file without pagination\n"
155 " -m[cpu] Select default CPU. Available options:\n"
164 " -n Don't do things behind your back in RISC assembler\n"
165 " -o file Output file name\n"
166 " +o[value] Turn a specific optimisation on\n"
167 " Available optimisation values and default settings:\n"
168 " o0: Absolute long adddresses to word (on)\n"
169 " o1: move.l #x,dn/an to moveq (on)\n"
170 " o2: Word branches to short (on)\n"
171 " o3: Outer displacement 0(an) to (an) (off)\n"
172 " o4: lea size(An),An to addq #size,An (off)\n"
173 " o5: Absolute long base displacement to word (off)\n"
174 " o6: Null branches to NOP (off)\n"
175 " o7: clr.l Dx to moveq #0,Dx (off)\n"
176 " o8: adda.w/l #x,Dy to addq.w/l #x,Dy (off)\n"
177 " o9: adda.w/l #x,Dy to lea x(Dy),Dy (off)\n"
178 " ~o[value] Turn a specific optimisation off\n"
179 " +oall Turn all optimisations on\n"
180 " ~oall Turn all optimisations off\n"
181 " -p Create an ST .prg (without symbols)\n"
182 " -ps Create an ST .prg (with symbols)\n"
183 " -px Create an ST .prg (with exsymbols)\n"
185 " -r[size] Pad segments to boundary size specified\n"
186 " w: word (2 bytes, default alignment)\n"
187 " l: long (4 bytes)\n"
188 " p: phrase (8 bytes)\n"
189 " d: double phrase (16 bytes)\n"
190 " q: quad phrase (32 bytes)\n"
191 " -s Warn about possible short branches\n"
192 " and applied optimisations\n"
193 " -u Force referenced and undefined symbols global\n"
194 " -v Set verbose mode\n"
195 " -x Turn on debugging mode\n"
196 " -y[pagelen] Set page line length (default: 61)\n"
202 // Display version information
204 void DisplayVersion(void)
207 " _ __ _ __ ___ __ _ ___ \n"
208 "| '__| '_ ` _ \\ / _` |/ __|\n"
209 "| | | | | | | | (_| | (__ \n"
210 "|_| |_| |_| |_|\\__,_|\\___|\n"
211 "\nReboot's Macro Assembler\n"
212 "Copyright (C) 199x Landon Dyer, 2011-2018 Reboot\n"
213 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
218 // Parse optimisation options
220 int ParseOptimization(char * optstring)
224 if (*optstring == '+')
226 else if (*optstring != '~')
229 if ((optstring[2] == 'a' || optstring[2] == 'A')
230 && (optstring[3] == 'l' || optstring[3] == 'L')
231 && (optstring[4] == 'l' || optstring[4] == 'L'))
233 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
236 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
238 int opt_no = atoi(&optstring[2]);
240 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
242 optim_flags[opt_no] = onoff;
256 // Process command line arguments and do an assembly
258 int Process(int argc, char ** argv)
260 int argno; // Argument number
261 SYM * sy; // Pointer to a symbol record
262 char * s; // String pointer
263 int fd; // File descriptor
264 char fnbuf[FNSIZ]; // Filename buffer
267 errcnt = 0; // Initialise error count
268 listing = 0; // Initialise listing level
269 list_flag = 0; // Initialise listing flag
270 verb_flag = perm_verb_flag; // Initialise verbose flag
271 as68_flag = 0; // Initialise as68 kludge mode
272 glob_flag = 0; // Initialise .globl flag
273 sbra_flag = 0; // Initialise short branch flag
274 debug = 0; // Initialise debug flag
275 searchpath = NULL; // Initialise search path
276 objfname = NULL; // Initialise object filename
277 list_fname = NULL; // Initialise listing filename
278 err_fname = NULL; // Initialise error filename
279 obj_format = BSD; // Initialise object format
280 firstfname = NULL; // Initialise first filename
281 err_fd = ERROUT; // Initialise error file descriptor
282 err_flag = 0; // Initialise error flag
283 rgpu = 0; // Initialise GPU assembly flag
284 rdsp = 0; // Initialise DSP assembly flag
285 robjproc = 0; // Initialise OP assembly flag
286 lsym_flag = 1; // Include local symbols in object file
287 regbank = BANK_N; // No RISC register bank specified
288 orgactive = 0; // Not in RISC org section
289 orgwarning = 0; // No ORG warning issued
290 segpadsize = 2; // Initialise segment padding size
291 m6502 = 0; // 6502 mode off by default
293 // Initialise modules
294 InitSymbolTable(); // Symbol table
295 InitTokenizer(); // Tokenizer
296 InitLineProcessor(); // Line processor
297 InitExpression(); // Expression analyzer
298 InitSection(); // Section manager / code generator
299 InitMark(); // Mark tape-recorder
300 InitMacro(); // Macro processor
301 InitListing(); // Listing generator
302 Init6502(); // 6502 assembler
304 // Process command line arguments and assemble source files
305 for(argno=0; argno<argc; argno++)
307 if (*argv[argno] == '-')
309 switch (argv[argno][1])
311 case 'd': // Define symbol
313 for(s=argv[argno]+2; *s!=EOS;)
322 if (argv[argno][2] == EOS)
324 printf("-d: empty symbol\n");
329 sy = lookup(argv[argno] + 2, 0, 0);
333 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
337 sy->sattr = DEFINED | EQUATED | ABS;
338 sy->svalue = (*s ? (uint64_t)atoi(s) : 0);
340 case 'e': // Redirect error message output
342 err_fname = argv[argno] + 2;
344 case 'f': // -f<format>
346 switch (argv[argno][2])
349 case 'a': // -fa = Alcyon [the default]
353 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
357 case 'e': // -fe = ELF
361 case 'x': // -fx = COM/EXE/XEX
366 printf("-f: unknown object format specified\n");
371 case 'g': // Debugging flag
373 printf("Debugging flag (-g) not yet implemented\n");
375 case 'i': // Set directory search path
377 searchpath = argv[argno] + 2;
379 case 'l': // Produce listing file
381 if (*(argv[argno] + 2) == '*')
383 list_fname = argv[argno] + 3;
384 list_pag = 0; // Special case - turn off pagination
388 list_fname = argv[argno] + 2;
397 if (strcmp(argv[argno] + 2, "68000") == 0)
399 else if (strcmp(argv[argno] + 2, "68020") == 0)
401 else if (strcmp(argv[argno] + 2, "68030") == 0)
403 else if (strcmp(argv[argno] + 2, "68040") == 0)
405 else if (strcmp(argv[argno] + 2, "68060") == 0)
407 else if (strcmp(argv[argno] + 2, "68881") == 0)
409 else if (strcmp(argv[argno] + 2, "68882") == 0)
411 else if (strcmp(argv[argno] + 2, "56001") == 0)
413 else if (strcmp(argv[argno] + 2, "6502") == 0)
415 else if (strcmp(argv[argno] + 2, "tom") == 0)
417 else if (strcmp(argv[argno] + 2, "jerry") == 0)
421 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
426 case 'o': // Direct object file output
428 if (argv[argno][2] != EOS)
429 objfname = argv[argno] + 2;
434 printf("Missing argument to -o");
439 objfname = argv[argno];
443 case 'p': /* -p: generate ".PRG" executable output */
446 * -p .PRG generation w/o symbols
447 * -ps .PRG generation with symbols
448 * -px .PRG generation with extended symbols
450 switch (argv[argno][2])
467 printf("-p: syntax error\n");
472 // Enforce Alcyon object format - kind of silly
473 // to ask for .prg output without it!
476 case 'r': // Pad seg to requested boundary size
478 switch(argv[argno][2])
480 case 'w': case 'W': segpadsize = 2; break;
481 case 'l': case 'L': segpadsize = 4; break;
482 case 'p': case 'P': segpadsize = 8; break;
483 case 'd': case 'D': segpadsize = 16; break;
484 case 'q': case 'Q': segpadsize = 32; break;
485 default: segpadsize = 2; break; // Effective autoeven();
488 case 's': // Warn about possible short branches
492 case 'u': // Make undefined symbols .globl
496 case 'v': // Verbose flag
504 case 'x': // Turn on debugging
507 printf("~ Debugging ON\n");
509 case 'y': // -y<pagelen>
511 pagelen = atoi(argv[argno] + 2);
515 printf("-y: bad page length\n");
521 case EOS: // Input is stdin
522 if (firstfname == NULL) // Kludge first filename
523 firstfname = defname;
525 include(0, "(stdin)");
528 case 'h': // Display command line usage
535 case 'n': // Turn off legacy mode
538 printf("Legacy mode OFF\n");
542 printf("Unknown switch: %s\n\n", argv[argno]);
548 else if (*argv[argno] == '+' || *argv[argno] == '~')
550 if (ParseOptimization(argv[argno]) != OK)
553 printf("Unknown switch: %s\n\n", argv[argno]);
561 // Record first filename.
562 if (firstfname == NULL)
563 firstfname = argv[argno];
565 strcpy(fnbuf, argv[argno]);
566 fext(fnbuf, ".s", 0);
571 printf("Cannot open: %s\n", fnbuf);
581 // Wind-up processing;
582 // o save current section (no more code generation)
583 // o do auto-even of all sections (or boundary alignment as requested
585 // o save the last pc value for 6502 (if we used 6502 at all) and
586 // detemine if the last section contains anything
587 // o determine name of object file:
588 // - "foo.o" for linkable output;
589 // - "foo.prg" for GEMDOS executable (-p flag).
592 for(i=TEXT; i<=BSS; i<<=1)
598 case 2: d_even(); break;
599 case 4: d_long(); break;
600 case 8: d_phrase(); break;
601 case 16: d_dphrase(); break;
602 case 32: d_qphrase(); break;
608 SwitchSection(M6502);
610 if (sloc != currentorg[0])
612 currentorg[1] = sloc;
616 if (objfname == NULL)
618 if (firstfname == NULL)
619 firstfname = defname;
621 strcpy(fnbuf, firstfname);
622 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
626 // With one pass finished, go back and:
627 // (1) run through all the fixups and resolve forward references;
628 // (1.5) ensure that remaining fixups can be handled by the linker
629 // (`lo68' format, extended (postfix) format....)
630 // (2) generate the output file image and symbol table;
631 // (3) generate relocation information from left-over fixups.
632 ResolveAllFixups(); // Do all fixups
633 StopMark(); // Stop mark tape-recorder
637 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
642 s = (prg_flag ? "executable" : "object");
643 printf("[Writing %s file: %s]\n", s, objfname);
656 printf("[Wrapping-up listing file]\n");
666 DEBUG dump_everything();
673 // Determine processor endianess
675 int GetEndianess(void)
678 char * p = (char *)&i;
688 // Application entry point
690 int main(int argc, char ** argv)
692 perm_verb_flag = 0; // Clobber "permanent" verbose flag
693 legacy_flag = 1; // Default is legacy mode on (:-P)
695 // Set legacy optimisation flags to on and everything else to off
696 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
697 optim_flags[OPT_ABS_SHORT] =
698 optim_flags[OPT_MOVEL_MOVEQ] =
699 optim_flags[OPT_BSR_BCC_S] = 1;
701 cmdlnexec = argv[0]; // Obtain executable name
702 endian = GetEndianess(); // Get processor endianess
704 // If commands were passed in, process them
706 return Process(argc - 1, argv + 1);