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 list_fd; // File to write listing to
43 int regbank; // RISC register bank
44 int segpadsize; // Segment padding size
45 int endian; // Host processor endianess (0 = LE, 1 = BE)
46 char * objfname; // Object filename pointer
47 char * firstfname; // First source filename
48 char * cmdlnexec; // Executable name, pointer to ARGV[0]
49 char * searchpath; // Search path for include files
50 char defname[] = "noname.o"; // Default output filename
51 int optim_flags[OPT_COUNT]; // Specific optimisations on/off matrix
54 // Manipulate file extension.
56 // 'name' must be large enough to hold any possible filename. If 'stripp' is
57 // nonzero, any old extension is removed. If the file does not already have an
58 // extension, 'extension' is appended to the filename.
60 char * fext(char * name, char * extension, int stripp)
64 // Find beginning of "real" name (strip off path)
65 char * beg = strrchr(name, SLASHCHAR);
70 // Clobber any old extension, if requested
73 for(s=beg; *s && *s!='.'; ++s)
79 if (strrchr(beg, '.') == NULL)
80 strcat(beg, extension);
87 // Return 'item'nth element of semicolon-seperated pathnames specified in the
88 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
89 // nth path doesn't exist.
91 // ['item' ranges from 0 to N-1, where N = #elements in search path]
93 int nthpath(char * env_var, int itemno, char * buf)
95 char * s = searchpath;
104 while (*s != EOS && *s++ != ';')
110 while (*s != EOS && *s != ';')
120 // Display command line help
122 void DisplayHelp(void)
125 " %s [options] srcfile\n"
128 " -? or -h Display usage information\n"
129 " -dsymbol[=value] Define symbol\n"
130 " -e[errorfile] Send error messages to file, not stdout\n"
131 " -f[format] Output object file format\n"
132 " a: ALCYON (use this for ST)\n"
133 " b: BSD (use this for Jaguar)\n"
135 " x: com/exe/xex (Atari 800)\n"
136 " -i[path] Directory to search for include files\n"
137 " -l[filename] Create an output listing file\n"
138 " -l*[filename] Create an output listing file without pagination\n"
139 " -n Don't do things behind your back in RISC assembler\n"
140 " -o file Output file name\n"
141 " +o[value] Turn a specific optimisation on\n"
142 " Available optimisation values and default settings:\n"
143 " o0: Absolute long adddresses to word (on)\n"
144 " o1: move.l #x,dn/an to moveq (on)\n"
145 " o2: Word branches to short (on)\n"
146 " o3: Outer displacement 0(an) to (an) (off)\n"
147 " ~o[value] Turn a specific optimisation off\n"
148 " +oall Turn all optimisations on\n"
149 " ~oall Turn all optimisations off\n"
150 " -p Create an ST .prg (without symbols)\n"
151 " -ps Create an ST .prg (with symbols)\n"
153 " -r[size] Pad segments to boundary size specified\n"
154 " w: word (2 bytes, default alignment)\n"
155 " l: long (4 bytes)\n"
156 " p: phrase (8 bytes)\n"
157 " d: double phrase (16 bytes)\n"
158 " q: quad phrase (32 bytes)\n"
159 " -s Warn about possible short branches\n"
160 " and applied optimisations\n"
161 " -u Force referenced and undefined symbols global\n"
162 " -v Set verbose mode\n"
163 " -x Turn on debugging mode\n"
164 " -y[pagelen] Set page line length (default: 61)\n"
170 // Display version information
172 void DisplayVersion(void)
175 " _ __ _ __ ___ __ _ ___ \n"
176 "| '__| '_ ` _ \\ / _` |/ __|\n"
177 "| | | | | | | | (_| | (__ \n"
178 "|_| |_| |_| |_|\\__,_|\\___|\n"
179 "\nReboot's Macro Assembler\n"
180 "Copyright (C) 199x Landon Dyer, 2011-2017 Reboot\n"
181 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
186 // Parse optimisation options
188 int ParseOptimization(char * optstring)
192 if (*optstring == '+')
194 else if (*optstring != '~')
197 if ((optstring[2] == 'a' || optstring[2] == 'A')
198 && (optstring[3] == 'l' || optstring[3] == 'L')
199 && (optstring[4] == 'l' || optstring[4] == 'L'))
201 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
204 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
206 int opt_no = atoi(&optstring[2]);
208 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
210 optim_flags[opt_no] = onoff;
224 // Process command line arguments and do an assembly
226 int Process(int argc, char ** argv)
228 int argno; // Argument number
229 SYM * sy; // Pointer to a symbol record
230 char * s; // String pointer
231 int fd; // File descriptor
232 char fnbuf[FNSIZ]; // Filename buffer
235 errcnt = 0; // Initialise error count
236 listing = 0; // Initialise listing level
237 list_flag = 0; // Initialise listing flag
238 verb_flag = perm_verb_flag; // Initialise verbose flag
239 as68_flag = 0; // Initialise as68 kludge mode
240 glob_flag = 0; // Initialise .globl flag
241 sbra_flag = 0; // Initialise short branch flag
242 debug = 0; // Initialise debug flag
243 searchpath = NULL; // Initialise search path
244 objfname = NULL; // Initialise object filename
245 list_fname = NULL; // Initialise listing filename
246 err_fname = NULL; // Initialise error filename
247 obj_format = BSD; // Initialise object format
248 firstfname = NULL; // Initialise first filename
249 err_fd = ERROUT; // Initialise error file descriptor
250 err_flag = 0; // Initialise error flag
251 rgpu = 0; // Initialise GPU assembly flag
252 rdsp = 0; // Initialise DSP assembly flag
253 lsym_flag = 1; // Include local symbols in object file
254 regbank = BANK_N; // No RISC register bank specified
255 orgactive = 0; // Not in RISC org section
256 orgwarning = 0; // No ORG warning issued
257 segpadsize = 2; // Initialise segment padding size
258 m6502 = 0; // 6502 mode off by default
260 // Initialise modules
261 InitSymbolTable(); // Symbol table
262 InitTokenizer(); // Tokenizer
263 InitLineProcessor(); // Line processor
264 InitExpression(); // Expression analyzer
265 InitSection(); // Section manager / code generator
266 InitMark(); // Mark tape-recorder
267 InitMacro(); // Macro processor
268 InitListing(); // Listing generator
269 Init6502(); // 6502 assembler
271 // Process command line arguments and assemble source files
272 for(argno=0; argno<argc; argno++)
274 if (*argv[argno] == '-')
276 switch (argv[argno][1])
278 case 'd': // Define symbol
280 for(s=argv[argno]+2; *s!=EOS;)
289 if (argv[argno][2] == EOS)
291 printf("-d: empty symbol\n");
296 sy = lookup(argv[argno] + 2, 0, 0);
300 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
304 sy->sattr = DEFINED | EQUATED | ABS;
305 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
307 case 'e': // Redirect error message output
309 err_fname = argv[argno] + 2;
311 case 'f': // -f<format>
313 switch (argv[argno][2])
316 case 'a': // -fa = Alcyon [the default]
320 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
324 case 'e': // -fe = ELF
328 case 'x': // -fx = COM/EXE/XEX
333 printf("-f: unknown object format specified\n");
338 case 'g': // Debugging flag
340 printf("Debugging flag (-g) not yet implemented\n");
342 case 'i': // Set directory search path
344 searchpath = argv[argno] + 2;
346 case 'l': // Produce listing file
348 if (*(argv[argno] + 2) == '*')
350 list_fname = argv[argno] + 3;
351 list_pag = 0; // Special case - turn off pagination
355 list_fname = argv[argno] + 2;
361 case 'o': // Direct object file output
363 if (argv[argno][2] != EOS)
364 objfname = argv[argno] + 2;
369 printf("Missing argument to -o");
374 objfname = argv[argno];
378 case 'p': /* -p: generate ".PRG" executable output */
381 * -p .PRG generation w/o symbols
382 * -ps .PRG generation with symbols
384 switch (argv[argno][2])
396 printf("-p: syntax error\n");
401 // Enforce Alcyon object format - kind of silly
402 // to ask for .prg output without it!
405 case 'r': // Pad seg to requested boundary size
407 switch(argv[argno][2])
409 case 'w': case 'W': segpadsize = 2; break;
410 case 'l': case 'L': segpadsize = 4; break;
411 case 'p': case 'P': segpadsize = 8; break;
412 case 'd': case 'D': segpadsize = 16; break;
413 case 'q': case 'Q': segpadsize = 32; break;
414 default: segpadsize = 2; break; // Effective autoeven();
417 case 's': // Warn about possible short branches
421 case 'u': // Make undefined symbols .globl
425 case 'v': // Verbose flag
433 case 'x': // Turn on debugging
436 printf("~ Debugging ON\n");
438 case 'y': // -y<pagelen>
440 pagelen = atoi(argv[argno] + 2);
444 printf("-y: bad page length\n");
450 case EOS: // Input is stdin
451 if (firstfname == NULL) // Kludge first filename
452 firstfname = defname;
454 include(0, "(stdin)");
457 case 'h': // Display command line usage
464 case 'n': // Turn off legacy mode
467 printf("Legacy mode OFF\n");
471 printf("Unknown switch: %s\n\n", argv[argno]);
477 else if (*argv[argno] == '+' || *argv[argno] == '~')
479 if (ParseOptimization(argv[argno]) != OK)
482 printf("Unknown switch: %s\n\n", argv[argno]);
490 // Record first filename.
491 if (firstfname == NULL)
492 firstfname = argv[argno];
494 strcpy(fnbuf, argv[argno]);
495 fext(fnbuf, ".s", 0);
500 printf("Cannot open: %s\n", fnbuf);
510 // Wind-up processing;
511 // o save current section (no more code generation)
512 // o do auto-even of all sections (or boundary alignment as requested
514 // o save the last pc value for 6502 (if we used 6502 at all) and
515 // detemine if the last section contains anything
516 // o determine name of object file:
517 // - "foo.o" for linkable output;
518 // - "foo.prg" for GEMDOS executable (-p flag).
521 for(i=TEXT; i<=BSS; i<<=1)
527 case 2: d_even(); break;
528 case 4: d_long(); break;
529 case 8: d_phrase(); break;
530 case 16: d_dphrase(); break;
531 case 32: d_qphrase(); break;
537 SwitchSection(M6502);
539 if (sloc != currentorg[0])
541 currentorg[1] = sloc;
545 if (objfname == NULL)
547 if (firstfname == NULL)
548 firstfname = defname;
550 strcpy(fnbuf, firstfname);
551 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
555 // With one pass finished, go back and:
556 // (1) run through all the fixups and resolve forward references;
557 // (1.5) ensure that remaining fixups can be handled by the linker
558 // (`lo68' format, extended (postfix) format....)
559 // (2) generate the output file image and symbol table;
560 // (3) generate relocation information from left-over fixups.
561 ResolveAllFixups(); // Do all fixups
562 StopMark(); // Stop mark tape-recorder
566 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
571 s = (prg_flag ? "executable" : "object");
572 printf("[Writing %s file: %s]\n", s, objfname);
585 printf("[Wrapping-up listing file]\n");
595 DEBUG dump_everything();
602 // Determine processor endianess
604 int GetEndianess(void)
607 char * p = (char *)&i;
617 // Application entry point
619 int main(int argc, char ** argv)
621 perm_verb_flag = 0; // Clobber "permanent" verbose flag
622 legacy_flag = 1; // Default is legacy mode on (:-P)
624 // Set legacy optimisation flags to on
625 // and everything else to off
626 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
627 optim_flags[OPT_ABS_SHORT] =
628 optim_flags[OPT_MOVEL_MOVEQ] =
629 optim_flags[OPT_BSR_BCC_S] = 1;
631 cmdlnexec = argv[0]; // Obtain executable name
633 endian = GetEndianess(); // Get processor endianess
635 // If commands were passed in, process them
637 return Process(argc - 1, argv + 1);