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