bf.ml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 rec string_of_program program =
  28. let string_of_command = function
  29. | Incptr -> ">"
  30. | Decptr -> "<"
  31. | Incdata -> "+"
  32. | Decdata -> "-"
  33. | Output -> "."
  34. | Input -> ","
  35. | Loop p -> "[" ^ string_of_program p ^ "]"
  36. in
  37. let rec cat buf = function
  38. | [] -> buf
  39. | cmd :: tl -> cat (buf ^ string_of_command cmd) tl
  40. in
  41. cat "" program
  42. let compile memsize program =
  43. let ctx = global_context () in
  44. let m = create_module ctx "brainfucker" in
  45. let byte_ty = i8_type ctx in
  46. let byteptr_ty = pointer_type byte_ty in
  47. let bool_ty = i1_type ctx in
  48. let i32_ty = i32_type ctx in
  49. let void_ty = void_type ctx in
  50. let putchar = declare_function "putchar" (function_type i32_ty [|byte_ty|]) m in
  51. let getchar = declare_function "getchar" (function_type byte_ty [||]) m in
  52. let cexit = declare_function "exit" (function_type void_ty [|i32_ty|]) m in
  53. (* use custom _start symbol rather than main function to reduce complexity *)
  54. let f = define_function "_start" (function_type void_ty [||]) m in
  55. let bb_cur = ref (entry_block f) in
  56. let b = builder_at_end ctx !bb_cur in
  57. let set_cur_bb bb =
  58. position_at_end bb b;
  59. bb_cur := bb
  60. in
  61. let i n = const_int i32_ty n in
  62. let i8 n = const_int byte_ty n in
  63. let mem = build_alloca (array_type byte_ty memsize) "mem" b in
  64. let idx = build_alloca i32_ty "idx" b in
  65. let gep () = build_in_bounds_gep mem [|i 0; build_load idx "" b|] "" b in
  66. let load ptr = build_load ptr "" b in
  67. let store ptr value = ignore (build_store value ptr b) in
  68. let rec compile_command = function
  69. | Incptr ->
  70. build_add (load idx) (i 1) "" b |> store idx
  71. | Decptr ->
  72. build_sub (load idx) (i 1) "" b |> store idx
  73. | Incdata ->
  74. build_add (load (gep ())) (i8 1) "" b |> store (gep ())
  75. | Decdata ->
  76. build_sub (load (gep ())) (i8 1) "" b |> store (gep ())
  77. | Output ->
  78. build_call putchar [|load (gep ())|] "" b |> ignore
  79. | Input ->
  80. build_call getchar [||] "" b |> store (gep ())
  81. | Loop p ->
  82. let bb_end = append_block ctx "" f in
  83. move_block_after !bb_cur bb_end;
  84. let bb_body = insert_block ctx "" bb_end in
  85. let bb_cond = insert_block ctx "" bb_body in
  86. build_br bb_cond b |> ignore;
  87. position_at_end bb_cond b;
  88. let cond = build_icmp Icmp.Eq (load (gep ())) (i8 0) "" b in
  89. build_cond_br cond bb_end bb_body b |> ignore;
  90. set_cur_bb bb_body;
  91. List.iter compile_command p;
  92. build_br bb_cond b |> ignore;
  93. set_cur_bb bb_end
  94. in
  95. (* zero-initialize memory (use intrinsic for optimization assumptions) *)
  96. set_data_layout "e" m; (* little-endian, needed for optimization *)
  97. let memset =
  98. let arg_types = [|byteptr_ty; byte_ty; i32_ty; i32_ty; bool_ty|] in
  99. declare_function "llvm.memset.p0i8.i32" (function_type void_ty arg_types) m
  100. in
  101. let ptr = build_bitcast mem byteptr_ty "" b in
  102. build_call memset [|ptr; i8 0; i memsize; i 0; const_int bool_ty 0|] "" b |> ignore;
  103. (* set pivot to index 0 and compile program commands *)
  104. store idx (i 0);
  105. List.iter compile_command program;
  106. (* exit gracefully *)
  107. build_call cexit [|i 0|] "" b |> ignore;
  108. build_ret_void b |> ignore;
  109. m
  110. let () =
  111. stdin |> read_program |> compile 30000 |> string_of_llmodule |> print_string