2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2020 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
27 int perm_verb_flag; // Permanently verbose, interactive mode
28 int list_flag; // "-l" listing flag on command line
29 int list_pag = 1; // Enable listing pagination by default
30 int verb_flag; // Be verbose about what's going on
31 int m6502; // 1, assembling 6502 code
32 int as68_flag; // as68 kludge mode
33 int glob_flag; // Assume undefined symbols are global
34 int lsym_flag; // Include local symbols in object file
35 int sbra_flag; // Warn about possible short branches
36 int prg_flag; // !=0, produce .PRG executable (2=symbols)
37 int prg_extend; // !=0, output extended .PRG symbols
38 int legacy_flag; // Do stuff like insert code in RISC assembler
39 int obj_format; // Object format flag
40 int debug; // [1..9] Enable debugging levels
41 int err_flag; // '-e' specified
42 int err_fd; // File to write error messages to
43 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
44 int robjproc; // Assembling Jaguar Object Processor code
45 int dsp56001; // Assembling DSP 56001 code
46 int list_fd; // File to write listing to
47 int regbank; // RISC register bank
48 int segpadsize; // Segment padding size
49 int endian; // Host processor endianess (0 = LE, 1 = BE)
50 char * objfname; // Object filename pointer
51 char * firstfname; // First source filename
52 char * cmdlnexec; // Executable name, pointer to ARGV[0]
53 char * searchpath = NULL; // Search path for include files
54 char defname[] = "noname.o"; // Default output filename
55 int optim_flags[OPT_COUNT]; // Specific optimisations on/off matrix
56 int optim_pc = 0; // Enforce PC relative
57 int activecpu = CPU_68000; // Active 68k CPU (68000 by default)
58 int activefpu = FPU_NONE; // Active FPU (none by default)
59 int org68k_active = 0; // .org switch for 68k (only with RAW output format)
60 uint32_t org68k_address; // .org for 68k
63 // Convert a string to uppercase
65 void strtoupper(char * s)
73 // Manipulate file extension.
75 // 'name' must be large enough to hold any possible filename. If 'stripp' is
76 // nonzero, any old extension is removed. If the file does not already have an
77 // extension, 'extension' is appended to the filename.
79 char * fext(char * name, char * extension, int stripp)
83 // Find beginning of "real" name (strip off path)
84 char * beg = strrchr(name, SLASHCHAR);
89 // Clobber any old extension, if requested
92 for(s=beg; *s && *s!='.'; ++s)
98 if (strrchr(beg, '.') == NULL)
99 strcat(beg, extension);
106 // Return 'item'nth element of semicolon-seperated pathnames specified in the
107 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
108 // nth path doesn't exist.
110 // ['item' ranges from 0 to N-1, where N = #elements in search path]
112 int nthpath(char * env_var, int itemno, char * buf)
114 char * s = searchpath;
123 while (*s != EOS && *s++ != ';')
129 while (*s != EOS && *s != ';')
139 // Display command line help
141 void DisplayHelp(void)
144 " %s [options] srcfile\n"
147 " -? or -h Display usage information\n"
148 " -dsymbol[=value] Define symbol (with optional value, default=0)\n"
149 " -e[errorfile] Send error messages to file, not stdout\n"
150 " -f[format] Output object file format\n"
152 " b: BSD (use this for Jaguar)\n"
154 " p: P56 (use this for DSP56001 only)\n"
155 " l: LOD (use this for DSP56001 only)\n"
156 " x: com/exe/xex (Atari 800)\n"
157 " r: absolute address\n"
158 " -i[path] Directory to search for include files\n"
159 " -l[filename] Create an output listing file\n"
160 " -l*[filename] Create an output listing file without pagination\n"
161 " -m[cpu] Select default CPU. Available options:\n"
162 " 68000, 68020, 68030, 68040, 68060, 6502, tom, jerry, 56001\n"
163 " -n Don't do things behind your back in RISC assembler\n"
164 " -o file Output file name\n"
165 " +o[value] Turn a specific optimisation on\n"
166 " Available optimisation values and default settings:\n"
167 " o0: Absolute long adddresses to word (on)\n"
168 " o1: move.l #x,dn/an to moveq (on)\n"
169 " o2: Word branches to short (on)\n"
170 " o3: Outer displacement 0(an) to (an) (off)\n"
171 " o4: lea size(An),An to addq #size,An (off)\n"
172 " o5: Absolute long base displacement to word (off)\n"
173 " o6: Null branches to NOP (off)\n"
174 " o7: clr.l Dx to moveq #0,Dx (off)\n"
175 " o8: adda.w/l #x,Dy to addq.w/l #x,Dy (off)\n"
176 " o9: adda.w/l #x,Dy to lea x(Dy),Dy (off)\n"
177 " op: Enforce PC relative (off)\n"
178 " ~o[value] Turn a specific optimisation off\n"
179 " +oall Turn all optimisations on\n"
180 " ~oall Turn all optimisations off\n"
181 " -p Create an ST .prg (without symbols). Forces -fa\n"
182 " -ps Create an ST .prg (with symbols). Forces -fa\n"
183 " -px Create an ST .prg (with exsymbols). Forces -fa\n"
184 " -r[size] Pad segments to boundary size specified\n"
185 " w: word (2 bytes, default alignment)\n"
186 " l: long (4 bytes)\n"
187 " p: phrase (8 bytes)\n"
188 " d: double phrase (16 bytes)\n"
189 " q: quad phrase (32 bytes)\n"
190 " -s Warn about possible short branches\n"
191 " and applied optimisations\n"
192 " -u Force referenced and undefined symbols global\n"
193 " -v Set verbose mode\n"
194 " -x Turn on debugging mode\n"
195 " -y[pagelen] Set page line length (default: 61)\n"
201 // Display version information
203 void DisplayVersion(void)
206 " _ __ _ __ ___ __ _ ___ \n"
207 "| '__| '_ ` _ \\ / _` |/ __|\n"
208 "| | | | | | | | (_| | (__ \n"
209 "|_| |_| |_| |_|\\__,_|\\___|\n"
210 "\nReboot's Macro Assembler\n"
211 "Copyright (C) 199x Landon Dyer, 2011-2020 Reboot\n"
212 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
217 // Parse optimisation options
219 int ParseOptimization(char * optstring)
223 if (*optstring == '+')
225 else if (*optstring != '~')
228 if ((optstring[2] == 'a' || optstring[2] == 'A')
229 && (optstring[3] == 'l' || optstring[3] == 'L')
230 && (optstring[4] == 'l' || optstring[4] == 'L'))
232 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
235 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
237 if (optstring[2] == 'p' || optstring[2] == 'P')
243 int opt_no = atoi(&optstring[2]);
245 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
247 optim_flags[opt_no] = onoff;
261 // Process command line arguments and do an assembly
263 int Process(int argc, char ** argv)
265 int argno; // Argument number
266 SYM * sy; // Pointer to a symbol record
267 char * s; // String pointer
268 int fd; // File descriptor
269 char fnbuf[FNSIZ]; // Filename buffer
272 errcnt = 0; // Initialize error count
273 listing = 0; // Initialize listing level
274 list_flag = 0; // Initialize listing flag
275 verb_flag = perm_verb_flag; // Initialize verbose flag
276 as68_flag = 0; // Initialize as68 kludge mode
277 glob_flag = 0; // Initialize .globl flag
278 sbra_flag = 0; // Initialize short branch flag
279 debug = 0; // Initialize debug flag
280 searchpath = NULL; // Initialize search path
281 objfname = NULL; // Initialize object filename
282 list_fname = NULL; // Initialize listing filename
283 err_fname = NULL; // Initialize error filename
284 obj_format = BSD; // Initialize object format
285 firstfname = NULL; // Initialize first filename
286 err_fd = ERROUT; // Initialize error file descriptor
287 err_flag = 0; // Initialize error flag
288 rgpu = 0; // Initialize GPU assembly flag
289 rdsp = 0; // Initialize DSP assembly flag
290 robjproc = 0; // Initialize OP assembly flag
291 lsym_flag = 1; // Include local symbols in object file
292 regbank = BANK_N; // No RISC register bank specified
293 orgactive = 0; // Not in RISC org section
294 orgwarning = 0; // No ORG warning issued
295 segpadsize = 2; // Initialize segment padding size
296 dsp_orgmap[0].start = 0; // Initialize 56001 org initial address
297 dsp_orgmap[0].memtype = ORG_P; // Initialize 56001 org start segment
298 m6502 = 0; // 6502 mode off by default
300 // Initialize modules
301 InitSymbolTable(); // Symbol table
302 InitTokenizer(); // Tokenizer
303 InitLineProcessor(); // Line processor
304 InitExpression(); // Expression analyzer
305 InitSection(); // Section manager / code generator
306 InitMark(); // Mark tape-recorder
307 InitMacro(); // Macro processor
308 InitListing(); // Listing generator
309 Init6502(); // 6502 assembler
311 // Process command line arguments and assemble source files
312 for(argno=0; argno<argc; argno++)
314 if (*argv[argno] == '-')
316 switch (argv[argno][1])
318 case 'd': // Define symbol
320 for(s=argv[argno]+2; *s!=EOS;)
329 if (argv[argno][2] == EOS)
331 printf("-d: empty symbol\n");
336 sy = lookup(argv[argno] + 2, 0, 0);
340 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
344 sy->sattr = DEFINED | EQUATED | ABS;
345 sy->svalue = (*s ? (uint64_t)atoi(s) : 0);
347 case 'e': // Redirect error message output
349 err_fname = argv[argno] + 2;
351 case 'f': // -f<format>
353 switch (argv[argno][2])
356 case 'a': // -fa = Alcyon [the default]
360 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
364 case 'e': // -fe = ELF
368 case 'l': // -fl = LOD
372 case 'p': // -fp = P56
376 case 'x': // -fx = COM/EXE/XEX
380 case 'r': // -fr = Absolute address
385 printf("-f: unknown object format specified\n");
390 case 'g': // Debugging flag
392 printf("Debugging flag (-g) not yet implemented\n");
394 case 'i': // Set directory search path
397 searchpath = argv[argno] + 2;
399 // Check to see if include paths actually exist
400 if (strlen(searchpath) > 0)
402 DIR * test = opendir(searchpath);
406 printf("Invalid include path: %s\n", searchpath);
416 case 'l': // Produce listing file
418 if (*(argv[argno] + 2) == '*')
420 list_fname = argv[argno] + 3;
421 list_pag = 0; // Special case - turn off pagination
425 list_fname = argv[argno] + 2;
434 if (strcmp(argv[argno] + 2, "68000") == 0)
436 else if (strcmp(argv[argno] + 2, "68020") == 0)
438 else if (strcmp(argv[argno] + 2, "68030") == 0)
440 else if (strcmp(argv[argno] + 2, "68040") == 0)
442 else if (strcmp(argv[argno] + 2, "68060") == 0)
444 else if (strcmp(argv[argno] + 2, "68881") == 0)
446 else if (strcmp(argv[argno] + 2, "68882") == 0)
448 else if (strcmp(argv[argno] + 2, "56001") == 0)
450 else if (strcmp(argv[argno] + 2, "6502") == 0)
452 else if (strcmp(argv[argno] + 2, "tom") == 0)
454 else if (strcmp(argv[argno] + 2, "jerry") == 0)
458 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
463 case 'o': // Direct object file output
465 if (argv[argno][2] != EOS)
466 objfname = argv[argno] + 2;
471 printf("Missing argument to -o");
476 objfname = argv[argno];
480 case 'p': /* -p: generate ".PRG" executable output */
483 * -p .PRG generation w/o symbols
484 * -ps .PRG generation with symbols
485 * -px .PRG generation with extended symbols
487 switch (argv[argno][2])
504 printf("-p: syntax error\n");
509 // Enforce Alcyon object format - kind of silly
510 // to ask for .prg output without it!
513 case 'r': // Pad seg to requested boundary size
515 switch(argv[argno][2])
517 case 'w': case 'W': segpadsize = 2; break;
518 case 'l': case 'L': segpadsize = 4; break;
519 case 'p': case 'P': segpadsize = 8; break;
520 case 'd': case 'D': segpadsize = 16; break;
521 case 'q': case 'Q': segpadsize = 32; break;
522 default: segpadsize = 2; break; // Effective autoeven();
525 case 's': // Warn about possible short branches
529 case 'u': // Make undefined symbols .globl
533 case 'v': // Verbose flag
541 case 'x': // Turn on debugging
544 printf("~ Debugging ON\n");
546 case 'y': // -y<pagelen>
548 pagelen = atoi(argv[argno] + 2);
552 printf("-y: bad page length\n");
558 case EOS: // Input is stdin
559 if (firstfname == NULL) // Kludge first filename
560 firstfname = defname;
562 include(0, "(stdin)");
565 case 'h': // Display command line usage
572 case 'n': // Turn off legacy mode
575 printf("Legacy mode OFF\n");
579 printf("Unknown switch: %s\n\n", argv[argno]);
585 else if (*argv[argno] == '+' || *argv[argno] == '~')
587 if (ParseOptimization(argv[argno]) != OK)
590 printf("Unknown switch: %s\n\n", argv[argno]);
598 // Record first filename.
599 if (firstfname == NULL)
600 firstfname = argv[argno];
602 strcpy(fnbuf, argv[argno]);
603 fext(fnbuf, ".s", 0);
608 printf("Cannot open: %s\n", fnbuf);
618 // Wind-up processing;
619 // o save current section (no more code generation)
620 // o do auto-even of all sections (or boundary alignment as requested
622 // o save the last pc value for 6502 (if we used 6502 at all) and
623 // detemine if the last section contains anything
624 // o determine name of object file:
625 // - "foo.o" for linkable output;
626 // - "foo.prg" for GEMDOS executable (-p flag).
628 int temp_section = cursect;
630 for(i=TEXT; i<=BSS; i<<=1)
636 case 2: d_even(); break;
637 case 4: d_long(); break;
638 case 8: d_phrase(); break;
639 case 16: d_dphrase(); break;
640 case 32: d_qphrase(); break;
646 SwitchSection(M6502);
648 if (sloc != currentorg[0])
650 currentorg[1] = sloc;
654 // This looks like an awful kludge... !!! FIX !!!
655 if (temp_section & (M56001P | M56001X | M56001Y))
657 SwitchSection(temp_section);
659 if (chptr != dsp_currentorg->start)
661 dsp_currentorg->end = chptr;
668 if (objfname == NULL)
670 if (firstfname == NULL)
671 firstfname = defname;
673 strcpy(fnbuf, firstfname);
674 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
678 // With one pass finished, go back and:
679 // (1) run through all the fixups and resolve forward references;
680 // (1.5) ensure that remaining fixups can be handled by the linker
681 // (`lo68' format, extended (postfix) format....)
682 // (2) generate the output file image and symbol table;
683 // (3) generate relocation information from left-over fixups.
684 ResolveAllFixups(); // Do all fixups
685 StopMark(); // Stop mark tape-recorder
689 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
690 CantCreateFile(objfname);
694 s = (prg_flag ? "executable" : "object");
695 printf("[Writing %s file: %s]\n", s, objfname);
708 printf("[Wrapping-up listing file]\n");
718 DEBUG dump_everything();
725 // Determine processor endianess
727 int GetEndianess(void)
730 char * p = (char *)&i;
740 // Application entry point
742 int main(int argc, char ** argv)
744 perm_verb_flag = 0; // Clobber "permanent" verbose flag
745 legacy_flag = 1; // Default is legacy mode on (:-P)
747 // Set legacy optimisation flags to on and everything else to off
748 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
749 optim_flags[OPT_ABS_SHORT] =
750 optim_flags[OPT_MOVEL_MOVEQ] =
751 optim_flags[OPT_BSR_BCC_S] = 1;
753 cmdlnexec = argv[0]; // Obtain executable name
754 endian = GetEndianess(); // Get processor endianess
756 // If commands were passed in, process them
758 return Process(argc - 1, argv + 1);