authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-25 22:24:01-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-25 22:24:01-04:00
log7b3686861f87d006da817db98f7d3b13fada9815
treeb603b7e34cd3138ebe24f42994500efa5bb13bdd
parent538c0cd2250e08aad07784355b402cfae6145507
signature Commit is signed but in an unrecognized format.

`@frameSize` works via PrefixData


7 files changed, 30 insertions(+), 22 deletions(-)

BRANCH_TODO+9
......@@ -1,5 +1,14 @@
1 * reimplement @frameSize with Prefix Data
2 * reimplement with function splitting rather than switch
3 * add the `anyframe` type and `anyframe->T`
14 * await
25 * await of a non async function
6 * await in single-threaded mode
37 * async call on a non async function
8 * @asyncCall with an async function pointer
9 * cancel
10 * defer and errdefer
411 * safety for resuming when it is awaiting
512 * implicit cast of normal function to async function should be allowed when it is inferred to be async
13 * go over the commented out tests
14 * revive std.event.Loop
src/all_types.hpp-1
......@@ -3063,7 +3063,6 @@ struct IrInstructionFrameSizeGen {
30633063 IrInstruction base;
30643064
30653065 IrInstruction *fn;
3066 IrInstruction *frame_ptr;
30673066};
30683067
30693068enum IrOverflowOp {
src/codegen.cpp+13-8
......@@ -4914,13 +4914,16 @@ static LLVMValueRef ir_render_coro_resume(CodeGen *g, IrExecutable *executable,
49144914 return nullptr;
49154915}
49164916
4917static LLVMValueRef ir_render_frame_size(CodeGen *g, IrExecutable *executable, IrInstructionFrameSizeGen *instruction) {
4917static LLVMValueRef ir_render_frame_size(CodeGen *g, IrExecutable *executable,
4918 IrInstructionFrameSizeGen *instruction)
4919{
4920 LLVMTypeRef usize_llvm_type = g->builtin_types.entry_usize->llvm_type;
4921 LLVMTypeRef ptr_usize_llvm_type = LLVMPointerType(usize_llvm_type, 0);
49184922 LLVMValueRef fn_val = ir_llvm_value(g, instruction->fn);
4919 LLVMValueRef frame_ptr = ir_llvm_value(g, instruction->frame_ptr);
4920 LLVMValueRef resume_index_ptr = LLVMBuildStructGEP(g->builder, frame_ptr, coro_resume_index_index, "");
4921 LLVMValueRef one = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 1, false);
4922 LLVMBuildStore(g->builder, one, resume_index_ptr);
4923 return ZigLLVMBuildCall(g->builder, fn_val, &frame_ptr, 1, LLVMFastCallConv, ZigLLVM_FnInlineAuto, "");
4923 LLVMValueRef casted_fn_val = LLVMBuildBitCast(g->builder, fn_val, ptr_usize_llvm_type, "");
4924 LLVMValueRef negative_one = LLVMConstInt(LLVMInt32Type(), -1, true);
4925 LLVMValueRef prefix_ptr = LLVMBuildInBoundsGEP(g->builder, casted_fn_val, &negative_one, 1, "");
4926 return LLVMBuildLoad(g->builder, prefix_ptr, "");
49244927}
49254928
49264929static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
......@@ -6409,13 +6412,16 @@ static void do_code_gen(CodeGen *g) {
64096412 }
64106413
64116414 if (is_async) {
6415 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
6416 LLVMValueRef size_val = LLVMConstInt(usize_type_ref, fn_table_entry->frame_type->abi_size, false);
6417 ZigLLVMFunctionSetPrefixData(fn_table_entry->llvm_value, size_val);
6418
64126419 if (!g->strip_debug_symbols) {
64136420 AstNode *source_node = fn_table_entry->proto_node;
64146421 ZigLLVMSetCurrentDebugLocation(g->builder, (int)source_node->line + 1,
64156422 (int)source_node->column + 1, get_di_scope(g, fn_table_entry->child_scope));
64166423 }
64176424 IrExecutable *executable = &fn_table_entry->analyzed_executable;
6418 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
64196425 LLVMBasicBlockRef bad_resume_block = LLVMAppendBasicBlock(g->cur_fn_val, "BadResume");
64206426 LLVMPositionBuilderAtEnd(g->builder, bad_resume_block);
64216427 gen_assertion_scope(g, PanicMsgIdBadResume, fn_table_entry->child_scope);
......@@ -6424,7 +6430,6 @@ static void do_code_gen(CodeGen *g) {
64246430 LLVMPositionBuilderAtEnd(g->builder, get_size_block);
64256431 assert(fn_table_entry->frame_type->abi_size != 0);
64266432 assert(fn_table_entry->frame_type->abi_size != SIZE_MAX);
6427 LLVMValueRef size_val = LLVMConstInt(usize_type_ref, fn_table_entry->frame_type->abi_size, false);
64286433 LLVMBuildRet(g->builder, size_val);
64296434
64306435 LLVMPositionBuilderAtEnd(g->builder, fn_table_entry->preamble_llvm_block);
src/ir.cpp+2-10
......@@ -2396,15 +2396,12 @@ static IrInstruction *ir_build_frame_size_src(IrBuilder *irb, Scope *scope, AstN
23962396 return &instruction->base;
23972397}
23982398
2399static IrInstruction *ir_build_frame_size_gen(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *fn,
2400 IrInstruction *frame_ptr)
2399static IrInstruction *ir_build_frame_size_gen(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *fn)
24012400{
24022401 IrInstructionFrameSizeGen *instruction = ir_build_instruction<IrInstructionFrameSizeGen>(irb, scope, source_node);
24032402 instruction->fn = fn;
2404 instruction->frame_ptr = frame_ptr;
24052403
24062404 ir_ref_instruction(fn, irb->current_basic_block);
2407 ir_ref_instruction(frame_ptr, irb->current_basic_block);
24082405
24092406 return &instruction->base;
24102407}
......@@ -21808,13 +21805,8 @@ static IrInstruction *ir_analyze_instruction_frame_size(IrAnalyze *ira, IrInstru
2180821805 return ira->codegen->invalid_instruction;
2180921806 }
2181021807
21811 IrInstruction *frame_ptr = ir_resolve_result(ira, &instruction->base, no_result_loc(),
21812 ira->codegen->builtin_types.entry_frame_header, nullptr, true, false);
21813 if (frame_ptr != nullptr && (type_is_invalid(frame_ptr->value.type) || instr_is_unreachable(frame_ptr)))
21814 return frame_ptr;
21815
2181621808 IrInstruction *result = ir_build_frame_size_gen(&ira->new_irb, instruction->base.scope,
21817 instruction->base.source_node, fn, frame_ptr);
21809 instruction->base.source_node, fn);
2181821810 result->value.type = ira->codegen->builtin_types.entry_usize;
2181921811 return result;
2182021812}
src/ir_print.cpp-2
......@@ -925,8 +925,6 @@ static void ir_print_frame_size_src(IrPrint *irp, IrInstructionFrameSizeSrc *ins
925925static void ir_print_frame_size_gen(IrPrint *irp, IrInstructionFrameSizeGen *instruction) {
926926 fprintf(irp->f, "@frameSize(");
927927 ir_print_other_instruction(irp, instruction->fn);
928 fprintf(irp->f, ",");
929 ir_print_other_instruction(irp, instruction->frame_ptr);
930928 fprintf(irp->f, ")");
931929}
932930
src/zig_llvm.cpp+5-1
......@@ -899,9 +899,13 @@ LLVMValueRef ZigLLVMBuildAShrExact(LLVMBuilderRef builder, LLVMValueRef LHS, LLV
899899}
900900
901901void ZigLLVMSetTailCall(LLVMValueRef Call) {
902 unwrap<CallInst>(Call)->setTailCallKind(CallInst::TCK_MustTail);
902 unwrap<CallInst>(Call)->setTailCallKind(CallInst::TCK_MustTail);
903903}
904904
905void ZigLLVMFunctionSetPrefixData(LLVMValueRef function, LLVMValueRef data) {
906 unwrap<Function>(function)->setPrefixData(unwrap<Constant>(data));
907}
908
905909
906910class MyOStream: public raw_ostream {
907911 public:
src/zig_llvm.h+1
......@@ -212,6 +212,7 @@ ZIG_EXTERN_C struct ZigLLVMDILocation *ZigLLVMGetDebugLoc(unsigned line, unsigne
212212
213213ZIG_EXTERN_C void ZigLLVMSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state);
214214ZIG_EXTERN_C void ZigLLVMSetTailCall(LLVMValueRef Call);
215ZIG_EXTERN_C void ZigLLVMFunctionSetPrefixData(LLVMValueRef fn, LLVMValueRef data);
215216
216217ZIG_EXTERN_C void ZigLLVMAddFunctionAttr(LLVMValueRef fn, const char *attr_name, const char *attr_value);
217218ZIG_EXTERN_C void ZigLLVMAddFunctionAttrCold(LLVMValueRef fn);