This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNodeTransformer.cs
More file actions
345 lines (301 loc) · 15.2 KB
/
Copy pathNodeTransformer.cs
File metadata and controls
345 lines (301 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//------------------------------------------------------------------------------
// <license file="NodeTransformer.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
using EcmaScript.NET.Collections;
namespace EcmaScript.NET
{
/// <summary> This class transforms a tree to a lower-level representation for codegen.
///
/// </summary>
public class NodeTransformer
{
public NodeTransformer ()
{
}
public void transform (ScriptOrFnNode tree)
{
transformCompilationUnit (tree);
for (int i = 0; i != tree.FunctionCount; ++i) {
FunctionNode fn = tree.getFunctionNode (i);
transform (fn);
}
}
private void transformCompilationUnit (ScriptOrFnNode tree)
{
loops = new ObjArray ();
loopEnds = new ObjArray ();
// to save against upchecks if no finally blocks are used.
hasFinally = false;
try {
transformCompilationUnit_r (tree, tree);
} catch (Helpers.StackOverflowVerifierException) {
throw Context.ReportRuntimeError (
ScriptRuntime.GetMessage ("mag.too.deep.parser.recursion"));
}
}
private void transformCompilationUnit_r (ScriptOrFnNode tree, Node parent)
{
Node node = null;
using (Helpers.StackOverflowVerifier sov = new Helpers.StackOverflowVerifier (1024)) {
for (; ; ) {
Node previous = null;
if (node == null) {
node = parent.FirstChild;
}
else {
previous = node;
node = node.Next;
}
if (node == null) {
break;
}
int type = node.Type;
switch (type) {
case Token.LABEL:
case Token.SWITCH:
case Token.LOOP:
loops.push (node);
loopEnds.push (((Node.Jump)node).target);
break;
case Token.WITH: {
loops.push (node);
Node leave = node.Next;
if (leave.Type != Token.LEAVEWITH) {
Context.CodeBug ();
}
loopEnds.push (leave);
break;
}
case Token.TRY: {
Node.Jump jump = (Node.Jump)node;
Node finallytarget = jump.Finally;
if (finallytarget != null) {
hasFinally = true;
loops.push (node);
loopEnds.push (finallytarget);
}
break;
}
case Token.TARGET:
case Token.LEAVEWITH:
if (!loopEnds.Empty && loopEnds.peek () == node) {
loopEnds.pop ();
loops.pop ();
}
break;
case Token.RETURN: {
/* If we didn't support try/finally, it wouldn't be
* necessary to put LEAVEWITH nodes here... but as
* we do need a series of JSR FINALLY nodes before
* each RETURN, we need to ensure that each finally
* block gets the correct scope... which could mean
* that some LEAVEWITH nodes are necessary.
*/
if (!hasFinally)
break; // skip the whole mess.
Node unwindBlock = null;
for (int i = loops.size () - 1; i >= 0; i--) {
Node n = (Node)loops.Get (i);
int elemtype = n.Type;
if (elemtype == Token.TRY || elemtype == Token.WITH) {
Node unwind;
if (elemtype == Token.TRY) {
Node.Jump jsrnode = new Node.Jump (Token.JSR);
Node jsrtarget = ((Node.Jump)n).Finally;
jsrnode.target = jsrtarget;
unwind = jsrnode;
}
else {
unwind = new Node (Token.LEAVEWITH);
}
if (unwindBlock == null) {
unwindBlock = new Node (Token.BLOCK, node.Lineno);
}
unwindBlock.addChildToBack (unwind);
}
}
if (unwindBlock != null) {
Node returnNode = node;
Node returnExpr = returnNode.FirstChild;
node = replaceCurrent (parent, previous, node, unwindBlock);
if (returnExpr == null) {
unwindBlock.addChildToBack (returnNode);
}
else {
Node store = new Node (Token.EXPR_RESULT, returnExpr);
unwindBlock.addChildToFront (store);
returnNode = new Node (Token.RETURN_RESULT);
unwindBlock.addChildToBack (returnNode);
// transform return expression
transformCompilationUnit_r (tree, store);
}
// skip transformCompilationUnit_r to avoid infinite loop
goto siblingLoop;
}
break;
}
case Token.BREAK:
case Token.CONTINUE: {
Node.Jump jump = (Node.Jump)node;
Node.Jump jumpStatement = jump.JumpStatement;
if (jumpStatement == null)
Context.CodeBug ();
for (int i = loops.size (); ; ) {
if (i == 0) {
// Parser/IRFactory ensure that break/continue
// always has a jump statement associated with it
// which should be found
throw Context.CodeBug ();
}
--i;
Node n = (Node)loops.Get (i);
if (n == jumpStatement) {
break;
}
int elemtype = n.Type;
if (elemtype == Token.WITH) {
Node leave = new Node (Token.LEAVEWITH);
previous = addBeforeCurrent (parent, previous, node, leave);
}
else if (elemtype == Token.TRY) {
Node.Jump tryNode = (Node.Jump)n;
Node.Jump jsrFinally = new Node.Jump (Token.JSR);
jsrFinally.target = tryNode.Finally;
previous = addBeforeCurrent (parent, previous, node, jsrFinally);
}
}
if (type == Token.BREAK) {
jump.target = jumpStatement.target;
}
else {
jump.target = jumpStatement.Continue;
}
jump.Type = Token.GOTO;
break;
}
case Token.CALL:
visitCall (node, tree);
break;
case Token.NEW:
visitNew (node, tree);
break;
case Token.VAR: {
Node result = new Node (Token.BLOCK);
for (Node cursor = node.FirstChild; cursor != null; ) {
// Move cursor to next before createAssignment get chance
// to change n.next
Node n = cursor;
if (n.Type != Token.NAME)
Context.CodeBug ();
cursor = cursor.Next;
if (!n.hasChildren ())
continue;
Node init = n.FirstChild;
n.removeChild (init);
n.Type = Token.BINDNAME;
n = new Node (Token.SETNAME, n, init);
Node pop = new Node (Token.EXPR_VOID, n, node.Lineno);
result.addChildToBack (pop);
}
node = replaceCurrent (parent, previous, node, result);
break;
}
case Token.NAME:
case Token.SETNAME:
case Token.DELPROP: {
// Turn name to var for faster access if possible
if (tree.Type != Token.FUNCTION || ((FunctionNode)tree).RequiresActivation) {
break;
}
Node nameSource;
if (type == Token.NAME) {
nameSource = node;
}
else {
nameSource = node.FirstChild;
if (nameSource.Type != Token.BINDNAME) {
if (type == Token.DELPROP) {
break;
}
throw Context.CodeBug ();
}
}
string name = nameSource.String;
if (tree.hasParamOrVar (name)) {
if (type == Token.NAME) {
node.Type = Token.GETVAR;
}
else if (type == Token.SETNAME) {
node.Type = Token.SETVAR;
nameSource.Type = Token.STRING;
}
else if (type == Token.DELPROP) {
// Local variables are by definition permanent
Node n = new Node (Token.FALSE);
node = replaceCurrent (parent, previous, node, n);
}
else {
throw Context.CodeBug ();
}
}
break;
}
}
transformCompilationUnit_r (tree, node);
siblingLoop:
;
}
}
}
protected internal virtual void visitNew (Node node, ScriptOrFnNode tree)
{
}
protected internal virtual void visitCall (Node node, ScriptOrFnNode tree)
{
}
private static Node addBeforeCurrent (Node parent, Node previous, Node current, Node toAdd)
{
if (previous == null) {
if (!(current == parent.FirstChild))
Context.CodeBug ();
parent.addChildToFront (toAdd);
}
else {
if (!(current == previous.Next))
Context.CodeBug ();
parent.addChildAfter (toAdd, previous);
}
return toAdd;
}
private static Node replaceCurrent (Node parent, Node previous, Node current, Node replacement)
{
if (previous == null) {
if (!(current == parent.FirstChild))
Context.CodeBug ();
parent.replaceChild (current, replacement);
}
else if (previous.next == current) {
// Check cachedPrev.next == current is necessary due to possible
// tree mutations
parent.replaceChildAfter (previous, replacement);
}
else {
parent.replaceChild (current, replacement);
}
return replacement;
}
private ObjArray loops;
private ObjArray loopEnds;
private bool hasFinally;
}
}