so I been working on adding Coroutines to my frontend following https://llvm.org/docs/Coroutines.html
and so far I was able to get it semi working and I wanted to have final suspension
but It is erroring out at runtime
this is the IR I am generating
; ModuleID = 'main.serk'
source_filename = "main.serk"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc"
@0 = private unnamed_addr constant [3 x i8] c"%d\00", align 1
declare i32 @printf(i8* %0, ...)
declare i8* @malloc(i64 %0)
declare i8* @realloc(i8* %0, i64 %1)
declare void @free(i8* %0)
define i8* @do() #0 !dbg !5 {
entry:
%0 = alloca i32, align 4
%1 = bitcast i32* %0 to i8*
%2 = call token @llvm.coro.id(i32 0, i8* %1, i8* null, i8* null)
%3 = call i64 @llvm.coro.size.i64()
%4 = call i8* @malloc(i64 %3)
%5 = call i8* @llvm.coro.begin(token %2, i8* %4)
%num = alloca i32, align 4
call void @llvm.dbg.addr(metadata i32* %num, metadata !10, metadata !DIExpression()), !dbg !11
store i32 4, i32* %num, align 4, !dbg !11
%6 = load i32, i32* %num, align 4
%7 = bitcast i8* %1 to i32*
store i32 %6, i32* %7, align 4
%8 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %8, label %suspend [
i8 0, label %after_yield1
i8 1, label %cleanup
], !dbg !12
cleanup: ; preds = %after_yield2, %after_yield1, %entry, %final_suspend
%9 = call i8* @llvm.coro.free(token %2, i8* %5)
call void @free(i8* %9)
br label %suspend
suspend: ; preds = %after_yield2, %after_yield1, %entry, %final_suspend, %cleanup
%10 = call i1 @llvm.coro.end(i8* %5, i1 false)
ret i8* %5
final_suspend: ; preds = %after_yield3
%11 = call i8 @llvm.coro.suspend(token none, i1 true)
switch i8 %11, label %suspend [
i8 0, label %after_yield
i8 1, label %cleanup
]
after_yield: ; preds = %final_suspend
after_yield1: ; preds = %entry
store i32 5, i32* %num, align 4, !dbg !13
%12 = load i32, i32* %num, align 4
%13 = bitcast i8* %1 to i32*
store i32 %12, i32* %13, align 4
%14 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %14, label %suspend [
i8 0, label %after_yield2
i8 1, label %cleanup
], !dbg !13
after_yield2: ; preds = %after_yield1
store i32 6, i32* %num, align 4, !dbg !14
%15 = load i32, i32* %num, align 4
%16 = bitcast i8* %1 to i32*
store i32 %15, i32* %16, align 4
%17 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %17, label %suspend [
i8 0, label %after_yield3
i8 1, label %cleanup
], !dbg !15
after_yield3: ; preds = %after_yield2
br label %final_suspend
}
; Function Attrs: argmemonly nounwind readonly
declare token @llvm.coro.id(i32 %0, i8* readnone %1, i8* nocapture readonly %2, i8* %3) #1
; Function Attrs: nounwind readnone
declare i64 @llvm.coro.size.i64() #2
; Function Attrs: nounwind
declare i8* @llvm.coro.begin(token %0, i8* writeonly %1) #3
; Function Attrs: argmemonly nounwind readonly
declare i8* @llvm.coro.free(token %0, i8* nocapture readonly %1) #1
; Function Attrs: nounwind
declare i1 @llvm.coro.end(i8* %0, i1 %1) #3
; Function Attrs: nounwind
declare i8 @llvm.coro.suspend(token %0, i1 %1) #3
; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
declare void @llvm.dbg.addr(metadata %0, metadata %1, metadata %2) #4
define i32 @main() !dbg !16 {
entry:
%i = alloca i32, align 4
call void @llvm.dbg.addr(metadata i32* %i, metadata !17, metadata !DIExpression()), !dbg !18
br label %For.initail
For.initail: ; preds = %entry
%0 = call i8* @do()
br label %For.Cond
For.body: ; preds = %For.Cond
%Raw_Promise = call i8* @llvm.coro.promise(i8* %0, i32 4, i1 false)
%1 = bitcast i8* %Raw_Promise to i32*
%2 = load i32, i32* %1, align 4
store i32 %2, i32* %i, align 4
%3 = load i32, i32* %i, align 4
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @0, i32 0, i32 0), i32 %3), !dbg !19
br label %For.Step
after.For: ; preds = %For.Cond
call void @llvm.coro.destroy(i8* %0)
ret i32 0, !dbg !20
For.Step: ; preds = %For.body
call void @llvm.coro.resume(i8* %0)
br label %For.Cond
For.Cond: ; preds = %For.Step, %For.initail
%5 = call i1 @llvm.coro.done(i8* %0)
%6 = xor i1 %5, true
br i1 %6, label %For.body, label %after.For, !dbg !18
}
; Function Attrs: argmemonly nounwind
declare i1 @llvm.coro.done(i8* nocapture readonly %0) #5
; Function Attrs: nounwind readnone
declare i8* @llvm.coro.promise(i8* nocapture %0, i32 %1, i1 %2) #2
declare void @llvm.coro.resume(i8* %0)
declare void @llvm.coro.destroy(i8* %0)
attributes #0 = { "coroutine.presplit"="0" }
attributes #1 = { argmemonly nounwind readonly }
attributes #2 = { nounwind readnone }
attributes #3 = { nounwind }
attributes #4 = { nofree nosync nounwind readnone speculatable willreturn }
attributes #5 = { argmemonly nounwind }
!llvm.module.flags = !{!0, !1, !2}
!llvm.dbg.cu = !{!3}
!0 = !{i32 5, !"CodeView", i32 1}
!1 = !{i32 5, !"uwtable", i32 1}
!2 = !{i32 5, !"Debug Info Version", i32 3}
!3 = distinct !DICompileUnit(language: DW_LANG_C, file: !4, producer: "Serk", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
!4 = !DIFile(filename: "main.serk", directory: "C:\\Serk\\build\\Debug")
!5 = distinct !DISubprogram(name: "do", linkageName: "do", scope: !4, file: !4, line: 1, type: !6, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !3, retainedNodes: !9)
!6 = !DISubroutineType(types: !7)
!7 = !{!8}
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!9 = !{}
!10 = !DILocalVariable(name: "num", scope: !5, file: !4, line: 7, type: !8)
!11 = !DILocation(line: 2, scope: !5)
!12 = !DILocation(line: 3, scope: !5)
!13 = !DILocation(line: 6, scope: !5)
!14 = !DILocation(line: 7, scope: !5)
!15 = !DILocation(line: 9, scope: !5)
!16 = distinct !DISubprogram(name: "main", linkageName: "main", scope: !4, file: !4, line: 12, type: !6, scopeLine: 12, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !3, retainedNodes: !9)
!17 = !DILocalVariable(name: "i", scope: !16, file: !4, line: 7, type: !8)
!18 = !DILocation(line: 13, scope: !16)
!19 = !DILocation(line: 14, scope: !16)
!20 = !DILocation(line: 16, scope: !16)