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