Skip to content
Snippets Groups Projects
Commit 497e5399 authored by Taddeüs Kroes's avatar Taddeüs Kroes
Browse files

Code cleanup

parent efc19d80
No related branches found
No related tags found
No related merge requests found
...@@ -27,22 +27,6 @@ let read_program ic = ...@@ -27,22 +27,6 @@ let read_program ic =
in in
next [] [] next [] []
let rec string_of_program program =
let string_of_command = function
| Incptr -> ">"
| Decptr -> "<"
| Incdata -> "+"
| Decdata -> "-"
| Output -> "."
| Input -> ","
| Loop p -> "[" ^ string_of_program p ^ "]"
in
let rec cat buf = function
| [] -> buf
| cmd :: tl -> cat (buf ^ string_of_command cmd) tl
in
cat "" program
let compile memsize program = let compile memsize program =
let ctx = global_context () in let ctx = global_context () in
let m = create_module ctx "brainfucker" in let m = create_module ctx "brainfucker" in
...@@ -67,21 +51,22 @@ let compile memsize program = ...@@ -67,21 +51,22 @@ let compile memsize program =
bb_cur := bb bb_cur := bb
in in
let i n = const_int i32_ty n in let i w n = const_int (integer_type ctx w) n in
let i8 n = const_int byte_ty n in let i8 = i 8 in
let i32 = i 32 in
let mem = build_alloca (array_type byte_ty memsize) "mem" b in let mem = build_alloca (array_type byte_ty memsize) "mem" b in
let idx = build_alloca i32_ty "idx" b in let idx = build_alloca i32_ty "idx" b in
let gep () = build_in_bounds_gep mem [|i 0; build_load idx "" b|] "" b in
let load ptr = build_load ptr "" b in let load ptr = build_load ptr "" b in
let store ptr value = ignore (build_store value ptr b) in let store ptr value = ignore (build_store value ptr b) in
let gep () = build_in_bounds_gep mem [|i32 0; load idx|] "" b in
let rec compile_command = function let rec compile_command = function
| Incptr -> | Incptr ->
build_add (load idx) (i 1) "" b |> store idx build_add (load idx) (i32 1) "" b |> store idx
| Decptr -> | Decptr ->
build_sub (load idx) (i 1) "" b |> store idx build_sub (load idx) (i32 1) "" b |> store idx
| Incdata -> | Incdata ->
build_add (load (gep ())) (i8 1) "" b |> store (gep ()) build_add (load (gep ())) (i8 1) "" b |> store (gep ())
| Decdata -> | Decdata ->
...@@ -115,14 +100,14 @@ let compile memsize program = ...@@ -115,14 +100,14 @@ let compile memsize program =
declare_function "llvm.memset.p0i8.i32" (function_type void_ty arg_types) m declare_function "llvm.memset.p0i8.i32" (function_type void_ty arg_types) m
in in
let ptr = build_bitcast mem byteptr_ty "" b in let ptr = build_bitcast mem byteptr_ty "" b in
build_call memset [|ptr; i8 0; i memsize; i 0; const_int bool_ty 0|] "" b |> ignore; build_call memset [|ptr; i8 0; i32 memsize; i32 0; i 1 0|] "" b |> ignore;
(* set pivot to index 0 and compile program commands *) (* set pivot to index 0 and compile program commands *)
store idx (i 0); store idx (i32 0);
List.iter compile_command program; List.iter compile_command program;
(* exit gracefully *) (* exit gracefully *)
build_call cexit [|i 0|] "" b |> ignore; build_call cexit [|i32 0|] "" b |> ignore;
build_ret_void b |> ignore; build_ret_void b |> ignore;
m m
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment