CSC448: Phases: Introduce Three Address Code [7/8] |
Write compound statements as
{ decls stats }
Write instances of DeclsStats as
< decls | stats >
Write instances of DeclsStatsVariable as
< decls | stats | Optional<v> >
If the exp must be present, then:
< decls | stats | v >
The relevant functions are:
transformStat (stat) = < decls | stats > transformExp (exp) = < decls | stats | Optional<v> >
To transform a statement, do a case analysis.
case StatCompound
:
transformStat (stat_0) = < decls_0 | stats_0 > ... transformStat (stat_n) = < decls_n | stats_n > ----------------------------------------------------- transformStat (({decls stat_0 ... stat_n})) = < nil | { decls decls_0 ... decls_n stats_0 ... stats_m } >
case StatExp
:
transformExp (exp) = < decls | stats | ... > ----------------------------------------------------- transformStat ((exp;)) = < decls | stats >
case StatGoto
:
----------------------------------------------------- transformStat ((goto l;)) = < nil | (goto l;) >
case StatIf
:
transformExp (exp) = < decls | stats | v > ----------------------------------------------------- transformStat ((if (exp) goto l1; else goto l2;)) = < decls | stats (if (v) goto l1; else goto l2;) >
case StatReturn
:
----------------------------------------------------- transformStat ((return;)) = < nil | (return;) > transformExp (exp) = < decls | stats | v > ----------------------------------------------------------------- transformStat ((return exp;)) = < decls | stats (return v;) >
case StatSkip
:
----------------------------------------------------- transformStat ((skip;)) = < nil | (skip;) >
To transform an expression, do a case analysis.
case ExpInt
:
----------------------------------------------------- transformExp (n) = < nil | nil | n >
case ExpVar
:
----------------------------------------------------- transformExp (x) = < nil | nil | x >
case ExpString
:
----------------------------------------------------- transformExp (("value")) = < (int[] x;) | (x = ("value");) | x >
case ExpNew
:
transformExp (exp) = < decls | stats | v > ----------------------------------------------------- transformExp ((new T[exp];)) = < decls (T[] x;) | stats (x = (new T[v]);) | x >
case ExpArrayAccess
:
transformExp (expA) = < decls1 | stats1 | v1 > transformExp (expI) = < decls2 | stats2 | v2 > ------------------------------------------------------------------------------ transformExp ((expA[expI]) :: T) = < decls1 decls2 (T x;) | stats1 stats2 (x = (v1[v2]);) | x >
case ExpAssign
:
transformExp (expR) = < decls3 | stats3 | v3 > ----------------------------------------------------- transformExp ((x = expR)) = < decls3 | stats3 (x = v3;) | v3 > transformExp (expA) = < decls1 | stats1 | v1 > transformExp (expI) = < decls2 | stats2 | v2 > transformExp (expR) = < decls3 | stats3 | v3 > ------------------------------------------------------------------------------- transformExp ((expA[expI] = expR)) = < decls1 decls2 decls3 | stats1 stats2 stats3 (v1[v2] = v3;) | v3 >
case ExpUnOp
:
transformExp (exp) = < decls | stats | v > ------------------------------------------------------------ transformExp ((unop exp) :: T) = < decls (T x;) | stats (x = (unop v);) | x >
case ExpBinOp
:
transformExp (expL) = < decls1 | stats1 | v1 > transformExp (expR) = < decls2 | stats2 | v2 > ---------------------------------------------------------------------------------- transformExp ((expL op expR) :: T) = < decls1 decls2 (T x;) | stats1 stats2 (x = (v1 op v2);) | x >
case ExpComma
:
transformExp (expL) = < decls1 | stats1 | Optional<exp1> > transformExp (expR) = < decls2 | stats2 | Optional<exp2> > ----------------------------------------------------------- transformExp ((expL , expR)) = < decls1 decls2 | stats1 stats2 | Optional<exp2> >
case ExpFunCall
:
transformExp (exp1) = < decls1 | stats1 | v1 > ... transformExp (expn) = < declsn | statsn | vn > ------------------------------------------------------------------------- transformExp ((f(exp1,...,expn)) :: void) = < decls1 ... declsn | stats1 ... statsn (f(v1,...,vn)) | nil > transformExp (exp1) = < decls1 | stats1 | v1 > ... transformExp (expn) = < declsn | statsn | vn > ------------------------------------------------------------------------- transformExp ((f(exp1,...,expn)) :: T) = < decls1 ... declsn (T x;) | stats1 ... statsn (x = (f(v1,...,vn))) | x >