4949 * run the stack of ensure blocks in the right order.
5050 * ----------------------------------------------------------------------------------- */
5151class EnsureBlockInfo {
52- final Label regionStart ;
53- final Label start ;
54- final Label end ;
55- final Label dummyRescueBlockLabel ;
52+ final Label start ; // Beginning of this ensure region
53+ final Label bodyStart ; // Beginning of any actual instrs in the ensure body (will not be emitted if none)
54+ final Label end ; // End of this ensure region
55+ final Label ensureRescue ; // Where exceptions in an ensure flow through
5656 Variable savedGlobalException ;
5757 boolean needsBacktrace ;
5858
@@ -65,13 +65,13 @@ class EnsureBlockInfo {
6565 // This ensure block's instructions
6666 final List <Instr > instrs ;
6767
68- public EnsureBlockInfo (IRScope s , IRLoop l , Label bodyRescuer ) {
68+ public EnsureBlockInfo (IRScope s , IRLoop l , Label bodyRescuer , int sourceLine ) {
6969 // this technically may be any block and not specifically rescue but for the sake of looking at the CFG
7070 // it is more or less a begin block with exception handling around it.
71- regionStart = s .getNewLabel ("BEGIN" );
72- start = s .getNewLabel ("RESC_START" );
73- end = s .getNewLabel ("AFTER_RESC" );
74- dummyRescueBlockLabel = s .getNewLabel ("RESC_DUMMY" );
71+ start = s .getNewLabel ("ENSURE_BEGIN_@" + sourceLine );
72+ bodyStart = s .getNewLabel ("ENSURE_BODY_:@" + sourceLine );
73+ end = s .getNewLabel ("ENSURE_END_:@" + sourceLine );
74+ ensureRescue = s .getNewLabel ("ENSURE_CATCH_@" + sourceLine );
7575 instrs = new ArrayList <>(4 );
7676 savedGlobalException = null ;
7777 innermostLoop = l ;
@@ -87,10 +87,14 @@ public void addInstrAtBeginning(Instr i) {
8787 instrs .add (0 , i );
8888 }
8989
90- public void emitBody (IRBuilder b ) {
91- b .addInstr (new LabelInstr (start ));
90+ /**
91+ * Emit the saved instrs for the ensure body into the current location in the provided builder.
92+ * @param builder
93+ */
94+ public void emitEnsureBody (IRBuilder builder ) {
95+ builder .addInstr (new LabelInstr (bodyStart ));
9296 for (Instr i : instrs ) {
93- b .addInstr (i );
97+ builder .addInstr (i );
9498 }
9599 }
96100
@@ -106,20 +110,20 @@ public void cloneIntoHostScope(IRBuilder builder) {
106110 // there are no actual instrs pushed yet (but ebi has reserved a frame for it -- e.g. the rescue/ensure
107111 // the next is in). Since it is doing nothing we have nothing to clone. By skipping this we prevent
108112 // setting exception regions and simplify CFG construction.
109- if (instrs .size () == 0 ) return ;
113+ if (instrs .isEmpty () ) return ;
110114
111115 SimpleCloneInfo ii = new SimpleCloneInfo (builder .scope , true );
112116
113117 // Clone required labels.
114118 // During normal cloning below, labels not found in the rename map
115119 // are not cloned.
116- ii .renameLabel (start );
120+ ii .renameLabel (bodyStart );
117121 for (Instr i : instrs ) {
118122 if (i instanceof LabelInstr ) ii .renameLabel (((LabelInstr )i ).getLabel ());
119123 }
120124
121125 // Clone instructions now
122- builder .addInstr (new LabelInstr (ii .getRenamedLabel (start )));
126+ builder .addInstr (new LabelInstr (ii .getRenamedLabel (bodyStart )));
123127 builder .addInstr (new ExceptionRegionStartMarkerInstr (bodyRescuer ));
124128 for (Instr instr : instrs ) {
125129 Instr clonedInstr = instr .clone (ii );
0 commit comments