Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/irgen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
32 changes: 32 additions & 0 deletions test/ir/test_list.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
;;

Expand Down