]> Shamusworld >> Repos - rmac/blob - rmac.h
Fix for #159: Split register sets according to architecture into different tables...
[rmac] / rmac.h
1 //
2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // RMAC.H - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2021 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     #define PATH_SEPS       ";"
31
32         #ifdef _MSC_VER
33                 #if _MSC_VER > 1000
34                         #pragma warning(disable:4996)
35                 #endif
36
37         // Makes warnings double clickable on visual studio (ggn)
38         #define STRINGIZE_HELPER(x) #x
39         #define STRINGIZE(x) STRINGIZE_HELPER(x)
40         #define WARNING(desc) __pragma(message(__FILE__ "(" STRINGIZE(__LINE__) ") : Warning: " #desc))
41         #define inline __inline
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     #define PATH_SEPS       ";:"
68
69         #ifdef __MINGW32__
70         #define off64_t long
71         #define off_t long
72         #undef _OPEN_FLAGS
73         #undef _OPEN_INC
74         #define _OPEN_FLAGS     _O_TRUNC|_O_CREAT|_O_BINARY|_O_RDWR
75         #define _OPEN_INC       O_RDONLY|_O_BINARY
76         #endif
77
78         // WARNING WARNING WARNING
79         #define DO_PRAGMA(x) _Pragma (#x)
80         #define WARNING(desc) DO_PRAGMA(message (#desc))
81
82         #else
83
84         // Release platform - not specified
85         #include <sys/fcntl.h>
86         #define PLATFORM        "Unknown"
87         #define _OPEN_FLAGS     O_TRUNC|O_CREAT|O_RDWR
88         #define _OPEN_INC       O_RDONLY
89         #define _PERM_MODE      S_IREAD|S_IWRITE
90     #define PATH_SEPS       ":;"
91         // Defined here, even though the platform may not support it...
92         #define DO_PRAGMA(x) _Pragma (#x)
93         #define WARNING(desc) DO_PRAGMA(message (#desc))
94         #endif
95
96 #endif
97
98
99 //
100 // Endian related, for safe handling of endian-sensitive data
101 // USAGE: GETBExx() is *always* an rvalue, a = pointer to a uint8_t,
102 //        r = offset from 0. SETBExx(), v = value to write into 'a'
103 //
104 #define GETBE16(a, r) \
105         (((uint16_t)(a)[(r + 0)] << 8) | ((uint16_t)(a)[(r + 1)]))
106
107 #define GETBE32(a, r) \
108         (((uint32_t)(a)[(r + 0)] << 24) | ((uint32_t)(a)[(r + 1)] << 16) \
109         | ((uint32_t)(a)[(r + 2)] << 8) | ((uint32_t)(a)[(r + 3)]))
110
111 #define GETBE64(a, r) \
112         (((uint64_t)(a)[(r + 0)] << 56) | ((uint64_t)(a)[(r + 1)] << 48) \
113         | ((uint64_t)(a)[(r + 2)] << 40) | ((uint64_t)(a)[(r + 3)] << 32) \
114         | ((uint64_t)(a)[(r + 4)] << 24) | ((uint64_t)(a)[(r + 5)] << 16) \
115         | ((uint64_t)(a)[(r + 6)] << 8) | ((uint64_t)(a)[(r + 7)]))
116
117 #define SETBE16(a, r, v) \
118         { (a)[(r + 0)] = (uint8_t)((v) >> 8); \
119         (a)[(r + 1)] = (uint8_t)((v) & 0xFF); }
120
121 #define SETBE32(a, r, v) \
122         { (a)[(r + 0)] = (uint8_t)((v) >> 24); \
123         (a)[(r + 1)] = (uint8_t)(((v) >> 16) & 0xFF); \
124         (a)[(r + 2)] = (uint8_t)(((v) >> 8) & 0xFF); \
125         (a)[(r + 3)] = (uint8_t)((v) & 0xFF); }
126
127 #define SETBE64(a, r, v) \
128         { (a)[(r + 0)] = (uint8_t)((v) >> 56); \
129         (a)[(r + 1)] = (uint8_t)(((v) >> 48) & 0xFF); \
130         (a)[(r + 2)] = (uint8_t)(((v) >> 40) & 0xFF); \
131         (a)[(r + 3)] = (uint8_t)(((v) >> 32) & 0xFF); \
132         (a)[(r + 4)] = (uint8_t)(((v) >> 24) & 0xFF); \
133         (a)[(r + 5)] = (uint8_t)(((v) >> 16) & 0xFF); \
134         (a)[(r + 6)] = (uint8_t)(((v) >> 8) & 0xFF); \
135         (a)[(r + 7)] = (uint8_t)((v) & 0xFF); }
136
137 // In 6502 mode, turns out we need this:
138 #define SETLE16(a, r, v) \
139         { (a)[(r + 0)] = (uint8_t)((v) & 0xFF); \
140         (a)[(r + 1)] = (uint8_t)((v) >> 8); }
141
142 // In DSP56001 mode, this is useful:
143 #define SETBE24(a, v) \
144         { (a)[0] = (uint8_t)(((v) >> 16) & 0xFF); \
145         (a)[1] = (uint8_t)(((v) >> 8) & 0xFF); \
146         (a)[2] = (uint8_t)((v) & 0xFF); }
147
148 // Byteswap crap
149 #define BYTESWAP16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
150 #define BYTESWAP32(x) ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
151 #define BYTESWAP64(x) (BYTESWAP32(x >> 32) | (BYTESWAP32(x & 0xFFFFFFFF) << 32))
152 #define WORDSWAP32(x) ((((x) & 0x0000FFFF) << 16) | (((x) & 0xFFFF0000) >> 16))
153
154 //
155 // Non-target specific stuff
156 //
157 #include <inttypes.h>
158 #include <dirent.h>
159 #include "symbol.h"
160
161 #if defined(WIN32) || defined(WIN64)
162 // Ever since Visual Studio... 2017? 2019? the following constants come defined in the
163 // platform SDK, which leads to endless warnings from the compiler. So let's just
164 // put the pacifier on and undef them, sheesh! (No, we won't rename the defines,
165 // we've been here since 1986, Visual Studio wasn't even a glimpse in the milkman's eyes,
166 // if you catch my drift)
167 #undef CONST
168 #undef ERROR
169 #undef TEXT
170 #endif
171
172 #define BYTE         uint8_t
173 #define WORD         uint16_t
174 #define LONG         uint32_t
175 #define VOID         void
176
177 #define ERROR        (-1)               // Generic error return
178 #define EOS          '\0'               // End of string
179 #define SPACE        ' '                // ASCII space
180 #define SLASHCHAR    '/'
181 #define SLASHSTRING  "/"
182 #define FNSIZ        128                // Maximum size of a filename
183 #define OK           0                  // OK return
184 #define DEBUG        if (debug) // Debug conditional
185 #define MAXARGV      100                // Maximum number of commandline args
186 #define STDOUT       1                  // Standard output
187 #define ERROUT       2                  // Error output
188 #define CREATMASK    0
189
190 // Object code formats
191 enum
192 {
193 ALCYON,                         // Alcyon/DRI C object format
194 BSD,                            // BSD object format
195 ELF,                            // ELF object format
196 LOD,                            // DSP 56001 object format
197 P56,                            // DSP 56001 object format
198 XEX,                            // COM/EXE/XEX/whatever a8 object format
199 RAW,                            // Output at absolute address
200 };
201
202 // Assembler token
203 #define TOKEN   uint32_t
204
205 // Pointer type that can point to (almost) anything
206 #define PTR union _ptr
207 PTR
208 {
209         uint8_t *  cp;                          // Char pointer
210         uint16_t * wp;                          // WORD pointer
211         uint32_t * lp;                          // LONG pointer
212         uint32_t * u32;                         // 32-bit pointer
213         uint64_t * u64;                         // 64-bit pointer
214         uint32_t   lw;                          // LONG (for some reason)
215         SYM **     sy;                          // SYM pointer
216         TOKEN *    tk;                          // TOKEN pointer
217         double *   dp;                          // Double pointer
218         int64_t *  i64;                         // 64-bit signed int pointer
219 };
220
221 // Symbol spaces
222 #define LABEL        0                  // User-defined symbol
223 #define MACRO        1                  // Macro definition
224 #define MACARG       2                  // Macro argument
225 #define SY_UNDEF     -1                 // Undefined (lookup never matches it)
226
227 // Symbol and expression attributes
228 #define DEFINED      0x8000             // Symbol has been defined
229 #define GLOBAL       0x4000             // Symbol has been .GLOBL'd
230 #define COMMON       0x2000             // Symbol has been .COMM'd
231 #define REFERENCED   0x1000             // Symbol has been referenced
232 #define EQUATED      0x0800             // Symbol was equated
233 #define SDECLLIST    0x0400             // Symbol is on 'sdecl'-order list
234 #define FLOAT        0x0200             // Symbol is a floating point value
235 #define RISCREG      0x0100             // Symbol is a RISC register
236
237 // Expression spaces, ORed with symbol and expression attributes above
238 #define ABS          0x0000             // In absolute space
239 #define TEXT         0x0001             // Relative to text
240 #define DATA         0x0002             // Relative to data
241 #define BSS          0x0004             // Relative to BSS
242 //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 !!!
243 #define M6502        0x0008             // 6502/microprocessor (absolute)
244 #define M56001P      0x0010             // DSP 56001 Program RAM
245 #define M56001X      0x0020             // DSP 56001 X RAM
246 #define M56001Y      0x0040             // DSP 56001 Y RAM
247 #define M56001L      0x0080             // DSP 56001 L RAM
248 #define TDB          (TEXT|DATA|BSS)    // Mask for TEXT+DATA+BSS
249 #define M56KPXYL     (M56001P|M56001X|M56001Y|M56001L)  // Mask for 56K stuff
250
251 // Sizes
252 #define SIZB         0x0001             // .b
253 #define SIZW         0x0002             // .w
254 #define SIZL         0x0004             // .l
255 #define SIZN         0x0008             // no .(size) specifier
256 #define SIZD         0x0010             // .d (FPU double precision real)
257 #define SIZS         0x0020             // .s (FPU single precision real)
258 #define SIZX         0x0040             // .x (FPU extended precision real)
259 #define SIZP         0x0080             // .p (FPU pakced decimal real)
260 #define SIZQ         0x0100             // .q (quad word)
261
262 #define EQUATEDREG   0x0008             // Equated register symbol
263 #define UNDEF_EQUR   0x0010
264 #define EQUATEDCC    0x0020
265 #define UNDEF_CC     0x0040
266
267 // Optimisation defines
268 enum
269 {
270     // These will be set to on/off when .opt "+Oall"/"~Oall" is called
271         OPT_ABS_SHORT     = 0,
272         OPT_MOVEL_MOVEQ   = 1,
273         OPT_BSR_BCC_S     = 2,
274         OPT_OUTER_DISP    = 3,
275         OPT_LEA_ADDQ      = 4,
276         OPT_020_DISP      = 5,          // 020+ base and outer displacements (bd, od) absolute long to short
277         OPT_NULL_BRA      = 6,
278         OPT_CLR_DX        = 7,
279         OPT_ADDA_ADDQ     = 8,
280         OPT_ADDA_LEA      = 9,
281         OPT_56K_SHORT     = 10,
282         OPT_56K_AUTO_LONG = 11,
283         OPT_COUNT,                  // Dummy, used to count number of optimisation switches
284     // These will be unaffected by "Oall"
285         OPT_PC_RELATIVE   = 30,         // Enforce PC relative
286     OPT_COUNT_ALL               // Dummy, used to count all switches
287 };
288
289 // Exported variables
290 extern int verb_flag;
291 extern int debug;
292 extern int rgpu, rdsp;
293 extern int robjproc;
294 extern int dsp56001;
295 extern int err_flag;
296 extern int err_fd;
297 extern char * firstfname;
298 extern int list_fd;
299 extern int list_pag;
300 extern int m6502;
301 extern int list_flag;
302 extern int glob_flag;
303 extern int lsym_flag;
304 extern int optim_warn_flag;
305 extern int obj_format;
306 extern int legacy_flag;
307 extern int prg_flag;    // 1 = write ".PRG" relocatable executable
308 extern LONG PRGFLAGS;
309 extern int optim_flags[OPT_COUNT_ALL];
310 extern int activecpu;
311 extern int activefpu;
312 extern uint32_t org68k_address;
313 extern int org68k_active;
314 extern int *regbase;
315 extern int *regtab;
316 extern int *regcheck;
317 extern int *regaccept;
318
319 // Exported functions
320 void strtoupper(char * s);
321 char * fext(char *, char *, int);
322 int nthpath(char *, int, char *);
323 int ParseOptimization(char * optstring);
324
325 #endif // __RMAC_H__
326