]> Shamusworld >> Repos - rmac/blob - rmac.h
Fixes for last commit; version is now 1.10.0.
[rmac] / rmac.h
1 //
2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // RMAC.H - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2017 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 __RMAC_H__
10 #define __RMAC_H__
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18
19 //
20 // TARGET SPECIFIC BUILD SETTINGS
21 //
22 #if defined(WIN32) || defined (WIN64)
23         #include <io.h>
24         #include <fcntl.h>
25         // Release platform - windows
26         #define PLATFORM        "Win32"
27         #define _OPEN_FLAGS     _O_TRUNC|_O_CREAT|_O_BINARY|_O_RDWR
28         #define _OPEN_INC       _O_RDONLY|_O_BINARY
29         #define _PERM_MODE      _S_IREAD|_S_IWRITE
30         #ifdef _MSC_VER
31                 #if _MSC_VER > 1000
32                         #pragma warning(disable:4996)
33                 #endif
34
35         // Makes warnings double clickable on visual studio (ggn)
36         #define STRINGIZE_HELPER(x) #x
37         #define STRINGIZE(x) STRINGIZE_HELPER(x)
38         #define WARNING(desc) __pragma(message(__FILE__ "(" STRINGIZE(__LINE__) ") : Warning: " #desc))
39         #define inline __inline
40
41         // usage:
42         // WARNING(FIXME: Code removed because...)
43
44         #else
45         // If we're not compiling for Visual Studio let's assume that we're using
46         // some flavour of gcc instead. So let's use the gcc compliant macro
47         // instead. If some weirdo uses something else (I dunno, Intel compiler or
48         // something?) this is probably going to explode spectacularly. Let's wait
49         // for the fireworks!
50         #define DO_PRAGMA(x) _Pragma (#x)
51         #define WARNING(desc) DO_PRAGMA(message (#desc))
52
53         #endif
54
55 #else
56
57         #ifdef __GCCUNIX__
58
59         #include <sys/fcntl.h>
60         #include <unistd.h>
61         // Release platform - mac OS-X or Linux
62         #define PLATFORM        "OSX/Linux"
63         #define _OPEN_FLAGS     O_TRUNC|O_CREAT|O_RDWR
64         #define _OPEN_INC       O_RDONLY
65         #define _PERM_MODE      S_IRUSR|S_IWUSR
66
67         #ifdef __MINGW32__
68         #define off64_t long
69         #define off_t long
70         #undef _OPEN_FLAGS
71         #undef _OPEN_INC
72         #define _OPEN_FLAGS     _O_TRUNC|_O_CREAT|_O_BINARY|_O_RDWR
73         #define _OPEN_INC       O_RDONLY|_O_BINARY
74         #endif
75
76         // WARNING WARNING WARNING
77         #define DO_PRAGMA(x) _Pragma (#x)
78         #define WARNING(desc) DO_PRAGMA(message (#desc))
79 #else
80         // Release platform - not specified
81         #include <sys/fcntl.h>
82         #define PLATFORM        "Unknown"
83         #define _OPEN_FLAGS     O_TRUNC|O_CREAT|O_RDWR
84         #define _OPEN_INC       O_RDONLY
85         #define _PERM_MODE      S_IREAD|S_IWRITE
86         // Defined here, even though the platform may not support it...
87         #define DO_PRAGMA(x) _Pragma (#x)
88         #define WARNING(desc) DO_PRAGMA(message (#desc))
89         #endif
90 #endif
91
92 //
93 // Endian related, for safe handling of endian-sensitive data
94 // USAGE: GETBExx() is *always* an rvalue, a = pointer to a uint8_t,
95 //        r = offset from 0. SETBExx(), v = value to write into 'a'
96 //
97 #define GETBE16(a, r) \
98         (((uint16_t)(a)[(r + 0)] << 8) | ((uint16_t)(a)[(r + 1)]))
99
100 #define GETBE32(a, r) \
101         (((uint32_t)(a)[(r + 0)] << 24) | ((uint32_t)(a)[(r + 1)] << 16) \
102         | ((uint32_t)(a)[(r + 2)] << 8) | ((uint32_t)(a)[(r + 3)]))
103
104 #define GETBE64(a, r) \
105         (((uint64_t)(a)[(r + 0)] << 56) | ((uint64_t)(a)[(r + 1)] << 48) \
106         | ((uint64_t)(a)[(r + 2)] << 40) | ((uint64_t)(a)[(r + 3)] << 32) \
107         | ((uint64_t)(a)[(r + 4)] << 24) | ((uint64_t)(a)[(r + 5)] << 16) \
108         | ((uint64_t)(a)[(r + 6)] << 8) | ((uint64_t)(a)[(r + 7)]))
109
110 #define SETBE16(a, r, v) \
111         { (a)[(r + 0)] = (uint8_t)((v) >> 8); \
112         (a)[(r + 1)] = (uint8_t)((v) & 0xFF); }
113
114 #define SETBE32(a, r, v) \
115         { (a)[(r + 0)] = (uint8_t)((v) >> 24); \
116         (a)[(r + 1)] = (uint8_t)(((v) >> 16) & 0xFF); \
117         (a)[(r + 2)] = (uint8_t)(((v) >> 8) & 0xFF); \
118         (a)[(r + 3)] = (uint8_t)((v) & 0xFF); }
119
120 #define SETBE64(a, r, v) \
121         { (a)[(r + 0)] = (uint8_t)((v) >> 56); \
122         (a)[(r + 1)] = (uint8_t)(((v) >> 48) & 0xFF); \
123         (a)[(r + 2)] = (uint8_t)(((v) >> 40) & 0xFF); \
124         (a)[(r + 3)] = (uint8_t)(((v) >> 32) & 0xFF); \
125         (a)[(r + 4)] = (uint8_t)(((v) >> 24) & 0xFF); \
126         (a)[(r + 5)] = (uint8_t)(((v) >> 16) & 0xFF); \
127         (a)[(r + 6)] = (uint8_t)(((v) >> 8) & 0xFF); \
128         (a)[(r + 7)] = (uint8_t)((v) & 0xFF); }
129
130 // In 6502 mode, turns out we need this:
131 #define SETLE16(a, r, v) \
132         { (a)[(r + 0)] = (uint8_t)((v) & 0xFF); \
133         (a)[(r + 1)] = (uint8_t)((v) >> 8); }
134
135 // Byteswap crap
136 #define BYTESWAP16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
137 #define BYTESWAP32(x) ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
138 #define BYTESWAP64(x) (BYTESWAP32(x>>32)|BYTESWAP32((x&0xffffffff)<<32))
139 #define WORDSWAP32(x) ((((x) & 0x0000FFFF) << 16) | (((x) & 0xFFFF0000) >> 16))
140
141 //
142 // Non-target specific stuff
143 //
144 #include <inttypes.h>
145 #include "symbol.h"
146
147 #define BYTE         uint8_t
148 #define WORD         uint16_t
149 #define LONG         uint32_t
150 #define VOID         void
151
152 #define ERROR        (-1)               // Generic error return
153 #define EOS          '\0'               // End of string
154 #define SPACE        ' '                // ASCII space
155 #define SLASHCHAR    '/'
156 #define SLASHSTRING  "/"
157 #define FNSIZ        128                // Maximum size of a filename
158 #define OK           0                  // OK return
159 #define DEBUG        if (debug) // Debug conditional
160 #define MAXARGV      100                // Maximum number of commandline args
161 #define STDOUT       1                  // Standard output
162 #define ERROUT       2                  // Error output
163 #define CREATMASK    0
164
165 // Object code formats
166 enum
167 {
168 ALCYON,                         // Alcyon/DRI C object format
169 MWC,                            // Mark Williams object format
170 BSD,                            // BSD object format
171 ELF,                            // ELF object format
172 LOD,                            // DSP 56001 object format
173 P56,                            // DSP 56001 object format
174 XEX,                            // COM/EXE/XEX/whatever a8 object format
175 };
176
177 // Assembler token
178 #define TOKEN   uint32_t
179
180 // Token pointer type is a union because we have 64-bit sized tokens now :-P
181 #define TOKENPTR union _tokenptr
182 TOKENPTR
183 {
184         uint32_t * u32;
185         uint64_t * u64;
186 };
187
188 // Pointer type that can point to (almost) anything
189 #define PTR union _ptr
190 PTR
191 {
192         uint8_t *  cp;                          // Char
193         uint16_t * wp;                          // WORD
194         uint32_t * lp;                          // LONG
195         uint32_t   lw;                          // LONG
196         SYM **     sy;                          // SYM
197         TOKENPTR   tk;                          // TOKEN
198 };
199
200 // Symbol spaces
201 #define LABEL        0                  // User-defined symbol
202 #define MACRO        1                  // Macro definition
203 #define MACARG       2                  // Macro argument
204 #define SY_UNDEF     -1                 // Undefined (lookup never matches it)
205
206 // Symbol and expression attributes
207 #define DEFINED      0x8000             // Symbol has been defined
208 #define GLOBAL       0x4000             // Symbol has been .GLOBL'd
209 #define COMMON       0x2000             // Symbol has been .COMM'd
210 #define REFERENCED   0x1000             // Symbol has been referenced
211 #define EQUATED      0x0800             // Symbol was equated
212 #define SDECLLIST    0x0400             // Symbol is on 'sdecl'-order list
213 #define FLOAT        0x0200             // Symbol is a floating point value
214
215 // Expression spaces, ORed with symbol and expression attributes above
216 #define ABS          0x0000             // In absolute space
217 #define TEXT         0x0001             // Relative to text
218 #define DATA         0x0002             // Relative to data
219 #define BSS          0x0004             // Relative to BSS
220 #define M6502        0x0008             // 6502/microprocessor (absolute)
221 #define TDB          (TEXT|DATA|BSS)    // Mask for text+data+bss
222
223 // Sizes
224 #define SIZB         0x0001             // .b
225 #define SIZW         0x0002             // .w
226 #define SIZL         0x0004             // .l
227 #define SIZN         0x0008             // no .(size) specifier
228 #define SIZD         0x0010             // .d (FPU double precision real)
229 #define SIZS         0x0020             // .s (FPU single precision real)
230 #define SIZX         0x0040             // .x (FPU extended precision real)
231 #define SIZP         0x0080             // .p (FPU pakced decimal real)
232 #define SIZQ         0x0100             // .q (quad word)
233
234 // RISC register bank definitions (used in extended symbol attributes also)
235 #define BANK_N       0x0000             // No register bank specified
236 #define BANK_0       0x0001             // Register bank zero specified
237 #define BANK_1       0x0002             // Register bank one specified
238 #define EQUATEDREG   0x0008             // Equated register symbol
239 #define UNDEF_EQUR   0x0010
240 #define EQUATEDCC    0x0020
241 #define UNDEF_CC     0x0040
242
243 // Construct binary constants at compile time
244 // Code by Tom Torfs
245
246 // Helper macros
247 #define HEX__(n) 0x##n##LU
248 #define B8__(x) \
249  ((x&0x0000000FLU)?1:0) \
250 +((x&0x000000F0LU)?2:0) \
251 +((x&0x00000F00LU)?4:0) \
252 +((x&0x0000F000LU)?8:0) \
253 +((x&0x000F0000LU)?16:0) \
254 +((x&0x00F00000LU)?32:0) \
255 +((x&0x0F000000LU)?64:0) \
256 +((x&0xF0000000LU)?128:0)
257
258 // User macros
259 #define B8(d) ((uint8_t)B8__(HEX__(d)))
260 #define B16(dmsb,dlsb) (((uint16_t)B8(dmsb)<<8) + B8(dlsb))
261 #define B32(dmsb,db2,db3,dlsb) (((uint32_t)B8(dmsb)<<24) \
262 + ((uint32_t)B8(db2)<<16) \
263 + ((uint32_t)B8(db3)<<8) \
264 + B8(dlsb))
265
266 // Optimisation defines
267 enum
268 {
269         OPT_ABS_SHORT     = 0,
270         OPT_MOVEL_MOVEQ   = 1,
271         OPT_BSR_BCC_S     = 2,
272         OPT_INDIRECT_DISP = 3,
273         OPT_LEA_ADDQ      = 4,
274         OPT_BASE_DISP     = 5,
275         OPT_COUNT   // Dummy, used to count number of optimisation switches
276 };
277
278 // Exported variables
279 extern int verb_flag;
280 extern int debug;
281 extern int rgpu, rdsp;
282 extern int dsp56001;
283 extern int err_flag;
284 extern int err_fd;
285 extern int regbank;
286 extern char * firstfname;
287 extern int list_fd;
288 extern int list_pag;
289 extern int as68_flag;
290 extern int m6502;
291 extern int list_flag;
292 extern int glob_flag;
293 extern int lsym_flag;
294 extern int sbra_flag;
295 extern int obj_format;
296 extern int legacy_flag;
297 extern int prg_flag;    // 1 = write ".PRG" relocatable executable
298 extern LONG PRGFLAGS;
299 extern int optim_flags[OPT_COUNT];
300 extern int activecpu;
301 extern int activefpu;
302
303 // Exported functions
304 char * fext(char *, char *, int);
305 int nthpath(char *, int, char *);
306 int ParseOptimization(char * optstring);
307
308 #endif // __RMAC_H__
309