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
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;}
29 #define NSECTS 16 // Max. number of sections
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
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
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.
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
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
67 #define FU_MOVEI 0x0100
69 //#define FU_MJR 0x0300
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 #define FU_SUB32 0x1000
76 #define FU_ISBRA 0x2000 // Word forward fixup is a BRA or DBRA
77 #define FU_LBRA 0x4000 // Long branch, for short branch detect
78 #define FU_DONE 0x8000 // Fixup has been done
80 // Chunks are used to hold generated code and fixup records
81 #define CHUNK struct _chunk
83 CHUNK * chnext; // Next, previous chunks in section
85 LONG chloc; // Base addr of this chunk
86 LONG challoc; // # bytes allocated for chunk
87 LONG ch_size; // # bytes chunk actually uses
88 char * chptr; // Data for this chunk
92 #define SECT struct _sect
94 WORD scattr; // Section attributes
95 LONG sloc; // Current loc-in / size-of section
96 CHUNK * sfcode; // First chunk in section
97 CHUNK * scode; // Last chunk in section
98 CHUNK * sffix; // First fixup chunk
99 CHUNK * sfix; // Last fixup chunk
102 // A mark is of the form:
103 // .W <to+flags> section mark is relative to, and flags in upper byte
104 // .L <loc> location of mark in "from" section
105 // .W [from] new from section
106 // .L [symbol] symbol involved in external reference
107 #define MCHUNK struct _mchunk
109 MCHUNK * mcnext; // Next mark chunk
110 PTR mcptr; // Vector of marks
111 LONG mcalloc; // # marks allocted to mark block
112 LONG mcused; // # marks used in block
115 #define MWORD 0x0000 // Marked word
116 #define MLONG 0x0100 // Marked long
117 #define MMOVEI 0x0200
118 #define MCHFROM 0x8000 // Mark includes change-to-from
119 #define MSYMBOL 0x4000 // Mark includes symbol number
120 #define MCHEND 0x2000 // Indicates end of mark chunk
121 #define MPCREL 0x1000 // Mark is PC-relative
123 //#define MAXFWDJUMPS 1024 // Maximum forward jumps to check
124 //extern unsigned fwdjump[MAXFWDJUMPS];
125 //extern unsigned fwindex;
127 // Globals, external etc
135 extern CHUNK * scode;
138 void InitSection(void);
139 void switchsect(int);
141 int fixtest(int, LONG);
143 int fixup(WORD, LONG, TOKEN *);
144 int ResolveAllFixups(void);