Skip to content

Commit

Permalink
- create ep21 from prototype of ep20
Browse files Browse the repository at this point in the history
- add proto create scripts
  • Loading branch information
whtoo committed Dec 18, 2023
1 parent 336925e commit e3b81bc
Show file tree
Hide file tree
Showing 76 changed files with 2,352 additions and 1,001 deletions.
121 changes: 108 additions & 13 deletions ep21/src/main/java/org/teachfx/antlr4/ep21/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.teachfx.antlr4.ep21.ast.ASTNode;
import org.teachfx.antlr4.ep21.ir.IRNode;
import org.teachfx.antlr4.ep21.ir.stmt.Label;
import org.teachfx.antlr4.ep21.parser.CymbolLexer;
import org.teachfx.antlr4.ep21.parser.CymbolParser;
import org.teachfx.antlr4.ep21.pass.ast.CymbolASTBuilder;
import org.teachfx.antlr4.ep21.pass.cfg.CFG;
import org.teachfx.antlr4.ep21.pass.cfg.ControlFlowAnalysis;
import org.teachfx.antlr4.ep21.pass.codegen.CymbolAssembler;
import org.teachfx.antlr4.ep21.pass.ir.CymbolIRBuilder;
import org.teachfx.antlr4.ep21.pass.symtab.LocalDefine;
import org.teachfx.antlr4.ep21.utils.StreamUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Compiler {

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);
prettyFormatText.ifPresent(logger::debug);
}

public static void main(String[] args) throws IOException {
String fileName = args.length > 0 ? args[0] : (new File("src/main/resources/t.cymbol")).getAbsolutePath();

Expand All @@ -31,23 +51,98 @@ public static void main(String[] args) throws IOException {
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
CymbolParser parser = new CymbolParser(tokenStream);
ParseTree parseTree = parser.file();
CymbolASTBuilder astBuilder = new CymbolASTBuilder("t.cymbol");

CymbolASTBuilder astBuilder = new CymbolASTBuilder();

ASTNode astRoot = parseTree.accept(astBuilder);

astRoot.accept(new LocalDefine());

var irBuilder = new CymbolIRBuilder();

astRoot.accept(irBuilder);

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

Stream.of(
StreamUtils.indexStream(irBuilder.prog.blockList.stream()
.map(irBuilder::getCFG))
.peek(cfgPair -> {
var cfg = cfgPair.getRight();
var idx = cfgPair.getLeft();
saveToEp20Res(cfg.toString(), "%d_origin".formatted(idx));
cfg.addOptimizer(new ControlFlowAnalysis<>());
cfg.applyOptimizers();
saveToEp20Res(cfg.toString(), "%d_optimized".formatted(idx));
})
.map(Pair::getRight)
.map(CFG::getIRNodes)
.reduce(new ArrayList<IRNode>(), (a, b) -> {
a.addAll(b);
return a;
})
)
.map(irNodeList -> {
var assembler = new CymbolAssembler();
assembler.visit(irNodeList);
return assembler;
})
.forEach(assembler -> {
saveToEp18Res(assembler.getAsmInfo());
logger.debug("\n%s".formatted(assembler.getAsmInfo()));
});
}

protected static void saveToEp18Res(String buffer) {
String modulePath = "../ep18/src/main/resources"; // 替换 "my-module" 为你的模块名称
File moduleDirectory = new File(modulePath);
logger.debug("file path %s".formatted(moduleDirectory.getAbsolutePath()));
if (moduleDirectory.exists()) {
logger.debug("模块路径:" + moduleDirectory.getAbsolutePath());
var filePath = modulePath+"/t.vm";
File file = new File(filePath);
try (var outputStream = new FileOutputStream(file)) {
if (!file.exists()) {
var res = file.createNewFile();
logger.debug("create file %s is %b".formatted(filePath,res));
}
outputStream.write(buffer.getBytes());

} catch (IOException e) {
throw new RuntimeException(e);
}

} else {
logger.error("模块路径不存在!");
}
}

var assembler = new CymbolAssembler();
protected static void saveToEp20Res(String buffer,String suffix) {
String modulePath = "./src/main/resources"; // 替换 "my-module" 为你的模块名称
File moduleDirectory = new File(modulePath);
logger.debug("file path %s".formatted(moduleDirectory.getAbsolutePath()));
if (moduleDirectory.exists()) {
logger.debug("模块路径:" + moduleDirectory.getAbsolutePath());
var filePath = modulePath+"/graph_%s.md".formatted(suffix);
File file = new File(filePath);
try (var outputStream = new FileOutputStream(file)) {
if (!file.exists()) {
var res = file.createNewFile();
logger.debug("create file %s is %b".formatted(filePath,res));
}
String template = """
```mermaid
%s
```
""".formatted(buffer);
outputStream.write(template.getBytes());

List.of(dataFlowAnalysis,assembler).forEach(irBuilder.root::accept);
} catch (IOException e) {
throw new RuntimeException(e);
}

System.out.println(assembler.flushCode());
var url = Compiler.class.getClassLoader().getResource("t.vm");
System.out.println(">>>=" + new File(".").getAbsolutePath());
assembler.saveToFile("src/main/resources/t.vm");
assembler.saveToFile("../ep18/src/main/resources/t.vm");
} else {
logger.error("模块路径不存在!");
}
}
}
5 changes: 5 additions & 0 deletions ep21/src/main/java/org/teachfx/antlr4/ep21/ast/ASTNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@ public void dump(Dumper d) {
}

abstract protected void _dump(Dumper d);

@Override
public String toString() {
return ctx.getText();
}
}

13 changes: 1 addition & 12 deletions ep21/src/main/java/org/teachfx/antlr4/ep21/ast/CompileUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.List;

public class CompileUnit extends ASTNode {
private String srcName;

public void addFuncDecl(FuncDeclNode funcDecl) {
this.funcDeclarations.add(funcDecl);
Expand All @@ -29,9 +28,7 @@ public CompileUnit(List<VarDeclNode> varDeclarations, List<FuncDeclNode> funcDec
this.ctx = ctx;
}

public CompileUnit(String srcName) {
this.srcName = srcName;
}
public CompileUnit() {}

public List<VarDeclNode> getVarDeclarations() {
return varDeclarations;
Expand All @@ -56,14 +53,6 @@ protected void _dump(Dumper d) {
d.printNodeList("funcDecl",funcDeclarations);
}

public String getSrcName() {
return srcName;
}

public void setSrcName(String srcName) {
this.srcName = srcName;
}

@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import org.antlr.v4.runtime.ParserRuleContext;
import org.teachfx.antlr4.ep21.ast.ASTVisitor;
import org.teachfx.antlr4.ep21.ast.expr.ExprNode;
import org.teachfx.antlr4.ep21.ast.expr.IDExprNode;
import org.teachfx.antlr4.ep21.debugger.ast.Dumper;
import org.teachfx.antlr4.ep21.symtab.symbol.VariableSymbol;

import java.util.Objects;

public class VarDeclNode extends DeclNode {
private IDExprNode idExprNode;

private ExprNode assignExprNode;

public ExprNode initializerExpr() {
Expand All @@ -20,10 +23,11 @@ public boolean hasInitializer() {
}


public VarDeclNode(VariableSymbol variableSymbol,ExprNode assignExprNode, ParserRuleContext ctx) {
public VarDeclNode(VariableSymbol variableSymbol,ExprNode assignExprNode,IDExprNode idExprNode, ParserRuleContext ctx) {
this.refSymbol = variableSymbol;
this.assignExprNode = assignExprNode;
this.ctx = ctx;
this.idExprNode = idExprNode;
this.declName = variableSymbol.getName();
}

Expand Down Expand Up @@ -52,4 +56,12 @@ protected void _dump(Dumper d) {
d.printMember("type",getRefSymbol().getType());
if (Objects.nonNull(assignExprNode)) d.printMember("assignee",assignExprNode);
}

public IDExprNode getIdExprNode() {
return idExprNode;
}

public void setIdExprNode(IDExprNode idExprNode) {
this.idExprNode = idExprNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ public class IDExprNode extends ExprNode {

protected Symbol refSymbol;

protected boolean isVal = false;

public IDExprNode(String image,ParserRuleContext ctx) {
this.image = image;
this.ctx = ctx;

}

public boolean isLValue() {
return isVal;
}

public void setLValue(boolean isVal) {
this.isVal = isVal;
}

public Symbol getRefSymbol() {
return refSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public IfStmtNode(ExprNode exprNode, StmtNode then, StmtNode elseBlock, ParserRu
this.ctx = ctx;
}

public ExprNode getConditionalNode() {
public ExprNode getCondExpr() {
return conditionalNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Optional;

public abstract class Phase<Input,Output> implements Task<Input,Output>,ErrorIssuer {

public final String name;

public Phase(String name) {
Expand Down
33 changes: 10 additions & 23 deletions ep21/src/main/java/org/teachfx/antlr4/ep21/ir/IRVisitor.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
package org.teachfx.antlr4.ep21.ir;

import org.teachfx.antlr4.ep21.ir.def.Func;
import org.teachfx.antlr4.ep21.ir.expr.ArrayAccessExpr;
import org.teachfx.antlr4.ep21.ir.expr.CallFunc;
import org.teachfx.antlr4.ep21.ir.expr.ClassAccessExpr;
import org.teachfx.antlr4.ep21.ir.expr.Temp;
import org.teachfx.antlr4.ep21.ir.expr.addr.FrameSlot;
import org.teachfx.antlr4.ep21.ir.expr.addr.OperandSlot;
import org.teachfx.antlr4.ep21.ir.expr.arith.BinExpr;
import org.teachfx.antlr4.ep21.ir.expr.arith.UnaryExpr;
import org.teachfx.antlr4.ep21.ir.expr.values.BoolVal;
import org.teachfx.antlr4.ep21.ir.expr.values.IntVal;
import org.teachfx.antlr4.ep21.ir.expr.values.StringVal;
import org.teachfx.antlr4.ep21.ir.expr.values.Var;
import org.teachfx.antlr4.ep21.ir.expr.val.ConstVal;
import org.teachfx.antlr4.ep21.ir.stmt.*;

public interface IRVisitor<S,E> {
/// Expr
E visit(IntVal node);
E visit(BoolVal node);
E visit(StringVal node);

E visit(BinExpr node);

Expand All @@ -30,22 +21,18 @@ public interface IRVisitor<S,E> {
S visit(CJMP cjmp);
S visit(Assign assign);

S visit(Func func);

E visit(Var var);

E visit(ClassAccessExpr classAccessExpr);

E visit(ArrayAccessExpr arrayAccessExpr);

default S visit(Stmt stmt) { return stmt.accept(this);}

S visit(ReturnVal returnVal);
S visit(ExprStmt exprStmt);
default S visit(ExprStmt exprStmt) { return exprStmt.accept(this); }

default S visit(Prog prog) { return null; }

E visit(OperandSlot operandSlot);

S visit(Prog prog);
E visit(FrameSlot frameSlot);

E visit(Temp temp);
<T> E visit(ConstVal<T> tConstVal);

/// Stmt

Expand Down
7 changes: 7 additions & 0 deletions ep21/src/main/java/org/teachfx/antlr4/ep21/ir/JMPInstr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.teachfx.antlr4.ep21.ir;

import org.teachfx.antlr4.ep21.ir.stmt.Label;

public interface JMPInstr {
public Label getTarget();
}
Loading

0 comments on commit e3b81bc

Please sign in to comment.