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[n] Create an ST .prg (1=normal, 2=w/symbols)\n"
137 " -r[size] Pad segments to boundary size specified\n"
138 " w: word (2 bytes, default alignment)\n"
139 " l: long (4 bytes)\n"
140 " p: phrase (8 bytes)\n"
141 " d: double phrase (16 bytes)\n"
142 " q: quad phrase (32 bytes)\n"
143 " -s Warn about possible short branches\n"
144 " -u Force referenced and undefined symbols global\n"
145 " -v Set verbose mode\n"
146 " -y[pagelen] Set page line length (default: 61)\n"
152 // Display version information
154 void DisplayVersion(void)
156 printf("\nReboot's Macro Assembler for Atari Jaguar\n"
157 "Copyright (C) 199x Landon Dyer, 2011-2015 Reboot\n"
158 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
163 // Process command line arguments and do an assembly
165 int Process(int argc, char ** argv)
167 int argno; // Argument number
168 SYM * sy; // Pointer to a symbol record
169 char * s; // String pointer
170 int fd; // File descriptor
171 char fnbuf[FNSIZ]; // Filename buffer
174 errcnt = 0; // Initialise error count
175 listing = 0; // Initialise listing level
176 list_flag = 0; // Initialise listing flag
177 verb_flag = perm_verb_flag; // Initialise verbose flag
178 as68_flag = 0; // Initialise as68 kludge mode
179 glob_flag = 0; // Initialise .globl flag
180 sbra_flag = 0; // Initialise short branch flag
181 debug = 0; // Initialise debug flag
182 searchpath = NULL; // Initialise search path
183 objfname = NULL; // Initialise object filename
184 list_fname = NULL; // Initialise listing filename
185 err_fname = NULL; // Initialise error filename
186 obj_format = BSD; // Initialise object format
187 firstfname = NULL; // Initialise first filename
188 err_fd = ERROUT; // Initialise error file descriptor
189 err_flag = 0; // Initialise error flag
190 rgpu = 0; // Initialise GPU assembly flag
191 rdsp = 0; // Initialise DSP assembly flag
192 lsym_flag = 1; // Include local symbols in object file
193 regbank = BANK_N; // No RISC register bank specified
194 orgactive = 0; // Not in RISC org section
195 orgwarning = 0; // No ORG warning issued
196 segpadsize = 2; // Initialise segment padding size
198 // Initialise modules
199 InitSymbolTable(); // Symbol table
200 InitTokenizer(); // Tokenizer
201 InitLineProcessor(); // Line processor
202 InitExpression(); // Expression analyzer
203 InitSection(); // Section manager / code generator
204 InitMark(); // Mark tape-recorder
205 InitMacro(); // Macro processor
206 InitListing(); // Listing generator
208 // Process command line arguments and assemble source files
209 for(argno=0; argno<argc; ++argno)
211 if (*argv[argno] == '-')
213 switch (argv[argno][1])
215 case 'd': // Define symbol
217 for(s=argv[argno]+2; *s!=EOS;)
226 if (argv[argno][2] == EOS)
228 printf("-d: empty symbol\n");
233 sy = lookup(argv[argno] + 2, 0, 0);
237 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
241 sy->sattr = DEFINED | EQUATED | ABS;
242 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
244 case 'e': // Redirect error message output
246 err_fname = argv[argno] + 2;
248 case 'f': // -f<format>
250 switch (argv[argno][2])
253 case 'a': // -fa = Alcyon [the default]
257 case 'b': // -fb = BSD (Jaguar Recommended)
262 printf("-f: unknown object format specified\n");
267 case 'g': // Debugging flag
269 printf("Debugging flag (-g) not yet implemented\n");
271 case 'i': // Set directory search path
273 searchpath = argv[argno] + 2;
275 case 'l': // Produce listing file
277 list_fname = argv[argno] + 2;
282 case 'o': // Direct object file output
284 if (argv[argno][2] != EOS)
285 objfname = argv[argno] + 2;
290 printf("Missing argument to -o");
295 objfname = argv[argno];
299 case 'p': /* -p: generate ".PRG" executable output */
302 * -p .PRG generation w/o symbols
303 * -ps .PRG generation with symbols
305 switch (argv[argno][2])
317 printf("-p: syntax error\n");
322 // Enforce Alcyon object format - kind of silly
323 // to ask for .prg output without it!
326 case 'r': // Pad seg to requested boundary size
328 switch(argv[argno][2])
330 case 'w': case 'W': segpadsize = 2; break;
331 case 'l': case 'L': segpadsize = 4; break;
332 case 'p': case 'P': segpadsize = 8; break;
333 case 'd': case 'D': segpadsize = 16; break;
334 case 'q': case 'Q': segpadsize = 32; break;
335 default: segpadsize = 2; break; // Effective autoeven();
338 case 's': // Warn about possible short branches
342 case 'u': // Make undefined symbols .globl
346 case 'v': // Verbose flag
354 case 'x': // Turn on debugging
357 printf("~ Debugging ON\n");
359 case 'y': // -y<pagelen>
361 pagelen = atoi(argv[argno] + 2);
365 printf("-y: bad page length\n");
371 case EOS: // Input is stdin
372 if (firstfname == NULL) // Kludge first filename
373 firstfname = defname;
375 include(0, "(stdin)");
378 case 'h': // Display command line usage
385 case 'n': // Turn off legacy mode
388 printf("Legacy mode OFF\n");
392 printf("Unknown switch: %s\n\n", argv[argno]);
400 // Record first filename.
401 if (firstfname == NULL)
402 firstfname = argv[argno];
404 strcpy(fnbuf, argv[argno]);
405 fext(fnbuf, ".s", 0);
410 printf("Cannot open: %s\n", fnbuf);
420 // Wind-up processing;
421 // o save current section (no more code generation)
422 // o do auto-even of all sections (or boundary alignment as requested
424 // o determine name of object file:
425 // - "foo.o" for linkable output;
426 // - "foo.prg" for GEMDOS executable (-p flag).
429 for(i=TEXT; i<=BSS; i<<=1)
435 case 2: d_even(); break;
436 case 4: d_long(); break;
437 case 8: d_phrase(); break;
438 case 16: d_dphrase(); break;
439 case 32: d_qphrase(); break;
445 if (objfname == NULL)
447 if (firstfname == NULL)
448 firstfname = defname;
450 strcpy(fnbuf, firstfname);
451 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
455 // With one pass finished, go back and:
456 // (1) run through all the fixups and resolve forward references;
457 // (1.5) ensure that remaining fixups can be handled by the linker
458 // (`lo68' format, extended (postfix) format....)
459 // (2) generate the output file image and symbol table;
460 // (3) generate relocation information from left-over fixups.
461 ResolveAllFixups(); // Do all fixups
462 StopMark(); // Stop mark tape-recorder
466 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
471 s = (prg_flag ? "executable" : "object");
472 printf("[Writing %s file: %s]\n", s, objfname);
485 printf("[Wrapping-up listing file]\n");
495 DEBUG dump_everything();
502 // Determine processor endianess
504 int GetEndianess(void)
507 char * p = (char *)&i;
517 // Application entry point
519 int main(int argc, char ** argv)
521 perm_verb_flag = 0; // Clobber "permanent" verbose flag
522 legacy_flag = 1; // Default is legacy mode on (:-P)
523 cmdlnexec = argv[0]; // Obtain executable name
525 endian = GetEndianess(); // Get processor endianess
527 // If commands were passed in, process them
529 return Process(argc - 1, argv + 1);