Skip to content

Commit

Permalink
impr: add line number instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
jumanji144 committed Apr 15, 2024
1 parent 7da0d13 commit b6a4fd6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public void index(int index) {

@Override
public void label(Label label) {
ctx.label(labelNames.get(label.getIndex())).next();
String name = labelNames.get(label.getIndex());
ctx.label(name).next();
if(label.getLineNumber() != Label.UNSET) {
ctx.instruction("line").print(name).arg().print(Integer.toString(label.getLineNumber())).next();
}
}

@Override
Expand Down Expand Up @@ -218,7 +222,7 @@ public void execute(ConditionalJumpInstruction instruction) {

@Override
public void execute(VariableIncrementInstruction instruction) {
ctx.instruction(OPCODES[instruction.opcode()]).arg()
ctx.instruction(OPCODES[instruction.opcode()])
.literal(names.getName(instruction.variableIndex(), currentIndex + 1)).arg()
.literal(Integer.toString(instruction.incrementBy())).next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static int opcode(String name) {
throw new ExceptionInInitializerError(e);
}
}
opcodes.put("line", -1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public void accept(ErrorCollector collector, ASTMethodVisitor visitor) {
if (instruction instanceof ASTLabel lab) {
instructionVisitor.visitLabel(lab.identifier());
} else {
instructionVisitor.visitInstruction(instruction);
if (!instruction.identifier().content().equals("line"))
instructionVisitor.visitInstruction(instruction);
localIrInstructions.get(instructionIndex++).transform(instruction, instructionVisitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.darknet.assembler.ast.primitive.ASTIdentifier;
import me.darknet.assembler.ast.primitive.ASTInstruction;
import me.darknet.assembler.ast.primitive.ASTNumber;
import org.jetbrains.annotations.NotNull;

public interface ASTInstructionVisitor {
Expand All @@ -22,6 +23,16 @@ public interface ASTInstructionVisitor {
*/
void visitLabel(@NotNull ASTIdentifier label);

/**
* Visit a line number
*
* @param label
* the label
* @param line
* the line number
*/
void visitLineNumber(ASTIdentifier label, ASTNumber line);

void visitException(@NotNull ASTIdentifier start, @NotNull ASTIdentifier end, @NotNull ASTIdentifier handler, @NotNull ASTIdentifier type);

void visitEnd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,4 @@ public interface ASTJvmInstructionVisitor extends ASTInstructionVisitor {
*/
void visitMultiANewArrayInsn(ASTIdentifier descriptor, ASTNumber numDimensions);

/**
* Visit a line number
*
* @param label
* the label
* @param line
* the line number
*/
void visitLineNumber(ASTIdentifier label, ASTNumber line);

}

0 comments on commit b6a4fd6

Please sign in to comment.