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 obj_format; // Object format flag
33 int debug; // [1..9] Enable debugging levels
34 int err_flag; // '-e' specified
35 int err_fd; // File to write error messages to
36 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
37 int list_fd; // File to write listing to
38 int regbank; // RISC register bank
39 int segpadsize; // Segment padding size
40 int endian; // Host processor endianess
41 char * objfname; // Object filename pointer
42 char * firstfname; // First source filename
43 char * cmdlnexec; // Executable name, pointer to ARGV[0]
44 char * searchpath; // Search path for include files
45 char defname[] = "noname.o"; // Default output filename
49 // Copy stuff around, return pointer to dest+count+1 (doesn't handle overlap)
51 char * copy(char * dest, char * src, LONG count)
61 // Clear a region of memory
63 void clear(char * dest, LONG count)
71 // Check to see if the string is a keyword. Returns -1, or a value from the
74 int kmatch(char * p, int * base, int * check, int * tab, int * accept)
79 for(state=0; state>=0;)
81 j = base[state] + (int)tolowertab[*p];
83 if (check[j] != state)
84 { // Reject, character doesn't match
85 state = -1; // No match
90 { // Must accept or reject at EOS
91 state = accept[j]; // (-1 on no terminal match)
103 // Auto-even a section
105 void autoeven(int sect)
114 // Manipulate file extension.
115 // `name' must be large enough to hold any possible filename.
116 // If `stripp' is nonzero, any old extension is removed.
117 // Then, if the file does not already have an extension,
118 // `extension' is appended to the filename.
120 char * fext(char * name, char * extension, int stripp)
122 char * s, * beg; // String pointers
124 // Find beginning of "real" name
125 beg = name + strlen(name) - 1;
127 for(; beg>name; --beg)
129 if (*beg == SLASHCHAR)
137 { // Clobber any old extension
138 for(s=beg; *s && *s!='.'; ++s)
144 for(s=beg; *s!='.'; ++s)
147 { // Append the new extension
148 strcat(beg, extension);
158 // Return `item'nth element of semicolon-seperated pathnames specified in the
159 // enviroment string `s'. Copy the pathname to `buf'. Return 0 if the `item'
160 // nth path doesn't exist.
162 // [`item' ranges from 0 to N-1, where N = #elements in search path]
164 int nthpath(char * env_var, int itemno, char * buf)
166 char * s = searchpath;
175 while (*s != EOS && *s++ != ';')
181 while (*s != EOS && *s != ';')
191 // Display Command Line Help
193 void display_help(void)
196 " %s [options] srcfile\n"
199 " -? or -h Display usage information\n"
200 " -dsymbol[=value] Define symbol\n"
201 " -e[errorfile] Send error messages to file, not stdout\n"
202 " -f[format] Output object file format\n"
203 " b: BSD (use this for Jaguar)\n"
204 " -i[path] Directory to search for include files\n"
205 " -l[filename] Create an output listing file\n"
206 " -o file Output file name\n"
207 " -r[size] Pad segments to boundary size specified\n"
208 " w: word (2 bytes, default alignment)\n"
209 " l: long (4 bytes)\n"
210 " p: phrase (8 bytes)\n"
211 " d: double phrase (16 bytes)\n"
212 " q: quad phrase (32 bytes)\n"
213 " -s Warn about possible short branches\n"
214 " -u Force referenced and undefined symbols global\n"
215 " -v Set verbose mode\n"
216 " -y[pagelen] Set page line length (default: 61)\n"
222 // Display Version Information
224 void display_version(void)
226 printf("\nReboot's Macro Assembler for Atari Jaguar\n"
227 "Copyright (C) 199x Landon Dyer, 2011 Reboot\n"
228 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
233 // Process Command Line Arguments and do an Assembly
235 int process(int argc, char ** argv)
237 int argno; // Argument number
238 SYM * sy; // Pointer to a symbol record
239 char * s; // String pointer
240 int fd; // File descriptor
241 char fnbuf[FNSIZ]; // Filename buffer
244 errcnt = 0; // Initialise error count
245 listing = 0; // Initialise listing level
246 list_flag = 0; // Initialise listing flag
247 verb_flag = perm_verb_flag; // Initialise verbose flag
248 as68_flag = 0; // Initialise as68 kludge mode
249 glob_flag = 0; // Initialise .globl flag
250 sbra_flag = 0; // Initialise short branch flag
251 debug = 0; // Initialise debug flag
252 searchpath = NULL; // Initialise search path
253 objfname = NULL; // Initialise object filename
254 list_fname = NULL; // Initialise listing filename
255 err_fname = NULL; // Initialise error filename
256 obj_format = BSD; // Initialise object format
257 firstfname = NULL; // Initialise first filename
258 err_fd = ERROUT; // Initialise error file descriptor
259 err_flag = 0; // Initialise error flag
260 rgpu = 0; // Initialise GPU assembly flag
261 rdsp = 0; // Initialise DSP assembly flag
262 lsym_flag = 1; // Include local symbols in object file
263 regbank = BANK_N; // No RISC register bank specified
264 orgactive = 0; // Not in RISC org section
265 orgwarning = 0; // No ORG warning issued
266 segpadsize = 2; // Initialise segment padding size
268 // Initialise modules
269 InitSymbolTable(); // Symbol table
270 InitTokenizer(); // Tokenizer
271 InitLineProcessor(); // Line processor
272 InitExpression(); // Expression analyzer
273 InitSection(); // Section manager / code generator
274 InitMark(); // Mark tape-recorder
275 InitMacro(); // Macro processor
276 InitListing(); // Listing generator
278 // Process command line arguments and assemble source files
279 for(argno=0; argno<argc; ++argno)
281 if (*argv[argno] == '-')
283 switch (argv[argno][1])
285 case 'd': // Define symbol
287 for(s=argv[argno]+2; *s!=EOS;)
296 if (argv[argno][2] == EOS)
298 printf("-d: empty symbol\n");
303 sy = lookup(argv[argno] + 2, 0, 0);
307 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
311 sy->sattr = DEFINED | EQUATED | ABS;
314 sy->svalue = (VALUE)atoi(s);
318 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
321 case 'e': // Redirect error message output
323 err_fname = argv[argno] + 2;
325 case 'f': // -f<format>
327 switch (argv[argno][2])
330 case 'b': // -fb = BSD (Jaguar Recommended)
335 printf("-f: unknown object format specified\n");
340 case 'g': // Debugging flag
342 printf("Debugging flag (-g) not yet implemented\n");
344 case 'i': // Set directory search path
346 searchpath = argv[argno] + 2;
348 case 'l': // Produce listing file
350 list_fname = argv[argno] + 2;
355 case 'o': // Direct object file output
357 if (argv[argno][2] != EOS)
358 objfname = argv[argno] + 2;
363 printf("Missing argument to -o");
367 objfname = argv[argno];
371 case 'r': // Pad seg to requested boundary size
373 switch(argv[argno][2])
375 case 'w': case 'W': segpadsize = 2; break;
376 case 'l': case 'L': segpadsize = 4; break;
377 case 'p': case 'P': segpadsize = 8; break;
378 case 'd': case 'D': segpadsize = 16; break;
379 case 'q': case 'Q': segpadsize = 32; break;
380 default: segpadsize = 2; break; // Effective autoeven();
383 case 's': // Warn about possible short branches
387 case 'u': // Make undefined symbols .globl
391 case 'v': // Verbose flag
399 case 'x': // Turn on debugging
402 printf("~ Debugging ON\n");
404 case 'y': // -y<pagelen>
406 pagelen = atoi(argv[argno] + 2);
410 printf("-y: bad page length\n");
416 case EOS: // Input is stdin
417 if (firstfname == NULL) // Kludge first filename
418 firstfname = defname;
420 include(0, "(stdin)");
423 case 'h': // Display command line usage
432 printf("Unknown switch: %s\n\n", argv[argno]);
440 // Record first filename.
441 if (firstfname == NULL)
442 firstfname = argv[argno];
444 strcpy(fnbuf, argv[argno]);
445 fext(fnbuf, ".s", 0);
450 printf("Cannot open: %s\n", fnbuf);
460 // Wind-up processing;
461 // o save current section (no more code generation)
462 // o do auto-even of all sections (or boundary alignment as requested through '-r')
463 // o determine name of object file:
464 // - "foo.o" for linkable output;
465 // - "foo.prg" for GEMDOS executable (-p flag).
468 for(i=TEXT; i<=BSS; i<<=1)
474 case 2: d_even(); break;
475 case 4: d_long(); break;
476 case 8: d_phrase(); break;
477 case 16: d_dphrase(); break;
478 case 32: d_qphrase(); break;
484 if (objfname == NULL)
486 if (firstfname == NULL)
487 firstfname = defname;
489 strcpy(fnbuf, firstfname);
490 //fext(fnbuf, prg_flag ? ".prg" : ".o", 1);
491 fext(fnbuf, ".o", 1);
495 // With one pass finished, go back and:
496 // (1) run through all the fixups and resolve forward references;
497 // (1.5) ensure that remaining fixups can be handled by the linker
498 // (`lo68' format, extended (postfix) format....)
499 // (2) generate the output file image and symbol table;
500 // (3) generate relocation information from left-over fixups.
501 ResolveAllFixups(); // Do all fixups
502 stopmark(); // Stop mark tape-recorder
506 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
512 printf("[Writing %s file: %s]\n", s, objfname);
525 printf("[Wrapping-up listing file]\n");
535 DEBUG dump_everything();
542 // Determine Processor Endianess
544 int get_endianess(void)
547 char * p = (char *)&i;
557 // Application Entry Point; Handle the Command Line
559 int main(int argc, char ** argv)
561 perm_verb_flag = 0; // Clobber "permanent" verbose flag
562 cmdlnexec = argv[0]; // Obtain executable name
564 endian = get_endianess(); // Get processor endianess
566 // If commands were passed in, process them
569 return process(argc - 1, argv + 1);