Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rot13 benchmark #375

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions benchmarks/core/rot13.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ARGS: 10
# Compute the char shifted forward by 13 (0-indexed)
@main(input: int) {
aed: int = call @rot input;
print aed;
}
@rot(n: int):int{
thirteen: int = const 13;
twenty_six: int = const 26;
shifted: int = add n thirteen;
over: bool = ge shifted twenty_six;
br over .if .else;
.if:
shifted: int = sub shifted twenty_six;
.else:
ret shifted;
}
1 change: 1 addition & 0 deletions benchmarks/core/rot13.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions benchmarks/core/rot13.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 8
205 changes: 205 additions & 0 deletions cs6120/lesson_2/ackermann.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"functions": [
{
"args": [
{
"name": "m",
"type": "int"
},
{
"name": "n",
"type": "int"
}
],
"instrs": [
{
"dest": "zero",
"op": "const",
"type": "int",
"value": 0
},
{
"dest": "one",
"op": "const",
"type": "int",
"value": 1
},
{
"args": [
"m",
"zero"
],
"dest": "cond_m",
"op": "eq",
"type": "bool"
},
{
"args": [
"cond_m"
],
"labels": [
"m_zero",
"m_nonzero"
],
"op": "br"
},
{
"label": "m_zero"
},
{
"args": [
"n",
"one"
],
"dest": "tmp",
"op": "add",
"type": "int"
},
{
"args": [
"tmp"
],
"op": "ret"
},
{
"label": "m_nonzero"
},
{
"args": [
"n",
"zero"
],
"dest": "cond_n",
"op": "eq",
"type": "bool"
},
{
"args": [
"cond_n"
],
"labels": [
"n_zero",
"n_nonzero"
],
"op": "br"
},
{
"label": "n_zero"
},
{
"args": [
"m",
"one"
],
"dest": "m1",
"op": "sub",
"type": "int"
},
{
"args": [
"m1",
"one"
],
"dest": "tmp",
"funcs": [
"ack"
],
"op": "call",
"type": "int"
},
{
"args": [
"tmp"
],
"op": "ret"
},
{
"label": "n_nonzero"
},
{
"args": [
"m",
"one"
],
"dest": "m1",
"op": "sub",
"type": "int"
},
{
"args": [
"n",
"one"
],
"dest": "n1",
"op": "sub",
"type": "int"
},
{
"args": [
"m",
"n1"
],
"dest": "t1",
"funcs": [
"ack"
],
"op": "call",
"type": "int"
},
{
"args": [
"m1",
"t1"
],
"dest": "t2",
"funcs": [
"ack"
],
"op": "call",
"type": "int"
},
{
"args": [
"t2"
],
"op": "ret"
}
],
"name": "ack",
"type": "int"
},
{
"args": [
{
"name": "m",
"type": "int"
},
{
"name": "n",
"type": "int"
}
],
"instrs": [
{
"args": [
"m",
"n"
],
"dest": "tmp",
"funcs": [
"ack"
],
"op": "call",
"type": "int"
},
{
"args": [
"tmp"
],
"op": "print"
}
],
"name": "main"
}
]
}
2 changes: 2 additions & 0 deletions cs6120/lesson_2/ackermann.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ack
main
35 changes: 35 additions & 0 deletions cs6120/lesson_2/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json
import sys

terminators = ('br', 'jmp', 'ret')

def blocks(filename):
with open(filename) as file:
program = json.load(file)
function = program["functions"][0]["instrs"]
cur_block = (None, [])
all_blocks = [cur_block]
for instr in function:
if "op" in instr:
cur_block[1].append(instr)
if(instr["op"] in terminators):
cur_block = (None, [])
all_blocks.append(cur_block)
else:
# blocks that are empty and have no labels can safely be removed
if(len(cur_block[1]) == 0 and cur_block[0] == None):
all_blocks = all_blocks[:-1]
cur_block = (instr["label"], [])
all_blocks.append(cur_block)
# remove empty last block
if(len(cur_block[1]) == 0 and cur_block[0] == None):
all_blocks = all_blocks[:-1]
return all_blocks


if __name__ == "__main__":
filename = sys.argv[1]
all_blocks = blocks(filename)
for (label, contents) in all_blocks:
print(label)
print(contents)
9 changes: 9 additions & 0 deletions cs6120/lesson_2/branch_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json
import sys

if __name__ == "__main__":
filename = sys.argv[1]
with open(filename) as file:
program = json.load(file)
for function in program["functions"]:
print(function["name"])
61 changes: 61 additions & 0 deletions cs6120/lesson_2/cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import sys
import blocks

def change_labels(all_blocks):
new_name = "label "
new_num = 0
all_labels = [block[0] for block in all_blocks]
for i in range(len(all_labels)):
if(all_labels[i] == None):
while((new_name + str(new_num)) in all_labels):
new_num += 1
all_labels[i] = new_name + str(new_num)

for i in range(len(all_blocks)):
all_blocks[i] = (all_labels[i], all_blocks[i][1])
return all_blocks

def build_cfg(all_blocks):
successors = {"entry": [], "exit": []}
for block in all_blocks:
successors[block[0]] = []
# dummy entry block
successors["entry"] = [all_blocks[0][0]]

for i in range(len(all_blocks)):
# last entry is ret -> successor = exit
# last entry is br -> successors = br targets
# last entry is jmp -> successor = jmp
# last entry is nothing -> successor = next block (or exit if last block)
last_entry = all_blocks[i][1][-1]
if(last_entry["op"] == "br" or last_entry["op"] == "jmp"):
successors[all_blocks[i][0]] = last_entry["labels"]
elif(last_entry["op"] == "ret"):
successors[all_blocks[i][0]] = ["exit"]
else:
if(i == len(all_blocks) - 1):
successors[all_blocks[i][0]] = ["exit"]
else:
successors[all_blocks[i][0]] = [all_blocks[i + 1][0]]
return successors

def remove_orphans(all_blocks, cfg):
visited = {}
for block in all_blocks:
visited[block[0]] = False

for entry in cfg:
for successor in cfg[entry]:
visited[successor] = True

parsed_blocks = []
for block in all_blocks:
if visited[block[0]]:
parsed_blocks.append(block)
return parsed_blocks
if __name__ == "__main__":
filename = sys.argv[1]
all_blocks = change_labels(blocks.blocks(filename))
cfg = build_cfg(all_blocks)
print(cfg)
# print(remove_orphans(all_blocks, cfg))
3 changes: 3 additions & 0 deletions cs6120/lesson_2/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"functions": []
}
Empty file added cs6120/lesson_2/empty.out
Empty file.
Loading
Loading