Skip to content

Commit

Permalink
implement a simple cpp program to count the most frequent control op …
Browse files Browse the repository at this point in the history
…in a bril program; include several test cases to test my implementation out
  • Loading branch information
jiahanxie353 committed Sep 2, 2023
1 parent 05bf45a commit f1b7845
Show file tree
Hide file tree
Showing 16 changed files with 1,080 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/.vscode
6 changes: 6 additions & 0 deletions lesson2/add.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@main {
v0: int = const 1;
v1: int = const 2;
v2: int = add v0 v1;
print v2;
}
36 changes: 36 additions & 0 deletions lesson2/add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"functions": [
{
"instrs": [
{
"dest": "v0",
"op": "const",
"type": "int",
"value": 1
},
{
"dest": "v1",
"op": "const",
"type": "int",
"value": 2
},
{
"args": [
"v0",
"v1"
],
"dest": "v2",
"op": "add",
"type": "int"
},
{
"args": [
"v2"
],
"op": "print"
}
],
"name": "main"
}
]
}
Empty file added lesson2/add.out
Empty file.
69 changes: 69 additions & 0 deletions lesson2/eight-queens.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
@main(input: int) {
n: int = id input;
zero: int = const 0;
icount: int = id zero;
site: ptr<int> = alloc n;
result: int = call @queen zero n icount site;
print result;
free site;
}
@queen(n: int, queens: int, icount: int, site: ptr<int>): int {
one: int = const 1;
ite: int = id one;
ret_cond: bool = eq n queens;
br ret_cond .next.ret .for.cond;
.next.ret:
icount: int = add icount one;
ret icount;
.for.cond:
for_cond_0: bool = le ite queens;
br for_cond_0 .for.body .next.ret.1;
.for.body:
nptr: ptr<int> = ptradd site n;
store nptr ite;
is_valid: bool = call @valid n site;
br is_valid .rec.func .next.loop;
.rec.func:
n_1: int = add n one;
icount: int = call @queen n_1 queens icount site;
.next.loop:
ite: int = add ite one;
jmp .for.cond;
.next.ret.1:
ret icount;
}
@valid(n: int, site: ptr<int>): bool {
zero: int = const 0;
one: int = const 1;
true: bool = eq one one;
false: bool = eq zero one;
ite: int = id zero;
.for.cond:
for_cond: bool = lt ite n;
br for_cond .for.body .ret.end;
.for.body:
iptr: ptr<int> = ptradd site ite;
nptr: ptr<int> = ptradd site n;
help_0: int = const 500;
vali: int = load iptr;
valn: int = load nptr;
eq_cond_0: bool = eq vali valn;
br eq_cond_0 .true.ret.0 .false.else;
.true.ret.0:
ret false;
.false.else:
sub_0: int = sub vali valn;
sub_1: int = sub valn vali;
sub_2: int = sub n ite;
eq_cond_1: bool = eq sub_0 sub_2;
eq_cond_2: bool = eq sub_1 sub_2;
eq_cond_12: bool = or eq_cond_1 eq_cond_2;
br eq_cond_12 .true.ret.1 .false.loop;
.true.ret.1:
ret false;
.false.loop:
ite: int = add ite one;
jmp .for.cond;
.ret.end:
ret true;
}
Loading

0 comments on commit f1b7845

Please sign in to comment.