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