From 027783ab012f4b8dbe3d3fc400cbdfde3c074449 Mon Sep 17 00:00:00 2001 From: Gavrilikhin Daniil Date: Sun, 28 Apr 2024 20:03:03 +0800 Subject: [PATCH] Fix IR for reference to none (#145) --- src/ir/to_ir.rs | 2 +- .../ppl__tests__reference_to_none.hir.snap | 10 +++ .../ppl__tests__reference_to_none.ir.snap | 84 +++++++++++++++++++ .../ppl__tests__reference_to_none.run.snap | 5 ++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/tests/snapshots/ppl__tests__reference_to_none.hir.snap create mode 100644 src/tests/snapshots/ppl__tests__reference_to_none.ir.snap create mode 100644 src/tests/snapshots/ppl__tests__reference_to_none.run.snap diff --git a/src/ir/to_ir.rs b/src/ir/to_ir.rs index 7ba6de69..aa990bfd 100644 --- a/src/ir/to_ir.rs +++ b/src/ir/to_ir.rs @@ -236,7 +236,7 @@ impl<'llvm, C: Context<'llvm>> ToIR<'llvm, C> for ClassData { fn to_ir(&self, context: &mut C) -> Self::IR { trace!(target: "to_ir", "{self}"); - if self.is_none() { + if self.is_none() || self.is_any_reference() && self.generics()[0].is_none() { return context.types().none().into(); } else if self.is_bool() { return context.types().bool().into(); diff --git a/src/tests/snapshots/ppl__tests__reference_to_none.hir.snap b/src/tests/snapshots/ppl__tests__reference_to_none.hir.snap new file mode 100644 index 00000000..6d7364c1 --- /dev/null +++ b/src/tests/snapshots/ppl__tests__reference_to_none.hir.snap @@ -0,0 +1,10 @@ +--- +source: src/tests/mod.rs +expression: hir +--- +fn foo <$arg0: Reference> -> None: + let $tmp@19: None = println "foo" + return ($tmp@19:None) + +let x: None = none +foo (&x:Reference) diff --git a/src/tests/snapshots/ppl__tests__reference_to_none.ir.snap b/src/tests/snapshots/ppl__tests__reference_to_none.ir.snap new file mode 100644 index 00000000..632f0b56 --- /dev/null +++ b/src/tests/snapshots/ppl__tests__reference_to_none.ir.snap @@ -0,0 +1,84 @@ +--- +source: src/tests/mod.rs +expression: ir +--- +; ModuleID = 'main' +source_filename = "src/main.ppl" + +%"Type" = type { %String, %Integer } +%String = type { ptr } +%Integer = type { ptr } + +@"Type" = private global %"Type" zeroinitializer +@0 = private unnamed_addr constant [7 x i8] c"String\00", align 1 +@1 = private unnamed_addr constant [4 x i8] c"foo\00", align 1 + +define private void @initialize() !dbg !3 { + %1 = alloca %"Type", align 8, !dbg !7 + %"Type.name" = getelementptr inbounds %"Type", ptr %1, i32 0, i32 0, !dbg !7 + %2 = call %String @string_from_c_string_and_length(ptr @0, i64 6), !dbg !8 + store %String %2, ptr %"Type.name", align 8, !dbg !8 + %"Type.size" = getelementptr inbounds %"Type", ptr %1, i32 0, i32 1, !dbg !8 + %3 = call %Integer @integer_from_i64(i64 8), !dbg !8 + store %Integer %3, ptr %"Type.size", align 8, !dbg !8 + %4 = load %"Type", ptr %1, align 8, !dbg !8 + store %"Type" %4, ptr @"Type", align 8, !dbg !8 + br label %return, !dbg !8 + +return: ; preds = %0 + ret void +} + +declare %String @string_from_c_string_and_length(ptr, i64) + +declare %Integer @integer_from_i64(i64) + +define void @main.execute() !dbg !9 { + call void @initialize(), !dbg !10 + call void @initialize.1(), !dbg !11 + call void @"foo <:Reference>"(), !dbg !12 + br label %return, !dbg !12 + +return: ; preds = %0 + ret void +} + +define void @"foo <:Reference>"() !dbg !13 { + %1 = call %String @string_from_c_string_and_length(ptr @1, i64 3), !dbg !14 + call void @"println <:String>"(%String %1), !dbg !14 + br label %return, !dbg !15 + +return: ; preds = %0 + ret void +} + +declare void @"println <:String>"(%String) + +define private void @initialize.1() !dbg !16 { + br label %return, !dbg !17 + +return: ; preds = %0 + ret void +} + +!llvm.module.flags = !{!0} +!llvm.dbg.cu = !{!1} + +!0 = !{i32 2, !"Debug Info Version", i32 3} +!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2, producer: "ppl", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, sysroot: "/") +!2 = !DIFile(filename: "src/main.ppl", directory: ".") +!3 = distinct !DISubprogram(name: "initialize", linkageName: "initialize", scope: !2, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1) +!4 = !DISubroutineType(types: !5) +!5 = !{!6} +!6 = !DIBasicType(name: "i32", size: 32, encoding: DW_ATE_signed) +!7 = !DILocation(line: 3, column: 5, scope: !3) +!8 = !DILocation(line: 0, scope: !3) +!9 = distinct !DISubprogram(name: "main.execute", linkageName: "main.execute", scope: !2, file: !2, type: !4, spFlags: DISPFlagDefinition, unit: !1) +!10 = !DILocation(line: 3, column: 5, scope: !9) +!11 = !DILocation(line: 2, column: 8, scope: !9) +!12 = !DILocation(line: 3, column: 4, scope: !9) +!13 = distinct !DISubprogram(name: "foo <:Reference>", linkageName: "foo <:Reference>", scope: !9, file: !2, type: !4, spFlags: DISPFlagDefinition, unit: !1) +!14 = !DILocation(line: 0, column: 27, scope: !13) +!15 = !DILocation(line: 0, column: 19, scope: !13) +!16 = distinct !DISubprogram(name: "initialize.1", linkageName: "initialize.1", scope: !9, file: !2, line: 2, type: !4, spFlags: DISPFlagDefinition, unit: !1) +!17 = !DILocation(line: 2, column: 8, scope: !16) diff --git a/src/tests/snapshots/ppl__tests__reference_to_none.run.snap b/src/tests/snapshots/ppl__tests__reference_to_none.run.snap new file mode 100644 index 00000000..af58f5d2 --- /dev/null +++ b/src/tests/snapshots/ppl__tests__reference_to_none.run.snap @@ -0,0 +1,5 @@ +--- +source: src/tests/mod.rs +expression: run_log +--- +foo