-
Notifications
You must be signed in to change notification settings - Fork 8
/
cogen.es
516 lines (446 loc) · 17.1 KB
/
cogen.es
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* -*- mode: java; mode: font-lock; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is [Open Source Virtual Machine.].
*
* The Initial Developer of the Original Code is
* Adobe System Incorporated.
* Portions created by the Initial Developer are Copyright (C) 2004-2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Adobe AS3 Team
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var CTX_shared;
class CTX {
var asm, stk, target;
var emitter, script, cp;
function CTX (asm, stk, target) {
this.asm = asm;
this.stk = stk;
this.target = target;
// tamarin hack
this.emitter = CTX_shared.emitter;
this.script = CTX_shared.script;
this.cp = CTX_shared.cp;
}
}
namespace Gen;
//package cogen
{
//import util.*;
//import abcfile.*;
//import assembler.*;
//import emitter.*;
use default namespace Gen;
use namespace Util;
use namespace Abc;
use namespace Asm;
use namespace Emit;
use namespace Ast;
/* Returns an ABCFile structure */
function cg(tree: PROGRAM) {
/// function cg(tree: PROGRAM) {
var e = new ABCEmitter;
var s = e.newScript();
// CTX.prototype = { "emitter": e, "script": s, "cp": e.constants }; // tamarin doesn't like initing prototype here
CTX_shared = { "emitter": e, "script": s, "cp": e.constants };
cgProgram(new CTX(s.init.asm, null, s), tree);
return e.finalize();
}
/* A context is a structure with the fields
*
* emitter -- the unique emitter
* script -- the only script we care about in that emitter
* cp -- the emitter's constant pool
* asm -- the current function's assembler
* stk -- the current function's binding stack (labels, ribs)
* target -- the current trait target
*
* All of these are invariant and kept in the prototype except for
* 'asm', 'stk', and some fields to come.
*
* FIXME, there are probably at least two targets: one for LET, another
* for VAR/CONST/FUNCTION.
*/
function push(ctx, node) {
node.link = ctx.stk;
return new CTX(ctx.asm, node, ctx.target);
}
function cgProgram(ctx, prog) {
if (prog.head.fixtures != null)
cgFixtures(ctx, prog.head.fixtures);
cgBlock(ctx, prog.block);
}
function hasTrait(traits, name, kind) {
for(var i = 0, l =traits.length; i < l; i++) {
let t = traits[i];
if(t.name==name && ((t.kind&15)==kind))
return true;
}
return false;
}
function cgFixtures(ctx, fixtures) {
let { target:target, asm:asm, emitter:emitter } = ctx;
let methidx, trait_kind, clsidx;
for ( let i=0 ; i < fixtures.length ; i++ ) {
let [fxname, fx] = fixtures[i];
let name = emitter.fixtureNameToName(fxname);
/// switch type (fx) {
/// case (fx:ValFixture) {
if (fx is ValFixture) {
if( !hasTrait(target.traits, name, TRAIT_Slot) )
target.addTrait(new ABCSlotTrait(name, 0, false, 0, emitter.typeFromTypeExpr(fx.type), 0, 0));
// FIXME when we have more general support for type annos
}
/// case (fx:MethodFixture) {
else if (fx is MethodFixture) {
var initScopeDepth = (ctx.stk!=null && ctx.stk.tag=="instance")?2:0;
methidx = cgFunc(ctx, fx.func, initScopeDepth);
/// switch type (target) {
/// case (m:Method) {
if (target is Method) {
target.addTrait(new ABCSlotTrait(name, 0, false, 0, 0, 0, 0));
asm.I_findpropstrict(name);
asm.I_newfunction(methidx);
asm.I_setproperty(name);
}
/// case (x:*) {
else {
// target.addTrait(new ABCOtherTrait(name, 0, TRAIT_Method, 0, methidx));
trait_kind = TRAIT_Method;
/// switch type(fx.func.name.kind) {
/// case (g:Get) {
if (fx.func.name.kind is Get) {
print("Getter, target: " + target);
trait_kind = TRAIT_Getter;
}
/// case (s:Set) {
else if (fx.func.name.kind is Set) {
print("Setter, target: " +target);
trait_kind = TRAIT_Setter;
}
/// }
target.addTrait(new ABCOtherTrait(name, 0, trait_kind, 0, methidx));
}
/// }
}
/// case (fx:ClassFixture) {
else if (fx is ClassFixture) {
clsidx = cgClass(ctx, fx.cls);
target.addTrait(new ABCOtherTrait(name, 0, TRAIT_Class, 0, clsidx));
}
/// case (fx:NamespaceFixture) {
else if (fx is NamespaceFixture) {
target.addTrait(new ABCSlotTrait(name, 0, true, 0, emitter.qname({ns:new PublicNamespace(""), id:"Namespace"},false), emitter.namespace(fx.ns), CONSTANT_Namespace));
}
/// case (fx:TypeFixture) {
else if (fx is TypeFixture) {
print ("warning: ignoring type fixture");
}
/// case (fx:*) {
else {
throw "Internal error: unhandled fixture type"
}
/// }
}
}
function cgBlock(ctx, b) {
// FIXME -- more here
cgHead(ctx, b.head);
let stmts = b.stmts;
for ( let i=0 ; i < stmts.length ; i++ )
cgStmt(ctx, stmts[i]);
}
/*
function cgDefn(ctx, d) {
let { asm:asm, emitter:emitter } = ctx;
switch type (d) {
case (fd:FunctionDefn) {
assert( fd.func.name.kind is Ordinary );
let name = emitter.nameFromIdent(fd.func.name.ident);
//asm.I_findpropstrict(name); // name is fixture, thus always defined
//asm.I_newfunction(cgFunc(ctx, fd.func));
//asm.I_initproperty(name);
}
case (vd: VariableDefn) {
// nothing to do, right?
}
case (x:*) { throw "Internal error: unimplemented defn" }
}
}
*/
function extractNamedFixtures(fixtures)
{
let named = [];
let fix_length = fixtures ? fixtures.length : 0;
for(let i = 0; i < fix_length; ++i)
{
let [name,fixture] = fixtures[i];
switch type (name) {
case (pn:PropName) {
named.push([name,fixture]);
}
case (tn:TempName) {
// do nothing
}
}
}
return named;
}
function cgClass(ctx, c) {
let {asm:asm, emitter:emitter, script:script} = ctx;
let classname = emitter.qname(c.name,false);
let basename = c.baseName != null ? emitter.qname(c.baseName,false) : 0;
let cls = script.newClass(classname, basename);
/*
let c_ctx = new CTX(asm, {tag:"class"}, cls);
cgHead(c_ctx, c.classHead);
*/
let inst = cls.getInstance();
// Context for the instance
let i_ctx = new CTX(asm, {tag:"instance"}, inst);
// do instance slots
cgFixtures(i_ctx, c.instanceHead.fixtures); // FIXME instanceHead and instanceInits should be unified
inst.setIInit(cgCtor(i_ctx, c.constructor, {fixtures:[],exprs:c.instanceHead.exprs}));
var clsidx = cls.finalize();
var Object_name = emitter.qname({ns:new PublicNamespace(""), id:"Object"},false);
asm.I_findpropstrict(Object_name);
asm.I_getproperty(Object_name);
asm.I_dup();
asm.I_pushscope();
asm.I_newclass(clsidx);
asm.I_popscope();
asm.I_getglobalscope();
asm.I_swap();
asm.I_initproperty(classname);
return clsidx;
}
/*
* Generate code for a ctor.
*/
function cgCtor(ctx, c, instanceInits) {
let formals_type = extractFormalTypes(ctx, c.func);
let method = new Method(ctx.script.e, formals_type, 2, "$construct", false);
let asm = method.asm;
let defaults = extractDefaultValues(ctx, c.func);
if( defaults.length > 0 )
{
method.setDefaults(defaults);
}
let t = asm.getTemp();
let ctor_ctx = new CTX(asm, {tag:"function", scope_reg:t}, method);
asm.I_getlocal(0);
asm.I_dup();
// Should this be instanceInits.inits only?
asm.I_pushscope(); // This isn't quite right...
for( let i = 0; i < instanceInits.length; i++ ) {
cgExpr(ctor_ctx, instanceInits[i]);
asm.I_pop();
}
cgHead(ctor_ctx, instanceInits);
asm.I_popscope();
//cgHead(ctor_ctx, instanceInits.inits, true);
// Push 'this' onto scope stack
//asm.I_getlocal(0);
//asm.I_pushscope();
// Create the activation object, and initialize params
asm.I_newactivation();
asm.I_dup();
asm.I_setlocal(t);
asm.I_dup();
asm.I_pushwith();
cgHead(ctor_ctx, c.func.params);
for ( let i=0 ; i < c.settings.length ; i++ ) {
cgExpr(ctor_ctx, c.settings[i]);
asm.I_pop();
}
// Eval super args, and call super ctor
asm.I_getlocal(0);
let nargs = c.superArgs.length;
for ( let i=0 ; i < nargs ; i++ )
cgExpr(ctx, c.superArgs[i]);
asm.I_constructsuper(nargs);
asm.I_popscope();
asm.I_getlocal(0);
asm.I_pushscope(); //'this'
asm.I_pushscope(); //'activation'
cgHead(ctor_ctx, c.func.vars);
cgBlock(ctor_ctx, c.func.block);
asm.I_kill(t);
return method.finalize();
}
function extractFormalTypes({emitter:emitter, script:script}, f:Func) {
function extractType([name,fixture])
emitter.fixtureTypeToType(fixture);
let named_fixtures = extractNamedFixtures(f.params.fixtures);
return map(extractType, named_fixtures);
}
function extractDefaultValues({emitter:emitter, script:script}, f:Func) {
function extractDefaults(expr)
emitter.defaultExpr(expr);
return map(extractDefaults, f.defaults);
}
/* Create a method trait in the ABCFile
* Generate code for the function
* Return the function index
*/
function cgFunc({emitter:emitter, script:script}, f:FUNC, initScopeDepth) {
let formals_type = extractFormalTypes({emitter:emitter, script:script}, f);
let method = script.newFunction(formals_type,initScopeDepth);
let asm = method.asm;
let defaults = extractDefaultValues({emitter:emitter, script:script}, f);
if( defaults.length > 0 )
{
method.setDefaults(defaults);
}
/* Create a new rib and populate it with the values of all the
* formals. Add slot traits for all the formals so that the
* rib have all the necessary names. Later code generation
* will add properties for all local (hoisted) VAR, CONST, and
* FUNCTION bindings, and they will be added to the rib too,
* but not initialized here. (That may have to change, for
* FUNCTION bindings at least.)
*
* FIXME: if a local VAR shadows a formal, there's more
* elaborate behavior here, and the compiler must perform some
* analysis and avoid the shadowed formal here.
*
* God only knows about the arguments object...
*/
let t = asm.getTemp();
asm.I_newactivation();
asm.I_dup();
asm.I_setlocal(t);
asm.I_pushscope();
let ctx = new CTX(asm, {tag: "function", scope_reg:t, has_scope:true}, method);
cgHead(ctx, f.params);
cgHead(ctx, f.vars);
/* Generate code for the body. If there is no return statement in the
* code then the default behavior of the emitter is to add a returnvoid
* at the end, so there's nothing to worry about here.
*/
cgBlock(ctx, f.block);
asm.I_kill(t);
return method.finalize();
}
function cgHead(ctx, head) {
let {asm:asm, emitter:emitter, target:target} = ctx;
function extractName([name,fixture])
ctx.emitter.fixtureNameToName(name); //FIXME: shouldn't need ctx.
function extractType([name,fixture])
ctx.emitter.fixtureTypeToType(fixture); //FIXME: shouldn't need ctx.
let named_fixtures = extractNamedFixtures(head.fixtures);
/*
let formals = map(extractName, named_fixtures);
let formals_type = map(extractType, named_fixtures);
for ( let i=0 ; i < formals.length ; i++ ) {
if(!hasTrait(target.traits, formals[i], TRAIT_Slot) )
target.addTrait(new ABCSlotTrait(formals[i], 0, false, 0, formals_type[i]));
}
*/
cgFixtures(ctx, named_fixtures);
for ( let i=0 ; i < head.exprs.length ; i++ ) {
cgExpr(ctx, head.exprs[i]);
asm.I_pop();
}
}
function cgInits(ctx, inits, baseOnStk){
let {asm:asm, emitter:emitter} = ctx;
let t = -1;
let inits_length = inits?inits.length:0;
for( let i=0; i < inits_length; ++i ) {
let [name, init] = inits[i];
let name_index = emitter.fixtureNameToName(name);
if( baseOnStk ) {
if(i < inits_length-1)
asm.I_dup();
}
else
asm.I_findproperty(name_index);
cgExpr(ctx, init);
asm.I_setproperty(name_index);
}
if( inits_length == 0 && baseOnStk )
{
asm.I_pop();
}
}
// Handles scopes and finally handlers and returns a label, if appropriate, to
// branch to. "tag" is one of "function", "break", "continue"
function unstructuredControlFlow({stk:stk, asm:asm}, hit, jump, msg) {
while (stk != null) {
if (hit(stk)) {
if (jump)
asm.I_jump(stk.target);
return;
}
else {
if(stk.has_scope) {
asm.I_popscope();
}
// FIXME
// if there's a FINALLY, visit it here
}
stk = stk.link;
}
throw msg;
}
function restoreScopes({stk:stk, asm:asm}) {
while (stk != null) {
if(stk.has_scope) {
asm.I_getlocal(stk.scope_reg);
asm.I_pushscope();
}
if( stk.tag != "function" ) {
stk = stk.link;
}
else {
stk = null;
}
}
}
// The following return extended contexts
function pushBreak(ctx, labels, target)
push(ctx, { tag:"break", labels:labels, target:target, has_scope:false });
function pushContinue(ctx, labels, target)
push(ctx, { tag:"continue", labels:labels, target:target, has_scope:false });
function pushFunction(ctx /*more*/) {
// FIXME
}
function pushWith(ctx /*more*/) {
// FIXME
}
function pushLet(ctx /*more*/) {
}
function pushCatch(ctx, scope_reg )
push(ctx, {tag:"catch", has_scope:true, scope_reg:scope_reg});
// FIXME anything else?
function pushFinally(ctx /*more*/) {
// FIXME
}
}