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 {
16101610 LLVMValueRef return_address_fn_val;
16111611 LLVMValueRef frame_address_fn_val;
16121612 LLVMValueRef coro_destroy_fn_val;
1613 LLVMValueRef coro_id_fn_val;
16131614 bool error_during_imports;
16141615
16151616 const char **clang_argv;
src/analyze.cpp+4
......@@ -5776,3 +5776,7 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) {
57765776 assert(err_set_type->data.error_set.infer_fn == nullptr);
57775777 return err_set_type->data.error_set.err_count == UINT32_MAX;
57785778}
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);
191191
192192TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry);
193193
194uint32_t get_coro_frame_align_bytes(CodeGen *g);
195
194196#endif
src/codegen.cpp+29-1
......@@ -942,6 +942,24 @@ static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) {
942942 return g->coro_destroy_fn_val;
943943}
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
945963static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
946964 if (g->return_address_fn_val)
947965 return g->return_address_fn_val;
......@@ -3730,7 +3748,17 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst
37303748}
37313749
37323750static 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, "");
37343762}
37353763
37363764static 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
60276027 IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, irb->exec->implicit_allocator_ptr,
60286028 alloc_field_name);
60296029 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));
60316032 size_t arg_count = 3;
60326033 IrInstruction **args = allocate<IrInstruction *>(arg_count);
60336034 args[0] = irb->exec->implicit_allocator_ptr; // self
src/zig_llvm.cpp+3
......@@ -182,6 +182,9 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
182182 return false;
183183}
184184
185ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref) {
186 return wrap(Type::getTokenTy(*unwrap(context_ref)));
187}
185188
186189LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
187190 unsigned NumArgs, unsigned CC, ZigLLVM_FnInline fn_inline, const char *Name)
src/zig_llvm.h+2
......@@ -54,6 +54,8 @@ enum ZigLLVM_EmitOutputType {
5454ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
5555 const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug);
5656
57ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);
58
5759enum ZigLLVM_FnInline {
5860 ZigLLVM_FnInlineAuto,
5961 ZigLLVM_FnInlineAlways,