Debian Codegraph @

utils/xml2pmx/xml2pmx-src/dynlink.c

raw
1 /*
2 * dynlink.c
3 *
4 * This file is part of the Oxford Oberon-2 compiler
5 * Copyright (c) 2006--2016 J. M. Spivey
6 * All rights reserved
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 Initially, the procedure descriptor for each such primitive has the
33 trap handler |dltrap| as its interpreter, and the CP_CODE field of
34 the descriptor points to the name of the primitive as a string.
35 When the primitive is first called, the |dltrap| primitive looks up
36 the symbol and binds the primitive to its value for future use.
37 Finally, it calls the newly-loaded primitive to complete the first
38 call.
39
40 Function |load_lib| loads a dynamic library. Each Oberon module that
41 links to a dynamic library should call |DynLink.Load("path")|
42 in its initialization part.
43 */
44
45 #include "obx.h"
46 #include <string.h>
47 #include <stdlib.h>
48
49 #ifdef DYNLINK
50
51 #ifndef __USE_GNU
52 #define __USE_GNU
53 #endif
54 #include <dlfcn.h>
55
56 #ifdef USE_FFI
57 #include <ffi.h>
58 #endif
59
60 void load_lib(char *fname) {
61 char buf[128];
62
63 /* If the library name starts with '@', look in the OBC lib directory
64 and append the extension ".so" or ".dylib" au chois */
65 if (fname[0] == '@') {
66 char *dir = getenv("OBC_LIB");
67 if (dir == NULL) dir = libpath;
68 if (dir == NULL) panic("no runtime library");
69 strcpy(buf, dir);
70 strcat(buf, "/");
71 strcat(buf, fname+1);
72 strcat(buf, DLEXT);
73 fname = buf;
74 }
75
76 /* Load the library */
77 if (dlopen(fname, RTLD_LAZY|RTLD_GLOBAL) == NULL)
78 panic(dlerror());
79 }
80
81 #ifdef USE_FFI
82 #define MAXP 16
83
84 typedef struct {
85 void (*fun)(void);
86 ffi_cif cif;
87 } wrapper;
88
89 static ffi_type *ffi_decode(char c) {
90 switch (c) {
91 case 'C':
92 case 'S':
93 case 'I':
94 return &ffi_type_sint32;
95 case 'L':
96 return &ffi_type_sint64;
97 case 'F':
98 return &ffi_type_float;
99 case 'D':
100 return &ffi_type_double;
101 case 'P':
102 case 'Q':
103 case 'X':
104 return &ffi_type_pointer;
105 case 'V':
106 return &ffi_type_void;
107 default:
108 panic("Bad type %c", c);
109 return NULL;
110 }
111 }
112
113 value *dlstub(value *bp) {
114 value *cp = valptr(bp[CP]);
115 char *tstring = pointer(cp[CP_CODE]);
116 value *sp = bp;
117
118 ffi_raw avals[MAXP], rval[2];
119 int i, p = 0, q = 0;
120 double d; longint z;
121
122 FPINIT;
123
124 for (i = 0; tstring[i+1] != '\0'; i++) {
125 switch (tstring[i+1]) {
126 case 'C':
127 avals[q].sint = align_byte(bp[HEAD+p].i);
128 p += 1; q += 1; break;
129 case 'S':
130 avals[q].sint = align_short(bp[HEAD+p].i);
131 p += 1; q += 1; break;
132 case 'I':
133 avals[q].sint = bp[HEAD+p].i;
134 p += 1; q += 1; break;
135 case 'L':
136 z = get_long(&bp[HEAD+p]);
137 memcpy(avals[q].data, &z, sizeof(longint));
138 p += 2; q += sizeof(longint)/sizeof(ffi_raw); break;
139 case 'F':
140 avals[q].flt = bp[HEAD+p].f;
141 p += 1; q += 1; break;
142 case 'D':
143 d = get_double(&bp[HEAD+p]);
144 memcpy(avals[q].data, &d, sizeof(double));
145 p += 2; q += sizeof(double)/sizeof(ffi_raw); break;
146 case 'P':
147 avals[q].ptr = pointer(bp[HEAD+p]);
148 p += 1; q += 1; break;
149 case 'X':
150 avals[q].ptr = pointer(bp[HEAD+p]);
151 p += 2; q += 1; break;
152 case 'Q':
153 avals[q].ptr = ptrcast(uchar, get_long(&bp[HEAD+p]));
154 p += 2; q += 1; break;
155 #ifdef SPECIALS
156 case 'S':
157 /* Static link for compilers course -- ignored */
158 p += 1; break;
159 #endif
160 default:
161 panic("Bad type 2 %c", tstring[i+1]);
162 }
163 }
164
165 wrapper *w = pointer(cp[CP_CONST]);
166 ffi_raw_call(&w->cif, w->fun, rval, avals);
167
168 switch (tstring[0]) {
169 case 'C':
170 case 'S':
171 case 'I':
172 (*--sp).i = rval->sint;
173 break;
174 case 'L':
175 memcpy(&z, rval, sizeof(longint));
176 sp -= 2;
177 put_long(sp, z);
178 break;
179 case 'F':
180 (*--sp).f = rval->flt;
181 break;
182 case 'D':
183 memcpy(&d, rval, sizeof(double));
184 sp -= 2;
185 put_double(sp, d);
186 break;
187 case 'P':
188 (*--sp).a = rval->uint;
189 break;
190 case 'Q':
191 sp -= 2;
192 put_long(sp, (ptrtype) rval->ptr);
193 break;
194 case 'V':
195 break;
196 default:
197 panic("Bad type 3");
198 }
199
200 return sp;
201 }
202 #endif
203
204 primitive *find_prim(char *name) {
205 return (primitive *) dlsym(RTLD_DEFAULT, name);
206 }
207
208 #else
209
210 void load_lib(char *fname) {
211 }
212
213 primitive *find_prim(char *name) {
214 int i;
215
216 for (i = 0; primtab[i].p_name != NULL; i++) {
217 if (strcmp(name, primtab[i].p_name) == 0)
218 return primtab[i].p_prim;
219 }
220
221 return NULL;
222 }
223
224 #endif
225
226 value *dltrap(value *bp) {
227 value *cp = valptr(bp[CP]);
228 char *tstring = pointer(cp[CP_CODE]);
229 char *name = tstring + strlen(tstring) + 1;
230 primitive *prim = NULL;
231
232 if (*name == '=') name++;
233
234 if (tstring[0] == '*')
235 prim = find_prim(name);
236 else {
237 /* Look for a static wrapper */
238 char primname[32];
239 sprintf(primname, "P_%s", name);
240 prim = find_prim(primname);
241 }
242
243 if (prim != NULL) {
244 cp[CP_PRIM].a = wrap_prim(prim);
245 return (*prim)(bp);
246 }
247
248 #ifdef DYNLINK
249 #ifdef USE_FFI
250 /* Build a wrapper with FFI */
251 void (*fun)(void) = (void(*)(void)) dlsym(RTLD_DEFAULT, name);
252
253 if (fun != NULL && tstring[0] != '*') {
254 int np = strlen(tstring)-1;
255 ffi_type *rtype = ffi_decode(tstring[0]);
256 ffi_type **atypes = scratch_alloc_atomic(np * sizeof(ffi_type *));
257 for (int i = 0; tstring[i+1] != '\0'; i++)
258 atypes[i] = ffi_decode(tstring[i+1]);
259
260 word a = virtual_alloc_atomic(sizeof(wrapper));
261 wrapper *w = ptrcast(wrapper, a);
262 w->fun = fun;
263 ffi_prep_cif(&w->cif, FFI_DEFAULT_ABI, np, rtype, atypes);
264
265 cp[CP_PRIM].a = dynstub;
266 cp[CP_CONST].a = a;
267
268 return dlstub(bp);
269 }
270 #endif
271 #endif
272
273 panic("Couldn't find primitive %s", name);
274 return NULL;
275 }