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