Skip to content

Commit

Permalink
Changes to display in required format
Browse files Browse the repository at this point in the history
  • Loading branch information
SwathiSBhat committed Feb 28, 2024
1 parent f467ea4 commit e83df43
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 40 deletions.
131 changes: 130 additions & 1 deletion headers/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ class ExternalFunction {
class Instruction{
public:
virtual void pretty_print() {};
virtual std::string ToString() { return ""; };
InstructionType instrType;
};

Expand Down Expand Up @@ -393,6 +394,10 @@ class AddrofInstruction : public Instruction{
}
}

std::string ToString() {
return lhs->name + " = $addrof " + rhs->name;
}

void pretty_print() {
std::cout << "******************* Addrof Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -430,6 +435,11 @@ class AllocInstruction : public Instruction{
}
}

std::string ToString() {
std::string num_str = (num->var != nullptr) ? num->var->name : std::to_string(num->val);
return lhs->name + " = $alloc " + num_str + " " + "[" + id->name + "]";
}

void pretty_print() {
std::cout << "******************* Alloc Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -476,6 +486,21 @@ class ArithInstruction : public Instruction{
}
}

std::string ToString() {
std::string op1_str = (op1->var != nullptr) ? op1->var->name : std::to_string(op1->val);
std::string op2_str = (op2->var != nullptr) ? op2->var->name : std::to_string(op2->val);
std::string aop_str = "";
if (arith_op == "Add")
aop_str = "add";
else if (arith_op == "Subtract")
aop_str = "sub";
else if (arith_op == "Multiply")
aop_str = "mul";
else if (arith_op == "Divide")
aop_str = "div";
return lhs->name + " = $arith " + aop_str + " " + op1_str + " " + op2_str;
}

void pretty_print() {
std::cout << "******************* Arith Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -523,6 +548,25 @@ class CmpInstruction : public Instruction{
}
}

std::string ToString() {
std::string op1_str = (op1->var != nullptr) ? op1->var->name : std::to_string(op1->val);
std::string op2_str = (op2->var != nullptr) ? op2->var->name : std::to_string(op2->val);
std::string rop_str = "";
if (cmp_op == "Eq")
rop_str = "eq";
else if (cmp_op == "Neq")
rop_str = "neq";
else if (cmp_op == "Less")
rop_str = "lt";
else if (cmp_op == "LessEq")
rop_str = "lte";
else if (cmp_op == "Greater")
rop_str = "gt";
else if (cmp_op == "GreaterEq")
rop_str = "gte";
return lhs->name + " = $cmp " + rop_str + " " + op1_str + " " + op2_str;
}

void pretty_print() {
std::cout << "******************* Cmp Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -562,6 +606,11 @@ class CopyInstruction : public Instruction{
}
}

std::string ToString() {
std::string op_str = (op->var != nullptr) ? op->var->name : std::to_string(op->val);
return lhs->name + " = $copy " + op_str;
}

void pretty_print() {
std::cout << "******************* Copy Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -599,6 +648,11 @@ class GepInstruction : public Instruction{
}
}

std::string ToString() {
std::string idx_str = (idx->var != nullptr) ? idx->var->name : std::to_string(idx->val);
return lhs->name + " = $gep " + src->name + " " + idx_str;
}

void pretty_print() {
std::cout << "******************* Gep Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -635,6 +689,11 @@ class GfpInstruction : public Instruction{
}
}

std::string ToString()
{
return lhs->name + " = $gfp " + src->name + " " + field->name;
}

void pretty_print() {
std::cout << "******************* Gfp Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -668,6 +727,11 @@ class LoadInstruction : public Instruction{
}
}

std::string ToString()
{
return lhs->name + " = $load " + src->name;
}

void pretty_print() {
std::cout << "******************* Load Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -701,6 +765,12 @@ class StoreInstruction : public Instruction {
}
}

std::string ToString()
{
std::string op_str = (op->var != nullptr) ? op->var->name : std::to_string(op->val);
return "$store " + dst->name + " " + op_str;
}

void pretty_print() {
std::cout << "******************* Store Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -731,7 +801,7 @@ class CallExtInstruction : public Instruction{
lhs = new Variable(inst_val["lhs"]["name"], new Type(inst_val["lhs"]["typ"]));
}
if (inst_val["ext_callee"] != nullptr) {
extFuncName = inst_val["ext_callee"].dump();
extFuncName = inst_val["ext_callee"];
}
if (inst_val["args"] != nullptr) {
for (auto &[arg_key, arg_val] : inst_val["args"].items()) {
Expand All @@ -743,6 +813,19 @@ class CallExtInstruction : public Instruction{
}
}

std::string ToString() {
std::string args_str = "";
int i = 0;
for (auto arg : args) {
args_str += (arg->var != nullptr) ? arg->var->name : std::to_string(arg->val);
if (i != args.size() - 1)
args_str += ", ";
i += 1;
}
std::string lhs_str = (lhs != nullptr) ? lhs->name + " = " : "";
return lhs_str + "$call_ext " + extFuncName + "(" + args_str + ")";
}

void pretty_print() {
std::cout << "******************* CallExt Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -791,6 +874,12 @@ class BranchInstruction : public Instruction{
}
};

std::string ToString()
{
std::string cond_str = (condition->var != nullptr) ? condition->var->name : std::to_string(condition->val);
return "$branch " + cond_str + " " + tt + " " + ff;
}

void pretty_print() {
std::cout << "******************* Branch Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand All @@ -816,6 +905,10 @@ class JumpInstruction : public Instruction{
// std::cout << inst_val << std::endl;
};

std::string ToString() {
return "$jump " + label;
}

void pretty_print() {
std::cout << "******************* Jump Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -848,6 +941,14 @@ class RetInstruction : public Instruction{
op = new Operand(inst_val["CInt"]);
}

std::string ToString()
{
if (op == nullptr)
return "$ret";
else
return "$ret " + ((op->var != nullptr) ? op->var->name : std::to_string(op->val));
}

void pretty_print() {
std::cout << "******************* Ret Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -892,6 +993,20 @@ class CallDirInstruction : public Instruction{
}
}

std::string ToString()
{
std::string args_str = "";
int i = 0;
for (auto arg : args) {
args_str += (arg->var != nullptr) ? arg->var->name : std::to_string(arg->val);
if (i != args.size() - 1)
args_str += ", ";
i += 1;
}
std::string lhs_str = (lhs != nullptr) ? lhs->name + " = " : "";
return lhs_str + "$call_dir " + callee + "(" + args_str + ") then " + next_bb;
}

void pretty_print() {
std::cout << "******************* CallDir Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down Expand Up @@ -946,6 +1061,20 @@ class CallIdrInstruction : public Instruction{
}
}

std::string ToString()
{
std::string args_str = "";
int i = 0;
for (auto arg : args) {
args_str += (arg->var != nullptr) ? arg->var->name : std::to_string(arg->val);
if (i != args.size() - 1)
args_str += ", ";
i += 1;
}
std::string lhs_str = (lhs != nullptr) ? lhs->name + " = " : "";
return lhs_str + "$call_idr " + fp->name + "(" + args_str + ") then " + next_bb;
}

void pretty_print() {
std::cout << "******************* CallIdr Instruction *******************" << std::endl;
std::cout << "Instruction type: " << instrType << std::endl;
Expand Down
Empty file.
30 changes: 30 additions & 0 deletions program-dependence-graph/pdg-tests/test.3.lir
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extern input:(int,&int) -> int

g1:int
g2:&int

fn main() -> int {
let i:int, j:int, x:int, y:int, z:int, tmp:int, a:&int

entry:
x = $call_ext input(tmp,a)
y = $copy 0
z = $copy 1
i = $copy 0
j = $copy 1
$jump loop_hdr

loop_hdr:
tmp = $cmp lt i x
$branch tmp loop_body exit

loop_body:
y = $arith add y 2
z = $arith mul z j
i = $arith add i 1
j = $arith add j 2
$jump loop_hdr

exit:
$ret y
}
1 change: 1 addition & 0 deletions program-dependence-graph/pdg-tests/test.3.lir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"structs":{},"globals":[{"name":"g1","typ":"Int","scope":null},{"name":"g2","typ":{"Pointer":"Int"},"scope":null}],"functions":{"main":{"id":"main","ret_ty":"Int","params":[],"locals":[{"name":"a","typ":{"Pointer":"Int"},"scope":"main"},{"name":"i","typ":"Int","scope":"main"},{"name":"j","typ":"Int","scope":"main"},{"name":"tmp","typ":"Int","scope":"main"},{"name":"x","typ":"Int","scope":"main"},{"name":"y","typ":"Int","scope":"main"},{"name":"z","typ":"Int","scope":"main"}],"body":{"entry":{"id":"entry","insts":[{"CallExt":{"lhs":{"name":"x","typ":"Int","scope":"main"},"ext_callee":"input","args":[{"Var":{"name":"tmp","typ":"Int","scope":"main"}},{"Var":{"name":"a","typ":{"Pointer":"Int"},"scope":"main"}}]}},{"Copy":{"lhs":{"name":"y","typ":"Int","scope":"main"},"op":{"CInt":0}}},{"Copy":{"lhs":{"name":"z","typ":"Int","scope":"main"},"op":{"CInt":1}}},{"Copy":{"lhs":{"name":"i","typ":"Int","scope":"main"},"op":{"CInt":0}}},{"Copy":{"lhs":{"name":"j","typ":"Int","scope":"main"},"op":{"CInt":1}}}],"term":{"Jump":"loop_hdr"}},"exit":{"id":"exit","insts":[],"term":{"Ret":{"Var":{"name":"y","typ":"Int","scope":"main"}}}},"loop_body":{"id":"loop_body","insts":[{"Arith":{"lhs":{"name":"y","typ":"Int","scope":"main"},"aop":"Add","op1":{"Var":{"name":"y","typ":"Int","scope":"main"}},"op2":{"CInt":2}}},{"Arith":{"lhs":{"name":"z","typ":"Int","scope":"main"},"aop":"Multiply","op1":{"Var":{"name":"z","typ":"Int","scope":"main"}},"op2":{"Var":{"name":"j","typ":"Int","scope":"main"}}}},{"Arith":{"lhs":{"name":"i","typ":"Int","scope":"main"},"aop":"Add","op1":{"Var":{"name":"i","typ":"Int","scope":"main"}},"op2":{"CInt":1}}},{"Arith":{"lhs":{"name":"j","typ":"Int","scope":"main"},"aop":"Add","op1":{"Var":{"name":"j","typ":"Int","scope":"main"}},"op2":{"CInt":2}}}],"term":{"Jump":"loop_hdr"}},"loop_hdr":{"id":"loop_hdr","insts":[{"Cmp":{"lhs":{"name":"tmp","typ":"Int","scope":"main"},"rop":"Less","op1":{"Var":{"name":"i","typ":"Int","scope":"main"}},"op2":{"Var":{"name":"x","typ":"Int","scope":"main"}}}}],"term":{"Branch":{"cond":{"Var":{"name":"tmp","typ":"Int","scope":"main"}},"tt":"loop_body","ff":"exit"}}}}}},"externs":{"input":{"Function":{"ret_ty":"Int","param_ty":["Int",{"Pointer":"Int"}]}}}}
Loading

0 comments on commit e83df43

Please sign in to comment.