VM: fixes to make some if-else statements read better

locals-and-roots
Björn Lindqvist 2016-03-31 00:06:35 +02:00
parent e0a40e4f96
commit 10ac4da0f6
6 changed files with 40 additions and 44 deletions

View File

@ -82,7 +82,7 @@ struct code_block {
byte_array* rels = untag<byte_array>(relocation); byte_array* rels = untag<byte_array>(relocation);
cell index = 0; cell index = 0;
cell length = (rels->capacity >> TAG_BITS) / sizeof(relocation_entry); cell length = untag_fixnum(rels->capacity) / sizeof(relocation_entry);
for (cell i = 0; i < length; i++) { for (cell i = 0; i < length; i++) {
relocation_entry rel = rels->data<relocation_entry>()[i]; relocation_entry rel = rels->data<relocation_entry>()[i];

View File

@ -20,8 +20,7 @@ cell factor_vm::search_lookup_hash(cell table, cell klass, cell hashcode) {
cell bucket = array_nth(buckets, hashcode & (array_capacity(buckets) - 1)); cell bucket = array_nth(buckets, hashcode & (array_capacity(buckets) - 1));
if (TAG(bucket) == ARRAY_TYPE) if (TAG(bucket) == ARRAY_TYPE)
return search_lookup_alist(bucket, klass); return search_lookup_alist(bucket, klass);
else return bucket;
return bucket;
} }
cell factor_vm::nth_superclass(tuple_layout* layout, fixnum echelon) { cell factor_vm::nth_superclass(tuple_layout* layout, fixnum echelon) {
@ -69,10 +68,9 @@ cell factor_vm::lookup_method(cell obj, cell methods) {
if (tag == TUPLE_TYPE) { if (tag == TUPLE_TYPE) {
if (TAG(method) == ARRAY_TYPE) if (TAG(method) == ARRAY_TYPE)
return lookup_tuple_method(obj, method); return lookup_tuple_method(obj, method);
else
return method;
} else
return method; return method;
}
return method;
} }
void factor_vm::primitive_lookup_method() { void factor_vm::primitive_lookup_method() {
@ -85,8 +83,7 @@ cell factor_vm::object_class(cell obj) {
cell tag = TAG(obj); cell tag = TAG(obj);
if (tag == TUPLE_TYPE) if (tag == TUPLE_TYPE)
return untag<tuple>(obj)->layout; return untag<tuple>(obj)->layout;
else return tag_fixnum(tag);
return tag_fixnum(tag);
} }
cell factor_vm::method_cache_hashcode(cell klass, array* array) { cell factor_vm::method_cache_hashcode(cell klass, array* array) {

View File

@ -127,24 +127,24 @@ char *threadsafe_strerror(int errnum) {
} }
FILE* factor_vm::open_image(vm_parameters* p) { FILE* factor_vm::open_image(vm_parameters* p) {
if (p->embedded_image) { if (!p->embedded_image)
FILE* file = OPEN_READ(p->executable_path);
if (file == NULL) {
std::cout << "Cannot open embedded image" << std::endl;
char *msg = threadsafe_strerror(errno);
std::cout << "strerror:1: " << msg << std::endl;
free(msg);
exit(1);
}
embedded_image_footer footer;
if (!read_embedded_image_footer(file, &footer)) {
std::cout << "No embedded image" << std::endl;
exit(1);
}
safe_fseek(file, (off_t)footer.image_offset, SEEK_SET);
return file;
} else
return OPEN_READ(p->image_path); return OPEN_READ(p->image_path);
FILE* file = OPEN_READ(p->executable_path);
if (file == NULL) {
std::cout << "Cannot open embedded image" << std::endl;
char *msg = threadsafe_strerror(errno);
std::cout << "strerror:1: " << msg << std::endl;
free(msg);
exit(1);
}
embedded_image_footer footer;
if (!read_embedded_image_footer(file, &footer)) {
std::cout << "No embedded image" << std::endl;
exit(1);
}
safe_fseek(file, (off_t)footer.image_offset, SEEK_SET);
return file;
} }
/* Read an image file from disk, only done once during startup */ /* Read an image file from disk, only done once during startup */
@ -209,9 +209,11 @@ bool factor_vm::save_image(const vm_char* saving_filename,
return false; return false;
if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1) if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1)
return false; return false;
if (safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1) if (h.data_size > 0 &&
safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1)
return false; return false;
if (safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1) if (h.code_size > 0 &&
safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1)
return false; return false;
if (raw_fclose(file) == -1) if (raw_fclose(file) == -1)
return false; return false;
@ -244,6 +246,9 @@ void factor_vm::primitive_save_image() {
get volatile data saved in the image. */ get volatile data saved in the image. */
active_contexts.clear(); active_contexts.clear();
code->uninitialized_blocks.clear(); code->uninitialized_blocks.clear();
/* I think clearing the callback heap should be fine too. */
callbacks->allocator->initial_free_list(0);
} }
/* do a full GC to push everything remaining into tenured space */ /* do a full GC to push everything remaining into tenured space */

View File

@ -9,16 +9,14 @@ static const fixnum array_size_max = ((cell)1 << (WORD_SIZE - TAG_BITS - 2));
inline cell factor_vm::from_signed_cell(fixnum x) { inline cell factor_vm::from_signed_cell(fixnum x) {
if (x < fixnum_min || x > fixnum_max) if (x < fixnum_min || x > fixnum_max)
return tag<bignum>(fixnum_to_bignum(x)); return tag<bignum>(fixnum_to_bignum(x));
else return tag_fixnum(x);
return tag_fixnum(x);
} }
/* Allocates memory */ /* Allocates memory */
inline cell factor_vm::from_unsigned_cell(cell x) { inline cell factor_vm::from_unsigned_cell(cell x) {
if (x > (cell)fixnum_max) if (x > (cell)fixnum_max)
return tag<bignum>(cell_to_bignum(x)); return tag<bignum>(cell_to_bignum(x));
else return tag_fixnum(x);
return tag_fixnum(x);
} }
/* Allocates memory */ /* Allocates memory */

View File

@ -47,13 +47,11 @@ cell factor_vm::clone_object(cell obj_) {
if (immediate_p(obj.value())) if (immediate_p(obj.value()))
return obj.value(); return obj.value();
else { cell size = object_size(obj.value());
cell size = object_size(obj.value()); object* new_obj = allot_object(obj.type(), size);
object* new_obj = allot_object(obj.type(), size); memcpy(new_obj, obj.untagged(), size);
memcpy(new_obj, obj.untagged(), size); new_obj->set_hashcode(0);
new_obj->set_hashcode(0); return tag_dynamic(new_obj);
return tag_dynamic(new_obj);
}
} }
/* Allocates memory */ /* Allocates memory */
@ -63,8 +61,7 @@ void factor_vm::primitive_clone() { ctx->replace(clone_object(ctx->peek())); }
cell factor_vm::object_size(cell tagged) { cell factor_vm::object_size(cell tagged) {
if (immediate_p(tagged)) if (immediate_p(tagged))
return 0; return 0;
else return untag<object>(tagged)->size();
return untag<object>(tagged)->size();
} }
/* Allocates memory */ /* Allocates memory */

View File

@ -155,11 +155,10 @@ void synchronous_signal_handler(int signal, siginfo_t* siginfo, void* uap) {
return; return;
factor_vm* vm = current_vm_p(); factor_vm* vm = current_vm_p();
if (vm) { if (!vm)
vm->signal_number = signal;
vm->dispatch_signal(uap, factor::synchronous_signal_handler_impl);
} else
fatal_error("Foreign thread received signal", signal); fatal_error("Foreign thread received signal", signal);
vm->signal_number = signal;
vm->dispatch_signal(uap, factor::synchronous_signal_handler_impl);
} }
void safe_write_nonblock(int fd, void* data, ssize_t size); void safe_write_nonblock(int fd, void* data, ssize_t size);