@@ -475,6 +475,7 @@ def end(self):
475475 assert not self .finished
476476 for block in self .blocks :
477477 block .end ()
478+ print ("%s is closed: %s" % (self , self .is_closed ()))
478479 self ._finished = True
479480
480481 def get_func_table (self ):
@@ -523,6 +524,7 @@ def variables_finalized(self):
523524 stackvars = self .add_entry_exit ()
524525 self .configure_parameters (bool (stackvars ))
525526 if self .is_closed ():
527+ self ._entryblock .force = True
526528 for insn in self ._exitblock .insns :
527529 self ._entryblock .add (insn )
528530 self ._exitblock .insns = []
@@ -533,7 +535,7 @@ def variables_finalized(self):
533535
534536 def add_entry_exit (self ):
535537 from .variables import LocalVariable , NbtOffsetVariable
536- from .instructions import PushNewStackFrame , PopStack , Call
538+ from .instructions import PushNewStackFrame , PopStack , Branch
537539 # sorted from head to tail of stack
538540 vars = sorted ([var .var for var in self .scope .values () \
539541 if isinstance (var , LocalVariable ) \
@@ -549,7 +551,7 @@ def add_entry_exit(self):
549551 in vars [::- 1 ])))
550552 self ._exitblock .add (PopStack ())
551553 # Always branch to real entry point
552- self ._entryblock .add (Call (self .blocks [0 ]))
554+ self ._entryblock .add (Branch (self .blocks [0 ]))
553555 return vars
554556
555557 def add_advancement_revoke (self , event ):
@@ -574,6 +576,8 @@ def __init__(self, name, func):
574576 self ._func = func
575577 self .needs_success_tracker = False
576578 self .defined = False
579+ self .is_function = False
580+ self .force = False
577581 self ._use = 0
578582
579583 def usage (self ):
@@ -585,6 +589,9 @@ def reset(self):
585589 def use_count (self ):
586590 return self ._use
587591
592+ def set_is_function (self ):
593+ self .is_function = True
594+
588595 @property
589596 def global_name (self ):
590597 return self ._func ._name + '/' + self ._name
@@ -597,14 +604,19 @@ def end(self):
597604
598605 def is_terminated (self ):
599606 assert self .defined
607+ if self .is_function :
608+ return True
600609 if not self .insns :
601610 return False
602611 return self .insns [- 1 ].terminator ()
603612
604613 def add (self , insn ):
605- from .instructions import Return , Call
614+ if not self .force and self .insns and self .insns [- 1 ].terminator ():
615+ assert False , "Block %s is terminated by %s. Tried adding %s" % (
616+ self , self .insns [- 1 ], insn )
617+ from .instructions import Return , Branch
606618 if isinstance (insn , Return ):
607- return super ().add (Call (self ._func ._exitblock ))
619+ return super ().add (Branch (self ._func ._exitblock ))
608620 return super ().add (insn )
609621
610622 def writeout (self , temp_gen ):
0 commit comments