2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // RMAC.H - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2017 Reboot & 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
32 #pragma warning(disable:4996)
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
42 // WARNING(FIXME: Code removed because...)
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
50 #define DO_PRAGMA(x) _Pragma (#x)
51 #define WARNING(desc) DO_PRAGMA(message (#desc))
59 #include <sys/fcntl.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
72 #define _OPEN_FLAGS _O_TRUNC|_O_CREAT|_O_BINARY|_O_RDWR
73 #define _OPEN_INC O_RDONLY|_O_BINARY
76 // WARNING WARNING WARNING
77 #define DO_PRAGMA(x) _Pragma (#x)
78 #define WARNING(desc) DO_PRAGMA(message (#desc))
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))
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'
97 #define GETBE16(a, r) \
98 (((uint16_t)(a)[(r + 0)] << 8) | ((uint16_t)(a)[(r + 1)]))
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)]))
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)]))
110 #define SETBE16(a, r, v) \
111 { (a)[(r + 0)] = (uint8_t)((v) >> 8); \
112 (a)[(r + 1)] = (uint8_t)((v) & 0xFF); }
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); }
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); }
131 #define BYTESWAP16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
132 #define BYTESWAP32(x) ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
133 #define WORDSWAP32(x) ((((x) & 0x0000FFFF) << 16) | (((x) & 0xFFFF0000) >> 16))
136 // Non-target specific stuff
138 #include <inttypes.h>
142 #define WORD uint16_t
143 #define LONG uint32_t
146 #define ERROR (-1) // Generic error return
147 #define EOS '\0' // End of string
148 #define SPACE ' ' // ASCII space
149 #define SLASHCHAR '/'
150 #define SLASHSTRING "/"
151 #define VALUE LONG // Assembler value
152 #define TOKEN LONG // Assembler token
153 #define FNSIZ 128 // Maximum size of a filename
154 #define OK 0 // OK return
155 #define DEBUG if (debug) // Debug conditional
156 #define MAXARGV 100 // Maximum number of commandline args
157 #define STDOUT 1 // Standard output
158 #define ERROUT 2 // Error output
161 // (Normally) non-printable tokens
162 #define COLON ':' // : (grumble: GNUmacs hates ':')
163 #define CONST 'a' // CONST <value>
164 #define ACONST 'A' // ACONST <value> <attrib>
165 #define STRING 'b' // STRING <address>
166 #define SYMBOL 'c' // SYMBOL <address>
167 #define EOL 'e' // End of line
168 #define TKEOF 'f' // End of file (or macro)
169 #define DEQUALS 'g' // ==
170 #define SET 149 // set
171 #define REG 'R' // reg
172 #define EQUREG 148 // equreg
173 #define CCDEF 183 // ccdef
174 #define DCOLON 'h' // ::
177 #define NE 'k' // <> or !=
178 #define SHR 'l' // >>
179 #define SHL 'm' // <<
180 #define UNMINUS 'n' // Unary '-'
181 #define DOTB 'B' // .b or .B or .s or .S
182 #define DOTW 'W' // .w or .W
183 #define DOTL 'L' // .l or .L
184 #define DOTI 'I' // .i or .I
185 #define ENDEXPR 'E' // End of expression
187 // Object code formats
188 #define ALCYON 0 // Alcyon/DRI C object format
189 #define MWC 1 // Mark Williams object format
190 #define BSD 2 // BSD object format
191 #define ELF 3 // ELF object format
193 // Pointer type that can point to (almost) anything
194 #define PTR union _ptr
197 uint8_t * cp; // Char
198 uint16_t * wp; // WORD
199 uint32_t * lp; // LONG
206 #define LABEL 0 // User-defined symbol
207 #define MACRO 1 // Macro definition
208 #define MACARG 2 // Macro argument
209 #define SY_UNDEF -1 // Undefined (lookup never matches it)
211 // Symbol and expression attributes
212 #define DEFINED 0x8000 // Symbol has been defined
213 #define GLOBAL 0x4000 // Symbol has been .GLOBL'd
214 #define COMMON 0x2000 // Symbol has been .COMM'd
215 #define REFERENCED 0x1000 // Symbol has been referenced
216 #define EQUATED 0x0800 // Symbol was equated
217 #define SDECLLIST 0x0400 // Symbol is on 'sdecl'-order list
219 // Expression spaces, ORed with symbol and expression attributes above
220 #define ABS 0x0000 // In absolute space
221 #define TEXT 0x0001 // Relative to text
222 #define DATA 0x0002 // Relative to data
223 #define BSS 0x0004 // Relative to BSS
224 //#define M6502 0x0008 // 6502/microprocessor (absolute)
225 #define TDB (TEXT|DATA|BSS) // Mask for text+data+bss
228 #define SIZB 0x0001 // .b
229 #define SIZW 0x0002 // .w
230 #define SIZL 0x0004 // .l
231 #define SIZN 0x0008 // no .(size) specifier
233 // RISC register bank definitions (used in extended symbol attributes also)
234 #define BANK_N 0x0000 // No register bank specified
235 #define BANK_0 0x0001 // Register bank zero specified
236 #define BANK_1 0x0002 // Register bank one specified
237 #define EQUATEDREG 0x0008 // Equated register symbol
238 #define UNDEF_EQUR 0x0010
239 #define EQUATEDCC 0x0020
240 #define UNDEF_CC 0x0040
242 //#define RISCSYM 0x00010000
244 // Optimisation defines
250 OPT_INDIRECT_DISP = 3,
251 OPT_COUNT // Dummy, used to count number of optimisation switches
254 // Globals, externals, etc.
255 extern int verb_flag;
257 extern int rgpu, rdsp;
261 extern char * firstfname;
263 extern int as68_flag;
264 extern int list_flag;
265 extern int glob_flag;
266 extern int lsym_flag;
267 extern int sbra_flag;
268 extern int obj_format;
269 extern int legacy_flag;
270 extern int prg_flag; // 1 = write ".PRG" relocatable executable
271 extern LONG PRGFLAGS;
272 extern int optim_flags[OPT_COUNT];
274 // Exported functions
275 char * fext(char *, char *, int);
276 int nthpath(char *, int, char *);
277 int ParseOptimization(char * optstring);