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 i w n = const_int (integer_type ctx w) n in
  36. let i8 = i 8 in
  37. let i32 = i 32 in
  38. let memset =
  39. let arg_types = [|byteptr_ty; byte_ty; i32_ty; i32_ty; bool_ty|] in
  40. declare_function "llvm.memset.p0i8.i32" (function_type void_ty arg_types) m
  41. in
  42. let putchar = declare_function "putchar" (function_type i32_ty [|byte_ty|]) m in
  43. let getchar = declare_function "getchar" (function_type byte_ty [||]) m in
  44. let cexit = declare_function "exit" (function_type void_ty [|i32_ty|]) m in
  45. (* use custom _start symbol rather than main function to reduce complexity *)
  46. let f = define_function "_start" (function_type void_ty [||]) m in
  47. let bb_cur = ref (entry_block f) in
  48. let b = builder_at_end ctx !bb_cur in
  49. let set_cur_bb bb =
  50. position_at_end bb b;
  51. bb_cur := bb
  52. in
  53. let mem = build_alloca (array_type byte_ty memsize) "mem" b in
  54. let ptr = build_alloca byteptr_ty "ptr" b in
  55. let load p = build_load p "" b in
  56. let store p value = ignore (build_store value p b) in
  57. let gep n = build_in_bounds_gep (load ptr) [|i32 n|] "" b in
  58. let rec compile_command = function
  59. | Incptr ->
  60. store ptr (gep 1)
  61. | Decptr ->
  62. store ptr (gep (-1))
  63. | Incdata ->
  64. build_add (load (gep 0)) (i8 1) "" b |> store (gep 0)
  65. | Decdata ->
  66. build_sub (load (gep 0)) (i8 1) "" b |> store (gep 0)
  67. | Output ->
  68. build_call putchar [|load (gep 0)|] "" b |> ignore
  69. | Input ->
  70. build_call getchar [||] "" b |> store (gep 0)
  71. | Loop p ->
  72. let bb_end = append_block ctx "" f in
  73. move_block_after !bb_cur bb_end;
  74. let bb_body = insert_block ctx "" bb_end in
  75. let bb_cond = insert_block ctx "" bb_body in
  76. build_br bb_cond b |> ignore;
  77. position_at_end bb_cond b;
  78. let cond = build_icmp Icmp.Eq (load (gep 0)) (i8 0) "" b in
  79. build_cond_br cond bb_end bb_body b |> ignore;
  80. set_cur_bb bb_body;
  81. List.iter compile_command p;
  82. build_br bb_cond b |> ignore;
  83. set_cur_bb bb_end
  84. in
  85. (* zero-initialize memory (use intrinsic for optimization assumptions) *)
  86. set_data_layout "e" m; (* little-endian, needed for optimization *)
  87. let memptr = build_bitcast mem byteptr_ty "" b in
  88. build_call memset [|memptr; i8 0; i32 memsize; i32 0; i 1 0|] "" b |> ignore;
  89. (* set pivot to index 0 and compile program commands *)
  90. build_in_bounds_gep mem [|i32 0; i32 0|] "" b |> store ptr;
  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