]> Shamusworld >> Repos - rmac/blob - sect.h
Fix for bug #29.
[rmac] / sect.h
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // SECT.H - 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 #ifndef __SECT_H__
10 #define __SECT_H__
11
12 #include "rmac.h"
13
14 // Macros to deposit code in the current section
15 // D_rword deposits a "6502" format (low, high) word (01).
16 // D_rlong deposits a MWC "canonical byte order" longword (2301).
17 #define D_byte(b)       {*chptr++=(char)b; ++sloc; ++ch_size; if(orgactive) ++orgaddr;}
18 #define D_word(w)       {chcheck(2);*chptr++=(char)(w>>8); *chptr++=(char)w; \
19                                                 sloc+=2; ch_size+=2; if(orgactive) orgaddr += 2;}
20 #define D_long(lw)      {*chptr++=(char)(lw>>24); *chptr++=(char)(lw>>16);\
21                                                 *chptr++=(char)(lw>>8); *chptr++=(char)lw; \
22                                                 sloc+=4; ch_size += 4; if(orgactive) orgaddr += 4;}
23 //#define D_rword(w)    {*chptr++=(char)w; *chptr++=(char)(w>>8); \
24 //                                              sloc+=2; ch_size+=2;if(orgactive) orgaddr += 2;}
25 //#define D_rlong(lw)   {*chptr++=(char)(lw>>16);*chptr++=(char)(lw>>24);\
26 //                                              *chptr++=(char)lw;*chptr++=(char)(lw>>8); \
27 //                                              sloc+=4; ch_size += 4;if(orgactive) orgaddr += 4;}
28 // Fill n bytes with zeroes
29 #define D_ZEROFILL(n)   {memset(chptr, 0, n); chptr+=n; sloc+=n; ch_size+=n;\
30                                                 if (orgactive) orgaddr+=n;}
31
32 #define NSECTS       16                         // Max. number of sections
33
34 // Tunable (storage) definitions
35 #define CH_THRESHOLD    64                      // Minimum amount of space in code chunk
36 #define CH_CODE_SIZE    2048            // Code chunk normal allocation
37 #define CH_FIXUP_SIZE   1024            // Fixup chunk normal allocation
38
39 // Section attributes (.scattr)
40 #define SUSED        0x8000                     // Section is used (really, valid)
41 #define SBSS         0x4000                     // Section can contain no data
42 #define SABS         0x2000                     // Section is absolute
43 #define SPIC         0x1000                     // Section is position-independent code
44
45 // Fixup record a WORD of these bits, followed by a loc and then a pointer
46 // to a symbol or an ENDEXPR-terminated postfix expression.
47 //
48 // SYMBOL               EXPRESSION
49 // ------               ----------
50 // ~FU_EXPR    FU_EXPR     fixup type
51 // loc.L       loc.L       location in section
52 // fileno.W    fileno.W    file number fixup occurred in
53 // lineno.W    lineno.W    line number fixup occurred in
54 // symbol.L    size.W      &symbol  /  size of expression
55 // token.L     expression list
56 // (etc)
57 // ENDEXPR.L    (end of expression)
58 #define FUMASK       007                        // Mask for fixup cases:
59 #define FU_QUICK     000                        // Fixup 3-bit quick instr field
60 #define FU_BYTE      001                        // Fixup byte
61 #define FU_WORD      002                        // Fixup word
62 #define FU_WBYTE     003                        // Fixup byte (at loc+1)
63 #define FU_LONG      004                        // Fixup long
64 #define FU_BBRA      005                        // Fixup byte branch
65 #define FU_6BRA      007                        // Fixup 6502-format branch offset
66 #define FU_SEXT      010                        // Ok to sign extend
67 #define FU_PCREL     020                        // Subtract PC first
68 #define FU_EXPR      040                        // Expression (not symbol) follows
69
70 #define FUMASKRISC      0x0F00                  // Mask for RISC fixup cases
71 #define FU_MOVEI     0x0100
72 #define FU_JR        0x0200
73 #define FU_REGONE    0x0400
74 #define FU_NUM15     0x0500
75 #define FU_NUM31     0x0600
76 #define FU_NUM32     0x0700
77 #define FU_REGTWO    0x0800
78
79 #define FU_SUB32     0x1000
80 #define FU_ISBRA     0x2000                     // Word forward fixup is a BRA or DBRA
81 #define FU_LBRA      0x4000                     // Long branch, for short branch detect
82 #define FU_DONE      0x8000                     // Fixup has been done
83
84 // Chunks are used to hold generated code and fixup records
85 #define CHUNK  struct _chunk
86 CHUNK {
87    CHUNK * chnext;                                      // Next, previous chunks in section
88    CHUNK * chprev;
89    LONG chloc;                                          // Base addr of this chunk
90    LONG challoc;                                        // # bytes allocated for chunk
91    LONG ch_size;                                        // # bytes chunk actually uses
92    char * chptr;                                        // Data for this chunk
93 };
94
95 // Section descriptor
96 #define SECT   struct _sect
97 SECT {
98    WORD scattr;                                         // Section attributes
99    LONG sloc;                                           // Current loc-in / size-of section 
100    CHUNK * sfcode;                                      // First chunk in section
101    CHUNK * scode;                                       // Last chunk in section
102    CHUNK * sffix;                                       // First fixup chunk
103    CHUNK * sfix;                                        // Last fixup chunk
104 };
105
106 // A mark is of the form:
107 // .W    <to+flags>     section mark is relative to, and flags in upper byte
108 // .L    <loc>          location of mark in "from" section
109 // .W    [from]         new from section
110 // .L    [symbol]       symbol involved in external reference
111 #define MCHUNK struct _mchunk
112 MCHUNK {
113    MCHUNK * mcnext;                                     // Next mark chunk
114    PTR mcptr;                                           // Vector of marks
115    LONG mcalloc;                                        // # marks allocted to mark block
116    LONG mcused;                                         // # marks used in block
117 };
118
119 #define MWORD        0x0000                     // Marked word
120 #define MLONG        0x0100                     // Marked long 
121 #define MMOVEI       0x0200
122 #define MCHFROM      0x8000                     // Mark includes change-to-from
123 #define MSYMBOL      0x4000                     // Mark includes symbol number
124 #define MCHEND       0x2000                     // Indicates end of mark chunk
125 #define MPCREL       0x1000                     // Mark is PC-relative
126
127 // Globals, external etc
128 extern LONG sloc;
129 extern WORD scattr;
130 extern char * chptr;
131 extern LONG ch_size;
132 extern int cursect;
133 extern SECT sect[];
134 extern LONG challoc;
135 extern CHUNK * scode;
136
137 // Prototypes
138 void InitSection(void);
139 void SwitchSection(int);
140 void SaveSection(void);
141 int fixtest(int, LONG);
142 int chcheck(LONG);
143 int AddFixup(WORD, LONG, TOKEN *);
144 int ResolveAllFixups(void);
145
146 #endif // __SECT_H__