33eb1a262227b19b95509dfa3319a983e3cb76cc
1 /* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-2000 NEC Research Institute.
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
26 #ifndef DEBUG_CCODEGEN
27 #define DEBUG_CCODEGEN FALSE
30 #define GCState ((Pointer)&gcState)
31 #define ExnStack *(size_t*)(GCState + ExnStackOffset)
32 #define FrontierMem *(Pointer*)(GCState + FrontierOffset)
33 #define Frontier frontier
34 #define StackBottom *(Pointer*)(GCState + StackBottomOffset)
35 #define StackTopMem *(Pointer*)(GCState + StackTopOffset)
36 #define StackTop stackTop
38 /* ------------------------------------------------- */
40 /* ------------------------------------------------- */
42 #define C(ty, x) (*(ty*)(x))
43 #define G(ty, i) (global##ty [i])
44 #define GPNR(i) G(ObjptrNonRoot, i)
45 #define O(ty, b, o) (*(ty*)((b) + (o)))
46 #define X(ty, b, i, s, o) (*(ty*)((b) + ((i) * (s)) + (o)))
47 #define S(ty, i) *(ty*)(StackTop + (i))
49 /* ------------------------------------------------- */
51 /* ------------------------------------------------- */
53 #define IsInt(p) (0x3 & (int)(p))
58 fprintf (stderr, "%s:%d: BZ(%d, %s)\n", \
59 __FILE__, __LINE__, (x), #l); \
60 if (0 == (x)) goto l; \
66 fprintf (stderr, "%s:%d: BNZ(%d, %s)\n", \
67 __FILE__, __LINE__, (x), #l); \
71 #define FlushFrontier() \
73 FrontierMem = Frontier; \
76 #define FlushStackTop() \
78 StackTopMem = StackTop; \
81 #define CacheFrontier() \
83 Frontier = FrontierMem; \
86 #define CacheStackTop() \
88 StackTop = StackTopMem; \
91 /* ------------------------------------------------- */
93 /* ------------------------------------------------- */
95 #if (defined (__sun__) && defined (REGISTER_FRONTIER_STACKTOP))
99 register unsigned int frontier asm("g5"); \
100 uintptr_t l_nextFun = nextFun; \
101 register unsigned int stackTop asm("g6");
107 uintptr_t l_nextFun = nextFun; \
111 #define ChunkSwitch(n) \
112 if (DEBUG_CCODEGEN) \
113 fprintf (stderr, "%s:%d: entering chunk %d l_nextFun = %d\n", \
114 __FILE__, __LINE__, n, (int)l_nextFun); \
123 /* interchunk return */ \
124 nextFun = l_nextFun; \
125 cont.nextChunk = (void*)nextChunks[nextFun]; \
130 } /* end switch (l_nextFun) */ \
131 } /* end while (1) */ \
134 /* ------------------------------------------------- */
135 /* Calling SML from C */
136 /* ------------------------------------------------- */
138 #define Thread_returnToC() \
140 if (DEBUG_CCODEGEN) \
141 fprintf (stderr, "%s:%d: Thread_returnToC()\n", \
142 __FILE__, __LINE__); \
147 /* ------------------------------------------------- */
149 /* ------------------------------------------------- */
151 #define FarJump(n, l) \
157 /* ------------------------------------------------- */
159 /* ------------------------------------------------- */
161 #define Push(bytes) \
163 if (DEBUG_CCODEGEN) \
164 fprintf (stderr, "%s:%d: Push (%d)\n", \
165 __FILE__, __LINE__, bytes); \
166 StackTop += (bytes); \
171 l_nextFun = *(uintptr_t*)(StackTop - sizeof(void*)); \
172 if (DEBUG_CCODEGEN) \
173 fprintf (stderr, "%s:%d: Return() l_nextFun = %d\n", \
174 __FILE__, __LINE__, (int)l_nextFun); \
180 if (DEBUG_CCODEGEN) \
181 fprintf (stderr, "%s:%d: Raise\n", \
182 __FILE__, __LINE__); \
183 StackTop = StackBottom + ExnStack; \
187 /* ------------------------------------------------- */
189 /* ------------------------------------------------- */
191 #ifndef MLTON_CODEGEN_STATIC_INLINE
192 #define MLTON_CODEGEN_STATIC_INLINE static inline
194 /* Declare inlined math functions, since <math.h> isn't included.
196 #ifndef MLTON_CODEGEN_MATHFN
197 #define MLTON_CODEGEN_MATHFN(decl) decl
199 /* WordS<N>_quot and WordS<N>_rem can't be inlined with the C-codegen,
200 * because the gcc optimizer sometimes produces incorrect results when
201 * one of the arguments is a constant.
203 #ifndef MLTON_CODEGEN_WORDSQUOTREM
204 #define MLTON_CODEGEN_WORDSQUOTREM(func) PRIVATE
206 #ifndef MLTON_CODEGEN_WORDSQUOTREM_IMPL
207 #define MLTON_CODEGEN_WORDSQUOTREM_IMPL(func)
209 /* Declare memcpy, since <string.h> isn't included.
211 #ifndef MLTON_CODEGEN_MEMCPY
212 #define MLTON_CODEGEN_MEMCPY(decl)
214 MLTON_CODEGEN_MEMCPY(void * memcpy(void *, const void*, size_t);)
215 #include "basis-ffi.h"
216 #include "basis/coerce.h"
217 #include "basis/cpointer.h"
218 #include "basis/Real/Real-ops.h"
219 #include "basis/Real/Math-fns.h"
220 #include "basis/Word/Word-ops.h"
221 #include "basis/Word/Word-consts.h"
222 #include "basis/Word/Word-check.h"
224 /* ------------------------------------------------- */
226 /* ------------------------------------------------- */
228 #define WordS_addCheckCX(size, dst, cW, xW, l) \
230 WordS##size c = cW; \
231 WordS##size x = xW; \
232 WordS_addCheckBodyCX(size, c, x, goto l, dst = c + x); \
234 #define WordS8_addCheckCX(dst, c, x, l) WordS_addCheckCX(8, dst, c, x, l)
235 #define WordS16_addCheckCX(dst, c, x, l) WordS_addCheckCX(16, dst, c, x, l)
236 #define WordS32_addCheckCX(dst, c, x, l) WordS_addCheckCX(32, dst, c, x, l)
237 #define WordS64_addCheckCX(dst, c, x, l) WordS_addCheckCX(64, dst, c, x, l)
239 #define WordS8_addCheckXC(dst, x, c, l) WordS8_addCheckCX(dst, c, x, l)
240 #define WordS16_addCheckXC(dst, x, c, l) WordS16_addCheckCX(dst, c, x, l)
241 #define WordS32_addCheckXC(dst, x, c, l) WordS32_addCheckCX(dst, c, x, l)
242 #define WordS64_addCheckXC(dst, x, c, l) WordS64_addCheckCX(dst, c, x, l)
244 #define WordS8_addCheck WordS8_addCheckXC
245 #define WordS16_addCheck WordS16_addCheckXC
246 #define WordS32_addCheck WordS32_addCheckXC
247 #define WordS64_addCheck WordS64_addCheckXC
250 #define WordU_addCheckCX(size, dst, cW, xW, l) \
252 WordU##size c = cW; \
253 WordU##size x = xW; \
254 WordU_addCheckBodyCX(size, c, x, goto l, dst = c + x); \
256 #define WordU8_addCheckCX(dst, c, x, l) WordU_addCheckCX(8, dst, c, x, l)
257 #define WordU16_addCheckCX(dst, c, x, l) WordU_addCheckCX(16, dst, c, x, l)
258 #define WordU32_addCheckCX(dst, c, x, l) WordU_addCheckCX(32, dst, c, x, l)
259 #define WordU64_addCheckCX(dst, c, x, l) WordU_addCheckCX(64, dst, c, x, l)
261 #define WordU8_addCheckXC(dst, x, c, l) WordU8_addCheckCX(dst, c, x, l)
262 #define WordU16_addCheckXC(dst, x, c, l) WordU16_addCheckCX(dst, c, x, l)
263 #define WordU32_addCheckXC(dst, x, c, l) WordU32_addCheckCX(dst, c, x, l)
264 #define WordU64_addCheckXC(dst, x, c, l) WordU64_addCheckCX(dst, c, x, l)
266 #define WordU8_addCheck WordU8_addCheckXC
267 #define WordU16_addCheck WordU16_addCheckXC
268 #define WordU32_addCheck WordU32_addCheckXC
269 #define WordU64_addCheck WordU64_addCheckXC
272 #define WordS_negCheck(size, dst, xW, l) \
274 WordS##size x = xW; \
275 WordS_negCheckBody(size, x, goto l, dst = -x); \
277 #define Word8_negCheck(dst, x, l) WordS_negCheck(8, dst, x, l)
278 #define Word16_negCheck(dst, x, l) WordS_negCheck(16, dst, x, l)
279 #define Word32_negCheck(dst, x, l) WordS_negCheck(32, dst, x, l)
280 #define Word64_negCheck(dst, x, l) WordS_negCheck(64, dst, x, l)
283 #define WordS_subCheckCX(size, dst, cW, xW, l) \
285 WordS##size c = cW; \
286 WordS##size x = xW; \
287 WordS_subCheckBodyCX(size, c, x, goto l, dst = c - x); \
289 #define WordS8_subCheckCX(dst, c, x, l) WordS_subCheckCX(8, dst, c, x, l)
290 #define WordS16_subCheckCX(dst, c, x, l) WordS_subCheckCX(16, dst, c, x, l)
291 #define WordS32_subCheckCX(dst, c, x, l) WordS_subCheckCX(32, dst, c, x, l)
292 #define WordS64_subCheckCX(dst, c, x, l) WordS_subCheckCX(64, dst, c, x, l)
294 #define WordS_subCheckXC(size, dst, xW, cW, l) \
296 WordS##size x = xW; \
297 WordS##size c = cW; \
298 WordS_subCheckBodyXC(size, x, c, goto l, dst = x - c); \
300 #define WordS8_subCheckXC(dst, x, c, l) WordS_subCheckXC(8, dst, x, c, l)
301 #define WordS16_subCheckXC(dst, x, c, l) WordS_subCheckXC(16, dst, x, c, l)
302 #define WordS32_subCheckXC(dst, x, c, l) WordS_subCheckXC(32, dst, x, c, l)
303 #define WordS64_subCheckXC(dst, x, c, l) WordS_subCheckXC(64, dst, x, c, l)
305 #define WordS8_subCheck WordS8_subCheckXC
306 #define WordS16_subCheck WordS16_subCheckXC
307 #define WordS32_subCheck WordS32_subCheckXC
308 #define WordS64_subCheck WordS64_subCheckXC
311 #define WordS_mulCheck(size, dst, xW, yW, l) \
313 WordS##size x = xW; \
314 WordS##size y = yW; \
315 WordS_mulCheckBody(size, x, y, goto l, dst = x * y); \
317 #define WordS8_mulCheck(dst, x, y, l) WordS_mulCheck(8, dst, x, y, l)
318 #define WordS16_mulCheck(dst, x, y, l) WordS_mulCheck(16, dst, x, y, l)
319 #define WordS32_mulCheck(dst, x, y, l) WordS_mulCheck(32, dst, x, y, l)
320 #define WordS64_mulCheck(dst, x, y, l) WordS_mulCheck(64, dst, x, y, l)
322 #define WordU_mulCheck(size, dst, xW, yW, l) \
324 WordU##size x = xW; \
325 WordU##size y = yW; \
326 WordU_mulCheckBody(size, x, y, goto l, dst = x * y); \
328 #define WordU8_mulCheck(dst, x, y, l) WordU_mulCheck(8, dst, x, y, l)
329 #define WordU16_mulCheck(dst, x, y, l) WordU_mulCheck(16, dst, x, y, l)
330 #define WordU32_mulCheck(dst, x, y, l) WordU_mulCheck(32, dst, x, y, l)
331 #define WordU64_mulCheck(dst, x, y, l) WordU_mulCheck(64, dst, x, y, l)
333 #endif /* #ifndef _C_CHUNK_H_ */