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