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