Skip to content

Commit

Permalink
1. Handle CallFunc stack correctly in IRBuilder
Browse files Browse the repository at this point in the history
2. Add linear IR instructions in Prog for CFG analysis
  • Loading branch information
whtoo committed Oct 30, 2023
1 parent a9657ca commit 020534b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
31 changes: 18 additions & 13 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.teachfx.antlr4.ep20.ast.ASTNode;
import org.teachfx.antlr4.ep20.ir.IRNode;
import org.teachfx.antlr4.ep20.ir.Prog;
import org.teachfx.antlr4.ep20.ir.stmt.Label;
import org.teachfx.antlr4.ep20.parser.CymbolLexer;
import org.teachfx.antlr4.ep20.parser.CymbolParser;
import org.teachfx.antlr4.ep20.pass.ast.CymbolASTBuilder;
//import org.teachfx.antlr4.ep20.pass.ir.CymbolIRBuilder;
import org.teachfx.antlr4.ep20.pass.cfg.BasicBlock;
import org.teachfx.antlr4.ep20.pass.cfg.ControlFlowAnalysis;
import org.teachfx.antlr4.ep20.pass.ir.CymbolIRBuilder;
import org.teachfx.antlr4.ep20.pass.symtab.LocalDefine;

Expand All @@ -21,16 +26,16 @@
import java.util.List;

public class Compiler {
protected static void printIRTree(List<BasicBlock> blocks) {
for (var block : blocks) {
System.out.println(block);
for (var instr : block.getStmts()) {
System.out.println(" " + instr);
}
if(!block.getSuccessors().isEmpty()) {
printIRTree(block.getSuccessors());

private final static Logger logger = LogManager.getLogger(Compiler.class);
protected static void printIRTree(List<IRNode> irNodeList) {
var prettyFormatText = irNodeList.stream().map(irNode -> {
if (irNode instanceof Label) {
return irNode.toString();
}
}
return " "+irNode.toString();
}).reduce((a, b) -> a + "\n" + b);
logger.info("IR Tree:" + "\n"+prettyFormatText.get());
}

public static void main(String[] args) throws IOException {
Expand All @@ -49,10 +54,10 @@ public static void main(String[] args) throws IOException {
var irBuilder = new CymbolIRBuilder();

astRoot.accept(irBuilder);
printIRTree(irBuilder.prog.blockList);
//
// var dataFlowAnalysis = new ControlFlowAnalysis();
// irBuilder.root.accept(dataFlowAnalysis);
printIRTree(irBuilder.prog.linearInstrs());

var dataFlowAnalysis = new ControlFlowAnalysis();
irBuilder.prog.accept(dataFlowAnalysis);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public <S, E> E accept(IRVisitor<S, E> visitor) {
}


public static StackSlot pushStack() { return new StackSlot(); }
public static StackSlot pushStack() { return StackSlot.genTemp(); }
public static void popStack() { ordSeq--;}

public static int getOrdSeq() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public <S, E> S accept(IRVisitor<S, E> visitor) {

public String toSource() {
if (Objects.nonNull(rawLabel)) {
return scope.getScopeName() + "_" + rawLabel ;
return rawLabel ;
}

return scope.getScopeName() + "_" + seq ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ public VarSlot visit(StringExprNode stringExprNode) {
@Override
public VarSlot visit(CallFuncNode callExprNode) {
curNode = callExprNode;

var methodSymbol = callExprNode.getCallFuncSymbol();
var funcName = callExprNode.getFuncName();
var args = callExprNode.getArgsNode().size();
callExprNode.getArgsNode().forEach(x -> x.accept(this));
addInstr(new CallFunc(funcName,args));
addInstr(new CallFunc(funcName,args,methodSymbol));
return null;
}

Expand Down Expand Up @@ -250,6 +250,7 @@ public Void visit(WhileStmtNode whileStmtNode) {
@Override
public Void visit(IfStmtNode ifStmtNode) {
curNode = ifStmtNode;

ifStmtNode.getCondExpr().accept(this);
var cond = peekEvalOperand();
var thenBlock = new BasicBlock();
Expand Down Expand Up @@ -331,13 +332,17 @@ public Optional<VarSlot> addInstr(IRNode stmt) {
return Optional.of(StackSlot.pushStack());
} else if(stmt instanceof CJMP) {
popEvalOperand();

} else if (stmt instanceof CallFunc callFunc) {
int i = callFunc.getArgs();

while (i > 0){
popEvalOperand();
i--;
}
// 如果存在返回值,则要模拟压入返回值以保证栈平衡
if(!callFunc.getFuncType().isVoid()) {
pushEvalOperand(StackSlot.genTemp());
}
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public Type getPrimitiveType() {
return this;
}

@Override
public boolean isVoid() {
return name.equalsIgnoreCase("void");
}
}

0 comments on commit 020534b

Please sign in to comment.