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; // 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 of memory we allocate
49 // 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
69 // qst: Do a quicksort. First, find the median element, and put that one in the first place as
70 // the discriminator. (This "median" is just the median of the first, last and middle elements).
71 // (Using this median instead of the first element is a big win). Then, the usual
72 // partitioning/swapping, followed by moving the discriminator into the right place. Then,
73 // figure out the sizes of the two partions, do the smaller one recursively and the larger one
74 // via a repeat of this code. Stopping when there are less than THRESH elements in a partition
75 // and cleaning up with an insertion sort (in our caller) is a huge win. All data swaps are done
76 // in-line, which is space-losing but time-saving. (And there are only three places where
79 static int qst(char *base, char *max) {
86 * At the top here, lo is the number of characters of elements in the
87 * current partition. (Which should be max - base).
88 * Find the median of the first, last, and middle element and make
89 * that the middle element. Set j to largest of first and middle.
90 * If max is larger than that guy, then it's that guy, else compare
91 * max with loser of first and take larger. Things are set up to
92 * prefer the middle, then the first in case of ties.
94 lo = max - base; /* number of elements as chars */
96 mid = i = base + qsz * ((lo / qsz) >> 1);
98 j = (qcmp((jj = base), i) > 0 ? jj : i);
99 if (qcmp(j, (tmp = max - qsz)) > 0) {
100 /* switch to first loser */
101 j = (j == jj ? i : jj);
102 if (qcmp(j, tmp) < 0)
115 * Semi-standard quicksort partitioning/swapping
117 for (i = base, j = max - qsz; ; ) {
118 while (i < mid && qcmp(i, mid) <= 0)
121 if (qcmp(mid, j) <= 0) {
125 tmp = i + qsz; /* value of i after swap */
127 /* j <-> mid, new mid is j */
139 /* i <-> mid, new mid is i */
141 tmp = mid = i; /* value of i after swap */
154 * Look at sizes of the two partitions, do the smaller
155 * one first by recursion, then do the larger one by
156 * making sure lo is its size, base and max are update
157 * correctly, and branching back. But only repeat
158 * (recursively or by branching) if the partition is
159 * of at least size THRESH.
162 if ((lo = j - base) <= (hi = max - i)) {
172 } while (lo >= thresh);
179 * First, set up some global parameters for qst to share. Then, quicksort
180 * with qst(), and then a cleanup insertion sort ourselves. Sound simple?
184 int rmac_qsort(char *base, int n, int size, int (*compar)()) {
185 register char c, *i, *j, *lo, *hi;
192 thresh = qsz * THRESH;
193 mthresh = qsz * MTHRESH;
194 max = base + n * qsz;
202 * First put smallest element, which must be in the first THRESH, in
203 * the first position as a sentinel. This is done just by searching
204 * the first THRESH elements (or the first n if n < THRESH), finding
205 * the min, and swapping it into the first position.
207 for (j = lo = base; (lo += qsz) < hi; )
211 /* swap j into place */
212 for (i = base, hi = base + qsz; i < hi; ) {
219 * With our sentinel in place, we now run the following hyper-fast
220 * insertion sort. For each remaining element, min, from [1] to [n-1],
221 * set hi to the index of the element AFTER which this one goes.
222 * Then, do the standard insertion sort shift on a character at a time
223 * basis for each element in the frob.
225 for (min = base; (hi = min += qsz) < max; ) {
226 while (qcmp(hi -= qsz, min) > 0)
228 if ((hi += qsz) != min) {
229 for (lo = min + qsz; --lo >= min; ) {
231 for (i = j = lo; (j -= qsz) >= hi; i = j)
241 // --- Allocate memory; Panic and Quit if we Run Out -----------------------------------------------
244 char * amem(LONG amount)
248 if (amount & 1) // Keep word alignment
251 if (amount < A_THRESH)
252 { // Honor *small* request
253 if (a_amount < amount)
255 a_ptr = amem(A_AMOUNT);
265 amemtot += amount; // Bump total alloc
266 p = (char *)malloc(amount); // Get memory from malloc
268 if ((LONG)p == (LONG)NULL)
269 fatal("memory exhausted");
271 memset(p, 0, amount);
278 // --- Copy stuff around, return pointer to dest+count+1 (doesn't handle overlap) ------------------
281 char * copy(char * dest, char * src, LONG count)
290 // --- Clear a region of memory --------------------------------------------------------------------
293 void clear(char *dest, LONG count)
300 // --- Check to see if the string is a keyword. Returns -1, or a value from the 'accept[]' table ---
303 int kmatch(char * p, int * base, int * check, int * tab, int * accept)
308 for(state=0; state>=0;)
310 j = base[state] + (int)tolowertab[*p];
312 if (check[j] != state)
313 { // Reject, character doesn't match
314 state = -1; // No match
319 { // Must accept or reject at EOS
320 state = accept[j]; // (-1 on no terminal match)
331 // --- Auto-even a section -------------------------------------------------------------------------
334 void autoeven(int sect)
342 // -------------------------------------------------------------------------------------------------
343 // Manipulate file extension.
344 // `name' must be large enough to hold any possible filename.
345 // If `stripp' is nonzero, any old extension is removed.
346 // Then, if the file does not already have an extension,
347 // `extension' is appended to the filename.
348 // -------------------------------------------------------------------------------------------------
351 char *fext(char *name, char *extension, int stripp) {
352 char *s, *beg; // String pointers
354 // Find beginning of "real" name
355 beg = name + strlen(name) - 1;
356 for(; beg > name; --beg) {
357 if(*beg == SLASHCHAR) {
363 if(stripp) { // Clobber any old extension
364 for(s = beg; *s && *s != '.'; ++s)
369 for(s = beg; *s != '.'; ++s) {
370 if(!*s) { // Append the new extension
371 strcat(beg, extension);
380 // -------------------------------------------------------------------------------------------------
381 // Return `item'nth element of semicolon-seperated pathnames specified in the enviroment string `s'.
382 // Copy the pathname to `buf'. Return 0 if the `item'nth path doesn't exist.
384 // [`item' ranges from 0 to N-1, where N = #elements in search path]
385 // -------------------------------------------------------------------------------------------------
388 int nthpath(char * env_var, int itemno, char * buf)
390 char * s = searchpath;
399 while (*s != EOS && *s++ != ';')
405 while (*s != EOS && *s != ';')
414 // --- Display Command Line Help -----------------------------------------------
417 void display_help(void)
420 printf(" %s [options] srcfile\n", cmdlnexec);
422 printf("Options:\n");
423 printf(" -? or -h display usage information\n");
424 printf(" -dsymbol[=value] define symbol\n");
425 printf(" -e[errorfile] send error messages to file, not stdout\n");
426 printf(" -f[format] output object file format\n");
427 printf(" b: BSD (use this for Jaguar)\n");
428 printf(" -i[path] directory to search for include files\n");
429 printf(" -l[filename] create an output listing file\n");
430 printf(" -o file output file name\n");
431 printf(" -r[size] pad segments to boundary size specified\n");
432 printf(" w: word (2 bytes, default alignment)\n");
433 printf(" l: long (4 bytes)\n");
434 printf(" p: phrase (8 bytes)\n");
435 printf(" d: double phrase (16 bytes)\n");
436 printf(" q: quad phrase (32 bytes)\n");
437 printf(" -s warn about possible short branches\n");
438 printf(" -u force referenced and undefined symbols global\n");
439 printf(" -v set verbose mode\n");
440 printf(" -y[pagelen] set page line length (default: 61)\n");
445 // --- Display Version Information ---------------------------------------------
448 void display_version(void)
450 printf("\nReboot's Macro Assembler for Atari Jaguar\n");
451 printf("Copyright (C) 199x Landon Dyer, 2011 Reboot\n");
452 printf("V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
456 // --- Process Command Line Arguments and do an Assembly -------------------------------------------
459 int process(int argc, char **argv) {
460 int argno; // Argument number
461 SYM *sy; // Pointer to a symbol record
462 char *s; // String pointer
463 int fd; // File descriptor
464 char fnbuf[FNSIZ]; // Filename buffer
467 errcnt = 0; // Initialise error count
468 listing = 0; // Initialise listing level
469 list_flag = 0; // Initialise listing flag
470 verb_flag = perm_verb_flag; // Initialise verbose flag
471 as68_flag = 0; // Initialise as68 kludge mode
472 glob_flag = 0; // Initialise .globl flag
473 sbra_flag = 0; // Initialise short branch flag
474 debug = 0; // Initialise debug flag
475 searchpath = NULL; // Initialise search path
476 objfname = NULL; // Initialise object filename
477 list_fname = NULL; // Initialise listing filename
478 err_fname = NULL; // Initialise error filename
479 obj_format = BSD; // Initialise object format
480 firstfname = NULL; // Initialise first filename
481 err_fd = ERROUT; // Initialise error file descriptor
482 err_flag = 0; // Initialise error flag
483 rgpu = 0; // Initialise GPU assembly flag
484 rdsp = 0; // Initialise DSP assembly flag
485 lsym_flag = 1; // Include local symbols in object file
486 regbank = BANK_N; // No RISC register bank specified
487 orgactive = 0; // Not in RISC org section
488 orgwarning = 0; // No ORG warning issued
490 segpadsize = 2; // Initialise segment padding size
493 // Initialise modules
494 init_sym(); // Symbol table
495 init_token(); // Tokenizer
496 init_procln(); // Line processor
497 init_expr(); // Expression analyzer
498 init_sect(); // Section manager / code generator
499 init_mark(); // Mark tape-recorder
500 init_macro(); // Macro processor
501 init_list(); // Listing generator
503 // Process command line arguments and assemble source files
504 for(argno = 0; argno < argc; ++argno) {
505 if(*argv[argno] == '-') {
506 switch(argv[argno][1]) {
507 case 'd': // Define symbol
509 for(s = argv[argno] + 2; *s != EOS;) {
515 if(argv[argno][2] == EOS) {
516 printf("-d: empty symbol\n");
520 sy = lookup(argv[argno] + 2, 0, 0);
522 sy = newsym(argv[argno] + 2, LABEL, 0);
525 sy->sattr = DEFINED | EQUATED | ABS;
527 sy->svalue = (VALUE)atoi(s);
531 case 'e': // Redirect error message output
533 err_fname = argv[argno] + 2;
535 case 'f': // -f<format>
537 switch(argv[argno][2]) {
539 case 'b': // -fb = BSD (Jaguar Recommended)
544 printf("-f: unknown object format specified\n");
549 case 'g': // Debugging flag
551 printf("Debugging flag (-g) not yet implemented\n");
553 case 'i': // Set directory search path
555 searchpath = argv[argno] + 2;
557 case 'l': // Produce listing file
559 list_fname = argv[argno] + 2;
564 case 'o': // Direct object file output
566 if(argv[argno][2] != EOS) objfname = argv[argno] + 2;
568 if(++argno >= argc) {
569 printf("Missing argument to -o");
573 objfname = argv[argno];
576 case 'r': // Pad seg to requested boundary size
578 switch(argv[argno][2]) {
579 case 'w': case 'W': segpadsize = 2; break;
580 case 'l': case 'L': segpadsize = 4; break;
581 case 'p': case 'P': segpadsize = 8; break;
582 case 'd': case 'D': segpadsize = 16; break;
583 case 'q': case 'Q': segpadsize = 32; break;
584 default: segpadsize = 2; break; // Effective autoeven();
587 case 's': // Warn about possible short branches
591 case 'u': // Make undefined symbols .globl
595 case 'v': // Verbose flag
598 if(verb_flag > 1) display_version();
600 case 'x': // Turn on debugging
603 printf("~ Debugging ON\n");
605 case 'y': // -y<pagelen>
607 pagelen = atoi(argv[argno] + 2);
609 printf("-y: bad page length\n");
614 case EOS: // Input is stdin
615 if(firstfname == NULL) // Kludge first filename
616 firstfname = defname;
617 include(0, "(stdin)");
620 case 'h': // Display command line usage
629 printf("Unknown switch: %s\n\n", argv[argno]);
635 // Record first filename.
636 if(firstfname == NULL)
637 firstfname = argv[argno];
638 strcpy(fnbuf, argv[argno]);
639 fext(fnbuf, ".s", 0);
642 printf("Cannot open: %s\n", fnbuf);
651 // Wind-up processing;
652 // o save current section (no more code generation)
653 // o do auto-even of all sections (or boundary alignment as requested through '-r')
654 // o determine name of object file:
655 // - "foo.o" for linkable output;
656 // - "foo.prg" for GEMDOS executable (-p flag).
658 for(i = TEXT; i <= BSS; i <<= 1) {
661 case 2: d_even(); break;
662 case 4: d_long(); break;
663 case 8: d_phrase(); break;
664 case 16: d_dphrase(); break;
665 case 32: d_qphrase(); break;
670 if(objfname == NULL) {
671 if(firstfname == NULL)
672 firstfname = defname;
673 strcpy(fnbuf, firstfname);
674 //fext(fnbuf, prg_flag ? ".prg" : ".o", 1);
675 fext(fnbuf, ".o", 1);
679 // With one pass finished, go back and:
680 // (1) run through all the fixups and resolve forward references;
681 // (1.5) ensure that remaining fixups can be handled by the linker
682 // (`lo68' format, extended (postfix) format....)
683 // (2) generate the output file image and symbol table;
684 // (3) generate relocation information from left-over fixups.
685 fixups(); // Do all fixups
686 stopmark(); // Stop mark tape-recorder
688 if((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
692 printf("[Writing %s file: %s]\n", s, objfname);
701 if(verb_flag) printf("[Wrapping-up listing file]\n");
710 DEBUG dump_everything();
716 // --- Interactive Mode ----------------------------------------------------------------------------
719 void interactive(void)
721 char * s; // String pointer for banner
722 char ln[LNSIZ]; // Input line
723 char * argv[MAXARGV]; // Argument values
724 int argcnt; // Argument count
726 // As there is no command line, print a copyright message and prompt for command line
727 s = "*****************************************************\n";
728 printf("\n%s* RMAC - Reboot's Macro Assembler for Atari Jaguar *\n", s);
729 printf("* Copyright (C) 199x Landon Dyer, 2011 Reboot *\n");
730 printf("* Version %01i.%01i.%01i Platform: %-9s *\n",MAJOR,MINOR,PATCH,PLATFORM);
731 printf("* ------------------------------------------------- *\n");
732 printf("* INTERACTIVE MODE (press ENTER by itself to quit) *\n%s\n", s);
734 perm_verb_flag = 1; // Enter permanent verbose mode
736 // Handle commandlines until EOF or we get an empty one
741 fflush(stdout); // Make prompt visible
743 // if (gets(ln) == NULL || !*ln) // Get input line
744 if (fgets(ln, LNSIZ, stdin) == NULL || !*ln) // Get input line
747 argcnt = 0; // Process input line
758 if (argcnt >= MAXARGV)
760 printf("Too many arguments\n");
766 while (*s && !isspace(*s))
774 if (argcnt == 0) // Exit if no arguments
777 process(argcnt, argv); // Process arguments
780 printf("%d assembly error%s\n", errcnt, (errcnt > 1) ? "s" : "");
785 // --- Determine Processor Endianess ---------------------------------------------------------------
788 int get_endianess(void)
791 char * p = (char *)&i;
800 // --- Application Entry Point; Handle the Command Line --------------------------------------------
803 int main(int argc, char ** argv)
805 int status; // Status flag
808 perm_verb_flag = 0; // Clobber "permanent" verbose flag
809 cmdlnexec = argv[0]; // Obtain executable name
811 endian = get_endianess(); // Get processor endianess
813 for(i=0; i<MAXFWDJUMPS; i++)
817 { // Full command line passed
818 status = process(argc - 1, argv + 1);
821 { // Interactive mode