2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // RMAC.H - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011-2022 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
16 #include <sys/types.h>
20 // TARGET SPECIFIC BUILD SETTINGS
22 #if defined(WIN32) || defined(WIN64)
25 #include "dirent_lose.h"
26 // Release platform - windows
27 #define PLATFORM "Win32"
28 #define _OPEN_FLAGS _O_TRUNC|_O_CREAT|_O_BINARY|_O_RDWR
29 #define _OPEN_INC _O_RDONLY|_O_BINARY
30 #define _PERM_MODE _S_IREAD|_S_IWRITE
32 #define realpath(_fn, _abs) _fullpath((_abs), (_fn), _MAX_PATH)
36 #pragma warning(disable:4996)
39 // Makes warnings double clickable on visual studio (ggn)
40 #define STRINGIZE_HELPER(x) #x
41 #define STRINGIZE(x) STRINGIZE_HELPER(x)
42 #define WARNING(desc) __pragma(message(__FILE__ "(" STRINGIZE(__LINE__) ") : Warning: " #desc))
43 #define inline __inline
45 // WARNING(FIXME: Code removed because...)
48 // If we're not compiling for Visual Studio let's assume that we're using
49 // some flavour of gcc instead. So let's use the gcc compliant macro
50 // instead. If some weirdo uses something else (I dunno, Intel compiler or
51 // something?) this is probably going to explode spectacularly. Let's wait
53 #define DO_PRAGMA(x) _Pragma (#x)
54 #define WARNING(desc) DO_PRAGMA(message (#desc))
58 // Ever since Visual Studio... 2017? 2019? the following constants come
59 // defined in the platform SDK, which leads to endless warnings from the
60 // compiler. So let's just put the pacifier on and undef them, sheesh! (No,
61 // we won't rename the defines, we've been here since 1986, Visual Studio
62 // wasn't even a glimpse in the milkman's eyes, if you catch my drift)
73 #include <sys/fcntl.h>
75 // Release platform - Linux or mac OS-X
76 #define PLATFORM "Linux/OSX"
77 #define _OPEN_FLAGS O_TRUNC|O_CREAT|O_RDWR
78 #define _OPEN_INC O_RDONLY
79 #define _PERM_MODE S_IRUSR|S_IWUSR
80 #define PATH_SEPS ";:"
87 #define _OPEN_FLAGS _O_TRUNC|_O_CREAT|_O_BINARY|_O_RDWR
88 #define _OPEN_INC O_RDONLY|_O_BINARY
91 // WARNING WARNING WARNING
92 #define DO_PRAGMA(x) _Pragma (#x)
93 #define WARNING(desc) DO_PRAGMA(message (#desc))
97 // Release platform - not specified
98 #include <sys/fcntl.h>
99 #define PLATFORM "Unknown"
100 #define _OPEN_FLAGS O_TRUNC|O_CREAT|O_RDWR
101 #define _OPEN_INC O_RDONLY
102 #define _PERM_MODE S_IREAD|S_IWRITE
103 #define PATH_SEPS ":;"
104 // Defined here, even though the platform may not support it...
105 #define DO_PRAGMA(x) _Pragma (#x)
106 #define WARNING(desc) DO_PRAGMA(message (#desc))
113 // Endian related, for safe handling of endian-sensitive data
114 // USAGE: GETBExx() is *always* an rvalue, a = pointer to a uint8_t,
115 // r = offset from 0. SETBExx(), v = value to write into 'a'
117 #define GETBE16(a, r) \
118 (((uint16_t)(a)[(r + 0)] << 8) | ((uint16_t)(a)[(r + 1)]))
120 #define GETBE32(a, r) \
121 (((uint32_t)(a)[(r + 0)] << 24) | ((uint32_t)(a)[(r + 1)] << 16) \
122 | ((uint32_t)(a)[(r + 2)] << 8) | ((uint32_t)(a)[(r + 3)]))
124 #define GETBE64(a, r) \
125 (((uint64_t)(a)[(r + 0)] << 56) | ((uint64_t)(a)[(r + 1)] << 48) \
126 | ((uint64_t)(a)[(r + 2)] << 40) | ((uint64_t)(a)[(r + 3)] << 32) \
127 | ((uint64_t)(a)[(r + 4)] << 24) | ((uint64_t)(a)[(r + 5)] << 16) \
128 | ((uint64_t)(a)[(r + 6)] << 8) | ((uint64_t)(a)[(r + 7)]))
130 #define SETBE16(a, r, v) \
131 { (a)[(r + 0)] = (uint8_t)((v) >> 8); \
132 (a)[(r + 1)] = (uint8_t)((v) & 0xFF); }
134 #define SETBE32(a, r, v) \
135 { (a)[(r + 0)] = (uint8_t)((v) >> 24); \
136 (a)[(r + 1)] = (uint8_t)(((v) >> 16) & 0xFF); \
137 (a)[(r + 2)] = (uint8_t)(((v) >> 8) & 0xFF); \
138 (a)[(r + 3)] = (uint8_t)((v) & 0xFF); }
140 #define SETBE64(a, r, v) \
141 { (a)[(r + 0)] = (uint8_t)((v) >> 56); \
142 (a)[(r + 1)] = (uint8_t)(((v) >> 48) & 0xFF); \
143 (a)[(r + 2)] = (uint8_t)(((v) >> 40) & 0xFF); \
144 (a)[(r + 3)] = (uint8_t)(((v) >> 32) & 0xFF); \
145 (a)[(r + 4)] = (uint8_t)(((v) >> 24) & 0xFF); \
146 (a)[(r + 5)] = (uint8_t)(((v) >> 16) & 0xFF); \
147 (a)[(r + 6)] = (uint8_t)(((v) >> 8) & 0xFF); \
148 (a)[(r + 7)] = (uint8_t)((v) & 0xFF); }
150 // In 6502 mode, turns out we need this:
151 #define SETLE16(a, r, v) \
152 { (a)[(r + 0)] = (uint8_t)((v) & 0xFF); \
153 (a)[(r + 1)] = (uint8_t)((v) >> 8); }
155 // In DSP56001 mode, this is useful:
156 #define SETBE24(a, v) \
157 { (a)[0] = (uint8_t)(((v) >> 16) & 0xFF); \
158 (a)[1] = (uint8_t)(((v) >> 8) & 0xFF); \
159 (a)[2] = (uint8_t)((v) & 0xFF); }
162 #define BYTESWAP16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
163 #define BYTESWAP32(x) ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
164 #define BYTESWAP64(x) (BYTESWAP32(x >> 32) | (BYTESWAP32(x & 0xFFFFFFFF) << 32))
165 #define WORDSWAP32(x) ((((x) & 0x0000FFFF) << 16) | (((x) & 0xFFFF0000) >> 16))
168 // Non-target specific stuff
170 #include <inttypes.h>
174 #define WORD uint16_t
175 #define LONG uint32_t
178 #define ERROR (-1) // Generic error return
179 #define EOS '\0' // End of string
180 #define SPACE ' ' // ASCII space
181 #define SLASHCHAR '/'
182 #define SLASHSTRING "/"
183 #define FNSIZ 128 // Maximum size of a filename
184 #define OK 0 // OK return
185 #define DEBUG if (debug) // Debug conditional
186 #define MAXARGV 100 // Maximum number of commandline args
187 #define STDOUT 1 // Standard output
188 #define ERROUT 2 // Error output
191 // Object code formats
194 ALCYON, // Alcyon/DRI C object format
195 BSD, // BSD object format
196 ELF, // ELF object format
197 LOD, // DSP 56001 object format
198 P56, // DSP 56001 object format
199 XEX, // COM/EXE/XEX/whatever a8 object format
200 RAW, // Output at absolute address
201 C64PRG, // C64 .PRG format
205 #define TOKEN uint32_t
207 // Pointer type that can point to (almost) anything
208 #define PTR union _ptr
211 uint8_t * cp; // Char pointer
212 uint16_t * wp; // WORD pointer
213 uint32_t * lp; // LONG pointer
214 uint32_t * u32; // 32-bit pointer
215 uint64_t * u64; // 64-bit pointer
216 uint32_t lw; // LONG (for some reason)
217 SYM ** sy; // SYM pointer
218 TOKEN * tk; // TOKEN pointer
219 double * dp; // Double pointer
220 int64_t * i64; // 64-bit signed int pointer
224 #define LABEL 0 // User-defined symbol
225 #define MACRO 1 // Macro definition
226 #define MACARG 2 // Macro argument
227 #define DBGSYM 3 // stabs debug symbol
228 #define SY_UNDEF -1 // Undefined (lookup never matches it)
230 // Symbol and expression attributes
231 #define DEFINED 0x8000 // Symbol has been defined
232 #define GLOBAL 0x4000 // Symbol has been .GLOBL'd
233 #define COMMON 0x2000 // Symbol has been .COMM'd
234 #define REFERENCED 0x1000 // Symbol has been referenced
235 #define EQUATED 0x0800 // Symbol was equated
236 #define SDECLLIST 0x0400 // Symbol is on 'sdecl'-order list
237 #define FLOAT 0x0200 // Symbol is a floating point value
238 #define RISCREG 0x0100 // Symbol is a RISC register
240 // Expression spaces, ORed with symbol and expression attributes above
241 #define ABS 0x0000 // In absolute space
242 #define TEXT 0x0001 // Relative to text
243 #define DATA 0x0002 // Relative to data
244 #define BSS 0x0004 // Relative to BSS
245 //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 !!!
246 #define M6502 0x0008 // 6502/microprocessor (absolute)
247 #define M56001P 0x0010 // DSP 56001 Program RAM
248 #define M56001X 0x0020 // DSP 56001 X RAM
249 #define M56001Y 0x0040 // DSP 56001 Y RAM
250 #define M56001L 0x0080 // DSP 56001 L RAM
251 #define TDB (TEXT|DATA|BSS) // Mask for TEXT+DATA+BSS
252 #define M56KPXYL (M56001P|M56001X|M56001Y|M56001L) // Mask for 56K stuff
255 #define SIZB 0x0001 // .b
256 #define SIZW 0x0002 // .w
257 #define SIZL 0x0004 // .l
258 #define SIZN 0x0008 // no .(size) specifier
259 #define SIZD 0x0010 // .d (FPU double precision real)
260 #define SIZS 0x0020 // .s (FPU single precision real)
261 #define SIZX 0x0040 // .x (FPU extended precision real)
262 #define SIZP 0x0080 // .p (FPU pakced decimal real)
263 #define SIZQ 0x0100 // .q (quad word)
265 // Extended attributes
266 #define EQUATEDREG 0x0008 // Equated register symbol
267 #define UNDEF_EQUR 0x0010
268 #define EQUATEDCC 0x0020
269 #define UNDEF_CC 0x0040
271 // Optimisation defines
274 // These will be set to on/off when .opt "+Oall"/"~Oall" is called
280 OPT_020_DISP = 5, // 020+ base and outer displacements (bd, od) absolute long to short
286 OPT_56K_AUTO_LONG = 11,
287 OPT_COUNT, // Dummy, used to count number of optimisation switches
288 // These will be unaffected by "Oall"
289 OPT_PC_RELATIVE = 30, // Enforce PC relative
290 OPT_COUNT_ALL // Dummy, used to count all switches
293 // Exported variables
294 extern int verb_flag;
296 extern int rgpu, rdsp;
301 extern char * firstfname;
305 extern int list_flag;
306 extern int glob_flag;
307 extern int lsym_flag;
308 extern int dsym_flag;
309 extern int optim_warn_flag;
310 extern int obj_format;
311 extern int legacy_flag;
312 extern int prg_flag; // 1 = write ".PRG" relocatable executable
313 extern LONG PRGFLAGS;
314 extern int optim_flags[OPT_COUNT_ALL];
315 extern int activecpu;
316 extern int activefpu;
317 extern uint32_t org68k_address;
318 extern int org68k_active;
321 extern int *regcheck;
322 extern int *regaccept;
323 extern uint32_t used_architectures;
325 // Exported functions
326 void strtoupper(char * s);
327 char * fext(char *, char *, int);
328 int nthpath(char *, int, char *);
329 int ParseOptimization(char * optstring);