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
16 #include <sys/types.h>
20 // TARGET SPECIFIC BUILD SETTINGS
22 #if defined(WIN32) || defined(WIN64)
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
34 #pragma warning(disable:4996)
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
44 // WARNING(FIXME: Code removed because...)
47 // If we're not compiling for Visual Studio let's assume that we're using
48 // some flavour of gcc instead. So let's use the gcc compliant macro
49 // instead. If some weirdo uses something else (I dunno, Intel compiler or
50 // something?) this is probably going to explode spectacularly. Let's wait
52 #define DO_PRAGMA(x) _Pragma (#x)
53 #define WARNING(desc) DO_PRAGMA(message (#desc))
61 #include <sys/fcntl.h>
63 // Release platform - Linux or mac OS-X
64 #define PLATFORM "Linux/OSX"
65 #define _OPEN_FLAGS O_TRUNC|O_CREAT|O_RDWR
66 #define _OPEN_INC O_RDONLY
67 #define _PERM_MODE S_IRUSR|S_IWUSR
68 #define PATH_SEPS ";:"
75 #define _OPEN_FLAGS _O_TRUNC|_O_CREAT|_O_BINARY|_O_RDWR
76 #define _OPEN_INC O_RDONLY|_O_BINARY
79 // WARNING WARNING WARNING
80 #define DO_PRAGMA(x) _Pragma (#x)
81 #define WARNING(desc) DO_PRAGMA(message (#desc))
85 // Release platform - not specified
86 #include <sys/fcntl.h>
87 #define PLATFORM "Unknown"
88 #define _OPEN_FLAGS O_TRUNC|O_CREAT|O_RDWR
89 #define _OPEN_INC O_RDONLY
90 #define _PERM_MODE S_IREAD|S_IWRITE
91 #define PATH_SEPS ":;"
92 // Defined here, even though the platform may not support it...
93 #define DO_PRAGMA(x) _Pragma (#x)
94 #define WARNING(desc) DO_PRAGMA(message (#desc))
101 // Endian related, for safe handling of endian-sensitive data
102 // USAGE: GETBExx() is *always* an rvalue, a = pointer to a uint8_t,
103 // r = offset from 0. SETBExx(), v = value to write into 'a'
105 #define GETBE16(a, r) \
106 (((uint16_t)(a)[(r + 0)] << 8) | ((uint16_t)(a)[(r + 1)]))
108 #define GETBE32(a, r) \
109 (((uint32_t)(a)[(r + 0)] << 24) | ((uint32_t)(a)[(r + 1)] << 16) \
110 | ((uint32_t)(a)[(r + 2)] << 8) | ((uint32_t)(a)[(r + 3)]))
112 #define GETBE64(a, r) \
113 (((uint64_t)(a)[(r + 0)] << 56) | ((uint64_t)(a)[(r + 1)] << 48) \
114 | ((uint64_t)(a)[(r + 2)] << 40) | ((uint64_t)(a)[(r + 3)] << 32) \
115 | ((uint64_t)(a)[(r + 4)] << 24) | ((uint64_t)(a)[(r + 5)] << 16) \
116 | ((uint64_t)(a)[(r + 6)] << 8) | ((uint64_t)(a)[(r + 7)]))
118 #define SETBE16(a, r, v) \
119 { (a)[(r + 0)] = (uint8_t)((v) >> 8); \
120 (a)[(r + 1)] = (uint8_t)((v) & 0xFF); }
122 #define SETBE32(a, r, v) \
123 { (a)[(r + 0)] = (uint8_t)((v) >> 24); \
124 (a)[(r + 1)] = (uint8_t)(((v) >> 16) & 0xFF); \
125 (a)[(r + 2)] = (uint8_t)(((v) >> 8) & 0xFF); \
126 (a)[(r + 3)] = (uint8_t)((v) & 0xFF); }
128 #define SETBE64(a, r, v) \
129 { (a)[(r + 0)] = (uint8_t)((v) >> 56); \
130 (a)[(r + 1)] = (uint8_t)(((v) >> 48) & 0xFF); \
131 (a)[(r + 2)] = (uint8_t)(((v) >> 40) & 0xFF); \
132 (a)[(r + 3)] = (uint8_t)(((v) >> 32) & 0xFF); \
133 (a)[(r + 4)] = (uint8_t)(((v) >> 24) & 0xFF); \
134 (a)[(r + 5)] = (uint8_t)(((v) >> 16) & 0xFF); \
135 (a)[(r + 6)] = (uint8_t)(((v) >> 8) & 0xFF); \
136 (a)[(r + 7)] = (uint8_t)((v) & 0xFF); }
138 // In 6502 mode, turns out we need this:
139 #define SETLE16(a, r, v) \
140 { (a)[(r + 0)] = (uint8_t)((v) & 0xFF); \
141 (a)[(r + 1)] = (uint8_t)((v) >> 8); }
143 // In DSP56001 mode, this is useful:
144 #define SETBE24(a, v) \
145 { (a)[0] = (uint8_t)(((v) >> 16) & 0xFF); \
146 (a)[1] = (uint8_t)(((v) >> 8) & 0xFF); \
147 (a)[2] = (uint8_t)((v) & 0xFF); }
150 #define BYTESWAP16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
151 #define BYTESWAP32(x) ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
152 #define BYTESWAP64(x) (BYTESWAP32(x >> 32) | (BYTESWAP32(x & 0xFFFFFFFF) << 32))
153 #define WORDSWAP32(x) ((((x) & 0x0000FFFF) << 16) | (((x) & 0xFFFF0000) >> 16))
156 // Non-target specific stuff
158 #include <inttypes.h>
163 #define WORD uint16_t
164 #define LONG uint32_t
167 #define ERROR (-1) // Generic error return
168 #define EOS '\0' // End of string
169 #define SPACE ' ' // ASCII space
170 #define SLASHCHAR '/'
171 #define SLASHSTRING "/"
172 #define FNSIZ 128 // Maximum size of a filename
173 #define OK 0 // OK return
174 #define DEBUG if (debug) // Debug conditional
175 #define MAXARGV 100 // Maximum number of commandline args
176 #define STDOUT 1 // Standard output
177 #define ERROUT 2 // Error output
180 // Object code formats
183 ALCYON, // Alcyon/DRI C object format
184 BSD, // BSD object format
185 ELF, // ELF object format
186 LOD, // DSP 56001 object format
187 P56, // DSP 56001 object format
188 XEX, // COM/EXE/XEX/whatever a8 object format
189 RAW, // Output at absolute address
193 #define TOKEN uint32_t
195 // Pointer type that can point to (almost) anything
196 #define PTR union _ptr
199 uint8_t * cp; // Char pointer
200 uint16_t * wp; // WORD pointer
201 uint32_t * lp; // LONG pointer
202 uint32_t * u32; // 32-bit pointer
203 uint64_t * u64; // 64-bit pointer
204 uint32_t lw; // LONG (for some reason)
205 SYM ** sy; // SYM pointer
206 TOKEN * tk; // TOKEN pointer
207 double * dp; // Double pointer
208 int64_t * i64; // 64-bit signed int pointer
212 #define LABEL 0 // User-defined symbol
213 #define MACRO 1 // Macro definition
214 #define MACARG 2 // Macro argument
215 #define SY_UNDEF -1 // Undefined (lookup never matches it)
217 // Symbol and expression attributes
218 #define DEFINED 0x8000 // Symbol has been defined
219 #define GLOBAL 0x4000 // Symbol has been .GLOBL'd
220 #define COMMON 0x2000 // Symbol has been .COMM'd
221 #define REFERENCED 0x1000 // Symbol has been referenced
222 #define EQUATED 0x0800 // Symbol was equated
223 #define SDECLLIST 0x0400 // Symbol is on 'sdecl'-order list
224 #define FLOAT 0x0200 // Symbol is a floating point value
225 #define RISCREG 0x0100 // Symbol is a RISC register
227 // Expression spaces, ORed with symbol and expression attributes above
228 #define ABS 0x0000 // In absolute space
229 #define TEXT 0x0001 // Relative to text
230 #define DATA 0x0002 // Relative to data
231 #define BSS 0x0004 // Relative to BSS
232 //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 !!!
233 #define M6502 0x0008 // 6502/microprocessor (absolute)
234 #define M56001P 0x0010 // DSP 56001 Program RAM
235 #define M56001X 0x0020 // DSP 56001 X RAM
236 #define M56001Y 0x0040 // DSP 56001 Y RAM
237 #define M56001L 0x0080 // DSP 56001 L RAM
238 #define TDB (TEXT|DATA|BSS) // Mask for TEXT+DATA+BSS
239 #define M56KPXYL (M56001P|M56001X|M56001Y|M56001L) // Mask for 56K stuff
242 #define SIZB 0x0001 // .b
243 #define SIZW 0x0002 // .w
244 #define SIZL 0x0004 // .l
245 #define SIZN 0x0008 // no .(size) specifier
246 #define SIZD 0x0010 // .d (FPU double precision real)
247 #define SIZS 0x0020 // .s (FPU single precision real)
248 #define SIZX 0x0040 // .x (FPU extended precision real)
249 #define SIZP 0x0080 // .p (FPU pakced decimal real)
250 #define SIZQ 0x0100 // .q (quad word)
252 // RISC register bank definitions (used in extended symbol attributes also)
253 #define BANK_N 0x0000 // No register bank specified
254 #define BANK_0 0x0001 // Register bank zero specified
255 #define BANK_1 0x0002 // Register bank one specified
256 #define EQUATEDREG 0x0008 // Equated register symbol
257 #define UNDEF_EQUR 0x0010
258 #define EQUATEDCC 0x0020
259 #define UNDEF_CC 0x0040
261 // Construct binary constants at compile time
265 #define HEX__(n) 0x##n##LU
267 ((x&0x0000000FLU)?1:0) \
268 +((x&0x000000F0LU)?2:0) \
269 +((x&0x00000F00LU)?4:0) \
270 +((x&0x0000F000LU)?8:0) \
271 +((x&0x000F0000LU)?16:0) \
272 +((x&0x00F00000LU)?32:0) \
273 +((x&0x0F000000LU)?64:0) \
274 +((x&0xF0000000LU)?128:0)
277 #define B8(d) ((uint8_t)B8__(HEX__(d)))
278 #define B16(dmsb,dlsb) (((uint16_t)B8(dmsb)<<8) + B8(dlsb))
279 #define B32(dmsb,db2,db3,dlsb) (((uint32_t)B8(dmsb)<<24) \
280 + ((uint32_t)B8(db2)<<16) \
281 + ((uint32_t)B8(db3)<<8) \
284 // Optimisation defines
287 // These will be set to on/off when .opt "+Oall"/"~Oall" is called
293 OPT_020_DISP = 5, // 020+ base and outer displacements (bd, od) absolute long to short
298 OPT_COUNT, // Dummy, used to count number of optimisation switches
299 // These will be unaffected by "Oall"
300 OPT_PC_RELATIVE = 11, // Enforce PC relative
301 OPT_COUNT_ALL // Dummy, used to count all switches
304 // Exported variables
305 extern int verb_flag;
307 extern int rgpu, rdsp;
313 extern char * firstfname;
317 extern int list_flag;
318 extern int glob_flag;
319 extern int lsym_flag;
320 extern int optim_warn_flag;
321 extern int obj_format;
322 extern int legacy_flag;
323 extern int prg_flag; // 1 = write ".PRG" relocatable executable
324 extern LONG PRGFLAGS;
325 extern int optim_flags[OPT_COUNT_ALL];
326 extern int activecpu;
327 extern int activefpu;
328 extern uint32_t org68k_address;
329 extern int org68k_active;
331 // Exported functions
332 void strtoupper(char * s);
333 char * fext(char *, char *, int);
334 int nthpath(char *, int, char *);
335 int ParseOptimization(char * optstring);