Skip to content

Commit

Permalink
Refactor IR generation and assembly code generation
Browse files Browse the repository at this point in the history
- Removed unused JUnit dependencies from submodule POMs

- Print full IR code in Compiler before assembly

- Handle empty blocks gracefully in IR generation

- Emit function names without parentheses for builtins

- Use load/store instead of iload/istore opcodes

- Support strings and booleans in IntVal

- Fix bugs in CJMP codegen

- Add lombok dependency for logging

- Improve comments and log messages
  • Loading branch information
whtoo committed Nov 4, 2023
1 parent f1ff323 commit 207eff2
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 85 deletions.
7 changes: 1 addition & 6 deletions ep16/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@

<!-- 运行测试 -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
7 changes: 1 addition & 6 deletions ep17/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@

<!-- 运行测试 -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
7 changes: 1 addition & 6 deletions ep18/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@

<!-- 运行测试 -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
33 changes: 19 additions & 14 deletions ep18/src/main/resources/t.vm
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
.def dec1: args=1 ,locals=1:
.def dec1: args=1 ,locals=1
load 0
iconst 1
iload 0
isub
br L2
L2:
ret
.def main: args=0 ,locals=1:
.def main: args=0 ,locals=1
iconst 10
istore 0
store 0
br L5
L5:
load 0
iconst 0
iload 0
igt
brf L7
L6:
load 0
iconst 5
iload 0
igt
brf L9
L8:
iload 0
call print()
load 0
print
load 0
iconst 7
iload 0
brf L11
ieq
brf L9
L10:
iconst 7
br L4
L9:
call print()
iload 0
sconst "break"
print
load 0
call dec1()
istore 0
store 0
br L5
L7:
iconst 0
br L4
L4:
ret
halt
7 changes: 1 addition & 6 deletions ep19/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@

<!-- 运行测试 -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
3 changes: 2 additions & 1 deletion ep20/src/main/java/org/teachfx/antlr4/ep20/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public static void main(String[] args) throws IOException {
var irBuilder = new CymbolIRBuilder();

astRoot.accept(irBuilder);
// printIRTree(irBuilder.prog.linearInstrs());
printIRTree(irBuilder.prog.linearInstrs());

var assembler = new CymbolAssembler();
irBuilder.prog.accept(assembler);
saveToEp18Res(assembler.getAsmInfo());
logger.info("\n%s".formatted(assembler.getAsmInfo()));
}

protected static void saveToEp18Res(String buffer) {
Expand Down
37 changes: 29 additions & 8 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/ir/Prog.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package org.teachfx.antlr4.ep20.ir;

import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.teachfx.antlr4.ep20.ir.stmt.CJMP;
import org.teachfx.antlr4.ep20.ir.stmt.FuncEntryLabel;
import org.teachfx.antlr4.ep20.ir.stmt.JMP;
import org.teachfx.antlr4.ep20.ir.stmt.Label;
import org.teachfx.antlr4.ep20.pass.cfg.BasicBlock;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;


public class Prog extends IRNode {
public List<BasicBlock> blockList;

protected static Logger logger = LogManager.getLogger(Prog.class);
public List<IRNode> instrs = new ArrayList<>();

private List<IRNode> truncateInstrList = new LinkedList<>();
public Prog() {
this.blockList = new ArrayList<>() ;
}
Expand All @@ -31,10 +38,20 @@ private void linearInstrsImpl(BasicBlock basicBlock) {
if (!basicBlock.getStmts().isEmpty()) {
instrs.add(new Label(basicBlock.toString(),null));
instrs.addAll(basicBlock.getStmts());
}
} else {
if (basicBlock.getSuccessors().isEmpty()) {
return;
}

var nextBlock = basicBlock.getSuccessors().get(0);
for (var ref : basicBlock.getJmpRefMap()){
if (ref instanceof JMP jmp) {
jmp.next = nextBlock;
} else if (ref instanceof CJMP cjmp) {
cjmp.setElseBlock(nextBlock);
}
}

if (basicBlock.getSuccessors().isEmpty()) {
return;
}

for(var successor : basicBlock.getSuccessors()){
Expand All @@ -43,11 +60,15 @@ private void linearInstrsImpl(BasicBlock basicBlock) {
}

public List<IRNode> linearInstrs() {

if (!truncateInstrList.isEmpty()){
return truncateInstrList;
}

for(var block : blockList) {
linearInstrsImpl(block);
}

var buffer = new ArrayList<IRNode>();
IRNode prev = null;
IRNode cur = null;

Expand All @@ -56,14 +77,14 @@ public List<IRNode> linearInstrs() {
cur = instr;
if (Objects.nonNull(prev) && prev instanceof Label) {
if (cur instanceof FuncEntryLabel) {
buffer.remove(prev);
truncateInstrList.remove(prev);
}
}

buffer.add(cur);
truncateInstrList.add(cur);
}

return buffer;
return truncateInstrList;
}


Expand Down
24 changes: 22 additions & 2 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/ir/stmt/CJMP.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
public class CJMP extends Stmt {
public VarSlot cond;

public BasicBlock thenBlock;
public BasicBlock elseBlock;
private BasicBlock thenBlock;
private BasicBlock elseBlock;


public CJMP(VarSlot cond, BasicBlock thenLabel, BasicBlock elseLabel) {
this.cond = cond;
this.thenBlock = thenLabel;
this.elseBlock = elseLabel;
thenLabel.refJMP(this);
elseBlock.refJMP(this);
}

@Override
Expand All @@ -31,4 +33,22 @@ public StmtType getStmtType() {
public String toString() {
return "jmpIf %s,%s,%s".formatted(cond,thenBlock,elseBlock);
}

public void setElseBlock(BasicBlock elseBlock) {
this.elseBlock = elseBlock;
elseBlock.refJMP(this);
}

public void setThenBlock(BasicBlock thenBlock) {
this.thenBlock = thenBlock;
thenBlock.refJMP(this);
}

public BasicBlock getElseBlock() {
return elseBlock;
}

public BasicBlock getThenBlock() {
return thenBlock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public <S, E> S accept(IRVisitor<S, E> visitor) {
public JMP(BasicBlock block)
{
this.next = block;
block.refJMP(this);
}
public BasicBlock next;

Expand Down
33 changes: 21 additions & 12 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/ir/stmt/Label.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
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;

import java.util.Objects;

public class Label extends Stmt {

private Stmt nextEntry = null;


private String rawLabel;


private Scope scope;
protected int seq;

Expand All @@ -20,18 +25,10 @@ public Label(String rawLabel, Scope scope) {
}
}

public String getRawLabel() {
return rawLabel;
}

public void setRawLabel(String rawLabel) {
this.rawLabel = rawLabel;
}

public Scope getScope() {
return scope;
}

public void setScope(Scope scope) {
this.scope = scope;
}
Expand Down Expand Up @@ -59,10 +56,6 @@ public String toString() {
return toSource();
}

public Stmt getNextEntry() {
return nextEntry;
}

public void setNextEntry(Stmt nextEntry) {
this.nextEntry = nextEntry;
}
Expand All @@ -80,4 +73,20 @@ public Stmt fetchNextJumpEntry() {

return item;
}

public Stmt getNextEntry() {
return nextEntry;
}

public String getRawLabel() {
return rawLabel;
}

public Scope getScope() {
return scope;
}

public int getSeq() {
return seq;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class BasicBlock {

protected Scope scope = null;

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

private int ord = 0;

public BasicBlock() {
Expand Down Expand Up @@ -80,10 +82,23 @@ public void setScope(Scope scope) {

@Override
public String toString() {
// var firstInstr = stmts.get(0);
// if (firstInstr instanceof FuncEntryLabel) {
// return firstInstr.toString();
// }
return "L"+ord;
}

public List<IRNode> getJmpRefMap() {
return jmpRefMap;
}

public void setJmpRefMap(List<IRNode> jmpRefMap) {
this.jmpRefMap = jmpRefMap;
}

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

@Override
public boolean equals(Object obj) {
return toString().equalsIgnoreCase(obj.toString());
}
}
Loading

0 comments on commit 207eff2

Please sign in to comment.