[project @ 1999-11-29 18:59:23 by sewardj]
[ghc-hetmet.git] / ghc / includes / Assembler.h
1
2 /* -----------------------------------------------------------------------------
3  * $Id: Assembler.h,v 1.12 1999/11/29 18:59:23 sewardj 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 /* This file is supposed to be somewhat self-contained because it is one
19  * of the major external interfaces to the runtime system.
20  * Keeping it self-contained reduces the chance of conflict with Hugs
21  * (or anything else that includes it).
22  * The big disadvantage of being self-contained is that definitions
23  * like AsmNat8, etc duplicate definitions in StgTypes.h.
24  * I'm not sure what we can do about this but, if you try to fix it,
25  * please remember why it was done this way in the first place.
26  * -- ADR
27  */
28
29 typedef unsigned char   AsmNat8;
30 typedef unsigned int    AsmNat;
31 typedef signed   int    AsmInt;
32 typedef signed long long int AsmInt64;  /* ToDo: not portable!  */
33 typedef unsigned int    AsmWord;
34 typedef void*           AsmAddr;
35 typedef unsigned char   AsmChar;
36 typedef float           AsmFloat;       /* ToDo: not on Alphas! */
37 typedef double          AsmDouble;
38 typedef char*           AsmString;
39
40 /* I want to #include this file into the file that defines the
41  * functions but I don't want to expose the structures that
42  * these types point to.
43  * This hack is the best I could think of.  Surely there's a better way?
44  */
45 #ifdef INSIDE_ASSEMBLER_C
46 typedef struct AsmObject_ *AsmObject;
47 typedef struct AsmBCO_    *AsmBCO;
48 typedef struct AsmCAF_    *AsmCAF;
49 typedef struct AsmCon_    *AsmCon;
50 typedef StgInfoTable      *AsmInfo;
51 typedef StgClosure        *AsmClosure;
52 typedef Instr              AsmInstr;
53 #else
54 /* the types we export are totally opaque */
55 typedef void              *AsmObject;
56 typedef void              *AsmBCO;
57 typedef void              *AsmCAF;
58 typedef void              *AsmCon;
59 typedef void              *AsmInfo;
60 typedef void              *AsmClosure;
61 typedef unsigned int       AsmInstr;
62 #endif
63
64 typedef int   AsmSp;   /* stack offset                  */
65 typedef int   AsmPc;   /* program counter               */
66 typedef AsmSp AsmVar;  /* offset of a Var on the stack  */
67
68 /* --------------------------------------------------------------------------
69  * "Types" used within the assembler
70  *
71  * Some of these types are synonyms for the same underlying representation
72  * to let Hugs (or whoever) generate useful Haskell types from the type
73  * of a primitive operation.
74  *
75  *  Extreme care should be taken if you change any of these - the
76  *  same constants are hardwired into Hugs (ILLEGAL_REP) and into
77  *  pieces of assembly language used to implement foreign import/export.
78  *  And, of course, you'll have to change the primop table in Assembler.c
79  * ------------------------------------------------------------------------*/
80
81 typedef enum {
82   ILLEGAL_REP = 0,
83
84   /* The following can be passed to C */
85   CHAR_REP    = 'C',     
86   INT_REP     = 'I',      
87   INTEGER_REP = 'Z',  
88   WORD_REP    = 'W',     
89   ADDR_REP    = 'A',     
90   FLOAT_REP   = 'F',    
91   DOUBLE_REP  = 'D',   
92   STABLE_REP  = 's',   /* StablePtr a */
93 #ifdef PROVIDE_FOREIGN
94   FOREIGN_REP = 'f',   /* ForeignObj  */
95 #endif
96 #ifdef PROVIDE_WEAK
97   WEAK_REP    = 'w',   /* Weak a      */
98 #endif
99   BARR_REP     = 'x',  /* PrimByteArray          a */
100   MUTBARR_REP  = 'm',  /* PrimMutableByteArray s a */
101
102   /* The following can't be passed to C */
103   PTR_REP      = 'P',      
104   ALPHA_REP    = 'a',  /* a                        */
105   BETA_REP     = 'b',  /* b                        */
106   GAMMA_REP    = 'c',  /* c                        */
107   DELTA_REP    = 'd',  /* d                        */
108   BOOL_REP     = 'B',  /* Bool                     */
109   IO_REP       = 'i',  /* IO a                     */
110   HANDLER_REP  = 'H',  /* Exception -> IO a        */
111   ERROR_REP    = 'E',  /* Exception                */
112   ARR_REP      = 'X',  /* PrimArray              a */
113   REF_REP      = 'R',  /* Ref                  s a */
114   MUTARR_REP   = 'M',  /* PrimMutableArray     s a */
115   THREADID_REP = 'T',  /* ThreadId                 */
116   MVAR_REP     = 'r',  /* MVar a                   */
117
118   /* Allegedly used in the IO monad */
119   VOID_REP     = 'v'      
120 } AsmRep;
121
122 /* --------------------------------------------------------------------------
123  * Allocating (top level) heap objects
124  * ------------------------------------------------------------------------*/
125
126 extern AsmBCO     asmBeginBCO        ( int /*StgExpr*/ e );
127 extern void       asmEndBCO          ( AsmBCO bco );
128
129 extern AsmBCO     asmBeginContinuation ( AsmSp sp, int /*List*/ alts );
130 extern void       asmEndContinuation   ( AsmBCO bco );
131
132 extern AsmObject  asmMkObject        ( AsmClosure c );
133
134 extern AsmCAF     asmBeginCAF        ( void );
135 extern void       asmEndCAF          ( AsmCAF caf, AsmBCO body );
136
137 extern AsmInfo    asmMkInfo          ( AsmNat tag, AsmNat ptrs );
138 extern AsmCon     asmBeginCon        ( AsmInfo info );
139 extern void       asmEndCon          ( AsmCon con );
140
141 /* NB: we add ptrs to other objects in left-to-right order.
142  * This is different from pushing arguments on the stack which is done
143  * in right to left order.
144  */
145 extern void       asmAddPtr          ( AsmObject obj, AsmObject arg );
146
147 extern int        asmObjectHasClosure( AsmObject obj );
148 extern AsmClosure asmClosureOfObject ( AsmObject obj );
149 extern void       asmMarkObject      ( AsmObject obj );
150
151 extern int        asmRepSizeW        ( AsmRep rep );
152
153 /* --------------------------------------------------------------------------
154  * Generating instruction streams
155  * ------------------------------------------------------------------------*/
156                                
157 extern AsmSp  asmBeginArgCheck ( AsmBCO bco );
158 extern void   asmEndArgCheck   ( AsmBCO bco, AsmSp last_arg );
159                                
160 extern AsmSp  asmBeginEnter    ( AsmBCO bco );
161 extern void   asmEndEnter      ( AsmBCO bco, AsmSp sp1, AsmSp sp2 );
162                                
163 extern AsmVar asmBind          ( AsmBCO bco, AsmRep rep );
164 extern void   asmVar           ( AsmBCO bco, AsmVar v, AsmRep rep );
165                                
166 extern AsmSp  asmBeginCase     ( AsmBCO bco );
167 extern void   asmEndCase       ( AsmBCO bco );
168 extern AsmSp  asmContinuation  ( AsmBCO bco, AsmBCO ret_addr );
169
170 extern AsmSp  asmBeginAlt      ( AsmBCO bco );
171 extern void   asmEndAlt        ( AsmBCO bco, AsmSp  sp );
172 extern AsmPc  asmTest          ( AsmBCO bco, AsmWord tag );
173 extern AsmPc  asmTestInt       ( AsmBCO bco, AsmVar v, AsmInt x );
174 extern void   asmFixBranch     ( AsmBCO bco, AsmPc pc );
175 extern void   asmPanic         ( AsmBCO bco );
176                                
177 extern AsmVar asmBox           ( AsmBCO bco, AsmRep rep );
178 extern AsmVar asmUnbox         ( AsmBCO bco, AsmRep rep );
179 extern void   asmReturnUnboxed ( AsmBCO bco, AsmRep rep );             
180
181 /* push unboxed Ints, Floats, etc */
182 extern void   asmConstInt      ( AsmBCO bco, AsmInt     x );
183 extern void   asmConstAddr     ( AsmBCO bco, AsmAddr    x );
184 extern void   asmConstWord     ( AsmBCO bco, AsmWord    x );
185 extern void   asmConstChar     ( AsmBCO bco, AsmChar    x );
186 extern void   asmConstFloat    ( AsmBCO bco, AsmFloat   x );
187 extern void   asmConstDouble   ( AsmBCO bco, AsmDouble  x );
188 extern void   asmConstInteger  ( AsmBCO bco, AsmString  x );
189              
190 /* Which monad (if any) does the primop live in? */
191 typedef enum {
192     MONAD_Id,  /* no monad (aka the identity monad) */
193     MONAD_ST,
194     MONAD_IO
195 } AsmMonad;
196
197 typedef struct {
198     char*    name;
199     char*    args;
200     char*    results;
201     AsmMonad monad;
202     AsmNat8  prefix; /* should be StgInstr           */
203     AsmNat8  opcode; /* should be Primop1 or Primop2 */
204 } AsmPrim;
205
206 extern AsmPrim asmPrimOps[]; /* null terminated list */
207
208 extern AsmPrim* asmFindPrim      ( char* s );
209 extern AsmPrim* asmFindPrimop    ( AsmInstr prefix, AsmInstr op );
210 extern AsmSp    asmBeginPrim     ( AsmBCO bco );
211 extern void     asmEndPrim       ( AsmBCO bco, const AsmPrim* prim, 
212                                                AsmSp base );
213 extern char*    asmGetPrimopName ( AsmPrim* p );
214
215 extern AsmBCO asm_BCO_catch    ( void );
216 extern AsmBCO asm_BCO_raise    ( void );
217 extern AsmBCO asm_BCO_seq      ( void );
218 extern AsmBCO asm_BCO_takeMVar ( void );
219
220
221 /* --------------------------------------------------------------------------
222  * Heap manipulation
223  * ------------------------------------------------------------------------*/
224
225 extern AsmVar asmClosure       ( AsmBCO bco, AsmObject p );
226 extern AsmVar asmGHCClosure    ( AsmBCO bco, AsmObject p );
227
228 extern AsmVar asmAllocCONSTR   ( AsmBCO bco, AsmInfo info );
229
230 extern AsmSp  asmBeginPack     ( AsmBCO bco );
231 extern void   asmEndPack       ( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info );
232
233 extern void   asmBeginUnpack   ( AsmBCO bco );
234 extern void   asmEndUnpack     ( AsmBCO bco );
235
236 extern AsmVar asmAllocAP       ( AsmBCO bco, AsmNat size );
237 extern AsmSp  asmBeginMkAP     ( AsmBCO bco );
238 extern void   asmEndMkAP       ( AsmBCO bco, AsmVar v, AsmSp start );
239
240 extern AsmVar asmAllocPAP      ( AsmBCO bco, AsmNat size );
241 extern AsmSp  asmBeginMkPAP    ( AsmBCO bco );
242 extern void   asmEndMkPAP      ( AsmBCO bco, AsmVar v, AsmSp start );
243
244 /* --------------------------------------------------------------------------
245  * C-call and H-call
246  * ------------------------------------------------------------------------*/
247
248 extern AsmPrim ccall_ccall_Id;
249 extern AsmPrim ccall_ccall_IO;
250 extern AsmPrim ccall_stdcall_Id;
251 extern AsmPrim ccall_stdcall_IO;
252
253 typedef struct {
254   unsigned int  num_args;
255   char*         arg_tys;
256   unsigned int  num_results;
257   char*         result_tys;
258 } CFunDescriptor;
259
260 CFunDescriptor* mkDescriptor( char* as, char* rs );
261
262 /*-------------------------------------------------------------------------*/