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 glob_flag; // Assume undefined symbols are global
33 int lsym_flag; // Include local symbols in object file
34 int optim_warn_flag; // Warn about possible short branches
35 int prg_flag; // !=0, produce .PRG executable (2=symbols)
36 int prg_extend; // !=0, output extended .PRG symbols
37 int legacy_flag; // Do stuff like insert code in RISC assembler
38 int obj_format; // Object format flag
39 int debug; // [1..9] Enable debugging levels
40 int err_flag; // '-e' specified
41 int err_fd; // File to write error messages to
42 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
43 int robjproc; // Assembling Jaguar Object Processor code
44 int dsp56001; // Assembling DSP 56001 code
45 int list_fd; // File to write listing to
46 int segpadsize; // Segment padding size
47 int endian; // Host processor endianess (0 = LE, 1 = BE)
48 int *regbase; // Points to current DFA register table (base)
49 int *regtab; // Points to current DFA register table (tab)
50 int *regcheck; // Points to current DFA register table (check)
51 int *regaccept; // Points to current DFA register table (accept)
52 char * objfname; // Object filename pointer
53 char * firstfname; // First source filename
54 char * cmdlnexec; // Executable name, pointer to ARGV[0]
55 char searchpatha[512] = { 0 }; // Buffer to hold searchpath when specified
56 char * searchpath = NULL; // Search path for include files
57 char defname[] = "noname.o"; // Default output filename
58 int optim_flags[OPT_COUNT_ALL] = { 0 }; // Specific optimisations on/off matrix
59 int activecpu = CPU_68000; // Active 68k CPU (68000 by default)
60 int activefpu = FPU_NONE; // Active FPU (none by default)
61 int org68k_active = 0; // .org switch for 68k (only with RAW output format)
62 uint32_t org68k_address; // .org for 68k
65 // Convert a string to uppercase
67 void strtoupper(char * s)
74 // Manipulate file extension.
76 // 'name' must be large enough to hold any possible filename. If 'stripp' is
77 // nonzero, any old extension is removed. If the file does not already have an
78 // extension, 'extension' is appended to the filename.
80 char * fext(char * name, char * extension, int stripp)
84 // Find beginning of "real" name (strip off path)
85 char * beg = strrchr(name, SLASHCHAR);
90 // Clobber any old extension, if requested
93 for(s=beg; *s && *s!='.'; ++s)
99 if (strrchr(beg, '.') == NULL)
100 strcat(beg, extension);
105 static int is_sep(char c)
107 const char *seps = PATH_SEPS;
109 for (seps = PATH_SEPS; *seps; seps++) {
118 // Return 'item'nth element of semicolon-seperated pathnames specified in the
119 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
120 // nth path doesn't exist.
122 // ['item' ranges from 0 to N-1, where N = #elements in search path]
124 int nthpath(char * env_var, int itemno, char * buf)
126 char * s = searchpath;
135 while (*s != EOS && !is_sep(*s++))
141 while (*s != EOS && !is_sep(*s))
150 // Display command line help
152 void DisplayHelp(void)
155 " %s [options] srcfile\n"
158 " -? or -h Display usage information\n"
159 " -dsymbol[=value] Define symbol (with optional value, default=0)\n"
160 " -e[errorfile] Send error messages to file, not stdout\n"
161 " -f[format] Output object file format\n"
163 " b: BSD (use this for Jaguar)\n"
165 " p: P56 (use this for DSP56001 only)\n"
166 " l: LOD (use this for DSP56001 only)\n"
167 " x: com/exe/xex (Atari 800)\n"
168 " r: absolute address\n"
169 " -i[path] Directory to search for include files\n"
170 " -l[filename] Create an output listing file\n"
171 " -l*[filename] Create an output listing file without pagination\n"
172 " -m[cpu] Select default CPU. Available options:\n"
173 " 68000, 68020, 68030, 68040, 68060, 6502, tom, jerry, 56001\n"
174 " -n Don't do things behind your back in RISC assembler\n"
175 " -o file Output file name\n"
176 " +o[value] Turn a specific optimisation on\n"
177 " Available optimisation switches:\n"
178 " o0: Absolute long addresses to word\n"
179 " o1: move.l #x,dn/an to moveq\n"
180 " o2: Word branches to short\n"
181 " o3: Outer displacement 0(an) to (an)\n"
182 " o4: lea size(An),An to addq #size,An\n"
183 " o5: 68020+ Absolute long base/outer disp. to word\n"
184 " o6: Null branches to NOP\n"
185 " o7: clr.l Dx to moveq #0,Dx\n"
186 " o8: adda.w/l #x,Dy to addq.w/l #x,Dy\n"
187 " o9: adda.w/l #x,Ay to lea x(Dy),Ay\n"
188 " o10: 56001 Use short format for immediate values if possible\n"
189 " o11: 56001 Auto convert short addressing mode to long (default: on)\n"
190 " o30: Enforce PC relative (alternative name: op)\n"
191 " ~o[value] Turn a specific optimisation off\n"
192 " +oall Turn all optimisations on\n"
193 " ~oall Turn all optimisations off\n"
194 " -p Create an ST .prg (without symbols). Forces -fa\n"
195 " -ps Create an ST .prg (with symbols). Forces -fa\n"
196 " -px Create an ST .prg (with exsymbols). Forces -fa\n"
197 " -r[size] Pad segments to boundary size specified\n"
198 " w: word (2 bytes, default alignment)\n"
199 " l: long (4 bytes)\n"
200 " p: phrase (8 bytes)\n"
201 " d: double phrase (16 bytes)\n"
202 " q: quad phrase (32 bytes)\n"
203 " -s Warn about possible short branches\n"
204 " and applied optimisations\n"
205 " -u Force referenced and undefined symbols global\n"
206 " -v Set verbose mode\n"
207 " -x Turn on debugging mode\n"
208 " -y[pagelen] Set page line length (default: 61)\n"
214 // Display version information
216 void DisplayVersion(void)
219 " _ __ _ __ ___ __ _ ___ \n"
220 "| '__| '_ ` _ \\ / _` |/ __|\n"
221 "| | | | | | | | (_| | (__ \n"
222 "|_| |_| |_| |_|\\__,_|\\___|\n"
223 "\nRenamed Macro Assembler\n"
224 "Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends\n"
225 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
229 // Parse optimisation options
231 int ParseOptimization(char * optstring)
237 if (*optstring == '+')
239 else if (*optstring != '~')
242 if (optstring[2] == 0)
243 return error(".opt called with zero arguments");
245 if ((optstring[2] == 'a' || optstring[2] == 'A')
246 && (optstring[3] == 'l' || optstring[3] == 'L')
247 && (optstring[4] == 'l' || optstring[4] == 'L'))
249 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
252 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
254 if (optstring[2] == 'p' || optstring[2] == 'P')
256 optim_flags[OPT_PC_RELATIVE] = onoff;
261 int opt_no = atoi(&optstring[2]);
263 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
265 optim_flags[opt_no] = onoff;
268 // If opt_no is 2 digits then consume an extra character.
269 // Sounds a bit sleazy but we know we're not going to hit
270 // more than 100 optimisation switches so this should be
271 // fine. If we do hit that number then it'll probably be
272 // time to burn the whole codebase and start from scratch.
285 if (*optstring == ',')
291 extern int reg68base[53];
292 extern int reg68tab[222];
293 extern int reg68check[222];
294 extern int reg68accept[222];
297 // Process command line arguments and do an assembly
299 int Process(int argc, char ** argv)
301 int argno; // Argument number
302 SYM * sy; // Pointer to a symbol record
303 char * s; // String pointer
304 int fd; // File descriptor
305 char fnbuf[FNSIZ]; // Filename buffer
307 int current_path_index = 0; // Iterator for search paths
309 errcnt = 0; // Initialize error count
310 listing = 0; // Initialize listing level
311 list_flag = 0; // Initialize listing flag
312 verb_flag = perm_verb_flag; // Initialize verbose flag
313 glob_flag = 0; // Initialize .globl flag
314 optim_warn_flag = 0; // Initialize short branch flag
315 debug = 0; // Initialize debug flag
316 objfname = NULL; // Initialize object filename
317 list_fname = NULL; // Initialize listing filename
318 err_fname = NULL; // Initialize error filename
319 obj_format = BSD; // Initialize object format
320 firstfname = NULL; // Initialize first filename
321 err_fd = ERROUT; // Initialize error file descriptor
322 err_flag = 0; // Initialize error flag
323 rgpu = 0; // Initialize GPU assembly flag
324 rdsp = 0; // Initialize DSP assembly flag
325 robjproc = 0; // Initialize OP assembly flag
326 lsym_flag = 1; // Include local symbols in object file
327 orgactive = 0; // Not in RISC org section
328 orgwarning = 0; // No ORG warning issued
329 segpadsize = 2; // Initialize segment padding size
330 dsp_orgmap[0].start = 0; // Initialize 56001 org initial address
331 dsp_orgmap[0].memtype = ORG_P; // Initialize 56001 org start segment
332 m6502 = 0; // 6502 mode off by default
333 regbase = reg68base; // Initialise DFA register tables
334 regtab = reg68tab; // Idem
335 regcheck = reg68check; // Idem
336 regaccept = reg68accept; // Idem
338 // Initialize modules
339 InitSymbolTable(); // Symbol table
340 InitTokenizer(); // Tokenizer
341 InitLineProcessor(); // Line processor
342 InitExpression(); // Expression analyzer
343 InitSection(); // Section manager / code generator
344 InitMark(); // Mark tape-recorder
345 InitMacro(); // Macro processor
346 InitListing(); // Listing generator
347 Init6502(); // 6502 assembler
349 // Process command line arguments and assemble source files
350 for(argno=0; argno<argc; argno++)
352 if (*argv[argno] == '-')
354 switch (argv[argno][1])
356 case 'd': // Define symbol
358 for(s=argv[argno]+2; *s!=EOS;)
367 if (argv[argno][2] == EOS)
369 printf("-d: empty symbol\n");
374 sy = lookup(argv[argno] + 2, 0, 0);
378 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
382 sy->sattr = DEFINED | EQUATED | ABS;
383 sy->svalue = (*s ? (uint64_t)atoi(s) : 0);
385 case 'e': // Redirect error message output
387 err_fname = argv[argno] + 2;
389 case 'f': // -f<format>
391 switch (argv[argno][2])
394 case 'a': // -fa = Alcyon [the default]
398 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
402 case 'e': // -fe = ELF
406 case 'l': // -fl = LOD
410 case 'p': // -fp = P56
414 case 'x': // -fx = COM/EXE/XEX
418 case 'r': // -fr = Absolute address
423 printf("-f: unknown object format specified\n");
428 case 'g': // Debugging flag
430 printf("Debugging flag (-g) not yet implemented\n");
432 case 'i': // Set directory search path
435 strcat(searchpatha, argv[argno] + 2);
436 strcat(searchpatha, ";");
437 searchpath = searchpatha;
439 // Check to see if include paths actually exist
440 char current_path[256];
442 for(i=current_path_index; nthpath("RMACPATH", i, current_path)!=0; i++)
444 if (strlen(current_path) > 0)
446 DIR * test = opendir(current_path);
450 printf("Invalid include path: %s\n", current_path);
459 current_path_index = i - 1;
462 case 'l': // Produce listing file
464 if (*(argv[argno] + 2) == '*')
466 list_fname = argv[argno] + 3;
467 list_pag = 0; // Special case - turn off pagination
471 list_fname = argv[argno] + 2;
480 if (strcmp(argv[argno] + 2, "68000") == 0)
482 else if (strcmp(argv[argno] + 2, "68020") == 0)
484 else if (strcmp(argv[argno] + 2, "68030") == 0)
486 else if (strcmp(argv[argno] + 2, "68040") == 0)
488 else if (strcmp(argv[argno] + 2, "68060") == 0)
490 else if (strcmp(argv[argno] + 2, "68881") == 0)
492 else if (strcmp(argv[argno] + 2, "68882") == 0)
494 else if (strcmp(argv[argno] + 2, "56001") == 0)
496 else if (strcmp(argv[argno] + 2, "6502") == 0)
498 else if (strcmp(argv[argno] + 2, "tom") == 0)
500 else if (strcmp(argv[argno] + 2, "jerry") == 0)
504 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
509 case 'o': // Direct object file output
511 if (argv[argno][2] != EOS)
512 objfname = argv[argno] + 2;
517 printf("Missing argument to -o");
522 objfname = argv[argno];
526 case 'p': /* -p: generate ".PRG" executable output */
529 * -p .PRG generation w/o symbols
530 * -ps .PRG generation with symbols
531 * -px .PRG generation with extended symbols
533 switch (argv[argno][2])
550 printf("-p: syntax error\n");
555 // Enforce Alcyon object format - kind of silly
556 // to ask for .prg output without it!
559 case 'r': // Pad seg to requested boundary size
561 switch(argv[argno][2])
563 case 'w': case 'W': segpadsize = 2; break;
564 case 'l': case 'L': segpadsize = 4; break;
565 case 'p': case 'P': segpadsize = 8; break;
566 case 'd': case 'D': segpadsize = 16; break;
567 case 'q': case 'Q': segpadsize = 32; break;
568 default: segpadsize = 2; break; // Effective autoeven();
571 case 's': // Warn about possible short branches and applied optimisations
575 case 'u': // Make undefined symbols .globl
579 case 'v': // Verbose flag
587 case 'x': // Turn on debugging
590 printf("~ Debugging ON\n");
592 case 'y': // -y<pagelen>
594 pagelen = atoi(argv[argno] + 2);
598 printf("-y: bad page length\n");
604 case EOS: // Input is stdin
605 if (firstfname == NULL) // Kludge first filename
606 firstfname = defname;
608 include(0, "(stdin)");
611 case 'h': // Display command line usage
618 case 'n': // Turn off legacy mode
621 printf("Legacy mode OFF\n");
625 printf("Unknown switch: %s\n\n", argv[argno]);
631 else if (*argv[argno] == '+' || *argv[argno] == '~')
633 if (ParseOptimization(argv[argno]) != OK)
636 printf("Unknown switch: %s\n\n", argv[argno]);
644 // Record first filename.
645 if (firstfname == NULL)
646 firstfname = argv[argno];
648 strcpy(fnbuf, argv[argno]);
649 fext(fnbuf, ".s", 0);
654 printf("Cannot open: %s\n", fnbuf);
664 // Wind-up processing;
665 // o save current section (no more code generation)
666 // o do auto-even of all sections (or boundary alignment as requested
668 // o save the last pc value for 6502 (if we used 6502 at all) and
669 // detemine if the last section contains anything
670 // o determine name of object file:
671 // - "foo.o" for linkable output;
672 // - "foo.prg" for GEMDOS executable (-p flag).
674 int temp_section = cursect;
676 for(i=TEXT; i<=BSS; i<<=1)
682 case 2: d_even(); break;
683 case 4: d_long(); break;
684 case 8: d_phrase(); break;
685 case 16: d_dphrase(); break;
686 case 32: d_qphrase(); break;
692 SwitchSection(M6502);
694 if (sloc != currentorg[0])
696 currentorg[1] = sloc;
700 // This looks like an awful kludge... !!! FIX !!!
701 if (temp_section & (M56001P | M56001X | M56001Y))
703 SwitchSection(temp_section);
705 if (chptr != dsp_currentorg->start)
707 dsp_currentorg->end = chptr;
714 if (objfname == NULL)
716 if (firstfname == NULL)
717 firstfname = defname;
719 strcpy(fnbuf, firstfname);
720 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
724 // With one pass finished, go back and:
725 // (1) run through all the fixups and resolve forward references;
726 // (1.5) ensure that remaining fixups can be handled by the linker
727 // (`lo68' format, extended (postfix) format....)
728 // (2) generate the output file image and symbol table;
729 // (3) generate relocation information from left-over fixups.
730 ResolveAllFixups(); // Do all fixups
731 StopMark(); // Stop mark tape-recorder
735 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
736 CantCreateFile(objfname);
740 s = (prg_flag ? "executable" : "object");
741 printf("[Writing %s file: %s]\n", s, objfname);
754 printf("[Wrapping-up listing file]\n");
764 DEBUG dump_everything();
770 // Determine processor endianess
772 int GetEndianess(void)
775 char * p = (char *)&i;
784 // Application entry point
786 int main(int argc, char ** argv)
788 perm_verb_flag = 0; // Clobber "permanent" verbose flag
789 legacy_flag = 1; // Default is legacy mode on (:-P)
790 optim_flags[OPT_56K_SHORT] = 1; // This ensures compatibilty with Motorola's 56k assembler
792 cmdlnexec = argv[0]; // Obtain executable name
793 endian = GetEndianess(); // Get processor endianess
795 // If commands were passed in, process them
797 return Process(argc - 1, argv + 1);