]> Shamusworld >> Repos - rmac/blob - rmac.c
Added "-4" switch that enables C style operator precedence (original work by Bastian...
[rmac] / rmac.c
1 //
2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2022 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
7 //
8
9 #include "rmac.h"
10 #include "6502.h"
11 #include "debug.h"
12 #include "direct.h"
13 #include "dsp56k.h"
14 #include "error.h"
15 #include "expr.h"
16 #include "listing.h"
17 #include "mark.h"
18 #include "macro.h"
19 #include "object.h"
20 #include "procln.h"
21 #include "riscasm.h"
22 #include "sect.h"
23 #include "symbol.h"
24 #include "token.h"
25 #include "version.h"
26
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 (ALWAYS true)
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
63 int correctMathRules;                   // 1, use C operator precedence in expressions
64
65 //
66 // Convert a string to uppercase
67 //
68 void strtoupper(char * s)
69 {
70         while (*s)
71                 *s++ &= 0xDF;
72 }
73
74 //
75 // Manipulate file extension.
76 //
77 // 'name' must be large enough to hold any possible filename. If 'stripp' is
78 // nonzero, any old extension is removed. If the file does not already have an
79 // extension, 'extension' is appended to the filename.
80 //
81 char * fext(char * name, char * extension, int stripp)
82 {
83         char * s;
84
85         // Find beginning of "real" name (strip off path)
86         char * beg = strrchr(name, SLASHCHAR);
87
88         if (beg == NULL)
89                 beg = name;
90
91         // Clobber any old extension, if requested
92         if (stripp)
93         {
94                 for(s=beg; *s && *s!='.'; ++s)
95                         ;
96
97                 *s = '\0';
98         }
99
100         if (strrchr(beg, '.') == NULL)
101                 strcat(beg, extension);
102
103         return name;
104 }
105
106 static int is_sep(char c)
107 {
108     const char *seps = PATH_SEPS;
109
110     for (seps = PATH_SEPS; *seps; seps++) {
111         if (*seps == c)
112             return 1;
113     }
114
115     return 0;
116 }
117
118 //
119 // Return 'item'nth element of semicolon-seperated pathnames specified in the
120 // enviroment string 's'. Copy the pathname to 'buf'.  Return 0 if the 'item'
121 // nth path doesn't exist.
122 //
123 // ['item' ranges from 0 to N-1, where N = #elements in search path]
124 //
125 int nthpath(char * env_var, int itemno, char * buf)
126 {
127         char * s = searchpath;
128
129         if (s == NULL)
130                 s = getenv(env_var);
131
132         if (s == NULL)
133                 return 0;
134
135         while (itemno--)
136                 while (*s != EOS && !is_sep(*s++))
137                         ;
138
139         if (*s == EOS)
140                 return 0;
141
142         while (*s != EOS && !is_sep(*s))
143                 *buf++ = *s++;
144
145         *buf++ = EOS;
146
147         return 1;
148 }
149
150 //
151 // Display command line help
152 //
153 void DisplayHelp(void)
154 {
155         printf("Usage:\n"
156                 "    %s [options] srcfile\n"
157                 "\n"
158                 "Options:\n"
159                 "  -? or -h          Display usage information\n"
160                 "  -dsymbol[=value]  Define symbol (with optional value, default=0)\n"
161                 "  -e[errorfile]     Send error messages to file, not stdout\n"
162                 "  -f[format]        Output object file format\n"
163                 "                    a: ALCYON\n"
164                 "                    b: BSD (use this for Jaguar)\n"
165                 "                    e: ELF\n"
166                 "                    p: P56 (use this for DSP56001 only)\n"
167                 "                    l: LOD (use this for DSP56001 only)\n"
168                 "                    x: com/exe/xex (Atari 800)\n"
169                 "                    r: absolute address\n"
170                 "  -i[path]          Directory to search for include files\n"
171                 "  -l[filename]      Create an output listing file\n"
172                 "  -l*[filename]     Create an output listing file without pagination\n"
173                 "  -m[cpu]           Select default CPU. Available options:\n"
174                 "                    68000, 68020, 68030, 68040, 68060, 6502, tom, jerry, 56001\n"
175                 "  -n                Don't do things behind your back in RISC assembler\n"
176                 "  -o file           Output file name\n"
177                 "  +o[value]         Turn a specific optimisation on\n"
178                 "                    Available optimisation switches:\n"
179                 "                    o0: Absolute long addresses to word\n"
180                 "                    o1: move.l #x,dn/an to moveq\n"
181                 "                    o2: Word branches to short\n"
182                 "                    o3: Outer displacement 0(an) to (an)\n"
183                 "                    o4: lea size(An),An to addq #size,An\n"
184                 "                    o5: 68020+ Absolute long base/outer disp. to word\n"
185                 "                    o6: Null branches to NOP\n"
186                 "                    o7: clr.l Dx to moveq #0,Dx\n"
187                 "                    o8: adda.w/l #x,Dy to addq.w/l #x,Dy\n"
188                 "                    o9: adda.w/l #x,Ay to lea x(Dy),Ay\n"
189                 "                    o10: 56001 Use short format for immediate values if possible\n"
190                 "                    o11: 56001 Auto convert short addressing mode to long (default: on)\n"
191                 "                    o30: Enforce PC relative (alternative name: op)\n"
192                 "  ~o[value]         Turn a specific optimisation off\n"
193                 "  +oall             Turn all optimisations on\n"
194                 "  ~oall             Turn all optimisations off\n"
195                 "  -p                Create an ST .prg (without symbols). Forces -fa\n"
196                 "  -ps               Create an ST .prg (with symbols). Forces -fa\n"
197                 "  -px               Create an ST .prg (with exsymbols). Forces -fa\n"
198                 "  -r[size]          Pad segments to boundary size specified\n"
199                 "                    w: word (2 bytes, default alignment)\n"
200                 "                    l: long (4 bytes)\n"
201                 "                    p: phrase (8 bytes)\n"
202                 "                    d: double phrase (16 bytes)\n"
203                 "                    q: quad phrase (32 bytes)\n"
204                 "  -s                Warn about possible short branches\n"
205                 "                    and applied optimisations\n"
206                 "  -u                Force referenced and undefined symbols global\n"
207                 "  -v                Set verbose mode\n"
208                 "  -x                Turn on debugging mode\n"
209                 "  -y[pagelen]       Set page line length (default: 61)\n"
210                 "  -4                Use C style operator precedence\n"
211                 "\n", cmdlnexec);
212 }
213
214
215 //
216 // Display version information
217 //
218 void DisplayVersion(void)
219 {
220         printf("\n"
221                 " _ __ _ __ ___   __ _  ___ \n"
222                 "| '__| '_ ` _ \\ / _` |/ __|\n"
223                 "| |  | | | | | | (_| | (__ \n"
224                 "|_|  |_| |_| |_|\\__,_|\\___|\n"
225                 "\nRenamed Macro Assembler\n"
226                 "Copyright (C) 199x Landon Dyer, 2011-2022 Reboot and Friends\n"
227                 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
228 }
229
230 //
231 // Parse optimisation options
232 //
233 int ParseOptimization(char * optstring)
234 {
235         int onoff = 0;
236
237         while (*optstring)
238         {
239                 if (*optstring == '+')
240                         onoff = 1;
241                 else if (*optstring != '~')
242                         return ERROR;
243
244         if (optstring[2] == 0)
245             return error(".opt called with zero arguments");
246
247                 if ((optstring[2] == 'a' || optstring[2] == 'A')
248                         && (optstring[3] == 'l' || optstring[3] == 'L')
249                         && (optstring[4] == 'l' || optstring[4] == 'L'))
250                 {
251                         memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
252                         optstring += 5;
253                 }
254                 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
255                 {
256                         if (optstring[2] == 'p' || optstring[2] == 'P')
257                         {
258                                 optim_flags[OPT_PC_RELATIVE] = onoff;
259                                 optstring += 3;
260                         }
261                         else
262                         {
263                                 int opt_no = atoi(&optstring[2]);
264
265                                 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
266                                 {
267                                         optim_flags[opt_no] = onoff;
268                                         optstring += 3;
269
270                                         // If opt_no is 2 digits then consume an extra character.
271                                         // Sounds a bit sleazy but we know we're not going to hit
272                                         // more than 100 optimisation switches so this should be
273                                         // fine. If we do hit that number then it'll probably be
274                                         // time to burn the whole codebase and start from scratch.
275                                         if (opt_no > 9)
276                                                 optstring++;
277                                 }
278                                 else
279                                         return ERROR;
280                         }
281                 }
282                 else
283                 {
284                         return ERROR;
285                 }
286
287                 if (*optstring == ',')
288                         optstring++;
289         }
290         return OK;
291 }
292
293 extern int reg68base[53];
294 extern int reg68tab[222];
295 extern int reg68check[222];
296 extern int reg68accept[222];
297
298 //
299 // Process command line arguments and do an assembly
300 //
301 int Process(int argc, char ** argv)
302 {
303         int argno;                                              // Argument number
304         SYM * sy;                                               // Pointer to a symbol record
305         char * s;                                               // String pointer
306         int fd;                                                 // File descriptor
307         char fnbuf[FNSIZ];                              // Filename buffer
308         int i;                                                  // Iterator
309         int current_path_index = 0;             // Iterator for search paths
310
311         errcnt = 0;                                             // Initialize error count
312         listing = 0;                                    // Initialize listing level
313         list_flag = 0;                                  // Initialize listing flag
314         verb_flag = perm_verb_flag;             // Initialize verbose flag
315         glob_flag = 0;                                  // Initialize .globl flag
316         optim_warn_flag = 0;                    // Initialize short branch flag
317         debug = 0;                                              // Initialize debug flag
318         objfname = NULL;                                // Initialize object filename
319         list_fname = NULL;                              // Initialize listing filename
320         err_fname = NULL;                               // Initialize error filename
321         obj_format = BSD;                               // Initialize object format
322         firstfname = NULL;                              // Initialize first filename
323         err_fd = ERROUT;                                // Initialize error file descriptor
324         err_flag = 0;                                   // Initialize error flag
325         rgpu = 0;                                               // Initialize GPU assembly flag
326         rdsp = 0;                                               // Initialize DSP assembly flag
327         robjproc = 0;                                   // Initialize OP assembly flag
328         lsym_flag = 1;                                  // Include local symbols in object file
329         orgactive = 0;                                  // Not in RISC org section
330         orgwarning = 0;                                 // No ORG warning issued
331         segpadsize = 2;                                 // Initialize segment padding size
332     dsp_orgmap[0].start = 0;            // Initialize 56001 org initial address
333     dsp_orgmap[0].memtype = ORG_P;      // Initialize 56001 org start segment
334         m6502 = 0;                                              // 6502 mode off by default
335         regbase = reg68base;                    // Initialise DFA register tables
336         regtab = reg68tab;                              // Idem
337         regcheck = reg68check;                  // Idem
338         regaccept = reg68accept;                // Idem
339     correctMathRules = 0;                       // respect operator precedence
340         // Initialize modules
341         InitSymbolTable();                              // Symbol table
342         InitTokenizer();                                // Tokenizer
343         InitLineProcessor();                    // Line processor
344         InitExpression();                               // Expression analyzer
345         InitSection();                                  // Section manager / code generator
346         InitMark();                                             // Mark tape-recorder
347         InitMacro();                                    // Macro processor
348         InitListing();                                  // Listing generator
349         Init6502();                                             // 6502 assembler
350
351         // Process command line arguments and assemble source files
352         for(argno=0; argno<argc; argno++)
353         {
354                 if (*argv[argno] == '-')
355                 {
356                         switch (argv[argno][1])
357                         {
358                         case '4':
359                                 correctMathRules = 1;
360                                 break;
361                         case 'd':                               // Define symbol
362                         case 'D':
363                                 for(s=argv[argno]+2; *s!=EOS;)
364                                 {
365                                         if (*s++ == '=')
366                                         {
367                                                 s[-1] = EOS;
368                                                 break;
369                                         }
370                                 }
371
372                                 if (argv[argno][2] == EOS)
373                                 {
374                                         printf("-d: empty symbol\n");
375                                         errcnt++;
376                                         return errcnt;
377                                 }
378
379                                 sy = lookup(argv[argno] + 2, 0, 0);
380
381                                 if (sy == NULL)
382                                 {
383                                         sy = NewSymbol(argv[argno] + 2, LABEL, 0);
384                                         sy->svalue = 0;
385                                 }
386
387                                 sy->sattr = DEFINED | EQUATED | ABS;
388                                 sy->svalue = (*s ? (uint64_t)atoi(s) : 0);
389                                 break;
390                         case 'e':                               // Redirect error message output
391                         case 'E':
392                                 err_fname = argv[argno] + 2;
393                                 break;
394                         case 'f':                               // -f<format>
395                         case 'F':
396                                 switch (argv[argno][2])
397                                 {
398                                 case EOS:
399                                 case 'a':                       // -fa = Alcyon [the default]
400                                 case 'A':
401                                         obj_format = ALCYON;
402                                         break;
403                                 case 'b':                       // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
404                                 case 'B':
405                                         obj_format = BSD;
406                                         break;
407                                 case 'e':                       // -fe = ELF
408                                 case 'E':
409                                         obj_format = ELF;
410                                         break;
411                 case 'l':           // -fl = LOD
412                 case 'L':
413                     obj_format = LOD;
414                     break;
415                 case 'p':           // -fp = P56
416                 case 'P':
417                     obj_format = P56;
418                                         break;
419                                 case 'x':                       // -fx = COM/EXE/XEX
420                                 case 'X':
421                                         obj_format = XEX;
422                                         break;
423                 case 'r':           // -fr = Absolute address
424                 case 'R':
425                     obj_format = RAW;
426                     break;
427                                 default:
428                                         printf("-f: unknown object format specified\n");
429                                         errcnt++;
430                                         return errcnt;
431                                 }
432                                 break;
433                         case 'g':                               // Debugging flag
434                         case 'G':
435                                 printf("Debugging flag (-g) not yet implemented\n");
436                                 break;
437                         case 'i':                               // Set directory search path
438                         case 'I':
439                         {
440                                 strcat(searchpatha, argv[argno] + 2);
441                                 strcat(searchpatha, ";");
442                                 searchpath = searchpatha;
443
444                                 // Check to see if include paths actually exist
445                                 char current_path[256];
446
447                                 for(i=current_path_index; nthpath("RMACPATH", i, current_path)!=0; i++)
448                                 {
449                                         if (strlen(current_path) > 0)
450                                         {
451                                                 DIR * test = opendir(current_path);
452
453                                                 if (test == NULL)
454                                                 {
455                                                         printf("Invalid include path: %s\n", current_path);
456                                                         errcnt++;
457                                                         return errcnt;
458                                                 }
459
460                                                 closedir(test);
461                                         }
462                                 }
463
464                                 current_path_index = i - 1;
465                                 break;
466                         }
467                         case 'l':                               // Produce listing file
468                         case 'L':
469                                 if (*(argv[argno] + 2) == '*')
470                                 {
471                                         list_fname = argv[argno] + 3;
472                                         list_pag = 0;    // Special case - turn off pagination
473                                 }
474                                 else
475                                 {
476                                         list_fname = argv[argno] + 2;
477                                 }
478
479                                 listing = 1;
480                                 list_flag = 1;
481                                 lnsave++;
482                                 break;
483                         case 'm':
484                         case 'M':
485                                 if (strcmp(argv[argno] + 2, "68000") == 0)
486                                         d_68000();
487                                 else if (strcmp(argv[argno] + 2, "68020") == 0)
488                                         d_68020();
489                                 else if (strcmp(argv[argno] + 2, "68030") == 0)
490                                         d_68030();
491                                 else if (strcmp(argv[argno] + 2, "68040") == 0)
492                                         d_68040();
493                                 else if (strcmp(argv[argno] + 2, "68060") == 0)
494                                         d_68060();
495                                 else if (strcmp(argv[argno] + 2, "68881") == 0)
496                                         d_68881();
497                                 else if (strcmp(argv[argno] + 2, "68882") == 0)
498                                         d_68882();
499                                 else if (strcmp(argv[argno] + 2, "56001") == 0)
500                                         d_56001();
501                                 else if (strcmp(argv[argno] + 2, "6502") == 0)
502                                         d_6502();
503                                 else if (strcmp(argv[argno] + 2, "tom") == 0)
504                                         d_gpu();
505                                 else if (strcmp(argv[argno] + 2, "jerry") == 0)
506                                         d_dsp();
507                                 else
508                                 {
509                                         printf("Unrecognized CPU '%s'\n", argv[argno] + 2);
510                                         errcnt++;
511                                         return errcnt;
512                                 }
513                                 break;
514                         case 'o':                               // Direct object file output
515                         case 'O':
516                                 if (argv[argno][2] != EOS)
517                                         objfname = argv[argno] + 2;
518                                 else
519                                 {
520                                         if (++argno >= argc)
521                                         {
522                                                 printf("Missing argument to -o");
523                                                 errcnt++;
524                                                 return errcnt;
525                                         }
526
527                                         objfname = argv[argno];
528                                 }
529
530                                 break;
531                         case 'p':               /* -p: generate ".PRG" executable output */
532                         case 'P':
533                                 /*
534                                  * -p           .PRG generation w/o symbols
535                                  * -ps          .PRG generation with symbols
536                                  * -px          .PRG generation with extended symbols
537                                  */
538                                 switch (argv[argno][2])
539                                 {
540                                         case EOS:
541                                                 prg_flag = 1;
542                                                 break;
543
544                                         case 's':
545                                         case 'S':
546                                                 prg_flag = 2;
547                                                 break;
548
549                                         case 'x':
550                                         case 'X':
551                                                 prg_flag = 3;
552                                                 break;
553
554                                         default:
555                                                 printf("-p: syntax error\n");
556                                                 errcnt++;
557                                                 return errcnt;
558                                 }
559
560                                 // Enforce Alcyon object format - kind of silly
561                                 // to ask for .prg output without it!
562                                 obj_format = ALCYON;
563                                 break;
564                         case 'r':                               // Pad seg to requested boundary size
565                         case 'R':
566                                 switch(argv[argno][2])
567                                 {
568                                 case 'w': case 'W': segpadsize = 2;  break;
569                                 case 'l': case 'L': segpadsize = 4;  break;
570                                 case 'p': case 'P': segpadsize = 8;  break;
571                                 case 'd': case 'D': segpadsize = 16; break;
572                                 case 'q': case 'Q': segpadsize = 32; break;
573                                 default: segpadsize = 2; break; // Effective autoeven();
574                                 }
575                                 break;
576                         case 's':                               // Warn about possible short branches and applied optimisations
577                         case 'S':
578                                 optim_warn_flag = 1;
579                                 break;
580                         case 'u':                               // Make undefined symbols .globl
581                         case 'U':
582                                 glob_flag = 1;
583                                 break;
584                         case 'v':                               // Verbose flag
585                         case 'V':
586                                 verb_flag++;
587
588                                 if (verb_flag > 1)
589                                         DisplayVersion();
590
591                                 break;
592                         case 'x':                               // Turn on debugging
593                         case 'X':
594                                 debug = 1;
595                                 printf("~ Debugging ON\n");
596                                 break;
597                         case 'y':                               // -y<pagelen>
598                         case 'Y':
599                                 pagelen = atoi(argv[argno] + 2);
600
601                                 if (pagelen < 10)
602                                 {
603                                         printf("-y: bad page length\n");
604                                         errcnt++;
605                                         return errcnt;
606                                 }
607
608                                 break;
609                         case EOS:                               // Input is stdin
610                                 if (firstfname == NULL) // Kludge first filename
611                                         firstfname = defname;
612
613                                 include(0, "(stdin)");
614                                 Assemble();
615                                 break;
616                         case 'h':                               // Display command line usage
617                         case 'H':
618                         case '?':
619                                 DisplayVersion();
620                                 DisplayHelp();
621                                 errcnt++;
622                                 break;
623                         case 'n':                               // Turn off legacy mode
624                         case 'N':
625                                 legacy_flag = 0;
626                                 printf("Legacy mode OFF\n");
627                                 break;
628                         default:
629                                 DisplayVersion();
630                                 printf("Unknown switch: %s\n\n", argv[argno]);
631                                 DisplayHelp();
632                                 errcnt++;
633                                 break;
634                         }
635                 }
636                 else if (*argv[argno] == '+' || *argv[argno] == '~')
637                 {
638                         if (ParseOptimization(argv[argno]) != OK)
639                         {
640                                 DisplayVersion();
641                                 printf("Unknown switch: %s\n\n", argv[argno]);
642                                 DisplayHelp();
643                                 errcnt++;
644                                 break;
645                         }
646                 }
647                 else
648                 {
649                         // Record first filename.
650                         if (firstfname == NULL)
651                                 firstfname = argv[argno];
652
653                         strcpy(fnbuf, argv[argno]);
654                         fext(fnbuf, ".s", 0);
655                         fd = open(fnbuf, 0);
656
657                         if (fd < 0)
658                         {
659                                 printf("Cannot open: %s\n", fnbuf);
660                                 errcnt++;
661                                 continue;
662                         }
663
664                         include(fd, fnbuf);
665                         Assemble();
666                 }
667         }
668
669         // Wind-up processing;
670         // o  save current section (no more code generation)
671         // o  do auto-even of all sections (or boundary alignment as requested
672         //    through '-r')
673     // o  save the last pc value for 6502 (if we used 6502 at all) and
674     //    detemine if the last section contains anything
675         // o  determine name of object file:
676         //    -  "foo.o" for linkable output;
677         //    -  "foo.prg" for GEMDOS executable (-p flag).
678         SaveSection();
679         int temp_section = cursect;
680
681         for(i=TEXT; i<=BSS; i<<=1)
682         {
683                 SwitchSection(i);
684
685                 switch(segpadsize)
686                 {
687                 case 2:  d_even();    break;
688                 case 4:  d_long();    break;
689                 case 8:  d_phrase();  break;
690                 case 16: d_dphrase(); break;
691                 case 32: d_qphrase(); break;
692                 }
693
694                 SaveSection();
695         }
696
697         SwitchSection(M6502);
698
699         if (sloc != currentorg[0])
700         {
701                 currentorg[1] = sloc;
702                 currentorg += 2;
703         }
704
705         // This looks like an awful kludge...  !!! FIX !!!
706         if (temp_section & (M56001P | M56001X | M56001Y))
707         {
708                 SwitchSection(temp_section);
709
710                 if (chptr != dsp_currentorg->start)
711                 {
712                         dsp_currentorg->end = chptr;
713                         dsp_currentorg++;
714                 }
715         }
716
717         SwitchSection(TEXT);
718
719         if (objfname == NULL)
720         {
721                 if (firstfname == NULL)
722                         firstfname = defname;
723
724                 strcpy(fnbuf, firstfname);
725                 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
726                 objfname = fnbuf;
727         }
728
729         // With one pass finished, go back and:
730         // (1)   run through all the fixups and resolve forward references;
731         // (1.5) ensure that remaining fixups can be handled by the linker
732         //       (`lo68' format, extended (postfix) format....)
733         // (2)   generate the output file image and symbol table;
734         // (3)   generate relocation information from left-over fixups.
735         ResolveAllFixups();                                             // Do all fixups
736         StopMark();                                                             // Stop mark tape-recorder
737
738         if (errcnt == 0)
739         {
740                 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
741                         CantCreateFile(objfname);
742
743                 if (verb_flag)
744                 {
745                         s = (prg_flag ? "executable" : "object");
746                         printf("[Writing %s file: %s]\n", s, objfname);
747                 }
748
749                 WriteObject(fd);
750                 close(fd);
751
752                 if (errcnt != 0)
753                         unlink(objfname);
754         }
755
756         if (list_flag)
757         {
758                 if (verb_flag)
759                         printf("[Wrapping-up listing file]\n");
760
761                 listing = 1;
762                 symtable();
763                 close(list_fd);
764         }
765
766         if (err_flag)
767                 close(err_fd);
768
769         DEBUG dump_everything();
770
771         return errcnt;
772 }
773
774 //
775 // Determine processor endianess
776 //
777 int GetEndianess(void)
778 {
779         int i = 1;
780         char * p = (char *)&i;
781
782         if (p[0] == 1)
783                 return 0;
784
785         return 1;
786 }
787
788 //
789 // Application entry point
790 //
791 int main(int argc, char ** argv)
792 {
793         perm_verb_flag = 0;                             // Clobber "permanent" verbose flag
794         legacy_flag = 1;                                // Default is legacy mode on (:-P)
795         optim_flags[OPT_56K_SHORT] = 1; // This ensures compatibilty with Motorola's 56k assembler
796
797         cmdlnexec = argv[0];                    // Obtain executable name
798         endian = GetEndianess();                // Get processor endianess
799
800         // If commands were passed in, process them
801         if (argc > 1)
802                 return Process(argc - 1, argv + 1);
803
804         DisplayVersion();
805         DisplayHelp();
806
807         return 0;
808 }