authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-25 15:10:29-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-25 15:10:29-05:00
log9f6c5a20de03a59bfcaead703fe9490a6d622f84
tree206e132f5c6896be9a0386c59cabb59a6591c741
parent7567448b91fd7012bf61d3f5532bfd86304899ae

codegen for coro_id instruction

See #727

7 files changed, 43 insertions(+), 2 deletions(-)

src/all_types.hpp+1
...@@ -1610,6 +1610,7 @@ struct CodeGen {...@@ -1610,6 +1610,7 @@ struct CodeGen {
1610 LLVMValueRef return_address_fn_val;1610 LLVMValueRef return_address_fn_val;
1611 LLVMValueRef frame_address_fn_val;1611 LLVMValueRef frame_address_fn_val;
1612 LLVMValueRef coro_destroy_fn_val;1612 LLVMValueRef coro_destroy_fn_val;
1613 LLVMValueRef coro_id_fn_val;
1613 bool error_during_imports;1614 bool error_during_imports;
16141615
1615 const char **clang_argv;1616 const char **clang_argv;
src/analyze.cpp+4
...@@ -5776,3 +5776,7 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) {...@@ -5776,3 +5776,7 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) {
5776 assert(err_set_type->data.error_set.infer_fn == nullptr);5776 assert(err_set_type->data.error_set.infer_fn == nullptr);
5777 return err_set_type->data.error_set.err_count == UINT32_MAX;5777 return err_set_type->data.error_set.err_count == UINT32_MAX;
5778}5778}
5779
5780uint32_t get_coro_frame_align_bytes(CodeGen *g) {
5781 return g->pointer_size_bytes * 2;
5782}
src/analyze.hpp+2
...@@ -191,4 +191,6 @@ void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry);...@@ -191,4 +191,6 @@ void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry);
191191
192TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry);192TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry);
193193
194uint32_t get_coro_frame_align_bytes(CodeGen *g);
195
194#endif196#endif
src/codegen.cpp+29-1
...@@ -942,6 +942,24 @@ static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) {...@@ -942,6 +942,24 @@ static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) {
942 return g->coro_destroy_fn_val;942 return g->coro_destroy_fn_val;
943}943}
944944
945static LLVMValueRef get_coro_id_fn_val(CodeGen *g) {
946 if (g->coro_id_fn_val)
947 return g->coro_id_fn_val;
948
949 LLVMTypeRef param_types[] = {
950 LLVMInt32Type(),
951 LLVMPointerType(LLVMInt8Type(), 0),
952 LLVMPointerType(LLVMInt8Type(), 0),
953 LLVMPointerType(LLVMInt8Type(), 0),
954 };
955 LLVMTypeRef fn_type = LLVMFunctionType(ZigLLVMTokenTypeInContext(LLVMGetGlobalContext()), param_types, 4, false);
956 Buf *name = buf_sprintf("llvm.coro.id");
957 g->coro_id_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
958 assert(LLVMGetIntrinsicID(g->coro_id_fn_val));
959
960 return g->coro_id_fn_val;
961}
962
945static LLVMValueRef get_return_address_fn_val(CodeGen *g) {963static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
946 if (g->return_address_fn_val)964 if (g->return_address_fn_val)
947 return g->return_address_fn_val;965 return g->return_address_fn_val;
...@@ -3730,7 +3748,17 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst...@@ -3730,7 +3748,17 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst
3730}3748}
37313749
3732static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {3750static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {
3733 zig_panic("TODO ir_render_coro_id");3751 LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);
3752 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false);
3753 LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->type_ref),
3754 LLVMPointerType(LLVMInt8Type(), 0));
3755 LLVMValueRef params[] = {
3756 align_val,
3757 promise_ptr,
3758 null,
3759 null,
3760 };
3761 return LLVMBuildCall(g->builder, get_coro_id_fn_val(g), params, 4, "");
3734}3762}
37353763
3736static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) {3764static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) {
src/ir.cpp+2-1
...@@ -6027,7 +6027,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec...@@ -6027,7 +6027,8 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
6027 IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr,6027 IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr,
6028 alloc_field_name);6028 alloc_field_name);
6029 IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr);6029 IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr);
6030 IrInstruction *alignment = ir_build_const_u29(irb, scope, node, irb->codegen->pointer_size_bytes * 2);6030 IrInstruction *alignment = ir_build_const_u29(irb, scope, node,
6031 get_coro_frame_align_bytes(irb->codegen));
6031 size_t arg_count = 3;6032 size_t arg_count = 3;
6032 IrInstruction **args = allocate<IrInstruction *>(arg_count);6033 IrInstruction **args = allocate<IrInstruction *>(arg_count);
6033 args[0] = irb->exec->implicit_allocator_ptr; // self6034 args[0] = irb->exec->implicit_allocator_ptr; // self
src/zig_llvm.cpp+3
...@@ -182,6 +182,9 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM...@@ -182,6 +182,9 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
182 return false;182 return false;
183}183}
184184
185ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref) {
186 return wrap(Type::getTokenTy(*unwrap(context_ref)));
187}
185188
186LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,189LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
187 unsigned NumArgs, unsigned CC, ZigLLVM_FnInline fn_inline, const char *Name)190 unsigned NumArgs, unsigned CC, ZigLLVM_FnInline fn_inline, const char *Name)
src/zig_llvm.h+2
...@@ -54,6 +54,8 @@ enum ZigLLVM_EmitOutputType {...@@ -54,6 +54,8 @@ enum ZigLLVM_EmitOutputType {
54ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,54ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
55 const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug);55 const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug);
5656
57ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);
58
57enum ZigLLVM_FnInline {59enum ZigLLVM_FnInline {
58 ZigLLVM_FnInlineAuto,60 ZigLLVM_FnInlineAuto,
59 ZigLLVM_FnInlineAlways,61 ZigLLVM_FnInlineAlways,