2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2021 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 searchpatha[512] = { 0 }; // Buffer to hold searchpath when specified
54 char * searchpath = NULL; // Search path for include files
55 char defname[] = "noname.o"; // Default output filename
56 int optim_flags[OPT_COUNT_ALL]; // Specific optimisations on/off matrix
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)
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);
103 static int is_sep(char c)
105 const char *seps = PATH_SEPS;
107 for (seps = PATH_SEPS; *seps; seps++) {
116 // Return 'item'nth element of semicolon-seperated pathnames specified in the
117 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
118 // nth path doesn't exist.
120 // ['item' ranges from 0 to N-1, where N = #elements in search path]
122 int nthpath(char * env_var, int itemno, char * buf)
124 char * s = searchpath;
133 while (*s != EOS && !is_sep(*s++))
139 while (*s != EOS && !is_sep(*s))
148 // Display command line help
150 void DisplayHelp(void)
153 " %s [options] srcfile\n"
156 " -? or -h Display usage information\n"
157 " -dsymbol[=value] Define symbol (with optional value, default=0)\n"
158 " -e[errorfile] Send error messages to file, not stdout\n"
159 " -f[format] Output object file format\n"
161 " b: BSD (use this for Jaguar)\n"
163 " p: P56 (use this for DSP56001 only)\n"
164 " l: LOD (use this for DSP56001 only)\n"
165 " x: com/exe/xex (Atari 800)\n"
166 " r: absolute address\n"
167 " -i[path] Directory to search for include files\n"
168 " -l[filename] Create an output listing file\n"
169 " -l*[filename] Create an output listing file without pagination\n"
170 " -m[cpu] Select default CPU. Available options:\n"
171 " 68000, 68020, 68030, 68040, 68060, 6502, tom, jerry, 56001\n"
172 " -n Don't do things behind your back in RISC assembler\n"
173 " -o file Output file name\n"
174 " +o[value] Turn a specific optimisation on\n"
175 " Available optimisation values and default settings:\n"
176 " o0: Absolute long addresses to word (on)\n"
177 " o1: move.l #x,dn/an to moveq (on)\n"
178 " o2: Word branches to short (on)\n"
179 " o3: Outer displacement 0(an) to (an) (off)\n"
180 " o4: lea size(An),An to addq #size,An (off)\n"
181 " o5: 68020+ Absolute long base/outer disp. to word (off)\n"
182 " o6: Null branches to NOP (off)\n"
183 " o7: clr.l Dx to moveq #0,Dx (off)\n"
184 " o8: adda.w/l #x,Dy to addq.w/l #x,Dy (off)\n"
185 " o9: adda.w/l #x,Dy to lea x(Dy),Dy (off)\n"
186 " o10: Enforce PC relative (alternative: op) (off)\n"
187 " ~o[value] Turn a specific optimisation off\n"
188 " +oall Turn all optimisations on\n"
189 " ~oall Turn all optimisations off\n"
190 " -p Create an ST .prg (without symbols). Forces -fa\n"
191 " -ps Create an ST .prg (with symbols). Forces -fa\n"
192 " -px Create an ST .prg (with exsymbols). Forces -fa\n"
193 " -r[size] Pad segments to boundary size specified\n"
194 " w: word (2 bytes, default alignment)\n"
195 " l: long (4 bytes)\n"
196 " p: phrase (8 bytes)\n"
197 " d: double phrase (16 bytes)\n"
198 " q: quad phrase (32 bytes)\n"
199 " -s Warn about possible short branches\n"
200 " and applied optimisations\n"
201 " -u Force referenced and undefined symbols global\n"
202 " -v Set verbose mode\n"
203 " -x Turn on debugging mode\n"
204 " -y[pagelen] Set page line length (default: 61)\n"
210 // Display version information
212 void DisplayVersion(void)
215 " _ __ _ __ ___ __ _ ___ \n"
216 "| '__| '_ ` _ \\ / _` |/ __|\n"
217 "| | | | | | | | (_| | (__ \n"
218 "|_| |_| |_| |_|\\__,_|\\___|\n"
219 "\nRenamed Macro Assembler\n"
220 "Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends\n"
221 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
225 // Parse optimisation options
227 int ParseOptimization(char * optstring)
233 if (*optstring == '+')
235 else if (*optstring != '~')
238 if (optstring[2] == 0)
239 return error(".opt called with zero arguments");
241 if ((optstring[2] == 'a' || optstring[2] == 'A')
242 && (optstring[3] == 'l' || optstring[3] == 'L')
243 && (optstring[4] == 'l' || optstring[4] == 'L'))
245 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
248 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
250 if (optstring[2] == 'p' || optstring[2] == 'P')
252 optim_flags[OPT_PC_RELATIVE] = onoff;
257 int opt_no = atoi(&optstring[2]);
259 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
261 optim_flags[opt_no] = onoff;
264 // If opt_no is 2 digits then consume an extra character.
265 // Sounds a bit sleazy but we know we're not going to hit
266 // more than 100 optimisation switches so this should be
267 // fine. If we do hit that number then it'll probably be
268 // time to burn the whole codebase and start from scratch.
281 if (*optstring == ',')
288 // Process command line arguments and do an assembly
290 int Process(int argc, char ** argv)
292 int argno; // Argument number
293 SYM * sy; // Pointer to a symbol record
294 char * s; // String pointer
295 int fd; // File descriptor
296 char fnbuf[FNSIZ]; // Filename buffer
298 int current_path_index = 0; // Iterator for search paths
300 errcnt = 0; // Initialize error count
301 listing = 0; // Initialize listing level
302 list_flag = 0; // Initialize listing flag
303 verb_flag = perm_verb_flag; // Initialize verbose flag
304 as68_flag = 0; // Initialize as68 kludge mode
305 glob_flag = 0; // Initialize .globl flag
306 optim_warn_flag = 0; // Initialize short branch flag
307 debug = 0; // Initialize debug flag
308 objfname = NULL; // Initialize object filename
309 list_fname = NULL; // Initialize listing filename
310 err_fname = NULL; // Initialize error filename
311 obj_format = BSD; // Initialize object format
312 firstfname = NULL; // Initialize first filename
313 err_fd = ERROUT; // Initialize error file descriptor
314 err_flag = 0; // Initialize error flag
315 rgpu = 0; // Initialize GPU assembly flag
316 rdsp = 0; // Initialize DSP assembly flag
317 robjproc = 0; // Initialize OP assembly flag
318 lsym_flag = 1; // Include local symbols in object file
319 regbank = BANK_N; // No RISC register bank specified
320 orgactive = 0; // Not in RISC org section
321 orgwarning = 0; // No ORG warning issued
322 segpadsize = 2; // Initialize segment padding size
323 dsp_orgmap[0].start = 0; // Initialize 56001 org initial address
324 dsp_orgmap[0].memtype = ORG_P; // Initialize 56001 org start segment
325 m6502 = 0; // 6502 mode off by default
327 // Initialize modules
328 InitSymbolTable(); // Symbol table
329 InitTokenizer(); // Tokenizer
330 InitLineProcessor(); // Line processor
331 InitExpression(); // Expression analyzer
332 InitSection(); // Section manager / code generator
333 InitMark(); // Mark tape-recorder
334 InitMacro(); // Macro processor
335 InitListing(); // Listing generator
336 Init6502(); // 6502 assembler
338 // Process command line arguments and assemble source files
339 for(argno=0; argno<argc; argno++)
341 if (*argv[argno] == '-')
343 switch (argv[argno][1])
345 case 'd': // Define symbol
347 for(s=argv[argno]+2; *s!=EOS;)
356 if (argv[argno][2] == EOS)
358 printf("-d: empty symbol\n");
363 sy = lookup(argv[argno] + 2, 0, 0);
367 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
371 sy->sattr = DEFINED | EQUATED | ABS;
372 sy->svalue = (*s ? (uint64_t)atoi(s) : 0);
374 case 'e': // Redirect error message output
376 err_fname = argv[argno] + 2;
378 case 'f': // -f<format>
380 switch (argv[argno][2])
383 case 'a': // -fa = Alcyon [the default]
387 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
391 case 'e': // -fe = ELF
395 case 'l': // -fl = LOD
399 case 'p': // -fp = P56
403 case 'x': // -fx = COM/EXE/XEX
407 case 'r': // -fr = Absolute address
412 printf("-f: unknown object format specified\n");
417 case 'g': // Debugging flag
419 printf("Debugging flag (-g) not yet implemented\n");
421 case 'i': // Set directory search path
424 strcat(searchpatha, argv[argno] + 2);
425 strcat(searchpatha, ";");
426 searchpath = searchpatha;
428 // Check to see if include paths actually exist
429 char current_path[256];
431 for(i=current_path_index; nthpath("RMACPATH", i, current_path)!=0; i++)
433 if (strlen(current_path) > 0)
435 DIR * test = opendir(current_path);
439 printf("Invalid include path: %s\n", current_path);
448 current_path_index = i - 1;
451 case 'l': // Produce listing file
453 if (*(argv[argno] + 2) == '*')
455 list_fname = argv[argno] + 3;
456 list_pag = 0; // Special case - turn off pagination
460 list_fname = argv[argno] + 2;
469 if (strcmp(argv[argno] + 2, "68000") == 0)
471 else if (strcmp(argv[argno] + 2, "68020") == 0)
473 else if (strcmp(argv[argno] + 2, "68030") == 0)
475 else if (strcmp(argv[argno] + 2, "68040") == 0)
477 else if (strcmp(argv[argno] + 2, "68060") == 0)
479 else if (strcmp(argv[argno] + 2, "68881") == 0)
481 else if (strcmp(argv[argno] + 2, "68882") == 0)
483 else if (strcmp(argv[argno] + 2, "56001") == 0)
485 else if (strcmp(argv[argno] + 2, "6502") == 0)
487 else if (strcmp(argv[argno] + 2, "tom") == 0)
489 else if (strcmp(argv[argno] + 2, "jerry") == 0)
493 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
498 case 'o': // Direct object file output
500 if (argv[argno][2] != EOS)
501 objfname = argv[argno] + 2;
506 printf("Missing argument to -o");
511 objfname = argv[argno];
515 case 'p': /* -p: generate ".PRG" executable output */
518 * -p .PRG generation w/o symbols
519 * -ps .PRG generation with symbols
520 * -px .PRG generation with extended symbols
522 switch (argv[argno][2])
539 printf("-p: syntax error\n");
544 // Enforce Alcyon object format - kind of silly
545 // to ask for .prg output without it!
548 case 'r': // Pad seg to requested boundary size
550 switch(argv[argno][2])
552 case 'w': case 'W': segpadsize = 2; break;
553 case 'l': case 'L': segpadsize = 4; break;
554 case 'p': case 'P': segpadsize = 8; break;
555 case 'd': case 'D': segpadsize = 16; break;
556 case 'q': case 'Q': segpadsize = 32; break;
557 default: segpadsize = 2; break; // Effective autoeven();
560 case 's': // Warn about possible short branches and applied optimisations
564 case 'u': // Make undefined symbols .globl
568 case 'v': // Verbose flag
576 case 'x': // Turn on debugging
579 printf("~ Debugging ON\n");
581 case 'y': // -y<pagelen>
583 pagelen = atoi(argv[argno] + 2);
587 printf("-y: bad page length\n");
593 case EOS: // Input is stdin
594 if (firstfname == NULL) // Kludge first filename
595 firstfname = defname;
597 include(0, "(stdin)");
600 case 'h': // Display command line usage
607 case 'n': // Turn off legacy mode
610 printf("Legacy mode OFF\n");
614 printf("Unknown switch: %s\n\n", argv[argno]);
620 else if (*argv[argno] == '+' || *argv[argno] == '~')
622 if (ParseOptimization(argv[argno]) != OK)
625 printf("Unknown switch: %s\n\n", argv[argno]);
633 // Record first filename.
634 if (firstfname == NULL)
635 firstfname = argv[argno];
637 strcpy(fnbuf, argv[argno]);
638 fext(fnbuf, ".s", 0);
643 printf("Cannot open: %s\n", fnbuf);
653 // Wind-up processing;
654 // o save current section (no more code generation)
655 // o do auto-even of all sections (or boundary alignment as requested
657 // o save the last pc value for 6502 (if we used 6502 at all) and
658 // detemine if the last section contains anything
659 // o determine name of object file:
660 // - "foo.o" for linkable output;
661 // - "foo.prg" for GEMDOS executable (-p flag).
663 int temp_section = cursect;
665 for(i=TEXT; i<=BSS; i<<=1)
671 case 2: d_even(); break;
672 case 4: d_long(); break;
673 case 8: d_phrase(); break;
674 case 16: d_dphrase(); break;
675 case 32: d_qphrase(); break;
681 SwitchSection(M6502);
683 if (sloc != currentorg[0])
685 currentorg[1] = sloc;
689 // This looks like an awful kludge... !!! FIX !!!
690 if (temp_section & (M56001P | M56001X | M56001Y))
692 SwitchSection(temp_section);
694 if (chptr != dsp_currentorg->start)
696 dsp_currentorg->end = chptr;
703 if (objfname == NULL)
705 if (firstfname == NULL)
706 firstfname = defname;
708 strcpy(fnbuf, firstfname);
709 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
713 // With one pass finished, go back and:
714 // (1) run through all the fixups and resolve forward references;
715 // (1.5) ensure that remaining fixups can be handled by the linker
716 // (`lo68' format, extended (postfix) format....)
717 // (2) generate the output file image and symbol table;
718 // (3) generate relocation information from left-over fixups.
719 ResolveAllFixups(); // Do all fixups
720 StopMark(); // Stop mark tape-recorder
724 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
725 CantCreateFile(objfname);
729 s = (prg_flag ? "executable" : "object");
730 printf("[Writing %s file: %s]\n", s, objfname);
743 printf("[Wrapping-up listing file]\n");
753 DEBUG dump_everything();
759 // Determine processor endianess
761 int GetEndianess(void)
764 char * p = (char *)&i;
773 // Application entry point
775 int main(int argc, char ** argv)
777 perm_verb_flag = 0; // Clobber "permanent" verbose flag
778 legacy_flag = 1; // Default is legacy mode on (:-P)
780 // Set legacy optimisation flags to on and everything else to off
781 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
782 optim_flags[OPT_ABS_SHORT] =
783 optim_flags[OPT_MOVEL_MOVEQ] =
784 optim_flags[OPT_BSR_BCC_S] = 1;
786 cmdlnexec = argv[0]; // Obtain executable name
787 endian = GetEndianess(); // Get processor endianess
789 // If commands were passed in, process them
791 return Process(argc - 1, argv + 1);