vm: more use of ctx->replace().

db4
John Benediktsson 2013-03-22 09:17:02 -07:00
parent 49a7d37dcd
commit 5be15d7e3f
6 changed files with 44 additions and 38 deletions

View File

@ -84,7 +84,7 @@ void factor_vm::primitive_displaced_alien()
if the object is a byte array, as a sanity check. */ if the object is a byte array, as a sanity check. */
void factor_vm::primitive_alien_address() void factor_vm::primitive_alien_address()
{ {
ctx->push(from_unsigned_cell((cell)pinned_alien_offset(ctx->pop()))); ctx->replace(from_unsigned_cell((cell)pinned_alien_offset(ctx->peek())));
} }
/* pop ( alien n ) from datastack, return alien's address plus n */ /* pop ( alien n ) from datastack, return alien's address plus n */
@ -125,7 +125,7 @@ void factor_vm::primitive_dlopen()
void factor_vm::primitive_dlsym() void factor_vm::primitive_dlsym()
{ {
data_root<object> library(ctx->pop(),this); data_root<object> library(ctx->pop(),this);
data_root<byte_array> name(ctx->pop(),this); data_root<byte_array> name(ctx->peek(),this);
name.untag_check(this); name.untag_check(this);
symbol_char *sym = name->data<symbol_char>(); symbol_char *sym = name->data<symbol_char>();
@ -135,12 +135,12 @@ void factor_vm::primitive_dlsym()
dll *d = untag_check<dll>(library.value()); dll *d = untag_check<dll>(library.value());
if(d->handle == NULL) if(d->handle == NULL)
ctx->push(false_object); ctx->replace(false_object);
else else
ctx->push(allot_alien(ffi_dlsym(d,sym))); ctx->replace(allot_alien(ffi_dlsym(d,sym)));
} }
else else
ctx->push(allot_alien(ffi_dlsym(NULL,sym))); ctx->replace(allot_alien(ffi_dlsym(NULL,sym)));
} }
/* Allocates memory */ /* Allocates memory */
@ -148,7 +148,7 @@ void factor_vm::primitive_dlsym()
void factor_vm::primitive_dlsym_raw() void factor_vm::primitive_dlsym_raw()
{ {
data_root<object> library(ctx->pop(),this); data_root<object> library(ctx->pop(),this);
data_root<byte_array> name(ctx->pop(),this); data_root<byte_array> name(ctx->peek(),this);
name.untag_check(this); name.untag_check(this);
symbol_char *sym = name->data<symbol_char>(); symbol_char *sym = name->data<symbol_char>();
@ -158,12 +158,12 @@ void factor_vm::primitive_dlsym_raw()
dll *d = untag_check<dll>(library.value()); dll *d = untag_check<dll>(library.value());
if(d->handle == NULL) if(d->handle == NULL)
ctx->push(false_object); ctx->replace(false_object);
else else
ctx->push(allot_alien(ffi_dlsym_raw(d,sym))); ctx->replace(allot_alien(ffi_dlsym_raw(d,sym)));
} }
else else
ctx->push(allot_alien(ffi_dlsym_raw(NULL,sym))); ctx->replace(allot_alien(ffi_dlsym_raw(NULL,sym)));
} }
/* close a native library handle */ /* close a native library handle */
@ -176,11 +176,11 @@ void factor_vm::primitive_dlclose()
void factor_vm::primitive_dll_validp() void factor_vm::primitive_dll_validp()
{ {
cell library = ctx->pop(); cell library = ctx->peek();
if(to_boolean(library)) if(to_boolean(library))
ctx->push(tag_boolean(untag_check<dll>(library)->handle != NULL)); ctx->replace(tag_boolean(untag_check<dll>(library)->handle != NULL));
else else
ctx->push(true_object); ctx->replace(true_object);
} }
/* gets the address of an object representing a C pointer */ /* gets the address of an object representing a C pointer */

View File

@ -50,8 +50,8 @@ void factor_vm::primitive_callstack()
void factor_vm::primitive_callstack_for() void factor_vm::primitive_callstack_for()
{ {
context *other_ctx = (context *)pinned_alien_offset(ctx->pop()); context *other_ctx = (context *)pinned_alien_offset(ctx->peek());
ctx->push(capture_callstack(other_ctx)); ctx->replace(capture_callstack(other_ctx));
} }
void *factor_vm::frame_predecessor(void *frame_top) void *factor_vm::frame_predecessor(void *frame_top)
@ -86,7 +86,7 @@ struct stack_frame_in_array { cell cells[3]; };
void factor_vm::primitive_callstack_to_array() void factor_vm::primitive_callstack_to_array()
{ {
data_root<callstack> callstack(ctx->pop(),this); data_root<callstack> callstack(ctx->peek(),this);
stack_frame_accumulator accum(this); stack_frame_accumulator accum(this);
iterate_callstack_object(callstack.untagged(),accum); iterate_callstack_object(callstack.untagged(),accum);
@ -98,7 +98,7 @@ void factor_vm::primitive_callstack_to_array()
accum.frames.trim(); accum.frames.trim();
ctx->push(accum.frames.elements.value()); ctx->replace(accum.frames.elements.value());
} }
@ -106,18 +106,18 @@ void factor_vm::primitive_callstack_to_array()
Used by the single stepper. */ Used by the single stepper. */
void factor_vm::primitive_innermost_stack_frame_executing() void factor_vm::primitive_innermost_stack_frame_executing()
{ {
callstack *stack = untag_check<callstack>(ctx->pop()); callstack *stack = untag_check<callstack>(ctx->peek());
void *frame = stack->top(); void *frame = stack->top();
void *addr = frame_return_address(frame); void *addr = frame_return_address(frame);
ctx->push(code->code_block_for_address((cell)addr)->owner_quot()); ctx->replace(code->code_block_for_address((cell)addr)->owner_quot());
} }
void factor_vm::primitive_innermost_stack_frame_scan() void factor_vm::primitive_innermost_stack_frame_scan()
{ {
callstack *stack = untag_check<callstack>(ctx->pop()); callstack *stack = untag_check<callstack>(ctx->peek());
void *frame = stack->top(); void *frame = stack->top();
void *addr = frame_return_address(frame); void *addr = frame_return_address(frame);
ctx->push(code->code_block_for_address((cell)addr)->scan(this,addr)); ctx->replace(code->code_block_for_address((cell)addr)->scan(this,addr));
} }
void factor_vm::primitive_set_innermost_stack_frame_quot() void factor_vm::primitive_set_innermost_stack_frame_quot()

View File

@ -233,8 +233,8 @@ void factor_vm::primitive_set_context_object()
void factor_vm::primitive_context_object_for() void factor_vm::primitive_context_object_for()
{ {
context *other_ctx = (context *)pinned_alien_offset(ctx->pop()); context *other_ctx = (context *)pinned_alien_offset(ctx->pop());
fixnum n = untag_fixnum(ctx->pop()); fixnum n = untag_fixnum(ctx->peek());
ctx->push(other_ctx->context_objects[n]); ctx->replace(other_ctx->context_objects[n]);
} }
/* Allocates memory */ /* Allocates memory */
@ -271,8 +271,8 @@ void factor_vm::primitive_datastack()
void factor_vm::primitive_datastack_for() void factor_vm::primitive_datastack_for()
{ {
context *other_ctx = (context *)pinned_alien_offset(ctx->pop()); context *other_ctx = (context *)pinned_alien_offset(ctx->peek());
ctx->push(datastack_to_array(other_ctx)); ctx->replace(datastack_to_array(other_ctx));
} }
cell factor_vm::retainstack_to_array(context *ctx) cell factor_vm::retainstack_to_array(context *ctx)
@ -294,8 +294,8 @@ void factor_vm::primitive_retainstack()
void factor_vm::primitive_retainstack_for() void factor_vm::primitive_retainstack_for()
{ {
context *other_ctx = (context *)pinned_alien_offset(ctx->pop()); context *other_ctx = (context *)pinned_alien_offset(ctx->peek());
ctx->push(retainstack_to_array(other_ctx)); ctx->replace(retainstack_to_array(other_ctx));
} }
/* returns pointer to top of stack */ /* returns pointer to top of stack */

View File

@ -182,37 +182,42 @@ FILE *factor_vm::pop_file_handle()
return (FILE *)alien_offset(ctx->pop()); return (FILE *)alien_offset(ctx->pop());
} }
FILE *factor_vm::peek_file_handle()
{
return (FILE *)alien_offset(ctx->peek());
}
void factor_vm::primitive_fgetc() void factor_vm::primitive_fgetc()
{ {
FILE *file = pop_file_handle(); FILE *file = peek_file_handle();
int c = safe_fgetc(file); int c = safe_fgetc(file);
if(c == EOF && feof(file)) if(c == EOF && feof(file))
{ {
clearerr(file); clearerr(file);
ctx->push(false_object); ctx->replace(false_object);
} }
else else
ctx->push(tag_fixnum(c)); ctx->replace(tag_fixnum(c));
} }
/* Allocates memory */ /* Allocates memory */
void factor_vm::primitive_fread() void factor_vm::primitive_fread()
{ {
FILE *file = pop_file_handle(); FILE *file = pop_file_handle();
void *buf = (void*)alien_offset(ctx->pop()); void *buf = (void*)alien_offset(ctx->peek());
fixnum size = unbox_array_size(); fixnum size = unbox_array_size();
if(size == 0) if(size == 0)
{ {
ctx->push(from_unsigned_cell(0)); ctx->replace(from_unsigned_cell(0));
return; return;
} }
size_t c = safe_fread(buf,1,size,file); size_t c = safe_fread(buf,1,size,file);
if(c == 0 || feof(file)) if(c == 0 || feof(file))
clearerr(file); clearerr(file);
ctx->push(from_unsigned_cell(c)); ctx->replace(from_unsigned_cell(c));
} }
void factor_vm::primitive_fputc() void factor_vm::primitive_fputc()
@ -238,8 +243,8 @@ void factor_vm::primitive_fwrite()
void factor_vm::primitive_ftell() void factor_vm::primitive_ftell()
{ {
FILE *file = pop_file_handle(); FILE *file = peek_file_handle();
ctx->push(from_signed_8(safe_ftell(file))); ctx->replace(from_signed_8(safe_ftell(file)));
} }
void factor_vm::primitive_fseek() void factor_vm::primitive_fseek()

View File

@ -219,8 +219,8 @@ void factor_vm::primitive_bignum_not()
void factor_vm::primitive_bignum_bitp() void factor_vm::primitive_bignum_bitp()
{ {
int bit = (int)to_fixnum(ctx->pop()); int bit = (int)to_fixnum(ctx->pop());
bignum *x = untag<bignum>(ctx->pop()); bignum *x = untag<bignum>(ctx->peek());
ctx->push(tag_boolean(bignum_logbitp(bit,x))); ctx->replace(tag_boolean(bignum_logbitp(bit,x)));
} }
void factor_vm::primitive_bignum_log2() void factor_vm::primitive_bignum_log2()
@ -258,9 +258,9 @@ void factor_vm::primitive_format_float()
{ {
byte_array *array = allot_byte_array(100); byte_array *array = allot_byte_array(100);
char *format = alien_offset(ctx->pop()); char *format = alien_offset(ctx->pop());
double value = untag_float_check(ctx->pop()); double value = untag_float_check(ctx->peek());
SNPRINTF(array->data<char>(),99,format,value); SNPRINTF(array->data<char>(),99,format,value);
ctx->push(tag<byte_array>(array)); ctx->replace(tag<byte_array>(array));
} }
#define POP_FLOATS(x,y) \ #define POP_FLOATS(x,y) \

View File

@ -556,6 +556,7 @@ struct factor_vm
void safe_fclose(FILE *stream); void safe_fclose(FILE *stream);
void primitive_fopen(); void primitive_fopen();
FILE *pop_file_handle(); FILE *pop_file_handle();
FILE *peek_file_handle();
void primitive_fgetc(); void primitive_fgetc();
void primitive_fread(); void primitive_fread();
void primitive_fputc(); void primitive_fputc();