2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011 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
25 int perm_verb_flag; // Permanently verbose, interactive mode
26 int list_flag; // "-l" Listing flag on command line
27 int verb_flag; // Be verbose about what's going on
28 int as68_flag; // as68 kludge mode
29 int glob_flag; // Assume undefined symbols are global
30 int lsym_flag; // Include local symbols in object file
31 int sbra_flag; // Warn about possible short branches
32 int obj_format; // Object format flag
33 int debug; // [1..9] Enable debugging levels
34 int err_flag; // '-e' specified
35 int err_fd; // File to write error messages to
36 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
37 int list_fd; // File to write listing to
38 int regbank; // RISC register bank
39 int segpadsize; // Segment padding size
40 int in_main; // In main memory flag for GPUMAIN
41 int endian; // Host processor endianess
42 char * objfname; // Object filename pointer
43 char * firstfname; // First source filename
44 char * cmdlnexec; // Executable name, pointer to ARGV[0]
45 char * searchpath; // Search path for include files
46 char defname[] = "noname.o"; // Default output filename
48 // Under Windows and UNIX malloc() is an expensive call, so for small amounts
49 // of memory we allocate from a previously allocated buffer.
51 #define A_AMOUNT 4096 // Amount to malloc() at a time
52 #define A_THRESH 64 // Use malloc() for amounts >= A_THRESH
54 static LONG a_amount; // Amount left at a_ptr
55 static char * a_ptr; // Next free chunk
56 LONG amemtot; // amem() total of requests
58 // Qsort; The THRESHold below is the insertion sort threshold, and has been adjusted
59 // for records of size 48 bytes.The MTHREShold is where we stop finding a better median.
61 #define THRESH 4 // Threshold for insertion
62 #define MTHRESH 6 // Threshold for median
64 static int (*qcmp)(); // The comparison routine
65 static int qsz; // Size of each record
66 static int thresh; // THRESHold in chars
67 static int mthresh; // MTHRESHold in chars
71 // qst: Do a quicksort. First, find the median element, and put that one in the first place as
72 // the discriminator. (This "median" is just the median of the first, last and middle elements).
73 // (Using this median instead of the first element is a big win). Then, the usual
74 // partitioning/swapping, followed by moving the discriminator into the right place. Then,
75 // figure out the sizes of the two partions, do the smaller one recursively and the larger one
76 // via a repeat of this code. Stopping when there are less than THRESH elements in a partition
77 // and cleaning up with an insertion sort (in our caller) is a huge win. All data swaps are done
78 // in-line, which is space-losing but time-saving. (And there are only three places where
81 static int qst(char * base, char * max)
83 char c, * i, * j, * jj;
89 * At the top here, lo is the number of characters of elements in the
90 * current partition. (Which should be max - base).
91 * Find the median of the first, last, and middle element and make
92 * that the middle element. Set j to largest of first and middle.
93 * If max is larger than that guy, then it's that guy, else compare
94 * max with loser of first and take larger. Things are set up to
95 * prefer the middle, then the first in case of ties.
97 lo = max - base; /* number of elements as chars */
99 mid = i = base + qsz * ((lo / qsz) >> 1);
101 j = (qcmp((jj = base), i) > 0 ? jj : i);
102 if (qcmp(j, (tmp = max - qsz)) > 0) {
103 /* switch to first loser */
104 j = (j == jj ? i : jj);
105 if (qcmp(j, tmp) < 0)
118 * Semi-standard quicksort partitioning/swapping
120 for (i = base, j = max - qsz; ; ) {
121 while (i < mid && qcmp(i, mid) <= 0)
124 if (qcmp(mid, j) <= 0) {
128 tmp = i + qsz; /* value of i after swap */
130 /* j <-> mid, new mid is j */
142 /* i <-> mid, new mid is i */
144 tmp = mid = i; /* value of i after swap */
157 * Look at sizes of the two partitions, do the smaller
158 * one first by recursion, then do the larger one by
159 * making sure lo is its size, base and max are update
160 * correctly, and branching back. But only repeat
161 * (recursively or by branching) if the partition is
162 * of at least size THRESH.
165 if ((lo = j - base) <= (hi = max - i)) {
175 } while (lo >= thresh);
183 * First, set up some global parameters for qst to share. Then, quicksort
184 * with qst(), and then a cleanup insertion sort ourselves. Sound simple?
187 int rmac_qsort(char * base, int n, int size, int (*compar)())
189 register char c, * i, * j, * lo, * hi;
197 thresh = qsz * THRESH;
198 mthresh = qsz * MTHRESH;
199 max = base + n * qsz;
212 * First put smallest element, which must be in the first THRESH, in
213 * the first position as a sentinel. This is done just by searching
214 * the first THRESH elements (or the first n if n < THRESH), finding
215 * the min, and swapping it into the first position.
217 for (j = lo = base; (lo += qsz) < hi; )
223 /* swap j into place */
224 for (i = base, hi = base + qsz; i < hi; )
232 * With our sentinel in place, we now run the following hyper-fast
233 * insertion sort. For each remaining element, min, from [1] to [n-1],
234 * set hi to the index of the element AFTER which this one goes.
235 * Then, do the standard insertion sort shift on a character at a time
236 * basis for each element in the frob.
238 for (min = base; (hi = min += qsz) < max; ) {
239 while (qcmp(hi -= qsz, min) > 0)
241 if ((hi += qsz) != min) {
242 for (lo = min + qsz; --lo >= min; ) {
244 for (i = j = lo; (j -= qsz) >= hi; i = j)
255 // Allocate memory; Panic and Quit if we Run Out
257 char * amem(LONG amount)
261 if (amount & 1) // Keep word alignment
264 if (amount < A_THRESH)
265 { // Honor *small* request
266 if (a_amount < amount)
268 a_ptr = amem(A_AMOUNT);
278 amemtot += amount; // Bump total alloc
279 p = (char *)malloc(amount); // Get memory from malloc
281 if ((LONG)p == (LONG)NULL)
282 fatal("memory exhausted");
284 memset(p, 0, amount);
292 // Copy stuff around, return pointer to dest+count+1 (doesn't handle overlap)
294 char * copy(char * dest, char * src, LONG count)
304 // Clear a region of memory
306 void clear(char * dest, LONG count)
314 // Check to see if the string is a keyword. Returns -1, or a value from the
317 int kmatch(char * p, int * base, int * check, int * tab, int * accept)
322 for(state=0; state>=0;)
324 j = base[state] + (int)tolowertab[*p];
326 if (check[j] != state)
327 { // Reject, character doesn't match
328 state = -1; // No match
333 { // Must accept or reject at EOS
334 state = accept[j]; // (-1 on no terminal match)
346 // Auto-even a section
348 void autoeven(int sect)
357 // Manipulate file extension.
358 // `name' must be large enough to hold any possible filename.
359 // If `stripp' is nonzero, any old extension is removed.
360 // Then, if the file does not already have an extension,
361 // `extension' is appended to the filename.
363 char * fext(char * name, char * extension, int stripp)
365 char * s, * beg; // String pointers
367 // Find beginning of "real" name
368 beg = name + strlen(name) - 1;
370 for(; beg>name; --beg)
372 if (*beg == SLASHCHAR)
380 { // Clobber any old extension
381 for(s=beg; *s && *s!='.'; ++s)
387 for(s=beg; *s!='.'; ++s)
390 { // Append the new extension
391 strcat(beg, extension);
401 // Return `item'nth element of semicolon-seperated pathnames specified in the
402 // enviroment string `s'. Copy the pathname to `buf'. Return 0 if the `item'
403 // nth path doesn't exist.
405 // [`item' ranges from 0 to N-1, where N = #elements in search path]
407 int nthpath(char * env_var, int itemno, char * buf)
409 char * s = searchpath;
418 while (*s != EOS && *s++ != ';')
424 while (*s != EOS && *s != ';')
434 // Display Command Line Help
436 void display_help(void)
439 printf(" %s [options] srcfile\n", cmdlnexec);
441 printf("Options:\n");
442 printf(" -? or -h display usage information\n");
443 printf(" -dsymbol[=value] define symbol\n");
444 printf(" -e[errorfile] send error messages to file, not stdout\n");
445 printf(" -f[format] output object file format\n");
446 printf(" b: BSD (use this for Jaguar)\n");
447 printf(" -i[path] directory to search for include files\n");
448 printf(" -l[filename] create an output listing file\n");
449 printf(" -o file output file name\n");
450 printf(" -r[size] pad segments to boundary size specified\n");
451 printf(" w: word (2 bytes, default alignment)\n");
452 printf(" l: long (4 bytes)\n");
453 printf(" p: phrase (8 bytes)\n");
454 printf(" d: double phrase (16 bytes)\n");
455 printf(" q: quad phrase (32 bytes)\n");
456 printf(" -s warn about possible short branches\n");
457 printf(" -u force referenced and undefined symbols global\n");
458 printf(" -v set verbose mode\n");
459 printf(" -y[pagelen] set page line length (default: 61)\n");
465 // Display Version Information
467 void display_version(void)
469 printf("\nReboot's Macro Assembler for Atari Jaguar\n");
470 printf("Copyright (C) 199x Landon Dyer, 2011 Reboot\n");
471 printf("V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
476 // Process Command Line Arguments and do an Assembly
478 int process(int argc, char ** argv)
480 int argno; // Argument number
481 SYM * sy; // Pointer to a symbol record
482 char * s; // String pointer
483 int fd; // File descriptor
484 char fnbuf[FNSIZ]; // Filename buffer
487 errcnt = 0; // Initialise error count
488 listing = 0; // Initialise listing level
489 list_flag = 0; // Initialise listing flag
490 verb_flag = perm_verb_flag; // Initialise verbose flag
491 as68_flag = 0; // Initialise as68 kludge mode
492 glob_flag = 0; // Initialise .globl flag
493 sbra_flag = 0; // Initialise short branch flag
494 debug = 0; // Initialise debug flag
495 searchpath = NULL; // Initialise search path
496 objfname = NULL; // Initialise object filename
497 list_fname = NULL; // Initialise listing filename
498 err_fname = NULL; // Initialise error filename
499 obj_format = BSD; // Initialise object format
500 firstfname = NULL; // Initialise first filename
501 err_fd = ERROUT; // Initialise error file descriptor
502 err_flag = 0; // Initialise error flag
503 rgpu = 0; // Initialise GPU assembly flag
504 rdsp = 0; // Initialise DSP assembly flag
505 lsym_flag = 1; // Include local symbols in object file
506 regbank = BANK_N; // No RISC register bank specified
507 orgactive = 0; // Not in RISC org section
508 orgwarning = 0; // No ORG warning issued
510 segpadsize = 2; // Initialise segment padding size
513 // Initialise modules
514 init_sym(); // Symbol table
515 init_token(); // Tokenizer
516 init_procln(); // Line processor
517 init_expr(); // Expression analyzer
518 init_sect(); // Section manager / code generator
519 init_mark(); // Mark tape-recorder
520 init_macro(); // Macro processor
521 init_list(); // Listing generator
523 // Process command line arguments and assemble source files
524 for(argno = 0; argno < argc; ++argno) {
525 if (*argv[argno] == '-') {
526 switch(argv[argno][1]) {
527 case 'd': // Define symbol
529 for(s = argv[argno] + 2; *s != EOS;) {
535 if (argv[argno][2] == EOS) {
536 printf("-d: empty symbol\n");
540 sy = lookup(argv[argno] + 2, 0, 0);
542 sy = newsym(argv[argno] + 2, LABEL, 0);
545 sy->sattr = DEFINED | EQUATED | ABS;
547 sy->svalue = (VALUE)atoi(s);
551 case 'e': // Redirect error message output
553 err_fname = argv[argno] + 2;
555 case 'f': // -f<format>
557 switch(argv[argno][2]) {
559 case 'b': // -fb = BSD (Jaguar Recommended)
564 printf("-f: unknown object format specified\n");
569 case 'g': // Debugging flag
571 printf("Debugging flag (-g) not yet implemented\n");
573 case 'i': // Set directory search path
575 searchpath = argv[argno] + 2;
577 case 'l': // Produce listing file
579 list_fname = argv[argno] + 2;
584 case 'o': // Direct object file output
586 if (argv[argno][2] != EOS) objfname = argv[argno] + 2;
588 if (++argno >= argc) {
589 printf("Missing argument to -o");
593 objfname = argv[argno];
596 case 'r': // Pad seg to requested boundary size
598 switch(argv[argno][2]) {
599 case 'w': case 'W': segpadsize = 2; break;
600 case 'l': case 'L': segpadsize = 4; break;
601 case 'p': case 'P': segpadsize = 8; break;
602 case 'd': case 'D': segpadsize = 16; break;
603 case 'q': case 'Q': segpadsize = 32; break;
604 default: segpadsize = 2; break; // Effective autoeven();
607 case 's': // Warn about possible short branches
611 case 'u': // Make undefined symbols .globl
615 case 'v': // Verbose flag
618 if (verb_flag > 1) display_version();
620 case 'x': // Turn on debugging
623 printf("~ Debugging ON\n");
625 case 'y': // -y<pagelen>
627 pagelen = atoi(argv[argno] + 2);
629 printf("-y: bad page length\n");
634 case EOS: // Input is stdin
635 if (firstfname == NULL) // Kludge first filename
636 firstfname = defname;
637 include(0, "(stdin)");
640 case 'h': // Display command line usage
649 printf("Unknown switch: %s\n\n", argv[argno]);
655 // Record first filename.
656 if (firstfname == NULL)
657 firstfname = argv[argno];
658 strcpy(fnbuf, argv[argno]);
659 fext(fnbuf, ".s", 0);
662 printf("Cannot open: %s\n", fnbuf);
671 // Wind-up processing;
672 // o save current section (no more code generation)
673 // o do auto-even of all sections (or boundary alignment as requested through '-r')
674 // o determine name of object file:
675 // - "foo.o" for linkable output;
676 // - "foo.prg" for GEMDOS executable (-p flag).
678 for(i = TEXT; i <= BSS; i <<= 1) {
681 case 2: d_even(); break;
682 case 4: d_long(); break;
683 case 8: d_phrase(); break;
684 case 16: d_dphrase(); break;
685 case 32: d_qphrase(); break;
690 if (objfname == NULL) {
691 if (firstfname == NULL)
692 firstfname = defname;
693 strcpy(fnbuf, firstfname);
694 //fext(fnbuf, prg_flag ? ".prg" : ".o", 1);
695 fext(fnbuf, ".o", 1);
699 // With one pass finished, go back and:
700 // (1) run through all the fixups and resolve forward references;
701 // (1.5) ensure that remaining fixups can be handled by the linker
702 // (`lo68' format, extended (postfix) format....)
703 // (2) generate the output file image and symbol table;
704 // (3) generate relocation information from left-over fixups.
705 fixups(); // Do all fixups
706 stopmark(); // Stop mark tape-recorder
708 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
712 printf("[Writing %s file: %s]\n", s, objfname);
721 if (verb_flag) printf("[Wrapping-up listing file]\n");
730 DEBUG dump_everything();
740 void interactive(void)
742 char * s; // String pointer for banner
743 char ln[LNSIZ]; // Input line
744 char * argv[MAXARGV]; // Argument values
745 int argcnt; // Argument count
747 // As there is no command line, print a copyright message and prompt for command line
748 s = "*****************************************************\n";
749 printf("\n%s* RMAC - Reboot's Macro Assembler for Atari Jaguar *\n", s);
750 printf("* Copyright (C) 199x Landon Dyer, 2011 Reboot *\n");
751 printf("* Version %01i.%01i.%01i Platform: %-9s *\n",MAJOR,MINOR,PATCH,PLATFORM);
752 printf("* ------------------------------------------------- *\n");
753 printf("* INTERACTIVE MODE (press ENTER by itself to quit) *\n%s\n", s);
755 perm_verb_flag = 1; // Enter permanent verbose mode
757 // Handle commandlines until EOF or we get an empty one
762 fflush(stdout); // Make prompt visible
764 // if (gets(ln) == NULL || !*ln) // Get input line
765 if (fgets(ln, LNSIZ, stdin) == NULL || !*ln) // Get input line
768 argcnt = 0; // Process input line
779 if (argcnt >= MAXARGV)
781 printf("Too many arguments\n");
787 while (*s && !isspace(*s))
795 if (argcnt == 0) // Exit if no arguments
798 process(argcnt, argv); // Process arguments
801 printf("%d assembly error%s\n", errcnt, (errcnt > 1) ? "s" : "");
808 // Determine Processor Endianess
810 int get_endianess(void)
813 char * p = (char *)&i;
823 // Application Entry Point; Handle the Command Line
825 int main(int argc, char ** argv)
827 int status; // Status flag
830 perm_verb_flag = 0; // Clobber "permanent" verbose flag
831 cmdlnexec = argv[0]; // Obtain executable name
833 endian = get_endianess(); // Get processor endianess
835 for(i=0; i<MAXFWDJUMPS; i++)
839 { // Full command line passed
840 status = process(argc - 1, argv + 1);
845 // Sorry Landon, this is the year 20xx and we haz plenty of resources now ;-)
848 // Instead, we show a nice banner and switches :-)