Skip to content

Commit

Permalink
- Make JMP next field private and add setter and getter
Browse files Browse the repository at this point in the history
- Add mergeBlock method to LinearIRBlock to merge two adjacent blocks
- Update places using JMP next field to use getter
- Add IOrdIdentity interface for future use
  • Loading branch information
whtoo committed Nov 30, 2023
1 parent 27aa6d0 commit e464c3a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ep20/src/main/java/org/teachfx/antlr4/ep20/ir/Prog.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void linearInstrsImpl(@NotNull LinearIRBlock linearIRBlock) {
var nextBlock = linearIRBlock.getSuccessors().get(0);
for (var ref : linearIRBlock.getJmpRefMap()){
if (ref instanceof JMP jmp) {
jmp.next = nextBlock;
jmp.setNext(nextBlock);
} else if (ref instanceof CJMP cjmp) {
cjmp.setElseBlock(nextBlock);
}
Expand Down
11 changes: 10 additions & 1 deletion ep20/src/main/java/org/teachfx/antlr4/ep20/ir/stmt/JMP.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public JMP(LinearIRBlock block)
this.next = block;
block.refJMP(this);
}
public LinearIRBlock next;
private LinearIRBlock next;

@Override
public Label getTarget() {
Expand All @@ -32,4 +32,13 @@ public StmtType getStmtType() {
public String toString() {
return "jmp %s".formatted(next);
}

public void setNext(LinearIRBlock next) {
this.next = next;
next.refJMP(this);
}

public LinearIRBlock getNext() {
return this.next;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.teachfx.antlr4.ep20.pass.cfg;

import org.jetbrains.annotations.NotNull;

public interface IOrdIdentity<I> extends Comparable<IOrdIdentity<I>> {
I getID();
void setID(I id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.jetbrains.annotations.NotNull;
import org.teachfx.antlr4.ep20.ir.IRNode;
import org.teachfx.antlr4.ep20.ir.JMPInstr;
import org.teachfx.antlr4.ep20.ir.expr.Operand;
import org.teachfx.antlr4.ep20.ir.stmt.*;
import org.teachfx.antlr4.ep20.symtab.scope.Scope;
import org.teachfx.antlr4.ep20.utils.Kind;
Expand Down Expand Up @@ -176,7 +175,7 @@ public Optional<TreeSet<LinearIRBlock>> getJumpEntries() {
}
case END_BY_JMP -> {
JMP cjmp = (JMP) stmts.get(stmts.size() - 1);
var tid = cjmp.next;
var tid = cjmp.getNext();
entries.add(tid);
return Optional.of(entries);
}
Expand All @@ -196,4 +195,26 @@ public int hashCode() {
return (ord * 177) % 71;
}

/**
*
* @param otherBlock 必须是相邻的基本块才能进行合并
*/
public void mergeBlock(LinearIRBlock otherBlock) {
stmts.addAll(otherBlock.getStmts());
updateKindByLastInstr(stmts.get(stmts.size() - 1));
setSuccessors(otherBlock.getSuccessors());
for(var s : otherBlock.getSuccessors()) {
s.predecessors.remove(otherBlock);
s.predecessors.add(this);
}
for (var jmp : otherBlock.getJmpRefMap()) {
if (jmp instanceof JMP jmpInstr) {
jmpInstr.setNext(this);
} else {
var jmpInstr = (CJMP) jmp;
jmpInstr.setElseBlock(this);
jmpInstr.setThenBlock(this);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Void visit(Label label) {

@Override
public Void visit(JMP jmp) {
emit("br %s".formatted(jmp.next.toString()));
emit("br %s".formatted(jmp.getNext().toString()));
indents--;
return null;
}
Expand Down

0 comments on commit e464c3a

Please sign in to comment.