From d2461ab6762a00525471baedf47492e791f18f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3fer=20Fannar=20Bj=C3=B6rnsson?= Date: Fri, 16 May 2025 14:04:17 -0400 Subject: [PATCH] fix list indexing --- lib/irgen.ml | 2 +- test/ir/test_list.ml | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/irgen.ml b/lib/irgen.ml index d516afc..8f273cd 100644 --- a/lib/irgen.ml +++ b/lib/irgen.ml @@ -326,7 +326,7 @@ let rec build_expr expr (vars : variable StringMap.t) var_types the_module build let elem_ptr = L.build_gep list_val [| index_val |] "elem_ptr" builder in (match fst expr with - | RInt | RFloat | RBool | RChar | REnumType _ -> + | RInt | RFloat | RString | RBool | REnumType _ -> L.build_load elem_ptr "elem_val" builder | RUserType _ -> elem_ptr | _ -> failwith "Unsupported list type for indexing") diff --git a/test/ir/test_list.ml b/test/ir/test_list.ml index 2773e80..4e260fa 100644 --- a/test/ir/test_list.ml +++ b/test/ir/test_list.ml @@ -123,6 +123,38 @@ let tests = in (* _write_to_file actual "actual.out"; *) assert_equal expected actual ~printer) + ; ("local_string_list_index" + >:: fun _ -> + let sast = + get_sast + "fun function() -> string {let a := [\"hello\", \"world\"]; return a[0];}" + in + let mdl = Irgen.translate sast in + let actual = L.string_of_llmodule mdl in + let expected = + "; ModuleID = 'Fly'\n\ + source_filename = \"Fly\"\n\n\ + @str = private unnamed_addr constant [6 x i8] c\"hello\\00\", align 1\n\ + @str.1 = private unnamed_addr constant [6 x i8] c\"world\\00\", align 1\n\n\ + define i8* @function() {\n\ + entry:\n\ + \ %list = alloca i8*, i32 2, align 8\n\ + \ %index = getelementptr inbounds i8*, i8** %list, i32 0\n\ + \ store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @str, i32 0, i32 \ + 0), i8** %index, align 8\n\ + \ %index1 = getelementptr inbounds i8*, i8** %list, i32 1\n\ + \ store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @str.1, i32 0, i32 \ + 0), i8** %index1, align 8\n\ + \ %a = alloca i8**, align 8\n\ + \ store i8** %list, i8*** %a, align 8\n\ + \ %a2 = load i8**, i8*** %a, align 8\n\ + \ %elem_ptr = getelementptr i8*, i8** %a2, i32 0\n\ + \ %elem_val = load i8*, i8** %elem_ptr, align 8\n\ + \ ret i8* %elem_val\n\ + }\n" + in + (* _write_to_file actual "actual.out"; *) + assert_equal expected actual ~printer) ] ;;