diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-03-31 06:18:20 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-03-31 06:18:20 -0400 |
| commit | f8e63c458478022813a67bfd5ee64bfad6eee82e (patch) | |
| tree | 52952d63618068cbe76ee1175623e7f9e2976de2 /src | |
| parent | 3ca027ca8219dbdbb6467645944c4daada037f51 (diff) | |
| download | zig-f8e63c458478022813a67bfd5ee64bfad6eee82e.tar.gz zig-f8e63c458478022813a67bfd5ee64bfad6eee82e.zip | |
change `@bitcast` to `@ptrcast`
See #290
Diffstat (limited to 'src')
| -rw-r--r-- | src/all_types.hpp | 8 | ||||
| -rw-r--r-- | src/codegen.cpp | 14 | ||||
| -rw-r--r-- | src/ir.cpp | 70 | ||||
| -rw-r--r-- | src/ir_print.cpp | 14 |
4 files changed, 56 insertions, 50 deletions
diff --git a/src/all_types.hpp b/src/all_types.hpp index 347654351c..7cf410faa4 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1196,7 +1196,7 @@ enum BuiltinFnId { BuiltinFnIdSetGlobalSection, BuiltinFnIdSetGlobalLinkage, BuiltinFnIdPanic, - BuiltinFnIdBitCast, + BuiltinFnIdPtrCast, }; struct BuiltinFnEntry { @@ -1720,7 +1720,7 @@ enum IrInstructionId { IrInstructionIdFnProto, IrInstructionIdTestComptime, IrInstructionIdInitEnum, - IrInstructionIdBitCast, + IrInstructionIdPtrCast, IrInstructionIdWidenOrShorten, IrInstructionIdIntToPtr, IrInstructionIdPtrToInt, @@ -2370,11 +2370,11 @@ struct IrInstructionInitEnum { LLVMValueRef tmp_ptr; }; -struct IrInstructionBitCast { +struct IrInstructionPtrCast { IrInstruction base; IrInstruction *dest_type; - IrInstruction *target; + IrInstruction *ptr; }; struct IrInstructionWidenOrShorten { diff --git a/src/codegen.cpp b/src/codegen.cpp index 25290bc279..cbc0d4c0b0 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1345,12 +1345,12 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable, zig_unreachable(); } -static LLVMValueRef ir_render_bitcast(CodeGen *g, IrExecutable *executable, - IrInstructionBitCast *instruction) +static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable, + IrInstructionPtrCast *instruction) { TypeTableEntry *wanted_type = instruction->base.value.type; - LLVMValueRef target = ir_llvm_value(g, instruction->target); - return LLVMBuildBitCast(g->builder, target, wanted_type->type_ref, ""); + LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr); + return LLVMBuildBitCast(g->builder, ptr, wanted_type->type_ref, ""); } static LLVMValueRef ir_render_widen_or_shorten(CodeGen *g, IrExecutable *executable, @@ -2776,8 +2776,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_init_enum(g, executable, (IrInstructionInitEnum *)instruction); case IrInstructionIdStructInit: return ir_render_struct_init(g, executable, (IrInstructionStructInit *)instruction); - case IrInstructionIdBitCast: - return ir_render_bitcast(g, executable, (IrInstructionBitCast *)instruction); + case IrInstructionIdPtrCast: + return ir_render_ptr_cast(g, executable, (IrInstructionPtrCast *)instruction); case IrInstructionIdWidenOrShorten: return ir_render_widen_or_shorten(g, executable, (IrInstructionWidenOrShorten *)instruction); case IrInstructionIdPtrToInt: @@ -4260,7 +4260,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdSetGlobalSection, "setGlobalSection", 2); create_builtin_fn(g, BuiltinFnIdSetGlobalLinkage, "setGlobalLinkage", 2); create_builtin_fn(g, BuiltinFnIdPanic, "panic", 1); - create_builtin_fn(g, BuiltinFnIdBitCast, "bitcast", 2); + create_builtin_fn(g, BuiltinFnIdPtrCast, "ptrcast", 2); } static void add_compile_var(CodeGen *g, const char *name, ConstExprValue *value) { diff --git a/src/ir.cpp b/src/ir.cpp index 0a0756f27a..06f761c8b0 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -480,8 +480,8 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionInitEnum *) { return IrInstructionIdInitEnum; } -static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCast *) { - return IrInstructionIdBitCast; +static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCast *) { + return IrInstructionIdPtrCast; } static constexpr IrInstructionId ir_instruction_id(IrInstructionWidenOrShorten *) { @@ -1940,16 +1940,16 @@ static IrInstruction *ir_build_init_enum_from(IrBuilder *irb, IrInstruction *old return new_instruction; } -static IrInstruction *ir_build_bit_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, - IrInstruction *dest_type, IrInstruction *target) +static IrInstruction *ir_build_ptr_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *dest_type, IrInstruction *ptr) { - IrInstructionBitCast *instruction = ir_build_instruction<IrInstructionBitCast>( + IrInstructionPtrCast *instruction = ir_build_instruction<IrInstructionPtrCast>( irb, scope, source_node); instruction->dest_type = dest_type; - instruction->target = target; + instruction->ptr = ptr; if (dest_type) ir_ref_instruction(dest_type, irb->current_basic_block); - ir_ref_instruction(target, irb->current_basic_block); + ir_ref_instruction(ptr, irb->current_basic_block); return &instruction->base; } @@ -2666,12 +2666,12 @@ static IrInstruction *ir_instruction_initenum_get_dep(IrInstructionInitEnum *ins } } -static IrInstruction *ir_instruction_bitcast_get_dep(IrInstructionBitCast *instruction, +static IrInstruction *ir_instruction_ptrcast_get_dep(IrInstructionPtrCast *instruction, size_t index) { switch (index) { case 0: return instruction->dest_type; - case 1: return instruction->target; + case 1: return instruction->ptr; default: return nullptr; } } @@ -2928,8 +2928,8 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t return ir_instruction_testcomptime_get_dep((IrInstructionTestComptime *) instruction, index); case IrInstructionIdInitEnum: return ir_instruction_initenum_get_dep((IrInstructionInitEnum *) instruction, index); - case IrInstructionIdBitCast: - return ir_instruction_bitcast_get_dep((IrInstructionBitCast *) instruction, index); + case IrInstructionIdPtrCast: + return ir_instruction_ptrcast_get_dep((IrInstructionPtrCast *) instruction, index); case IrInstructionIdWidenOrShorten: return ir_instruction_widenorshorten_get_dep((IrInstructionWidenOrShorten *) instruction, index); case IrInstructionIdIntToPtr: @@ -4200,7 +4200,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return ir_build_panic(irb, scope, node, arg0_value); } - case BuiltinFnIdBitCast: + case BuiltinFnIdPtrCast: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); @@ -4212,7 +4212,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bit_cast(irb, scope, node, arg0_value, arg1_value); + return ir_build_ptr_cast(irb, scope, node, arg0_value, arg1_value); } } zig_unreachable(); @@ -12182,34 +12182,36 @@ static TypeTableEntry *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructio return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable); } -static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstructionBitCast *instruction) { +static bool is_ptr_type(TypeTableEntry *t) { + return (t->id == TypeTableEntryIdPointer || t->id == TypeTableEntryIdFn || + (t->id == TypeTableEntryIdMaybe && (t->data.maybe.child_type->id == TypeTableEntryIdPointer || + t->data.maybe.child_type->id == TypeTableEntryIdFn))); +} + +static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtrCast *instruction) { IrInstruction *dest_type_value = instruction->dest_type->other; TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value); if (type_is_invalid(dest_type)) return ira->codegen->builtin_types.entry_invalid; - IrInstruction *target = instruction->target->other; - TypeTableEntry *src_type = target->value.type; + IrInstruction *ptr = instruction->ptr->other; + TypeTableEntry *src_type = ptr->value.type; if (type_is_invalid(src_type)) return ira->codegen->builtin_types.entry_invalid; - ensure_complete_type(ira->codegen, dest_type); - ensure_complete_type(ira->codegen, src_type); + if (!is_ptr_type(src_type)) { + ir_add_error(ira, ptr, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } - uint64_t dest_size_bytes = type_size(ira->codegen, dest_type); - uint64_t src_size_bytes = type_size(ira->codegen, src_type); - if (dest_size_bytes != src_size_bytes) { - ir_add_error(ira, &instruction->base, - buf_sprintf("destination type '%s' has size %" PRIu64 " but source type '%s' has size %" PRIu64, - buf_ptr(&dest_type->name), dest_size_bytes, - buf_ptr(&src_type->name), src_size_bytes)); + if (!is_ptr_type(dest_type)) { + ir_add_error(ira, dest_type_value, + buf_sprintf("expected pointer, found '%s'", buf_ptr(&dest_type->name))); return ira->codegen->builtin_types.entry_invalid; } - if (instr_is_comptime(target) && src_type->id == dest_type->id && - (src_type->id == TypeTableEntryIdPointer || src_type->id == TypeTableEntryIdMaybe)) - { - ConstExprValue *val = ir_resolve_const(ira, target, UndefOk); + if (instr_is_comptime(ptr)) { + ConstExprValue *val = ir_resolve_const(ira, ptr, UndefOk); if (!val) return ira->codegen->builtin_types.entry_invalid; @@ -12219,8 +12221,8 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc return dest_type; } - IrInstruction *result = ir_build_bit_cast(&ira->new_irb, instruction->base.scope, - instruction->base.source_node, nullptr, target); + IrInstruction *result = ir_build_ptr_cast(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, nullptr, ptr); ir_link_new_instruction(result, &instruction->base); result->value.type = dest_type; return dest_type; @@ -12464,8 +12466,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_decl_ref(ira, (IrInstructionDeclRef *)instruction); case IrInstructionIdPanic: return ir_analyze_instruction_panic(ira, (IrInstructionPanic *)instruction); - case IrInstructionIdBitCast: - return ir_analyze_instruction_bit_cast(ira, (IrInstructionBitCast *)instruction); + case IrInstructionIdPtrCast: + return ir_analyze_instruction_ptr_cast(ira, (IrInstructionPtrCast *)instruction); case IrInstructionIdMaybeWrap: case IrInstructionIdErrWrapCode: case IrInstructionIdErrWrapPayload: @@ -12637,7 +12639,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdFnProto: case IrInstructionIdTestComptime: case IrInstructionIdInitEnum: - case IrInstructionIdBitCast: + case IrInstructionIdPtrCast: case IrInstructionIdWidenOrShorten: case IrInstructionIdPtrToInt: case IrInstructionIdIntToPtr: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 9446be43bd..7814064f85 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -765,9 +765,13 @@ static void ir_print_init_enum(IrPrint *irp, IrInstructionInitEnum *instruction) fprintf(irp->f, "}"); } -static void ir_print_bit_cast(IrPrint *irp, IrInstructionBitCast *instruction) { - fprintf(irp->f, "@bitcast("); - ir_print_other_instruction(irp, instruction->target); +static void ir_print_ptr_cast(IrPrint *irp, IrInstructionPtrCast *instruction) { + fprintf(irp->f, "@ptrcast("); + if (instruction->dest_type) { + ir_print_other_instruction(irp, instruction->dest_type); + } + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->ptr); fprintf(irp->f, ")"); } @@ -1098,8 +1102,8 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdInitEnum: ir_print_init_enum(irp, (IrInstructionInitEnum *)instruction); break; - case IrInstructionIdBitCast: - ir_print_bit_cast(irp, (IrInstructionBitCast *)instruction); + case IrInstructionIdPtrCast: + ir_print_ptr_cast(irp, (IrInstructionPtrCast *)instruction); break; case IrInstructionIdWidenOrShorten: ir_print_widen_or_shorten(irp, (IrInstructionWidenOrShorten *)instruction); |
