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 " -p Create an ST .prg (without symbols)\n"
136 " -ps Create an ST .prg (with symbols)\n"
138 " -r[size] Pad segments to boundary size specified\n"
139 " w: word (2 bytes, default alignment)\n"
140 " l: long (4 bytes)\n"
141 " p: phrase (8 bytes)\n"
142 " d: double phrase (16 bytes)\n"
143 " q: quad phrase (32 bytes)\n"
144 " -s Warn about possible short branches\n"
145 " -u Force referenced and undefined symbols global\n"
146 " -v Set verbose mode\n"
147 " -y[pagelen] Set page line length (default: 61)\n"
153 // Display version information
155 void DisplayVersion(void)
158 " _ __ _ __ ___ __ _ ___ \n"
159 "| '__| '_ ` _ \\ / _` |/ __|\n"
160 "| | | | | | | | (_| | (__ \n"
161 "|_| |_| |_| |_|\\__,_|\\___|\n"
162 "\nReboot's Macro Assembler\n"
163 "Copyright (C) 199x Landon Dyer, 2011-2015 Reboot\n"
164 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
169 // Process command line arguments and do an assembly
171 int Process(int argc, char ** argv)
173 int argno; // Argument number
174 SYM * sy; // Pointer to a symbol record
175 char * s; // String pointer
176 int fd; // File descriptor
177 char fnbuf[FNSIZ]; // Filename buffer
180 errcnt = 0; // Initialise error count
181 listing = 0; // Initialise listing level
182 list_flag = 0; // Initialise listing flag
183 verb_flag = perm_verb_flag; // Initialise verbose flag
184 as68_flag = 0; // Initialise as68 kludge mode
185 glob_flag = 0; // Initialise .globl flag
186 sbra_flag = 0; // Initialise short branch flag
187 debug = 0; // Initialise debug flag
188 searchpath = NULL; // Initialise search path
189 objfname = NULL; // Initialise object filename
190 list_fname = NULL; // Initialise listing filename
191 err_fname = NULL; // Initialise error filename
192 obj_format = BSD; // Initialise object format
193 firstfname = NULL; // Initialise first filename
194 err_fd = ERROUT; // Initialise error file descriptor
195 err_flag = 0; // Initialise error flag
196 rgpu = 0; // Initialise GPU assembly flag
197 rdsp = 0; // Initialise DSP assembly flag
198 lsym_flag = 1; // Include local symbols in object file
199 regbank = BANK_N; // No RISC register bank specified
200 orgactive = 0; // Not in RISC org section
201 orgwarning = 0; // No ORG warning issued
202 segpadsize = 2; // Initialise segment padding size
204 // Initialise modules
205 InitSymbolTable(); // Symbol table
206 InitTokenizer(); // Tokenizer
207 InitLineProcessor(); // Line processor
208 InitExpression(); // Expression analyzer
209 InitSection(); // Section manager / code generator
210 InitMark(); // Mark tape-recorder
211 InitMacro(); // Macro processor
212 InitListing(); // Listing generator
214 // Process command line arguments and assemble source files
215 for(argno=0; argno<argc; ++argno)
217 if (*argv[argno] == '-')
219 switch (argv[argno][1])
221 case 'd': // Define symbol
223 for(s=argv[argno]+2; *s!=EOS;)
232 if (argv[argno][2] == EOS)
234 printf("-d: empty symbol\n");
239 sy = lookup(argv[argno] + 2, 0, 0);
243 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
247 sy->sattr = DEFINED | EQUATED | ABS;
248 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
250 case 'e': // Redirect error message output
252 err_fname = argv[argno] + 2;
254 case 'f': // -f<format>
256 switch (argv[argno][2])
259 case 'a': // -fa = Alcyon [the default]
263 case 'b': // -fb = BSD (Jaguar Recommended)
268 printf("-f: unknown object format specified\n");
273 case 'g': // Debugging flag
275 printf("Debugging flag (-g) not yet implemented\n");
277 case 'i': // Set directory search path
279 searchpath = argv[argno] + 2;
281 case 'l': // Produce listing file
283 list_fname = argv[argno] + 2;
288 case 'o': // Direct object file output
290 if (argv[argno][2] != EOS)
291 objfname = argv[argno] + 2;
296 printf("Missing argument to -o");
301 objfname = argv[argno];
305 case 'p': /* -p: generate ".PRG" executable output */
308 * -p .PRG generation w/o symbols
309 * -ps .PRG generation with symbols
311 switch (argv[argno][2])
323 printf("-p: syntax error\n");
328 // Enforce Alcyon object format - kind of silly
329 // to ask for .prg output without it!
332 case 'r': // Pad seg to requested boundary size
334 switch(argv[argno][2])
336 case 'w': case 'W': segpadsize = 2; break;
337 case 'l': case 'L': segpadsize = 4; break;
338 case 'p': case 'P': segpadsize = 8; break;
339 case 'd': case 'D': segpadsize = 16; break;
340 case 'q': case 'Q': segpadsize = 32; break;
341 default: segpadsize = 2; break; // Effective autoeven();
344 case 's': // Warn about possible short branches
348 case 'u': // Make undefined symbols .globl
352 case 'v': // Verbose flag
360 case 'x': // Turn on debugging
363 printf("~ Debugging ON\n");
365 case 'y': // -y<pagelen>
367 pagelen = atoi(argv[argno] + 2);
371 printf("-y: bad page length\n");
377 case EOS: // Input is stdin
378 if (firstfname == NULL) // Kludge first filename
379 firstfname = defname;
381 include(0, "(stdin)");
384 case 'h': // Display command line usage
391 case 'n': // Turn off legacy mode
394 printf("Legacy mode OFF\n");
398 printf("Unknown switch: %s\n\n", argv[argno]);
406 // Record first filename.
407 if (firstfname == NULL)
408 firstfname = argv[argno];
410 strcpy(fnbuf, argv[argno]);
411 fext(fnbuf, ".s", 0);
416 printf("Cannot open: %s\n", fnbuf);
426 // Wind-up processing;
427 // o save current section (no more code generation)
428 // o do auto-even of all sections (or boundary alignment as requested
430 // o determine name of object file:
431 // - "foo.o" for linkable output;
432 // - "foo.prg" for GEMDOS executable (-p flag).
435 for(i=TEXT; i<=BSS; i<<=1)
441 case 2: d_even(); break;
442 case 4: d_long(); break;
443 case 8: d_phrase(); break;
444 case 16: d_dphrase(); break;
445 case 32: d_qphrase(); break;
451 if (objfname == NULL)
453 if (firstfname == NULL)
454 firstfname = defname;
456 strcpy(fnbuf, firstfname);
457 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
461 // With one pass finished, go back and:
462 // (1) run through all the fixups and resolve forward references;
463 // (1.5) ensure that remaining fixups can be handled by the linker
464 // (`lo68' format, extended (postfix) format....)
465 // (2) generate the output file image and symbol table;
466 // (3) generate relocation information from left-over fixups.
467 ResolveAllFixups(); // Do all fixups
468 StopMark(); // Stop mark tape-recorder
472 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
477 s = (prg_flag ? "executable" : "object");
478 printf("[Writing %s file: %s]\n", s, objfname);
491 printf("[Wrapping-up listing file]\n");
501 DEBUG dump_everything();
508 // Determine processor endianess
510 int GetEndianess(void)
513 char * p = (char *)&i;
523 // Application entry point
525 int main(int argc, char ** argv)
527 perm_verb_flag = 0; // Clobber "permanent" verbose flag
528 legacy_flag = 1; // Default is legacy mode on (:-P)
529 cmdlnexec = argv[0]; // Obtain executable name
531 endian = GetEndianess(); // Get processor endianess
533 // If commands were passed in, process them
535 return Process(argc - 1, argv + 1);