Fixed problem with nested MACROs.
[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 = &sect[sno];
75         p->scattr = attr;
76         p->sloc = 0;
77         p->scode = p->sfcode = NULL;
78         p->sfix = p->sffix = NULL;
79 }
80
81
82 //
83 // Switch to Another Section (Copy Section & Chunk Descriptors to Global Vars
84 // for Fast Access)
85 //
86 void switchsect(int sno)
87 {
88         CHUNK * cp;                                                             // Chunk pointer
89         cursect = sno;
90         SECT * p = &sect[sno];
91
92         scattr = p->scattr;                                             // Copy section vars
93         sloc = p->sloc;
94         scode = p->scode;
95         sfix = p->sfix;
96
97         // Copy code chunk vars
98         if ((cp = scode) != NULL)
99         {
100                 challoc = cp->challoc;
101                 ch_size = cp->ch_size;
102                 chptr = cp->chptr + ch_size;
103         }
104         else
105                 challoc = ch_size = 0;
106
107         // Copy fixup chunk vars 
108         if ((cp = sfix) != NULL)
109         {
110                 fchalloc = cp->challoc;
111                 fchsize = cp->ch_size;
112                 fchptr.cp = cp->chptr + fchsize;
113         }
114         else
115                 fchalloc = fchsize = 0;
116 }
117
118
119 //
120 // Save Current Section
121 //
122 void savsect(void)
123 {
124         SECT * p = &sect[cursect];
125
126         p->scattr = scattr;                                             // Bailout section vars
127         p->sloc = sloc;
128
129         if (scode != NULL)                                              // Bailout code chunk
130                 scode->ch_size = ch_size;
131
132         if (sfix != NULL)                                               // Bailout fixup chunk
133                 sfix->ch_size = fchsize;
134 }
135
136
137 //
138 // Initialize Sections; Setup initial ABS, TEXT, DATA and BSS sections
139 //
140 void init_sect(void)
141 {
142         int i;
143
144         // Cleanup all sections
145         for(i=0; i<NSECTS; i++)
146                 mksect(i, 0);
147
148         // Construct default sections, make TEXT the current section
149         mksect(ABS,   SUSED | SABS | SBSS);             // ABS
150         mksect(TEXT,  SUSED | TEXT       );             // TEXT
151         mksect(DATA,  SUSED | DATA       );             // DATA
152         mksect(BSS,   SUSED | BSS | SBSS );             // BSS
153 //      mksect(M6502, SUSED | TEXT       );             // 6502 code section
154
155         switchsect(TEXT);                                               // Switch to TEXT for starters
156 }
157
158
159 //
160 // Test to see if a location has a fixup sic'd on it.  This is used by the
161 // listing generator to print 'xx's instead of '00's for forward references
162 //
163 int fixtest(int sno, LONG loc)
164 {
165         CHUNK * ch;
166         PTR fup;
167         char * fuend;
168         WORD w;
169         LONG xloc;
170
171         stopmark();                                                             // Force update to sect[] variables
172
173         // Hairy, ugly linear search for a mark on our location;
174         // the speed doesn't matter, since this is only done when generating a
175         // listing, which is SLOW.
176         for(ch=sect[sno].sffix; ch!=NULL; ch=ch->chnext)
177         {
178                 fup.cp = (char *)ch->chptr;
179                 fuend = fup.cp + ch->ch_size;
180
181                 while (fup.cp < fuend)
182                 {
183                         w = *fup.wp++;
184                         xloc = *fup.lp++ + (int)fusizoffs[w & FUMASK];
185                         fup.wp += 2;
186
187                         if (xloc == loc)
188                                 return (int)fusiztab[w & FUMASK];
189
190                         if (w & FU_EXPR)
191                         {
192                                 w = *fup.wp++;
193                                 fup.lp += w;
194                         }
195                         else
196                                 ++fup.lp;
197                 }
198         }
199
200         return 0;
201 }
202
203
204 // 
205 // Check that there are at least `amt' bytes left in the current chunk. If
206 // there are not, allocate another chunk of at least `amt' bytes (and probably
207 // more).
208 // 
209 // If `amt' is zero, ensure there are at least CH_THRESHOLD bytes, likewise.
210 //
211 int chcheck(LONG amt)
212 {
213         // If in BSS section, no allocation required
214         if (scattr & SBSS)
215                 return 0;
216
217         if (!amt)
218                 amt = CH_THRESHOLD;
219
220         if ((int)(challoc - ch_size) >= (int)amt) 
221                 return 0;
222
223         if (amt < CH_CODE_SIZE)
224                 amt = CH_CODE_SIZE;
225
226         SECT * p = &sect[cursect];
227         CHUNK * cp = malloc(sizeof(CHUNK) + amt);
228
229         // First chunk in section
230         if (scode == NULL)
231         {
232                 cp->chprev = NULL;
233                 p->sfcode = cp;
234         }
235         // Add chunk to other chunks
236         else
237         {
238                 cp->chprev = scode;
239                 scode->chnext = cp;
240                 scode->ch_size = ch_size;                       // Save old chunk's globals 
241         }
242
243         // Setup chunk and global vars
244         cp->chloc = sloc;
245         cp->chnext = NULL;
246         challoc = cp->challoc = amt;
247         ch_size = cp->ch_size = 0;
248         chptr = cp->chptr = ((char *)cp) + sizeof(CHUNK);
249         scode = p->scode = cp;
250
251         return 0;
252 }
253
254
255 // This is really wrong. We need to make some proper structures here so we don't
256 // have to count sizes of objects, that's what the compiler's for! :-P
257 #define FIXUP_BASE_SIZE (sizeof(WORD) + sizeof(LONG) + sizeof(WORD) + sizeof(WORD))
258 //
259 // Arrange for a fixup on a location
260 //
261 int fixup(WORD attr, LONG loc, TOKEN * fexpr)
262 {
263         LONG i;
264         LONG len = 0;
265         CHUNK * cp;
266         SECT * p;
267         // Shamus: Expression lengths are voodoo ATM (varibale "i"). Need to fix this.
268         DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
269
270         // Compute length of expression (could be faster); determine if it's the
271         // single-symbol case; no expression if it's just a mark. This code assumes
272         // 16 bit WORDs and 32 bit LONGs
273         if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
274         {
275                 // Just a single symbol
276                 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
277                 if ((attr & 0x0F00) == FU_JR)
278                 {
279 //                      i = 18;
280 //                      i = FIXUP_BASE_SIZE + (sizeof(LONG) * 2);
281                         i = FIXUP_BASE_SIZE + sizeof(SYM *) + sizeof(LONG);
282                 }
283                 else
284                 {
285 //                      i = 14;
286                         i = FIXUP_BASE_SIZE + sizeof(SYM *);
287                 }
288         }
289         else
290         {
291                 attr |= FU_EXPR;
292
293                 for(len=0; fexpr[len]!=ENDEXPR; len++)
294                 {
295                         if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
296                                 len++;
297                 }
298
299                 len++;                                                          // Add 1 for ENDEXPR 
300 //              i = (len << 2) + 12;
301                 i = FIXUP_BASE_SIZE + sizeof(WORD) + (len * sizeof(TOKEN));
302         }
303
304         // Maybe alloc another fixup chunk for this one to fit in
305         if ((fchalloc - fchsize) < i)
306         {
307                 p = &sect[cursect];
308                 cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
309
310                 // First fixup chunk in section
311                 if (sfix == NULL)
312                 {
313                         cp->chprev = NULL;
314                         p->sffix = cp;
315                 }
316                 // Add to other chunks
317                 else
318                 {
319                         cp->chprev = sfix;
320                         sfix->chnext = cp;
321                         sfix->ch_size = fchsize;
322                 }
323
324                 // Setup fixup chunk and its global vars
325                 cp->chnext = NULL;
326                 fchalloc = cp->challoc = CH_FIXUP_SIZE;
327                 fchsize = cp->ch_size = 0;
328                 fchptr.cp = cp->chptr = ((char *)cp) + sizeof(CHUNK);
329                 sfix = p->sfix = cp;
330         }
331
332         // Record fixup type, fixup location, and the file number and line number
333         // the fixup is located at.
334         *fchptr.wp++ = attr;
335         *fchptr.lp++ = loc;
336         *fchptr.wp++ = cfileno;
337         *fchptr.wp++ = (WORD)curlineno;
338
339         // Store postfix expression or pointer to a single symbol, or nothing for a
340         // mark.
341         if (attr & FU_EXPR)
342         {
343                 *fchptr.wp++ = (WORD)len;
344
345                 while (len--)
346                         *fchptr.lp++ = (LONG)*fexpr++;
347         }
348         else
349         {
350 //              *fchptr.lp++ = (LONG)fexpr[1];
351                 *fchptr.sy++ = symbolPtr[fexpr[1]];
352         }
353
354         // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
355         if ((attr & 0x0F00) == FU_JR)
356         {
357                 if (orgactive)
358                         *fchptr.lp++ = orgaddr;
359                 else
360                         *fchptr.lp++ = 0x00000000;
361         }
362
363         fchsize += i;
364         return 0;
365 }
366
367
368 //
369 // Resolve all Fixups
370 //
371 int ResolveAllFixups(void)
372 {
373         unsigned i;
374         char buf[EBUFSIZ];
375
376         // Make undefined symbols GLOBL
377         if (glob_flag)
378                 syg_fix();
379
380         DEBUG printf("Resolving TEXT sections...\n");
381         ResolveFixups(TEXT);
382         DEBUG printf("Resolving DATA sections...\n");
383         ResolveFixups(DATA);
384         
385         // We need to do a final check of forward 'jump' destination addresses that
386         // are external
387         for(i=0; i<MAXFWDJUMPS; i++)
388         {
389                 if (fwdjump[i])
390                 {
391                         err_setup();
392                         sprintf(buf, "* \'jump\' at $%08X - destination address is external to this source file and cannot have its aligment validated", fwdjump[i]);
393
394                         if (listing > 0)
395                                 ship_ln(buf);
396
397                         if (err_flag)
398                                 write(err_fd, buf, (LONG)strlen(buf));
399                         else
400                                 printf("%s\n", buf);
401                 }
402         }
403
404         return 0;
405 }
406
407
408 //
409 // Resolve Fixups in a Section
410 //
411 int ResolveFixups(int sno)
412 {
413         PTR fup;                                        // Current fixup
414         WORD * fuend;                           // End of last fixup (in this chunk)
415         WORD w;                                         // Fixup word (type+modes+flags)
416         char * locp;                            // Location to fix (in cached chunk) 
417         LONG loc;                                       // Location to fixup
418         VALUE eval;                                     // Expression value 
419         WORD eattr;                                     // Expression attrib
420         SYM * esym;                                     // External symbol involved in expr
421         SYM * sy;                                       // (Temp) pointer to a symbol
422         WORD i;                                         // (Temp) word
423         WORD tdb;                                       // eattr & TDB
424         LONG oaddr;
425         int reg2;
426         WORD flags;
427         unsigned page_jump = 0;
428         unsigned address = 0;
429         unsigned j;
430         char buf[EBUFSIZ];
431         
432         SECT * sc = &sect[sno];
433         CHUNK * ch = sc->sffix;
434
435         if (ch == NULL)
436                 return 0;
437
438         CHUNK * cch = sc->sfcode;                               // "cache" first chunk
439
440         if (cch == NULL)                                                // Can't fixup a sect with nothing in it
441                 return 0;
442
443         do
444         {
445                 fup.cp = ch->chptr;                                     // fup -> start of chunk
446                 fuend = (WORD *)(fup.cp + ch->ch_size); // fuend -> end of chunk
447
448                 while (fup.wp < fuend)
449                 {
450                         w = *fup.wp++;
451                         loc = *fup.lp++;
452                         cfileno = *fup.wp++;
453                         curlineno = (int)*fup.wp++;
454
455                         esym = NULL;
456
457                         // Search for chunk containing location to fix up; compute a
458                         // pointer to the location (in the chunk). Often we will find the
459                         // fixup is in the "cached" chunk, so the linear-search is seldom
460                         // executed.
461                         if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
462                         {
463                                 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
464                                 {
465                                         if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
466                                                 break;
467                                 }
468
469                                 if (cch == NULL)
470                                 {
471                                         interror(7);                    // Fixup (loc) out of range 
472                                         // NOTREACHED
473                                 }
474                         }
475
476                         locp = cch->chptr + (loc - cch->chloc);
477                         eattr = 0;
478
479                         // Compute expression/symbol value and attribs
480                         // Complex expression
481                         if (w & FU_EXPR)
482                         {
483                                 i = *fup.wp++;
484
485                                 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
486                                 {
487                                         fup.lp += i;
488                                         continue;
489                                 }
490
491                                 fup.lp += i;
492                         }
493                         // Simple symbol
494                         else
495                         {
496                                 sy = *fup.sy++;
497                                 eattr = sy->sattr;
498
499                                 if (eattr & DEFINED)
500                                         eval = sy->svalue;
501                                 else
502                                         eval = 0;
503
504                                 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
505                                         esym = sy;
506                         }
507
508                         tdb = (WORD)(eattr & TDB);
509
510                         // If the expression is undefined and no external symbol is
511                         // involved, then it's an error.
512                         if (!(eattr & DEFINED) && esym == NULL)
513                         {
514                                 error(undef_error);
515                                 continue;
516                         }
517
518                         if (((w & 0x0F00) == FU_MOVEI) && esym)
519                                 esym->sattre |= RISCSYM;
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 & 0x0F00) == FU_JR) || ((w & 0x0F00) == FU_MJR))
616                                 {
617                                         oaddr = *fup.lp++;
618
619                                         if (oaddr)
620                                         {
621                                                 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
622                                         }
623                                         else
624                                         {
625                                                 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
626                                         }
627
628                                         if ((w & 0x0F00) == FU_MJR)
629                                         {
630                                                 // Main code destination alignment checking here for
631                                                 // forward declared labels
632                                                 address = (oaddr) ? oaddr : loc;
633
634                                                 if (((address >= 0xF03000) && (address < 0xF04000)
635                                                         && (eval < 0xF03000)) || ((eval >= 0xF03000)
636                                                         && (eval < 0xF04000) && (address < 0xF03000)))
637                                                 {
638                                                         warni("* \'jr\' at $%08X - cannot jump relative between "
639                                                                 "main memory and local gpu ram", address);
640                                                 }
641                                                 else
642                                                 {
643                                                         page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
644
645                                                         if (page_jump)
646                                                         {
647                                                                 // This jump is to a page outside of the
648                                                                 // current 256 byte page
649                                                                 if (eval % 4)
650                                                                 {
651                                                                         warni("* \'jr\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
652                                                                 }
653                                                         }
654                                                         else
655                                                         {
656                                                                 // This jump is in the current 256 byte page
657                                                                 if ((eval - 2) % 4)
658                                                                 {
659                                                                         warni("* \'jr\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
660                                                                 }
661                                                         }
662                                                 }
663                                         }
664
665                                         if ((reg2 < -16) || (reg2 > 15))
666                                         {
667                                                 error("relative jump out of range");
668                                                 break;
669                                         }
670
671                                         *locp = (char)(*locp | ((reg2 >> 3) & 0x03));
672                                         locp++;
673                                         *locp = (char)(*locp | ((reg2 & 0x07) << 5));
674                                         break;
675                                 }
676
677                                 if ((w & 0x0F00) == FU_NUM15)
678                                 {
679                                         if (eval < -16 || eval > 15)
680                                         {
681                                                 error("constant out of range");
682                                                 break;
683                                         }
684
685                                         *locp = (char)(*locp | ((eval >> 3) & 0x03));
686                                         locp++;
687                                         *locp = (char)(*locp | ((eval & 0x07) << 5));
688                                         break;
689                                 }
690
691                                 if ((w & 0x0F00) == FU_NUM31)
692                                 {
693                                         if (eval < 0 || eval > 31)
694                                         {
695                                                 error("constant out of range");
696                                                 break;
697                                         }
698
699                                         *locp = (char)(*locp | ((eval >> 3) & 0x03));
700                                         locp++;
701                                         *locp = (char)(*locp | ((eval & 0x07) << 5));
702                                         break;
703                                 }
704
705                                 if ((w & 0x0F00) == FU_NUM32)
706                                 {
707                                         if (eval < 1 || eval > 32)
708                                         {
709                                                 error("constant out of range");
710                                                 break;
711                                         }
712
713                                         if (w & FU_SUB32)
714                                                 eval = (32 - eval);
715
716                                         eval = (eval == 32) ? 0 : eval;
717                                         *locp = (char)(*locp | ((eval >> 3) & 0x03));
718                                         locp++;
719                                         *locp = (char)(*locp | ((eval & 0x07) << 5));
720                                         break;
721                                 }
722
723                                 if ((w & 0x0F00) == FU_REGONE)
724                                 {
725                                         if (eval < 0 || eval > 31)
726                                         {
727                                                 error("register value out of range");
728                                                 break;
729                                         }
730
731                                         *locp = (char)(*locp | ((eval >> 3) & 0x03));
732                                         locp++;
733                                         *locp = (char)(*locp | ((eval & 0x07) << 5));
734                                         break;
735                                 }
736
737                                 if ((w & 0x0F00) == FU_REGTWO)
738                                 {
739                                         if (eval < 0 || eval > 31)
740                                         {
741                                                 error("register value out of range");
742                                                 break;
743                                         }
744
745                                         locp++;
746                                         *locp = (char)(*locp | (eval & 0x1F));
747                                         break;
748                                 }
749
750                                 if (!(eattr & DEFINED))
751                                 {
752                                         if (w & FU_PCREL)
753                                                 w = MPCREL | MWORD;
754                                         else
755                                                 w = MWORD;
756
757                                         rmark(sno, loc, 0, w, esym);
758                                 }
759                                 else
760                                 {
761                                         if (tdb)
762                                                 rmark(sno, loc, tdb, MWORD, NULL);
763
764                                         if (w & FU_SEXT)
765                                         {
766                                                 if (eval + 0x10000 >= 0x20000)
767                                                         goto range;
768                                         }
769                                         else
770                                         {
771                                                 // Range-check BRA and DBRA
772                                                 if (w & FU_ISBRA)
773                                                 {
774                                                         if (eval + 0x8000 >= 0x10000)
775                                                         goto range;
776                                                 }
777                                                 else if (eval >= 0x10000)
778                                                         goto range;
779                                         }
780                                 }
781
782                                 *locp++ = (char)(eval >> 8);
783                                 *locp = (char)eval;
784                                 break;
785                         // Fixup LONG forward references;
786                         // the long could be unaligned in the section buffer, so be careful
787                         // (again).
788                         case FU_LONG:
789                                 if ((w & 0x0F00) == FU_MOVEI)
790                                 {
791                                         address = loc + 4;
792
793                                         if (eattr & DEFINED)
794                                         {
795                                                 for(j=0; j<fwindex; j++)
796                                                 {
797                                                         if (fwdjump[j] == address)
798                                                         {
799                                                                 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
800
801                                                                 if (page_jump)
802                                                                 {
803                                                                         if (eval % 4)
804                                                                         {
805                                                                                 err_setup();
806                                                                                 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
807
808                                                                                 if (listing > 0)
809                                                                                         ship_ln(buf);
810
811                                                                                 if (err_flag)
812                                                                                         write(err_fd, buf, (LONG)strlen(buf));
813                                                                                 else
814                                                                                         printf("%s\n", buf);
815                                                                         }          
816                                                                 }
817                                                                 else
818                                                                 {
819                                                                         if (!(eval & 0x0000000F) || ((eval - 2) % 4))
820                                                                         {
821                                                                                 err_setup();
822                                                                                 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
823
824                                                                                 if (listing > 0)
825                                                                                         ship_ln(buf);
826
827                                                                                 if (err_flag)
828                                                                                         write(err_fd, buf, (LONG)strlen(buf));
829                                                                                 else
830                                                                                         printf("%s\n", buf);
831                                                                         }          
832                                                                 }
833
834                                                                 // Clear this jump as it has been checked
835                                                                 fwdjump[j] = 0;
836                                                                 j = fwindex;
837                                                         }
838                                                 }
839                                         }
840
841                                         eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
842                                         flags = (MLONG|MMOVEI);
843                                 }
844                                 else
845                                         flags = MLONG;
846
847                                 if (!(eattr & DEFINED))
848                                 {
849                                         rmark(sno, loc, 0, flags, esym);
850                                 }
851                                 else if (tdb)
852                                 {
853                                         rmark(sno, loc, tdb, flags, NULL);
854                                 }
855
856                                 *locp++ = (char)(eval >> 24);
857                                 *locp++ = (char)(eval >> 16);
858                                 *locp++ = (char)(eval >> 8);
859                                 *locp = (char)eval;
860                                 break;
861                         // Fixup a 3-bit "QUICK" reference in bits 9..1
862                         // (range of 1..8) in a word.  Really bits 1..3 in a byte.
863                         case FU_QUICK:
864                                 if (!(eattr & DEFINED))
865                                 {
866                                         error("External quick reference");
867                                         continue;
868                                 }
869
870                                 if (eval < 1 || eval > 8)
871                                         goto range;
872
873                                 *locp |= (eval & 7) << 1;
874                                 break;
875                         // Fix up 6502 funny branch
876                         case FU_6BRA:
877                                 eval -= (loc + 1);
878
879                                 if (eval + 0x80 >= 0x100)
880                                         goto range;
881
882                                 *locp = (char)eval;
883                                 break;
884                         default:
885                                 interror(4);                                 // Bad fixup type
886                                 // NOTREACHED
887                         }
888                         continue;
889 range:
890                         error("expression out of range");
891                 }
892
893                 ch = ch->chnext;
894         }
895         while (ch != NULL);
896
897         return 0;
898 }