authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-31 01:36:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-31 01:36:57-04:00
logccce3d852681bfe60bce92a6766b4f431dd73571
tree521bf62dffb4d1c95be881a6c8c3d7208124c2eb
parent461382ae941e235ad75a6b3d00e05e5369baa98a
signature Commit is signed but in an unrecognized format.

no-copy semantics for function forwarding

```zig fn foo() Foo { return bar(); } ``` ```llvm define internal fastcc void @foo(%Foo* nonnull sret) unnamed_addr #2 !dbg !48 { Entry: call fastcc void @bar(%Foo* sret %0), !dbg !52 ret void, !dbg !54 } ```

3 files changed, 16 insertions(+), 5 deletions(-)

src/analyze.cpp+1-1
......@@ -7278,6 +7278,6 @@ void src_assert(bool ok, AstNode *source_node) {
72787278 buf_ptr(source_node->owner->data.structure.root_struct->path),
72797279 (unsigned)source_node->line + 1, (unsigned)source_node->column + 1);
72807280 }
7281 const char *msg = "assertion failed";
7281 const char *msg = "assertion failed. This is a bug in the Zig compiler.";
72827282 stage2_panic(msg, strlen(msg));
72837283}
src/codegen.cpp+6-2
......@@ -1995,7 +1995,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
19951995 if (!type_has_bits(instruction->value.type))
19961996 return nullptr;
19971997 if (!instruction->llvm_value) {
1998 assert(instruction->value.special != ConstValSpecialRuntime);
1998 src_assert(instruction->value.special != ConstValSpecialRuntime, instruction->source_node);
19991999 assert(instruction->value.type);
20002000 render_const_val(g, &instruction->value, "");
20012001 // we might have to do some pointer casting here due to the way union
......@@ -2388,7 +2388,11 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns
23882388
23892389 if (want_first_arg_sret(g, &g->cur_fn->type_entry->data.fn.fn_type_id)) {
23902390 assert(g->cur_ret_ptr);
2391 gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value);
2391 if (return_instruction->value->value.special != ConstValSpecialRuntime) {
2392 // if it's comptime we have to do this but if it's runtime trust that
2393 // result location mechanism took care of it.
2394 gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value);
2395 }
23922396 LLVMBuildRetVoid(g->builder);
23932397 } else if (handle_is_ptr(return_type)) {
23942398 LLVMValueRef by_val_value = gen_load_untyped(g, value, 0, false, "");
src/ir.cpp+9-2
......@@ -1382,6 +1382,9 @@ static IrInstruction *ir_build_call_gen(IrAnalyze *ira, IrInstruction *source_in
13821382 FnInline fn_inline, bool is_async, IrInstruction *async_allocator, IrInstruction *new_stack,
13831383 ResultLoc *result_loc, ZigType *return_type)
13841384{
1385 // must be resolved before building the call instruction
1386 IrInstruction *resolved_result_loc = ir_resolve_result(ira, result_loc, return_type, nullptr);
1387
13851388 IrInstructionCallGen *call_instruction = ir_build_instruction<IrInstructionCallGen>(&ira->new_irb,
13861389 source_instruction->scope, source_instruction->source_node);
13871390 call_instruction->base.value.type = return_type;
......@@ -1393,7 +1396,7 @@ static IrInstruction *ir_build_call_gen(IrAnalyze *ira, IrInstruction *source_in
13931396 call_instruction->is_async = is_async;
13941397 call_instruction->async_allocator = async_allocator;
13951398 call_instruction->new_stack = new_stack;
1396 call_instruction->result_loc = ir_resolve_result(ira, result_loc, return_type, nullptr);
1399 call_instruction->result_loc = resolved_result_loc;
13971400
13981401 if (fn_ref != nullptr) ir_ref_instruction(fn_ref, ira->new_irb.current_basic_block);
13991402 for (size_t i = 0; i < arg_count; i += 1)
......@@ -14347,10 +14350,14 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, ResultLoc *result_lo
1434714350}
1434814351
1434914352// give nullptr for value to resolve it at runtime
14350// returns a result location, or nullptr if the result location was already taken care of by this function
14353// returns a result location, or nullptr if the result location was already taken care of
1435114354static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, ZigType *value_type,
1435214355 IrInstruction *value)
1435314356{
14357 if (result_loc->implicit_elem_type != nullptr) {
14358 // already resolved
14359 return nullptr;
14360 }
1435414361 result_loc->gen_instruction = value;
1435514362 result_loc->implicit_elem_type = value_type;
1435614363 switch (result_loc->id) {