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