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