bf.ml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. open Llvm
  2. type program = command list
  3. and command =
  4. | Incptr | Decptr
  5. | Incdata | Decdata
  6. | Output | Input
  7. | Loop of program
  8. let read_program ic =
  9. let rec next cur stack =
  10. try
  11. match input_char ic, stack with
  12. | '>', _ -> next (Incptr :: cur) stack
  13. | '<', _ -> next (Decptr :: cur) stack
  14. | '+', _ -> next (Incdata :: cur) stack
  15. | '-', _ -> next (Decdata :: cur) stack
  16. | '.', _ -> next (Output :: cur) stack
  17. | ',', _ -> next (Input :: cur) stack
  18. | '[', _ -> next [] (cur :: stack)
  19. | ']', [] -> failwith "unmatched ']'"
  20. | ']', (hd :: tl) -> next (Loop (List.rev cur) :: hd) tl
  21. | _ -> next cur stack
  22. with End_of_file ->
  23. if List.length stack > 0 then failwith "unmatched '['";
  24. List.rev cur
  25. in
  26. next [] []
  27. let compile memsize program =
  28. let ctx = global_context () in
  29. let m = create_module ctx "brainfucker" in
  30. let byte_ty = i8_type ctx in
  31. let byteptr_ty = pointer_type byte_ty in
  32. let bool_ty = i1_type ctx in
  33. let i32_ty = i32_type ctx in
  34. let void_ty = void_type ctx in
  35. let putchar = declare_function "putchar" (function_type i32_ty [|byte_ty|]) m in
  36. let getchar = declare_function "getchar" (function_type byte_ty [||]) m in
  37. let cexit = declare_function "exit" (function_type void_ty [|i32_ty|]) m in
  38. (* use custom _start symbol rather than main function to reduce complexity *)
  39. let f = define_function "_start" (function_type void_ty [||]) m in
  40. let bb_cur = ref (entry_block f) in
  41. let b = builder_at_end ctx !bb_cur in
  42. let set_cur_bb bb =
  43. position_at_end bb b;
  44. bb_cur := bb
  45. in
  46. let i w n = const_int (integer_type ctx w) n in
  47. let i8 = i 8 in
  48. let i32 = i 32 in
  49. let mem = build_alloca (array_type byte_ty memsize) "mem" b in
  50. let idx = build_alloca i32_ty "idx" b in
  51. let load ptr = build_load ptr "" b in
  52. let store ptr value = ignore (build_store value ptr b) in
  53. let gep () = build_in_bounds_gep mem [|i32 0; load idx|] "" b in
  54. let rec compile_command = function
  55. | Incptr ->
  56. build_add (load idx) (i32 1) "" b |> store idx
  57. | Decptr ->
  58. build_sub (load idx) (i32 1) "" b |> store idx
  59. | Incdata ->
  60. build_add (load (gep ())) (i8 1) "" b |> store (gep ())
  61. | Decdata ->
  62. build_sub (load (gep ())) (i8 1) "" b |> store (gep ())
  63. | Output ->
  64. build_call putchar [|load (gep ())|] "" b |> ignore
  65. | Input ->
  66. build_call getchar [||] "" b |> store (gep ())
  67. | Loop p ->
  68. let bb_end = append_block ctx "" f in
  69. move_block_after !bb_cur bb_end;
  70. let bb_body = insert_block ctx "" bb_end in
  71. let bb_cond = insert_block ctx "" bb_body in
  72. build_br bb_cond b |> ignore;
  73. position_at_end bb_cond b;
  74. let cond = build_icmp Icmp.Eq (load (gep ())) (i8 0) "" b in
  75. build_cond_br cond bb_end bb_body b |> ignore;
  76. set_cur_bb bb_body;
  77. List.iter compile_command p;
  78. build_br bb_cond b |> ignore;
  79. set_cur_bb bb_end
  80. in
  81. (* zero-initialize memory (use intrinsic for optimization assumptions) *)
  82. set_data_layout "e" m; (* little-endian, needed for optimization *)
  83. let memset =
  84. let arg_types = [|byteptr_ty; byte_ty; i32_ty; i32_ty; bool_ty|] in
  85. declare_function "llvm.memset.p0i8.i32" (function_type void_ty arg_types) m
  86. in
  87. let ptr = build_bitcast mem byteptr_ty "" b in
  88. build_call memset [|ptr; i8 0; i32 memsize; i32 0; i 1 0|] "" b |> ignore;
  89. (* set pivot to index 0 and compile program commands *)
  90. store idx (i32 0);
  91. List.iter compile_command program;
  92. (* exit gracefully *)
  93. build_call cexit [|i32 0|] "" b |> ignore;
  94. build_ret_void b |> ignore;
  95. m
  96. let () =
  97. stdin |> read_program |> compile 30000 |> string_of_llmodule |> print_string