diff --git a/vm/alien.cpp b/vm/alien.cpp index 92eb85c0c3..894ba2d4c8 100644 --- a/vm/alien.cpp +++ b/vm/alien.cpp @@ -32,7 +32,7 @@ cell factor_vm::allot_alien(cell delegate_, cell displacement) { data_root delegate(delegate_, this); data_root new_alien(allot(sizeof(alien)), this); - if (delegate.type() == ALIEN_TYPE) { + if (TAG(delegate_) == ALIEN_TYPE) { tagged delegate_alien = delegate.as(); displacement += delegate_alien->displacement; new_alien->base = delegate_alien->base; @@ -99,7 +99,7 @@ EACH_ALIEN_PRIMITIVE(DEFINE_ALIEN_ACCESSOR) // Allocates memory void factor_vm::primitive_dlopen() { data_root path(ctx->pop(), this); - path.untag_check(this); + check_tagged(path); data_root library(allot(sizeof(dll)), this); library->path = path.value(); ffi_dlopen(library.untagged()); @@ -111,7 +111,7 @@ void factor_vm::primitive_dlopen() { void factor_vm::primitive_dlsym() { data_root library(ctx->pop(), this); data_root name(ctx->peek(), this); - name.untag_check(this); + check_tagged(name); symbol_char* sym = name->data(); @@ -131,7 +131,7 @@ void factor_vm::primitive_dlsym() { void factor_vm::primitive_dlsym_raw() { data_root library(ctx->pop(), this); data_root name(ctx->peek(), this); - name.untag_check(this); + check_tagged(name); symbol_char* sym = name->data(); diff --git a/vm/arrays.cpp b/vm/arrays.cpp index 65479e0b58..9a32c13913 100644 --- a/vm/arrays.cpp +++ b/vm/arrays.cpp @@ -35,7 +35,7 @@ cell factor_vm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) { // Allocates memory void factor_vm::primitive_resize_array() { data_root a(ctx->pop(), this); - a.untag_check(this); + check_tagged(a); cell capacity = unbox_array_size(); ctx->push(tag(reallot_array(a.untagged(), capacity))); } diff --git a/vm/byte_arrays.cpp b/vm/byte_arrays.cpp index b4da9d8dff..794bd64eec 100644 --- a/vm/byte_arrays.cpp +++ b/vm/byte_arrays.cpp @@ -24,7 +24,7 @@ void factor_vm::primitive_uninitialized_byte_array() { // Allocates memory void factor_vm::primitive_resize_byte_array() { data_root array(ctx->pop(), this); - array.untag_check(this); + check_tagged(array); cell capacity = unbox_array_size(); ctx->push(tag(reallot_array(array.untagged(), capacity))); } diff --git a/vm/callbacks.cpp b/vm/callbacks.cpp index 77a2c20689..492e7a9884 100644 --- a/vm/callbacks.cpp +++ b/vm/callbacks.cpp @@ -43,7 +43,7 @@ void callback_heap::store_callback_operand(code_block* stub, cell index, } void callback_heap::update(code_block* stub) { - word* w = (word*)UNTAG(stub->owner); + word* w = untag(stub->owner); store_callback_operand(stub, 1, w->entry_point); stub->flush_icache(); } @@ -88,7 +88,7 @@ code_block* callback_heap::add(cell owner, cell return_rewind) { void factor_vm::primitive_callback() { cell return_rewind = to_cell(ctx->pop()); tagged w(ctx->pop()); - w.untag_check(this); + check_tagged(w); cell func = callbacks->add(w.value(), return_rewind)->entry_point(); CODE_TO_FUNCTION_POINTER_CALLBACK(this, func); diff --git a/vm/callstack.cpp b/vm/callstack.cpp index a386ed2eca..45caa01315 100644 --- a/vm/callstack.cpp +++ b/vm/callstack.cpp @@ -96,8 +96,8 @@ void factor_vm::primitive_set_innermost_stack_frame_quotation() { data_root stack(ctx->pop(), this); data_root quot(ctx->pop(), this); - stack.untag_check(this); - quot.untag_check(this); + check_tagged(stack); + check_tagged(quot); jit_compile_quotation(quot.value(), true); diff --git a/vm/code_blocks.hpp b/vm/code_blocks.hpp index c609c260ee..09b0dc4a47 100644 --- a/vm/code_blocks.hpp +++ b/vm/code_blocks.hpp @@ -77,7 +77,7 @@ struct code_block { if (!to_boolean(relocation)) return; - byte_array* rels = (byte_array*)UNTAG(relocation); + byte_array* rels = untag(relocation); cell index = 0; cell length = untag_fixnum(rels->capacity) / sizeof(relocation_entry); diff --git a/vm/cpu-x86.cpp b/vm/cpu-x86.cpp index 5afac917e9..c7169d26fd 100644 --- a/vm/cpu-x86.cpp +++ b/vm/cpu-x86.cpp @@ -70,8 +70,7 @@ void factor_vm::dispatch_resumable_signal(cell* sp, cell* pc, cell handler) { *sp = new_sp; *(cell*)new_sp = *pc; - tagged handler_word = tagged(special_objects[index]); - *pc = (cell)handler_word->entry_point; + *pc = untag(special_objects[index])->entry_point; } void factor_vm::dispatch_signal_handler(cell* sp, cell* pc, cell handler) { diff --git a/vm/io.cpp b/vm/io.cpp index ba8050051d..6f768b034c 100644 --- a/vm/io.cpp +++ b/vm/io.cpp @@ -162,8 +162,8 @@ void factor_vm::safe_fflush(FILE* stream) { void factor_vm::primitive_fopen() { data_root mode(ctx->pop(), this); data_root path(ctx->pop(), this); - mode.untag_check(this); - path.untag_check(this); + check_tagged(mode); + check_tagged(path); FILE* file; file = safe_fopen((char*)(path.untagged() + 1), diff --git a/vm/master.hpp b/vm/master.hpp index 36b56e5f8b..8a3bcf4367 100644 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -103,6 +103,7 @@ namespace factor { struct factor_vm; } #include "bignum.hpp" #include "booleans.hpp" #include "instruction_operands.hpp" +#include "tagged.hpp" #include "code_blocks.hpp" #include "bump_allocator.hpp" #include "bitwise_hacks.hpp" @@ -122,7 +123,6 @@ namespace factor { struct factor_vm; } #include "callbacks.hpp" #include "dispatch.hpp" #include "vm.hpp" -#include "tagged.hpp" #include "allot.hpp" #include "data_roots.hpp" #include "code_roots.hpp" diff --git a/vm/strings.cpp b/vm/strings.cpp index cd4d3d4877..bcde346294 100644 --- a/vm/strings.cpp +++ b/vm/strings.cpp @@ -100,7 +100,7 @@ string* factor_vm::reallot_string(string* str_, cell capacity) { // Allocates memory void factor_vm::primitive_resize_string() { data_root str(ctx->pop(), this); - str.untag_check(this); + check_tagged(str); cell capacity = unbox_array_size(); ctx->push(tag(reallot_string(str.untagged(), capacity))); } diff --git a/vm/tagged.hpp b/vm/tagged.hpp index 28fb12b4e2..c9a77da676 100644 --- a/vm/tagged.hpp +++ b/vm/tagged.hpp @@ -29,12 +29,6 @@ template struct tagged { return (Type*)(UNTAG(value_)); } - Type* untag_check(factor_vm* parent) const { - if (!type_p()) - parent->type_error(Type::type_number, value_); - return untagged(); - } - explicit tagged(cell tagged) : value_(tagged) {} explicit tagged(Type* untagged) : value_(factor::tag(untagged)) {} @@ -58,10 +52,6 @@ template struct tagged { } }; -template Type* factor_vm::untag_check(cell value) { - return tagged(value).untag_check(this); -} - template Type* untag(cell value) { return tagged(value).untagged(); } diff --git a/vm/vm.hpp b/vm/vm.hpp index 6169b9746d..e2a7872e41 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -515,7 +515,16 @@ struct factor_vm { inline double fixnum_to_float(cell tagged); // tagged - template Type* untag_check(cell value); + template void check_tagged(tagged t) { + if (!t.type_p()) + type_error(Type::type_number, t.value_); + } + + template Type* untag_check(cell value) { + tagged t(value); + check_tagged(t); + return t.untagged(); + } // io void init_c_io(); diff --git a/vm/words.cpp b/vm/words.cpp index bfb77cc118..cce37446ed 100644 --- a/vm/words.cpp +++ b/vm/words.cpp @@ -59,7 +59,7 @@ void factor_vm::primitive_word() { // Allocates memory (from_unsigned_cell allocates) void factor_vm::primitive_word_code() { data_root w(ctx->pop(), this); - w.untag_check(this); + check_tagged(w); ctx->push(from_unsigned_cell(w->entry_point)); ctx->push(from_unsigned_cell((cell)w->code() + w->code()->size()));