2 /* -----------------------------------------------------------------------------
3 * $Id: Assembler.h,v 1.15 2000/06/15 13:18:08 daan Exp $
5 * (c) The GHC Team 1994-1998.
9 * NB This is one of the few files shared between Hugs and the runtime system,
10 * so it is very important that it not conflict with either and that it not
12 * (In fact, it might be fun to create a GreenCard interface to this file too.)
13 * ---------------------------------------------------------------------------*/
15 /* ToDo: put this somewhere more sensible */
16 extern void DEBUG_LoadSymbols( char *name );
18 /* Make this compilable with Visual C++ */
22 typedef __int64 int64;
23 typedef unsigned __int64 nat64;
25 typedef long long int64;
26 typedef unsigned long long nat64;
30 /* This file is supposed to be somewhat self-contained because it is one
31 * of the major external interfaces to the runtime system.
32 * Keeping it self-contained reduces the chance of conflict with Hugs
33 * (or anything else that includes it).
34 * The big disadvantage of being self-contained is that definitions
35 * like AsmNat8, etc duplicate definitions in StgTypes.h.
36 * I'm not sure what we can do about this but, if you try to fix it,
37 * please remember why it was done this way in the first place.
41 typedef unsigned char AsmNat8;
42 typedef unsigned int AsmNat;
43 typedef signed int AsmInt;
44 typedef int64 AsmInt64;
45 typedef unsigned int AsmWord;
46 typedef void* AsmAddr;
47 typedef unsigned char AsmChar;
48 typedef float AsmFloat; /* ToDo: not on Alphas! */
49 typedef double AsmDouble;
50 typedef char* AsmString;
52 typedef int AsmSp; /* stack offset */
53 typedef int AsmPc; /* program counter */
54 typedef AsmSp AsmVar; /* offset of a Var on the stack */
56 /* I want to #include this file into the file that defines the
57 * functions but I don't want to expose the structures that
58 * these types point to.
59 * This hack is the best I could think of. Surely there's a better way?
61 #ifdef INSIDE_ASSEMBLER_C
62 /* these types are defined in Assembler.c */
65 Asm_RefNoOp, /* Pointer which needs no further messing with */
66 Asm_RefObject, /* Reference to malloc'd AsmCAF/AsmBCO/AsmCon */
67 Asm_RefHugs, /* Reference to Hugs name or tycon table */
69 Asm_NonPtrWord, /* A non-pointer word */
70 Asm_Insn8, /* One BCO insn byte */
77 StgWord val; /* StgWord is allegedly big enough to also hold
78 a pointer, on all platforms */
85 struct AsmObject_* next;
86 enum { Asm_BCO, Asm_CAF, Asm_Con } kind;
92 int n_refs; /* number of ptr words */
93 int n_words; /* number of words */
94 int n_insns; /* number of insn BYTES */
96 /* AsmCon specifics */
99 /* AsmBCO specifics */
100 int /*StgExpr*/ stgexpr; /* stg tree for debugging */
101 AsmSp sp; /* simulated sp */
102 AsmSp max_sp; /* high-tide of sp */
103 Instr lastOpc; /* last opcode, for peephole opt */
105 /* AsmObject_ is only mentioned in Assembler.c; clients use
106 AsmObject/AsmBCO/AsmCAF/AsmCon.
109 typedef StgInfoTable* AsmInfo;
110 typedef struct AsmObject_* AsmBCO;
111 typedef struct AsmObject_* AsmCAF;
112 typedef struct AsmObject_* AsmCon;
113 typedef struct AsmObject_* AsmObject;
114 typedef Instr AsmInstr;
116 /* the types we export are totally opaque */
117 typedef void* AsmObject;
118 typedef void* AsmBCO;
119 typedef void* AsmCAF;
120 typedef void* AsmCon;
121 typedef void* AsmInfo;
122 typedef void* AsmClosure;
123 typedef unsigned int AsmInstr;
128 /* --------------------------------------------------------------------------
129 * "Types" used within the assembler
131 * Some of these types are synonyms for the same underlying representation
132 * to let Hugs (or whoever) generate useful Haskell types from the type
133 * of a primitive operation.
135 * Extreme care should be taken if you change any of these - the
136 * same constants are hardwired into Hugs (ILLEGAL_REP) and into
137 * pieces of assembly language used to implement foreign import/export.
138 * And, of course, you'll have to change the primop table in Assembler.c
139 * ------------------------------------------------------------------------*/
144 /* The following can be passed to C */
152 STABLE_REP = 's', /* StablePtr a */
153 #ifdef PROVIDE_FOREIGN
154 FOREIGN_REP = 'f', /* ForeignObj */
157 WEAK_REP = 'w', /* Weak a */
159 BARR_REP = 'x', /* PrimByteArray a */
160 MUTBARR_REP = 'm', /* PrimMutableByteArray s a */
162 /* The following can't be passed to C */
164 ALPHA_REP = 'a', /* a */
165 BETA_REP = 'b', /* b */
166 GAMMA_REP = 'c', /* c */
167 DELTA_REP = 'd', /* d */
168 BOOL_REP = 'B', /* Bool */
169 IO_REP = 'i', /* IO a */
170 HANDLER_REP = 'H', /* Exception -> IO a */
171 ERROR_REP = 'E', /* Exception */
172 ARR_REP = 'X', /* PrimArray a */
173 REF_REP = 'R', /* Ref s a */
174 MUTARR_REP = 'M', /* PrimMutableArray s a */
175 THREADID_REP = 'T', /* ThreadId */
176 MVAR_REP = 'r', /* MVar a */
178 /* Allegedly used in the IO monad */
182 /* --------------------------------------------------------------------------
183 * Top-level control of the BCO generation + linking mechanism
184 * ------------------------------------------------------------------------*/
186 extern void asmInitialise ( void );
187 extern void asmAllocateHeapSpace ( void );
188 extern void asmCopyAndLink ( void );
189 extern void asmShutdown ( void );
191 extern void* /* StgClosure* */ asmGetClosureOfObject ( AsmObject );
193 /* --------------------------------------------------------------------------
194 * Allocating (top level) heap objects
195 * ------------------------------------------------------------------------*/
197 extern AsmBCO asmBeginBCO ( int /*StgExpr*/ e );
198 extern void asmEndBCO ( AsmBCO bco );
200 extern AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts );
201 extern void asmEndContinuation ( AsmBCO bco );
203 extern AsmCAF asmBeginCAF ( void );
204 extern void asmEndCAF ( AsmCAF caf );
206 extern AsmInfo asmMkInfo ( AsmNat tag, AsmNat ptrs );
207 extern AsmCon asmBeginCon ( AsmInfo info );
208 extern void asmEndCon ( AsmCon con );
210 /* NB: we add ptrs to other objects in left-to-right order.
211 * This is different from pushing arguments on the stack which is done
212 * in right to left order.
214 extern void asmAddPtr ( AsmObject obj, AsmObject arg );
215 extern int asmRepSizeW ( AsmRep rep );
217 /* --------------------------------------------------------------------------
218 * Generating instruction streams
219 * ------------------------------------------------------------------------*/
221 extern AsmSp asmBeginArgCheck ( AsmBCO bco );
222 extern void asmEndArgCheck ( AsmBCO bco, AsmSp last_arg );
224 extern AsmSp asmBeginEnter ( AsmBCO bco );
225 extern void asmEndEnter ( AsmBCO bco, AsmSp sp1, AsmSp sp2 );
227 extern AsmVar asmBind ( AsmBCO bco, AsmRep rep );
228 extern void asmVar ( AsmBCO bco, AsmVar v, AsmRep rep );
230 extern AsmSp asmBeginCase ( AsmBCO bco );
231 extern void asmEndCase ( AsmBCO bco );
232 extern AsmSp asmContinuation ( AsmBCO bco, AsmBCO ret_addr );
234 extern AsmSp asmBeginAlt ( AsmBCO bco );
235 extern void asmEndAlt ( AsmBCO bco, AsmSp sp );
236 extern AsmPc asmTest ( AsmBCO bco, AsmWord tag );
237 extern AsmPc asmTestInt ( AsmBCO bco, AsmVar v, AsmInt x );
238 extern void asmFixBranch ( AsmBCO bco, AsmPc pc );
239 extern void asmPanic ( AsmBCO bco );
241 extern AsmVar asmBox ( AsmBCO bco, AsmRep rep );
242 extern AsmVar asmUnbox ( AsmBCO bco, AsmRep rep );
243 extern void asmReturnUnboxed ( AsmBCO bco, AsmRep rep );
245 /* push unboxed Ints, Floats, etc */
246 extern void asmConstInt ( AsmBCO bco, AsmInt x );
247 extern void asmConstAddr ( AsmBCO bco, AsmAddr x );
248 extern void asmConstWord ( AsmBCO bco, AsmWord x );
249 extern void asmConstChar ( AsmBCO bco, AsmChar x );
250 extern void asmConstFloat ( AsmBCO bco, AsmFloat x );
251 extern void asmConstDouble ( AsmBCO bco, AsmDouble x );
252 extern void asmConstInteger ( AsmBCO bco, AsmString x );
254 /* Which monad (if any) does the primop live in? */
256 MONAD_Id, /* no monad (aka the identity monad) */
266 AsmNat8 prefix; /* should be StgInstr */
267 AsmNat8 opcode; /* should be Primop1 or Primop2 */
270 extern AsmPrim asmPrimOps[]; /* null terminated list */
272 extern AsmPrim* asmFindPrim ( char* s );
273 extern AsmPrim* asmFindPrimop ( AsmInstr prefix, AsmInstr op );
274 extern AsmSp asmBeginPrim ( AsmBCO bco );
275 extern void asmEndPrim ( AsmBCO bco, const AsmPrim* prim,
277 extern char* asmGetPrimopName ( AsmPrim* p );
279 extern void* /* StgBCO* */ asm_BCO_catch ( void );
280 extern void* /* StgBCO* */ asm_BCO_raise ( void );
281 extern void* /* StgBCO* */ asm_BCO_seq ( void );
282 extern void* /* StgBCO* */ asm_BCO_takeMVar ( void );
285 /* --------------------------------------------------------------------------
287 * ------------------------------------------------------------------------*/
289 extern AsmVar asmPushRefHugs ( AsmBCO bco, int /*Name*/ n );
290 extern AsmVar asmPushRefObject ( AsmBCO bco, AsmObject p );
291 extern AsmVar asmPushRefNoOp ( AsmBCO bco, StgPtr p );
293 extern void asmAddRefObject ( AsmObject obj, AsmObject p );
294 extern void asmAddRefNoOp ( AsmObject obj, StgPtr p );
295 extern void asmAddRefHugs ( AsmObject obj,int /*Name*/ n );
297 extern AsmVar asmAllocCONSTR ( AsmBCO bco, AsmInfo info );
299 extern AsmSp asmBeginPack ( AsmBCO bco );
300 extern void asmEndPack ( AsmBCO bco, AsmVar v, AsmSp start,
303 extern void asmBeginUnpack ( AsmBCO bco );
304 extern void asmEndUnpack ( AsmBCO bco );
306 extern AsmVar asmAllocAP ( AsmBCO bco, AsmNat size );
307 extern AsmSp asmBeginMkAP ( AsmBCO bco );
308 extern void asmEndMkAP ( AsmBCO bco, AsmVar v, AsmSp start );
310 extern AsmVar asmAllocPAP ( AsmBCO bco, AsmNat size );
311 extern AsmSp asmBeginMkPAP ( AsmBCO bco );
312 extern void asmEndMkPAP ( AsmBCO bco, AsmVar v, AsmSp start );
315 /*------------------------------------------------------------------------
317 ------------------------------------------------------------------------*/
318 typedef AsmInt AsmIndex;
321 extern AsmVar asmAllocRow ( AsmBCO bco, AsmNat /*number of fields*/ n );
323 extern AsmSp asmBeginPackRow ( AsmBCO bco );
324 extern void asmEndPackRow ( AsmBCO bco, AsmVar v, AsmSp start,
325 AsmNat /*number of fields*/ n );
327 extern void asmBeginUnpackRow( AsmBCO bco );
328 extern void asmEndUnpackRow ( AsmBCO bco );
330 extern AsmPrim primRowRemoveAtN;
331 extern AsmPrim primRowIndexAtN;
334 extern AsmVar asmInj( AsmBCO bco, AsmVar var );
335 extern AsmVar asmInjConst( AsmBCO bco, AsmIndex i );
336 extern AsmVar asmUnInj( AsmBCO bco );
337 extern AsmPc asmTestInj( AsmBCO bco, AsmVar var );
338 extern AsmPc asmTestInjConst( AsmBCO, AsmIndex i );
339 extern AsmVar asmConstIndex( AsmBCO bco, AsmIndex x );
342 /* --------------------------------------------------------------------------
344 * ------------------------------------------------------------------------*/
346 extern AsmPrim ccall_ccall_Id;
347 extern AsmPrim ccall_ccall_IO;
348 extern AsmPrim ccall_stdcall_Id;
349 extern AsmPrim ccall_stdcall_IO;
352 unsigned int num_args;
354 unsigned int num_results;
358 CFunDescriptor* mkDescriptor( char* as, char* rs );
360 /*-------------------------------------------------------------------------*/