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 optim_warn_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[512] = { 0 }; // 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 activecpu = CPU_68000; // Active 68k CPU (68000 by default)
57 int activefpu = FPU_NONE; // Active FPU (none by default)
58 int org68k_active = 0; // .org switch for 68k (only with RAW output format)
59 uint32_t org68k_address; // .org for 68k
62 // Convert a string to uppercase
64 void strtoupper(char * s)
72 // Manipulate file extension.
74 // 'name' must be large enough to hold any possible filename. If 'stripp' is
75 // nonzero, any old extension is removed. If the file does not already have an
76 // extension, 'extension' is appended to the filename.
78 char * fext(char * name, char * extension, int stripp)
82 // Find beginning of "real" name (strip off path)
83 char * beg = strrchr(name, SLASHCHAR);
88 // Clobber any old extension, if requested
91 for(s=beg; *s && *s!='.'; ++s)
97 if (strrchr(beg, '.') == NULL)
98 strcat(beg, extension);
105 // Return 'item'nth element of semicolon-seperated pathnames specified in the
106 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
107 // nth path doesn't exist.
109 // ['item' ranges from 0 to N-1, where N = #elements in search path]
111 int nthpath(char * env_var, int itemno, char * buf)
113 char * s = searchpath;
122 while (*s != EOS && *s++ != ';')
128 while (*s != EOS && *s != ';')
138 // Display command line help
140 void DisplayHelp(void)
143 " %s [options] srcfile\n"
146 " -? or -h Display usage information\n"
147 " -dsymbol[=value] Define symbol (with optional value, default=0)\n"
148 " -e[errorfile] Send error messages to file, not stdout\n"
149 " -f[format] Output object file format\n"
151 " b: BSD (use this for Jaguar)\n"
153 " p: P56 (use this for DSP56001 only)\n"
154 " l: LOD (use this for DSP56001 only)\n"
155 " x: com/exe/xex (Atari 800)\n"
156 " r: absolute address\n"
157 " -i[path] Directory to search for include files\n"
158 " -l[filename] Create an output listing file\n"
159 " -l*[filename] Create an output listing file without pagination\n"
160 " -m[cpu] Select default CPU. Available options:\n"
161 " 68000, 68020, 68030, 68040, 68060, 6502, tom, jerry, 56001\n"
162 " -n Don't do things behind your back in RISC assembler\n"
163 " -o file Output file name\n"
164 " +o[value] Turn a specific optimisation on\n"
165 " Available optimisation values and default settings:\n"
166 " o0: Absolute long addresses to word (on)\n"
167 " o1: move.l #x,dn/an to moveq (on)\n"
168 " o2: Word branches to short (on)\n"
169 " o3: Outer displacement 0(an) to (an) (off)\n"
170 " o4: lea size(An),An to addq #size,An (off)\n"
171 " o5: 68020+ Absolute long base/outer disp. to word (off)\n"
172 " o6: Null branches to NOP (off)\n"
173 " o7: clr.l Dx to moveq #0,Dx (off)\n"
174 " o8: adda.w/l #x,Dy to addq.w/l #x,Dy (off)\n"
175 " o9: adda.w/l #x,Dy to lea x(Dy),Dy (off)\n"
176 " op: Enforce PC relative (off)\n"
177 " ~o[value] Turn a specific optimisation off\n"
178 " +oall Turn all optimisations on\n"
179 " ~oall Turn all optimisations off\n"
180 " -p Create an ST .prg (without symbols). Forces -fa\n"
181 " -ps Create an ST .prg (with symbols). Forces -fa\n"
182 " -px Create an ST .prg (with exsymbols). Forces -fa\n"
183 " -r[size] Pad segments to boundary size specified\n"
184 " w: word (2 bytes, default alignment)\n"
185 " l: long (4 bytes)\n"
186 " p: phrase (8 bytes)\n"
187 " d: double phrase (16 bytes)\n"
188 " q: quad phrase (32 bytes)\n"
189 " -s Warn about possible short branches\n"
190 " and applied optimisations\n"
191 " -u Force referenced and undefined symbols global\n"
192 " -v Set verbose mode\n"
193 " -x Turn on debugging mode\n"
194 " -y[pagelen] Set page line length (default: 61)\n"
200 // Display version information
202 void DisplayVersion(void)
205 " _ __ _ __ ___ __ _ ___ \n"
206 "| '__| '_ ` _ \\ / _` |/ __|\n"
207 "| | | | | | | | (_| | (__ \n"
208 "|_| |_| |_| |_|\\__,_|\\___|\n"
209 "\nReboot's Macro Assembler\n"
210 "Copyright (C) 199x Landon Dyer, 2011-2020 Reboot\n"
211 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
216 // Parse optimisation options
218 int ParseOptimization(char * optstring)
224 if (*optstring == '+')
226 else if (*optstring != '~')
229 if ((optstring[2] == 'a' || optstring[2] == 'A')
230 && (optstring[3] == 'l' || optstring[3] == 'L')
231 && (optstring[4] == 'l' || optstring[4] == 'L'))
233 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
236 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
238 if (optstring[2] == 'p' || optstring[2] == 'P')
240 optim_flags[OPT_PC_RELATIVE] = onoff;
245 int opt_no = atoi(&optstring[2]);
247 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
249 optim_flags[opt_no] = onoff;
252 // If opt_no is 2 digits then consume an extra character.
253 // Sounds a bit sleazy but we know we're not going to hit
254 // more than 100 optimisation switches so this should be
255 // fine. If we do hit that number then it'll probably be
256 // time to burn the whole codebase and start from scratch.
269 if (*optstring == ',')
277 // Process command line arguments and do an assembly
279 int Process(int argc, char ** argv)
281 int argno; // Argument number
282 SYM * sy; // Pointer to a symbol record
283 char * s; // String pointer
284 int fd; // File descriptor
285 char fnbuf[FNSIZ]; // Filename buffer
287 int current_path_index = 0; // Iterator for search paths
289 errcnt = 0; // Initialize error count
290 listing = 0; // Initialize listing level
291 list_flag = 0; // Initialize listing flag
292 verb_flag = perm_verb_flag; // Initialize verbose flag
293 as68_flag = 0; // Initialize as68 kludge mode
294 glob_flag = 0; // Initialize .globl flag
295 optim_warn_flag = 0; // Initialize short branch flag
296 debug = 0; // Initialize debug flag
297 objfname = NULL; // Initialize object filename
298 list_fname = NULL; // Initialize listing filename
299 err_fname = NULL; // Initialize error filename
300 obj_format = BSD; // Initialize object format
301 firstfname = NULL; // Initialize first filename
302 err_fd = ERROUT; // Initialize error file descriptor
303 err_flag = 0; // Initialize error flag
304 rgpu = 0; // Initialize GPU assembly flag
305 rdsp = 0; // Initialize DSP assembly flag
306 robjproc = 0; // Initialize OP assembly flag
307 lsym_flag = 1; // Include local symbols in object file
308 regbank = BANK_N; // No RISC register bank specified
309 orgactive = 0; // Not in RISC org section
310 orgwarning = 0; // No ORG warning issued
311 segpadsize = 2; // Initialize segment padding size
312 dsp_orgmap[0].start = 0; // Initialize 56001 org initial address
313 dsp_orgmap[0].memtype = ORG_P; // Initialize 56001 org start segment
314 m6502 = 0; // 6502 mode off by default
316 // Initialize modules
317 InitSymbolTable(); // Symbol table
318 InitTokenizer(); // Tokenizer
319 InitLineProcessor(); // Line processor
320 InitExpression(); // Expression analyzer
321 InitSection(); // Section manager / code generator
322 InitMark(); // Mark tape-recorder
323 InitMacro(); // Macro processor
324 InitListing(); // Listing generator
325 Init6502(); // 6502 assembler
327 // Process command line arguments and assemble source files
328 for(argno=0; argno<argc; argno++)
330 if (*argv[argno] == '-')
332 switch (argv[argno][1])
334 case 'd': // Define symbol
336 for(s=argv[argno]+2; *s!=EOS;)
345 if (argv[argno][2] == EOS)
347 printf("-d: empty symbol\n");
352 sy = lookup(argv[argno] + 2, 0, 0);
356 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
360 sy->sattr = DEFINED | EQUATED | ABS;
361 sy->svalue = (*s ? (uint64_t)atoi(s) : 0);
363 case 'e': // Redirect error message output
365 err_fname = argv[argno] + 2;
367 case 'f': // -f<format>
369 switch (argv[argno][2])
372 case 'a': // -fa = Alcyon [the default]
376 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
380 case 'e': // -fe = ELF
384 case 'l': // -fl = LOD
388 case 'p': // -fp = P56
392 case 'x': // -fx = COM/EXE/XEX
396 case 'r': // -fr = Absolute address
401 printf("-f: unknown object format specified\n");
406 case 'g': // Debugging flag
408 printf("Debugging flag (-g) not yet implemented\n");
410 case 'i': // Set directory search path
413 strcat(searchpath, argv[argno] + 2);
414 strcat(searchpath, ";");
416 // Check to see if include paths actually exist
417 char current_path[256];
419 for(i=current_path_index; nthpath("RMACPATH", i, current_path)!=0; i++)
421 if (strlen(current_path) > 0)
423 DIR * test = opendir(current_path);
427 printf("Invalid include path: %s\n", current_path);
436 current_path_index = i - 1;
439 case 'l': // Produce listing file
441 if (*(argv[argno] + 2) == '*')
443 list_fname = argv[argno] + 3;
444 list_pag = 0; // Special case - turn off pagination
448 list_fname = argv[argno] + 2;
457 if (strcmp(argv[argno] + 2, "68000") == 0)
459 else if (strcmp(argv[argno] + 2, "68020") == 0)
461 else if (strcmp(argv[argno] + 2, "68030") == 0)
463 else if (strcmp(argv[argno] + 2, "68040") == 0)
465 else if (strcmp(argv[argno] + 2, "68060") == 0)
467 else if (strcmp(argv[argno] + 2, "68881") == 0)
469 else if (strcmp(argv[argno] + 2, "68882") == 0)
471 else if (strcmp(argv[argno] + 2, "56001") == 0)
473 else if (strcmp(argv[argno] + 2, "6502") == 0)
475 else if (strcmp(argv[argno] + 2, "tom") == 0)
477 else if (strcmp(argv[argno] + 2, "jerry") == 0)
481 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
486 case 'o': // Direct object file output
488 if (argv[argno][2] != EOS)
489 objfname = argv[argno] + 2;
494 printf("Missing argument to -o");
499 objfname = argv[argno];
503 case 'p': /* -p: generate ".PRG" executable output */
506 * -p .PRG generation w/o symbols
507 * -ps .PRG generation with symbols
508 * -px .PRG generation with extended symbols
510 switch (argv[argno][2])
527 printf("-p: syntax error\n");
532 // Enforce Alcyon object format - kind of silly
533 // to ask for .prg output without it!
536 case 'r': // Pad seg to requested boundary size
538 switch(argv[argno][2])
540 case 'w': case 'W': segpadsize = 2; break;
541 case 'l': case 'L': segpadsize = 4; break;
542 case 'p': case 'P': segpadsize = 8; break;
543 case 'd': case 'D': segpadsize = 16; break;
544 case 'q': case 'Q': segpadsize = 32; break;
545 default: segpadsize = 2; break; // Effective autoeven();
548 case 's': // Warn about possible short branches and applied optimisations
552 case 'u': // Make undefined symbols .globl
556 case 'v': // Verbose flag
564 case 'x': // Turn on debugging
567 printf("~ Debugging ON\n");
569 case 'y': // -y<pagelen>
571 pagelen = atoi(argv[argno] + 2);
575 printf("-y: bad page length\n");
581 case EOS: // Input is stdin
582 if (firstfname == NULL) // Kludge first filename
583 firstfname = defname;
585 include(0, "(stdin)");
588 case 'h': // Display command line usage
595 case 'n': // Turn off legacy mode
598 printf("Legacy mode OFF\n");
602 printf("Unknown switch: %s\n\n", argv[argno]);
608 else if (*argv[argno] == '+' || *argv[argno] == '~')
610 if (ParseOptimization(argv[argno]) != OK)
613 printf("Unknown switch: %s\n\n", argv[argno]);
621 // Record first filename.
622 if (firstfname == NULL)
623 firstfname = argv[argno];
625 strcpy(fnbuf, argv[argno]);
626 fext(fnbuf, ".s", 0);
631 printf("Cannot open: %s\n", fnbuf);
641 // Wind-up processing;
642 // o save current section (no more code generation)
643 // o do auto-even of all sections (or boundary alignment as requested
645 // o save the last pc value for 6502 (if we used 6502 at all) and
646 // detemine if the last section contains anything
647 // o determine name of object file:
648 // - "foo.o" for linkable output;
649 // - "foo.prg" for GEMDOS executable (-p flag).
651 int temp_section = cursect;
653 for(i=TEXT; i<=BSS; i<<=1)
659 case 2: d_even(); break;
660 case 4: d_long(); break;
661 case 8: d_phrase(); break;
662 case 16: d_dphrase(); break;
663 case 32: d_qphrase(); break;
669 SwitchSection(M6502);
671 if (sloc != currentorg[0])
673 currentorg[1] = sloc;
677 // This looks like an awful kludge... !!! FIX !!!
678 if (temp_section & (M56001P | M56001X | M56001Y))
680 SwitchSection(temp_section);
682 if (chptr != dsp_currentorg->start)
684 dsp_currentorg->end = chptr;
691 if (objfname == NULL)
693 if (firstfname == NULL)
694 firstfname = defname;
696 strcpy(fnbuf, firstfname);
697 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
701 // With one pass finished, go back and:
702 // (1) run through all the fixups and resolve forward references;
703 // (1.5) ensure that remaining fixups can be handled by the linker
704 // (`lo68' format, extended (postfix) format....)
705 // (2) generate the output file image and symbol table;
706 // (3) generate relocation information from left-over fixups.
707 ResolveAllFixups(); // Do all fixups
708 StopMark(); // Stop mark tape-recorder
712 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
713 CantCreateFile(objfname);
717 s = (prg_flag ? "executable" : "object");
718 printf("[Writing %s file: %s]\n", s, objfname);
731 printf("[Wrapping-up listing file]\n");
741 DEBUG dump_everything();
748 // Determine processor endianess
750 int GetEndianess(void)
753 char * p = (char *)&i;
763 // Application entry point
765 int main(int argc, char ** argv)
767 perm_verb_flag = 0; // Clobber "permanent" verbose flag
768 legacy_flag = 1; // Default is legacy mode on (:-P)
770 // Set legacy optimisation flags to on and everything else to off
771 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
772 optim_flags[OPT_ABS_SHORT] =
773 optim_flags[OPT_MOVEL_MOVEQ] =
774 optim_flags[OPT_BSR_BCC_S] = 1;
776 cmdlnexec = argv[0]; // Obtain executable name
777 endian = GetEndianess(); // Get processor endianess
779 // If commands were passed in, process them
781 return Process(argc - 1, argv + 1);