Skip to content

Commit 9f6c5a2

Browse files
committed
codegen for coro_id instruction
See #727
1 parent 7567448 commit 9f6c5a2

File tree

7 files changed

+43
-2
lines changed

7 files changed

+43
-2
lines changed

src/all_types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
5780+
uint32_t get_coro_frame_align_bytes(CodeGen *g) {
5781+
return g->pointer_size_bytes * 2;
5782+
}

src/analyze.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,6 @@ void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry);
191191

192192
TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry);
193193

194+
uint32_t get_coro_frame_align_bytes(CodeGen *g);
195+
194196
#endif

src/codegen.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,24 @@ static LLVMValueRef get_coro_destroy_fn_val(CodeGen *g) {
942942
return g->coro_destroy_fn_val;
943943
}
944944

945+
static 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+
945963
static 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

37323750
static 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

37363764
static LLVMValueRef ir_render_coro_alloc(CodeGen *g, IrExecutable *executable, IrInstructionCoroAlloc *instruction) {

src/ir.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
182182
return false;
183183
}
184184

185+
ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref) {
186+
return wrap(Type::getTokenTy(*unwrap(context_ref)));
187+
}
185188

186189
LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
187190
unsigned NumArgs, unsigned CC, ZigLLVM_FnInline fn_inline, const char *Name)

src/zig_llvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ enum ZigLLVM_EmitOutputType {
5454
ZIG_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

57+
ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);
58+
5759
enum ZigLLVM_FnInline {
5860
ZigLLVM_FnInlineAuto,
5961
ZigLLVM_FnInlineAlways,

0 commit comments

Comments
 (0)