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 legacy_flag; // Do stuff like insert code in RISC assembler
33 int obj_format; // Object format flag
34 int debug; // [1..9] Enable debugging levels
35 int err_flag; // '-e' specified
36 int err_fd; // File to write error messages to
37 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
38 int list_fd; // File to write listing to
39 int regbank; // RISC register bank
40 int segpadsize; // Segment padding size
41 int endian; // Host processor endianess
42 char * objfname; // Object filename pointer
43 char * firstfname; // First source filename
44 char * cmdlnexec; // Executable name, pointer to ARGV[0]
45 char * searchpath; // Search path for include files
46 char defname[] = "noname.o"; // Default output filename
50 // Manipulate file extension.
52 // 'name' must be large enough to hold any possible filename. If 'stripp' is
53 // nonzero, any old extension is removed. If the file does not already have an
54 // extension, 'extension' is appended to the filename.
56 char * fext(char * name, char * extension, int stripp)
60 // Find beginning of "real" name (strip off path)
61 char * beg = strrchr(name, SLASHCHAR);
66 // Clobber any old extension, if requested
69 for(s=beg; *s && *s!='.'; ++s)
75 if (strrchr(beg, '.') == NULL)
76 strcat(beg, extension);
83 // Return 'item'nth element of semicolon-seperated pathnames specified in the
84 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
85 // nth path doesn't exist.
87 // ['item' ranges from 0 to N-1, where N = #elements in search path]
89 int nthpath(char * env_var, int itemno, char * buf)
91 char * s = searchpath;
100 while (*s != EOS && *s++ != ';')
106 while (*s != EOS && *s != ';')
116 // Display command line help
118 void DisplayHelp(void)
121 " %s [options] srcfile\n"
124 " -? or -h Display usage information\n"
125 " -dsymbol[=value] Define symbol\n"
126 " -e[errorfile] Send error messages to file, not stdout\n"
127 " -f[format] Output object file format\n"
128 " b: BSD (use this for Jaguar)\n"
129 " -i[path] Directory to search for include files\n"
130 " -l[filename] Create an output listing file\n"
131 " -n Don't do things behind your back in RISC assembler\n"
132 " -o file Output file name\n"
133 " -r[size] Pad segments to boundary size specified\n"
134 " w: word (2 bytes, default alignment)\n"
135 " l: long (4 bytes)\n"
136 " p: phrase (8 bytes)\n"
137 " d: double phrase (16 bytes)\n"
138 " q: quad phrase (32 bytes)\n"
139 " -s Warn about possible short branches\n"
140 " -u Force referenced and undefined symbols global\n"
141 " -v Set verbose mode\n"
142 " -y[pagelen] Set page line length (default: 61)\n"
148 // Display version information
150 void DisplayVersion(void)
152 printf("\nReboot's Macro Assembler for Atari Jaguar\n"
153 "Copyright (C) 199x Landon Dyer, 2011-2015 Reboot\n"
154 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
159 // Process command line arguments and do an assembly
161 int Process(int argc, char ** argv)
163 int argno; // Argument number
164 SYM * sy; // Pointer to a symbol record
165 char * s; // String pointer
166 int fd; // File descriptor
167 char fnbuf[FNSIZ]; // Filename buffer
170 errcnt = 0; // Initialise error count
171 listing = 0; // Initialise listing level
172 list_flag = 0; // Initialise listing flag
173 verb_flag = perm_verb_flag; // Initialise verbose flag
174 as68_flag = 0; // Initialise as68 kludge mode
175 glob_flag = 0; // Initialise .globl flag
176 sbra_flag = 0; // Initialise short branch flag
177 debug = 0; // Initialise debug flag
178 searchpath = NULL; // Initialise search path
179 objfname = NULL; // Initialise object filename
180 list_fname = NULL; // Initialise listing filename
181 err_fname = NULL; // Initialise error filename
182 obj_format = BSD; // Initialise object format
183 firstfname = NULL; // Initialise first filename
184 err_fd = ERROUT; // Initialise error file descriptor
185 err_flag = 0; // Initialise error flag
186 rgpu = 0; // Initialise GPU assembly flag
187 rdsp = 0; // Initialise DSP assembly flag
188 lsym_flag = 1; // Include local symbols in object file
189 regbank = BANK_N; // No RISC register bank specified
190 orgactive = 0; // Not in RISC org section
191 orgwarning = 0; // No ORG warning issued
192 segpadsize = 2; // Initialise segment padding size
194 // Initialise modules
195 InitSymbolTable(); // Symbol table
196 InitTokenizer(); // Tokenizer
197 InitLineProcessor(); // Line processor
198 InitExpression(); // Expression analyzer
199 InitSection(); // Section manager / code generator
200 InitMark(); // Mark tape-recorder
201 InitMacro(); // Macro processor
202 InitListing(); // Listing generator
204 // Process command line arguments and assemble source files
205 for(argno=0; argno<argc; ++argno)
207 if (*argv[argno] == '-')
209 switch (argv[argno][1])
211 case 'd': // Define symbol
213 for(s=argv[argno]+2; *s!=EOS;)
222 if (argv[argno][2] == EOS)
224 printf("-d: empty symbol\n");
229 sy = lookup(argv[argno] + 2, 0, 0);
233 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
237 sy->sattr = DEFINED | EQUATED | ABS;
238 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
240 case 'e': // Redirect error message output
242 err_fname = argv[argno] + 2;
244 case 'f': // -f<format>
246 switch (argv[argno][2])
249 case 'b': // -fb = BSD (Jaguar Recommended)
254 printf("-f: unknown object format specified\n");
259 case 'g': // Debugging flag
261 printf("Debugging flag (-g) not yet implemented\n");
263 case 'i': // Set directory search path
265 searchpath = argv[argno] + 2;
267 case 'l': // Produce listing file
269 list_fname = argv[argno] + 2;
274 case 'o': // Direct object file output
276 if (argv[argno][2] != EOS)
277 objfname = argv[argno] + 2;
282 printf("Missing argument to -o");
286 objfname = argv[argno];
290 case 'r': // Pad seg to requested boundary size
292 switch(argv[argno][2])
294 case 'w': case 'W': segpadsize = 2; break;
295 case 'l': case 'L': segpadsize = 4; break;
296 case 'p': case 'P': segpadsize = 8; break;
297 case 'd': case 'D': segpadsize = 16; break;
298 case 'q': case 'Q': segpadsize = 32; break;
299 default: segpadsize = 2; break; // Effective autoeven();
302 case 's': // Warn about possible short branches
306 case 'u': // Make undefined symbols .globl
310 case 'v': // Verbose flag
318 case 'x': // Turn on debugging
321 printf("~ Debugging ON\n");
323 case 'y': // -y<pagelen>
325 pagelen = atoi(argv[argno] + 2);
329 printf("-y: bad page length\n");
335 case EOS: // Input is stdin
336 if (firstfname == NULL) // Kludge first filename
337 firstfname = defname;
339 include(0, "(stdin)");
342 case 'h': // Display command line usage
349 case 'n': // Turn off legacy mode
352 printf("Legacy mode OFF\n");
356 printf("Unknown switch: %s\n\n", argv[argno]);
364 // Record first filename.
365 if (firstfname == NULL)
366 firstfname = argv[argno];
368 strcpy(fnbuf, argv[argno]);
369 fext(fnbuf, ".s", 0);
374 printf("Cannot open: %s\n", fnbuf);
384 // Wind-up processing;
385 // o save current section (no more code generation)
386 // o do auto-even of all sections (or boundary alignment as requested
388 // o determine name of object file:
389 // - "foo.o" for linkable output;
390 // - "foo.prg" for GEMDOS executable (-p flag).
393 for(i=TEXT; i<=BSS; i<<=1)
399 case 2: d_even(); break;
400 case 4: d_long(); break;
401 case 8: d_phrase(); break;
402 case 16: d_dphrase(); break;
403 case 32: d_qphrase(); break;
409 if (objfname == NULL)
411 if (firstfname == NULL)
412 firstfname = defname;
414 strcpy(fnbuf, firstfname);
415 //fext(fnbuf, prg_flag ? ".prg" : ".o", 1);
416 fext(fnbuf, ".o", 1);
420 // With one pass finished, go back and:
421 // (1) run through all the fixups and resolve forward references;
422 // (1.5) ensure that remaining fixups can be handled by the linker
423 // (`lo68' format, extended (postfix) format....)
424 // (2) generate the output file image and symbol table;
425 // (3) generate relocation information from left-over fixups.
426 ResolveAllFixups(); // Do all fixups
427 StopMark(); // Stop mark tape-recorder
431 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
437 printf("[Writing %s file: %s]\n", s, objfname);
450 printf("[Wrapping-up listing file]\n");
460 DEBUG dump_everything();
467 // Determine processor endianess
469 int GetEndianess(void)
472 char * p = (char *)&i;
482 // Application entry point
484 int main(int argc, char ** argv)
486 perm_verb_flag = 0; // Clobber "permanent" verbose flag
487 legacy_flag = 1; // Default is legacy mode on (:-P)
488 cmdlnexec = argv[0]; // Obtain executable name
490 endian = GetEndianess(); // Get processor endianess
492 // If commands were passed in, process them
494 return Process(argc - 1, argv + 1);