From 71fb62d896ac3d34d86636123470f09482821add Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 8 Jul 2024 23:30:36 +0200 Subject: Add some basic LLVM IR tests --- test/data/constants.ll | 13 +++++++++++++ test/data/constants.yx | 17 +++++++++++++++++ test/data/globals.ll | 16 ++++++++++++++++ test/data/globals.yx | 17 +++++++++++++++++ test/data/nested-fns.ll | 18 ++++++++++++++++++ test/data/nested-fns.yx | 13 +++++++++++++ test/llvm-ir-tests.sh | 18 ++++++++++++++++++ 7 files changed, 112 insertions(+) create mode 100644 test/data/constants.ll create mode 100644 test/data/constants.yx create mode 100644 test/data/globals.ll create mode 100644 test/data/globals.yx create mode 100644 test/data/nested-fns.ll create mode 100644 test/data/nested-fns.yx create mode 100755 test/llvm-ir-tests.sh diff --git a/test/data/constants.ll b/test/data/constants.ll new file mode 100644 index 0000000..bc517ad --- /dev/null +++ b/test/data/constants.ll @@ -0,0 +1,13 @@ +define i64 @foo() { +entry: + %x = alloca i64, align 8 + store i64 42, ptr %x, align 8 + %load = load i64, ptr %x, align 8 + %add = add i64 %load, 84 + ret i64 %add +} + +define i64 @bar() { +entry: + ret i64 43 +} diff --git a/test/data/constants.yx b/test/data/constants.yx new file mode 100644 index 0000000..cc9e6df --- /dev/null +++ b/test/data/constants.yx @@ -0,0 +1,17 @@ +/* Basic definitions, with Unicode support */ +π :: 3.14159265358979323846264338327950288419716939937510582097494459; +MEANING_OF_LIFE :: 42; + +/* You can use them in expressions, and they can be defined + out-of-order */ +foo :: () int { + x := MEANING_OF_LIFE; + return x + MOL_DBL; + MOL_DBL :: MEANING_OF_LIFE * 2; +} + +/* You can perform constant shadowing */ +bar :: () int { + MEANING_OF_LIFE :: MEANING_OF_LIFE + 1; + return MEANING_OF_LIFE; +} diff --git a/test/data/globals.ll b/test/data/globals.ll new file mode 100644 index 0000000..f52d967 --- /dev/null +++ b/test/data/globals.ll @@ -0,0 +1,16 @@ +@x = internal global i64 0 +@y = internal global i16 0 +@"x\E2\80\B2" = internal global i64 42 +@"y\E2\80\B2" = internal global i64 69 +@"x\E2\80\B3" = internal global i64 42 +@"y\E2\80\B3" = internal global i16 69 + +define i64 @foo() { +entry: + %x = alloca i64, align 8 + %load = load i64, ptr @x, align 8 + %add = add i64 %load, 1 + store i64 %add, ptr %x, align 8 + %load1 = load i64, ptr %x, align 8 + ret i64 %load1 +} diff --git a/test/data/globals.yx b/test/data/globals.yx new file mode 100644 index 0000000..c933c6d --- /dev/null +++ b/test/data/globals.yx @@ -0,0 +1,17 @@ +/* Globals without initializers */ +x: int; +y: u16; + +/* Globals with initializers */ +x′ := 42; +y′ := 69; + +/* Globals with initializers and types */ +x″: int = 42; +y″: u16 = 69; + +/* Globals can be shadowed */ +foo :: () int { + x := x + 1; + return x; /* 1 */ +} diff --git a/test/data/nested-fns.ll b/test/data/nested-fns.ll new file mode 100644 index 0000000..c29a847 --- /dev/null +++ b/test/data/nested-fns.ll @@ -0,0 +1,18 @@ +define i64 @fn1.fn2.fn1() { +entry: + ret i64 42 +} + +define i64 @fn1.fn2() { +entry: + %call = call i64 @fn1.fn2.fn1() + %call1 = call i64 @fn1.fn2.fn1() + %mul = mul i64 %call, %call1 + ret i64 %mul +} + +define i64 @fn1() { +entry: + %call = call i64 @fn1.fn2() + ret i64 %call +} diff --git a/test/data/nested-fns.yx b/test/data/nested-fns.yx new file mode 100644 index 0000000..3f60f5b --- /dev/null +++ b/test/data/nested-fns.yx @@ -0,0 +1,13 @@ +fn1 :: () int { + /* You can create and use nested functions */ + fn2 :: () int { + return fn1() * fn1(); + + /* Nested functions shadow parent functions */ + fn1 :: () int { + return 42; + } + } + + return fn2(); +} diff --git a/test/llvm-ir-tests.sh b/test/llvm-ir-tests.sh new file mode 100755 index 0000000..46ef922 --- /dev/null +++ b/test/llvm-ir-tests.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +readonly OUT=/tmp/llvm-ir-tests-out +trap "rm -f $OUT" EXIT + +for f in ./test/data/*.yx +do + if ! ./oryx -l "$f" \ + | sed '1,/^$/d' \ + | diff -u --color=always - "${f%%yx}ll" >$OUT + then + file="$(basename "$f" .yx)" + echo "llvm-ir-tests: Test ‘$file’ failed" >&2 + printf '\tFailing diff:\n' >&2 + cat $OUT + exit 1 + fi +done -- cgit v1.2.3