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 legacy_flag; // Do stuff like insert code in RISC assembler
33 int obj_format; // Object format flag
34 int debug; // [1..9] Enable debugging levels
35 int err_flag; // '-e' specified
36 int err_fd; // File to write error messages to
37 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
38 int list_fd; // File to write listing to
39 int regbank; // RISC register bank
40 int segpadsize; // Segment padding size
41 int endian; // Host processor endianess
42 char * objfname; // Object filename pointer
43 char * firstfname; // First source filename
44 char * cmdlnexec; // Executable name, pointer to ARGV[0]
45 char * searchpath; // Search path for include files
46 char defname[] = "noname.o"; // Default output filename
50 // Copy stuff around, return pointer to dest+count+1 (doesn't handle overlap)
52 char * copy(char * dest, char * src, LONG count)
62 // Clear a region of memory
64 void clear(char * dest, LONG count)
72 // Check to see if the string is a keyword. Returns -1, or a value from the
75 int kmatch(char * p, int * base, int * check, int * tab, int * accept)
80 for(state=0; state>=0;)
82 j = base[state] + (int)tolowertab[*p];
84 if (check[j] != state)
85 { // Reject, character doesn't match
86 state = -1; // No match
91 { // Must accept or reject at EOS
92 state = accept[j]; // (-1 on no terminal match)
104 // Auto-even a section
106 void autoeven(int sect)
115 // Manipulate file extension.
116 // `name' must be large enough to hold any possible filename.
117 // If `stripp' is nonzero, any old extension is removed.
118 // Then, if the file does not already have an extension,
119 // `extension' is appended to the filename.
121 char * fext(char * name, char * extension, int stripp)
123 char * s, * beg; // String pointers
125 // Find beginning of "real" name
126 beg = name + strlen(name) - 1;
128 for(; beg>name; --beg)
130 if (*beg == SLASHCHAR)
138 { // Clobber any old extension
139 for(s=beg; *s && *s!='.'; ++s)
145 for(s=beg; *s!='.'; ++s)
148 { // Append the new extension
149 strcat(beg, extension);
159 // Return `item'nth element of semicolon-seperated pathnames specified in the
160 // enviroment string `s'. Copy the pathname to `buf'. Return 0 if the `item'
161 // nth path doesn't exist.
163 // [`item' ranges from 0 to N-1, where N = #elements in search path]
165 int nthpath(char * env_var, int itemno, char * buf)
167 char * s = searchpath;
176 while (*s != EOS && *s++ != ';')
182 while (*s != EOS && *s != ';')
192 // Display Command Line Help
194 void display_help(void)
197 " %s [options] srcfile\n"
200 " -? or -h Display usage information\n"
201 " -dsymbol[=value] Define symbol\n"
202 " -e[errorfile] Send error messages to file, not stdout\n"
203 " -f[format] Output object file format\n"
204 " b: BSD (use this for Jaguar)\n"
205 " -i[path] Directory to search for include files\n"
206 " -l[filename] Create an output listing file\n"
207 " -n Don't do things behind your back in RISC assembler\n"
208 " -o file Output file name\n"
209 " -r[size] Pad segments to boundary size specified\n"
210 " w: word (2 bytes, default alignment)\n"
211 " l: long (4 bytes)\n"
212 " p: phrase (8 bytes)\n"
213 " d: double phrase (16 bytes)\n"
214 " q: quad phrase (32 bytes)\n"
215 " -s Warn about possible short branches\n"
216 " -u Force referenced and undefined symbols global\n"
217 " -v Set verbose mode\n"
218 " -y[pagelen] Set page line length (default: 61)\n"
224 // Display Version Information
226 void display_version(void)
228 printf("\nReboot's Macro Assembler for Atari Jaguar\n"
229 "Copyright (C) 199x Landon Dyer, 2011 Reboot\n"
230 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
235 // Process Command Line Arguments and do an Assembly
237 int process(int argc, char ** argv)
239 int argno; // Argument number
240 SYM * sy; // Pointer to a symbol record
241 char * s; // String pointer
242 int fd; // File descriptor
243 char fnbuf[FNSIZ]; // Filename buffer
246 errcnt = 0; // Initialise error count
247 listing = 0; // Initialise listing level
248 list_flag = 0; // Initialise listing flag
249 verb_flag = perm_verb_flag; // Initialise verbose flag
250 as68_flag = 0; // Initialise as68 kludge mode
251 glob_flag = 0; // Initialise .globl flag
252 sbra_flag = 0; // Initialise short branch flag
253 debug = 0; // Initialise debug flag
254 searchpath = NULL; // Initialise search path
255 objfname = NULL; // Initialise object filename
256 list_fname = NULL; // Initialise listing filename
257 err_fname = NULL; // Initialise error filename
258 obj_format = BSD; // Initialise object format
259 firstfname = NULL; // Initialise first filename
260 err_fd = ERROUT; // Initialise error file descriptor
261 err_flag = 0; // Initialise error flag
262 rgpu = 0; // Initialise GPU assembly flag
263 rdsp = 0; // Initialise DSP assembly flag
264 lsym_flag = 1; // Include local symbols in object file
265 regbank = BANK_N; // No RISC register bank specified
266 orgactive = 0; // Not in RISC org section
267 orgwarning = 0; // No ORG warning issued
268 segpadsize = 2; // Initialise segment padding size
270 // Initialise modules
271 InitSymbolTable(); // Symbol table
272 InitTokenizer(); // Tokenizer
273 InitLineProcessor(); // Line processor
274 InitExpression(); // Expression analyzer
275 InitSection(); // Section manager / code generator
276 InitMark(); // Mark tape-recorder
277 InitMacro(); // Macro processor
278 InitListing(); // Listing generator
280 // Process command line arguments and assemble source files
281 for(argno=0; argno<argc; ++argno)
283 if (*argv[argno] == '-')
285 switch (argv[argno][1])
287 case 'd': // Define symbol
289 for(s=argv[argno]+2; *s!=EOS;)
298 if (argv[argno][2] == EOS)
300 printf("-d: empty symbol\n");
305 sy = lookup(argv[argno] + 2, 0, 0);
309 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
313 sy->sattr = DEFINED | EQUATED | ABS;
316 sy->svalue = (VALUE)atoi(s);
320 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
323 case 'e': // Redirect error message output
325 err_fname = argv[argno] + 2;
327 case 'f': // -f<format>
329 switch (argv[argno][2])
332 case 'b': // -fb = BSD (Jaguar Recommended)
337 printf("-f: unknown object format specified\n");
342 case 'g': // Debugging flag
344 printf("Debugging flag (-g) not yet implemented\n");
346 case 'i': // Set directory search path
348 searchpath = argv[argno] + 2;
350 case 'l': // Produce listing file
352 list_fname = argv[argno] + 2;
357 case 'o': // Direct object file output
359 if (argv[argno][2] != EOS)
360 objfname = argv[argno] + 2;
365 printf("Missing argument to -o");
369 objfname = argv[argno];
373 case 'r': // Pad seg to requested boundary size
375 switch(argv[argno][2])
377 case 'w': case 'W': segpadsize = 2; break;
378 case 'l': case 'L': segpadsize = 4; break;
379 case 'p': case 'P': segpadsize = 8; break;
380 case 'd': case 'D': segpadsize = 16; break;
381 case 'q': case 'Q': segpadsize = 32; break;
382 default: segpadsize = 2; break; // Effective autoeven();
385 case 's': // Warn about possible short branches
389 case 'u': // Make undefined symbols .globl
393 case 'v': // Verbose flag
401 case 'x': // Turn on debugging
404 printf("~ Debugging ON\n");
406 case 'y': // -y<pagelen>
408 pagelen = atoi(argv[argno] + 2);
412 printf("-y: bad page length\n");
418 case EOS: // Input is stdin
419 if (firstfname == NULL) // Kludge first filename
420 firstfname = defname;
422 include(0, "(stdin)");
425 case 'h': // Display command line usage
432 case 'n': // Turn off legacy mode
435 printf("Legacy mode OFF\n");
439 printf("Unknown switch: %s\n\n", argv[argno]);
447 // Record first filename.
448 if (firstfname == NULL)
449 firstfname = argv[argno];
451 strcpy(fnbuf, argv[argno]);
452 fext(fnbuf, ".s", 0);
457 printf("Cannot open: %s\n", fnbuf);
467 // Wind-up processing;
468 // o save current section (no more code generation)
469 // o do auto-even of all sections (or boundary alignment as requested through '-r')
470 // o determine name of object file:
471 // - "foo.o" for linkable output;
472 // - "foo.prg" for GEMDOS executable (-p flag).
475 for(i=TEXT; i<=BSS; i<<=1)
481 case 2: d_even(); break;
482 case 4: d_long(); break;
483 case 8: d_phrase(); break;
484 case 16: d_dphrase(); break;
485 case 32: d_qphrase(); break;
491 if (objfname == NULL)
493 if (firstfname == NULL)
494 firstfname = defname;
496 strcpy(fnbuf, firstfname);
497 //fext(fnbuf, prg_flag ? ".prg" : ".o", 1);
498 fext(fnbuf, ".o", 1);
502 // With one pass finished, go back and:
503 // (1) run through all the fixups and resolve forward references;
504 // (1.5) ensure that remaining fixups can be handled by the linker
505 // (`lo68' format, extended (postfix) format....)
506 // (2) generate the output file image and symbol table;
507 // (3) generate relocation information from left-over fixups.
508 ResolveAllFixups(); // Do all fixups
509 stopmark(); // Stop mark tape-recorder
513 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
519 printf("[Writing %s file: %s]\n", s, objfname);
532 printf("[Wrapping-up listing file]\n");
542 DEBUG dump_everything();
549 // Determine Processor Endianess
551 int get_endianess(void)
554 char * p = (char *)&i;
564 // Application Entry Point; Handle the Command Line
566 int main(int argc, char ** argv)
568 perm_verb_flag = 0; // Clobber "permanent" verbose flag
569 legacy_flag = 1; // Default is legacy mode on (:-P)
570 cmdlnexec = argv[0]; // Obtain executable name
572 endian = get_endianess(); // Get processor endianess
574 // If commands were passed in, process them
577 return process(argc - 1, argv + 1);