Skip to content

Commit

Permalink
Verify and encapsulate rescue IPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jun 19, 2024
1 parent 92b25ac commit fe33fa2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ExitableReturn interpret(ThreadContext context, Block block, IRubyObject
StaticScope currScope = interpreterContext.getStaticScope();
DynamicScope currDynScope = context.getCurrentScope();

int[] rescuePCs = interpreterContext.getRescueIPCs();
InterpreterContext.RescueTable rescuePCs = interpreterContext.getRescueIPCs();

// Init profiling this scope
boolean debug = IRRuntimeHelpers.isDebug();
Expand All @@ -95,7 +95,7 @@ public ExitableReturn interpret(ThreadContext context, Block block, IRubyObject

Operation operation = instr.getOperation();
if (debug) {
Interpreter.LOG.info("I: " + ipc + ", R: " + rescuePCs[ipc] + " - " + instr + ">");
Interpreter.LOG.info("I: " + ipc + ", R: " + rescuePCs.get(ipc) + " - " + instr + ">");
Interpreter.interpInstrsCount++;
} else if (profile) {
Profiler.instrTick(operation);
Expand Down Expand Up @@ -150,7 +150,7 @@ public ExitableReturn interpret(ThreadContext context, Block block, IRubyObject
} catch (Throwable t) {
if (debug) extractToMethodToAvoidC2Crash(instr, t);

ipc = rescuePCs == null ? -1 : rescuePCs[ipc];
ipc = rescuePCs == null ? -1 : rescuePCs.get(ipc);

if (debug) {
Interpreter.LOG.info("in : " + interpreterContext.getScope() + ", caught Java throwable: " + t + "; excepting instr: " + instr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ public void generateInstructionsForInterpretation() {
}
}

this.rescueIPCs = rescueIPCs;
checkRescueIPCs(rescueIPCs);

this.rescueIPCs = new RescueTable(rescueIPCs);
this.instructions = linearizedInstrArray;

// System.out.println("SCOPE: " + getScope().getId());
Expand Down Expand Up @@ -222,9 +224,9 @@ public FullInterpreterContext duplicate() {


public int determineRPC(int ipc) {
int length = rescueIPCs.length;
int length = rescueIPCs.size();
for (int i = 0; i + 1 < length; i += 2) {
if (ipc <= rescueIPCs[i]) return rescueIPCs[i + 1];
if (ipc <= rescueIPCs.get(i)) return rescueIPCs.get(i + 1);
}

throw new RuntimeException("BUG: no RPC found for " + getFileName() + ":" + getName() + ":" + ipc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class InterpreterContext {
// Contains pairs of values. The first value is number of instrs in this range + number of instrs before
// this range. The second number is the rescuePC. getRescuePC(ipc) will walk this list and first odd value
// less than this value will be the rpc.
protected volatile int[] rescueIPCs = null;
protected volatile RescueTable rescueIPCs = null;

// Cached computed fields
protected boolean hasExplicitCallProtocol; // Only can be true in Full+
Expand Down Expand Up @@ -125,12 +125,34 @@ private Instr[] prepareBuildInstructions(List<Instr> instructions) {
rescueIPCs[ipc] = rpc;
}

this.rescueIPCs = rescueIPCs;
checkRescueIPCs(rescueIPCs);

this.rescueIPCs = new RescueTable(rescueIPCs);

return linearizedInstrArray;
}

public int[] getRescueIPCs() {
public static class RescueTable {
final int[] table;
public RescueTable(int[] table) {
this.table = table;
}
public int get(int i) {
return table[i];
}

public int size() {
return table.length;
}
}

static void checkRescueIPCs(int[] rescueIPCs) {
for (int i = 0; i < rescueIPCs.length; i++) {
if (rescueIPCs[i] == 0) throw new RuntimeException("BUG: there should never be zero IPCs in the rescue table");
}
}

public RescueTable getRescueIPCs() {
return rescueIPCs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public IRubyObject interpret(ThreadContext context, Block block, IRubyObject sel
StaticScope currScope = interpreterContext.getStaticScope();
DynamicScope currDynScope = context.getCurrentScope();

int[] rescuePCs = interpreterContext.getRescueIPCs();
InterpreterContext.RescueTable rescuePCs = interpreterContext.getRescueIPCs();

// Init profiling this scope
boolean debug = IRRuntimeHelpers.isDebug();
Expand All @@ -55,7 +55,7 @@ public IRubyObject interpret(ThreadContext context, Block block, IRubyObject sel

Operation operation = instr.getOperation();
if (debug) {
Interpreter.LOG.info("I: " + ipc + ", R: " + rescuePCs[ipc] + " - " + instr + ">");
Interpreter.LOG.info("I: " + ipc + ", R: " + rescuePCs.get(ipc) + " - " + instr + ">");
Interpreter.interpInstrsCount++;
} else if (profile) {
Profiler.instrTick(operation);
Expand Down Expand Up @@ -109,7 +109,7 @@ public IRubyObject interpret(ThreadContext context, Block block, IRubyObject sel
} catch (Throwable t) {
if (debug) extractToMethodToAvoidC2Crash(instr, t);

ipc = rescuePCs == null ? -1 : rescuePCs[ipc];
ipc = rescuePCs == null ? -1 : rescuePCs.get(ipc);

if (debug) {
Interpreter.LOG.info("in : " + interpreterContext.getScope() + ", caught Java throwable: " + t + "; excepting instr: " + instr);
Expand Down

0 comments on commit fe33fa2

Please sign in to comment.