@@ -1723,9 +1723,8 @@ export class Compiler extends DiagnosticEmitter {
1723
1723
nativeValueType , instance . memoryOffset
1724
1724
) ;
1725
1725
1726
- // We need to hardcode the local index used by makeRetain because there is no corresponding flow
1727
1726
if ( type . isManaged ) {
1728
- valueExpr = this . makeRetain ( valueExpr , type , 1 ) ;
1727
+ valueExpr = this . makeRetain ( valueExpr , type ) ;
1729
1728
varTypes = [ NativeType . I32 ] ;
1730
1729
}
1731
1730
@@ -1765,9 +1764,9 @@ export class Compiler extends DiagnosticEmitter {
1765
1764
) ,
1766
1765
module . block ( null , [
1767
1766
module . drop (
1768
- this . makeRetain ( module . local_get ( 1 , nativeValueType ) , type , 1 )
1767
+ this . makeRetain ( module . local_get ( 1 , nativeValueType ) , type )
1769
1768
) ,
1770
- this . makeRelease ( module . local_get ( 2 , nativeValueType ) , type , 2 )
1769
+ this . makeRelease ( module . local_get ( 2 , nativeValueType ) , type )
1771
1770
] )
1772
1771
) ,
1773
1772
module . local_get ( 1 , nativeValueType )
@@ -2805,24 +2804,14 @@ export class Compiler extends DiagnosticEmitter {
2805
2804
// Remember that this flow returns
2806
2805
flow . set ( FlowFlags . RETURNS | FlowFlags . TERMINATES ) ;
2807
2806
2808
- // Prevent returning a closure in an exported function, since interop with closures is
2809
- // not yet supported
2807
+ // TODO: implement interop so we can return function references in exported functions
2810
2808
var returnSignature = returnType . signatureReference ;
2811
2809
if ( returnSignature !== null && flow . parentFunction . is ( CommonFlags . EXPORT ) ) {
2812
2810
var returnValueLocalIndex = flow . getTempLocal ( returnType ) . index ;
2813
2811
var nativeReturnType = returnType . toNativeType ( ) ;
2814
- expr = module . flatten ( [
2815
- module . local_set ( returnValueLocalIndex , expr ) ,
2816
- this . ifClosure (
2817
- module . local_get ( returnValueLocalIndex , nativeReturnType ) ,
2818
- this . makeAbort ( null , statement ) , // TODO: throw
2819
- module . nop ( )
2820
- ) ,
2821
- module . local_get ( returnValueLocalIndex , nativeReturnType )
2822
- ] , nativeReturnType ) ;
2812
+ expr = this . makeAbort ( null , statement ) ; // TODO: Use unimplemented here instead, since we know at compile time this will fail
2823
2813
}
2824
2814
2825
-
2826
2815
// If the last statement anyway, make it the block's return value
2827
2816
if ( isLastInBody && expr != 0 && returnType != Type . void ) {
2828
2817
if ( ! stmts . length ) return expr ;
@@ -3644,7 +3633,7 @@ export class Compiler extends DiagnosticEmitter {
3644
3633
expr
3645
3634
) ,
3646
3635
this . injectClosedLocals ( tempResult ) ,
3647
- this . getClosureReference ( module . local_get ( tempResult . index , fromType . toNativeType ( ) ) )
3636
+ module . local_get ( tempResult . index , fromType . toNativeType ( ) )
3648
3637
] , toType . toNativeType ( ) ) ;
3649
3638
3650
3639
// this.currentFlow.freeTempLocal(tempResult);
@@ -6774,36 +6763,21 @@ export class Compiler extends DiagnosticEmitter {
6774
6763
var usize = this . options . nativeSizeType ;
6775
6764
return module . block ( null , [
6776
6765
module . local_set ( tempFunctionReferenceLocal . index , indexArg ) ,
6777
- this . ifClosure (
6778
- module . local_get ( tempFunctionReferenceLocal . index , usize ) ,
6779
- this . compileCallIndirect ( // If this is a closure
6780
- signature . toClosureSignature ( ) ,
6781
- module . block ( null , [
6782
- module . load (
6783
- 4 ,
6784
- true ,
6785
- this . getClosurePtr (
6786
- module . local_get ( tempFunctionReferenceLocal . index , usize ) ,
6787
- ) ,
6788
- usize ,
6789
- 0
6790
- ) ,
6791
- ] , this . options . nativeSizeType ) ,
6792
- expression . args ,
6793
- expression ,
6794
- this . getClosurePtr (
6766
+ this . compileCallIndirect ( // If this is a closure
6767
+ signature . toClosureSignature ( ) ,
6768
+ module . block ( null , [
6769
+ module . load (
6770
+ 4 ,
6771
+ true ,
6795
6772
module . local_get ( tempFunctionReferenceLocal . index , usize ) ,
6773
+ usize ,
6774
+ 0
6796
6775
) ,
6797
- contextualType == Type . void
6798
- ) ,
6799
- this . compileCallIndirect ( // If this function isn't a closure
6800
- signature ,
6801
- module . local_get ( tempFunctionReferenceLocal . index , usize ) ,
6802
- expression . args ,
6803
- expression ,
6804
- 0 ,
6805
- contextualType == Type . void
6806
- )
6776
+ ] , this . options . nativeSizeType ) ,
6777
+ expression . args ,
6778
+ expression ,
6779
+ module . local_get ( tempFunctionReferenceLocal . index , usize ) ,
6780
+ contextualType == Type . void
6807
6781
)
6808
6782
] , constraints & Constraints . WILL_DROP ? contextualType . toNativeType ( ) : returnType . toNativeType ( ) ) ;
6809
6783
}
@@ -7458,75 +7432,20 @@ export class Compiler extends DiagnosticEmitter {
7458
7432
// <reference-counting>
7459
7433
7460
7434
/** Makes a retain call, retaining the expression's value. */
7461
- makeRetain ( expr : ExpressionRef , type : Type , exprLocalIndex : i32 = - 1 ) : ExpressionRef {
7435
+ makeRetain ( expr : ExpressionRef , type : Type ) : ExpressionRef {
7462
7436
var module = this . module ;
7463
7437
var retainInstance = this . program . retainInstance ;
7464
7438
this . compileFunction ( retainInstance ) ;
7465
- if ( type !== null && type . isFunctionIndex ) {
7466
- if ( exprLocalIndex < 0 ) {
7467
- var exprLocal = this . currentFlow . getTempLocal ( type ) ;
7468
- exprLocalIndex = exprLocal . index ;
7469
- }
7470
- var nativeType = type . toNativeType ( ) ;
7471
- var usize = this . options . nativeSizeType ;
7472
- var functionRetainCall = module . block ( null , [
7473
- module . local_set ( exprLocalIndex , expr ) ,
7474
- module . drop (
7475
- module . call (
7476
- retainInstance . internalName ,
7477
- [
7478
- this . ifClosure (
7479
- module . local_get ( exprLocalIndex , nativeType ) ,
7480
- this . getClosurePtr ( module . local_get ( exprLocalIndex , nativeType ) ) ,
7481
- usize == NativeType . I32 ? module . i32 ( 0 ) : module . i64 ( 0 )
7482
- )
7483
- ] ,
7484
- usize
7485
- )
7486
- ) ,
7487
- module . local_get ( exprLocalIndex , nativeType )
7488
- ] , nativeType ) ;
7489
-
7490
- // this.currentFlow.freeTempLocal(exprLocal);
7491
-
7492
- return functionRetainCall ;
7493
- }
7494
7439
7495
7440
return module . call ( retainInstance . internalName , [ expr ] , this . options . nativeSizeType ) ;
7496
7441
}
7497
7442
7498
7443
/** Makes a release call, releasing the expression's value. Changes the current type to void.*/
7499
- makeRelease ( expr : ExpressionRef , type : Type , exprLocalIndex : i32 = - 1 ) : ExpressionRef {
7444
+ makeRelease ( expr : ExpressionRef , type : Type ) : ExpressionRef {
7500
7445
var module = this . module ;
7501
7446
var releaseInstance = this . program . releaseInstance ;
7502
7447
this . compileFunction ( releaseInstance ) ;
7503
7448
7504
- if ( type !== null && type . isFunctionIndex ) {
7505
- if ( exprLocalIndex < 0 ) {
7506
- var exprLocal = this . currentFlow . getTempLocal ( type ) ;
7507
- exprLocalIndex = exprLocal . index ;
7508
- }
7509
- var nativeType = type . toNativeType ( ) ;
7510
- var functionReleaseCall = module . block ( null , [
7511
- module . local_set ( exprLocalIndex , expr ) ,
7512
- module . call (
7513
- releaseInstance . internalName ,
7514
- [
7515
- this . ifClosure (
7516
- module . local_get ( exprLocalIndex , nativeType ) ,
7517
- this . getClosurePtr ( module . local_get ( exprLocalIndex , nativeType ) ) ,
7518
- this . options . nativeSizeType == NativeType . I32 ? module . i32 ( 0 ) : module . i64 ( 0 )
7519
- )
7520
- ] ,
7521
- NativeType . None
7522
- )
7523
- ] , NativeType . None ) ;
7524
-
7525
- // TODO: fix a bug in which this free causes some overwrites
7526
- // this.currentFlow.freeTempLocal(exprLocal);
7527
-
7528
- return functionReleaseCall ;
7529
- }
7530
7449
return this . module . call ( releaseInstance . internalName , [ expr ] , NativeType . None ) ;
7531
7450
}
7532
7451
@@ -8332,55 +8251,6 @@ export class Compiler extends DiagnosticEmitter {
8332
8251
return closureExpr ;
8333
8252
}
8334
8253
8335
- private ifClosure (
8336
- indexExpr : ExpressionRef ,
8337
- thenExpr : ExpressionRef ,
8338
- elseExpr : ExpressionRef
8339
- ) : ExpressionRef {
8340
- var module = this . module ;
8341
- var wasm64 = this . options . nativeSizeType == NativeType . I64 ;
8342
-
8343
- return module . if (
8344
- module . binary (
8345
- wasm64 ? BinaryOp . EqI64 : BinaryOp . EqI32 ,
8346
- module . binary (
8347
- wasm64 ? BinaryOp . AndI64 : BinaryOp . AndI32 ,
8348
- indexExpr ,
8349
- wasm64 ? module . i64 ( CLOSURE_TAG ) : module . i32 ( CLOSURE_TAG )
8350
- ) ,
8351
- wasm64 ? module . i64 ( CLOSURE_TAG ) : module . i32 ( CLOSURE_TAG )
8352
- ) ,
8353
- thenExpr ,
8354
- elseExpr
8355
- ) ;
8356
- }
8357
-
8358
- private getClosurePtr ( closureExpr : ExpressionRef ) : ExpressionRef {
8359
- var module = this . module ;
8360
- var wasm64 = this . options . nativeSizeType == NativeType . I64 ;
8361
-
8362
- return module . binary (
8363
- wasm64 ? BinaryOp . ShlI64 : BinaryOp . ShlI32 ,
8364
- closureExpr ,
8365
- wasm64 ? module . i64 ( 4 ) : module . i32 ( 4 )
8366
- ) ;
8367
- }
8368
-
8369
- private getClosureReference ( closureExpr : ExpressionRef ) : ExpressionRef {
8370
- var module = this . module ;
8371
- var wasm64 = this . options . nativeSizeType == NativeType . I64 ;
8372
-
8373
- return module . binary (
8374
- wasm64 ? BinaryOp . OrI64 : BinaryOp . OrI32 ,
8375
- module . binary (
8376
- wasm64 ? BinaryOp . ShrI64 : BinaryOp . ShrI32 ,
8377
- closureExpr ,
8378
- wasm64 ? module . i64 ( 4 ) : module . i32 ( 4 )
8379
- ) ,
8380
- wasm64 ? module . i64 ( CLOSURE_TAG ) : module . i32 ( CLOSURE_TAG )
8381
- ) ;
8382
- }
8383
-
8384
8254
/** Makes sure the enclosing source file of the specified expression has been compiled. */
8385
8255
private maybeCompileEnclosingSource ( expression : Expression ) : void {
8386
8256
var internalPath = expression . range . source . internalPath ;
0 commit comments