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 " -y[pagelen] Set page line length (default: 61)\n"
155 // Display version information
157 void DisplayVersion(void)
160 " _ __ _ __ ___ __ _ ___ \n"
161 "| '__| '_ ` _ \\ / _` |/ __|\n"
162 "| | | | | | | | (_| | (__ \n"
163 "|_| |_| |_| |_|\\__,_|\\___|\n"
164 "\nReboot's Macro Assembler\n"
165 "Copyright (C) 199x Landon Dyer, 2011-2015 Reboot\n"
166 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
171 // Process command line arguments and do an assembly
173 int Process(int argc, char ** argv)
175 int argno; // Argument number
176 SYM * sy; // Pointer to a symbol record
177 char * s; // String pointer
178 int fd; // File descriptor
179 char fnbuf[FNSIZ]; // Filename buffer
182 errcnt = 0; // Initialise error count
183 listing = 0; // Initialise listing level
184 list_flag = 0; // Initialise listing flag
185 verb_flag = perm_verb_flag; // Initialise verbose flag
186 as68_flag = 0; // Initialise as68 kludge mode
187 glob_flag = 0; // Initialise .globl flag
188 sbra_flag = 0; // Initialise short branch flag
189 debug = 0; // Initialise debug flag
190 searchpath = NULL; // Initialise search path
191 objfname = NULL; // Initialise object filename
192 list_fname = NULL; // Initialise listing filename
193 err_fname = NULL; // Initialise error filename
194 obj_format = BSD; // Initialise object format
195 firstfname = NULL; // Initialise first filename
196 err_fd = ERROUT; // Initialise error file descriptor
197 err_flag = 0; // Initialise error flag
198 rgpu = 0; // Initialise GPU assembly flag
199 rdsp = 0; // Initialise DSP assembly flag
200 lsym_flag = 1; // Include local symbols in object file
201 regbank = BANK_N; // No RISC register bank specified
202 orgactive = 0; // Not in RISC org section
203 orgwarning = 0; // No ORG warning issued
204 segpadsize = 2; // Initialise segment padding size
205 optim_flag = 1; // Automatically optimise
207 // Initialise modules
208 InitSymbolTable(); // Symbol table
209 InitTokenizer(); // Tokenizer
210 InitLineProcessor(); // Line processor
211 InitExpression(); // Expression analyzer
212 InitSection(); // Section manager / code generator
213 InitMark(); // Mark tape-recorder
214 InitMacro(); // Macro processor
215 InitListing(); // Listing generator
217 // Process command line arguments and assemble source files
218 for(argno=0; argno<argc; ++argno)
220 if (*argv[argno] == '-')
222 switch (argv[argno][1])
224 case 'd': // Define symbol
226 for(s=argv[argno]+2; *s!=EOS;)
235 if (argv[argno][2] == EOS)
237 printf("-d: empty symbol\n");
242 sy = lookup(argv[argno] + 2, 0, 0);
246 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
250 sy->sattr = DEFINED | EQUATED | ABS;
251 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
253 case 'e': // Redirect error message output
255 err_fname = argv[argno] + 2;
257 case 'f': // -f<format>
259 switch (argv[argno][2])
262 case 'a': // -fa = Alcyon [the default]
266 case 'b': // -fb = BSD (Jaguar Recommended)
271 printf("-f: unknown object format specified\n");
276 case 'g': // Debugging flag
278 printf("Debugging flag (-g) not yet implemented\n");
280 case 'i': // Set directory search path
282 searchpath = argv[argno] + 2;
284 case 'l': // Produce listing file
286 list_fname = argv[argno] + 2;
291 case 'o': // Direct object file output
293 if (argv[argno][2] != EOS)
294 objfname = argv[argno] + 2;
299 printf("Missing argument to -o");
304 objfname = argv[argno];
308 case 'p': /* -p: generate ".PRG" executable output */
311 * -p .PRG generation w/o symbols
312 * -ps .PRG generation with symbols
314 switch (argv[argno][2])
326 printf("-p: syntax error\n");
331 // Enforce Alcyon object format - kind of silly
332 // to ask for .prg output without it!
335 case 'r': // Pad seg to requested boundary size
337 switch(argv[argno][2])
339 case 'w': case 'W': segpadsize = 2; break;
340 case 'l': case 'L': segpadsize = 4; break;
341 case 'p': case 'P': segpadsize = 8; break;
342 case 'd': case 'D': segpadsize = 16; break;
343 case 'q': case 'Q': segpadsize = 32; break;
344 default: segpadsize = 2; break; // Effective autoeven();
347 case 's': // Warn about possible short branches
351 case 'u': // Make undefined symbols .globl
355 case 'v': // Verbose flag
367 case 'x': // Turn on debugging
370 printf("~ Debugging ON\n");
372 case 'y': // -y<pagelen>
374 pagelen = atoi(argv[argno] + 2);
378 printf("-y: bad page length\n");
384 case EOS: // Input is stdin
385 if (firstfname == NULL) // Kludge first filename
386 firstfname = defname;
388 include(0, "(stdin)");
391 case 'h': // Display command line usage
398 case 'n': // Turn off legacy mode
401 printf("Legacy mode OFF\n");
405 printf("Unknown switch: %s\n\n", argv[argno]);
413 // Record first filename.
414 if (firstfname == NULL)
415 firstfname = argv[argno];
417 strcpy(fnbuf, argv[argno]);
418 fext(fnbuf, ".s", 0);
423 printf("Cannot open: %s\n", fnbuf);
433 // Wind-up processing;
434 // o save current section (no more code generation)
435 // o do auto-even of all sections (or boundary alignment as requested
437 // o determine name of object file:
438 // - "foo.o" for linkable output;
439 // - "foo.prg" for GEMDOS executable (-p flag).
442 for(i=TEXT; i<=BSS; i<<=1)
448 case 2: d_even(); break;
449 case 4: d_long(); break;
450 case 8: d_phrase(); break;
451 case 16: d_dphrase(); break;
452 case 32: d_qphrase(); break;
458 if (objfname == NULL)
460 if (firstfname == NULL)
461 firstfname = defname;
463 strcpy(fnbuf, firstfname);
464 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
468 // With one pass finished, go back and:
469 // (1) run through all the fixups and resolve forward references;
470 // (1.5) ensure that remaining fixups can be handled by the linker
471 // (`lo68' format, extended (postfix) format....)
472 // (2) generate the output file image and symbol table;
473 // (3) generate relocation information from left-over fixups.
474 ResolveAllFixups(); // Do all fixups
475 StopMark(); // Stop mark tape-recorder
479 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
484 s = (prg_flag ? "executable" : "object");
485 printf("[Writing %s file: %s]\n", s, objfname);
498 printf("[Wrapping-up listing file]\n");
508 DEBUG dump_everything();
515 // Determine processor endianess
517 int GetEndianess(void)
520 char * p = (char *)&i;
530 // Application entry point
532 int main(int argc, char ** argv)
534 perm_verb_flag = 0; // Clobber "permanent" verbose flag
535 legacy_flag = 1; // Default is legacy mode on (:-P)
536 cmdlnexec = argv[0]; // Obtain executable name
538 endian = GetEndianess(); // Get processor endianess
540 // If commands were passed in, process them
542 return Process(argc - 1, argv + 1);