f27154ebc2637994719c6b508dc2905cc7b8e8b2
[ghc-hetmet.git] / ghc / includes / Assembler.h
1
2 /* -----------------------------------------------------------------------------
3  * $Id: Assembler.h,v 1.15 2000/06/15 13:18:08 daan Exp $
4  *
5  * (c) The GHC Team 1994-1998.
6  *
7  * Bytecode assembler
8  *
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
11  * rely on either.  
12  * (In fact, it might be fun to create a GreenCard interface to this file too.)
13  * ---------------------------------------------------------------------------*/
14
15 /* ToDo: put this somewhere more sensible */
16 extern void DEBUG_LoadSymbols( char *name );
17
18 /* Make this compilable with Visual C++ */
19 #ifndef HAVE_INT64
20 #define HAVE_INT64
21 #ifdef _MSC_VER
22 typedef __int64            int64;
23 typedef unsigned __int64   nat64;
24 #else
25 typedef long long          int64;
26 typedef unsigned long long nat64;
27 #endif
28 #endif
29
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.
38  * -- ADR
39  */
40
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;
51
52 typedef int   AsmSp;   /* stack offset                  */
53 typedef int   AsmPc;   /* program counter               */
54 typedef AsmSp AsmVar;  /* offset of a Var on the stack  */
55
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?
60  */
61 #ifdef INSIDE_ASSEMBLER_C
62 /* these types are defined in Assembler.c */
63 typedef 
64    enum { 
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 */
68
69      Asm_NonPtrWord,                          /* A non-pointer word */
70      Asm_Insn8,                                /* One BCO insn byte */
71    }
72    Asm_Kind;
73
74 typedef
75    struct {
76       Asm_Kind  kind;
77       StgWord   val;   /* StgWord is allegedly big enough to also hold
78                           a pointer, on all platforms */
79    }
80    Asm_Entity;
81
82
83    struct AsmObject_ {
84       unsigned int magic;
85       struct AsmObject_* next;
86       enum { Asm_BCO, Asm_CAF, Asm_Con } kind;
87       int           sizeEntities;
88       int           usedEntities;
89       Asm_Entity*   entities;
90       StgClosure*   closure;
91
92       int           n_refs;          /* number of ptr words  */
93       int           n_words;         /* number of words      */
94       int           n_insns;         /* number of insn BYTES */
95
96       /* AsmCon specifics */
97       StgInfoTable* itbl;
98
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 */
104    };
105    /* AsmObject_ is only mentioned in Assembler.c; clients use
106       AsmObject/AsmBCO/AsmCAF/AsmCon. 
107    */
108
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;
115 #else
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;
124 #endif
125
126
127
128 /* --------------------------------------------------------------------------
129  * "Types" used within the assembler
130  *
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.
134  *
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  * ------------------------------------------------------------------------*/
140
141 typedef enum {
142   ILLEGAL_REP = 0,
143
144   /* The following can be passed to C */
145   CHAR_REP    = 'C',     
146   INT_REP     = 'I',      
147   INTEGER_REP = 'Z',  
148   WORD_REP    = 'W',     
149   ADDR_REP    = 'A',     
150   FLOAT_REP   = 'F',    
151   DOUBLE_REP  = 'D',   
152   STABLE_REP  = 's',   /* StablePtr a */
153 #ifdef PROVIDE_FOREIGN
154   FOREIGN_REP = 'f',   /* ForeignObj  */
155 #endif
156 #ifdef PROVIDE_WEAK
157   WEAK_REP    = 'w',   /* Weak a      */
158 #endif
159   BARR_REP     = 'x',  /* PrimByteArray          a */
160   MUTBARR_REP  = 'm',  /* PrimMutableByteArray s a */
161
162   /* The following can't be passed to C */
163   PTR_REP      = 'P',      
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                   */
177
178   /* Allegedly used in the IO monad */
179   VOID_REP     = 'v'      
180 } AsmRep;
181
182 /* --------------------------------------------------------------------------
183  * Top-level control of the BCO generation + linking mechanism
184  * ------------------------------------------------------------------------*/
185
186 extern void asmInitialise         ( void );
187 extern void asmAllocateHeapSpace  ( void );
188 extern void asmCopyAndLink        ( void );
189 extern void asmShutdown           ( void );
190
191 extern void* /* StgClosure* */ asmGetClosureOfObject ( AsmObject );
192
193 /* --------------------------------------------------------------------------
194  * Allocating (top level) heap objects
195  * ------------------------------------------------------------------------*/
196
197 extern AsmBCO     asmBeginBCO        ( int /*StgExpr*/ e );
198 extern void       asmEndBCO          ( AsmBCO bco );
199
200 extern AsmBCO     asmBeginContinuation ( AsmSp sp, int /*List*/ alts );
201 extern void       asmEndContinuation   ( AsmBCO bco );
202
203 extern AsmCAF     asmBeginCAF        ( void );
204 extern void       asmEndCAF          ( AsmCAF caf );
205
206 extern AsmInfo    asmMkInfo          ( AsmNat tag, AsmNat ptrs );
207 extern AsmCon     asmBeginCon        ( AsmInfo info );
208 extern void       asmEndCon          ( AsmCon con );
209
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.
213  */
214 extern void       asmAddPtr          ( AsmObject obj, AsmObject arg );
215 extern int        asmRepSizeW        ( AsmRep rep );
216
217 /* --------------------------------------------------------------------------
218  * Generating instruction streams
219  * ------------------------------------------------------------------------*/
220                                
221 extern AsmSp  asmBeginArgCheck ( AsmBCO bco );
222 extern void   asmEndArgCheck   ( AsmBCO bco, AsmSp last_arg );
223                                
224 extern AsmSp  asmBeginEnter    ( AsmBCO bco );
225 extern void   asmEndEnter      ( AsmBCO bco, AsmSp sp1, AsmSp sp2 );
226                                
227 extern AsmVar asmBind          ( AsmBCO bco, AsmRep rep );
228 extern void   asmVar           ( AsmBCO bco, AsmVar v, AsmRep rep );
229                                
230 extern AsmSp  asmBeginCase     ( AsmBCO bco );
231 extern void   asmEndCase       ( AsmBCO bco );
232 extern AsmSp  asmContinuation  ( AsmBCO bco, AsmBCO ret_addr );
233
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 );
240                                
241 extern AsmVar asmBox           ( AsmBCO bco, AsmRep rep );
242 extern AsmVar asmUnbox         ( AsmBCO bco, AsmRep rep );
243 extern void   asmReturnUnboxed ( AsmBCO bco, AsmRep rep );             
244
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 );
253              
254 /* Which monad (if any) does the primop live in? */
255 typedef enum {
256     MONAD_Id,  /* no monad (aka the identity monad) */
257     MONAD_ST,
258     MONAD_IO
259 } AsmMonad;
260
261 typedef struct {
262     char*    name;
263     char*    args;
264     char*    results;
265     AsmMonad monad;
266     AsmNat8  prefix; /* should be StgInstr           */
267     AsmNat8  opcode; /* should be Primop1 or Primop2 */
268 } AsmPrim;
269
270 extern AsmPrim asmPrimOps[]; /* null terminated list */
271
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, 
276                                                AsmSp base );
277 extern char*    asmGetPrimopName ( AsmPrim* p );
278
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 );
283
284
285 /* --------------------------------------------------------------------------
286  * Heap manipulation
287  * ------------------------------------------------------------------------*/
288
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 );
292
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 );
296
297 extern AsmVar asmAllocCONSTR   ( AsmBCO bco, AsmInfo info );
298
299 extern AsmSp  asmBeginPack     ( AsmBCO bco );
300 extern void   asmEndPack       ( AsmBCO bco, AsmVar v, AsmSp start, 
301                                                        AsmInfo info );
302
303 extern void   asmBeginUnpack   ( AsmBCO bco );
304 extern void   asmEndUnpack     ( AsmBCO bco );
305
306 extern AsmVar asmAllocAP       ( AsmBCO bco, AsmNat size );
307 extern AsmSp  asmBeginMkAP     ( AsmBCO bco );
308 extern void   asmEndMkAP       ( AsmBCO bco, AsmVar v, AsmSp start );
309
310 extern AsmVar asmAllocPAP      ( AsmBCO bco, AsmNat size );
311 extern AsmSp  asmBeginMkPAP    ( AsmBCO bco );
312 extern void   asmEndMkPAP      ( AsmBCO bco, AsmVar v, AsmSp start );
313
314 #ifdef XMLAMBDA
315 /*------------------------------------------------------------------------
316  XMlambda primitives.
317 ------------------------------------------------------------------------*/
318 typedef AsmInt  AsmIndex;
319
320 /* Rows */
321 extern AsmVar asmAllocRow      ( AsmBCO bco, AsmNat /*number of fields*/ n );
322
323 extern AsmSp  asmBeginPackRow  ( AsmBCO bco );
324 extern void   asmEndPackRow    ( AsmBCO bco, AsmVar v, AsmSp start, 
325                                              AsmNat /*number of fields*/ n );
326
327 extern void   asmBeginUnpackRow( AsmBCO bco );
328 extern void   asmEndUnpackRow  ( AsmBCO bco );
329
330 extern AsmPrim primRowRemoveAtN;
331 extern AsmPrim primRowIndexAtN;
332
333 /* Inj */
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 );
340 #endif
341
342 /* --------------------------------------------------------------------------
343  * C-call and H-call
344  * ------------------------------------------------------------------------*/
345
346 extern AsmPrim ccall_ccall_Id;
347 extern AsmPrim ccall_ccall_IO;
348 extern AsmPrim ccall_stdcall_Id;
349 extern AsmPrim ccall_stdcall_IO;
350
351 typedef struct {
352   unsigned int  num_args;
353   char*         arg_tys;
354   unsigned int  num_results;
355   char*         result_tys;
356 } CFunDescriptor;
357
358 CFunDescriptor* mkDescriptor( char* as, char* rs );
359
360 /*-------------------------------------------------------------------------*/