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
50b028d2
Commit
50b028d2
authored
Jan 08, 2015
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code + makefile cleanup
parent
bf263fa2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
19 deletions
+18
-19
Makefile
Makefile
+5
-4
bf.ml
bf.ml
+13
-15
No files found.
Makefile
View file @
50b028d2
LD
:=
clang
LD
:=
clang
LDFLAGS
:=
-nostartfiles
LDFLAGS
:=
-nostartfiles
BFILES
:=
$(
wildcard
*
.b
)
BFILES
:=
$(
patsubst
%.b,%,
$(
wildcard
*
.b
)
)
.PHONY
:
clean
.PHONY
:
clean
.PRECIOUS
:
$(
patsubst %.b
,
%.ll %
-opt.ll
,
$(BFILES))
.PRECIOUS
:
$(
addsuffix .ll
,
$(BFILES)) $(addsuffix
-opt.ll
,
$(BFILES))
bf
:
bf.ml
bf
:
bf.ml
ocamlopt
-o
$@
-g
-I
/usr/lib/ocaml/llvm-3.5 llvm.cmxa
$^
ocamlopt
-o
$@
-g
-I
/usr/lib/ocaml/llvm-3.5 llvm.cmxa
$<
rm
-f
$@
.cmi
$@
.cmx
$@
.o
%
:
%.o
%
:
%.o
$(LD)
-o
$@
$<
$(LDFLAGS)
$(LD)
-o
$@
$<
$(LDFLAGS)
...
@@ -24,4 +25,4 @@ bf: bf.ml
...
@@ -24,4 +25,4 @@ bf: bf.ml
./bf <
$<
>
$@
./bf <
$<
>
$@
clean
:
clean
:
rm
-f
bf
*
.cmi
*
.cmx
*
.ll
*
.bc
*
.o
$(
patsubst
%.b,%
,
$(BFILES)
)
rm
-f
bf
*
.cmi
*
.cmx
*
.ll
*
.bc
*
.o
$(
BFILES)
$(
addsuffix
-opt
,
$(BFILES)
)
bf.ml
View file @
50b028d2
...
@@ -49,14 +49,15 @@ let compile memsize program =
...
@@ -49,14 +49,15 @@ let compile memsize program =
let
byte_ty
=
i8_type
ctx
in
let
byte_ty
=
i8_type
ctx
in
let
byteptr_ty
=
pointer_type
byte_ty
in
let
byteptr_ty
=
pointer_type
byte_ty
in
let
i1
_ty
=
i1_type
ctx
in
let
bool
_ty
=
i1_type
ctx
in
let
i32_ty
=
i32_type
ctx
in
let
i32_ty
=
i32_type
ctx
in
let
int_ty
=
i32_ty
in
let
void_ty
=
void_type
ctx
in
let
void_ty
=
void_type
ctx
in
let
putchar
=
declare_function
"putchar"
(
function_type
i
nt
_ty
[
|
byte_ty
|
])
m
in
let
putchar
=
declare_function
"putchar"
(
function_type
i
32
_ty
[
|
byte_ty
|
])
m
in
let
getchar
=
declare_function
"getchar"
(
function_type
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
(* use custom _start symbol rather than main function to reduce complexity *)
let
f
=
define_function
"_start"
(
function_type
void_ty
[
||
])
m
in
let
f
=
define_function
"_start"
(
function_type
void_ty
[
||
])
m
in
let
bb_cur
=
ref
(
entry_block
f
)
in
let
bb_cur
=
ref
(
entry_block
f
)
in
let
b
=
builder_at_end
ctx
!
bb_cur
in
let
b
=
builder_at_end
ctx
!
bb_cur
in
...
@@ -66,13 +67,11 @@ let compile memsize program =
...
@@ -66,13 +67,11 @@ let compile memsize program =
bb_cur
:=
bb
bb_cur
:=
bb
in
in
let
i
n
=
const_int
i
nt
_ty
n
in
let
i
n
=
const_int
i
32
_ty
n
in
let
i8
n
=
const_int
byte_ty
n
in
let
i8
n
=
const_int
byte_ty
n
in
(*let mem = define_global "mem" (const_null (array_type byte_ty memsize)) m in
set_linkage Linkage.Private mem;*)
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
i
nt
_ty
"idx"
b
in
let
idx
=
build_alloca
i
32
_ty
"idx"
b
in
let
gep
()
=
build_in_bounds_gep
mem
[
|
i
0
;
build_load
idx
""
b
|
]
""
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
...
@@ -97,14 +96,14 @@ let compile memsize program =
...
@@ -97,14 +96,14 @@ let compile memsize program =
let
bb_body
=
insert_block
ctx
""
bb_end
in
let
bb_body
=
insert_block
ctx
""
bb_end
in
let
bb_cond
=
insert_block
ctx
""
bb_body
in
let
bb_cond
=
insert_block
ctx
""
bb_body
in
ignore
(
build_br
bb_cond
b
)
;
build_br
bb_cond
b
|>
ignore
;
position_at_end
bb_cond
b
;
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
()
))
(
i8
0
)
""
b
in
ignore
(
build_cond_br
cond
bb_end
bb_body
b
)
;
build_cond_br
cond
bb_end
bb_body
b
|>
ignore
;
set_cur_bb
bb_body
;
set_cur_bb
bb_body
;
List
.
iter
compile_command
p
;
List
.
iter
compile_command
p
;
ignore
(
build_br
bb_cond
b
)
;
build_br
bb_cond
b
|>
ignore
;
set_cur_bb
bb_end
set_cur_bb
bb_end
in
in
...
@@ -112,20 +111,19 @@ let compile memsize program =
...
@@ -112,20 +111,19 @@ let compile memsize program =
(* zero-initialize memory (use intrinsic for optimization assumptions) *)
(* zero-initialize memory (use intrinsic for optimization assumptions) *)
set_data_layout
"e"
m
;
(* little-endian, needed for optimization *)
set_data_layout
"e"
m
;
(* little-endian, needed for optimization *)
let
memset
=
let
memset
=
let
arg_types
=
[
|
byteptr_ty
;
byte_ty
;
i32_ty
;
i32_ty
;
i1
_ty
|
]
in
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
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
i1
_ty
0
|
]
""
b
|>
ignore
;
build_call
memset
[
|
ptr
;
i8
0
;
i
memsize
;
i
0
;
const_int
bool
_ty
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
(
i
0
);
List
.
iter
compile_command
program
;
List
.
iter
compile_command
program
;
(* exit gracefully *)
(* exit gracefully *)
let
cexit
=
declare_function
"exit"
(
function_type
void_ty
[
|
i32_ty
|
])
m
in
build_call
cexit
[
|
i
0
|
]
""
b
|>
ignore
;
ignore
(
build_call
cexit
[
|
i
0
|
]
""
b
);
build_ret_void
b
|>
ignore
;
ignore
(
build_ret_void
b
);
m
m
let
()
=
let
()
=
...
...
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