Created
January 31, 2013 19:59
-
-
Save jbranchaud/4685896 to your computer and use it in GitHub Desktop.
Solution for Parity Analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TYPE | |
StrSet = set(str) | |
StrLifted = lift(StrSet) | |
State = Var -> StrLifted | |
PROBLEM Constant_Propagation | |
direction : forward | |
carrier : State | |
init : bot | |
init_start : [-> top] | |
combine : lub | |
TRANSFER | |
// in assignments calculate the new value of the variable and add it to the state | |
ASSIGN(variable, expression) = | |
@\[variable -> evalAExp(expression, @)] | |
// in procedur calls pass the value of the actual argument to the formal parameter | |
CALL(_, param, exp), call_edge = | |
@\[param -> evalAExp(exp, @)] | |
CALL(_, _, _), local_edge = bot | |
// at the end of procedures reset the formal parameter | |
END(_, param) = | |
@\[param -> top] | |
SUPPORT | |
evalAExp :: Expression * State -> StrLifted | |
evalAExp(expression, state) = | |
case expType(expression) of | |
"ARITH_BINARY" => case expOp(expression) of | |
"+" => let valLeft <= evalAExp(expSubLeft(expression), state), | |
valRight <= evalAExp(expSubRight(expression), state) in | |
lift( | |
{ if l = r then | |
"even" | |
else "odd" endif | |
| l in valLeft; r in valRight }); | |
"-" => let valLeft <= evalAExp(expSubLeft(expression), state), | |
valRight <= evalAExp(expSubRight(expression), state) in | |
lift( | |
{ if l = r then | |
"even" | |
else "odd" endif | |
| l in valLeft; r in valRight }); | |
"*" => let valLeft <= evalAExp(expSubLeft(expression), state), | |
valRight <= evalAExp(expSubRight(expression), state) in | |
lift( | |
{ if l = "odd" then | |
if r = "odd" then | |
"odd" | |
else "even" endif | |
else "even" endif | |
| l in valLeft; r in valRight }); | |
"/" => let valLeft <= evalAExp(expSubLeft(expression), state), | |
valRight <= evalAExp(expSubRight(expression), state) in | |
lift( | |
{ if l = "odd" then | |
if r = "odd" then | |
"odd" | |
else "even" endif | |
else "even" endif | |
| l in valLeft; r in valRight }); | |
endcase; | |
"ARITH_UNARY" => | |
case expOp(expression) of | |
"-" => let value <= evalAExp(expSub(expression), state) in | |
lift(value); | |
endcase; | |
"VAR" => state ( expVar(expression) ); | |
"CONST" => | |
if ((expVal(expression) % 2) = 0) then | |
lift({"even"}) | |
else lift({"odd"}) endif; | |
_ => error("Runtime Error: evalAExp applied to nonarithmetic Expression"); | |
endcase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment