Skip to content

Commit

Permalink
更新设置, 删除行末空白
Browse files Browse the repository at this point in the history
  • Loading branch information
Origami404 committed Jun 23, 2022
1 parent bd328d0 commit d054289
Show file tree
Hide file tree
Showing 25 changed files with 78 additions and 76 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ target

# Generated frontend codes
*.tokens
src/top/origami404/ssyc/frontend/*
src/top/origami404/ssyc/frontend/SysY*
!src/top/origami404/ssyc/frontend/SysY.g4
!src/top/origami404/ssyc/frontend/IRGen.java

# Dustbin
_trash
Expand Down
9 changes: 6 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
"Instruction.java": "*Inst.java",
"IRType.java": "*IRTy.java"
},

"workbench.editor.enablePreview": true,
"explorer.compactFolders": true,

"editor.rulers": [
120
],
"files.trimTrailingWhitespace": true,


"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx16G -Xms512m",
"java.completion.filteredTypes": [
"java.awt.*",
"com.sun.*",
Expand All @@ -41,4 +41,7 @@
// Add javax
"javax.*",
],
"java.project.referencedLibraries": [
"lib/**/*.jar",
]
}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@

```
$ ./m test
Test test-data/asm-handmade/0-number.sy return 1
Test test-data/asm-handmade/1-add-sub.sy return 1
Test test-data/asm-handmade/2-mul-div-mod.sy return 1
Test test-data/asm-handmade/3-variable.sy return 1
Test test-data/asm-handmade/4-if.sy return 1
Test test-data/asm-handmade/5-while.sy return 1
Test test-data/asm-handmade/0-number.sy return 1
Test test-data/asm-handmade/1-add-sub.sy return 1
Test test-data/asm-handmade/2-mul-div-mod.sy return 1
Test test-data/asm-handmade/3-variable.sy return 1
Test test-data/asm-handmade/4-if.sy return 1
Test test-data/asm-handmade/5-while.sy return 1
```

此即为所有代码均编译错误, 因为在 `asm-handmade` 文件夹内的程序均期望返回 0. 自动判断与比对输出将在日后支持.
Expand All @@ -92,4 +92,4 @@ Test test-data/asm-handmade/5-while.sy return 1
- 时间规划: <docs/deadline.md>
- 代码风格: <docs/style.md>
- IR 设计: <docs/ir.md>
-
-
2 changes: 1 addition & 1 deletion docker/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ do
sy_file="${PWD}/${input}"
arm_file="${PWD}/${input/sy/s}"
exec_file="${PWD}/${input/sy/exec}"

./m run asm ${sy_file} ${arm_file}
if [ $? -ne 0 ]; then
print_info ${input} ${RED} "ssyc FAIL"
Expand Down
22 changes: 11 additions & 11 deletions docs/ir.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Value
> User
> Instruction (InstKind)
> BinaryOpInst
: IAdd, ISub, IMul, IDiv, IMod, FAdd, FSub, FMul, FDiv,
: IAdd, ISub, IMul, IDiv, IMod, FAdd, FSub, FMul, FDiv,
> UnaryOpInst
: INeg, FNeg,
> CmpInst
Expand All @@ -35,13 +35,13 @@ Value
IRType (IRTyKind)
> SimpleIRTy
: Void, Bool, Int, Float,
: Void, Bool, Int, Float,
> ArrayIRTy
> PointerIRTy
> FunctionIRTy
```

其中每一层缩进代表一个逻辑上的 `is-a`/从属 关系, `>` 代表该关系使用 Java 中的继承来表示, `:` 代表该关系通过该类的某个类别枚举 (`xxxKind`) 来实现.
其中每一层缩进代表一个逻辑上的 `is-a`/从属 关系, `>` 代表该关系使用 Java 中的继承来表示, `:` 代表该关系通过该类的某个类别枚举 (`xxxKind`) 来实现.

### 枚举实现的 "子类关系"

Expand All @@ -66,7 +66,7 @@ IR 里的被使用者(Usee)是 `Value` 类, 任何一个 `Value` 类的实例都

### IList 与 INode

`IList``I` 代表 `intrusive`, 是侵入式链表的缩写. 所谓侵入式链表, 就是在这个链表里的元素本身必须要作出修改才能成为这个链表里的元素. 在实现上, 这个修改就是得把 `INode`, 侵入式链表的节点, 作为该元素的一个成员.
`IList``I` 代表 `intrusive`, 是侵入式链表的缩写. 所谓侵入式链表, 就是在这个链表里的元素本身必须要作出修改才能成为这个链表里的元素. 在实现上, 这个修改就是得把 `INode`, 侵入式链表的节点, 作为该元素的一个成员.

实现该侵入式链表的原因有二:

Expand Down Expand Up @@ -183,10 +183,10 @@ public class INode<E, P> {

// 如果当前节点是链表的头节点, 那么当往前插入时, 还要修改链表的头节点
oldPrev.ifPresentOrElse(
n -> n.setNext(newPrev),
n -> n.setNext(newPrev),
() -> parent.ifPresent(p -> p.setBegin(newPrev)));
newPrev.setPrev(oldPrev.orElse(null));

newPrev.setNext(this);
this.setPrev(newPrev);

Expand All @@ -211,31 +211,31 @@ public class INode<E, P> {

### 类型 (type) 与类别 (kind)

凡是与源语言语义有关的, 比如表示一段二进制串的可用操作, 或者是 IR 里的值的种类的, 称之为类型 (Type). 凡是用于代码实现里用来做 RTTI, 或者标志同一个类的实例的种类的, 称之为类别 (kind).
凡是与源语言语义有关的, 比如表示一段二进制串的可用操作, 或者是 IR 里的值的种类的, 称之为类型 (Type). 凡是用于代码实现里用来做 RTTI, 或者标志同一个类的实例的种类的, 称之为类别 (kind).

比如一条 IR 指令有类型也有类别. 其类型 (`inst.getType()`) 就是指其 IR 里返回值的类型, 比如 `INT`, `FLOAT`, `BOOL`, `[4 * INT]`. 其类别 (`Inst.getKind()`) 就是它具体是哪一种 IR, 比如 `IAdd`, `ISub`, `FCmpGt`.

### 所有者 (owner) 与父对象 (parent)

称类 `A` 为类 `B` 的所有者 (owner), 若 A 中包含一个 `B` 的实例. 称类 `P` 为类 `B` 的父对象 (parent), 若 P 中包含一个 `IList<B>` (即多个 `B` 对象). 更一般地, 所有者一般只是指有作为类成员的关系, 而父对象一般不但有实现上的包含, 还有概念上的包含.

## 笔记: Alloc, Load, Store, GEP
## 笔记: Alloc, Load, Store, GEP

Alloc 是用来获得一块特定大小的内存的 (通过提供特定的类型, 分配该类型大小的一块内存, 其返回类型永远是一个指向该特定类型的指针).
Alloc 是用来获得一块特定大小的内存的 (通过提供特定的类型, 分配该类型大小的一块内存, 其返回类型永远是一个指向该特定类型的指针).

Load 解引用某个指针获得其值 (可视为去掉 `*`), Store 将某个值存放到指针所指的区域 (可视为加上 `*`).

GEP (get element pointer) 可以去掉嵌套的指针与数组类型, 将高维的指针偏移为某个基本类型的低维指针. 返回值就是该指针.

所以, 一般而言, 对数组的访问的 ir 一般是长这样的:
所以, 一般而言, 对数组的访问的 ir 一般是长这样的:

```
%arr = alloc [4 x [5 x i32]] # %0 的类型是 [4 x [5 x i32]]*
# 第一个 0 表示 "将 %arr 偏移 0 个基类型([4 x [5 x i32]]), 获得了一个 [4 x [5 x i32]]*"
# 第二个 1 表示 "将上一步得到的指针偏移 1 个基类型([5 x i32]), 获得了一个 [5 x i32]*"
# 第三个 3 表示 "将上一步得到的指针偏移 3 个基类型(i32), 获得了一个 i32*"
%target = gep %arr, (0, 1, 3)
%target = gep %arr, (0, 1, 3)
store %target 233 # 将 233 存进去
```
Expand Down
2 changes: 1 addition & 1 deletion docs/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- 使用 `switch` 表达式
- 在 interface 中使用 default 方法
- 使用 `record` 关键字
-
-

若需要新特性一览, 可参考 [Java Guide](https://github.com/forax/java-guide).

Expand Down
2 changes: 1 addition & 1 deletion m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cleanup() {
compile() {
# Generate lexer and parser
java -jar bin/antlr4.jar -visitor -no-listener $(find src -name '*.g4')

ANTLR_EXT_CACHE=$(find src -name '.antlr')
test -d "$ANTLR_EXT_CACHE" && rm -r "$ANTLR_EXT_CACHE"

Expand Down
6 changes: 3 additions & 3 deletions src/top/origami404/ssyc/DebugTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static String findFirstToken(ParserRuleContext rc) {

for (final var child : rc.children) {
if (child instanceof TerminalNode tn) {
return tn.getText();
return tn.getText();
}
}

Expand All @@ -22,15 +22,15 @@ public static String findFirstToken(ParserRuleContext rc) {
public static StringBuffer toDebugTreeString(ParserRuleContext rc) {
final var sb = new StringBuffer();
sb.append('(');

sb.append(SysYParser.ruleNames[rc.getRuleIndex()]);
sb.append('['); sb.append(findFirstToken(rc)); sb.append(']');

for (final var child : rc.getRuleContexts(ParserRuleContext.class)) {
sb.append(' ');
sb.append(toDebugTreeString(child));
}

sb.append(')');
return sb;
}
Expand Down
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void main(String[] args) throws IOException, FileNotFoundException
private static InputStream openInput(String filename) throws FileNotFoundException {
if (filename.equals("-")) {
return System.in;
}
}

return new FileInputStream(filename);
}
Expand Down
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/ir/BasicBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import top.origami404.ssyc.utils.INode;
import top.origami404.ssyc.utils.INodeOwner;

public class BasicBlock extends Value
public class BasicBlock extends Value
implements IListOwner<Instruction, BasicBlock>, INodeOwner<BasicBlock, Function>
{
public BasicBlock() {
Expand Down
4 changes: 2 additions & 2 deletions src/top/origami404/ssyc/ir/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public IRType getType() {
return type;
}


public List<User> getUserList() {
return userList;
}

/**
* 一个增加使用者的 "被动" 方法, 它只是朴素地加入一个 User, 不会 "主动" 维护 use-def 关系
* @param user 待加入的 User
Expand Down
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/ir/inst/BinaryOpInst.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public BinaryOpInst(InstKind opKind, Value lhs, Value rhs) {
super.addOperandCO(lhs);
super.addOperandCO(rhs);


final var resultTyKind = lhs.getType().getKind();
assert lhs.getType() == rhs.getType()
: "lhs should have the same type as rhs";
Expand Down
4 changes: 2 additions & 2 deletions src/top/origami404/ssyc/ir/inst/BrCondInst.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
public class BrCondInst extends Instruction {
BrCondInst(Value cond, BasicBlock trueBB, BasicBlock falseBB) {
super(InstKind.BrCond, IRType.VoidTy);

this.cond = cond;
this.trueBB = trueBB;
this.falseBB = falseBB;

super.addOperandCO(cond);
super.addOperandCO(trueBB);
super.addOperandCO(falseBB);
Expand Down
4 changes: 2 additions & 2 deletions src/top/origami404/ssyc/ir/inst/GEPInst.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class GEPInst extends Instruction {
// indices: index 的复数形式
public GEPInst(Value ptr, List<Value> indices) {
super(InstKind.GEP, calcResultType(ptr.getType(), indices.size()));

this.ptr = ptr;
this.indices = indices;
this.indices = indices;

super.addOperandCO(ptr);
super.addAllOperandsCO(indices);
Expand Down
8 changes: 4 additions & 4 deletions src/top/origami404/ssyc/ir/inst/InstKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.Set;

public enum InstKind {
IAdd, ISub, IMul, IDiv, IMod,
FAdd, FSub, FMul, FDiv,
IAdd, ISub, IMul, IDiv, IMod,
FAdd, FSub, FMul, FDiv,

INeg, FNeg,

Expand All @@ -23,14 +23,14 @@ public enum InstKind {
public boolean isUnary() { return unarySet.contains(this); }
public boolean isCmp() { return cmpSet.contains(this); }
public boolean isBr() { return brSet.contains(this); }

public boolean isInt() { return intSet.contains(this); }
public boolean isFloat() { return floatSet.contains(this); }


private static final Set<InstKind> unarySet = Set.of(INeg, FNeg);
private static final Set<InstKind> binarySet = Set.of(
IAdd, ISub, IMul, IDiv, IMod,
IAdd, ISub, IMul, IDiv, IMod,
FAdd, FSub, FMul, FDiv);
private static final Set<InstKind> cmpSet = Set.of(
ICmpEq, ICmpNe, ICmpGt, ICmpGe, ICmpLt, ICmpLe,
Expand Down
6 changes: 3 additions & 3 deletions src/top/origami404/ssyc/ir/inst/Instruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import top.origami404.ssyc.utils.INode;
import top.origami404.ssyc.utils.INodeOwner;

public abstract class Instruction extends User
implements INodeOwner<Instruction, BasicBlock>
{
public abstract class Instruction extends User
implements INodeOwner<Instruction, BasicBlock>
{
Instruction(InstKind kind, IRType type) {
super(type);
this.kind = kind;
Expand Down
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/ir/inst/LoadInst.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class LoadInst extends Instruction {
public LoadInst(Value ptr) {
super(
InstKind.Load,
InstKind.Load,
Optional.ofNullable((PointerIRTy) ptr.getType())
.map(PointerIRTy::getBaseType)
.orElseThrow(() -> new RuntimeException("Argument of load instrution must be a pointer"))
Expand Down
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/ir/inst/UnaryOpInst.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public UnaryOpInst(InstKind opKind, Value arg) {
this.arg = arg;
super.addOperandCO(arg);


final var resultTyKind = arg.getType().getKind();
assert resultTyKind.isInt() && resultTyKind.isFloat()
: "BinOpInst require type INT or FLOAT";
Expand Down
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/ir/type/ArrayIRTy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class ArrayIRTy implements IRType {
public IRTyKind getKind() {
return IRTyKind.Array;
}

ArrayIRTy(int elementNum, IRType elementType) {
this.elementNum = elementNum;
this.elementType = elementType;
Expand Down
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/ir/type/FunctionIRTy.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public List<IRType> getParamTypes() {
public IRType getParamType(int idx) {
return paramTypes.get(idx);
}

private IRType returnType;
private List<IRType> paramTypes;
}
2 changes: 1 addition & 1 deletion src/top/origami404/ssyc/ir/type/IRTyKind.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package top.origami404.ssyc.ir.type;

public enum IRTyKind {
Void, Bool, Int, Float,
Void, Bool, Int, Float,
Pointer, Array, Function,
BBlock, Parameter

Expand Down
6 changes: 3 additions & 3 deletions src/top/origami404/ssyc/ir/type/IRType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public interface IRType {
*/
public int getSize();

public static final SimpleIRTy IntTy = new SimpleIRTy(IRTyKind.Int, 4);
public static final SimpleIRTy IntTy = new SimpleIRTy(IRTyKind.Int, 4);
public static final SimpleIRTy FloatTy = new SimpleIRTy(IRTyKind.Float, 4);
public static final SimpleIRTy VoidTy = new SimpleIRTy(IRTyKind.Void);
public static final SimpleIRTy BoolTy = new SimpleIRTy(IRTyKind.Bool, 1);
public static final SimpleIRTy VoidTy = new SimpleIRTy(IRTyKind.Void);
public static final SimpleIRTy BoolTy = new SimpleIRTy(IRTyKind.Bool, 1);
public static final SimpleIRTy BBlockTy = new SimpleIRTy(IRTyKind.BBlock);
public static final SimpleIRTy ArgTy = new SimpleIRTy(IRTyKind.Parameter);

Expand Down
Loading

0 comments on commit d054289

Please sign in to comment.