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