e010ff24696f5629e6f6ebaa623ba44573192109
[rmac] / sect.c
1 //
2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // SECT.C - Code Generation, Fixups and Section Management
4 // Copyright (C) 199x Landon Dyer, 2011-2017 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 "sect.h"
10 #include "6502.h"
11 #include "direct.h"
12 #include "error.h"
13 #include "expr.h"
14 #include "listing.h"
15 #include "mach.h"
16 #include "mark.h"
17 #include "riscasm.h"
18 #include "symbol.h"
19 #include "token.h"
20
21
22 // Minimum size of a fixup record
23 #define MIN_FIXUP_MEM  (3 * sizeof(uint16_t) + 1 * sizeof(uint32_t))
24
25 // Function prototypes
26 void MakeSection(int, uint16_t);
27 void SwitchSection(int);
28
29 // Section descriptors
30 SECT sect[NSECTS];              // All sections...
31 int cursect;                    // Current section number
32
33 // These are copied from the section descriptor, the current code chunk
34 // descriptor and the current fixup chunk descriptor when a switch is made into
35 // a section.  They are copied back to the descriptors when the section is left.
36 uint16_t scattr;                // Section attributes
37 uint32_t sloc;                  // Current loc in section
38
39 CHUNK * scode;                  // Current (last) code chunk
40 uint32_t challoc;               // # bytes alloc'd to code chunk
41 uint32_t ch_size;               // # bytes used in code chunk
42 uint8_t * chptr;                // Deposit point in code chunk buffer
43 uint8_t * chptr_opcode; // Backup of chptr, updated before entering code generators
44
45 CHUNK * sfix;                   // Current (last) fixup chunk
46 uint32_t fchalloc;              // # bytes alloc'd to fixup chunk
47 uint32_t fchsize;               // # bytes used in fixup chunk
48 PTR fchptr;                             // Deposit point in fixup chunk buffer
49
50 // Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is
51 // associated with a location.
52 static uint8_t fusiztab[] = {
53         0,      // FU_QUICK
54         1,      // FU_BYTE
55         2,      // FU_WORD
56         2,      // FU_WBYTE
57         4,      // FU_LONG
58         1,      // FU_BBRA
59         0,      // (unused)
60         1,      // FU_6BRA
61 };
62
63 // Offset to REAL fixup location
64 static uint8_t fusizoffs[] = {
65         0,      // FU_QUICK
66         0,      // FU_BYTE
67         0,      // FU_WORD
68         1,      // FU_WBYTE
69         0,      // FU_LONG
70         1,      // FU_BBRA
71         0,      // (unused)
72         0,      // FU_6BRA
73 };
74
75
76 //
77 // Initialize sections; setup initial ABS, TEXT, DATA and BSS sections
78 //
79 void InitSection(void)
80 {
81         // Cleanup all sections
82         for(int i=0; i<NSECTS; i++)
83                 MakeSection(i, 0);
84
85         // Construct default sections, make TEXT the current section
86         MakeSection(ABS,   SUSED | SABS | SBSS);                // ABS
87         MakeSection(TEXT,  SUSED | TEXT       );                // TEXT
88         MakeSection(DATA,  SUSED | DATA       );                // DATA
89         MakeSection(BSS,   SUSED | BSS  | SBSS);                // BSS
90         MakeSection(M6502, SUSED | TEXT       );                // 6502 code section
91
92         // Switch to TEXT for starters
93         SwitchSection(TEXT);
94 }
95
96
97 //
98 // Make a new (clean) section
99 //
100 void MakeSection(int sno, uint16_t attr)
101 {
102         SECT * p = &sect[sno];
103         p->scattr = attr;
104         p->sloc = 0;
105         p->scode = p->sfcode = NULL;
106         p->sfix = p->sffix = NULL;
107 }
108
109
110 //
111 // Switch to another section (copy section & chunk descriptors to global vars
112 // for fast access)
113 //
114 void SwitchSection(int sno)
115 {
116         CHUNK * cp;
117         cursect = sno;
118         SECT * p = &sect[sno];
119
120         m6502 = (sno == M6502); // Set 6502-mode flag
121
122         // Copy section vars
123         scattr = p->scattr;
124         sloc = p->sloc;
125         scode = p->scode;
126         sfix = p->sfix;
127
128         // Copy code chunk vars
129         if ((cp = scode) != NULL)
130         {
131                 challoc = cp->challoc;
132                 ch_size = cp->ch_size;
133                 chptr = cp->chptr + ch_size;
134
135                 // For 6502 mode, add the last org'd address
136                 if (m6502)
137                         chptr = cp->chptr + orgaddr;
138         }
139         else
140                 challoc = ch_size = 0;
141
142         // Copy fixup chunk vars
143         if ((cp = sfix) != NULL)
144         {
145                 fchalloc = cp->challoc;
146                 fchsize = cp->ch_size;
147                 fchptr.cp = cp->chptr + fchsize;
148         }
149         else
150                 fchalloc = fchsize = 0;
151 }
152
153
154 //
155 // Save current section
156 //
157 void SaveSection(void)
158 {
159         SECT * p = &sect[cursect];
160
161         p->scattr = scattr;                                             // Bailout section vars
162         p->sloc = sloc;
163
164         if (scode != NULL)                                              // Bailout code chunk
165                 scode->ch_size = ch_size;
166
167         if (sfix != NULL)                                               // Bailout fixup chunk
168                 sfix->ch_size = fchsize;
169 }
170
171
172 //
173 // Test to see if a location has a fixup sic'd on it.  This is used by the
174 // listing generator to print 'xx's instead of '00's for forward references
175 //
176 int fixtest(int sno, uint32_t loc)
177 {
178         PTR fup;
179
180         // Force update to sect[] variables
181         StopMark();
182
183         // Hairy, ugly linear search for a mark on our location; the speed doesn't
184         // matter, since this is only done when generating a listing, which is
185         // SLOW.
186         for(CHUNK * ch=sect[sno].sffix; ch!=NULL; ch=ch->chnext)
187         {
188                 fup.cp = (uint8_t *)ch->chptr;
189                 uint8_t * fuend = fup.cp + ch->ch_size;
190
191                 while (fup.cp < fuend)
192                 {
193                         uint16_t w = *fup.wp++;
194                         uint32_t xloc = *fup.lp++ + (int)fusizoffs[w & FUMASK];
195                         fup.wp += 2;
196
197                         if (xloc == loc)
198                                 return (int)fusiztab[w & FUMASK];
199
200                         if (w & FU_EXPR)
201                         {
202                                 w = *fup.wp++;
203                                 fup.lp += w;
204                         }
205                         else
206                                 fup.lp++;
207                 }
208         }
209
210         return 0;
211 }
212
213
214 //
215 // Check that there are at least 'amt' bytes left in the current chunk. If
216 // there are not, allocate another chunk of at least 'amt' bytes (and probably
217 // more).
218 //
219 // If 'amt' is zero, ensure there are at least CH_THRESHOLD bytes, likewise.
220 //
221 int chcheck(uint32_t amt)
222 {
223         DEBUG { printf("chcheck(%u)\n", amt); }
224         // If in BSS section, no allocation required
225         if (scattr & SBSS)
226                 return 0;
227
228         if (!amt)
229                 amt = CH_THRESHOLD;
230
231         DEBUG { printf("    challoc=%i, ch_size=%i, diff=%i\n", challoc, ch_size, challoc-ch_size); }
232         if ((int)(challoc - ch_size) >= (int)amt)
233                 return 0;
234
235         if (amt < CH_CODE_SIZE)
236                 amt = CH_CODE_SIZE;
237
238         DEBUG { printf("    amt (adjusted)=%u\n", amt); }
239         SECT * p = &sect[cursect];
240         CHUNK * cp = malloc(sizeof(CHUNK) + amt);
241
242         // First chunk in section
243         if (scode == NULL)
244         {
245                 cp->chprev = NULL;
246                 p->sfcode = cp;
247         }
248         // Add chunk to other chunks
249         else
250         {
251                 cp->chprev = scode;
252                 scode->chnext = cp;
253                 scode->ch_size = ch_size;                       // Save old chunk's globals
254         }
255
256         // Setup chunk and global vars
257         cp->chloc = sloc;
258         cp->chnext = NULL;
259         challoc = cp->challoc = amt;
260         ch_size = cp->ch_size = 0;
261         chptr = cp->chptr = ((uint8_t *)cp) + sizeof(CHUNK);
262         scode = p->scode = cp;
263
264         return 0;
265 }
266
267
268 //
269 // A fixup record is at least 4 pieces of data long, with some optional data at
270 // the end. Is of the form:
271 //
272 // SYMBOL      EXPRESSION
273 // ------      ----------
274 // FU_EXPR.W   FU_EXPR.W        fixup type
275 // loc.L       loc.L            location in section
276 // fileno.W    fileno.W         file number fixup occurred in
277 // lineno.W    lineno.W         line number fixup occurred in
278 // symbol.*    size.W           &symbol (32 or 64 bits) / size of expression
279 //             token.L          size (zero or more) TOKENS (32-bits each)
280 //             ENDEXPR.L        End of expression (with size > zero)
281 // JR.L                         Possible ORG address of RISC JR instruction
282 //
283 // Arrange for a fixup on a location
284 //
285 int AddFixup(uint16_t attr, uint32_t loc, TOKEN * fexpr)
286 {
287         uint32_t i = MIN_FIXUP_MEM;
288         uint16_t len = 0;
289
290         DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
291
292         // Compute length of expression (could be faster); determine if it's the
293         // single-symbol case; no expression if it's just a mark. (? is this true?)
294         if ((*fexpr == SYMBOL) && (fexpr[2] == ENDEXPR))
295         {
296                 // Just a single symbol, possibly followed by a DWORD
297                 i += sizeof(SYM *);
298
299                 // SCPCD: Correct bit mask for attr (else other FU_xxx will match)
300                 // NYAN !
301                 if ((attr & FUMASKRISC) == FU_JR)
302                         i += sizeof(uint32_t);
303         }
304         else
305         {
306                 attr |= FU_EXPR;
307
308                 // Count the # of tokens in the expression
309                 for(len=0; fexpr[len]!=ENDEXPR; len++)
310                 {
311                         // Add one to len for 2X tokens
312                         if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
313                                 len++;
314                 }
315
316                 // Add 1 for ENDEXPR
317                 len++;
318                 i += sizeof(uint16_t) + (len * sizeof(TOKEN));
319         }
320
321         // Alloc another fixup chunk for this one to fit in if necessary
322         if ((fchalloc - fchsize) < i)
323         {
324                 SECT * p = &sect[cursect];
325                 CHUNK * cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
326
327                 // First fixup chunk in section
328                 if (sfix == NULL)
329                 {
330                         cp->chprev = NULL;
331                         p->sffix = cp;
332                 }
333                 // Add to other chunks
334                 else
335                 {
336                         cp->chprev = sfix;
337                         sfix->chnext = cp;
338                         sfix->ch_size = fchsize;
339                 }
340
341                 // Setup fixup chunk and its global vars
342                 cp->chnext = NULL;
343                 fchalloc = cp->challoc = CH_FIXUP_SIZE;
344                 fchsize = cp->ch_size = 0;
345                 fchptr.cp = cp->chptr = ((uint8_t *)cp) + sizeof(CHUNK);
346                 sfix = p->sfix = cp;
347         }
348
349         // Record fixup type, fixup location, and the file number and line number
350         // the fixup is located at.
351         *fchptr.wp++ = attr;
352         *fchptr.lp++ = loc;
353         *fchptr.wp++ = cfileno;
354         *fchptr.wp++ = curlineno;
355
356         // Store postfix expression or pointer to a single symbol, or nothing for a
357         // mark (a zero word is stored in this case--[? is it?]).
358         if (attr & FU_EXPR)
359         {
360                 *fchptr.wp++ = len;
361
362                 while (len--)
363                         *fchptr.lp++ = *fexpr++;
364         }
365         else
366         {
367                 *fchptr.sy++ = symbolPtr[fexpr[1]];
368
369                 // SCPCD: Correct bit mask for attr (else other FU_xxx will match)
370                 // NYAN !
371                 if ((attr & FUMASKRISC) == FU_JR)
372                 {
373                         if (orgactive)
374                                 *fchptr.lp++ = orgaddr;
375                         else
376                                 *fchptr.lp++ = 0x00000000;
377                 }
378         }
379
380         fchsize += i;
381         return 0;
382 }
383
384
385 //
386 // Resolve fixups in a section
387 //
388 int ResolveFixups(int sno)
389 {
390         PTR fup;                                // Current fixup
391         uint32_t eval;                          // Expression value
392         SYM * sy;                               // (Temp) pointer to a symbol
393         uint16_t i;                             // (Temp) word
394         int reg2;
395         uint16_t flags;
396
397         SECT * sc = &sect[sno];
398         CHUNK * ch = sc->sffix;
399
400         if (ch == NULL)
401                 return 0;
402
403         // "Cache" first chunk
404         CHUNK * cch = sc->sfcode;
405
406         // Can't fixup a section with nothing in it
407         if (cch == NULL)
408                 return 0;
409
410         // Wire the 6502 segment's size to its allocated size (64K)
411         if (sno == M6502)
412                 cch->ch_size = cch->challoc;
413
414         do
415         {
416                 fup.cp = ch->chptr;                                     // fup -> start of chunk
417                 uint16_t * fuend = (uint16_t *)(fup.cp + ch->ch_size);  // fuend -> end of chunk
418
419                 while (fup.wp < fuend)
420                 {
421                         uint16_t w = *fup.wp++;         // Fixup word (type+modes+flags)
422                         uint32_t loc = *fup.lp++;       // Location to fixup
423                         cfileno = *fup.wp++;
424                         curlineno = *fup.wp++;
425                         DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
426
427                         // This is based on global vars cfileno, curfname :-P
428                         // This approach is kinda meh as well. I think we can do better
429                         // than this.
430                         SetFilenameForErrorReporting();
431
432                         SYM * esym = NULL;                      // External symbol involved in expr
433
434                         // Search for chunk containing location to fix up; compute a
435                         // pointer to the location (in the chunk). Often we will find the
436                         // Fixup is in the "cached" chunk, so the linear-search is seldom
437                         // executed.
438                         if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
439                         {
440                                 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
441                                 {
442                                         if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
443                                                 break;
444                                 }
445
446                                 if (cch == NULL)
447                                 {
448                                         // Fixup (loc) out of range
449                                         interror(7);
450                                         // NOTREACHED
451                                 }
452                         }
453
454                         // Location to fix (in cached chunk)
455                         uint8_t * locp = cch->chptr + (loc - cch->chloc);
456                         uint16_t eattr = 0;                     // Expression attrib
457
458                         // Compute expression/symbol value and attribs
459
460                         // Complex expression
461                         if (w & FU_EXPR)
462                         {
463                                 i = *fup.wp++;
464
465                                 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
466                                 {
467                                         fup.lp += i;
468                                         continue;
469                                 }
470
471                                 fup.lp += i;
472                         }
473                         // Simple symbol
474                         else
475                         {
476                                 sy = *fup.sy++;
477                                 eattr = sy->sattr;
478
479                                 if (eattr & DEFINED)
480                                         eval = sy->svalue;
481                                 else
482                                         eval = 0;
483
484                                 // If the symbol is not defined, but global, set esym to sy
485                                 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
486                                         esym = sy;
487                         }
488
489                         uint16_t tdb = eattr & TDB;
490
491                         // If the expression is undefined and no external symbol is
492                         // involved, then that's an error.
493                         if (!(eattr & DEFINED) && (esym == NULL))
494                         {
495                                 error(undef_error);
496                                 continue;
497                         }
498
499                         // Do the fixup
500                         //
501                         // If a PC-relative fixup is undefined, its value is *not*
502                         // subtracted from the location (that will happen in the linker
503                         // when the external reference is resolved).
504                         //
505                         // MWC expects PC-relative things to have the LOC subtracted from
506                         // the value, if the value is external (that is, undefined at this
507                         // point).
508                         //
509                         // PC-relative fixups must be DEFINED and either in the same
510                         // section (whereupon the subtraction takes place) or ABS (with no
511                         // subtract).
512                         if (w & FU_PCREL)
513                         {
514                                 if (eattr & DEFINED)
515                                 {
516                                         if (tdb == sno)
517                                                 eval -= (uint32_t)loc;
518                                         else if (tdb)
519                                         {
520                                                 error("PC-relative expr across sections");
521                                                 continue;
522                                         }
523
524                                         if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
525                                                 warn("unoptimized short branch");
526                                 }
527                                 else if (obj_format == MWC)
528                                         eval -= (uint32_t)loc;
529
530                                 tdb = 0;
531                                 eattr &= ~TDB;
532                         }
533
534                         // Do fixup classes
535                         switch (w & FUMASK)
536                         {
537                         // FU_BBRA fixes up a one-byte branch offset.
538                         case FU_BBRA:
539                                 if (!(eattr & DEFINED))
540                                 {
541                                         error("external short branch");
542                                         continue;
543                                 }
544
545                                 eval -= 2;
546
547                                 if (eval + 0x80 >= 0x100)
548                                         goto rangeErr;
549
550                                 if (eval == 0)
551                                 {
552                                         error("illegal bra.s with zero offset");
553                                         continue;
554                                 }
555
556                                 *++locp = (uint8_t)eval;
557                                 break;
558                         // Fixup one-byte value at locp + 1.
559                         case FU_WBYTE:
560                                 locp++;
561                                 // FALLTHROUGH
562                         // Fixup one-byte forward references
563                         case FU_BYTE:
564                                 if (!(eattr & DEFINED))
565                                 {
566                                         error("external byte reference");
567                                         continue;
568                                 }
569
570                                 if (tdb)
571                                 {
572                                         error("non-absolute byte reference");
573                                         continue;
574                                 }
575
576                                 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
577                                         goto rangeErr;
578
579                                 if (w & FU_SEXT)
580                                 {
581                                         if (eval + 0x100 >= 0x200)
582                                                 goto rangeErr;
583                                 }
584                                 else if (eval >= 0x100)
585                                         goto rangeErr;
586
587                                 *locp = (uint8_t)eval;
588                                 break;
589                         // Fixup high/low byte off word for 6502
590                         case FU_BYTEH:
591                                 if (!(eattr & DEFINED))
592                                 {
593                                         error("external byte reference");
594                                         continue;
595                                 }
596
597                                 *locp = (uint8_t)((eval >> 8) & 0xFF);
598                                 break;
599                         case FU_BYTEL:
600                                 if (!(eattr & DEFINED))
601                                 {
602                                         error("external byte reference");
603                                         continue;
604                                 }
605
606                                 *locp = (uint8_t)(eval & 0xFF);
607                                 break;
608                         // Fixup WORD forward references;
609                         // the word could be unaligned in the section buffer, so we have to
610                         // be careful.
611                         case FU_WORD:
612                                 if ((w & FUMASKRISC) == FU_JR)
613                                 {
614                                         uint32_t orgaddr = *fup.lp++;
615
616                                         if (orgaddr)
617                                                 reg2 = (signed)((eval - (orgaddr + 2)) / 2);
618                                         else
619                                                 reg2 = (signed)((eval - (loc + 2)) / 2);
620
621                                         if ((reg2 < -16) || (reg2 > 15))
622                                         {
623                                                 error("relative jump out of range");
624                                                 break;
625                                         }
626
627                                         *locp = (uint8_t)(*locp | ((reg2 >> 3) & 0x03));
628                                         locp++;
629                                         *locp = (uint8_t)(*locp | ((reg2 & 0x07) << 5));
630                                         break;
631                                 }
632
633                                 if ((w & FUMASKRISC) == FU_NUM15)
634                                 {
635                                         if (eval < -16 || eval > 15)
636                                         {
637                                                 error("constant out of range");
638                                                 break;
639                                         }
640
641                                         *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
642                                         locp++;
643                                         *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
644                                         break;
645                                 }
646
647                                 if ((w & FUMASKRISC) == FU_NUM31)
648                                 {
649                                         if (eval < 0 || eval > 31)
650                                         {
651                                                 error("constant out of range");
652                                                 break;
653                                         }
654
655                                         *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
656                                         locp++;
657                                         *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
658                                         break;
659                                 }
660
661                                 if ((w & FUMASKRISC) == FU_NUM32)
662                                 {
663                                         if (eval < 1 || eval > 32)
664                                         {
665                                                 error("constant out of range");
666                                                 break;
667                                         }
668
669                                         if (w & FU_SUB32)
670                                                 eval = (32 - eval);
671
672                                         eval = (eval == 32) ? 0 : eval;
673                                         *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
674                                         locp++;
675                                         *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
676                                         break;
677                                 }
678
679                                 if ((w & FUMASKRISC) == FU_REGONE)
680                                 {
681                                         if (eval < 0 || eval > 31)
682                                         {
683                                                 error("register value out of range");
684                                                 break;
685                                         }
686
687                                         *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
688                                         locp++;
689                                         *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
690                                         break;
691                                 }
692
693                                 if ((w & FUMASKRISC) == FU_REGTWO)
694                                 {
695                                         if (eval < 0 || eval > 31)
696                                         {
697                                                 error("register value out of range");
698                                                 break;
699                                         }
700
701                                         locp++;
702                                         *locp = (uint8_t)(*locp | (eval & 0x1F));
703                                         break;
704                                 }
705
706                                 if (!(eattr & DEFINED))
707                                 {
708                                         flags = MWORD;
709
710                                         if (w & FU_PCREL)
711                                                 flags |= MPCREL;
712
713                                         MarkRelocatable(sno, loc, 0, flags, esym);
714                                 }
715                                 else
716                                 {
717                                         if (tdb)
718                                                 MarkRelocatable(sno, loc, tdb, MWORD, NULL);
719
720                                         if (w & FU_SEXT)
721                                         {
722                                                 if (eval + 0x10000 >= 0x20000)
723                                                         goto rangeErr;
724                                         }
725                                         else
726                                         {
727                                                 // Range-check BRA and DBRA
728                                                 if (w & FU_ISBRA)
729                                                 {
730                                                         if (eval + 0x8000 >= 0x10000)
731                                                         goto rangeErr;
732                                                 }
733                                                 else if (eval >= 0x10000)
734                                                         goto rangeErr;
735                                         }
736                                 }
737
738                                 // 6502 words are little endian, so handle that here
739                                 if (sno == M6502)
740                                         SETLE16(locp, 0, eval)
741                                 else
742                                     SETBE16(locp, 0, eval)
743
744                                 break;
745                         // Fixup LONG forward references;
746                         // the long could be unaligned in the section buffer, so be careful
747                         // (again).
748                         case FU_LONG:
749                                 flags = MLONG;
750
751                                 if ((w & FUMASKRISC) == FU_MOVEI)
752                                 {
753                                         // Long constant in MOVEI # is word-swapped, so fix it here
754                                         eval = WORDSWAP32(eval);
755                                         flags |= MMOVEI;
756                                 }
757
758                                 // If the symbol is undefined, make sure to pass the symbol in
759                                 // to the MarkRelocatable() function.
760                                 if (!(eattr & DEFINED))
761                                         MarkRelocatable(sno, loc, 0, flags, esym);
762                                 else if (tdb)
763                                         MarkRelocatable(sno, loc, tdb, flags, NULL);
764
765                                 SETBE32(locp, 0, eval);
766                                 break;
767
768                         // Fixup a 3-bit "QUICK" reference in bits 9..1
769                         // (range of 1..8) in a word. Really bits 1..3 in a byte.
770                         case FU_QUICK:
771                                 if (!(eattr & DEFINED))
772                                 {
773                                         error("External quick reference");
774                                         continue;
775                                 }
776
777                                 if (eval < 1 || eval > 8)
778                                         goto rangeErr;
779
780                                 *locp |= (eval & 7) << 1;
781                                 break;
782
783                         // Fix up 6502 funny branch
784                         case FU_6BRA:
785                                 eval -= (loc + 1);
786
787                                 if (eval + 0x80 >= 0x100)
788                                         goto rangeErr;
789
790                                 *locp = (uint8_t)eval;
791                                 break;
792
793                         default:
794                                 // Bad fixup type--this should *never* happen!
795                                 interror(4);
796                                 // NOTREACHED
797                         }
798                         continue;
799 rangeErr:
800                         error("expression out of range");
801                 }
802
803                 ch = ch->chnext;
804         }
805         while (ch != NULL);
806
807         return 0;
808 }
809
810 //
811 // Resolve all fixups
812 //
813 int ResolveAllFixups(void)
814 {
815         // Make undefined symbols GLOBL
816         if (glob_flag)
817                 ForceUndefinedSymbolsGlobal();
818
819         DEBUG printf("Resolving TEXT sections...\n");
820         ResolveFixups(TEXT);
821         DEBUG printf("Resolving DATA sections...\n");
822         ResolveFixups(DATA);
823         DEBUG printf("Resolving 6502 sections...\n");
824         ResolveFixups(M6502);           /* fixup 6502 section (if any) */
825
826         return 0;
827 }
828