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]; // 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)
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);
104 static int is_sep(char c)
106 const char *seps = PATH_SEPS;
108 for (seps = PATH_SEPS; *seps; seps++) {
117 // Return 'item'nth element of semicolon-seperated pathnames specified in the
118 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
119 // nth path doesn't exist.
121 // ['item' ranges from 0 to N-1, where N = #elements in search path]
123 int nthpath(char * env_var, int itemno, char * buf)
125 char * s = searchpath;
134 while (*s != EOS && !is_sep(*s++))
140 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 values and default settings:\n"
178 " o0: Absolute long addresses to word (on)\n"
179 " o1: move.l #x,dn/an to moveq (on)\n"
180 " o2: Word branches to short (on)\n"
181 " o3: Outer displacement 0(an) to (an) (off)\n"
182 " o4: lea size(An),An to addq #size,An (off)\n"
183 " o5: 68020+ Absolute long base/outer disp. to word (off)\n"
184 " o6: Null branches to NOP (off)\n"
185 " o7: clr.l Dx to moveq #0,Dx (off)\n"
186 " o8: adda.w/l #x,Dy to addq.w/l #x,Dy (off)\n"
187 " o9: adda.w/l #x,Dy to lea x(Dy),Dy (off)\n"
188 " op: Enforce PC relative (off)\n"
189 " ~o[value] Turn a specific optimisation off\n"
190 " +oall Turn all optimisations on\n"
191 " ~oall Turn all optimisations off\n"
192 " -p Create an ST .prg (without symbols). Forces -fa\n"
193 " -ps Create an ST .prg (with symbols). Forces -fa\n"
194 " -px Create an ST .prg (with exsymbols). Forces -fa\n"
195 " -r[size] Pad segments to boundary size specified\n"
196 " w: word (2 bytes, default alignment)\n"
197 " l: long (4 bytes)\n"
198 " p: phrase (8 bytes)\n"
199 " d: double phrase (16 bytes)\n"
200 " q: quad phrase (32 bytes)\n"
201 " -s Warn about possible short branches\n"
202 " and applied optimisations\n"
203 " -u Force referenced and undefined symbols global\n"
204 " -v Set verbose mode\n"
205 " -x Turn on debugging mode\n"
206 " -y[pagelen] Set page line length (default: 61)\n"
212 // Display version information
214 void DisplayVersion(void)
217 " _ __ _ __ ___ __ _ ___ \n"
218 "| '__| '_ ` _ \\ / _` |/ __|\n"
219 "| | | | | | | | (_| | (__ \n"
220 "|_| |_| |_| |_|\\__,_|\\___|\n"
221 "\nRenamed Macro Assembler\n"
222 "Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends\n"
223 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
228 // Parse optimisation options
230 int ParseOptimization(char * optstring)
236 if (*optstring == '+')
238 else if (*optstring != '~')
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 == ',')
289 // Process command line arguments and do an assembly
291 int Process(int argc, char ** argv)
293 int argno; // Argument number
294 SYM * sy; // Pointer to a symbol record
295 char * s; // String pointer
296 int fd; // File descriptor
297 char fnbuf[FNSIZ]; // Filename buffer
299 int current_path_index = 0; // Iterator for search paths
301 errcnt = 0; // Initialize error count
302 listing = 0; // Initialize listing level
303 list_flag = 0; // Initialize listing flag
304 verb_flag = perm_verb_flag; // Initialize verbose flag
305 as68_flag = 0; // Initialize as68 kludge mode
306 glob_flag = 0; // Initialize .globl flag
307 optim_warn_flag = 0; // Initialize short branch flag
308 debug = 0; // Initialize debug flag
309 objfname = NULL; // Initialize object filename
310 list_fname = NULL; // Initialize listing filename
311 err_fname = NULL; // Initialize error filename
312 obj_format = BSD; // Initialize object format
313 firstfname = NULL; // Initialize first filename
314 err_fd = ERROUT; // Initialize error file descriptor
315 err_flag = 0; // Initialize error flag
316 rgpu = 0; // Initialize GPU assembly flag
317 rdsp = 0; // Initialize DSP assembly flag
318 robjproc = 0; // Initialize OP assembly flag
319 lsym_flag = 1; // Include local symbols in object file
320 regbank = BANK_N; // No RISC register bank specified
321 orgactive = 0; // Not in RISC org section
322 orgwarning = 0; // No ORG warning issued
323 segpadsize = 2; // Initialize segment padding size
324 dsp_orgmap[0].start = 0; // Initialize 56001 org initial address
325 dsp_orgmap[0].memtype = ORG_P; // Initialize 56001 org start segment
326 m6502 = 0; // 6502 mode off by default
328 // Initialize modules
329 InitSymbolTable(); // Symbol table
330 InitTokenizer(); // Tokenizer
331 InitLineProcessor(); // Line processor
332 InitExpression(); // Expression analyzer
333 InitSection(); // Section manager / code generator
334 InitMark(); // Mark tape-recorder
335 InitMacro(); // Macro processor
336 InitListing(); // Listing generator
337 Init6502(); // 6502 assembler
339 // Process command line arguments and assemble source files
340 for(argno=0; argno<argc; argno++)
342 if (*argv[argno] == '-')
344 switch (argv[argno][1])
346 case 'd': // Define symbol
348 for(s=argv[argno]+2; *s!=EOS;)
357 if (argv[argno][2] == EOS)
359 printf("-d: empty symbol\n");
364 sy = lookup(argv[argno] + 2, 0, 0);
368 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
372 sy->sattr = DEFINED | EQUATED | ABS;
373 sy->svalue = (*s ? (uint64_t)atoi(s) : 0);
375 case 'e': // Redirect error message output
377 err_fname = argv[argno] + 2;
379 case 'f': // -f<format>
381 switch (argv[argno][2])
384 case 'a': // -fa = Alcyon [the default]
388 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
392 case 'e': // -fe = ELF
396 case 'l': // -fl = LOD
400 case 'p': // -fp = P56
404 case 'x': // -fx = COM/EXE/XEX
408 case 'r': // -fr = Absolute address
413 printf("-f: unknown object format specified\n");
418 case 'g': // Debugging flag
420 printf("Debugging flag (-g) not yet implemented\n");
422 case 'i': // Set directory search path
425 strcat(searchpatha, argv[argno] + 2);
426 strcat(searchpatha, ";");
427 searchpath = searchpatha;
429 // Check to see if include paths actually exist
430 char current_path[256];
432 for(i=current_path_index; nthpath("RMACPATH", i, current_path)!=0; i++)
434 if (strlen(current_path) > 0)
436 DIR * test = opendir(current_path);
440 printf("Invalid include path: %s\n", current_path);
449 current_path_index = i - 1;
452 case 'l': // Produce listing file
454 if (*(argv[argno] + 2) == '*')
456 list_fname = argv[argno] + 3;
457 list_pag = 0; // Special case - turn off pagination
461 list_fname = argv[argno] + 2;
470 if (strcmp(argv[argno] + 2, "68000") == 0)
472 else if (strcmp(argv[argno] + 2, "68020") == 0)
474 else if (strcmp(argv[argno] + 2, "68030") == 0)
476 else if (strcmp(argv[argno] + 2, "68040") == 0)
478 else if (strcmp(argv[argno] + 2, "68060") == 0)
480 else if (strcmp(argv[argno] + 2, "68881") == 0)
482 else if (strcmp(argv[argno] + 2, "68882") == 0)
484 else if (strcmp(argv[argno] + 2, "56001") == 0)
486 else if (strcmp(argv[argno] + 2, "6502") == 0)
488 else if (strcmp(argv[argno] + 2, "tom") == 0)
490 else if (strcmp(argv[argno] + 2, "jerry") == 0)
494 printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
499 case 'o': // Direct object file output
501 if (argv[argno][2] != EOS)
502 objfname = argv[argno] + 2;
507 printf("Missing argument to -o");
512 objfname = argv[argno];
516 case 'p': /* -p: generate ".PRG" executable output */
519 * -p .PRG generation w/o symbols
520 * -ps .PRG generation with symbols
521 * -px .PRG generation with extended symbols
523 switch (argv[argno][2])
540 printf("-p: syntax error\n");
545 // Enforce Alcyon object format - kind of silly
546 // to ask for .prg output without it!
549 case 'r': // Pad seg to requested boundary size
551 switch(argv[argno][2])
553 case 'w': case 'W': segpadsize = 2; break;
554 case 'l': case 'L': segpadsize = 4; break;
555 case 'p': case 'P': segpadsize = 8; break;
556 case 'd': case 'D': segpadsize = 16; break;
557 case 'q': case 'Q': segpadsize = 32; break;
558 default: segpadsize = 2; break; // Effective autoeven();
561 case 's': // Warn about possible short branches and applied optimisations
565 case 'u': // Make undefined symbols .globl
569 case 'v': // Verbose flag
577 case 'x': // Turn on debugging
580 printf("~ Debugging ON\n");
582 case 'y': // -y<pagelen>
584 pagelen = atoi(argv[argno] + 2);
588 printf("-y: bad page length\n");
594 case EOS: // Input is stdin
595 if (firstfname == NULL) // Kludge first filename
596 firstfname = defname;
598 include(0, "(stdin)");
601 case 'h': // Display command line usage
608 case 'n': // Turn off legacy mode
611 printf("Legacy mode OFF\n");
615 printf("Unknown switch: %s\n\n", argv[argno]);
621 else if (*argv[argno] == '+' || *argv[argno] == '~')
623 if (ParseOptimization(argv[argno]) != OK)
626 printf("Unknown switch: %s\n\n", argv[argno]);
634 // Record first filename.
635 if (firstfname == NULL)
636 firstfname = argv[argno];
638 strcpy(fnbuf, argv[argno]);
639 fext(fnbuf, ".s", 0);
644 printf("Cannot open: %s\n", fnbuf);
654 // Wind-up processing;
655 // o save current section (no more code generation)
656 // o do auto-even of all sections (or boundary alignment as requested
658 // o save the last pc value for 6502 (if we used 6502 at all) and
659 // detemine if the last section contains anything
660 // o determine name of object file:
661 // - "foo.o" for linkable output;
662 // - "foo.prg" for GEMDOS executable (-p flag).
664 int temp_section = cursect;
666 for(i=TEXT; i<=BSS; i<<=1)
672 case 2: d_even(); break;
673 case 4: d_long(); break;
674 case 8: d_phrase(); break;
675 case 16: d_dphrase(); break;
676 case 32: d_qphrase(); break;
682 SwitchSection(M6502);
684 if (sloc != currentorg[0])
686 currentorg[1] = sloc;
690 // This looks like an awful kludge... !!! FIX !!!
691 if (temp_section & (M56001P | M56001X | M56001Y))
693 SwitchSection(temp_section);
695 if (chptr != dsp_currentorg->start)
697 dsp_currentorg->end = chptr;
704 if (objfname == NULL)
706 if (firstfname == NULL)
707 firstfname = defname;
709 strcpy(fnbuf, firstfname);
710 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
714 // With one pass finished, go back and:
715 // (1) run through all the fixups and resolve forward references;
716 // (1.5) ensure that remaining fixups can be handled by the linker
717 // (`lo68' format, extended (postfix) format....)
718 // (2) generate the output file image and symbol table;
719 // (3) generate relocation information from left-over fixups.
720 ResolveAllFixups(); // Do all fixups
721 StopMark(); // Stop mark tape-recorder
725 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
726 CantCreateFile(objfname);
730 s = (prg_flag ? "executable" : "object");
731 printf("[Writing %s file: %s]\n", s, objfname);
744 printf("[Wrapping-up listing file]\n");
754 DEBUG dump_everything();
761 // Determine processor endianess
763 int GetEndianess(void)
766 char * p = (char *)&i;
776 // Application entry point
778 int main(int argc, char ** argv)
780 perm_verb_flag = 0; // Clobber "permanent" verbose flag
781 legacy_flag = 1; // Default is legacy mode on (:-P)
783 // Set legacy optimisation flags to on and everything else to off
784 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
785 optim_flags[OPT_ABS_SHORT] =
786 optim_flags[OPT_MOVEL_MOVEQ] =
787 optim_flags[OPT_BSR_BCC_S] = 1;
789 cmdlnexec = argv[0]; // Obtain executable name
790 endian = GetEndianess(); // Get processor endianess
792 // If commands were passed in, process them
794 return Process(argc - 1, argv + 1);