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