6 /* gets the address of an object representing a C pointer, with the
7 intention of storing the pointer across code which may potentially GC. */
8 char *pinned_alien_offset(cell obj)
10 switch(tagged<object>(obj).type())
14 alien *ptr = untag<alien>(obj);
16 general_error(ERROR_EXPIRED,obj,F,NULL);
17 return pinned_alien_offset(ptr->alien) + ptr->displacement;
22 type_error(ALIEN_TYPE,obj);
23 return NULL; /* can't happen */
28 cell allot_alien(cell delegate_, cell displacement)
30 gc_root<object> delegate(delegate_);
31 gc_root<alien> new_alien(allot<alien>(sizeof(alien)));
33 if(delegate.type_p(ALIEN_TYPE))
35 tagged<alien> delegate_alien = delegate.as<alien>();
36 displacement += delegate_alien->displacement;
37 new_alien->alien = delegate_alien->alien;
40 new_alien->alien = delegate.value();
42 new_alien->displacement = displacement;
43 new_alien->expired = F;
45 return new_alien.value();
48 /* make an alien pointing at an offset of another alien */
49 PRIMITIVE(displaced_alien)
52 cell displacement = to_cell(dpop());
54 if(alien == F && displacement == 0)
58 switch(tagged<object>(alien).type())
63 dpush(allot_alien(alien,displacement));
66 type_error(ALIEN_TYPE,alien);
72 /* address of an object representing a C pointer. Explicitly throw an error
73 if the object is a byte array, as a sanity check. */
74 PRIMITIVE(alien_address)
76 box_unsigned_cell((cell)pinned_alien_offset(dpop()));
79 /* pop ( alien n ) from datastack, return alien's address plus n */
80 static void *alien_pointer()
82 fixnum offset = to_fixnum(dpop());
83 return unbox_alien() + offset;
86 /* define words to read/write values at an alien address */
87 #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \
88 PRIMITIVE(alien_##name) \
90 boxer(*(type*)alien_pointer()); \
92 PRIMITIVE(set_alien_##name) \
94 type *ptr = (type *)alien_pointer(); \
95 type value = to(dpop()); \
99 DEFINE_ALIEN_ACCESSOR(signed_cell,fixnum,box_signed_cell,to_fixnum)
100 DEFINE_ALIEN_ACCESSOR(unsigned_cell,cell,box_unsigned_cell,to_cell)
101 DEFINE_ALIEN_ACCESSOR(signed_8,s64,box_signed_8,to_signed_8)
102 DEFINE_ALIEN_ACCESSOR(unsigned_8,u64,box_unsigned_8,to_unsigned_8)
103 DEFINE_ALIEN_ACCESSOR(signed_4,s32,box_signed_4,to_fixnum)
104 DEFINE_ALIEN_ACCESSOR(unsigned_4,u32,box_unsigned_4,to_cell)
105 DEFINE_ALIEN_ACCESSOR(signed_2,s16,box_signed_2,to_fixnum)
106 DEFINE_ALIEN_ACCESSOR(unsigned_2,u16,box_unsigned_2,to_cell)
107 DEFINE_ALIEN_ACCESSOR(signed_1,s8,box_signed_1,to_fixnum)
108 DEFINE_ALIEN_ACCESSOR(unsigned_1,u8,box_unsigned_1,to_cell)
109 DEFINE_ALIEN_ACCESSOR(float,float,box_float,to_float)
110 DEFINE_ALIEN_ACCESSOR(double,double,box_double,to_double)
111 DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset)
113 /* open a native library and push a handle */
116 gc_root<byte_array> path(dpop());
118 gc_root<dll> library(allot<dll>(sizeof(dll)));
119 library->path = path.value();
120 ffi_dlopen(library.untagged());
121 dpush(library.value());
124 /* look up a symbol in a native library */
127 gc_root<object> library(dpop());
128 gc_root<byte_array> name(dpop());
131 symbol_char *sym = name->data<symbol_char>();
133 if(library.value() == F)
134 box_alien(ffi_dlsym(NULL,sym));
137 dll *d = untag_check<dll>(library.value());
142 box_alien(ffi_dlsym(d,sym));
146 /* close a native library handle */
149 dll *d = untag_check<dll>(dpop());
154 PRIMITIVE(dll_validp)
156 cell library = dpop();
160 dpush(untag_check<dll>(library)->dll == NULL ? F : T);
163 /* gets the address of an object representing a C pointer */
164 VM_C_API char *alien_offset(cell obj)
166 switch(tagged<object>(obj).type())
168 case BYTE_ARRAY_TYPE:
169 return untag<byte_array>(obj)->data<char>();
172 alien *ptr = untag<alien>(obj);
173 if(ptr->expired != F)
174 general_error(ERROR_EXPIRED,obj,F,NULL);
175 return alien_offset(ptr->alien) + ptr->displacement;
180 type_error(ALIEN_TYPE,obj);
181 return NULL; /* can't happen */
185 /* pop an object representing a C pointer */
186 VM_C_API char *unbox_alien()
188 return alien_offset(dpop());
191 /* make an alien and push */
192 VM_C_API void box_alien(void *ptr)
197 dpush(allot_alien(F,(cell)ptr));
200 /* for FFI calls passing structs by value */
201 VM_C_API void to_value_struct(cell src, void *dest, cell size)
203 memcpy(dest,alien_offset(src),size);
206 /* for FFI callbacks receiving structs by value */
207 VM_C_API void box_value_struct(void *src, cell size)
209 byte_array *bytes = allot_byte_array(size);
210 memcpy(bytes->data<void>(),src,size);
211 dpush(tag<byte_array>(bytes));
214 /* On some x86 OSes, structs <= 8 bytes are returned in registers. */
215 VM_C_API void box_small_struct(cell x, cell y, cell size)
220 box_value_struct(data,size);
223 /* On OS X/PPC, complex numbers are returned in registers. */
224 VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size)
231 box_value_struct(data,size);