18 |
props : PropList.holder |
props : PropList.holder |
19 |
} |
} |
20 |
|
|
21 |
(* a statement is a CFG fragment with a single entrypoint and |
datatype block = BLK of { |
22 |
* a single continuation. |
id : Stamp.stamp, |
23 |
|
props : PropList.holder, |
24 |
|
preds : block list ref, |
25 |
|
succs : block list ref, |
26 |
|
phi : (var * var list) list ref, (* phi statements *) |
27 |
|
body : assign list ref, |
28 |
|
exit : transfer ref |
29 |
|
} |
30 |
|
|
31 |
|
and rhs |
32 |
|
= VAR of var |
33 |
|
| LIT of Literal.literal |
34 |
|
| OP of Op.rator * var list |
35 |
|
| CONS of var list (* tensor-value construction *) |
36 |
|
|
37 |
|
and transfer |
38 |
|
= EXIT |
39 |
|
| GOTO of block |
40 |
|
| COND of var * block * block |
41 |
|
| DIE |
42 |
|
| STABILIZE |
43 |
|
|
44 |
|
withtype assign = (var * rhs) |
45 |
|
|
46 |
|
(* we layer a block-structured statement tree over the CFG to preserve |
47 |
|
* the high-level control-flow structure for when we need to produce |
48 |
|
* code. |
49 |
*) |
*) |
50 |
datatype stmt |
datatype stmt |
51 |
= EXIT |
= BLOCK of block |
52 |
| SEQ of block * stmt |
| SEQ of stmt list |
53 |
| IF of { |
| IF of { |
|
pre : stmt, |
|
54 |
cond : var, |
cond : var, |
55 |
trueBranch : stmt, |
trueBranch : stmt, |
56 |
falseBranch : stmt |
falseBranch : stmt |
57 |
} |
} |
58 |
| WHILE of { |
| WHILE of { |
59 |
hdr : stmt, |
hdr : block, |
60 |
cond : var, |
cond : var, |
61 |
body : stmt |
body : stmt |
62 |
} |
} |
63 |
|
|
|
and block = BLK of { |
|
|
parent : stmt ref, (* parent statement of this block *) |
|
|
id : Stamp.stamp, (* unique ID *) |
|
|
preds : block list ref, (* list of predecessor blocks in the CFG *) |
|
|
phi : (var * var list) list ref, (* phi statements *) |
|
|
body : simple_stmt list ref, |
|
|
succs : block list ref (* successor blocks in the CFG *) |
|
|
} |
|
|
|
|
|
and simple_stmt |
|
|
= ASSIGN of var * rhs |
|
|
| DIE |
|
|
| STABILIZE |
|
|
| RETURN |
|
|
|
|
|
and rhs |
|
|
= VAR of var |
|
|
| LIT of Literal.literal |
|
|
| OP of Op.rator * var list |
|
|
| CONS of var list (* tensor-value construction *) |
|
|
|
|
64 |
val newVar : string -> var |
val newVar : string -> var |
65 |
val newBlock : unit -> block |
val newBlock : unit -> block |
66 |
|
|
83 |
props : PropList.holder |
props : PropList.holder |
84 |
} |
} |
85 |
|
|
86 |
datatype stmt |
datatype block = BLK of { |
87 |
|
id : Stamp.stamp, |
88 |
|
props : PropList.holder, |
89 |
|
preds : block list ref, |
90 |
|
succs : block list ref, |
91 |
|
phi : (var * var list) list ref, (* phi statements *) |
92 |
|
body : assign list ref, |
93 |
|
exit : transfer ref |
94 |
|
} |
95 |
|
|
96 |
|
and rhs |
97 |
|
= VAR of var |
98 |
|
| LIT of Literal.literal |
99 |
|
| OP of Op.rator * var list |
100 |
|
| CONS of var list (* tensor-value construction *) |
101 |
|
|
102 |
|
and transfer |
103 |
= EXIT |
= EXIT |
104 |
| SEQ of block * stmt |
| GOTO of block |
105 |
|
| COND of var * block * block |
106 |
|
| DIE |
107 |
|
| STABILIZE |
108 |
|
|
109 |
|
withtype assign = (var * rhs) |
110 |
|
|
111 |
|
(* we layer a block-structured statement tree over the CFG to preserve |
112 |
|
* the high-level control-flow structure for when we need to produce |
113 |
|
* code. |
114 |
|
*) |
115 |
|
datatype stmt |
116 |
|
= BLOCK of block |
117 |
|
| SEQ of stmt list |
118 |
| IF of { |
| IF of { |
|
pre : stmt, |
|
119 |
cond : var, |
cond : var, |
120 |
trueBranch : stmt, |
trueBranch : stmt, |
121 |
falseBranch : stmt |
falseBranch : stmt |
122 |
} |
} |
123 |
| WHILE of { |
| WHILE of { |
124 |
hdr : stmt, |
hdr : block, |
125 |
cond : var, |
cond : var, |
126 |
body : stmt |
body : stmt |
127 |
} |
} |
128 |
|
|
|
and block = BLK of { |
|
|
parent : stmt ref, (* parent statement of this block *) |
|
|
id : Stamp.stamp, (* unique ID *) |
|
|
preds : block list ref, (* list of predecessor blocks in the CFG *) |
|
|
phi : (var * var list) list ref, (* phi statements *) |
|
|
body : simple_stmt list ref, |
|
|
succs : block list ref (* successor blocks in the CFG *) |
|
|
} |
|
|
|
|
|
and simple_stmt |
|
|
= ASSIGN of var * rhs |
|
|
| DIE |
|
|
| STABILIZE |
|
|
| RETURN |
|
|
|
|
|
and rhs |
|
|
= VAR of var |
|
|
| LIT of Literal.literal |
|
|
| OP of Op.rator * var list |
|
|
| CONS of var list (* tensor-value construction *) |
|
|
|
|
|
(* block properties *) |
|
|
fun parentOf (BLK{parent, ...}) = !parent |
|
|
fun predsOf (BLK{preds, ...}) = !preds |
|
|
fun succsOf (BLK{succs, ...}) = !succs |
|
|
|
|
129 |
(* IL construction code *) |
(* IL construction code *) |
130 |
fun newVar name = V{ |
fun newVar name = V{ |
131 |
name = name, |
name = name, |
135 |
} |
} |
136 |
|
|
137 |
fun newBlock () = BLK{ |
fun newBlock () = BLK{ |
|
parent = ref(EXIT), |
|
138 |
id = Stamp.new(), |
id = Stamp.new(), |
139 |
|
props = PropList.newHolder(), |
140 |
preds = ref[], |
preds = ref[], |
141 |
|
succs = ref[], |
142 |
phi = ref[], |
phi = ref[], |
143 |
body = ref[], |
body = ref[], |
144 |
succs = ref[] |
exit = ref EXIT |
145 |
} |
} |
146 |
(* |
(* |
147 |
local |
local |