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