From 0ae5a9cee073887af6a7b5216a28d822004b2fd0 Mon Sep 17 00:00:00 2001 From: Samuel Hammersberg Date: Sun, 22 Jan 2023 19:42:51 +0100 Subject: [PATCH] Switched directories for the test program, added some example LL programs, and gave the Funky programs a temporary extension. --- test/helloworld.ll | 20 +++++++++ test/simple.sf | 5 +++ test/simple_goal.ll | 62 ++++++++++++++++++++++++++++ test_program => test/test_program.sf | 0 4 files changed, 87 insertions(+) create mode 100644 test/helloworld.ll create mode 100644 test/simple.sf create mode 100644 test/simple_goal.ll rename test_program => test/test_program.sf (100%) diff --git a/test/helloworld.ll b/test/helloworld.ll new file mode 100644 index 0000000..16a03fd --- /dev/null +++ b/test/helloworld.ll @@ -0,0 +1,20 @@ +; Copied directly from the documentation +; Declare the string constant as a global constant. +@.hello = private unnamed_addr constant [13 x i8] c"hello world\0A\00" + +; External declaration of the puts function +declare i32 @puts(i8* nocapture) nounwind + +; Definition of main function +define i32 @main() { ; i32()* + ; Convert [13 x i8]* to i8 *... + %cast210 = getelementptr [13 x i8],[13 x i8]* @.hello, i64 0, i64 0 + + ; Call puts function to write out the string to stdout. + call i32 @puts(i8* %cast210) + ret i32 0 +} + +; Named metadata +!0 = !{i32 42, null, !"string"} +!foo = !{!0} \ No newline at end of file diff --git a/test/simple.sf b/test/simple.sf new file mode 100644 index 0000000..6bdd22f --- /dev/null +++ b/test/simple.sf @@ -0,0 +1,5 @@ + + + + +main = 2 diff --git a/test/simple_goal.ll b/test/simple_goal.ll new file mode 100644 index 0000000..33dddd7 --- /dev/null +++ b/test/simple_goal.ll @@ -0,0 +1,62 @@ +; External declaration of the puts function +declare i32 @puts(i8* nocapture) nounwind + +define [22 x i8] @i64ToString(i64 %val) { + ; https://stackoverflow.com/a/7123710 + ; an algorithm for translating ints to strings + ; s = '' + ; sign = '' + ; if i < 0: + ; sign = '-' + ; i = -i + ; while True: + ; remainder = i % 10 + ; i = i / 10 + ; s = chr(ord('0') + remainder) + s + ; if i == 0: + ; break + ; return sign + s + + ; allocate memory for the string, and store the temp variable into it + %string_ptr = alloca [22 x i8] + store [22 x i8] c"Tjena banane hellosa\0A\00", ptr %string_ptr + + ; create a pointer to the array + %array_index_ptr = alloca i32 + store i32 0, ptr %array_index_ptr + + ; get the value of the first element in the array + %array_index_ptr.0 = load i32, i32* %array_index_ptr + %p = getelementptr [22 x i8], [22 x i8]* %string_ptr, i32 0, i32 %array_index_ptr.0 + + ; check if p is below 0 + %condition = icmp slt i64 %val, 0 + br i1 %condition, label %negative_check_true, label %negative_check_false + negative_check_true: + store i8 45, i8* %p + br label %negative_check_done + negative_check_false: + store i8 43, i8* %p + br label %negative_check_done + negative_check_done: + + ; iterate over the next nums + + + ; load the result and return it + %res = load [22 x i8],[22 x i8]* %string_ptr + ret [22 x i8] %res +} + +; Definition of main function +define i32 @main() { ; i32()* + %val = add i64 1, 0 + + %print_res = call [22 x i8] @i64ToString(i64 %val) + %ptr = alloca [22 x i8] + store [22 x i8] %print_res, ptr %ptr + + call i32 @puts(i8* %ptr) + + ret i32 0 +} diff --git a/test_program b/test/test_program.sf similarity index 100% rename from test_program rename to test/test_program.sf