Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
brainfucker
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
brainfucker
Commits
9a3c281f
Commit
9a3c281f
authored
Jan 10, 2015
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Now using pointer instead of array index
parent
17ead78e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
42 deletions
+43
-42
README.md
README.md
+21
-20
bf.ml
bf.ml
+22
-22
No files found.
README.md
View file @
9a3c281f
...
...
@@ -72,36 +72,37 @@ The compiler generates very verbose code:
entry:
; initialization
%mem = alloca [30000 x i8]
%
idx = alloca i32
%
ptr = alloca i8*
%0 = bitcast [30000 x i8]* %mem to i8*
call void @llvm.memset.p0i8.i32(i8* %0, i8 0, i32 30000, i32 0, i1 false)
store i32 0, i32* %idx
%1 = getelementptr inbounds [30000 x i8]* %mem, i32 0, i32 0
store i8* %1, i8** %ptr
; command: +
%
1 = load i32* %idx
%
2 = getelementptr inbounds [30000 x i8]* %mem, i32 0, i32 %1
%
3 = load i8* %2
%
4 = add i8 %3
, 1
%
5 = load i32* %idx
%
6 = getelementptr inbounds [30000 x i8]* %mem, i32 0, i32 %5
store i8 %
4, i8* %6
%
2 = load i8** %ptr
%
3 = getelementptr inbounds i8* %2, i32 0
%
4 = load i8* %3
%
5 = add i8 %4
, 1
%
6 = load i8** %ptr
%
7 = getelementptr inbounds i8* %6, i32 0
store i8 %
5, i8* %7
; command: +
%
7 = load i32* %idx
%
8 = getelementptr inbounds [30000 x i8]* %mem, i32 0, i32 %7
%
9 = load i8* %8
%1
0 = add i8 %9
, 1
%1
1 = load i32* %idx
%1
2 = getelementptr inbounds [30000 x i8]* %mem, i32 0, i32 %11
store i8 %1
0, i8* %12
%
8 = load i8** %ptr
%
9 = getelementptr inbounds i8* %8, i32 0
%
10 = load i8* %9
%1
1 = add i8 %10
, 1
%1
2 = load i8** %ptr
%1
3 = getelementptr inbounds i8* %12, i32 0
store i8 %1
1, i8* %13
...
; command: . (outputs 'H' after 72 times a '+' command)
%43
3 = load i32* %idx
%43
4 = getelementptr inbounds [30000 x i8]* %mem, i32 0, i32 %433
%43
5 = load i8* %434
%43
6 = call i32 @putchar(i8 %435
)
%43
4 = load i8** %ptr
%43
5 = getelementptr inbounds i8* %434, i32 0
%43
6 = load i8* %435
%43
7 = call i32 @putchar(i8 %436
)
...
...
...
bf.ml
View file @
9a3c281f
...
...
@@ -37,6 +37,14 @@ let compile memsize program =
let
i32_ty
=
i32_type
ctx
in
let
void_ty
=
void_type
ctx
in
let
i
w
n
=
const_int
(
integer_type
ctx
w
)
n
in
let
i8
=
i
8
in
let
i32
=
i
32
in
let
memset
=
let
arg_types
=
[
|
byteptr_ty
;
byte_ty
;
i32_ty
;
i32_ty
;
bool_ty
|
]
in
declare_function
"llvm.memset.p0i8.i32"
(
function_type
void_ty
arg_types
)
m
in
let
putchar
=
declare_function
"putchar"
(
function_type
i32_ty
[
|
byte_ty
|
])
m
in
let
getchar
=
declare_function
"getchar"
(
function_type
byte_ty
[
||
])
m
in
let
cexit
=
declare_function
"exit"
(
function_type
void_ty
[
|
i32_ty
|
])
m
in
...
...
@@ -51,30 +59,26 @@ let compile memsize program =
bb_cur
:=
bb
in
let
i
w
n
=
const_int
(
integer_type
ctx
w
)
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
idx
=
build_alloca
i32_ty
"idx
"
b
in
let
ptr
=
build_alloca
byteptr_ty
"ptr
"
b
in
let
load
p
tr
=
build_load
ptr
""
b
in
let
store
p
tr
value
=
ignore
(
build_store
value
ptr
b
)
in
let
gep
()
=
build_in_bounds_gep
mem
[
|
i32
0
;
load
idx
|
]
""
b
in
let
load
p
=
build_load
p
""
b
in
let
store
p
value
=
ignore
(
build_store
value
p
b
)
in
let
gep
n
=
build_in_bounds_gep
(
load
ptr
)
[
|
i32
n
|
]
""
b
in
let
rec
compile_command
=
function
|
Incptr
->
build_add
(
load
idx
)
(
i32
1
)
""
b
|>
store
idx
store
ptr
(
gep
1
)
|
Decptr
->
build_sub
(
load
idx
)
(
i32
1
)
""
b
|>
store
idx
store
ptr
(
gep
(
-
1
))
|
Incdata
->
build_add
(
load
(
gep
()
))
(
i8
1
)
""
b
|>
store
(
gep
()
)
build_add
(
load
(
gep
0
))
(
i8
1
)
""
b
|>
store
(
gep
0
)
|
Decdata
->
build_sub
(
load
(
gep
()
))
(
i8
1
)
""
b
|>
store
(
gep
()
)
build_sub
(
load
(
gep
0
))
(
i8
1
)
""
b
|>
store
(
gep
0
)
|
Output
->
build_call
putchar
[
|
load
(
gep
()
)
|
]
""
b
|>
ignore
build_call
putchar
[
|
load
(
gep
0
)
|
]
""
b
|>
ignore
|
Input
->
build_call
getchar
[
||
]
""
b
|>
store
(
gep
()
)
build_call
getchar
[
||
]
""
b
|>
store
(
gep
0
)
|
Loop
p
->
let
bb_end
=
append_block
ctx
""
f
in
move_block_after
!
bb_cur
bb_end
;
...
...
@@ -83,7 +87,7 @@ let compile memsize program =
build_br
bb_cond
b
|>
ignore
;
position_at_end
bb_cond
b
;
let
cond
=
build_icmp
Icmp
.
Eq
(
load
(
gep
()
))
(
i8
0
)
""
b
in
let
cond
=
build_icmp
Icmp
.
Eq
(
load
(
gep
0
))
(
i8
0
)
""
b
in
build_cond_br
cond
bb_end
bb_body
b
|>
ignore
;
set_cur_bb
bb_body
;
...
...
@@ -95,15 +99,11 @@ let compile memsize program =
(* zero-initialize memory (use intrinsic for optimization assumptions) *)
set_data_layout
"e"
m
;
(* little-endian, needed for optimization *)
let
memset
=
let
arg_types
=
[
|
byteptr_ty
;
byte_ty
;
i32_ty
;
i32_ty
;
bool_ty
|
]
in
declare_function
"llvm.memset.p0i8.i32"
(
function_type
void_ty
arg_types
)
m
in
let
ptr
=
build_bitcast
mem
byteptr_ty
""
b
in
build_call
memset
[
|
ptr
;
i8
0
;
i32
memsize
;
i32
0
;
i
1
0
|
]
""
b
|>
ignore
;
let
memptr
=
build_bitcast
mem
byteptr_ty
""
b
in
build_call
memset
[
|
memptr
;
i8
0
;
i32
memsize
;
i32
0
;
i
1
0
|
]
""
b
|>
ignore
;
(* set pivot to index 0 and compile program commands *)
store
idx
(
i32
0
)
;
build_in_bounds_gep
mem
[
|
i32
0
;
i32
0
|
]
""
b
|>
store
ptr
;
List
.
iter
compile_command
program
;
(* exit gracefully *)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment