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
48 int optim_flag; // Optimise all the things!
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 " and applied optimisations\n"
146 " -u Force referenced and undefined symbols global\n"
147 " -w Turn off optimisations done automatically\n"
148 " -v Set verbose mode\n"
149 " -x Turn on debugging mode\n"
150 " -y[pagelen] Set page line length (default: 61)\n"
156 // Display version information
158 void DisplayVersion(void)
161 " _ __ _ __ ___ __ _ ___ \n"
162 "| '__| '_ ` _ \\ / _` |/ __|\n"
163 "| | | | | | | | (_| | (__ \n"
164 "|_| |_| |_| |_|\\__,_|\\___|\n"
165 "\nReboot's Macro Assembler\n"
166 "Copyright (C) 199x Landon Dyer, 2011-2015 Reboot\n"
167 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
172 // Process command line arguments and do an assembly
174 int Process(int argc, char ** argv)
176 int argno; // Argument number
177 SYM * sy; // Pointer to a symbol record
178 char * s; // String pointer
179 int fd; // File descriptor
180 char fnbuf[FNSIZ]; // Filename buffer
183 errcnt = 0; // Initialise error count
184 listing = 0; // Initialise listing level
185 list_flag = 0; // Initialise listing flag
186 verb_flag = perm_verb_flag; // Initialise verbose flag
187 as68_flag = 0; // Initialise as68 kludge mode
188 glob_flag = 0; // Initialise .globl flag
189 sbra_flag = 0; // Initialise short branch flag
190 debug = 0; // Initialise debug flag
191 searchpath = NULL; // Initialise search path
192 objfname = NULL; // Initialise object filename
193 list_fname = NULL; // Initialise listing filename
194 err_fname = NULL; // Initialise error filename
195 obj_format = BSD; // Initialise object format
196 firstfname = NULL; // Initialise first filename
197 err_fd = ERROUT; // Initialise error file descriptor
198 err_flag = 0; // Initialise error flag
199 rgpu = 0; // Initialise GPU assembly flag
200 rdsp = 0; // Initialise DSP assembly flag
201 lsym_flag = 1; // Include local symbols in object file
202 regbank = BANK_N; // No RISC register bank specified
203 orgactive = 0; // Not in RISC org section
204 orgwarning = 0; // No ORG warning issued
205 segpadsize = 2; // Initialise segment padding size
206 optim_flag = 1; // Automatically optimise
208 // Initialise modules
209 InitSymbolTable(); // Symbol table
210 InitTokenizer(); // Tokenizer
211 InitLineProcessor(); // Line processor
212 InitExpression(); // Expression analyzer
213 InitSection(); // Section manager / code generator
214 InitMark(); // Mark tape-recorder
215 InitMacro(); // Macro processor
216 InitListing(); // Listing generator
218 // Process command line arguments and assemble source files
219 for(argno=0; argno<argc; ++argno)
221 if (*argv[argno] == '-')
223 switch (argv[argno][1])
225 case 'd': // Define symbol
227 for(s=argv[argno]+2; *s!=EOS;)
236 if (argv[argno][2] == EOS)
238 printf("-d: empty symbol\n");
243 sy = lookup(argv[argno] + 2, 0, 0);
247 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
251 sy->sattr = DEFINED | EQUATED | ABS;
252 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
254 case 'e': // Redirect error message output
256 err_fname = argv[argno] + 2;
258 case 'f': // -f<format>
260 switch (argv[argno][2])
263 case 'a': // -fa = Alcyon [the default]
267 case 'b': // -fb = BSD (Jaguar Recommended)
272 printf("-f: unknown object format specified\n");
277 case 'g': // Debugging flag
279 printf("Debugging flag (-g) not yet implemented\n");
281 case 'i': // Set directory search path
283 searchpath = argv[argno] + 2;
285 case 'l': // Produce listing file
287 list_fname = argv[argno] + 2;
292 case 'o': // Direct object file output
294 if (argv[argno][2] != EOS)
295 objfname = argv[argno] + 2;
300 printf("Missing argument to -o");
305 objfname = argv[argno];
309 case 'p': /* -p: generate ".PRG" executable output */
312 * -p .PRG generation w/o symbols
313 * -ps .PRG generation with symbols
315 switch (argv[argno][2])
327 printf("-p: syntax error\n");
332 // Enforce Alcyon object format - kind of silly
333 // to ask for .prg output without it!
336 case 'r': // Pad seg to requested boundary size
338 switch(argv[argno][2])
340 case 'w': case 'W': segpadsize = 2; break;
341 case 'l': case 'L': segpadsize = 4; break;
342 case 'p': case 'P': segpadsize = 8; break;
343 case 'd': case 'D': segpadsize = 16; break;
344 case 'q': case 'Q': segpadsize = 32; break;
345 default: segpadsize = 2; break; // Effective autoeven();
348 case 's': // Warn about possible short branches
352 case 'u': // Make undefined symbols .globl
356 case 'v': // Verbose flag
368 case 'x': // Turn on debugging
371 printf("~ Debugging ON\n");
373 case 'y': // -y<pagelen>
375 pagelen = atoi(argv[argno] + 2);
379 printf("-y: bad page length\n");
385 case EOS: // Input is stdin
386 if (firstfname == NULL) // Kludge first filename
387 firstfname = defname;
389 include(0, "(stdin)");
392 case 'h': // Display command line usage
399 case 'n': // Turn off legacy mode
402 printf("Legacy mode OFF\n");
406 printf("Unknown switch: %s\n\n", argv[argno]);
414 // Record first filename.
415 if (firstfname == NULL)
416 firstfname = argv[argno];
418 strcpy(fnbuf, argv[argno]);
419 fext(fnbuf, ".s", 0);
424 printf("Cannot open: %s\n", fnbuf);
434 // Wind-up processing;
435 // o save current section (no more code generation)
436 // o do auto-even of all sections (or boundary alignment as requested
438 // o determine name of object file:
439 // - "foo.o" for linkable output;
440 // - "foo.prg" for GEMDOS executable (-p flag).
443 for(i=TEXT; i<=BSS; i<<=1)
449 case 2: d_even(); break;
450 case 4: d_long(); break;
451 case 8: d_phrase(); break;
452 case 16: d_dphrase(); break;
453 case 32: d_qphrase(); break;
459 if (objfname == NULL)
461 if (firstfname == NULL)
462 firstfname = defname;
464 strcpy(fnbuf, firstfname);
465 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
469 // With one pass finished, go back and:
470 // (1) run through all the fixups and resolve forward references;
471 // (1.5) ensure that remaining fixups can be handled by the linker
472 // (`lo68' format, extended (postfix) format....)
473 // (2) generate the output file image and symbol table;
474 // (3) generate relocation information from left-over fixups.
475 ResolveAllFixups(); // Do all fixups
476 StopMark(); // Stop mark tape-recorder
480 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
485 s = (prg_flag ? "executable" : "object");
486 printf("[Writing %s file: %s]\n", s, objfname);
499 printf("[Wrapping-up listing file]\n");
509 DEBUG dump_everything();
516 // Determine processor endianess
518 int GetEndianess(void)
521 char * p = (char *)&i;
531 // Application entry point
533 int main(int argc, char ** argv)
535 perm_verb_flag = 0; // Clobber "permanent" verbose flag
536 legacy_flag = 1; // Default is legacy mode on (:-P)
537 cmdlnexec = argv[0]; // Obtain executable name
539 endian = GetEndianess(); // Get processor endianess
541 // If commands were passed in, process them
543 return Process(argc - 1, argv + 1);