[project @ 1999-01-18 14:35:20 by sof]
[ghc-hetmet.git] / ghc / rts / Adjustor.c
1 /* -----------------------------------------------------------------------------
2  * Foreign export adjustor thunks
3  *
4  * Copyright (c) 1998.
5  *
6  * ---------------------------------------------------------------------------*/
7
8 /* A little bit of background...
9
10 An adjustor thunk is a dynamically allocated code snippet that allows
11 Haskell closures to be viewed as C function pointers. 
12
13 Stable pointers provide a way for the outside world to get access to,
14 and evaluate, Haskell heap objects, with the RTS providing a small
15 range of ops for doing so. So, assuming we've got a stable pointer in
16 our hand in C, we can jump into the Haskell world and evaluate a callback
17 procedure, say. This works OK in some cases where callbacks are used, but
18 does require the external code to know about stable pointers and how to deal
19 with them. We'd like to hide the Haskell-nature of a callback and have it
20 be invoked just like any other C function pointer. 
21
22 Enter adjustor thunks. An adjustor thunk is a little piece of code
23 that's generated on-the-fly (one per Haskell closure being exported)
24 that, when entered using some 'universal' calling convention (e.g., the
25 C calling convention on platform X), pushes an implicit stable pointer
26 (to the Haskell callback) before calling another (static) C function stub
27 which takes care of entering the Haskell code via its stable pointer.
28
29 An adjustor thunk is allocated on the C heap, and is called from within
30 Haskell just before handing out the function pointer to the Haskell (IO)
31 action. User code should never have to invoke it explicitly.
32
33 An adjustor thunk differs from a C function pointer in one respect: when
34 the code is through with it, it has to be freed in order to release Haskell
35 and C resources. Failure to do so result in memory leaks on both the C and
36 Haskell side.
37
38 */
39 #include "Rts.h"
40 #include "RtsUtils.h"
41 #include "RtsFlags.h"
42
43 /* Heavily arch-specific, I'm afraid.. */
44 #if defined(i386_TARGET_ARCH)
45 void*
46 createAdjustor(int cconv, StgStablePtr hptr, StgFunPtr wptr)
47 {
48   void *adjustor;
49   unsigned char* adj_code;
50   size_t sizeof_adjustor;
51
52   if (cconv == 0) { /* the adjustor will be _stdcall'ed */
53
54     /* Magic constant computed by inspecting the code length of
55        the following assembly language snippet
56        (offset and machine code prefixed):
57
58      <0>:       58                popl   %eax              # temp. remove ret addr..
59      <1>:       68 fd fc fe fa    pushl  0xfafefcfd        # constant is large enough to
60                                                            # hold a StgStablePtr
61      <6>:       50                pushl  %eax              # put back ret. addr
62      <7>:       b8 fa ef ff 00    movl   $0x00ffeffa, %eax # load up wptr
63      <c>:       ff e0             jmp    %eax              # and jump to it.
64                 # the callee cleans up the stack
65     */
66     sizeof_adjustor = 14*sizeof(char);
67
68     if ((adjustor = stgMallocBytes(sizeof_adjustor,"createAdjustor")) == NULL) {
69         return NULL;
70     }
71
72     adj_code       = (unsigned char*)adjustor;
73     adj_code[0x00] = (unsigned char)0x58;  /* popl %eax  */
74
75     adj_code[0x01] = (unsigned char)0x68;  /* pushl hptr (which is a dword immediate ) */
76     *((StgStablePtr*)(adj_code + 0x02)) = (StgStablePtr)hptr;
77
78     adj_code[0x06] = (unsigned char)0x50; /* pushl %eax */
79
80     adj_code[0x07] = (unsigned char)0xb8; /* movl  $wptr, %eax */
81     *((StgFunPtr*)(adj_code + 0x08)) = (StgFunPtr)wptr;
82
83     adj_code[0x0c] = (unsigned char)0xff; /* jmp %eax */
84     adj_code[0x0d] = (unsigned char)0xe0;
85
86
87   } else { /* the adjustor will be _ccall'ed */
88
89   /* Magic constant computed by inspecting the code length of
90      the following assembly language snippet
91      (offset and machine code prefixed):
92
93   <00>: 68 ef be ad de    pushl  $0xdeadbeef       # constant is large enough to
94                                                    # hold a StgStablePtr
95   <05>: b8 fa ef ff 00    movl   $0x00ffeffa, %eax # load up wptr
96   <0a>: ff d0             call   %eax              # and call it.
97   <0c>: 83 c4 04          addl   $0x4,%esp         # remove stable pointer.
98   <0f>: c3                ret                      # return to where you came from.
99
100     The ccall'ing version is a tad different, passing in the return
101     address of the caller to the auto-generated C stub (which enters
102     via the stable pointer.) (The auto-generated C stub is on this
103     game, don't worry :-)
104
105     The adjustor makes the assumption that any return value
106     coming back from the C stub is not stored on the stack.
107     That's (thankfully) the case here with the restricted set of 
108     return types that we support.
109   */
110     sizeof_adjustor = 16*sizeof(char);
111
112     if ((adjustor = stgMallocBytes(sizeof_adjustor,"createAdjustor")) == NULL) {
113         return NULL;
114     }
115
116     adj_code       = (unsigned char*)adjustor;
117
118     adj_code[0x00] = (unsigned char)0x68;  /* pushl hptr (which is a dword immediate ) */
119     *((StgStablePtr*)(adj_code+0x01)) = (StgStablePtr)hptr;
120
121     adj_code[0x05] = (unsigned char)0xb8;  /* movl  $wptr, %eax */
122     *((StgFunPtr*)(adj_code + 0x06)) = (StgFunPtr)wptr;
123     
124     adj_code[0x0a] = (unsigned char)0xff; /* call %eax */
125     adj_code[0x0b] = (unsigned char)0xd0; 
126     
127     adj_code[0x0c] = (unsigned char)0x83; /* addl $0x4, %esp */
128     adj_code[0x0d] = (unsigned char)0xc4; 
129     adj_code[0x0e] = (unsigned char)0x04; 
130
131     adj_code[0x0f] = (unsigned char)0xc3; /* ret */
132
133   }
134
135   /* Have fun! */
136   return ((void*)adjustor);
137 }
138
139 void
140 freeHaskellFunctionPtr(void* ptr)
141 {
142  if ( *(unsigned char*)ptr != 0x68 &&
143       *(unsigned char*)ptr != 0x58 ) {
144    fprintf(stderr, "freeHaskellFunctionPtr: not for me, guv! %p\n", ptr);
145    return;
146  }
147
148  /* Free the stable pointer first..*/
149  if (*(unsigned char*)ptr == 0x68) { /* Aha, a ccall adjustor! */
150     freeStablePointer(*((StgStablePtr*)((unsigned char*)ptr + 0x01)));
151  } else {
152     freeStablePointer(*((StgStablePtr*)((unsigned char*)ptr + 0x02)));
153  }    
154  *((unsigned char*)ptr) = '\0';
155
156  free(ptr);
157 }
158
159 #endif /* i386_TARGET_ARCH */
160