TYPE ConstFlattened = flat(snum) State = Var -> ConstFlattened PROBLEM Constant_Propagation2 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 // could test for all manner of boolean tests... IF(exp), false_edge = if expType(exp) = "BOOL_BINARY" then if expOp(exp) = "=" then if expType(expSubLeft(exp)) = "VAR" then if evalAExp(expSubRight(exp), @) = @(expVar(expSubLeft(exp))) then @\[ expVar(expSubLeft(exp)) -> bot ] else @ endif else @ endif else @ endif else @ endif IF(exp), true_edge = if expType(exp) = "BOOL_BINARY" then if expOp(exp) = "=" then if expType(expSubLeft(exp)) = "VAR" then @\[ expVar(expSubLeft(exp)) -> evalAExp(expSubRight(exp), @) ] else @ endif else @ endif else @ endif // at the end of procedures reset the formal parameter END(_, param) = @\[param -> top] SUPPORT evalAExp :: Expression * State -> ConstFlattened 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(valLeft + valRight); "-" => let valLeft <= evalAExp(expSubLeft(expression), state), valRight <= evalAExp(expSubRight(expression), state) in lift(valLeft - valRight); "*" => let valLeft <= evalAExp(expSubLeft(expression), state), valRight <= evalAExp(expSubRight(expression), state) in lift(valLeft * valRight); "/" => let valLeft <= evalAExp(expSubLeft(expression), state), valRight <= evalAExp(expSubRight(expression), state) in if valRight = 0 then top else lift(valLeft / valRight) endif; endcase; "ARITH_UNARY" => case expOp(expression) of "-" => let value <= evalAExp(expSub(expression), state) in lift(-(value)); endcase; "VAR" => state ( expVar(expression) ); "CONST" => lift(expVal(expression)); _ => error("Runtime Error: evalAExp applied to nonarithmetic Expression"); endcase