From 69f0d6be43373efd6baf566b773061ce41da1068 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Mon, 26 Dec 2016 21:49:00 +0000 Subject: [PATCH] Division and null check operators no longer use bookmarks --- src/cgen.c | 14 ++++---------- src/eval.c | 2 +- src/lower.c | 11 ++++------- src/rt/rtkern.c | 14 ++++++++------ src/vcode.c | 14 +++----------- src/vcode.h | 4 ++-- 6 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/cgen.c b/src/cgen.c index f2976626..0deba229 100644 --- a/src/cgen.c +++ b/src/cgen.c @@ -949,9 +949,7 @@ static void cgen_op_div(int op, cgen_ctx_t *ctx) LLVMPositionBuilderAtEnd(builder, zero_bb); LLVMValueRef args[] = { - llvm_int32(vcode_get_index(op)), - LLVMBuildPointerCast(builder, mod_name, - LLVMPointerType(LLVMInt8Type(), 0), "") + cgen_location(ctx) }; LLVMBuildCall(builder, llvm_fn("_div_zero"), args, ARRAY_LEN(args), ""); LLVMBuildUnreachable(builder); @@ -2179,9 +2177,7 @@ static void cgen_op_null_check(int op, cgen_ctx_t *ctx) LLVMPositionBuilderAtEnd(builder, null_bb); LLVMValueRef args[] = { - llvm_int32(vcode_get_index(op)), - LLVMBuildPointerCast(builder, mod_name, - LLVMPointerType(LLVMInt8Type(), 0), "") + cgen_location(ctx) }; LLVMBuildCall(builder, llvm_fn("_null_deref"), args, ARRAY_LEN(args), ""); LLVMBuildUnreachable(builder); @@ -3563,8 +3559,7 @@ static LLVMValueRef cgen_support_fn(const char *name) } else if (strcmp(name, "_div_zero") == 0) { LLVMTypeRef args[] = { - LLVMInt32Type(), - LLVMPointerType(LLVMInt8Type(), 0) + LLVMPointerType(llvm_rt_loc(), 0) }; fn = LLVMAddFunction(module, "_div_zero", LLVMFunctionType(LLVMVoidType(), @@ -3573,8 +3568,7 @@ static LLVMValueRef cgen_support_fn(const char *name) } else if (strcmp(name, "_null_deref") == 0) { LLVMTypeRef args[] = { - LLVMInt32Type(), - LLVMPointerType(LLVMInt8Type(), 0) + LLVMPointerType(llvm_rt_loc(), 0) }; fn = LLVMAddFunction(module, "_null_deref", LLVMFunctionType(LLVMVoidType(), diff --git a/src/eval.c b/src/eval.c index 02e6ea41..a522db3e 100644 --- a/src/eval.c +++ b/src/eval.c @@ -485,7 +485,7 @@ static void eval_op_div(int op, eval_state_t *state) switch (lhs->kind) { case VALUE_INTEGER: if (rhs->integer == 0) { - error_at(tree_loc(state->fcall), "division by zero"); + error_at(&(state->last_loc), "division by zero"); state->failed = true; } else { diff --git a/src/lower.c b/src/lower.c index f5943d81..daacf849 100644 --- a/src/lower.c +++ b/src/lower.c @@ -1171,8 +1171,7 @@ static vcode_reg_t lower_builtin(tree_t fcall, ident_t builtin) else if (icmp(builtin, "div")) { if (!type_eq(r0_type, r1_type)) r1 = emit_cast(lower_type(r0_type), lower_bounds(r0_type), r1); - return lower_narrow(tree_type(fcall), - emit_div(r0, r1, lower_bookmark(fcall))); + return lower_narrow(tree_type(fcall), emit_div(r0, r1)); } else if (icmp(builtin, "exp")) { if (!type_eq(r0_type, r1_type)) @@ -1305,15 +1304,13 @@ static vcode_reg_t lower_builtin(tree_t fcall, ident_t builtin) vcode_type_t vreal = vtype_real(); vcode_type_t rtype = lower_type(tree_type(fcall)); return emit_cast(rtype, rtype, - emit_div(emit_cast(vreal, vreal, r0), - r1, lower_bookmark(fcall))); + emit_div(emit_cast(vreal, vreal, r0), r1)); } else if (icmp(builtin, "divri")) { vcode_type_t vreal = vtype_real(); vcode_type_t rtype = lower_type(tree_type(fcall)); return emit_cast(rtype, rtype, - emit_div(r0, emit_cast(vreal, vreal, r1), - lower_bookmark(fcall))); + emit_div(r0, emit_cast(vreal, vreal, r1))); } else fatal_at(tree_loc(fcall), "cannot lower builtin %s", istr(builtin)); @@ -2531,7 +2528,7 @@ static vcode_reg_t lower_new(tree_t expr, expr_ctx_t ctx) static vcode_reg_t lower_all(tree_t all, expr_ctx_t ctx) { vcode_reg_t access_reg = lower_reify_expr(tree_value(all)); - emit_null_check(access_reg, lower_bookmark(all)); + emit_null_check(access_reg); vcode_reg_t all_reg = emit_all(access_reg); type_t type = tree_type(all); diff --git a/src/rt/rtkern.c b/src/rt/rtkern.c index 5b8c484a..37e5656a 100644 --- a/src/rt/rtkern.c +++ b/src/rt/rtkern.c @@ -792,16 +792,18 @@ int64_t _value_attr(const uint8_t *raw_str, int32_t str_len, return value; } -void _div_zero(int32_t where, const char *module) +void _div_zero(const rt_loc_t *where) { - tree_t t = rt_recall_tree(module, where); - fatal_at(tree_loc(t), "division by zero"); + loc_t loc; + from_rt_loc(where, &loc); + fatal_at(&loc, "division by zero"); } -void _null_deref(int32_t where, const char *module) +void _null_deref(const rt_loc_t *where) { - tree_t t = rt_recall_tree(module, where); - fatal_at(tree_loc(t), "null access dereference"); + loc_t loc; + from_rt_loc(where, &loc); + fatal_at(&loc, "null access dereference"); } int64_t _std_standard_now(void) diff --git a/src/vcode.c b/src/vcode.c index 210d92be..fab7105f 100644 --- a/src/vcode.c +++ b/src/vcode.c @@ -73,7 +73,6 @@ DECLARE_AND_DEFINE_ARRAY(vcode_type); (x == VCODE_OP_COMMENT) #define OP_HAS_BOOKMARK(x) \ (x == VCODE_OP_SET_INITIAL \ - || x == VCODE_OP_DIV || x == VCODE_OP_NULL_CHECK \ || x == VCODE_OP_BOUNDS \ || x == VCODE_OP_DYNAMIC_BOUNDS || x == VCODE_OP_ARRAY_SIZE \ || x == VCODE_OP_INDEX_CHECK) @@ -3332,19 +3331,13 @@ vcode_reg_t emit_mul(vcode_reg_t lhs, vcode_reg_t rhs) return reg; } -vcode_reg_t emit_div(vcode_reg_t lhs, vcode_reg_t rhs, vcode_bookmark_t where) +vcode_reg_t emit_div(vcode_reg_t lhs, vcode_reg_t rhs) { int64_t lconst, rconst; if (vcode_reg_const(lhs, &lconst) && vcode_reg_const(rhs, &rconst)) return emit_const(vcode_reg_type(lhs), lconst / rconst); - vcode_reg_t result = emit_arith(VCODE_OP_DIV, lhs, rhs); - - block_t *block = vcode_block_data(); - op_t *op = op_array_nth_ptr(&(block->ops), block->ops.count - 1); - op->bookmark = where; - - return result; + return emit_arith(VCODE_OP_DIV, lhs, rhs); } vcode_reg_t emit_exp(vcode_reg_t lhs, vcode_reg_t rhs) @@ -4349,7 +4342,7 @@ vcode_reg_t emit_new(vcode_type_t type, vcode_reg_t length) return (op->result = vcode_add_reg(vtype_access(type))); } -void emit_null_check(vcode_reg_t ptr, vcode_bookmark_t where) +void emit_null_check(vcode_reg_t ptr) { VCODE_FOR_EACH_MATCHING_OP(other, VCODE_OP_NULL_CHECK) { if (other->args.items[0] == ptr) @@ -4358,7 +4351,6 @@ void emit_null_check(vcode_reg_t ptr, vcode_bookmark_t where) op_t *op = vcode_add_op(VCODE_OP_NULL_CHECK); vcode_add_arg(op, ptr); - op->bookmark = where; VCODE_ASSERT(vtype_kind(vcode_reg_type(ptr)) == VCODE_TYPE_ACCESS, "null check argument must be an access"); diff --git a/src/vcode.h b/src/vcode.h index c9500dc7..daef99d6 100644 --- a/src/vcode.h +++ b/src/vcode.h @@ -334,7 +334,7 @@ vcode_reg_t emit_const_real(double value); vcode_reg_t emit_add(vcode_reg_t lhs, vcode_reg_t rhs); vcode_reg_t emit_sub(vcode_reg_t lhs, vcode_reg_t rhs); vcode_reg_t emit_mul(vcode_reg_t lhs, vcode_reg_t rhs); -vcode_reg_t emit_div(vcode_reg_t lhs, vcode_reg_t rhs, vcode_bookmark_t where); +vcode_reg_t emit_div(vcode_reg_t lhs, vcode_reg_t rhs); vcode_reg_t emit_exp(vcode_reg_t lhs, vcode_reg_t rhs); vcode_reg_t emit_mod(vcode_reg_t lhs, vcode_reg_t rhs); vcode_reg_t emit_rem(vcode_reg_t lhs, vcode_reg_t rhs); @@ -422,7 +422,7 @@ void emit_file_read(vcode_reg_t file, vcode_reg_t ptr, vcode_reg_t inlen, vcode_reg_t outlen); vcode_reg_t emit_null(vcode_type_t type); vcode_reg_t emit_new(vcode_type_t type, vcode_reg_t length); -void emit_null_check(vcode_reg_t ptr, vcode_bookmark_t index); +void emit_null_check(vcode_reg_t ptr); void emit_deallocate(vcode_reg_t ptr); vcode_reg_t emit_all(vcode_reg_t reg); vcode_reg_t emit_bit_vec_op(bit_vec_op_kind_t kind, vcode_reg_t lhs_data, -- 2.39.2