[project @ 1999-02-23 17:20:34 by sof]
[ghc-hetmet.git] / ghc / includes / Assembler.h
1 /* -*- mode: hugs-c; -*- */
2 /* -----------------------------------------------------------------------------
3  * $Id: Assembler.h,v 1.4 1999/02/05 16:02:18 simonm 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 #ifdef PROVIDE_INT64
88   INT64_REP   = 'z', 
89 #endif
90 #ifdef PROVIDE_INTEGER
91   INTEGER_REP = 'Z',  
92 #endif
93 #ifdef PROVIDE_WORD
94   WORD_REP    = 'W',     
95 #endif
96 #ifdef PROVIDE_ADDR
97   ADDR_REP    = 'A',     
98 #endif
99   FLOAT_REP   = 'F',    
100   DOUBLE_REP  = 'D',   
101 #ifdef PROVIDE_STABLE
102   STABLE_REP  = 's',   /* StablePtr a */
103 #endif
104 #ifdef PROVIDE_FOREIGN
105   FOREIGN_REP = 'f',   /* ForeignObj  */
106 #endif
107 #ifdef PROVIDE_WEAK
108   WEAK_REP    = 'w',   /* Weak a      */
109 #endif
110 #ifdef PROVIDE_ARRAY
111   BARR_REP     = 'x',  /* PrimByteArray          a */
112   MUTBARR_REP  = 'm',  /* PrimMutableByteArray s a */
113 #endif
114
115   /* The following can't be passed to C */
116   PTR_REP      = 'P',      
117   ALPHA_REP    = 'a',  /* a                        */
118   BETA_REP     = 'b',  /* b                        */
119   GAMMA_REP    = 'c',  /* c                        */
120   BOOL_REP     = 'B',  /* Bool                     */
121   IO_REP       = 'i',  /* IO a                     */
122   HANDLER_REP  = 'H',  /* Exception -> IO a        */
123   ERROR_REP    = 'E',  /* Exception                */
124 #ifdef PROVIDE_ARRAY            
125   ARR_REP      = 'X',  /* PrimArray              a */
126   REF_REP      = 'R',  /* Ref                  s a */
127   MUTARR_REP   = 'M',  /* PrimMutableArray     s a */
128 #endif
129 #ifdef PROVIDE_CONCURRENT
130   THREADID_REP = 'T',  /* ThreadId                 */
131   MVAR_REP     = 'r',  /* MVar a                   */
132 #endif
133
134   /* Allegedly used in the IO monad */
135   VOID_REP     = 'v'      
136 } AsmRep;
137
138 /* --------------------------------------------------------------------------
139  * Allocating (top level) heap objects
140  * ------------------------------------------------------------------------*/
141
142 extern AsmBCO     asmBeginBCO        ( void );
143 extern void       asmEndBCO          ( AsmBCO bco );
144
145 extern AsmBCO     asmBeginContinuation ( AsmSp sp );
146 extern void       asmEndContinuation   ( AsmBCO bco );
147
148 extern AsmObject  asmMkObject        ( AsmClosure c );
149
150 extern AsmCAF     asmBeginCAF        ( void );
151 extern void       asmEndCAF          ( AsmCAF caf, AsmBCO body );
152
153 extern AsmInfo    asmMkInfo          ( AsmNat tag, AsmNat ptrs );
154 extern AsmCon     asmBeginCon        ( AsmInfo info );
155 extern void       asmEndCon          ( AsmCon con );
156
157 /* NB: we add ptrs to other objects in left-to-right order.
158  * This is different from pushing arguments on the stack which is done
159  * in right to left order.
160  */
161 extern void       asmAddPtr          ( AsmObject obj, AsmObject arg );
162
163 extern int        asmObjectHasClosure( AsmObject obj );
164 extern AsmClosure asmClosureOfObject ( AsmObject obj );
165 extern void       asmMarkObject      ( AsmObject obj );
166
167 /* --------------------------------------------------------------------------
168  * Generating instruction streams
169  * ------------------------------------------------------------------------*/
170                                
171 extern AsmSp  asmBeginArgCheck ( AsmBCO bco );
172 extern void   asmEndArgCheck   ( AsmBCO bco, AsmSp last_arg );
173                                
174 extern AsmSp  asmBeginEnter    ( AsmBCO bco );
175 extern void   asmEndEnter      ( AsmBCO bco, AsmSp sp1, AsmSp sp2 );
176                                
177 extern AsmVar asmBind          ( AsmBCO bco, AsmRep rep );
178 extern void   asmVar           ( AsmBCO bco, AsmVar v, AsmRep rep );
179                                
180 extern AsmSp  asmBeginCase     ( AsmBCO bco );
181 extern void   asmEndCase       ( AsmBCO bco );
182 extern AsmSp  asmContinuation  ( AsmBCO bco, AsmBCO ret_addr );
183                                
184 extern AsmSp  asmBeginAlt      ( AsmBCO bco );
185 extern void   asmEndAlt        ( AsmBCO bco, AsmSp  sp );
186 extern AsmPc  asmTest          ( AsmBCO bco, AsmWord tag );
187 extern AsmPc  asmTestInt       ( AsmBCO bco, AsmVar v, AsmInt x );
188 extern void   asmFixBranch     ( AsmBCO bco, AsmPc pc );
189 extern void   asmPanic         ( AsmBCO bco );
190                                
191 extern AsmVar asmBox           ( AsmBCO bco, AsmRep rep );
192 extern AsmVar asmUnbox         ( AsmBCO bco, AsmRep rep );
193 extern void   asmReturnUnboxed ( AsmBCO bco, AsmRep rep );             
194
195 /* push unboxed Ints, Floats, etc */
196 extern void   asmConstInt      ( AsmBCO bco, AsmInt     x );
197 #ifdef PROVIDE_ADDR
198 extern void   asmConstAddr     ( AsmBCO bco, AsmAddr    x );
199 #endif
200 #ifdef PROVIDE_WORD
201 extern void   asmConstWord     ( AsmBCO bco, AsmWord    x );
202 #endif
203 extern void   asmConstChar     ( AsmBCO bco, AsmChar    x );
204 extern void   asmConstFloat    ( AsmBCO bco, AsmFloat   x );
205 extern void   asmConstDouble   ( AsmBCO bco, AsmDouble  x );
206 #ifdef PROVIDE_INT64
207 extern void   asmConstInt64    ( AsmBCO bco, AsmInt64   x );
208 #endif
209 #ifdef PROVIDE_INTEGER
210 extern void   asmConstInteger  ( AsmBCO bco, AsmString  x );
211 #endif
212              
213 /* Which monad (if any) does the primop live in? */
214 typedef enum {
215     MONAD_Id,  /* no monad (aka the identity monad) */
216     MONAD_ST,
217     MONAD_IO
218 } AsmMonad;
219
220 typedef struct {
221     char*    name;
222     char*    args;
223     char*    results;
224     AsmMonad monad;
225     AsmNat8  prefix; /* should be StgInstr           */
226     AsmNat8  opcode; /* should be Primop1 or Primop2 */
227 } AsmPrim;
228
229 extern const AsmPrim asmPrimOps[]; /* null terminated list */
230
231 extern const AsmPrim* asmFindPrim    ( char* s );
232 extern const AsmPrim* asmFindPrimop  ( AsmInstr prefix, AsmInstr op );
233 extern AsmSp          asmBeginPrim   ( AsmBCO bco );
234 extern void           asmEndPrim     ( AsmBCO bco, const AsmPrim* prim, AsmSp base );
235
236 /* --------------------------------------------------------------------------
237  * Heap manipulation
238  * ------------------------------------------------------------------------*/
239
240 extern AsmVar asmClosure       ( AsmBCO bco, AsmObject p );
241
242 extern AsmVar asmAllocCONSTR   ( AsmBCO bco, AsmInfo info );
243
244 extern AsmSp  asmBeginPack     ( AsmBCO bco );
245 extern void   asmEndPack       ( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info );
246
247 extern void   asmBeginUnpack   ( AsmBCO bco );
248 extern void   asmEndUnpack     ( AsmBCO bco );
249
250 extern AsmVar asmAllocAP       ( AsmBCO bco, AsmNat size );
251 extern AsmSp  asmBeginMkAP     ( AsmBCO bco );
252 extern void   asmEndMkAP       ( AsmBCO bco, AsmVar v, AsmSp start );
253
254 extern AsmVar asmAllocPAP      ( AsmBCO bco, AsmNat size );
255 extern AsmSp  asmBeginMkPAP    ( AsmBCO bco );
256 extern void   asmEndMkPAP      ( AsmBCO bco, AsmVar v, AsmSp start );
257
258 /* --------------------------------------------------------------------------
259  * C-call and H-call
260  * ------------------------------------------------------------------------*/
261
262 extern const AsmPrim ccall_Id;
263 extern const AsmPrim ccall_IO;
264
265 typedef struct {
266   char *        arg_tys;
267   int           arg_size;
268   char *        result_tys;
269   int           result_size;
270 } CFunDescriptor;
271
272 typedef struct {
273   char *        arg_tys;
274   char *        result_tys;
275 } HFunDescriptor;
276
277 CFunDescriptor* mkDescriptor( char* as, char* rs );
278
279 /*-------------------------------------------------------------------------*/