@@ -166,11 +166,8 @@ func (f *Function) EndBody() {
166166
167167 param .name = "$" + param .name
168168 fb .emit (param )
169- if hasChunk {
170- param .init = NewRetain (NewGet (& np , np .pos ), np .pos )
171- } else {
172- param .init = NewGet (& np , np .pos )
173- }
169+
170+ param .init = NewGet (& np , np .pos )
174171
175172 f .params [i ] = & np
176173 }
@@ -309,9 +306,6 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
309306 case * Alloc :
310307 s .init = rc_expr (s .init , inloop , false , false , d , & pre )
311308 d .Stmts = append (d .Stmts , pre ... )
312- if s .init != nil && rtimp .hasChunk (s .init .Type ()) && ! s .init .retained () {
313- s .init = NewRetain (s .init , s .init .Pos ())
314- }
315309 d .emit (s )
316310
317311 case * Get :
@@ -322,33 +316,42 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
322316 s .Lhs [i ] = rc_expr (s .Lhs [i ], inloop , true , false , d , & pre )
323317 }
324318
325- for i := range s .Rhs {
326- s .Rhs [i ] = rc_expr (s .Rhs [i ], inloop , false , false , d , & pre )
327- if rtimp .hasChunk (s .Rhs [i ].Type ()) && ! s .Rhs [i ].retained () {
328- s .Rhs [i ] = NewRetain (s .Rhs [i ], s .Rhs [i ].Pos ())
319+ rhNeedRetain := make ([]bool , len (s .Lhs ))
320+ if len (s .Lhs ) == len (s .Rhs ) {
321+ for i := range s .Rhs {
322+ s .Rhs [i ] = rc_expr (s .Rhs [i ], inloop , false , false , d , & pre )
323+ if rtimp .hasChunk (s .Rhs [i ].Type ()) && ! s .Rhs [i ].retained () {
324+ rhNeedRetain [i ] = true
325+ }
326+ }
327+ } else {
328+ // rh 是元组
329+ notRetained := rtimp .hasChunk (s .Rhs [0 ].Type ()) && ! s .Rhs [0 ].retained ()
330+ for i := range rhNeedRetain {
331+ rhNeedRetain [i ] = notRetained
329332 }
330333 }
331334
332335 d .Stmts = append (d .Stmts , pre ... )
333336
334- allAssignable := true
335- assignable := make ([]bool , len (s .Lhs ))
337+ allLhsAssignable := true
338+ lhAssignable := make ([]bool , len (s .Lhs ))
336339 lhs := make ([]Var , len (s .Lhs ))
337340 for i , lh := range s .Lhs {
338341 if lh == nil {
339342 lhs [i ] = nil
340- assignable [i ] = true
343+ lhAssignable [i ] = true
341344 continue
342345 }
343346
344347 if v , ok := lh .(Var ); ok && v .Kind () == Register {
345348 lhs [i ] = v
346- assignable [i ] = true
349+ lhAssignable [i ] = true
347350 continue
348351 }
349352
350- allAssignable = false
351- assignable [i ] = false
353+ allLhsAssignable = false
354+ lhAssignable [i ] = false
352355 if get , ok := lh .(* Get ); ok {
353356 if v , ok := get .Loc .(Var ); ok && v .Kind () == Register {
354357 lhs [i ] = v
@@ -361,8 +364,10 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
361364 lhs [i ] = loc
362365 }
363366
364- if allAssignable {
365- d .emit (NewAssignN (lhs , s .Rhs , s .pos ))
367+ if allLhsAssignable {
368+ assign := NewAssignN (lhs , s .Rhs , s .pos )
369+ copy (assign .needRetain , rhNeedRetain )
370+ d .emit (assign )
366371 } else {
367372 rhs := make ([]Expr , len (s .Lhs ))
368373
@@ -386,31 +391,41 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
386391 loc := lhs [i ]
387392 rh := rhs [i ]
388393
389- if assignable [i ] {
390- d .emit (NewAssign (loc , rh , s .pos ))
394+ if lhAssignable [i ] {
395+ assign := NewAssign (loc , rh , s .pos )
396+ assign .needRetain [0 ] = rhNeedRetain [i ]
397+ d .emit (assign )
391398 continue
392399 }
393400
394401 if v , ok := rh .(Var ); ok {
395- d .emit (NewStore (loc , v , s .pos ))
402+ store := NewStore (loc , v , s .pos )
403+ store .needRetain = rhNeedRetain [i ]
404+ d .emit (store )
396405 continue
397406 }
398407
399408 if get , ok := rh .(* Get ); ok {
400409 if v , ok := get .Loc .(Var ); ok && v .Kind () == Register {
401- d .emit (NewStore (loc , v , s .pos ))
410+ store := NewStore (loc , v , s .pos )
411+ store .needRetain = rhNeedRetain [i ]
412+ d .emit (store )
402413 continue
403414 }
404415 }
405416
406417 if c , ok := rh .(* Const ); ok {
407- d .emit (NewStore (loc , c , s .pos ))
418+ store := NewStore (loc , c , s .pos )
419+ store .needRetain = false
420+ d .emit (store )
408421 continue
409422 }
410423
411424 imv := NewImv (d .newTempVarName (), rh , s .pos )
412425 d .emit (imv )
413- d .emit (NewStore (loc , imv , s .pos ))
426+ store := NewStore (loc , imv , s .pos )
427+ store .needRetain = rhNeedRetain [i ]
428+ d .emit (store )
414429 }
415430 }
416431
@@ -436,8 +451,8 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
436451 rc_block (s .Body , true , l .Body )
437452 rc_block (s .Post , true , l .Post )
438453
439- case * Retain :
440- d .emit (s )
454+ // case *Retain:
455+ // d.emit(s)
441456
442457 case * Drop :
443458 d .emit (s )
@@ -556,7 +571,7 @@ func (f *Function) autoDrop(b *Block) {
556571 f .autoDrop (s .Body )
557572 f .autoDrop (s .Post )
558573
559- case * Get , * Set , * Assign , * Store , * Br , * Return , * Unop , * Biop , * Retain , * Drop :
574+ case * Get , * Set , * Assign , * Store , * Br , * Return , * Unop , * Biop , * Drop :
560575 continue
561576
562577 default :
@@ -621,8 +636,8 @@ func (f *Function) allocVR_stmt(s Stmt, inloop bool) {
621636 case * Drop :
622637 f .dropVR_tank (s .X .Tank ())
623638
624- case * Retain :
625- panic ("Retain should not be here" )
639+ // case *Retain:
640+ // panic("Retain should not be here")
626641
627642 case * Br , * Return :
628643
@@ -691,8 +706,8 @@ func (f *Function) allocVR_expr(e Expr, inloop bool) {
691706 // f.allocVR_expr(e.X, inloop)
692707 // f.allocVR_var(e.Imv, inloop)
693708
694- case * Retain :
695- f .allocVR_expr (e .X , inloop )
709+ // case *Retain:
710+ // f.allocVR_expr(e.X, inloop)
696711
697712 default :
698713 panic (fmt .Sprintf ("Todo: %s" , e .(Stmt ).String ()))
@@ -865,8 +880,8 @@ func getReplace_expr(e Expr) (ret Expr) {
865880 e .X = getReplace_expr (e .X )
866881 e .Y = getReplace_expr (e .Y )
867882
868- case * Retain :
869- e .X = getReplace_expr (e .X )
883+ // case *Retain:
884+ // e.X = getReplace_expr(e.X)
870885
871886 case * Call :
872887 var call_common * CallCommon
0 commit comments