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 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
25 int perm_verb_flag; // Permanently verbose, interactive mode
26 int list_flag; // "-l" listing flag on command line
27 int verb_flag; // Be verbose about what's going on
28 int as68_flag; // as68 kludge mode
29 int glob_flag; // Assume undefined symbols are global
30 int lsym_flag; // Include local symbols in object file
31 int sbra_flag; // Warn about possible short branches
32 int prg_flag; // !=0, produce .PRG executable (2=symbols)
33 int legacy_flag; // Do stuff like insert code in RISC assembler
34 int obj_format; // Object format flag
35 int debug; // [1..9] Enable debugging levels
36 int err_flag; // '-e' specified
37 int err_fd; // File to write error messages to
38 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
39 int list_fd; // File to write listing to
40 int regbank; // RISC register bank
41 int segpadsize; // Segment padding size
42 int endian; // Host processor endianess
43 char * objfname; // Object filename pointer
44 char * firstfname; // First source filename
45 char * cmdlnexec; // Executable name, pointer to ARGV[0]
46 char * searchpath; // Search path for include files
47 char defname[] = "noname.o"; // Default output filename
51 // Manipulate file extension.
53 // 'name' must be large enough to hold any possible filename. If 'stripp' is
54 // nonzero, any old extension is removed. If the file does not already have an
55 // extension, 'extension' is appended to the filename.
57 char * fext(char * name, char * extension, int stripp)
61 // Find beginning of "real" name (strip off path)
62 char * beg = strrchr(name, SLASHCHAR);
67 // Clobber any old extension, if requested
70 for(s=beg; *s && *s!='.'; ++s)
76 if (strrchr(beg, '.') == NULL)
77 strcat(beg, extension);
84 // Return 'item'nth element of semicolon-seperated pathnames specified in the
85 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
86 // nth path doesn't exist.
88 // ['item' ranges from 0 to N-1, where N = #elements in search path]
90 int nthpath(char * env_var, int itemno, char * buf)
92 char * s = searchpath;
101 while (*s != EOS && *s++ != ';')
107 while (*s != EOS && *s != ';')
117 // Display command line help
119 void DisplayHelp(void)
122 " %s [options] srcfile\n"
125 " -? or -h Display usage information\n"
126 " -dsymbol[=value] Define symbol\n"
127 " -e[errorfile] Send error messages to file, not stdout\n"
128 " -f[format] Output object file format\n"
129 " a: ALCYON (use this for ST)\n"
130 " b: BSD (use this for Jaguar)\n"
131 " -i[path] Directory to search for include files\n"
132 " -l[filename] Create an output listing file\n"
133 " -n Don't do things behind your back in RISC assembler\n"
134 " -o file Output file name\n"
135 " -r[size] Pad segments to boundary size specified\n"
136 " w: word (2 bytes, default alignment)\n"
137 " l: long (4 bytes)\n"
138 " p: phrase (8 bytes)\n"
139 " d: double phrase (16 bytes)\n"
140 " q: quad phrase (32 bytes)\n"
141 " -s Warn about possible short branches\n"
142 " -u Force referenced and undefined symbols global\n"
143 " -v Set verbose mode\n"
144 " -y[pagelen] Set page line length (default: 61)\n"
150 // Display version information
152 void DisplayVersion(void)
154 printf("\nReboot's Macro Assembler for Atari Jaguar\n"
155 "Copyright (C) 199x Landon Dyer, 2011-2015 Reboot\n"
156 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
161 // Process command line arguments and do an assembly
163 int Process(int argc, char ** argv)
165 int argno; // Argument number
166 SYM * sy; // Pointer to a symbol record
167 char * s; // String pointer
168 int fd; // File descriptor
169 char fnbuf[FNSIZ]; // Filename buffer
172 errcnt = 0; // Initialise error count
173 listing = 0; // Initialise listing level
174 list_flag = 0; // Initialise listing flag
175 verb_flag = perm_verb_flag; // Initialise verbose flag
176 as68_flag = 0; // Initialise as68 kludge mode
177 glob_flag = 0; // Initialise .globl flag
178 sbra_flag = 0; // Initialise short branch flag
179 debug = 0; // Initialise debug flag
180 searchpath = NULL; // Initialise search path
181 objfname = NULL; // Initialise object filename
182 list_fname = NULL; // Initialise listing filename
183 err_fname = NULL; // Initialise error filename
184 obj_format = BSD; // Initialise object format
185 firstfname = NULL; // Initialise first filename
186 err_fd = ERROUT; // Initialise error file descriptor
187 err_flag = 0; // Initialise error flag
188 rgpu = 0; // Initialise GPU assembly flag
189 rdsp = 0; // Initialise DSP assembly flag
190 lsym_flag = 1; // Include local symbols in object file
191 regbank = BANK_N; // No RISC register bank specified
192 orgactive = 0; // Not in RISC org section
193 orgwarning = 0; // No ORG warning issued
194 segpadsize = 2; // Initialise segment padding size
196 // Initialise modules
197 InitSymbolTable(); // Symbol table
198 InitTokenizer(); // Tokenizer
199 InitLineProcessor(); // Line processor
200 InitExpression(); // Expression analyzer
201 InitSection(); // Section manager / code generator
202 InitMark(); // Mark tape-recorder
203 InitMacro(); // Macro processor
204 InitListing(); // Listing generator
206 // Process command line arguments and assemble source files
207 for(argno=0; argno<argc; ++argno)
209 if (*argv[argno] == '-')
211 switch (argv[argno][1])
213 case 'd': // Define symbol
215 for(s=argv[argno]+2; *s!=EOS;)
224 if (argv[argno][2] == EOS)
226 printf("-d: empty symbol\n");
231 sy = lookup(argv[argno] + 2, 0, 0);
235 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
239 sy->sattr = DEFINED | EQUATED | ABS;
240 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
242 case 'e': // Redirect error message output
244 err_fname = argv[argno] + 2;
246 case 'f': // -f<format>
248 switch (argv[argno][2])
251 case 'a': // -fa = Alcyon [the default]
255 case 'b': // -fb = BSD (Jaguar Recommended)
260 printf("-f: unknown object format specified\n");
265 case 'g': // Debugging flag
267 printf("Debugging flag (-g) not yet implemented\n");
269 case 'i': // Set directory search path
271 searchpath = argv[argno] + 2;
273 case 'l': // Produce listing file
275 list_fname = argv[argno] + 2;
280 case 'o': // Direct object file output
282 if (argv[argno][2] != EOS)
283 objfname = argv[argno] + 2;
288 printf("Missing argument to -o");
293 objfname = argv[argno];
297 case 'p': /* -p: generate ".PRG" executable output */
300 * -p .PRG generation w/o symbols
301 * -ps .PRG generation with symbols
303 switch (argv[argno][2])
315 printf("-p: syntax error\n");
320 case 'r': // Pad seg to requested boundary size
322 switch(argv[argno][2])
324 case 'w': case 'W': segpadsize = 2; break;
325 case 'l': case 'L': segpadsize = 4; break;
326 case 'p': case 'P': segpadsize = 8; break;
327 case 'd': case 'D': segpadsize = 16; break;
328 case 'q': case 'Q': segpadsize = 32; break;
329 default: segpadsize = 2; break; // Effective autoeven();
332 case 's': // Warn about possible short branches
336 case 'u': // Make undefined symbols .globl
340 case 'v': // Verbose flag
348 case 'x': // Turn on debugging
351 printf("~ Debugging ON\n");
353 case 'y': // -y<pagelen>
355 pagelen = atoi(argv[argno] + 2);
359 printf("-y: bad page length\n");
365 case EOS: // Input is stdin
366 if (firstfname == NULL) // Kludge first filename
367 firstfname = defname;
369 include(0, "(stdin)");
372 case 'h': // Display command line usage
379 case 'n': // Turn off legacy mode
382 printf("Legacy mode OFF\n");
386 printf("Unknown switch: %s\n\n", argv[argno]);
394 // Record first filename.
395 if (firstfname == NULL)
396 firstfname = argv[argno];
398 strcpy(fnbuf, argv[argno]);
399 fext(fnbuf, ".s", 0);
404 printf("Cannot open: %s\n", fnbuf);
414 // Wind-up processing;
415 // o save current section (no more code generation)
416 // o do auto-even of all sections (or boundary alignment as requested
418 // o determine name of object file:
419 // - "foo.o" for linkable output;
420 // - "foo.prg" for GEMDOS executable (-p flag).
423 for(i=TEXT; i<=BSS; i<<=1)
429 case 2: d_even(); break;
430 case 4: d_long(); break;
431 case 8: d_phrase(); break;
432 case 16: d_dphrase(); break;
433 case 32: d_qphrase(); break;
439 if (objfname == NULL)
441 if (firstfname == NULL)
442 firstfname = defname;
444 strcpy(fnbuf, firstfname);
445 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
446 fext(fnbuf, ".o", 1);
450 // With one pass finished, go back and:
451 // (1) run through all the fixups and resolve forward references;
452 // (1.5) ensure that remaining fixups can be handled by the linker
453 // (`lo68' format, extended (postfix) format....)
454 // (2) generate the output file image and symbol table;
455 // (3) generate relocation information from left-over fixups.
456 ResolveAllFixups(); // Do all fixups
457 StopMark(); // Stop mark tape-recorder
461 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
466 s = (prg_flag ? "executable" : "object");
467 printf("[Writing %s file: %s]\n", s, objfname);
480 printf("[Wrapping-up listing file]\n");
490 DEBUG dump_everything();
497 // Determine processor endianess
499 int GetEndianess(void)
502 char * p = (char *)&i;
512 // Application entry point
514 int main(int argc, char ** argv)
516 perm_verb_flag = 0; // Clobber "permanent" verbose flag
517 legacy_flag = 1; // Default is legacy mode on (:-P)
518 cmdlnexec = argv[0]; // Obtain executable name
520 endian = GetEndianess(); // Get processor endianess
522 // If commands were passed in, process them
524 return Process(argc - 1, argv + 1);