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