From 8bf425957b253d0ab2e4902d57340b19b7436698 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 13 Jan 2020 16:51:58 -0500 Subject: [PATCH] fix regressions double implicit casting return ptr --- src/analyze.cpp | 31 ++++++++++++++++++++++++++++--- src/codegen.cpp | 1 + src/ir.cpp | 15 ++++++--------- src/softfloat.hpp | 17 +++++++++++++++++ src/util.hpp | 21 ++------------------- test/stage1/behavior.zig | 2 +- 6 files changed, 55 insertions(+), 32 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 14419d274c..94d0c66e19 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -9396,6 +9396,9 @@ static void dump_value_indent(ZigValue *val, int indent) { case ZigTypeIdUnreachable: fprintf(stderr, "\n"); return; + case ZigTypeIdUndefined: + fprintf(stderr, "\n"); + return; case ZigTypeIdVoid: fprintf(stderr, "<{}>\n"); return; @@ -9405,11 +9408,16 @@ static void dump_value_indent(ZigValue *val, int indent) { case ZigTypeIdBool: fprintf(stderr, "<%s>\n", val->data.x_bool ? "true" : "false"); return; - case ZigTypeIdComptimeFloat: case ZigTypeIdComptimeInt: - case ZigTypeIdInt: + case ZigTypeIdInt: { + Buf *tmp_buf = buf_alloc(); + bigint_append_buf(tmp_buf, &val->data.x_bigint, 10); + fprintf(stderr, "<%s>\n", buf_ptr(tmp_buf)); + buf_destroy(tmp_buf); + return; + } + case ZigTypeIdComptimeFloat: case ZigTypeIdFloat: - case ZigTypeIdUndefined: fprintf(stderr, "\n"); return; @@ -9458,6 +9466,23 @@ static void dump_value_indent(ZigValue *val, int indent) { fprintf(stderr, "data.x_ptr.data.ref.pointee, indent + 1); break; + case ConstPtrSpecialBaseStruct: { + ZigValue *struct_val = val->data.x_ptr.data.base_struct.struct_val; + size_t field_index = val->data.x_ptr.data.base_struct.field_index; + fprintf(stderr, "data.x_struct.fields[field_index]; + if (field_val != nullptr) { + dump_value_indent(field_val, indent + 1); + } else { + for (int i = 0; i < indent; i += 1) { + fprintf(stderr, " "); + } + fprintf(stderr, "(invalid null field)\n"); + } + } + break; + } default: fprintf(stderr, "TODO dump more pointer things\n"); } diff --git a/src/codegen.cpp b/src/codegen.cpp index c4add2ce71..6b2b95d246 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -20,6 +20,7 @@ #include "zig_llvm.h" #include "userland.h" #include "dump_analysis.hpp" +#include "softfloat.hpp" #include #include diff --git a/src/ir.cpp b/src/ir.cpp index b926b3ae3b..5b1494553a 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -12313,8 +12313,8 @@ static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction if (type_is_invalid(casted_payload->value->type)) return ira->codegen->invalid_instruction; - ZigValue *val = ir_resolve_const(ira, casted_payload, UndefBad); - if (!val) + ZigValue *val = ir_resolve_const(ira, casted_payload, UndefOk); + if (val == nullptr) return ira->codegen->invalid_instruction; ZigValue *err_set_val = create_const_vals(1); @@ -17519,6 +17519,7 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s { result_loc_pass1 = no_result_loc(); } + bool was_written = result_loc_pass1->written; IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type, value, force_runtime, allow_discard); if (result_loc == nullptr || (instr_is_unreachable(result_loc) || type_is_invalid(result_loc->value->type))) @@ -17596,6 +17597,9 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s result_loc_pass1->resolved_loc = result_loc; } + if (was_written) { + return result_loc; + } ir_assert(result_loc->value->type->id == ZigTypeIdPointer, suspend_source_instr); ZigType *actual_elem_type = result_loc->value->type->data.pointer.child_type; @@ -18242,13 +18246,6 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstruction *source_i return ira->codegen->invalid_instruction; } - if (fn_proto_node->data.fn_proto.is_var_args) { - ir_add_error(ira, source_instr, - buf_sprintf("compiler bug: unable to call var args function at compile time. https://github.com/ziglang/zig/issues/313")); - return ira->codegen->invalid_instruction; - } - - for (size_t call_i = 0; call_i < args_len; call_i += 1) { IrInstruction *old_arg = args_ptr[call_i]; diff --git a/src/softfloat.hpp b/src/softfloat.hpp index 4b227e9d7c..a1173690b5 100644 --- a/src/softfloat.hpp +++ b/src/softfloat.hpp @@ -12,4 +12,21 @@ extern "C" { #include "softfloat.h" } +static inline float16_t zig_double_to_f16(double x) { + float64_t y; + static_assert(sizeof(x) == sizeof(y), ""); + memcpy(&y, &x, sizeof(x)); + return f64_to_f16(y); +} + + +// Return value is safe to coerce to float even when |x| is NaN or Infinity. +static inline double zig_f16_to_double(float16_t x) { + float64_t y = f16_to_f64(x); + double z; + static_assert(sizeof(y) == sizeof(z), ""); + memcpy(&z, &y, sizeof(y)); + return z; +} + #endif diff --git a/src/util.hpp b/src/util.hpp index 8dcd41438e..e1722921f5 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -38,6 +38,8 @@ #if defined(__MINGW32__) || defined(__MINGW64__) #define BREAKPOINT __debugbreak() +#elif defined(__i386__) || defined(__x86_64__) +#define BREAKPOINT __asm__ volatile("int $0x03"); #elif defined(__clang__) #define BREAKPOINT __builtin_debugtrap() #elif defined(__GNUC__) @@ -49,8 +51,6 @@ #endif -#include "softfloat.hpp" - ATTRIBUTE_COLD ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF(1, 2) @@ -244,23 +244,6 @@ static inline uint8_t log2_u64(uint64_t x) { return (63 - clzll(x)); } -static inline float16_t zig_double_to_f16(double x) { - float64_t y; - static_assert(sizeof(x) == sizeof(y), ""); - memcpy(&y, &x, sizeof(x)); - return f64_to_f16(y); -} - - -// Return value is safe to coerce to float even when |x| is NaN or Infinity. -static inline double zig_f16_to_double(float16_t x) { - float64_t y = f16_to_f64(x); - double z; - static_assert(sizeof(y) == sizeof(z), ""); - memcpy(&z, &y, sizeof(y)); - return z; -} - void zig_pretty_print_bytes(FILE *f, double n); template diff --git a/test/stage1/behavior.zig b/test/stage1/behavior.zig index a5f13c204c..62a6dedccd 100644 --- a/test/stage1/behavior.zig +++ b/test/stage1/behavior.zig @@ -59,7 +59,7 @@ comptime { _ = @import("behavior/defer.zig"); _ = @import("behavior/enum.zig"); _ = @import("behavior/enum_with_members.zig"); - //_ = @import("behavior/error.zig"); + _ = @import("behavior/error.zig"); _ = @import("behavior/eval.zig"); _ = @import("behavior/field_parent_ptr.zig"); _ = @import("behavior/floatop.zig");