From f8782244d357d15294e9374dcf4f7e7f4c5c317d Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 19 Nov 2022 16:41:25 +0000 Subject: [PATCH] Store source object with vcode units --- src/cgen.c | 10 +++--- src/jit/jit-core.c | 37 +++++++------------- src/jit/jit-exits.c | 14 ++++++-- src/jit/jit-llvm.c | 51 +++++++++++++-------------- src/jit/jit-priv.h | 3 +- src/lower.c | 42 +++++++++++------------ src/object.c | 36 ++++++++++++------- src/object.h | 27 +-------------- src/prim.h | 2 +- src/rt/structs.h | 6 ++-- src/symbols.txt | 3 +- src/tree.c | 14 ++++++-- src/tree.h | 3 ++ src/type.c | 11 ++++++ src/type.h | 3 ++ src/vcode.c | 84 +++++++++++++++++++++++++-------------------- src/vcode.h | 19 +++++----- 17 files changed, 191 insertions(+), 174 deletions(-) diff --git a/src/cgen.c b/src/cgen.c index 5d635c33..1f36d7e5 100644 --- a/src/cgen.c +++ b/src/cgen.c @@ -24,6 +24,7 @@ #include "jit/jit-llvm.h" #include "jit/jit.h" #include "lib.h" +#include "object.h" #include "opt.h" #include "phase.h" #include "rt/cover.h" @@ -662,21 +663,20 @@ static void cgen_debug_push_func(cgen_ctx_t *ctx) LOCAL_TEXT_BUF symbol = safe_symbol(vcode_unit_name()); - const loc_t *loc = vcode_unit_loc(); - assert(!loc_invalid_p(loc)); + object_t *obj = vcode_unit_object(vcode_active_unit()); - LLVMMetadataRef file_ref = cgen_debug_file(loc, true); + LLVMMetadataRef file_ref = cgen_debug_file(&(obj->loc), true); LLVMMetadataRef dtype = LLVMDIBuilderCreateSubroutineType( debuginfo, file_ref, NULL, 0, 0); LLVMMetadataRef sp = LLVMDIBuilderCreateFunction( debuginfo, scope, tb_get(symbol), tb_len(symbol), tb_get(symbol), tb_len(symbol), file_ref, - loc->first_line, dtype, true, true, + obj->loc.first_line, dtype, true, true, 1, 0, opt_get_int(OPT_OPTIMISE)); LLVMSetSubprogram(ctx->fn, sp); cgen_push_debug_scope(sp); - cgen_debug_loc(ctx, vcode_unit_loc(), true); + cgen_debug_loc(ctx, &(obj->loc), true); } static LLVMValueRef cgen_get_arg(int op, int arg, cgen_ctx_t *ctx) diff --git a/src/jit/jit-core.c b/src/jit/jit-core.c index d38668b3..16f0460b 100644 --- a/src/jit/jit-core.c +++ b/src/jit/jit-core.c @@ -21,14 +21,16 @@ #include "debug.h" #include "diag.h" #include "hash.h" -#include "lib.h" -#include "jit/jit.h" #include "jit/jit-priv.h" +#include "jit/jit.h" +#include "lib.h" +#include "object.h" #include "opt.h" #include "rt/model.h" #include "rt/mspace.h" #include "rt/structs.h" #include "thread.h" +#include "tree.h" #include "type.h" #include "vcode.h" @@ -210,6 +212,7 @@ jit_handle_t jit_lazy_compile(jit_t *j, ident_t name) f->next_tier = j->tiers; f->hotness = f->next_tier ? f->next_tier->threshold : 0; f->entry = jit_interp; + f->object = vu ? vcode_unit_object(vu) : NULL; if (vu) hash_put(j->index, vu, f); hash_put(j->index, name, f); @@ -241,7 +244,7 @@ jit_handle_t jit_compile(jit_t *j, ident_t name) } void jit_register(jit_t *j, const char *name, jit_entry_fn_t fn, - const uint8_t *debug, size_t bufsz) + const uint8_t *debug, size_t bufsz, object_t *obj) { jit_func_t *f = hash_get(j->index, name); if (f != NULL) @@ -258,6 +261,7 @@ void jit_register(jit_t *j, const char *name, jit_entry_fn_t fn, f->entry = fn; f->irbuf = xcalloc_array(bufsz, sizeof(jit_ir_t)); f->nirs = bufsz; + f->object = obj; for (int i = 0; i < bufsz; i++) { jit_ir_t *ir = &(f->irbuf[i]); @@ -338,11 +342,9 @@ void *jit_link(jit_t *j, jit_handle_t handle) if (kind != VCODE_UNIT_PACKAGE && kind != VCODE_UNIT_INSTANCE) fatal_trace("cannot link unit %s", istr(f->name)); - const loc_t *loc = vcode_unit_loc(); - jit_scalar_t p1 = { .pointer = NULL }, p2 = p1, result; if (!jit_fastcall(j, f->handle, &result, p1, p2)) { - error_at(loc, "failed to initialise %s", istr(f->name)); + error_at(&(f->object->loc), "failed to initialise %s", istr(f->name)); result.pointer = NULL; } else if (result.pointer == NULL) @@ -465,24 +467,6 @@ static void jit_native_trace(diag_t *d) static void jit_interp_trace(diag_t *d) { for (jit_anchor_t *a = jit_thread_local()->anchor; a; a = a->caller) { - vcode_state_t state; - vcode_state_save(&state); - - vcode_select_unit(a->func->unit); - while (vcode_unit_context() != NULL) - vcode_select_unit(vcode_unit_context()); - - ident_t unit_name = vcode_unit_name(); - if (vcode_unit_kind() == VCODE_UNIT_INSTANCE) - unit_name = ident_prefix(unit_name, well_known(W_ELAB), '.'); - - vcode_state_restore(&state); - - const char *symbol = istr(a->func->name); - tree_t enclosing = find_enclosing_decl(unit_name, symbol); - if (enclosing == NULL) - return; - // Scan backwards to find the last debug info assert(a->irpos < a->func->nirs); const loc_t *loc = NULL; @@ -496,6 +480,10 @@ static void jit_interp_trace(diag_t *d) break; } + tree_t enclosing = tree_from_object(a->func->object); + assert(enclosing != NULL); + + const char *symbol = istr(a->func->name); jit_emit_trace(d, loc ?: tree_loc(enclosing), enclosing, symbol); } } @@ -746,6 +734,7 @@ bool jit_call_thunk(jit_t *j, vcode_unit_t unit, jit_scalar_t *result) f->jit = j; f->handle = JIT_HANDLE_INVALID; f->entry = jit_interp; + f->object = vcode_unit_object(unit); jit_irgen(f); diff --git a/src/jit/jit-exits.c b/src/jit/jit-exits.c index 9b504fa3..262d36de 100644 --- a/src/jit/jit-exits.c +++ b/src/jit/jit-exits.c @@ -22,6 +22,7 @@ #include "jit/jit-priv.h" #include "jit/jit.h" #include "lib.h" +#include "object.h" #include "rt/rt.h" #include "type.h" @@ -1475,12 +1476,12 @@ void __nvc_setup_toggle_cb(sig_shared_t *ss, int32_t* toggle_mask) DLLEXPORT void __nvc_register(const char *name, jit_entry_fn_t fn, const uint8_t *debug, - int32_t bufsz) + int32_t bufsz, object_t *obj) { - printf("register! name=%s fn=%p bufsz=%d\n", name, fn, bufsz); + printf("register! name=%s fn=%p bufsz=%d obj=%p\n", name, fn, bufsz, obj); jit_t *j = jit_thread_local()->jit; - jit_register(j, name, fn, debug, bufsz); + jit_register(j, name, fn, debug, bufsz, obj); } DLLEXPORT @@ -1515,3 +1516,10 @@ tree_t __nvc_get_tree(const char *unit, ptrdiff_t offset) { return tree_from_locus(ident_new(unit), offset, lib_get_qualified); } + +DLLEXPORT +object_t *__nvc_get_object(const char *unit, ptrdiff_t offset) +{ + return object_from_locus(ident_new(unit), offset, + (object_load_fn_t)lib_get_qualified); +} diff --git a/src/jit/jit-llvm.c b/src/jit/jit-llvm.c index c605e150..b972649e 100644 --- a/src/jit/jit-llvm.c +++ b/src/jit/jit-llvm.c @@ -22,6 +22,7 @@ #include "jit/jit-llvm.h" #include "jit/jit-priv.h" #include "lib.h" +#include "object.h" #include "opt.h" #include "rt/rt.h" #include "thread.h" @@ -112,6 +113,7 @@ typedef enum { LLVM_GET_FUNC, LLVM_GET_FOREIGN, LLVM_GET_TREE, + LLVM_GET_OBJECT, LLVM_LAST_FN, } llvm_fn_t; @@ -609,6 +611,7 @@ static LLVMValueRef llvm_get_fn(llvm_obj_t *obj, llvm_fn_t which) obj->types[LLVM_PTR], obj->types[LLVM_PTR], obj->types[LLVM_INT32], + obj->types[LLVM_PTR], }; obj->fntypes[which] = LLVMFunctionType(obj->types[LLVM_VOID], args, ARRAY_LEN(args), false); @@ -649,6 +652,18 @@ static LLVMValueRef llvm_get_fn(llvm_obj_t *obj, llvm_fn_t which) } break; + case LLVM_GET_OBJECT: + { + LLVMTypeRef args[] = { + obj->types[LLVM_PTR], + obj->types[LLVM_INTPTR] + }; + obj->fntypes[which] = LLVMFunctionType(obj->types[LLVM_PTR], args, + ARRAY_LEN(args), false); + fn = llvm_add_fn(obj, "__nvc_get_object", obj->fntypes[which]); + } + break; + default: fatal_trace("cannot generate prototype for function %d", which); } @@ -744,39 +759,20 @@ static LLVMBasicBlockRef cgen_add_ctor(llvm_obj_t *obj) return old_bb; } -static LLVMValueRef cgen_rematerialise_tree(llvm_obj_t *obj, tree_t tree) +static LLVMValueRef cgen_rematerialise_object(llvm_obj_t *obj, object_t *ptr) { ident_t unit; ptrdiff_t offset; - tree_locus(tree, &unit, &offset); + object_locus(ptr, &unit, &offset); LOCAL_TEXT_BUF tb = tb_new(); tb_istr(tb, unit); - tb_printf(tb, ".%"PRIiPTR".tree", offset); - LLVMValueRef global = LLVMGetNamedGlobal(obj->module, tb_get(tb)); - if (global == NULL) { - global = LLVMAddGlobal(obj->module, obj->types[LLVM_PTR], tb_get(tb)); - LLVMSetUnnamedAddr(global, true); - LLVMSetLinkage(global, LLVMPrivateLinkage); - LLVMSetInitializer(global, llvm_ptr(obj, NULL)); - - LLVMBasicBlockRef old_bb = cgen_add_ctor(obj); - - tb_trim(tb, ident_len(unit)); // Strip suffix - - LLVMValueRef args[] = { - llvm_const_string(obj, tb_get(tb)), - llvm_intptr(obj, offset), - }; - LLVMValueRef init = - llvm_call_fn(obj, LLVM_GET_TREE, args, ARRAY_LEN(args)); - LLVMBuildStore(obj->builder, init, global); - - LLVMPositionBuilderAtEnd(obj->builder, old_bb); - } - - return LLVMBuildLoad2(obj->builder, obj->types[LLVM_PTR], global, ""); + LLVMValueRef args[] = { + llvm_const_string(obj, tb_get(tb)), + llvm_intptr(obj, offset), + }; + return llvm_call_fn(obj, LLVM_GET_TREE, args, ARRAY_LEN(args)); } static LLVMValueRef cgen_rematerialise_ffi(llvm_obj_t *obj, jit_foreign_t *ff) @@ -869,7 +865,7 @@ static LLVMValueRef cgen_get_value(llvm_obj_t *obj, cgen_block_t *cgb, return llvm_ptr(obj, value.foreign); case JIT_VALUE_TREE: if (cgb->func->cpool != NULL) - return cgen_rematerialise_tree(obj, value.tree); + return cgen_rematerialise_object(obj, tree_to_object(value.tree)); else return llvm_ptr(obj, value.tree); default: @@ -1875,6 +1871,7 @@ static void cgen_function(llvm_obj_t *obj, cgen_func_t *func) PTR(func->llvmfn), PTR(cgen_debug_irbuf(obj, func->source)), llvm_int32(obj, func->source->nirs), + cgen_rematerialise_object(obj, func->source->object) }; llvm_call_fn(obj, LLVM_REGISTER, args, ARRAY_LEN(args)); diff --git a/src/jit/jit-priv.h b/src/jit/jit-priv.h index 11ab7ca2..293d06b8 100644 --- a/src/jit/jit-priv.h +++ b/src/jit/jit-priv.h @@ -254,6 +254,7 @@ typedef struct _jit_func { jit_entry_fn_t entry; jit_cfg_t *cfg; ffi_spec_t spec; + object_t *object; } jit_func_t; typedef struct _jit_anchor { @@ -307,7 +308,7 @@ int jit_backedge_limit(jit_t *j); void jit_tier_up(jit_func_t *f); jit_thread_local_t *jit_thread_local(void); void jit_register(jit_t *j, const char *name, jit_entry_fn_t fn, - const uint8_t *debug, size_t bufsz); + const uint8_t *debug, size_t bufsz, object_t *obj); jit_cfg_t *jit_get_cfg(jit_func_t *f); void jit_free_cfg(jit_func_t *f); diff --git a/src/lower.c b/src/lower.c index edc27810..455330c6 100644 --- a/src/lower.c +++ b/src/lower.c @@ -2793,8 +2793,6 @@ static vcode_reg_t lower_resolved(type_t type, vcode_reg_t reg) vcode_state_t state; vcode_state_save(&state); - const loc_t *loc = vcode_last_loc(); - vcode_unit_t helper_ctx = vcode_active_unit(); int hops = 0; for (; vcode_unit_context() != NULL; hops++) @@ -2817,7 +2815,7 @@ static vcode_reg_t lower_resolved(type_t type, vcode_reg_t reg) ident_t helper_func = ident_new(tb_get(tb)); vcode_unit_t vu = vcode_find_unit(helper_func); if (vu == NULL) { - vu = emit_function(helper_func, loc, helper_ctx); + vu = emit_function(helper_func, type_to_object(type), helper_ctx); vcode_set_result(vrtype); lower_push_scope(NULL); @@ -6978,7 +6976,7 @@ static ident_t lower_guard_func(ident_t prefix, tree_t expr) ident_t context_id = vcode_unit_name(); - emit_function(func, tree_loc(expr), vcode_active_unit()); + emit_function(func, tree_to_object(expr), vcode_active_unit()); vcode_set_result(lower_type(tree_type(expr))); vcode_type_t vcontext = vtype_context(context_id); @@ -7209,7 +7207,7 @@ static void lower_image_helper(tree_t decl) ident_t context_id = vcode_unit_name(); - emit_function(func, tree_loc(decl), vcode_active_unit()); + emit_function(func, tree_to_object(decl), vcode_active_unit()); emit_debug_info(tree_loc(decl)); lower_push_scope(NULL); @@ -7550,7 +7548,7 @@ static void lower_value_helper(tree_t decl) ident_t context_id = vcode_unit_name(); - emit_function(func, tree_loc(decl), vcode_active_unit()); + emit_function(func, tree_to_object(decl), vcode_active_unit()); vcode_set_result(lower_type(type)); lower_push_scope(NULL); @@ -7594,7 +7592,7 @@ static void lower_instantiated_package(tree_t decl, vcode_unit_t context) vcode_select_unit(context); ident_t name = ident_prefix(vcode_unit_name(), tree_ident(decl), '.'); - vcode_unit_t vu = emit_package(name, tree_loc(decl), context); + vcode_unit_t vu = emit_package(name, tree_to_object(decl), context); lower_push_scope(decl); lower_generics(decl, NULL); @@ -7695,8 +7693,9 @@ static void lower_protected_body(tree_t body, vcode_unit_t context) { vcode_select_unit(context); + object_t *obj = tree_to_object(body); type_t type = tree_type(body); - vcode_unit_t vu = emit_protected(type_ident(type), tree_loc(body), context); + vcode_unit_t vu = emit_protected(type_ident(type), obj, context); lower_push_scope(body); cover_push_scope(cover_tags, body); @@ -8820,7 +8819,7 @@ static void lower_predef(tree_t decl, vcode_unit_t context) vcode_select_unit(context); ident_t context_id = vcode_unit_name(); - emit_function(name, tree_loc(decl), context); + emit_function(name, tree_to_object(decl), context); vcode_set_result(lower_func_result_type(type_result(type))); lower_push_scope(NULL); @@ -8926,10 +8925,11 @@ static void lower_proc_body(tree_t body, vcode_unit_t context) ident_t context_id = vcode_unit_name(); + object_t *obj = tree_to_object(body); if (never_waits) - vu = emit_function(name, tree_loc(body), context); + vu = emit_function(name, obj, context); else - vu = emit_procedure(name, tree_loc(body), context); + vu = emit_procedure(name, obj, context); lower_push_scope(body); cover_push_scope(cover_tags, body); @@ -8976,7 +8976,7 @@ static void lower_func_body(tree_t body, vcode_unit_t context) ident_t context_id = vcode_unit_name(); - vu = emit_function(name, tree_loc(body), context); + vu = emit_function(name, tree_to_object(body), context); vcode_set_result(lower_func_result_type(type_result(tree_type(body)))); emit_debug_info(tree_loc(body)); @@ -9091,7 +9091,7 @@ static void lower_process(tree_t proc, vcode_unit_t context) vcode_select_unit(context); ident_t label = tree_ident(proc); ident_t name = ident_prefix(vcode_unit_name(), label, '.'); - vcode_unit_t vu = emit_process(name, tree_loc(proc), context); + vcode_unit_t vu = emit_process(name, tree_to_object(proc), context); emit_debug_info(tree_loc(proc)); // The code generator assumes the first state starts at block number @@ -9231,7 +9231,7 @@ static ident_t lower_converter(tree_t expr, type_t atype, type_t rtype, ident_t context_id = vcode_unit_name(); - vu = emit_function(name, tree_loc(expr), vcode_active_unit()); + vu = emit_function(name, tree_to_object(expr), vcode_active_unit()); vcode_set_result(*vrtype); emit_debug_info(tree_loc(expr)); @@ -9835,9 +9835,7 @@ static vcode_unit_t lower_concurrent_block(tree_t block, vcode_unit_t context) ident_t label = tree_ident(block); ident_t name = ident_prefix(prefix, label, '.'); - const loc_t *loc = tree_loc(block); - vcode_unit_t vu = emit_instance(name, loc, context); - emit_debug_info(loc); + vcode_unit_t vu = emit_instance(name, tree_to_object(block), context); if (cover_enabled(cover_tags, COVER_MASK_ALL)) { if (!context) @@ -9897,7 +9895,8 @@ static vcode_unit_t lower_pack_body(tree_t unit) tree_t pack = tree_primary(unit); assert(!is_uninstantiated_package(pack)); - vcode_unit_t context = emit_package(tree_ident(pack), tree_loc(unit), NULL); + object_t *obj = tree_to_object(unit); + vcode_unit_t context = emit_package(tree_ident(pack), obj, NULL); lower_push_scope(unit); lower_dependencies(pack, unit); @@ -9915,7 +9914,8 @@ static vcode_unit_t lower_package(tree_t unit) { assert(!is_uninstantiated_package(unit)); - vcode_unit_t context = emit_package(tree_ident(unit), tree_loc(unit), NULL); + object_t *obj = tree_to_object(unit); + vcode_unit_t context = emit_package(tree_ident(unit), obj, NULL); lower_push_scope(unit); lower_dependencies(unit, NULL); @@ -9973,7 +9973,7 @@ static void lower_subprogram_for_thunk(tree_t body, vcode_unit_t context) if (vu != NULL) return; - vcode_unit_t thunk = emit_thunk(name, context); + vcode_unit_t thunk = emit_thunk(name, tree_to_object(body), context); const tree_kind_t kind = tree_kind(body); if (kind == T_FUNC_BODY || kind == T_FUNC_INST) @@ -10022,7 +10022,7 @@ vcode_unit_t lower_thunk(tree_t t) else assert(top_scope == NULL); - vcode_unit_t thunk = emit_thunk(name, NULL); + vcode_unit_t thunk = emit_thunk(name, tree_to_object(t), NULL); vcode_type_t vtype = VCODE_INVALID_TYPE; switch (tree_kind(t)) { diff --git a/src/object.c b/src/object.c index 4521effe..6da10280 100644 --- a/src/object.c +++ b/src/object.c @@ -51,6 +51,27 @@ typedef struct _object_arena { uint32_t checksum; } object_arena_t; +#ifndef __SANITIZE_ADDRESS__ +#define OBJECT_UNMAP_UNUSED 1 +#endif + +#define ITEM_IDENT (I_IDENT | I_IDENT2) +#define ITEM_OBJECT (I_VALUE | I_SEVERITY | I_MESSAGE | I_TARGET \ + | I_DELAY | I_REJECT | I_REF | I_FILE_MODE \ + | I_NAME | I_SPEC | I_RESOLUTION \ + | I_LEFT | I_RIGHT | I_TYPE | I_BASE | I_ELEM \ + | I_ACCESS | I_RESULT | I_FILE | I_PRIMARY \ + | I_GUARD) +#define ITEM_OBJ_ARRAY (I_DECLS | I_STMTS | I_PORTS | I_GENERICS \ + | I_WAVES | I_CONDS | I_TRIGGERS | I_CONSTR \ + | I_PARAMS | I_GENMAPS | I_ASSOCS | I_CONTEXT \ + | I_LITERALS | I_FIELDS | I_UNITS | I_CHARS \ + | I_DIMS | I_RANGES | I_INDEXCON | I_PARTS \ + | I_PRAGMAS) +#define ITEM_INT64 (I_POS | I_IVAL) +#define ITEM_INT32 (I_SUBKIND | I_CLASS | I_FLAGS) +#define ITEM_DOUBLE (I_DVAL) + static const char *item_text_map[] = { "I_IDENT", "I_VALUE", "I_SEVERITY", "I_MESSAGE", "I_TARGET", "I_LITERAL", "I_IDENT2", "I_DECLS", "I_STMTS", "I_PORTS", @@ -138,15 +159,6 @@ object_t *arena_root(object_arena_t *arena) return arena->root ?: (object_t *)arena->base; } -void arena_set_root(object_arena_t *arena, object_t *root) -{ - assert(__object_arena(root) == arena); - assert(arena->root == NULL); - assert(!arena->frozen); - - arena->root = root; -} - bool arena_frozen(object_arena_t *arena) { return arena->frozen; @@ -1233,7 +1245,7 @@ void object_locus(object_t *object, ident_t *module, ptrdiff_t *offset) } object_t *object_from_locus(ident_t module, ptrdiff_t offset, - object_load_fn_t loader, unsigned tag) + object_load_fn_t loader) { // Search backwards to ensure we find the most recent arena with the // given name @@ -1267,8 +1279,8 @@ object_t *object_from_locus(ident_t module, ptrdiff_t offset, fatal_trace("invalid object locus %s%+"PRIiPTR, istr(module), offset); object_t *obj = ptr; - if (obj->tag != tag) - fatal_trace("incorrect tag %d for object locus %s%+"PRIiPTR, obj->tag, + if (obj->tag >= OBJECT_TAG_COUNT) + fatal_trace("invalid tag %d for object locus %s%+"PRIiPTR, obj->tag, istr(module), offset); return obj; diff --git a/src/object.h b/src/object.h index 95f7a854..a6deb16b 100644 --- a/src/object.h +++ b/src/object.h @@ -95,23 +95,6 @@ typedef uint64_t imask_t; // Unused ONE_HOT(58) #define I_PRIMARY ONE_HOT(59) -#define ITEM_IDENT (I_IDENT | I_IDENT2) -#define ITEM_OBJECT (I_VALUE | I_SEVERITY | I_MESSAGE | I_TARGET \ - | I_DELAY | I_REJECT | I_REF | I_FILE_MODE \ - | I_NAME | I_SPEC | I_RESOLUTION \ - | I_LEFT | I_RIGHT | I_TYPE | I_BASE | I_ELEM \ - | I_ACCESS | I_RESULT | I_FILE | I_PRIMARY \ - | I_GUARD) -#define ITEM_OBJ_ARRAY (I_DECLS | I_STMTS | I_PORTS | I_GENERICS \ - | I_WAVES | I_CONDS | I_TRIGGERS | I_CONSTR \ - | I_PARAMS | I_GENMAPS | I_ASSOCS | I_CONTEXT \ - | I_LITERALS | I_FIELDS | I_UNITS | I_CHARS \ - | I_DIMS | I_RANGES | I_INDEXCON | I_PARTS \ - | I_PRAGMAS) -#define ITEM_INT64 (I_POS | I_IVAL) -#define ITEM_INT32 (I_SUBKIND | I_CLASS | I_FLAGS) -#define ITEM_DOUBLE (I_DVAL) - enum { OBJECT_TAG_TREE = 0, OBJECT_TAG_TYPE = 1, @@ -124,10 +107,6 @@ enum { #define OBJECT_ALIGN_BITS 4 #define OBJECT_ALIGN (1 << OBJECT_ALIGN_BITS) -#ifndef __SANITIZE_ADDRESS__ -#define OBJECT_UNMAP_UNUSED 1 -#endif - STATIC_ASSERT(OBJECT_ALIGN >= sizeof(double)); #define lookup_item(class, t, mask) ({ \ @@ -247,13 +226,10 @@ typedef object_t *(*object_load_fn_t)(ident_t); __attribute__((noreturn, cold)) void object_lookup_failed(object_class_t *class, object_t *obj, imask_t mask); -void item_without_type(imask_t mask); - void object_change_kind(const object_class_t *class, object_t *object, int kind); object_t *object_new(object_arena_t *arena, const object_class_t *class, int kind); -void object_one_time_init(void); void object_visit(object_t *object, object_visit_ctx_t *ctx); object_t *object_rewrite(object_t *object, object_rewrite_ctx_t *ctx); unsigned object_next_generation(void); @@ -261,7 +237,6 @@ void object_copy(object_copy_ctx_t *ctx); object_arena_t *object_arena(object_t *object); size_t object_arena_default_size(void); object_t *arena_root(object_arena_t *arena); -void arena_set_root(object_arena_t *arena, object_t *root); void arena_set_checksum(object_arena_t *arena, uint32_t checksum); bool arena_frozen(object_arena_t *arena); @@ -288,6 +263,6 @@ void object_arena_walk_deps(object_arena_t *arena, object_arena_deps_fn_t fn, void object_locus(object_t *object, ident_t *module, ptrdiff_t *offset); object_t *object_from_locus(ident_t module, ptrdiff_t offset, - object_load_fn_t loader, unsigned tag); + object_load_fn_t loader); #endif // _OBJECT_H diff --git a/src/prim.h b/src/prim.h index 0d33217b..47f564ba 100644 --- a/src/prim.h +++ b/src/prim.h @@ -66,7 +66,7 @@ typedef struct event event_t; typedef struct waveform waveform_t; typedef struct sens_list sens_list_t; -typedef struct vcode_unit *vcode_unit_t; +typedef struct _vcode_unit *vcode_unit_t; typedef struct _cover_tagging cover_tagging_t; diff --git a/src/rt/structs.h b/src/rt/structs.h index 8e9d4e8b..b78354c3 100644 --- a/src/rt/structs.h +++ b/src/rt/structs.h @@ -30,10 +30,10 @@ typedef enum { W_PROC, W_WATCH, W_IMPLICIT } wakeable_kind_t; -typedef uint32_t generation_t; +typedef uint32_t wakeup_gen_t; typedef struct { - generation_t wakeup_gen; + wakeup_gen_t wakeup_gen; wakeable_kind_t kind : 8; bool pending; bool postponed; @@ -69,7 +69,7 @@ typedef struct { typedef struct { rt_proc_t *proc; - generation_t wakeup_gen; + wakeup_gen_t wakeup_gen; } event_proc_t; struct event { diff --git a/src/symbols.txt b/src/symbols.txt index 4143a3e3..92086d33 100644 --- a/src/symbols.txt +++ b/src/symbols.txt @@ -42,9 +42,10 @@ __nvc_exponent_fail; __nvc_flush; __nvc_force; - __nvc_get_func; __nvc_get_foreign; + __nvc_get_func; __nvc_get_handle; + __nvc_get_object; __nvc_get_tree; __nvc_getpriv; __nvc_index_fail; diff --git a/src/tree.c b/src/tree.c index b43d479f..2e11f595 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1306,8 +1306,7 @@ tree_t tree_from_locus(ident_t unit, ptrdiff_t offset, tree_load_fn_t find_deps_fn) { object_t *o = object_from_locus(unit, offset, - (object_load_fn_t)find_deps_fn, - OBJECT_TAG_TREE); + (object_load_fn_t)find_deps_fn); assert(o->tag == OBJECT_TAG_TREE); return container_of(o, struct _tree, object); } @@ -1329,3 +1328,14 @@ int tree_stable_compar(const void *pa, const void *pb) else return tree_loc(a)->first_line - tree_loc(b)->first_line; } + +object_t *tree_to_object(tree_t t) +{ + return &(t->object); +} + +tree_t tree_from_object(object_t *obj) +{ + assert(obj->tag == OBJECT_TAG_TREE); + return container_of(obj, struct _tree, object); +} diff --git a/src/tree.h b/src/tree.h index a727c7d3..abd4da80 100644 --- a/src/tree.h +++ b/src/tree.h @@ -567,4 +567,7 @@ void tree_walk_deps(tree_t t, tree_deps_fn_t fn, void *ctx); int tree_stable_compar(const void *pa, const void *pb); +object_t *tree_to_object(tree_t t); +tree_t tree_from_object(object_t *obj); + #endif // _TREE_H diff --git a/src/type.c b/src/type.c index 68968fb7..dc191889 100644 --- a/src/type.c +++ b/src/type.c @@ -886,3 +886,14 @@ bool type_frozen(type_t t) { return arena_frozen(object_arena(&(t->object))); } + +object_t *type_to_object(type_t t) +{ + return &(t->object); +} + +type_t type_from_object(object_t *obj) +{ + assert(obj->tag == OBJECT_TAG_TYPE); + return container_of(obj, struct _type, object); +} diff --git a/src/type.h b/src/type.h index ffc418cf..25bea98e 100644 --- a/src/type.h +++ b/src/type.h @@ -151,4 +151,7 @@ int type_freedom(type_t t); bool type_frozen(type_t t); +object_t *type_to_object(type_t t); +type_t type_from_object(object_t *obj); + #endif // _TYPE_H diff --git a/src/vcode.c b/src/vcode.c index 571d8a6a..3500cb40 100644 --- a/src/vcode.c +++ b/src/vcode.c @@ -21,6 +21,7 @@ #include "diag.h" #include "hash.h" #include "lib.h" +#include "object.h" #include "tree.h" #include "vcode.h" @@ -166,7 +167,7 @@ typedef enum { UNIT_ESCAPING_TLAB = (1 << 2) } unit_flags_t; -struct vcode_unit { +struct _vcode_unit { vunit_kind_t kind; vcode_unit_t context; ident_t name; @@ -180,7 +181,7 @@ struct vcode_unit { unit_flags_t flags; vcode_unit_t children; vcode_unit_t next; - loc_t loc; + object_t *object; }; #define MASK_CONTEXT(x) ((x) >> 24) @@ -204,7 +205,7 @@ struct vcode_unit { #define VCODE_FOR_EACH_MATCHING_OP(name, k) \ VCODE_FOR_EACH_OP(name) if (name->kind == k) -#define VCODE_VERSION 26 +#define VCODE_VERSION 27 #define VCODE_CHECK_UNIONS 0 static __thread vcode_unit_t active_unit = NULL; @@ -2907,10 +2908,10 @@ vcode_unit_t vcode_unit_context(void) return active_unit->context; } -const loc_t *vcode_unit_loc(void) +object_t *vcode_unit_object(vcode_unit_t vu) { - assert(active_unit != NULL); - return &(active_unit->loc); + assert(vu != NULL); + return vu->object; } static unsigned vcode_unit_calc_depth(vcode_unit_t unit) @@ -2953,150 +2954,149 @@ static void vcode_add_child(vcode_unit_t context, vcode_unit_t child) } } -vcode_unit_t emit_function(ident_t name, const loc_t *loc, vcode_unit_t context) +vcode_unit_t emit_function(ident_t name, object_t *obj, vcode_unit_t context) { - vcode_unit_t vu = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t vu = xcalloc(sizeof(struct _vcode_unit)); vu->kind = VCODE_UNIT_FUNCTION; vu->name = name; vu->context = context; vu->result = VCODE_INVALID_TYPE; vu->depth = vcode_unit_calc_depth(vu); - vu->loc = *loc; + vu->object = obj; vcode_add_child(context, vu); vcode_select_unit(vu); vcode_select_block(emit_block()); - emit_debug_info(loc); + emit_debug_info(&(obj->loc)); vcode_registry_add(vu); return vu; } -vcode_unit_t emit_procedure(ident_t name, const loc_t *loc, - vcode_unit_t context) +vcode_unit_t emit_procedure(ident_t name, object_t *obj, vcode_unit_t context) { - vcode_unit_t vu = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t vu = xcalloc(sizeof(struct _vcode_unit)); vu->kind = VCODE_UNIT_PROCEDURE; vu->name = name; vu->context = context; vu->result = VCODE_INVALID_TYPE; vu->depth = vcode_unit_calc_depth(vu); - vu->loc = *loc; + vu->object = obj; vcode_add_child(context, vu); vcode_select_unit(vu); vcode_select_block(emit_block()); - emit_debug_info(loc); + emit_debug_info(&(obj->loc)); vcode_registry_add(vu); return vu; } -vcode_unit_t emit_process(ident_t name, const loc_t *loc, vcode_unit_t context) +vcode_unit_t emit_process(ident_t name, object_t *obj, vcode_unit_t context) { assert(context->kind == VCODE_UNIT_INSTANCE); - vcode_unit_t vu = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t vu = xcalloc(sizeof(struct _vcode_unit)); vu->kind = VCODE_UNIT_PROCESS; vu->name = name; vu->context = context; vu->depth = vcode_unit_calc_depth(vu); vu->result = VCODE_INVALID_TYPE; - vu->loc = *loc; + vu->object = obj; vcode_add_child(context, vu); vcode_select_unit(vu); vcode_select_block(emit_block()); - emit_debug_info(loc); + emit_debug_info(&(obj->loc)); vcode_registry_add(vu); return vu; } -vcode_unit_t emit_instance(ident_t name, const loc_t *loc, vcode_unit_t context) +vcode_unit_t emit_instance(ident_t name, object_t *obj, vcode_unit_t context) { assert(context == NULL || context->kind == VCODE_UNIT_INSTANCE); - vcode_unit_t vu = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t vu = xcalloc(sizeof(struct _vcode_unit)); vu->kind = VCODE_UNIT_INSTANCE; vu->name = name; vu->context = context; vu->depth = vcode_unit_calc_depth(vu); vu->result = VCODE_INVALID_TYPE; - vu->loc = *loc; + vu->object = obj; if (context != NULL) vcode_add_child(context, vu); vcode_select_unit(vu); vcode_select_block(emit_block()); - emit_debug_info(loc); + emit_debug_info(&(obj->loc)); vcode_registry_add(vu); return vu; } -vcode_unit_t emit_package(ident_t name, const loc_t *loc, vcode_unit_t context) +vcode_unit_t emit_package(ident_t name, object_t *obj, vcode_unit_t context) { - vcode_unit_t vu = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t vu = xcalloc(sizeof(struct _vcode_unit)); vu->kind = VCODE_UNIT_PACKAGE; vu->name = name; vu->context = context; vu->depth = vcode_unit_calc_depth(vu); vu->result = VCODE_INVALID_TYPE; - vu->loc = *loc; + vu->object = obj; if (context != NULL) vcode_add_child(context, vu); vcode_select_unit(vu); vcode_select_block(emit_block()); - emit_debug_info(loc); + emit_debug_info(&(obj->loc)); vcode_registry_add(vu); return vu; } -vcode_unit_t emit_protected(ident_t name, const loc_t *loc, - vcode_unit_t context) +vcode_unit_t emit_protected(ident_t name, object_t *obj, vcode_unit_t context) { - vcode_unit_t vu = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t vu = xcalloc(sizeof(struct _vcode_unit)); vu->kind = VCODE_UNIT_PROTECTED; vu->name = name; vu->context = context; vu->depth = vcode_unit_calc_depth(vu); vu->result = VCODE_INVALID_TYPE; - vu->loc = *loc; + vu->object = obj; if (context != NULL) vcode_add_child(context, vu); vcode_select_unit(vu); vcode_select_block(emit_block()); - emit_debug_info(loc); + emit_debug_info(&(obj->loc)); vcode_registry_add(vu); return vu; } -vcode_unit_t emit_thunk(ident_t name, vcode_unit_t context) +vcode_unit_t emit_thunk(ident_t name, object_t *obj, vcode_unit_t context) { - vcode_unit_t vu = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t vu = xcalloc(sizeof(struct _vcode_unit)); vu->kind = VCODE_UNIT_THUNK; vu->name = name; vu->context = context; vu->depth = vcode_unit_calc_depth(vu); vu->result = VCODE_INVALID_TYPE; vu->depth = vcode_unit_calc_depth(vu); + vu->object = obj; if (context != NULL) vcode_add_child(context, vu); @@ -5649,7 +5649,13 @@ static void vcode_write_unit(vcode_unit_t unit, fbuf_t *f, fbuf_put_int(f, unit->result); fbuf_put_int(f, unit->flags); fbuf_put_int(f, unit->depth); - loc_write(&(unit->loc), loc_wr_ctx); + + ident_t unit_name; + ptrdiff_t offset; + object_locus(unit->object, &unit_name, &offset); + + ident_write(unit_name, ident_wr_ctx); + fbuf_put_uint(f, offset); if (unit->context != NULL) { vcode_select_unit(unit); @@ -5813,14 +5819,18 @@ static vcode_unit_t vcode_read_unit(fbuf_t *f, ident_rd_ctx_t ident_rd_ctx, if (marker == 0xff) return false; - vcode_unit_t unit = xcalloc(sizeof(struct vcode_unit)); + vcode_unit_t unit = xcalloc(sizeof(struct _vcode_unit)); unit->kind = marker; unit->name = ident_read(ident_rd_ctx); unit->result = fbuf_get_int(f); unit->flags = fbuf_get_int(f); unit->depth = fbuf_get_int(f); - loc_read(&(unit->loc), loc_rd_ctx); + ident_t unit_name = ident_read(ident_rd_ctx); + ptrdiff_t offset = fbuf_get_uint(f); + + unit->object = object_from_locus(unit_name, offset, + (object_load_fn_t)lib_get_qualified); ident_t context_name = ident_read(ident_rd_ctx); if (context_name != NULL) { diff --git a/src/vcode.h b/src/vcode.h index d4ac6167..811dc2b1 100644 --- a/src/vcode.h +++ b/src/vcode.h @@ -299,7 +299,7 @@ vcode_type_t vcode_unit_result(void); vcode_block_t vcode_active_block(void); vcode_unit_t vcode_active_unit(void); vcode_unit_t vcode_unit_context(void); -const loc_t *vcode_unit_loc(void); +object_t *vcode_unit_object(vcode_unit_t vu); void vcode_set_result(vcode_type_t type); void vcode_write(vcode_unit_t unit, fbuf_t *fbuf, ident_wr_ctx_t ident_ctx, @@ -348,16 +348,13 @@ vcode_type_t vcode_var_type(vcode_var_t var); vcode_type_t vcode_var_bounds(vcode_var_t var); vcode_var_flags_t vcode_var_flags(vcode_var_t var); -vcode_unit_t emit_function(ident_t name, const loc_t *loc, vcode_unit_t context); -vcode_unit_t emit_procedure(ident_t name, const loc_t *loc, - vcode_unit_t context); -vcode_unit_t emit_process(ident_t name, const loc_t *loc, vcode_unit_t context); -vcode_unit_t emit_instance(ident_t name, const loc_t *loc, - vcode_unit_t context); -vcode_unit_t emit_package(ident_t name, const loc_t *loc, vcode_unit_t context); -vcode_unit_t emit_protected(ident_t name, const loc_t *loc, - vcode_unit_t context); -vcode_unit_t emit_thunk(ident_t name, vcode_unit_t context); +vcode_unit_t emit_function(ident_t name, object_t *obj, vcode_unit_t context); +vcode_unit_t emit_procedure(ident_t name, object_t *obj, vcode_unit_t context); +vcode_unit_t emit_process(ident_t name, object_t *obj, vcode_unit_t context); +vcode_unit_t emit_instance(ident_t name, object_t *obj, vcode_unit_t context); +vcode_unit_t emit_package(ident_t name, object_t *obj, vcode_unit_t context); +vcode_unit_t emit_protected(ident_t name, object_t *obj, vcode_unit_t context); +vcode_unit_t emit_thunk(ident_t name, object_t *obj, vcode_unit_t context); vcode_block_t emit_block(void); vcode_var_t emit_var(vcode_type_t type, vcode_type_t bounds, ident_t name, vcode_var_flags_t flags); -- 2.39.2