Skip to content
Open
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
23 changes: 22 additions & 1 deletion ChironCore/ChironAST/ChironAST.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class Value(Expression):

class Num(Value):
def __init__(self, v):
self.val = int(v)
self.val = float(v)

def __str__(self):
return str(self.val)
Expand All @@ -233,3 +233,24 @@ def __init__(self, vname):

def __str__(self):
return self.varname


class PhiCommand(Instruction):
def __init__(self, var: str, operands: list):
"""
Represents a φ-function in SSA form
:param var: Variable being assigned (e.g., ":x")
:param operands: List of operand versions from predecessors (e.g., [":x_0", ":x_1"])
"""
self.var = var
self.operands = operands # Ordered list matching predecessors

def __str__(self):
return f"{self.var} = φ({', '.join(self.operands)})"

def __repr__(self):
return f"PhiCommand(var={self.var}, operands={self.operands})"

def add_operand(self, value: str):
"""Add an operand from a predecessor block"""
self.operands.append(value)
Binary file added ChironCore/cfg0_simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChironCore/cfg1_old_after_phi_insertion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChironCore/cfg2_old_after_rename.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChironCore/cfg3_new_post_ssa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChironCore/cfg4_old_out_of_ssa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChironCore/cfg5_new_out_of_ssa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChironCore/cfg6_new_sscp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 65 additions & 2 deletions ChironCore/chiron.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from ChironAST.builder import astGenPass
import abstractInterpretation as AI
import dataFlowAnalysis as DFA
import ssa.SSATransformation as SSA
import ssa.outOfSSA as OutSSA
import ssa.SSCP as SSCP
from sbfl import testsuiteGenerator
from ChironAST.ChironAST import (
Instruction, PhiCommand, AssignmentCommand, ConditionCommand, BoolExpr, BoolFalse, Var as VarExpr, Num
)

sys.path.insert(0, "../Submission/")
sys.path.insert(0, "ChironAST/")
Expand Down Expand Up @@ -134,6 +140,29 @@ def stopTurtle():
help="Run data flow analysis using worklist algorithm on a Chiron Program.",
)

#added by Sarthak Motwani
cmdparser.add_argument(
"-ssa",
"--ssa_transformation",
action="store_true",
help="Run SSA Transformation",
)

cmdparser.add_argument(
"-outssa",
"--out_of_ssa",
action="store_true",
help="Run Out of SSA Transformation",
)

cmdparser.add_argument(
"-sscp",
"--sparse_simple_const_prop",
action="store_true",
help="Perform SSCP optimization",
)

# adding ends here
cmdparser.add_argument(
"-sbfl",
"--SBFL",
Expand Down Expand Up @@ -219,15 +248,49 @@ def stopTurtle():

# generate control_flow_graph from IR statements.
if args.control_flow:
cfg = cfgB.buildCFG(ir, "control_flow_graph", True)
cfg = cfgB.buildCFG(ir, "control_flow_graph")
irHandler.setCFG(cfg)
else:
irHandler.setCFG(None)

if args.dump_cfg:
cfgB.dumpCFG(cfg, "control_flow_graph")
cfgB.dumpCFG(cfg, "cfg0_simple")
# set the cfg of the program.

#Added by Sarthak Motwani for SSA Transformation + Out-of-SSA + SSCP
if args.ssa_transformation:
# Handling parameters
if args.params:
for var in args.params.keys():
if not isinstance(args.params[var], (int, float)):
raise ValueError(f"{args.params[var]} is not a number")
rhs_val = Num(args.params[var])
if not var.startswith(':'):
var = ':'+ var
# print(var, args.params[var])
lhs_var = VarExpr(var)
assignment = AssignmentCommand(lhs_var, rhs_val)
ir.insert(0, (assignment, 1))
args.params = {}

cfg = cfgB.buildCFG(ir, "control_flow_graph")
irHandler.setCFG(cfg)
ssa_cfg = SSA.build_ssa(ir, cfg)
sscp_cfg = ssa_cfg
result_sscp = None

if args.sparse_simple_const_prop:
sscp_obj = SSCP.SSCP(ssa_cfg)
result_sscp = sscp_obj.get_results()
SSCP.optimize_ir(ssa_cfg, sscp_obj, ir, result_sscp)
sscp_cfg = cfgB.buildCFG(ir, "cfg_sscp")
cfgB.dumpCFG(sscp_cfg, "cfg6_new_sscp")

if args.out_of_ssa:
OutSSA.out_of_ssa(ir, sscp_cfg, result_sscp)

#Adding ends

if args.ir:
irHandler.pretty_print(irHandler.ir)

Expand Down
20 changes: 20 additions & 0 deletions ChironCore/demo_testcases/1_straightline.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
:x = 50
:y = 100
pendown
forward :x
right :y
pendown
backward 20
:z = :x + :y
left :z
:x = 25
forward :x
:x = :x + (1/3)
forward :x
:x = :x + (1/3)
forward :x
:x = :x + (1/3)
forward :x
:x = :x + (1/3)
forward :x
goto (:x, :x)
22 changes: 22 additions & 0 deletions ChironCore/demo_testcases/2_ifelse.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
:x = 5
:a = 10
:b = :a - 10
:c = 10
:d = 50
if (:a > :b) [
if (:c < :d) [
:x = :c * :d
:c = :d
:d = :c
:c = :d
]
] else [
:x = :a - :b
:x = (:a + :b*:b + 3/2 + 8)/:a
]
:b = 5
repeat :d[
forward 10/3
:b = :b + 1/2
]
forward :x - 50
9 changes: 9 additions & 0 deletions ChironCore/demo_testcases/3_loops.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:global1 = 10
repeat 2 [
:global1 = 20
if (:global1 > 15) [
:local = :global1
:global1 = :local
]
]
forward :global1
27 changes: 27 additions & 0 deletions ChironCore/demo_testcases/4_nestedloops.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
:x = 0
:y = 100
:z = 39
repeat 3 [
if (:x < :y) [
:uninit = 10
:x = :x + 2
if (:x - 2 == 0) [
forward :x
] else [
backward :y
:t = 90
left :t
:y = :y - 1
]
right 90
backward :x
]
forward :uninit
]
penup
goto (:x, :y)
pendown
repeat 4[
forward :z
right 90
]
37 changes: 37 additions & 0 deletions ChironCore/demo_testcases/5_bigprogram_giveparams.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pendown
repeat 3 [

if (:x > :y) [
penup
goto (:x, :y)
pendown
repeat 4 [
forward :x
left 90
]
] else [
penup
goto (:y, :x)
pendown
repeat 5 [
forward :p
left 72
]
]

if (:z >= :p) [
penup
goto (:p, :x + :z)
pendown
repeat 6 [
backward :x
left 60
]
]

:x = :x + 10
:y = :y + 10
:z = :z + 10
]

penup
46 changes: 46 additions & 0 deletions ChironCore/demo_testcases/6_bigprogram.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
:a = 100
:b = 50
:c = 0
pendown

repeat 1 [
:a = :a - 20
:b = :b + 10

repeat 2 [
:shadow = :a * 2
if (:shadow > :b) [
:shadow = :shadow / 2
forward :shadow
right 45
] else [
:shadow = :shadow + :b
backward :shadow
left 90
]

:c = :c + :shadow

repeat 1 [
:a = 5
forward :a
:a = :a + (:shadow * 3)
left :a

if (:c > 75) [
:b = :b - (:a * 2)
penup
goto (0, 0)
pendown
]
]
]
:d=:a* :b * :c

:a = :a + :b
:b = :b - :c
]
if (:d >10) [
goto (0, 1)
penup
]
Loading