@@ -861,6 +861,7 @@ def __init__(self, ir_downstream):
861861 self .current_function = None
862862 self .loop_attacher = LoopAttacher (None , None , self , None )
863863 self .python_functions = {}
864+ self .temp_slots = set ()
864865 self .pragmas = {}
865866
866867 def import_py_lib (self , lib_exports ):
@@ -918,6 +919,14 @@ def unique_label(self, label):
918919 self .local_labels [label ] += 1
919920 return IR .Label ('%s_%d' % (label , self .local_labels [label ]))
920921
922+ def consume_slot (self , slot ):
923+ if slot in self .temp_slots :
924+ self .temp_slots .remove (slot )
925+ self .emit (IR .Free (slot ))
926+
927+ def virtual_slot (self , slot ):
928+ self .temp_slots .add (slot )
929+
921930 ### Pragmas
922931
923932 def visit_pragma (self , pragma ):
@@ -1169,6 +1178,7 @@ def visit_func_decl(self, decl):
11691178 self .current_function = func_symbol
11701179
11711180 self .local_labels = {}
1181+ self .temp_slots .clear ()
11721182
11731183 pragma = {}
11741184 # check pragmas
@@ -1342,6 +1352,7 @@ def visit_while_stmt(self, stmt):
13421352 self .emit (begin )
13431353 cond = self .visit_expression (stmt .cond )
13441354 self .emit (IR .JumpIfNot (end , cond ))
1355+ self .consume_slot (cond )
13451356 self .visit_statement (stmt .body )
13461357 self .emit (IR .Jump (begin ))
13471358 self .emit (end )
@@ -1355,21 +1366,23 @@ def visit_do_while_stmt(self, stmt):
13551366 self .emit (cond_label )
13561367 cond = self .visit_expression (stmt .cond )
13571368 self .emit (IR .JumpIf (begin , cond ))
1369+ self .consume_slot (cond )
13581370 self .emit (end )
13591371
13601372 def visit_for_stmt (self , stmt ):
13611373 with self .loop (continue_ = 'for_cont' , break_ = 'end_for' ) as (after_label , end ):
13621374 if stmt .init :
1363- self .visit_expression (stmt .init )
1375+ self .consume_slot ( self . visit_expression (stmt .init ) )
13641376 cond_label = self .unique_label ('for' )
13651377 self .emit (cond_label )
13661378 if stmt .cond :
13671379 cond = self .visit_expression (stmt .cond )
13681380 self .emit (IR .JumpIfNot (end , cond ))
1381+ self .consume_slot (cond )
13691382 self .visit_statement (stmt .body )
13701383 self .emit (after_label )
13711384 if stmt .after :
1372- self .visit_expression (stmt .after )
1385+ self .consume_slot ( self . visit_expression (stmt .after ) )
13731386 self .emit (IR .Jump (cond_label ))
13741387 self .emit (end )
13751388
@@ -1379,6 +1392,7 @@ def visit_if_stmt(self, stmt):
13791392
13801393 cond = self .visit_expression (stmt .cond )
13811394 self .emit (IR .JumpIfNot (false_label , cond ))
1395+ self .consume_slot (cond )
13821396
13831397 if self .exists (stmt .true ):
13841398 self .visit_statement (stmt .true )
@@ -1415,6 +1429,7 @@ def visit_switch_stmt(self, stmt):
14151429 self .emit_op (IR .Op .Eq , option , self .int (choice ), res )
14161430 self .emit (IR .JumpIf (dest , res ))
14171431 self .emit (IR .Jump (default_label ))
1432+ self .consume_slot (option )
14181433
14191434 for label , body in case_bodies :
14201435 self .emit (label )
@@ -1444,6 +1459,7 @@ def visit_return_stmt(self, stmt):
14441459 # for the return value, so store it there
14451460 dest = self .offset (IR .StackPointer , - ret_type .size , ret_type )
14461461 self .emit (IR .Move (ret_val , dest ))
1462+ self .consume_slot (ret_val )
14471463 self .emit (IR .Return ())
14481464
14491465 def visit_goto_stmt (self , stmt ):
@@ -1453,7 +1469,7 @@ def visit_sync_stmt(self, stmt):
14531469 self .emit (IR .Sync ())
14541470
14551471 def visit_expr_stmt (self , stmt ):
1456- self .visit_expression (stmt .expr )
1472+ self .consume_slot ( self . visit_expression (stmt .expr ) )
14571473
14581474 ### Expressions
14591475
@@ -1523,6 +1539,7 @@ def visit_binop_expr(self, expr):
15231539 op = IR .Op .lookup (expr .op )
15241540 res = mk_slot (left .type )
15251541 self .emit_op (op , left , right , res )
1542+ self .virtual_slot (res )
15261543 return res
15271544
15281545 def visit_member_access_expr (self , expr ):
0 commit comments