Skip to content

Commit

Permalink
1. Moving the block label to a separate Label class
Browse files Browse the repository at this point in the history
2. Adding various utility methods for managing blocks
Removing unused fields like predecessors
3. Changing CFGBuilder to build from LinearIRBlock instead of raw IR
4. Deleting the lombok dependency from pom.xml
  • Loading branch information
whtoo committed Nov 18, 2023
1 parent f86770f commit 4ffc7f7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 46 deletions.
10 changes: 7 additions & 3 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/ir/Prog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.teachfx.antlr4.ep20.ir.stmt.CJMP;
import org.teachfx.antlr4.ep20.ir.stmt.FuncEntryLabel;
import org.teachfx.antlr4.ep20.ir.stmt.JMP;
Expand Down Expand Up @@ -33,17 +34,19 @@ public void addBlock(LinearIRBlock linearIRBlock) {
blockList.add(linearIRBlock);
}

private void linearInstrsImpl(LinearIRBlock linearIRBlock) {
private void linearInstrsImpl(@NotNull LinearIRBlock linearIRBlock) {
// Add all instr from non-empty block
if (!linearIRBlock.getStmts().isEmpty()) {
if (!linearIRBlock.getJmpRefMap().isEmpty()){
instrs.add(new Label(linearIRBlock.toString(),null));
instrs.add(linearIRBlock.getLabel());
}
instrs.addAll(linearIRBlock.getStmts());
} else {
// Drop empty block
if (linearIRBlock.getSuccessors().isEmpty()) {
return;
}

// Auto-fill next block for jmp/cjmp
var nextBlock = linearIRBlock.getSuccessors().get(0);
for (var ref : linearIRBlock.getJmpRefMap()){
if (ref instanceof JMP jmp) {
Expand All @@ -55,6 +58,7 @@ private void linearInstrsImpl(LinearIRBlock linearIRBlock) {

}

// recursive call
for(var successor : linearIRBlock.getSuccessors()){
linearInstrsImpl(successor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

public class CJMP extends Stmt {
public VarSlot cond;

private LinearIRBlock thenBlock;
private LinearIRBlock elseBlock;

Expand Down
8 changes: 6 additions & 2 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/ir/stmt/Label.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.teachfx.antlr4.ep20.ir.stmt;

import lombok.Getter;
import org.teachfx.antlr4.ep20.ir.IRVisitor;
import org.teachfx.antlr4.ep20.symtab.scope.Scope;

Expand All @@ -10,7 +9,6 @@ public class Label extends Stmt {

private Stmt nextEntry = null;


private String rawLabel;


Expand All @@ -25,6 +23,12 @@ public Label(String rawLabel, Scope scope) {
}
}

public Label(Scope scope) {
this.scope = scope;
this.seq = scope.getLabelSeq();
this.rawLabel = null;
}

public void setRawLabel(String rawLabel) {
this.rawLabel = rawLabel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.teachfx.antlr4.ep20.pass.cfg;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import org.teachfx.antlr4.ep20.ir.IRNode;
Expand All @@ -17,7 +18,7 @@ public class BasicBlock<I extends IRNode> implements Iterable<Loc<I>> {
// Generate codes
public List<Loc<I>> codes;

public int id;
public final int id;

protected Optional<Label> label;

Expand Down Expand Up @@ -45,6 +46,12 @@ public Loc<I> next() {
};
}

@NotNull
@Contract("_ -> new")
public static BasicBlock<IRNode> buildFromLinearBlock(@NotNull LinearIRBlock block) {
return new BasicBlock<IRNode>(block.getKind(), block.getOrd(), block.getStmts().stream().map(Loc::new).toList(), Optional.of(block.getLabel()));
}

public BasicBlock(Kind kind, int id, List<Loc<I>> codes, Optional<Label> label) {
this.codes = codes;
this.label = label;
Expand All @@ -56,6 +63,10 @@ public int getId() {
return id;
}

public Optional<Label> getLabel() {
return label;
}

// Generate isEmpty
public boolean isEmpty() {
return codes.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

public class CFGBuilder<I extends IRNode> {
public CFGBuilder(List<I> instrs){
public CFGBuilder(BasicBlock<IRNode> startNode){

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@

public class LinearIRBlock {

public Kind kind = Kind.CONTINUOUS;
// Fields
private static int LABEL_SEQ = 1;
private Kind kind = Kind.CONTINUOUS;
private int ord = 1;
private ArrayList<IRNode> stmts;

private List<LinearIRBlock> successors;

private List<LinearIRBlock> predecessors;

protected Scope scope = null;

private Scope scope = null;
private List<IRNode> jmpRefMap = new ArrayList<>();

private int ord = 0;

// Constructor
public LinearIRBlock() {
stmts = new ArrayList<>();
successors = new ArrayList<>();
predecessors = new ArrayList<>();
ord = LABEL_SEQ++;
}

// Methods
// Statement Operations
public void addStmt(IRNode stmt) {
stmts.add(stmt);
updateKindByLastInstr(stmt);
Expand All @@ -47,10 +46,12 @@ private void updateKindByLastInstr(IRNode stmt) {
kind = Kind.CONTINUOUS;
}
}

public static boolean isBasicBlock(Stmt stmt) {
return !(stmt instanceof CJMP) && !(stmt instanceof JMP);
}

// Getters and Setters
public List<IRNode> getStmts() {
return stmts;
}
Expand All @@ -77,15 +78,6 @@ public void setPredecessors(List<LinearIRBlock> predecessors) {
this.predecessors = predecessors;
}

public static void setLink(LinearIRBlock current, LinearIRBlock next) {
current.successors.add(next);
next.predecessors.add(current);
}

public void setLink(LinearIRBlock next) {
LinearIRBlock.setLink(this,next);
}

public Scope getScope() {
return scope;
}
Expand All @@ -94,17 +86,6 @@ public void setScope(Scope scope) {
this.scope = scope;
}

@Override
public String toString() {
var firstInstr = stmts.get(0);

if (firstInstr instanceof Label){
return firstInstr.toString();
}

return "L"+ord;
}

public List<IRNode> getJmpRefMap() {
return jmpRefMap;
}
Expand All @@ -117,17 +98,53 @@ public int getOrd() {
return ord;
}

// Link Operations
public static void setLink(LinearIRBlock current, LinearIRBlock next) {
current.successors.add(next);
next.predecessors.add(current);
}

public void setLink(LinearIRBlock next) {
LinearIRBlock.setLink(this, next);
}

public Kind getKind() {
return kind;
}

// Jump Operations
public void refJMP(IRNode node) {
jmpRefMap.add(node);
}

// Utility Methods
@Override
public boolean equals(Object obj) {
return toString().equalsIgnoreCase(obj.toString());
public String toString() {
var firstInstr = stmts.get(0);

if (firstInstr instanceof FuncEntryLabel) {
return firstInstr.toString();
}
return "L" + ord;
}

public IRNode getLastInstr() {
return stmts.get(stmts.size() - 1);
public String toSource(){
var firstInstr = stmts.get(0);

if (firstInstr instanceof FuncEntryLabel) {
return ((FuncEntryLabel) firstInstr).toSource();
}

return "L" + ord;
}

public Label getLabel() {
var firstInstr = stmts.get(0);

if (firstInstr instanceof FuncEntryLabel funcEntryLabel) {
return funcEntryLabel;
}

return new Label(toString(),scope);
}
}
}
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@
<version>${JUnit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<!-- JUnit Jupiter Engine for running tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down

0 comments on commit 4ffc7f7

Please sign in to comment.