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 *factor_vm::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 factor_vm::allot_alien(cell delegate_, cell displacement)
30 gc_root<object> delegate(delegate_,this);
31 gc_root<alien> new_alien(allot<alien>(sizeof(alien)),this);
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 inline void factor_vm::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 PRIMITIVE(displaced_alien)
74 PRIMITIVE_GETVM()->primitive_displaced_alien();
77 /* address of an object representing a C pointer. Explicitly throw an error
78 if the object is a byte array, as a sanity check. */
79 inline void factor_vm::primitive_alien_address()
81 box_unsigned_cell((cell)pinned_alien_offset(dpop()));
84 PRIMITIVE(alien_address)
86 PRIMITIVE_GETVM()->primitive_alien_address();
89 /* pop ( alien n ) from datastack, return alien's address plus n */
90 void *factor_vm::alien_pointer()
92 fixnum offset = to_fixnum(dpop());
93 return unbox_alien() + offset;
96 /* define words to read/write values at an alien address */
97 #define DEFINE_ALIEN_ACCESSOR(name,type,boxer,to) \
98 PRIMITIVE(alien_##name) \
100 PRIMITIVE_GETVM()->boxer(*(type*)PRIMITIVE_GETVM()->alien_pointer()); \
102 PRIMITIVE(set_alien_##name) \
104 type *ptr = (type *)PRIMITIVE_GETVM()->alien_pointer(); \
105 type value = PRIMITIVE_GETVM()->to(dpop()); \
109 DEFINE_ALIEN_ACCESSOR(signed_cell,fixnum,box_signed_cell,to_fixnum)
110 DEFINE_ALIEN_ACCESSOR(unsigned_cell,cell,box_unsigned_cell,to_cell)
111 DEFINE_ALIEN_ACCESSOR(signed_8,s64,box_signed_8,to_signed_8)
112 DEFINE_ALIEN_ACCESSOR(unsigned_8,u64,box_unsigned_8,to_unsigned_8)
113 DEFINE_ALIEN_ACCESSOR(signed_4,s32,box_signed_4,to_fixnum)
114 DEFINE_ALIEN_ACCESSOR(unsigned_4,u32,box_unsigned_4,to_cell)
115 DEFINE_ALIEN_ACCESSOR(signed_2,s16,box_signed_2,to_fixnum)
116 DEFINE_ALIEN_ACCESSOR(unsigned_2,u16,box_unsigned_2,to_cell)
117 DEFINE_ALIEN_ACCESSOR(signed_1,s8,box_signed_1,to_fixnum)
118 DEFINE_ALIEN_ACCESSOR(unsigned_1,u8,box_unsigned_1,to_cell)
119 DEFINE_ALIEN_ACCESSOR(float,float,box_float,to_float)
120 DEFINE_ALIEN_ACCESSOR(double,double,box_double,to_double)
121 DEFINE_ALIEN_ACCESSOR(cell,void *,box_alien,pinned_alien_offset)
123 /* open a native library and push a handle */
124 inline void factor_vm::primitive_dlopen()
126 gc_root<byte_array> path(dpop(),this);
127 path.untag_check(this);
128 gc_root<dll> library(allot<dll>(sizeof(dll)),this);
129 library->path = path.value();
130 ffi_dlopen(library.untagged());
131 dpush(library.value());
136 PRIMITIVE_GETVM()->primitive_dlopen();
139 /* look up a symbol in a native library */
140 inline void factor_vm::primitive_dlsym()
142 gc_root<object> library(dpop(),this);
143 gc_root<byte_array> name(dpop(),this);
144 name.untag_check(this);
146 symbol_char *sym = name->data<symbol_char>();
148 if(library.value() == F)
149 box_alien(ffi_dlsym(NULL,sym));
152 dll *d = untag_check<dll>(library.value());
157 box_alien(ffi_dlsym(d,sym));
163 PRIMITIVE_GETVM()->primitive_dlsym();
166 /* close a native library handle */
167 inline void factor_vm::primitive_dlclose()
169 dll *d = untag_check<dll>(dpop());
176 PRIMITIVE_GETVM()->primitive_dlclose();
179 inline void factor_vm::primitive_dll_validp()
181 cell library = dpop();
185 dpush(untag_check<dll>(library)->dll == NULL ? F : T);
188 PRIMITIVE(dll_validp)
190 PRIMITIVE_GETVM()->primitive_dll_validp();
193 /* gets the address of an object representing a C pointer */
194 char *factor_vm::alien_offset(cell obj)
196 switch(tagged<object>(obj).type())
198 case BYTE_ARRAY_TYPE:
199 return untag<byte_array>(obj)->data<char>();
202 alien *ptr = untag<alien>(obj);
203 if(ptr->expired != F)
204 general_error(ERROR_EXPIRED,obj,F,NULL);
205 return alien_offset(ptr->alien) + ptr->displacement;
210 type_error(ALIEN_TYPE,obj);
211 return NULL; /* can't happen */
215 VM_C_API char *alien_offset(cell obj, factor_vm *myvm)
218 return VM_PTR->alien_offset(obj);
221 /* pop an object representing a C pointer */
222 char *factor_vm::unbox_alien()
224 return alien_offset(dpop());
227 VM_C_API char *unbox_alien(factor_vm *myvm)
230 return VM_PTR->unbox_alien();
233 /* make an alien and push */
234 void factor_vm::box_alien(void *ptr)
239 dpush(allot_alien(F,(cell)ptr));
242 VM_C_API void box_alien(void *ptr, factor_vm *myvm)
245 return VM_PTR->box_alien(ptr);
248 /* for FFI calls passing structs by value */
249 void factor_vm::to_value_struct(cell src, void *dest, cell size)
251 memcpy(dest,alien_offset(src),size);
254 VM_C_API void to_value_struct(cell src, void *dest, cell size, factor_vm *myvm)
257 return VM_PTR->to_value_struct(src,dest,size);
260 /* for FFI callbacks receiving structs by value */
261 void factor_vm::box_value_struct(void *src, cell size)
263 byte_array *bytes = allot_byte_array(size);
264 memcpy(bytes->data<void>(),src,size);
265 dpush(tag<byte_array>(bytes));
268 VM_C_API void box_value_struct(void *src, cell size,factor_vm *myvm)
271 return VM_PTR->box_value_struct(src,size);
274 /* On some x86 OSes, structs <= 8 bytes are returned in registers. */
275 void factor_vm::box_small_struct(cell x, cell y, cell size)
280 box_value_struct(data,size);
283 VM_C_API void box_small_struct(cell x, cell y, cell size, factor_vm *myvm)
286 return VM_PTR->box_small_struct(x,y,size);
289 /* On OS X/PPC, complex numbers are returned in registers. */
290 void factor_vm::box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size)
297 box_value_struct(data,size);
300 VM_C_API void box_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size, factor_vm *myvm)
303 return VM_PTR->box_medium_struct(x1, x2, x3, x4, size);
306 inline void factor_vm::primitive_vm_ptr()
313 PRIMITIVE_GETVM()->primitive_vm_ptr();