Skip to content

Commit

Permalink
feat: hook in Visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
xdecock committed Oct 30, 2024
1 parent 534543f commit bcf5965
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.objectweb.asm.commons.GeneratorAdapter;

import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;

// TODO testen wurde noch nicht getestet

Expand All @@ -31,21 +32,25 @@ public final class DoWhileVisitor implements LoopVisitor {
private Label begin;
private Label end;
private Label beforeEnd;
private int loopCounter;

public void visitBeginBody(GeneratorAdapter mv) {
end = new Label();
beforeEnd = new Label();

begin = new Label();
loopCounter = InterruptHandlerInjector.writeLoopInit(mv);
mv.visitLabel(begin);
}

public void visitEndBodyBeginExpr(GeneratorAdapter mv) {
InterruptHandlerInjector.writeLoopBodyEnd(mv, loopCounter, beforeEnd, "during do while");
mv.visitLabel(beforeEnd);
}

public void visitEndExpr(GeneratorAdapter mv) {
mv.ifZCmp(Opcodes.IFNE, begin);
InterruptHandlerInjector.writePreempt(mv, end, "after do while");
mv.visitLabel(end);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import lucee.transformer.Position;
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.Types;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;

public final class ForDoubleVisitor implements Opcodes, LoopVisitor {

Expand All @@ -33,9 +34,11 @@ public final class ForDoubleVisitor implements Opcodes, LoopVisitor {
public Label beforeBody = new Label(), afterBody = new Label();
public Label beforeUpdate = new Label(), afterUpdate = new Label();
public int i;
private int loopCounter;

public int visitBeforeExpression(GeneratorAdapter adapter, int start, int step, boolean isLocal) {
// init
loopCounter = InterruptHandlerInjector.writeLoopInit(adapter);
adapter.visitLabel(beforeInit);
forInit(adapter, start, isLocal);
adapter.goTo(beforeExpr);
Expand Down Expand Up @@ -75,6 +78,8 @@ public void forUpdate(GeneratorAdapter adapter, int step, boolean isLocal) {
adapter.visitVarInsn(DSTORE, i);
}
else adapter.visitIincInsn(i, step);

InterruptHandlerInjector.writeLoopBodyEnd(mv, loopCounter, beforeExpr, "during for loop");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import lucee.transformer.Position;
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.Types;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;

public final class ForIntVisitor implements Opcodes, LoopVisitor {

Expand All @@ -33,9 +34,11 @@ public final class ForIntVisitor implements Opcodes, LoopVisitor {
private Label beforeBody = new Label(), afterBody = new Label();
private Label beforeUpdate = new Label(), afterUpdate = new Label();
private int i;
private int loopCounter;

public int visitBeforeExpression(GeneratorAdapter adapter, int start, int step, boolean isLocal) {
// init
loopCounter = InterruptHandlerInjector.writeLoopInit(adapter);
adapter.visitLabel(beforeInit);
forInit(adapter, start, isLocal);
adapter.goTo(beforeExpr);
Expand Down Expand Up @@ -75,6 +78,8 @@ private void forUpdate(GeneratorAdapter adapter, int step, boolean isLocal) {
adapter.visitVarInsn(ISTORE, i);
}
else adapter.visitIincInsn(i, step);

InterruptHandlerInjector.writeLoopBodyEnd(mv, loopCounter, beforeExpr, "during for loop");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import lucee.transformer.Position;
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.Types;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;

public final class ForVisitor implements Opcodes, LoopVisitor {

Expand All @@ -35,8 +36,11 @@ public final class ForVisitor implements Opcodes, LoopVisitor {
private int i;
private Label lend = new Label();
private Label lbegin = new Label();
private int loopCounter;

public int visitBegin(GeneratorAdapter adapter, int start, boolean isLocal) {
loopCounter = InterruptHandlerInjector.writeLoopInit(adapter);

adapter.visitLabel(l0);

forInit(adapter, start, isLocal);
Expand Down Expand Up @@ -77,6 +81,8 @@ private void forInit(GeneratorAdapter adapter, int start, boolean isLocal) {
if (isLocal) adapter.loadLocal(start);
else adapter.push(start);
adapter.visitVarInsn(ISTORE, i);

InterruptHandlerInjector.writeLoopBodyEnd(mv, loopCounter, l1, "during for loop");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@

import lucee.transformer.Position;
import lucee.transformer.bytecode.BytecodeContext;
import lucee.transformer.bytecode.util.InterruptHandlerInjector;

public final class WhileVisitor implements LoopVisitor {

private Label begin;
private Label end;
private int loopCounter;

public void visitBeforeExpression(BytecodeContext bc) {
begin = new Label();
end = new Label();
bc.getAdapter().visitLabel(begin);
loopCounter = InterruptHandlerInjector.writeLoopInit(bc.getAdapter());
}

public void visitAfterExpressionBeforeBody(BytecodeContext bc) {
Expand All @@ -41,6 +44,8 @@ public void visitAfterExpressionBeforeBody(BytecodeContext bc) {

public void visitAfterBody(BytecodeContext bc, Position endline) {
bc.getAdapter().visitJumpInsn(Opcodes.GOTO, begin);

InterruptHandlerInjector.writeLoopBodyEnd(bc.getAdapter(), loopCounter, end, "during while loop");
bc.getAdapter().visitLabel(end);
bc.visitLine(endline);
}
Expand Down

0 comments on commit bcf5965

Please sign in to comment.