factor/vm/alien.cpp

245 lines
5.8 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2009-05-04 02:46:13 -04:00
namespace factor
{
2009-05-02 05:04:19 -04:00
/* gets the address of an object representing a C pointer, with the
intention of storing the pointer across code which may potentially GC. */
2009-09-23 14:05:46 -04:00
char *factor_vm::pinned_alien_offset(cell obj)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
switch(tagged<object>(obj).type())
2009-05-02 05:04:19 -04:00
{
case ALIEN_TYPE:
{
alien *ptr = untag<alien>(obj);
if(to_boolean(ptr->expired))
2010-03-27 07:33:28 -04:00
general_error(ERROR_EXPIRED,obj,false_object);
if(to_boolean(ptr->base))
type_error(ALIEN_TYPE,obj);
else
return (char *)ptr->address;
}
2009-05-02 05:04:19 -04:00
case F_TYPE:
return NULL;
default:
2009-05-04 05:50:24 -04:00
type_error(ALIEN_TYPE,obj);
2009-05-02 05:04:19 -04:00
return NULL; /* can't happen */
}
}
VM_C_API char *pinned_alien_offset(cell obj, factor_vm *parent)
{
return parent->pinned_alien_offset(obj);
}
2009-05-02 05:04:19 -04:00
/* make an alien */
2009-09-23 14:05:46 -04:00
cell factor_vm::allot_alien(cell delegate_, cell displacement)
2009-05-02 05:04:19 -04:00
{
if(displacement == 0)
return delegate_;
data_root<object> delegate(delegate_,this);
data_root<alien> new_alien(allot<alien>(sizeof(alien)),this);
2009-05-02 05:04:19 -04:00
if(delegate.type_p(ALIEN_TYPE))
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
tagged<alien> delegate_alien = delegate.as<alien>();
2009-05-02 05:04:19 -04:00
displacement += delegate_alien->displacement;
2009-10-09 04:20:50 -04:00
new_alien->base = delegate_alien->base;
2009-05-02 05:04:19 -04:00
}
else
2009-10-09 04:20:50 -04:00
new_alien->base = delegate.value();
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
new_alien->displacement = displacement;
new_alien->expired = false_object;
new_alien->update_address();
2009-05-02 10:19:09 -04:00
2009-05-04 05:50:24 -04:00
return new_alien.value();
2009-05-02 05:04:19 -04:00
}
cell factor_vm::allot_alien(void *address)
{
return allot_alien(false_object,(cell)address);
}
VM_C_API cell allot_alien(void *address, factor_vm *vm)
{
return vm->allot_alien(address);
}
2009-05-02 05:04:19 -04:00
/* make an alien pointing at an offset of another alien */
void factor_vm::primitive_displaced_alien()
2009-05-02 05:04:19 -04:00
{
cell alien = ctx->pop();
cell displacement = to_cell(ctx->pop());
2009-05-02 05:04:19 -04:00
switch(tagged<object>(alien).type())
2009-05-02 05:04:19 -04:00
{
case BYTE_ARRAY_TYPE:
case ALIEN_TYPE:
case F_TYPE:
ctx->push(allot_alien(alien,displacement));
break;
default:
type_error(ALIEN_TYPE,alien);
break;
2009-05-02 05:04:19 -04:00
}
}
/* address of an object representing a C pointer. Explicitly throw an error
if the object is a byte array, as a sanity check. */
void factor_vm::primitive_alien_address()
2009-05-02 05:04:19 -04:00
{
ctx->push(allot_cell((cell)pinned_alien_offset(ctx->pop())));
2009-05-02 05:04:19 -04:00
}
/* pop ( alien n ) from datastack, return alien's address plus n */
2009-09-23 14:05:46 -04:00
void *factor_vm::alien_pointer()
2009-05-02 05:04:19 -04:00
{
fixnum offset = to_fixnum(ctx->pop());
return alien_offset(ctx->pop()) + offset;
2009-05-02 05:04:19 -04:00
}
/* define words to read/write values at an alien address */
#define DEFINE_ALIEN_ACCESSOR(name,type,from,to) \
VM_C_API void primitive_alien_##name(factor_vm *parent) \
2009-05-02 05:04:19 -04:00
{ \
parent->ctx->push(from(*(type*)(parent->alien_pointer()),parent)); \
2009-05-02 05:04:19 -04:00
} \
VM_C_API void primitive_set_alien_##name(factor_vm *parent) \
2009-05-02 05:04:19 -04:00
{ \
type *ptr = (type *)parent->alien_pointer(); \
type value = (type)to(parent->ctx->pop(),parent); \
2009-05-02 05:04:19 -04:00
*ptr = value; \
}
EACH_ALIEN_PRIMITIVE(DEFINE_ALIEN_ACCESSOR)
2009-05-02 05:04:19 -04:00
/* open a native library and push a handle */
void factor_vm::primitive_dlopen()
2009-05-02 05:04:19 -04:00
{
data_root<byte_array> path(ctx->pop(),this);
2009-08-17 16:37:15 -04:00
path.untag_check(this);
data_root<dll> library(allot<dll>(sizeof(dll)),this);
library->path = path.value();
ffi_dlopen(library.untagged());
ctx->push(library.value());
2009-05-02 05:04:19 -04:00
}
/* look up a symbol in a native library */
void factor_vm::primitive_dlsym()
2009-05-02 05:04:19 -04:00
{
data_root<object> library(ctx->pop(),this);
data_root<byte_array> name(ctx->pop(),this);
2009-08-17 16:37:15 -04:00
name.untag_check(this);
2009-05-05 14:03:24 -04:00
symbol_char *sym = name->data<symbol_char>();
2009-05-02 05:04:19 -04:00
if(to_boolean(library.value()))
2009-05-02 05:04:19 -04:00
{
dll *d = untag_check<dll>(library.value());
2009-05-04 05:50:24 -04:00
if(d->handle == NULL)
ctx->push(false_object);
2009-05-02 05:04:19 -04:00
else
ctx->push(allot_alien(ffi_dlsym(d,sym)));
2009-05-02 05:04:19 -04:00
}
else
ctx->push(allot_alien(ffi_dlsym(NULL,sym)));
2009-05-02 05:04:19 -04:00
}
/* close a native library handle */
void factor_vm::primitive_dlclose()
2009-05-02 05:04:19 -04:00
{
dll *d = untag_check<dll>(ctx->pop());
if(d->handle != NULL)
ffi_dlclose(d);
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_dll_validp()
2009-05-02 05:04:19 -04:00
{
cell library = ctx->pop();
if(to_boolean(library))
ctx->push(tag_boolean(untag_check<dll>(library)->handle != NULL));
2009-05-02 05:04:19 -04:00
else
ctx->push(true_object);
2009-05-02 05:04:19 -04:00
}
/* gets the address of an object representing a C pointer */
2009-09-23 14:05:46 -04:00
char *factor_vm::alien_offset(cell obj)
{
2009-05-04 05:50:24 -04:00
switch(tagged<object>(obj).type())
{
case BYTE_ARRAY_TYPE:
2009-05-04 05:50:24 -04:00
return untag<byte_array>(obj)->data<char>();
case ALIEN_TYPE:
return (char *)untag<alien>(obj)->address;
case F_TYPE:
return NULL;
default:
2009-05-04 05:50:24 -04:00
type_error(ALIEN_TYPE,obj);
return NULL; /* can't happen */
}
}
VM_C_API char *alien_offset(cell obj, factor_vm *parent)
2009-08-17 16:37:08 -04:00
{
return parent->alien_offset(obj);
2009-08-17 16:37:08 -04:00
}
/* For FFI calls passing structs by value. Cannot allocate */
2009-09-23 14:05:46 -04:00
void factor_vm::to_value_struct(cell src, void *dest, cell size)
{
memcpy(dest,alien_offset(src),size);
}
VM_C_API void to_value_struct(cell src, void *dest, cell size, factor_vm *parent)
2009-08-17 16:37:08 -04:00
{
return parent->to_value_struct(src,dest,size);
2009-08-17 16:37:08 -04:00
}
/* For FFI callbacks receiving structs by value */
cell factor_vm::from_value_struct(void *src, cell size)
{
2009-05-04 05:50:24 -04:00
byte_array *bytes = allot_byte_array(size);
memcpy(bytes->data<void>(),src,size);
return tag<byte_array>(bytes);
}
VM_C_API cell from_value_struct(void *src, cell size, factor_vm *parent)
2009-08-17 16:37:08 -04:00
{
return parent->from_value_struct(src,size);
2009-08-17 16:37:08 -04:00
}
/* On some x86 OSes, structs <= 8 bytes are returned in registers. */
cell factor_vm::from_small_struct(cell x, cell y, cell size)
{
2009-05-04 05:50:24 -04:00
cell data[2];
data[0] = x;
data[1] = y;
return from_value_struct(data,size);
}
VM_C_API cell from_small_struct(cell x, cell y, cell size, factor_vm *parent)
2009-08-17 16:37:08 -04:00
{
return parent->from_small_struct(x,y,size);
2009-08-17 16:37:08 -04:00
}
/* On OS X/PPC, complex numbers are returned in registers. */
cell factor_vm::from_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size)
{
2009-05-04 05:50:24 -04:00
cell data[4];
data[0] = x1;
data[1] = x2;
data[2] = x3;
data[3] = x4;
return from_value_struct(data,size);
}
2009-05-04 02:46:13 -04:00
VM_C_API cell from_medium_struct(cell x1, cell x2, cell x3, cell x4, cell size, factor_vm *parent)
2009-08-17 16:37:08 -04:00
{
return parent->from_medium_struct(x1, x2, x3, x4, size);
2009-08-17 16:37:08 -04:00
}
2009-05-04 02:46:13 -04:00
}