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))
63 #include <sys/fcntl.h>
65 // Release platform - mac OS-X or Linux
66 #define PLATFORM "OSX/Linux"
67 #define _OPEN_FLAGS O_TRUNC|O_CREAT|O_RDWR
68 #define _OPEN_INC O_RDONLY
69 #define _PERM_MODE S_IRUSR|S_IWUSR
70 // WARNING WARNING WARNING
71 #define DO_PRAGMA(x) _Pragma (#x)
72 #define WARNING(desc) DO_PRAGMA(message (#desc))
74 // Release platform - not specified
75 #include <sys/fcntl.h>
76 #define PLATFORM "Unknown"
77 #define _OPEN_FLAGS O_TRUNC|O_CREAT|O_RDWR
78 #define _OPEN_INC O_RDONLY
79 #define _PERM_MODE S_IREAD|S_IWRITE
80 // Defined here, even though the platform may not support it...
81 #define DO_PRAGMA(x) _Pragma (#x)
82 #define WARNING(desc) DO_PRAGMA(message (#desc))
87 // Endian related, for safe handling of endian-sensitive data
88 // USAGE: GETBExx() is *always* an rvalue, a = pointer to a uint8_t,
89 // r = offset from 0. SETBExx(), v = value to write into 'a'
91 #define GETBE16(a, r) \
92 (((uint16_t)(a)[(r + 0)] << 8) | ((uint16_t)(a)[(r + 1)]))
94 #define GETBE32(a, r) \
95 (((uint32_t)(a)[(r + 0)] << 24) | ((uint32_t)(a)[(r + 1)] << 16) \
96 | ((uint32_t)(a)[(r + 2)] << 8) | ((uint32_t)(a)[(r + 3)]))
98 #define GETBE64(a, r) \
99 (((uint64_t)(a)[(r + 0)] << 56) | ((uint64_t)(a)[(r + 1)] << 48) \
100 | ((uint64_t)(a)[(r + 2)] << 40) | ((uint64_t)(a)[(r + 3)] << 32) \
101 | ((uint64_t)(a)[(r + 4)] << 24) | ((uint64_t)(a)[(r + 5)] << 16) \
102 | ((uint64_t)(a)[(r + 6)] << 8) | ((uint64_t)(a)[(r + 7)]))
104 #define SETBE16(a, r, v) \
105 { (a)[(r + 0)] = (uint8_t)((v) >> 8); \
106 (a)[(r + 1)] = (uint8_t)((v) & 0xFF); }
108 #define SETBE32(a, r, v) \
109 { (a)[(r + 0)] = (uint8_t)((v) >> 24); \
110 (a)[(r + 1)] = (uint8_t)(((v) >> 16) & 0xFF); \
111 (a)[(r + 2)] = (uint8_t)(((v) >> 8) & 0xFF); \
112 (a)[(r + 3)] = (uint8_t)((v) & 0xFF); }
114 #define SETBE64(a, r, v) \
115 { (a)[(r + 0)] = (uint8_t)((v) >> 56); \
116 (a)[(r + 1)] = (uint8_t)(((v) >> 48) & 0xFF); \
117 (a)[(r + 2)] = (uint8_t)(((v) >> 40) & 0xFF); \
118 (a)[(r + 3)] = (uint8_t)(((v) >> 32) & 0xFF); \
119 (a)[(r + 4)] = (uint8_t)(((v) >> 24) & 0xFF); \
120 (a)[(r + 5)] = (uint8_t)(((v) >> 16) & 0xFF); \
121 (a)[(r + 6)] = (uint8_t)(((v) >> 8) & 0xFF); \
122 (a)[(r + 7)] = (uint8_t)((v) & 0xFF); }
125 #define BYTESWAP16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
126 #define BYTESWAP32(x) ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
127 #define WORDSWAP32(x) ((((x) & 0x0000FFFF) << 16) | (((x) & 0xFFFF0000) >> 16))
130 // Non-target specific stuff
132 #include <inttypes.h>
136 #define WORD uint16_t
137 #define LONG uint32_t
140 #define ERROR (-1) // Generic error return
141 #define EOS '\0' // End of string
142 #define SPACE ' ' // ASCII space
143 #define SLASHCHAR '/'
144 #define SLASHSTRING "/"
145 #define VALUE LONG // Assembler value
146 #define TOKEN LONG // Assembler token
147 #define FNSIZ 128 // Maximum size of a filename
148 #define OK 0 // OK return
149 #define DEBUG if (debug) // Debug conditional
150 #define MAXARGV 100 // Maximum number of commandline args
151 #define STDOUT 1 // Standard output
152 #define ERROUT 2 // Error output
155 // (Normally) non-printable tokens
156 #define COLON ':' // : (grumble: GNUmacs hates ':')
157 #define CONST 'a' // CONST <value>
158 #define ACONST 'A' // ACONST <value> <attrib>
159 #define STRING 'b' // STRING <address>
160 #define SYMBOL 'c' // SYMBOL <address>
161 #define EOL 'e' // End of line
162 #define TKEOF 'f' // End of file (or macro)
163 #define DEQUALS 'g' // ==
164 #define SET 149 // set
165 #define REG 'R' // reg
166 #define EQUREG 148 // equreg
167 #define CCDEF 183 // ccdef
168 #define DCOLON 'h' // ::
171 #define NE 'k' // <> or !=
172 #define SHR 'l' // >>
173 #define SHL 'm' // <<
174 #define UNMINUS 'n' // Unary '-'
175 #define DOTB 'B' // .b or .B or .s or .S
176 #define DOTW 'W' // .w or .W
177 #define DOTL 'L' // .l or .L
178 #define DOTI 'I' // .i or .I
179 #define ENDEXPR 'E' // End of expression
181 // Object code formats
182 #define ALCYON 0 // Alcyon/DRI C object format
183 #define MWC 1 // Mark Williams object format
184 #define BSD 2 // BSD object format
185 #define ELF 3 // ELF object format
187 // Pointer type that can point to (almost) anything
188 #define PTR union _ptr
191 uint8_t * cp; // Char
192 uint16_t * wp; // WORD
193 uint32_t * lp; // LONG
200 #define LABEL 0 // User-defined symbol
201 #define MACRO 1 // Macro definition
202 #define MACARG 2 // Macro argument
203 #define SY_UNDEF -1 // Undefined (lookup never matches it)
205 // Symbol and expression attributes
206 #define DEFINED 0x8000 // Symbol has been defined
207 #define GLOBAL 0x4000 // Symbol has been .GLOBL'd
208 #define COMMON 0x2000 // Symbol has been .COMM'd
209 #define REFERENCED 0x1000 // Symbol has been referenced
210 #define EQUATED 0x0800 // Symbol was equated
211 #define SDECLLIST 0x0400 // Symbol is on 'sdecl'-order list
213 // Expression spaces, ORed with symbol and expression attributes above
214 #define ABS 0x0000 // In absolute space
215 #define TEXT 0x0001 // Relative to text
216 #define DATA 0x0002 // Relative to data
217 #define BSS 0x0004 // Relative to BSS
218 //#define M6502 0x0008 // 6502/microprocessor (absolute)
219 #define TDB (TEXT|DATA|BSS) // Mask for text+data+bss
222 #define SIZB 0x0001 // .b
223 #define SIZW 0x0002 // .w
224 #define SIZL 0x0004 // .l
225 #define SIZN 0x0008 // no .(size) specifier
227 // RISC register bank definitions (used in extended symbol attributes also)
228 #define BANK_N 0x0000 // No register bank specified
229 #define BANK_0 0x0001 // Register bank zero specified
230 #define BANK_1 0x0002 // Register bank one specified
231 #define EQUATEDREG 0x0008 // Equated register symbol
232 #define UNDEF_EQUR 0x0010
233 #define EQUATEDCC 0x0020
234 #define UNDEF_CC 0x0040
236 //#define RISCSYM 0x00010000
238 // Optimisation defines
244 OPT_INDIRECT_DISP = 3,
245 OPT_COUNT // Dummy, used to count number of optimisation switches
248 // Globals, externals, etc.
249 extern int verb_flag;
251 extern int rgpu, rdsp;
255 extern char * firstfname;
257 extern int as68_flag;
258 extern int list_flag;
259 extern int glob_flag;
260 extern int lsym_flag;
261 extern int sbra_flag;
262 extern int obj_format;
263 extern int legacy_flag;
264 extern int prg_flag; // 1 = write ".PRG" relocatable executable
265 extern LONG PRGFLAGS;
266 extern int optim_flags[OPT_COUNT];
268 // Exported functions
269 char * fext(char *, char *, int);
270 int nthpath(char *, int, char *);
271 int ParseOptimization(char * optstring);