From 19a8a755a0b87dc40de59e8f322c6377afd758bd Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sat, 7 Nov 2015 11:54:21 +0100 Subject: [PATCH 01/46] Uncomment part specific to assignment number four. Add genInstr methods with three and two params --- no/uio/ifi/pascal2100/main/CodeFile.java | 8 ++++++++ no/uio/ifi/pascal2100/main/Main.java | 13 ++++++------- no/uio/ifi/pascal2100/parser/PascalSyntax.java | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/no/uio/ifi/pascal2100/main/CodeFile.java b/no/uio/ifi/pascal2100/main/CodeFile.java index c3d185a..66c3e2b 100644 --- a/no/uio/ifi/pascal2100/main/CodeFile.java +++ b/no/uio/ifi/pascal2100/main/CodeFile.java @@ -69,6 +69,14 @@ public void genInstr(String lab, String instr, String arg, String comment) { code.println(); } + public void genInstr(String instr, String arg, String comment) { + this.genInstr("", instr, arg, comment); + } + + public void genInstr(String instr, String arg) { + this.genInstr(instr, arg, ""); + } + public void genString(String name, String s, String comment) { genDirective(".data", ""); printLabel(name, false); diff --git a/no/uio/ifi/pascal2100/main/Main.java b/no/uio/ifi/pascal2100/main/Main.java index b10b05d..b44d6a6 100644 --- a/no/uio/ifi/pascal2100/main/Main.java +++ b/no/uio/ifi/pascal2100/main/Main.java @@ -115,16 +115,15 @@ private static void doRunRealCompiler(Scanner s) { library = new Library(); prog.check(library, library); - /* Del 4 - * System.out.print(" generating code..."); - * CodeFile code = new CodeFile(baseFileName+".s"); - * library.genCode(code); prog.genCode(code); - * code.finish(); - */ + System.out.print(" generating code..."); + CodeFile code = new CodeFile(baseFileName+".s"); + library.genCode(code); + prog.genCode(code); + code.finish(); System.out.println("OK"); - // Del 4: assembleCode(); + assembleCode(); } private static void assembleCode() { diff --git a/no/uio/ifi/pascal2100/parser/PascalSyntax.java b/no/uio/ifi/pascal2100/parser/PascalSyntax.java index 967e41d..b87aaf9 100644 --- a/no/uio/ifi/pascal2100/parser/PascalSyntax.java +++ b/no/uio/ifi/pascal2100/parser/PascalSyntax.java @@ -23,7 +23,7 @@ String getSourceLocation() { abstract void check(Block curScope, Library lib); - //Del 4: abstract void genCode(CodeFile f); + abstract void genCode(CodeFile f); abstract public String identify(); abstract void prettyPrint(); From 5fdb4a427332fee6ff9daf8a7b8d01da528e524f Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sat, 7 Nov 2015 12:17:22 +0100 Subject: [PATCH 02/46] Add code generation for while, empty statm and number literal --- no/uio/ifi/pascal2100/parser/EmptyStatm.java | 4 ++++ no/uio/ifi/pascal2100/parser/NumberLiteral.java | 6 ++++++ no/uio/ifi/pascal2100/parser/WhileStatm.java | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/EmptyStatm.java b/no/uio/ifi/pascal2100/parser/EmptyStatm.java index 6913076..f7b0605 100644 --- a/no/uio/ifi/pascal2100/parser/EmptyStatm.java +++ b/no/uio/ifi/pascal2100/parser/EmptyStatm.java @@ -1,6 +1,7 @@ package no.uio.ifi.pascal2100.parser; import static no.uio.ifi.pascal2100.scanner.TokenKind.semicolonToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -33,4 +34,7 @@ public void check(Block curScope, Library lib) { } void prettyPrint() { Main.log.prettyPrint(";"); } + + @Override + void genCode(CodeFile f) { } } diff --git a/no/uio/ifi/pascal2100/parser/NumberLiteral.java b/no/uio/ifi/pascal2100/parser/NumberLiteral.java index 8575aaf..ec2de7f 100644 --- a/no/uio/ifi/pascal2100/parser/NumberLiteral.java +++ b/no/uio/ifi/pascal2100/parser/NumberLiteral.java @@ -1,6 +1,7 @@ package no.uio.ifi.pascal2100.parser; import static no.uio.ifi.pascal2100.scanner.TokenKind.intValToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -38,4 +39,9 @@ public void check(Block curScope, Library lib) { } void prettyPrint() { Main.log.prettyPrint(Integer.toString(val)); } + + @Override + void genCode(CodeFile f) { + f.genInstr("movl", "$" + this.val + ",%eax"); + } } diff --git a/no/uio/ifi/pascal2100/parser/WhileStatm.java b/no/uio/ifi/pascal2100/parser/WhileStatm.java index d203c42..35c1a35 100644 --- a/no/uio/ifi/pascal2100/parser/WhileStatm.java +++ b/no/uio/ifi/pascal2100/parser/WhileStatm.java @@ -4,6 +4,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.doToken; import no.uio.ifi.pascal2100.scanner.Scanner; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; public class WhileStatm extends Statement { @@ -56,4 +57,17 @@ void prettyPrint() { } } + @Override + void genCode(CodeFile f) { + String testLabel = f.getLocalLabel(), + endLabel = f.getLocalLabel(); + + f.genInstr(testLabel, "", "", "Start while-statement"); + expr.genCode(f); + f.genInstr("cmpl", "$0,%eax"); + f.genInstr("je", endLabel); + body.genCode(f); + f.genInstr("jmp", testLabel); + f.genInstr(endLabel, "", "", "End while-statement"); + } } From 76e4aa9ce39da5fc915ef9094282dccb80048bd5 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 11 Nov 2015 21:11:47 +0100 Subject: [PATCH 03/46] Add code generation for if statements --- no/uio/ifi/pascal2100/parser/IfStatm.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/IfStatm.java b/no/uio/ifi/pascal2100/parser/IfStatm.java index ca1edf1..f4ef827 100644 --- a/no/uio/ifi/pascal2100/parser/IfStatm.java +++ b/no/uio/ifi/pascal2100/parser/IfStatm.java @@ -4,6 +4,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.thenToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.elseToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -70,4 +71,26 @@ void prettyPrint() { Main.log.prettyOutdent(); } + + void genCode(CodeFile f) { + String elseLabel = f.getLocalLabel(), + endLabel = f.getLocalLabel(); + + f.genInstr("", "", "Start if-statement"); + expr.genCode(f); + f.genInstr("cmpl", "$0,%eax"); + + if (elseStatm == null) { + f.genInstr("je", endLabel); + thenStatm.genCode(f); + } else { + f.genInstr("je", elseLabel); + thenStatm.genCode(f); + f.genInstr("jmp", endLabel); + f.genInstr(elseLabel, "", "", ""); + elseStatm.genCode(f); + } + + f.genInstr(endLabel, "", "", "End if-statement"); + } } From 68382d6e102ad4b69a9790b90c36b4db7b9dd903 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Thu, 12 Nov 2015 09:34:08 +0100 Subject: [PATCH 04/46] Add code gen for program --- no/uio/ifi/pascal2100/main/CodeFile.java | 8 +++---- no/uio/ifi/pascal2100/parser/IfStatm.java | 10 ++++---- .../ifi/pascal2100/parser/NumberLiteral.java | 2 +- no/uio/ifi/pascal2100/parser/Program.java | 24 +++++++++++++++++++ no/uio/ifi/pascal2100/parser/WhileStatm.java | 6 ++--- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/no/uio/ifi/pascal2100/main/CodeFile.java b/no/uio/ifi/pascal2100/main/CodeFile.java index 66c3e2b..d711450 100644 --- a/no/uio/ifi/pascal2100/main/CodeFile.java +++ b/no/uio/ifi/pascal2100/main/CodeFile.java @@ -69,12 +69,12 @@ public void genInstr(String lab, String instr, String arg, String comment) { code.println(); } - public void genInstr(String instr, String arg, String comment) { - this.genInstr("", instr, arg, comment); + public void genInstr(String lab, String instr, String arg) { + this.genInstr(lab, instr, arg, ""); } - public void genInstr(String instr, String arg) { - this.genInstr(instr, arg, ""); + public void genInstr(String lab, String instr) { + this.genInstr(lab, instr, "", ""); } public void genString(String name, String s, String comment) { diff --git a/no/uio/ifi/pascal2100/parser/IfStatm.java b/no/uio/ifi/pascal2100/parser/IfStatm.java index f4ef827..7168163 100644 --- a/no/uio/ifi/pascal2100/parser/IfStatm.java +++ b/no/uio/ifi/pascal2100/parser/IfStatm.java @@ -76,17 +76,17 @@ void genCode(CodeFile f) { String elseLabel = f.getLocalLabel(), endLabel = f.getLocalLabel(); - f.genInstr("", "", "Start if-statement"); + f.genInstr("", "", "", "Start if-statement"); expr.genCode(f); - f.genInstr("cmpl", "$0,%eax"); + f.genInstr("", "cmpl", "$0,%eax", ""); if (elseStatm == null) { - f.genInstr("je", endLabel); + f.genInstr("", "je", endLabel, ""); thenStatm.genCode(f); } else { - f.genInstr("je", elseLabel); + f.genInstr("", "je", elseLabel, ""); thenStatm.genCode(f); - f.genInstr("jmp", endLabel); + f.genInstr("", "jmp", endLabel, ""); f.genInstr(elseLabel, "", "", ""); elseStatm.genCode(f); } diff --git a/no/uio/ifi/pascal2100/parser/NumberLiteral.java b/no/uio/ifi/pascal2100/parser/NumberLiteral.java index ec2de7f..c6b7f9c 100644 --- a/no/uio/ifi/pascal2100/parser/NumberLiteral.java +++ b/no/uio/ifi/pascal2100/parser/NumberLiteral.java @@ -42,6 +42,6 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - f.genInstr("movl", "$" + this.val + ",%eax"); + f.genInstr("", "movl", "$" + this.val + ",%eax", ""); } } diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index 445b162..851a364 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.*; import static no.uio.ifi.pascal2100.scanner.TokenKind.*; @@ -52,4 +53,27 @@ public void prettyPrint() { progBlock.prettyPrint(); Main.log.prettyPrintLn("."); } + + @Override + void genCode(CodeFile f) { + int level = 1; + String progLabel = "func$" + name + "_" + level; + int progBlockSize = progBlock.decls.size() * 4; + + f.genInstr("", ".extern", "write_char"); + f.genInstr("", ".extern", "write_int"); + f.genInstr("", ".extern", "write_string"); + f.genInstr("", ".globl", "_main"); + f.genInstr("", ".globl", "main"); + + f.genInstr("_main", "", ""); + f.genInstr("main", "call", progLabel, "Start program"); + f.genInstr("", "movl", "$0,%eax", "Set status 0"); + f.genInstr("", "ret", "", "terminate the program"); + + f.genInstr(progLabel, "enter", "$" + (32 + progBlockSize) + ",$" + level, "Start of " + name); + progBlock.genCode(f); + f.genInstr("", "leave"); + f.genInstr("", "ret"); + } } diff --git a/no/uio/ifi/pascal2100/parser/WhileStatm.java b/no/uio/ifi/pascal2100/parser/WhileStatm.java index 35c1a35..3a97294 100644 --- a/no/uio/ifi/pascal2100/parser/WhileStatm.java +++ b/no/uio/ifi/pascal2100/parser/WhileStatm.java @@ -64,10 +64,10 @@ void genCode(CodeFile f) { f.genInstr(testLabel, "", "", "Start while-statement"); expr.genCode(f); - f.genInstr("cmpl", "$0,%eax"); - f.genInstr("je", endLabel); + f.genInstr("", "cmpl", "$0,%eax", ""); + f.genInstr("", "je", endLabel, ""); body.genCode(f); - f.genInstr("jmp", testLabel); + f.genInstr("", "jmp", testLabel, ""); f.genInstr(endLabel, "", "", "End while-statement"); } } From 3323cb53d7135f9884943eba0a38aad46fdc2f53 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sun, 22 Nov 2015 20:57:01 +0100 Subject: [PATCH 05/46] More code gen. Harder than expected :x --- no/uio/ifi/pascal2100/parser/ArrayType.java | 26 ++++++++++++++ no/uio/ifi/pascal2100/parser/AssignStatm.java | 34 +++++++++++++++++++ no/uio/ifi/pascal2100/parser/Block.java | 26 ++++++++++++++ .../ifi/pascal2100/parser/CompoundStatm.java | 6 ++++ no/uio/ifi/pascal2100/parser/ConstDecl.java | 5 +++ .../ifi/pascal2100/parser/ConstDeclPart.java | 10 ++++++ no/uio/ifi/pascal2100/parser/EnumLiteral.java | 5 +++ no/uio/ifi/pascal2100/parser/EnumType.java | 11 ++++++ no/uio/ifi/pascal2100/parser/ParamDecl.java | 5 +++ no/uio/ifi/pascal2100/parser/PascalDecl.java | 4 ++- .../ifi/pascal2100/parser/PascalSyntax.java | 4 ++- .../ifi/pascal2100/parser/ProcCallStatm.java | 19 +++++++++++ no/uio/ifi/pascal2100/parser/ProcDecl.java | 5 +++ no/uio/ifi/pascal2100/parser/Program.java | 21 ++++++++---- no/uio/ifi/pascal2100/parser/RangeType.java | 4 +++ no/uio/ifi/pascal2100/parser/StatmList.java | 8 +++++ no/uio/ifi/pascal2100/parser/Type.java | 2 ++ no/uio/ifi/pascal2100/parser/TypeDecl.java | 6 +++- .../ifi/pascal2100/parser/TypeDeclPart.java | 10 ++++++ no/uio/ifi/pascal2100/parser/TypeName.java | 10 +++++- no/uio/ifi/pascal2100/parser/VarDecl.java | 5 +++ no/uio/ifi/pascal2100/parser/VarDeclPart.java | 10 ++++++ no/uio/ifi/pascal2100/parser/Variable.java | 4 +-- 23 files changed, 228 insertions(+), 12 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/ArrayType.java b/no/uio/ifi/pascal2100/parser/ArrayType.java index 961fffd..0c9f3f6 100644 --- a/no/uio/ifi/pascal2100/parser/ArrayType.java +++ b/no/uio/ifi/pascal2100/parser/ArrayType.java @@ -3,6 +3,7 @@ import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; +import no.uio.ifi.pascal2100.parser.NumberLiteral; import static no.uio.ifi.pascal2100.scanner.TokenKind.arrayToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.leftBracketToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.rightBracketToken; @@ -42,6 +43,26 @@ public static ArrayType parse(Scanner s) { return at; } + private RangeType getRange() { + Type curType = type; + + while (!(curType instanceof RangeType)) { + curType = ((TypeName) curType).decl.type; + } + + return (RangeType) curType; + } + + public int getLow() { + NumberLiteral low = (NumberLiteral) getRange().from; + return low.val; + } + + public int getHigh() { + NumberLiteral high = (NumberLiteral) getRange().to; + return high.val; + } + @Override public void check(Block curScope, Library lib) { type.check(curScope, lib); @@ -54,4 +75,9 @@ void prettyPrint() { Main.log.prettyPrint("] of "); ofType.prettyPrint(); } + + @Override + public int getSize() { + return (getHigh() - getLow()) * type.getSize(); + } } diff --git a/no/uio/ifi/pascal2100/parser/AssignStatm.java b/no/uio/ifi/pascal2100/parser/AssignStatm.java index 9adf13a..88a2f9f 100644 --- a/no/uio/ifi/pascal2100/parser/AssignStatm.java +++ b/no/uio/ifi/pascal2100/parser/AssignStatm.java @@ -2,6 +2,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.assignToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -43,4 +44,37 @@ void prettyPrint() { Main.log.prettyPrint(" := "); expr.prettyPrint(); } + + public void genCode(CodeFile f) { + expr.genCode(f); + + String varLocation = "-" + (4 * var.nameDecl.declLevel) + "(%ebp),%edx"; + + // Assignment to array value + if (var.nameDecl.type instanceof ArrayType) { + ArrayType at = (ArrayType) var.nameDecl.type; + + var.expr.genCode(f); + f.genInstr("", "pushl", "%eax"); + expr.genCode(f); + f.genInstr("", "subl", at.getLow() + "%eax"); + f.genInstr("", "movl", varLocation); + + // get array offset value and lower end of range + f.genInstr("", "leal", "-" + var.nameDecl.declOffset + "(%edx),%edx"); + f.genInstr("", "popl", "%ecx"); + f.genInstr("", "movl", "%ecx,(%edx,%eax,4)"); + return; + } + + // Assign function return value + if (var.nameDecl instanceof FuncDecl) { + f.genInstr("", "movl", "%eax,-32(%ebp)"); + return; + } + + // Simple variable assignment + f.genInstr("", "movl", varLocation); + f.genInstr("", "movl", "%eax,-0(%edx)"); + } } diff --git a/no/uio/ifi/pascal2100/parser/Block.java b/no/uio/ifi/pascal2100/parser/Block.java index ce1146f..be859f9 100644 --- a/no/uio/ifi/pascal2100/parser/Block.java +++ b/no/uio/ifi/pascal2100/parser/Block.java @@ -11,6 +11,7 @@ import java.util.HashMap; import java.util.LinkedList; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -22,6 +23,7 @@ public class Block extends PascalSyntax { public VarDeclPart varDeclPart = null; public LinkedList procDeclList = new LinkedList(); + int blockLevel = 0; Block outerScope = null; HashMap decls = new HashMap(); @@ -98,6 +100,9 @@ public void addDecl(String id, PascalDecl decl) { decl.error(id + " declared twice in same block"); } + decl.declLevel = blockLevel; + decl.declOffset = decls.size(); + decls.put(id.toLowerCase(), decl); } @@ -109,6 +114,7 @@ public void addDecl(String id, PascalDecl decl) { * @param lib */ public void check(Block outerScope, Block curScope, Library lib) { + curScope.blockLevel = outerScope.blockLevel + 1; curScope.outerScope = outerScope; check(curScope, lib); @@ -185,4 +191,24 @@ public void prettyPrint() { Main.log.prettyOutdent(); Main.log.prettyPrint("end"); } + + /** + * Get number of bytes that should be allocated for this block + * + * @return integer Number of bytes + */ + int getSize() { + return ( + (constDeclPart != null ? constDeclPart.getSize() : 0) + + (typeDeclPart != null ? typeDeclPart.getSize() : 0) + + (varDeclPart != null ? varDeclPart.getSize() : 0) + ); + } + + @Override + public void genCode(CodeFile f) { + if (stmtList != null) { + stmtList.genCode(f); + } + } } diff --git a/no/uio/ifi/pascal2100/parser/CompoundStatm.java b/no/uio/ifi/pascal2100/parser/CompoundStatm.java index edfc0b0..cf1d82c 100644 --- a/no/uio/ifi/pascal2100/parser/CompoundStatm.java +++ b/no/uio/ifi/pascal2100/parser/CompoundStatm.java @@ -3,6 +3,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.beginToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.endToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -47,4 +48,9 @@ void prettyPrint() { Main.log.prettyOutdent(); Main.log.prettyPrint("end"); } + + @Override + void genCode(CodeFile f) { + stmtList.genCode(f); + } } diff --git a/no/uio/ifi/pascal2100/parser/ConstDecl.java b/no/uio/ifi/pascal2100/parser/ConstDecl.java index a5b7f2d..93b56eb 100644 --- a/no/uio/ifi/pascal2100/parser/ConstDecl.java +++ b/no/uio/ifi/pascal2100/parser/ConstDecl.java @@ -45,4 +45,9 @@ public void prettyPrint() { constant.prettyPrint(); Main.log.prettyPrint(";"); } + + @Override + public int getSize() { + return 0; + } } diff --git a/no/uio/ifi/pascal2100/parser/ConstDeclPart.java b/no/uio/ifi/pascal2100/parser/ConstDeclPart.java index 415b24d..22398e9 100644 --- a/no/uio/ifi/pascal2100/parser/ConstDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/ConstDeclPart.java @@ -56,4 +56,14 @@ public void prettyPrint() { cd.prettyPrint(); } } + + public int getSize() { + int size = 0; + + for (ConstDecl cd : decls) { + size += cd.getSize(); + } + + return size; + } } diff --git a/no/uio/ifi/pascal2100/parser/EnumLiteral.java b/no/uio/ifi/pascal2100/parser/EnumLiteral.java index 8dd997a..c0f4ce7 100644 --- a/no/uio/ifi/pascal2100/parser/EnumLiteral.java +++ b/no/uio/ifi/pascal2100/parser/EnumLiteral.java @@ -34,4 +34,9 @@ public void check(Block curScope, Library lib) { } public void prettyPrint() { Main.log.prettyPrint(name); } + + @Override + public int getSize() { + return 4; + } } diff --git a/no/uio/ifi/pascal2100/parser/EnumType.java b/no/uio/ifi/pascal2100/parser/EnumType.java index 1dfb1e7..727998c 100644 --- a/no/uio/ifi/pascal2100/parser/EnumType.java +++ b/no/uio/ifi/pascal2100/parser/EnumType.java @@ -58,4 +58,15 @@ void prettyPrint() { Main.log.prettyPrint(")"); } + + @Override + public int getSize() { + int size = 0; + + for (EnumLiteral el : literals) { + size += el.getSize(); + } + + return size; + } } diff --git a/no/uio/ifi/pascal2100/parser/ParamDecl.java b/no/uio/ifi/pascal2100/parser/ParamDecl.java index 1bb0666..b01fc3b 100644 --- a/no/uio/ifi/pascal2100/parser/ParamDecl.java +++ b/no/uio/ifi/pascal2100/parser/ParamDecl.java @@ -43,4 +43,9 @@ public void prettyPrint() { Main.log.prettyPrint(name + ": "); typeName.prettyPrint(); } + + @Override + public int getSize() { + return 0; + } } diff --git a/no/uio/ifi/pascal2100/parser/PascalDecl.java b/no/uio/ifi/pascal2100/parser/PascalDecl.java index 1fd064c..9bbce65 100644 --- a/no/uio/ifi/pascal2100/parser/PascalDecl.java +++ b/no/uio/ifi/pascal2100/parser/PascalDecl.java @@ -3,12 +3,14 @@ public abstract class PascalDecl extends PascalSyntax { String name, progProcFuncName; int declLevel = 0, declOffset = 0; - //Del 3: Type type = null; + Type type = null; PascalDecl(String id, int lNum) { super(lNum); name = id; } + + abstract public int getSize(); /** * checkWhetherAssignable: Utility method to check whether this PascalDecl is diff --git a/no/uio/ifi/pascal2100/parser/PascalSyntax.java b/no/uio/ifi/pascal2100/parser/PascalSyntax.java index b87aaf9..9152be3 100644 --- a/no/uio/ifi/pascal2100/parser/PascalSyntax.java +++ b/no/uio/ifi/pascal2100/parser/PascalSyntax.java @@ -23,7 +23,7 @@ String getSourceLocation() { abstract void check(Block curScope, Library lib); - abstract void genCode(CodeFile f); + // abstract void genCode(CodeFile f); abstract public String identify(); abstract void prettyPrint(); @@ -38,4 +38,6 @@ static void enterParser(String nonTerm) { static void leaveParser(String nonTerm) { Main.log.leaveParser(nonTerm); } + + void genCode(CodeFile f) { } } diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index 9281804..1c304a4 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -7,6 +7,7 @@ import java.util.LinkedList; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -80,4 +81,22 @@ void prettyPrint() { Main.log.prettyPrint(")"); } } + + private void genWriteCode(CodeFile f) { + f.genString("", "", "write call here"); + } + + public void genCode(CodeFile f) { + System.out.println("fooo " + name); + + // Check this is a call on the library function write + if (name.toLowerCase() == "write") { + genWriteCode(f); + return; + } + + for (int i = exprs.size() -1; i > 0; i--) { + + } + } } diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index e623636..b59601c 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -74,4 +74,9 @@ public void prettyPrint() { block.prettyPrint(); Main.log.prettyPrintLn("; {" + name + "}"); } + + @Override + public int getSize() { + return 0; + } } diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index 851a364..ed31f0f 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -55,10 +55,9 @@ public void prettyPrint() { } @Override - void genCode(CodeFile f) { - int level = 1; - String progLabel = "func$" + name + "_" + level; - int progBlockSize = progBlock.decls.size() * 4; + public void genCode(CodeFile f) { + String progLabel = f.getLabel("prog$" + name); + int progBlockSize = (32 + progBlock.getSize()); f.genInstr("", ".extern", "write_char"); f.genInstr("", ".extern", "write_int"); @@ -71,9 +70,19 @@ void genCode(CodeFile f) { f.genInstr("", "movl", "$0,%eax", "Set status 0"); f.genInstr("", "ret", "", "terminate the program"); - f.genInstr(progLabel, "enter", "$" + (32 + progBlockSize) + ",$" + level, "Start of " + name); + for (PascalDecl pd : progBlock.decls.values()) { + pd.genCode(f); + } + + f.genInstr(progLabel, ""); + f.genInstr("", "enter", "$" + progBlockSize + ",$" + progBlock.blockLevel, "Start of " + name); progBlock.genCode(f); - f.genInstr("", "leave"); + f.genInstr("", "leave", "", "End of " + name); f.genInstr("", "ret"); } + + @Override + public int getSize() { + return 0; + } } diff --git a/no/uio/ifi/pascal2100/parser/RangeType.java b/no/uio/ifi/pascal2100/parser/RangeType.java index 335f2e2..c3683be 100644 --- a/no/uio/ifi/pascal2100/parser/RangeType.java +++ b/no/uio/ifi/pascal2100/parser/RangeType.java @@ -44,4 +44,8 @@ void prettyPrint() { Main.log.prettyPrint(" .. "); to.prettyPrint(); } + + public int getSize() { + return 4; + } } diff --git a/no/uio/ifi/pascal2100/parser/StatmList.java b/no/uio/ifi/pascal2100/parser/StatmList.java index 6ba2029..364ca79 100644 --- a/no/uio/ifi/pascal2100/parser/StatmList.java +++ b/no/uio/ifi/pascal2100/parser/StatmList.java @@ -5,6 +5,7 @@ import java.util.LinkedList; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -64,4 +65,11 @@ void prettyPrint() { } Main.log.prettyPrintLn(); } + + @Override + void genCode(CodeFile f) { + for (Statement s : statements) { + s.genCode(f); + } + } } diff --git a/no/uio/ifi/pascal2100/parser/Type.java b/no/uio/ifi/pascal2100/parser/Type.java index 2883fa0..8951aac 100644 --- a/no/uio/ifi/pascal2100/parser/Type.java +++ b/no/uio/ifi/pascal2100/parser/Type.java @@ -35,4 +35,6 @@ public static Type parse(Scanner s) { return t; } + + abstract public int getSize(); } diff --git a/no/uio/ifi/pascal2100/parser/TypeDecl.java b/no/uio/ifi/pascal2100/parser/TypeDecl.java index 98e5f7e..61725ed 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDecl.java +++ b/no/uio/ifi/pascal2100/parser/TypeDecl.java @@ -26,7 +26,7 @@ public static TypeDecl parse(Scanner s) { td.name = TypeName.parse(s); s.skip(equalToken); - td.type= Type.parse(s); + td.type = Type.parse(s); s.skip(semicolonToken); leaveParser("type decl"); @@ -49,4 +49,8 @@ public void prettyPrint() { type.prettyPrint(); Main.log.prettyPrint(";"); } + + public int getSize() { + return type.getSize(); + } } diff --git a/no/uio/ifi/pascal2100/parser/TypeDeclPart.java b/no/uio/ifi/pascal2100/parser/TypeDeclPart.java index 3379595..ce82bd8 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/TypeDeclPart.java @@ -56,4 +56,14 @@ public void prettyPrint() { td.prettyPrint(); } } + + public int getSize() { + int size = 0; + + for (TypeDecl td : decls) { + size += td.getSize(); + } + + return size; + } } diff --git a/no/uio/ifi/pascal2100/parser/TypeName.java b/no/uio/ifi/pascal2100/parser/TypeName.java index f159023..3f48af3 100644 --- a/no/uio/ifi/pascal2100/parser/TypeName.java +++ b/no/uio/ifi/pascal2100/parser/TypeName.java @@ -7,6 +7,7 @@ public class TypeName extends Type { public String name; + public PascalDecl decl; TypeName(String id, int lNum) { super(lNum); @@ -32,9 +33,16 @@ public static TypeName parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { } + public void check(Block curScope, Library lib) { + decl = curScope.findDecl(name, this); + } void prettyPrint() { Main.log.prettyPrint(name); } + + @Override + public int getSize() { + return 0; + } } diff --git a/no/uio/ifi/pascal2100/parser/VarDecl.java b/no/uio/ifi/pascal2100/parser/VarDecl.java index 4ddcd2f..523e9a3 100644 --- a/no/uio/ifi/pascal2100/parser/VarDecl.java +++ b/no/uio/ifi/pascal2100/parser/VarDecl.java @@ -49,4 +49,9 @@ public void prettyPrint() { type.prettyPrint(); Main.log.prettyPrint(";"); } + + @Override + public int getSize() { + return 0; + } } diff --git a/no/uio/ifi/pascal2100/parser/VarDeclPart.java b/no/uio/ifi/pascal2100/parser/VarDeclPart.java index bb22c32..c078a7e 100644 --- a/no/uio/ifi/pascal2100/parser/VarDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/VarDeclPart.java @@ -56,4 +56,14 @@ public void prettyPrint() { vd.prettyPrint(); } } + + public int getSize() { + int size = 0; + + for (VarDecl vd : decls) { + size += vd.getSize(); + } + + return size; + } } diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index a64579e..d9c5694 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -10,7 +10,7 @@ public class Variable extends Factor { public String name; public Expression expr = null; - + public PascalDecl nameDecl = null; Variable(String id, int lNum) { super(lNum); @@ -46,7 +46,7 @@ public static Variable parse(Scanner s) { @Override public void check(Block curScope, Library lib) { - curScope.findDecl(name, this); + nameDecl = curScope.findDecl(name, this); if (expr != null) { expr.check(curScope, lib); From 62d9d66e2eff313cfc8a6897824ab59f7f3d9655 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 24 Nov 2015 19:25:49 +0100 Subject: [PATCH 06/46] Add libpas2100.c file with write functions --- libpas2100.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 libpas2100.c diff --git a/libpas2100.c b/libpas2100.c new file mode 100644 index 0000000..cbc07fc --- /dev/null +++ b/libpas2100.c @@ -0,0 +1,16 @@ +#include + +void write_char (int c) +{ + printf("%c", c); +} + +void write_int (int n) +{ + printf("%d", n); +} + +void write_string (char *s) +{ + printf("%s", s); +} From 7213c72f45b108f99e1d51767d2e0dcbedc56b79 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 24 Nov 2015 22:15:59 +0100 Subject: [PATCH 07/46] Implemented working generation for a limited set of features. The following file can now be sucsessfully compiled: program Assign; var v : Integer; w : Integer; begin w := v end. --- no/uio/ifi/pascal2100/parser/ArrayType.java | 5 ++-- no/uio/ifi/pascal2100/parser/AssignStatm.java | 23 ++-------------- no/uio/ifi/pascal2100/parser/Block.java | 26 ++++++++----------- no/uio/ifi/pascal2100/parser/ConstDecl.java | 5 ---- .../ifi/pascal2100/parser/ConstDeclPart.java | 10 ------- no/uio/ifi/pascal2100/parser/EnumLiteral.java | 5 ---- no/uio/ifi/pascal2100/parser/EnumType.java | 11 -------- no/uio/ifi/pascal2100/parser/Expression.java | 12 +++++++++ no/uio/ifi/pascal2100/parser/ParamDecl.java | 5 ---- no/uio/ifi/pascal2100/parser/PascalDecl.java | 2 -- .../ifi/pascal2100/parser/PrefixOperator.java | 11 ++++++++ .../ifi/pascal2100/parser/ProcCallStatm.java | 2 +- no/uio/ifi/pascal2100/parser/ProcDecl.java | 5 ---- no/uio/ifi/pascal2100/parser/Program.java | 7 +---- no/uio/ifi/pascal2100/parser/RangeType.java | 4 --- no/uio/ifi/pascal2100/parser/SimpleExpr.java | 18 +++++++++++++ no/uio/ifi/pascal2100/parser/Term.java | 14 ++++++++++ no/uio/ifi/pascal2100/parser/Type.java | 2 -- no/uio/ifi/pascal2100/parser/TypeDecl.java | 4 --- .../ifi/pascal2100/parser/TypeDeclPart.java | 10 ------- no/uio/ifi/pascal2100/parser/TypeName.java | 5 ---- no/uio/ifi/pascal2100/parser/VarDecl.java | 5 ---- no/uio/ifi/pascal2100/parser/VarDeclPart.java | 10 ------- no/uio/ifi/pascal2100/parser/Variable.java | 7 +++++ 24 files changed, 80 insertions(+), 128 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/ArrayType.java b/no/uio/ifi/pascal2100/parser/ArrayType.java index 0c9f3f6..5e0edf7 100644 --- a/no/uio/ifi/pascal2100/parser/ArrayType.java +++ b/no/uio/ifi/pascal2100/parser/ArrayType.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -77,7 +78,7 @@ void prettyPrint() { } @Override - public int getSize() { - return (getHigh() - getLow()) * type.getSize(); + void genCode(CodeFile f) { + // TODO Auto-generated method stub } } diff --git a/no/uio/ifi/pascal2100/parser/AssignStatm.java b/no/uio/ifi/pascal2100/parser/AssignStatm.java index 88a2f9f..fbcd108 100644 --- a/no/uio/ifi/pascal2100/parser/AssignStatm.java +++ b/no/uio/ifi/pascal2100/parser/AssignStatm.java @@ -48,25 +48,6 @@ void prettyPrint() { public void genCode(CodeFile f) { expr.genCode(f); - String varLocation = "-" + (4 * var.nameDecl.declLevel) + "(%ebp),%edx"; - - // Assignment to array value - if (var.nameDecl.type instanceof ArrayType) { - ArrayType at = (ArrayType) var.nameDecl.type; - - var.expr.genCode(f); - f.genInstr("", "pushl", "%eax"); - expr.genCode(f); - f.genInstr("", "subl", at.getLow() + "%eax"); - f.genInstr("", "movl", varLocation); - - // get array offset value and lower end of range - f.genInstr("", "leal", "-" + var.nameDecl.declOffset + "(%edx),%edx"); - f.genInstr("", "popl", "%ecx"); - f.genInstr("", "movl", "%ecx,(%edx,%eax,4)"); - return; - } - // Assign function return value if (var.nameDecl instanceof FuncDecl) { f.genInstr("", "movl", "%eax,-32(%ebp)"); @@ -74,7 +55,7 @@ public void genCode(CodeFile f) { } // Simple variable assignment - f.genInstr("", "movl", varLocation); - f.genInstr("", "movl", "%eax,-0(%edx)"); + f.genInstr("", "movl", "-" + (4 * var.nameDecl.declLevel) + "(%ebp),%edx"); + f.genInstr("", "movl", "%eax,-" + (36 + (4 * var.nameDecl.declOffset)) + "(%edx)", var.name + " :="); } } diff --git a/no/uio/ifi/pascal2100/parser/Block.java b/no/uio/ifi/pascal2100/parser/Block.java index be859f9..2393ba5 100644 --- a/no/uio/ifi/pascal2100/parser/Block.java +++ b/no/uio/ifi/pascal2100/parser/Block.java @@ -122,6 +122,15 @@ public void check(Block outerScope, Block curScope, Library lib) { @Override public void check(Block curScope, Library lib) { + // Variable declarations + if (varDeclPart != null) { + varDeclPart.check(this, lib); + + for (VarDecl vd: varDeclPart.decls) { + this.addDecl(vd.name, vd); + } + } + // Constant declarations if (constDeclPart != null) { constDeclPart.check(this, lib); @@ -140,15 +149,6 @@ public void check(Block curScope, Library lib) { } } - // Variable declarations - if (varDeclPart != null) { - varDeclPart.check(this, lib); - - for (VarDecl vd: varDeclPart.decls) { - this.addDecl(vd.name, vd); - } - } - // Procedure declarations for (ProcDecl pd : procDeclList) { pd.check(curScope, lib); @@ -193,16 +193,12 @@ public void prettyPrint() { } /** - * Get number of bytes that should be allocated for this block + * Get number of bytes that should be allocated for this block. 32 + (number-of-variables * 4) * * @return integer Number of bytes */ int getSize() { - return ( - (constDeclPart != null ? constDeclPart.getSize() : 0) + - (typeDeclPart != null ? typeDeclPart.getSize() : 0) + - (varDeclPart != null ? varDeclPart.getSize() : 0) - ); + return 32 + (varDeclPart != null ? varDeclPart.decls.size() * 4 : 0); } @Override diff --git a/no/uio/ifi/pascal2100/parser/ConstDecl.java b/no/uio/ifi/pascal2100/parser/ConstDecl.java index 93b56eb..a5b7f2d 100644 --- a/no/uio/ifi/pascal2100/parser/ConstDecl.java +++ b/no/uio/ifi/pascal2100/parser/ConstDecl.java @@ -45,9 +45,4 @@ public void prettyPrint() { constant.prettyPrint(); Main.log.prettyPrint(";"); } - - @Override - public int getSize() { - return 0; - } } diff --git a/no/uio/ifi/pascal2100/parser/ConstDeclPart.java b/no/uio/ifi/pascal2100/parser/ConstDeclPart.java index 22398e9..415b24d 100644 --- a/no/uio/ifi/pascal2100/parser/ConstDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/ConstDeclPart.java @@ -56,14 +56,4 @@ public void prettyPrint() { cd.prettyPrint(); } } - - public int getSize() { - int size = 0; - - for (ConstDecl cd : decls) { - size += cd.getSize(); - } - - return size; - } } diff --git a/no/uio/ifi/pascal2100/parser/EnumLiteral.java b/no/uio/ifi/pascal2100/parser/EnumLiteral.java index c0f4ce7..8dd997a 100644 --- a/no/uio/ifi/pascal2100/parser/EnumLiteral.java +++ b/no/uio/ifi/pascal2100/parser/EnumLiteral.java @@ -34,9 +34,4 @@ public void check(Block curScope, Library lib) { } public void prettyPrint() { Main.log.prettyPrint(name); } - - @Override - public int getSize() { - return 4; - } } diff --git a/no/uio/ifi/pascal2100/parser/EnumType.java b/no/uio/ifi/pascal2100/parser/EnumType.java index 727998c..1dfb1e7 100644 --- a/no/uio/ifi/pascal2100/parser/EnumType.java +++ b/no/uio/ifi/pascal2100/parser/EnumType.java @@ -58,15 +58,4 @@ void prettyPrint() { Main.log.prettyPrint(")"); } - - @Override - public int getSize() { - int size = 0; - - for (EnumLiteral el : literals) { - size += el.getSize(); - } - - return size; - } } diff --git a/no/uio/ifi/pascal2100/parser/Expression.java b/no/uio/ifi/pascal2100/parser/Expression.java index a7a6651..ff7973f 100644 --- a/no/uio/ifi/pascal2100/parser/Expression.java +++ b/no/uio/ifi/pascal2100/parser/Expression.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -55,4 +56,15 @@ void prettyPrint() { trailing.prettyPrint(); } } + + @Override + void genCode(CodeFile f) { + leading.genCode(f); + + if (trailing != null) { + f.genInstr("", "pushl", "%eax"); + trailing.genCode(f); + relOperator.genCode(f); + } + } } \ No newline at end of file diff --git a/no/uio/ifi/pascal2100/parser/ParamDecl.java b/no/uio/ifi/pascal2100/parser/ParamDecl.java index b01fc3b..1bb0666 100644 --- a/no/uio/ifi/pascal2100/parser/ParamDecl.java +++ b/no/uio/ifi/pascal2100/parser/ParamDecl.java @@ -43,9 +43,4 @@ public void prettyPrint() { Main.log.prettyPrint(name + ": "); typeName.prettyPrint(); } - - @Override - public int getSize() { - return 0; - } } diff --git a/no/uio/ifi/pascal2100/parser/PascalDecl.java b/no/uio/ifi/pascal2100/parser/PascalDecl.java index 9bbce65..2883e09 100644 --- a/no/uio/ifi/pascal2100/parser/PascalDecl.java +++ b/no/uio/ifi/pascal2100/parser/PascalDecl.java @@ -10,8 +10,6 @@ public abstract class PascalDecl extends PascalSyntax { name = id; } - abstract public int getSize(); - /** * checkWhetherAssignable: Utility method to check whether this PascalDecl is * assignable, i.e., may be used to the left of a :=. diff --git a/no/uio/ifi/pascal2100/parser/PrefixOperator.java b/no/uio/ifi/pascal2100/parser/PrefixOperator.java index 0cd99d0..1c6274e 100644 --- a/no/uio/ifi/pascal2100/parser/PrefixOperator.java +++ b/no/uio/ifi/pascal2100/parser/PrefixOperator.java @@ -1,8 +1,10 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; import no.uio.ifi.pascal2100.scanner.TokenKind; +import static no.uio.ifi.pascal2100.scanner.TokenKind.subtractToken; public class PrefixOperator extends Operator { public TokenKind kind; @@ -51,4 +53,13 @@ void prettyPrint() { Main.log.prettyPrint(symbol); } + + @Override + void genCode(CodeFile f) { + if (kind != subtractToken) { + return; + } + + f.genInstr("", "negl", "%eax"); + } } diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index 1c304a4..581ffde 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -83,7 +83,7 @@ void prettyPrint() { } private void genWriteCode(CodeFile f) { - f.genString("", "", "write call here"); + } public void genCode(CodeFile f) { diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index b59601c..e623636 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -74,9 +74,4 @@ public void prettyPrint() { block.prettyPrint(); Main.log.prettyPrintLn("; {" + name + "}"); } - - @Override - public int getSize() { - return 0; - } } diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index ed31f0f..f6183f4 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -57,7 +57,7 @@ public void prettyPrint() { @Override public void genCode(CodeFile f) { String progLabel = f.getLabel("prog$" + name); - int progBlockSize = (32 + progBlock.getSize()); + int progBlockSize = progBlock.getSize(); f.genInstr("", ".extern", "write_char"); f.genInstr("", ".extern", "write_int"); @@ -80,9 +80,4 @@ public void genCode(CodeFile f) { f.genInstr("", "leave", "", "End of " + name); f.genInstr("", "ret"); } - - @Override - public int getSize() { - return 0; - } } diff --git a/no/uio/ifi/pascal2100/parser/RangeType.java b/no/uio/ifi/pascal2100/parser/RangeType.java index c3683be..335f2e2 100644 --- a/no/uio/ifi/pascal2100/parser/RangeType.java +++ b/no/uio/ifi/pascal2100/parser/RangeType.java @@ -44,8 +44,4 @@ void prettyPrint() { Main.log.prettyPrint(" .. "); to.prettyPrint(); } - - public int getSize() { - return 4; - } } diff --git a/no/uio/ifi/pascal2100/parser/SimpleExpr.java b/no/uio/ifi/pascal2100/parser/SimpleExpr.java index e78f4bc..164777c 100644 --- a/no/uio/ifi/pascal2100/parser/SimpleExpr.java +++ b/no/uio/ifi/pascal2100/parser/SimpleExpr.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -74,4 +75,21 @@ void prettyPrint() { } } } + + @Override + void genCode(CodeFile f) { + Iterator termOpersIter = termOpers.iterator(); + + for (Term t : terms) { + t.genCode(f); + + if (termOpersIter.hasNext()) { + termOpersIter.next().genCode(f); + } + } + + if (prefOper != null) { + prefOper.genCode(f); + } + } } diff --git a/no/uio/ifi/pascal2100/parser/Term.java b/no/uio/ifi/pascal2100/parser/Term.java index d908cac..5d3878a 100644 --- a/no/uio/ifi/pascal2100/parser/Term.java +++ b/no/uio/ifi/pascal2100/parser/Term.java @@ -3,6 +3,7 @@ import java.util.Iterator; import java.util.LinkedList; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -68,4 +69,17 @@ void prettyPrint() { } } } + + @Override + void genCode(CodeFile f) { + Iterator factorOpersIter = factorOpers.iterator(); + + for (Factor factor : factors) { + factor.genCode(f); + + if (factorOpersIter.hasNext()) { + factorOpersIter.next().genCode(f); + } + } + } } diff --git a/no/uio/ifi/pascal2100/parser/Type.java b/no/uio/ifi/pascal2100/parser/Type.java index 8951aac..2883fa0 100644 --- a/no/uio/ifi/pascal2100/parser/Type.java +++ b/no/uio/ifi/pascal2100/parser/Type.java @@ -35,6 +35,4 @@ public static Type parse(Scanner s) { return t; } - - abstract public int getSize(); } diff --git a/no/uio/ifi/pascal2100/parser/TypeDecl.java b/no/uio/ifi/pascal2100/parser/TypeDecl.java index 61725ed..47ef788 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDecl.java +++ b/no/uio/ifi/pascal2100/parser/TypeDecl.java @@ -49,8 +49,4 @@ public void prettyPrint() { type.prettyPrint(); Main.log.prettyPrint(";"); } - - public int getSize() { - return type.getSize(); - } } diff --git a/no/uio/ifi/pascal2100/parser/TypeDeclPart.java b/no/uio/ifi/pascal2100/parser/TypeDeclPart.java index ce82bd8..3379595 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/TypeDeclPart.java @@ -56,14 +56,4 @@ public void prettyPrint() { td.prettyPrint(); } } - - public int getSize() { - int size = 0; - - for (TypeDecl td : decls) { - size += td.getSize(); - } - - return size; - } } diff --git a/no/uio/ifi/pascal2100/parser/TypeName.java b/no/uio/ifi/pascal2100/parser/TypeName.java index 3f48af3..9fc8c08 100644 --- a/no/uio/ifi/pascal2100/parser/TypeName.java +++ b/no/uio/ifi/pascal2100/parser/TypeName.java @@ -40,9 +40,4 @@ public void check(Block curScope, Library lib) { void prettyPrint() { Main.log.prettyPrint(name); } - - @Override - public int getSize() { - return 0; - } } diff --git a/no/uio/ifi/pascal2100/parser/VarDecl.java b/no/uio/ifi/pascal2100/parser/VarDecl.java index 523e9a3..4ddcd2f 100644 --- a/no/uio/ifi/pascal2100/parser/VarDecl.java +++ b/no/uio/ifi/pascal2100/parser/VarDecl.java @@ -49,9 +49,4 @@ public void prettyPrint() { type.prettyPrint(); Main.log.prettyPrint(";"); } - - @Override - public int getSize() { - return 0; - } } diff --git a/no/uio/ifi/pascal2100/parser/VarDeclPart.java b/no/uio/ifi/pascal2100/parser/VarDeclPart.java index c078a7e..bb22c32 100644 --- a/no/uio/ifi/pascal2100/parser/VarDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/VarDeclPart.java @@ -56,14 +56,4 @@ public void prettyPrint() { vd.prettyPrint(); } } - - public int getSize() { - int size = 0; - - for (VarDecl vd : decls) { - size += vd.getSize(); - } - - return size; - } } diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index d9c5694..5ac3cb2 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -4,6 +4,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.leftBracketToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.rightBracketToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -63,4 +64,10 @@ void prettyPrint() { Main.log.prettyPrint("]"); } } + + @Override + void genCode(CodeFile f) { + f.genInstr("", "movl", "-" + (4 * nameDecl.declLevel) + "(%ebp),%edx"); + f.genInstr("", "movl", "-" + (36 + (4 * nameDecl.declOffset)) + "(%edx),%eax", name); + } } From 4aa6d6ce76ab4e6bcd05a84cbfaaf63ba64ffdd6 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:16:29 +0100 Subject: [PATCH 08/46] Add char literal generation --- no/uio/ifi/pascal2100/parser/CharLiteral.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/CharLiteral.java b/no/uio/ifi/pascal2100/parser/CharLiteral.java index e75b368..ba6a064 100644 --- a/no/uio/ifi/pascal2100/parser/CharLiteral.java +++ b/no/uio/ifi/pascal2100/parser/CharLiteral.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -35,4 +36,11 @@ public void check(Block curScope, Library lib) { } void prettyPrint() { Main.log.prettyPrint("'" + String.valueOf(val) + "'"); } + + @Override + void genCode(CodeFile f) { + int intVal = Character.getNumericValue(val); + + f.genInstr("", "movl", "$" + intVal + ",%eax", "" + val); + } } From 51d2f389c7dd1cd2cc1c2101b970fb6f364a3446 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:16:52 +0100 Subject: [PATCH 09/46] Add inner expr code gen --- no/uio/ifi/pascal2100/parser/InnerExpr.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/InnerExpr.java b/no/uio/ifi/pascal2100/parser/InnerExpr.java index 8728ce8..2ee6b8a 100644 --- a/no/uio/ifi/pascal2100/parser/InnerExpr.java +++ b/no/uio/ifi/pascal2100/parser/InnerExpr.java @@ -3,6 +3,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.leftParToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.rightParToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -47,4 +48,9 @@ void prettyPrint() { Main.log.prettyPrint(")"); } + + @Override + void genCode(CodeFile f) { + expr.genCode(f); + } } From 60aedbcd8ea2607c4eb321bb780333935d3495b3 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:17:20 +0100 Subject: [PATCH 10/46] Add code gen for negation --- no/uio/ifi/pascal2100/parser/Negation.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/Negation.java b/no/uio/ifi/pascal2100/parser/Negation.java index 5807528..7e20cdd 100644 --- a/no/uio/ifi/pascal2100/parser/Negation.java +++ b/no/uio/ifi/pascal2100/parser/Negation.java @@ -2,6 +2,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.notToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -42,4 +43,9 @@ void prettyPrint() { factor.prettyPrint(); } + + @Override + void genCode(CodeFile f) { + f.genInstr("", "xorl", "%1,%eax"); + } } From 38f55ca7c944db0969c9653b9a201c71caa261ce Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:17:39 +0100 Subject: [PATCH 11/46] Add number literal code gen --- no/uio/ifi/pascal2100/parser/NumberLiteral.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/NumberLiteral.java b/no/uio/ifi/pascal2100/parser/NumberLiteral.java index c6b7f9c..5790cac 100644 --- a/no/uio/ifi/pascal2100/parser/NumberLiteral.java +++ b/no/uio/ifi/pascal2100/parser/NumberLiteral.java @@ -42,6 +42,6 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - f.genInstr("", "movl", "$" + this.val + ",%eax", ""); + f.genInstr("", "movl", "$" + val + ",%eax", "" + val); } } From 821c7c4f1d47ee3f61f8504718fec749476c385d Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:18:54 +0100 Subject: [PATCH 12/46] Add rel operator code gen --- no/uio/ifi/pascal2100/parser/RelOperator.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/RelOperator.java b/no/uio/ifi/pascal2100/parser/RelOperator.java index 2a910dc..23ae958 100644 --- a/no/uio/ifi/pascal2100/parser/RelOperator.java +++ b/no/uio/ifi/pascal2100/parser/RelOperator.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; import no.uio.ifi.pascal2100.scanner.TokenKind; @@ -63,4 +64,32 @@ void prettyPrint() { Main.log.prettyPrint(symbol); } + + @Override + void genCode(CodeFile f) { + f.genInstr("", "popl", "%ecx"); + f.genInstr("", "cmpl", "%eax,%ecx"); + f.genInstr("", "movl", "$0,%eax"); + + switch (kind) { + case equalToken: + f.genInstr("", "sete", "%al", "Test ="); + break; + case notEqualToken: + f.genInstr("", "setne", "%al", "Test <>"); + break; + case lessToken: + f.genInstr("", "setl", "%al", "Test <"); + break; + case lessEqualToken: + f.genInstr("", "setle", "%al", "Test <="); + break; + case greaterToken: + f.genInstr("", "setg", "%al", "Test >"); + break; + default: + f.genInstr("", "setge", "%al", "Test >="); + break; + } + } } From 9af17459f69fa2302004d40e289259bb0e7751f0 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:19:59 +0100 Subject: [PATCH 13/46] Fix code gen for simple expression and term factors --- no/uio/ifi/pascal2100/parser/SimpleExpr.java | 15 ++++++++------- no/uio/ifi/pascal2100/parser/Term.java | 13 +++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/SimpleExpr.java b/no/uio/ifi/pascal2100/parser/SimpleExpr.java index 164777c..a83b3d7 100644 --- a/no/uio/ifi/pascal2100/parser/SimpleExpr.java +++ b/no/uio/ifi/pascal2100/parser/SimpleExpr.java @@ -78,14 +78,15 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - Iterator termOpersIter = termOpers.iterator(); - - for (Term t : terms) { - t.genCode(f); - - if (termOpersIter.hasNext()) { - termOpersIter.next().genCode(f); + for (int i = 0; i < termOpers.size(); i++) { + if (i == 0) { + terms.get(0).genCode(f); } + + f.genInstr("", "pushl", "%eax"); + terms.get(i + 1).genCode(f); + + termOpers.get(i).genCode(f); } if (prefOper != null) { diff --git a/no/uio/ifi/pascal2100/parser/Term.java b/no/uio/ifi/pascal2100/parser/Term.java index 5d3878a..eb30130 100644 --- a/no/uio/ifi/pascal2100/parser/Term.java +++ b/no/uio/ifi/pascal2100/parser/Term.java @@ -72,14 +72,15 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - Iterator factorOpersIter = factorOpers.iterator(); + for (int i = 0; i < factorOpers.size(); i++) { + if (i == 0) { + factors.get(0).genCode(f); + } - for (Factor factor : factors) { - factor.genCode(f); + f.genInstr("", "pushl", "%eax"); + factors.get(i + 1).genCode(f); - if (factorOpersIter.hasNext()) { - factorOpersIter.next().genCode(f); - } + factorOpers.get(i).genCode(f); } } } From 94970bea490c5846dc89eeef902350f35f011b78 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:21:08 +0100 Subject: [PATCH 14/46] Add code gen for addition operation --- no/uio/ifi/pascal2100/parser/TermOperator.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/TermOperator.java b/no/uio/ifi/pascal2100/parser/TermOperator.java index 400b999..c6e39fd 100644 --- a/no/uio/ifi/pascal2100/parser/TermOperator.java +++ b/no/uio/ifi/pascal2100/parser/TermOperator.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; import no.uio.ifi.pascal2100.scanner.TokenKind; @@ -54,4 +55,18 @@ void prettyPrint() { Main.log.prettyPrint(symbol); } + + @Override + void genCode(CodeFile f) { + switch (kind) { + case addToken: + f.genInstr("", "movl", "%eax,%ecx"); + f.genInstr("", "popl", "%eax"); + f.genInstr("", "addl", "%ecx,%eax", "+"); + break; + default: + f.genInstr("", "", "", kind.name()); + break; + } + } } From c53e103e5d4e9f9a64835077e5326f2147e6f1fc Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Wed, 25 Nov 2015 22:22:32 +0100 Subject: [PATCH 15/46] Add empty genCode function to pascal syntac class to alllow building --- no/uio/ifi/pascal2100/parser/PascalSyntax.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/PascalSyntax.java b/no/uio/ifi/pascal2100/parser/PascalSyntax.java index 9152be3..c4061e4 100644 --- a/no/uio/ifi/pascal2100/parser/PascalSyntax.java +++ b/no/uio/ifi/pascal2100/parser/PascalSyntax.java @@ -23,7 +23,9 @@ String getSourceLocation() { abstract void check(Block curScope, Library lib); - // abstract void genCode(CodeFile f); + //abstract void genCode(CodeFile f); + void genCode(CodeFile f) { } + abstract public String identify(); abstract void prettyPrint(); @@ -38,6 +40,4 @@ static void enterParser(String nonTerm) { static void leaveParser(String nonTerm) { Main.log.leaveParser(nonTerm); } - - void genCode(CodeFile f) { } } From 13f92b815ff203469121731a17755a2497258c82 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 10:14:15 +0100 Subject: [PATCH 16/46] Fix bug causing terms with only one factor to not be rendered --- no/uio/ifi/pascal2100/parser/SimpleExpr.java | 14 +++++++------- no/uio/ifi/pascal2100/parser/Term.java | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/SimpleExpr.java b/no/uio/ifi/pascal2100/parser/SimpleExpr.java index a83b3d7..1cbc78a 100644 --- a/no/uio/ifi/pascal2100/parser/SimpleExpr.java +++ b/no/uio/ifi/pascal2100/parser/SimpleExpr.java @@ -78,15 +78,15 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - for (int i = 0; i < termOpers.size(); i++) { - if (i == 0) { + for (int i = -1; i < termOpers.size(); i++) { + if (i < 0) { terms.get(0).genCode(f); - } - - f.genInstr("", "pushl", "%eax"); - terms.get(i + 1).genCode(f); + } else { + f.genInstr("", "pushl", "%eax"); + terms.get(i + 1).genCode(f); - termOpers.get(i).genCode(f); + termOpers.get(i).genCode(f); + } } if (prefOper != null) { diff --git a/no/uio/ifi/pascal2100/parser/Term.java b/no/uio/ifi/pascal2100/parser/Term.java index eb30130..9c942d3 100644 --- a/no/uio/ifi/pascal2100/parser/Term.java +++ b/no/uio/ifi/pascal2100/parser/Term.java @@ -72,15 +72,15 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - for (int i = 0; i < factorOpers.size(); i++) { - if (i == 0) { + for (int i = -1; i < factorOpers.size(); i++) { + if (i < 0) { factors.get(0).genCode(f); - } - - f.genInstr("", "pushl", "%eax"); - factors.get(i + 1).genCode(f); + } else { + f.genInstr("", "pushl", "%eax"); + factors.get(i + 1).genCode(f); - factorOpers.get(i).genCode(f); + factorOpers.get(i).genCode(f); + } } } } From a131fcf8d8c6aa8c01de50b4587f5834f25e3038 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 10:32:17 +0100 Subject: [PATCH 17/46] Add subtraction operation code gen --- no/uio/ifi/pascal2100/parser/TermOperator.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/TermOperator.java b/no/uio/ifi/pascal2100/parser/TermOperator.java index c6e39fd..6a4370f 100644 --- a/no/uio/ifi/pascal2100/parser/TermOperator.java +++ b/no/uio/ifi/pascal2100/parser/TermOperator.java @@ -64,6 +64,11 @@ void genCode(CodeFile f) { f.genInstr("", "popl", "%eax"); f.genInstr("", "addl", "%ecx,%eax", "+"); break; + case subtractToken: + f.genInstr("", "movl", "%eax,%ecx"); + f.genInstr("", "popl", "%eax"); + f.genInstr("", "subl", "%ecx,%eax", "-"); + break; default: f.genInstr("", "", "", kind.name()); break; From e72ec25daaafc7fd378dda7a381774a83693e938 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 11:13:55 +0100 Subject: [PATCH 18/46] Implement code gen for logical or --- no/uio/ifi/pascal2100/parser/Variable.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index 5ac3cb2..92738b8 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -67,7 +67,25 @@ void prettyPrint() { @Override void genCode(CodeFile f) { + TypeDecl td; + EnumType et; + + // Check if this is a reference to a enum type value + if (nameDecl instanceof TypeDecl) { + td = (TypeDecl) nameDecl; + et = (EnumType) td.type; + + for (int i = 0; i < et.literals.size(); i++) { + if (name.equals(et.literals.get(i).name)) { + f.genInstr("", "movl", "$" + i, "enum value " + name + "(=" + i + ")"); + return; + } + } + + return; + } + f.genInstr("", "movl", "-" + (4 * nameDecl.declLevel) + "(%ebp),%edx"); - f.genInstr("", "movl", "-" + (36 + (4 * nameDecl.declOffset)) + "(%edx),%eax", name); + f.genInstr("", "movl", "-" + (36 + (4 * nameDecl.declOffset)) + "(%edx),%eax", name + "(" + nameDecl.identify() + ")"); } } From cee0511587de9122afa4254a5bf3ff62c39d4526 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 11:15:37 +0100 Subject: [PATCH 19/46] Fix "typo" in comment --- no/uio/ifi/pascal2100/parser/Variable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index 92738b8..dd5efc0 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -70,7 +70,7 @@ void genCode(CodeFile f) { TypeDecl td; EnumType et; - // Check if this is a reference to a enum type value + // Check if this is a reference to a enumerated type value if (nameDecl instanceof TypeDecl) { td = (TypeDecl) nameDecl; et = (EnumType) td.type; From 8282becb3a91946e6f293d30dba8dbc77e753e84 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 11:22:11 +0100 Subject: [PATCH 20/46] Add logical or operation code gen --- no/uio/ifi/pascal2100/parser/TermOperator.java | 9 ++++----- no/uio/ifi/pascal2100/parser/Variable.java | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/TermOperator.java b/no/uio/ifi/pascal2100/parser/TermOperator.java index 6a4370f..45a285a 100644 --- a/no/uio/ifi/pascal2100/parser/TermOperator.java +++ b/no/uio/ifi/pascal2100/parser/TermOperator.java @@ -58,19 +58,18 @@ void prettyPrint() { @Override void genCode(CodeFile f) { + f.genInstr("", "movl", "%eax,%ecx"); + f.genInstr("", "popl", "%eax"); + switch (kind) { case addToken: - f.genInstr("", "movl", "%eax,%ecx"); - f.genInstr("", "popl", "%eax"); f.genInstr("", "addl", "%ecx,%eax", "+"); break; case subtractToken: - f.genInstr("", "movl", "%eax,%ecx"); - f.genInstr("", "popl", "%eax"); f.genInstr("", "subl", "%ecx,%eax", "-"); break; default: - f.genInstr("", "", "", kind.name()); + f.genInstr("", "orl", "%ecx,%eax", "or"); break; } } diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index dd5efc0..f153d7e 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -77,7 +77,7 @@ void genCode(CodeFile f) { for (int i = 0; i < et.literals.size(); i++) { if (name.equals(et.literals.get(i).name)) { - f.genInstr("", "movl", "$" + i, "enum value " + name + "(=" + i + ")"); + f.genInstr("", "movl", "$" + i + ",%eax", "enum value " + name + "(=" + i + ")"); return; } } From defb18404ecf5afe6cc02e01fe0fb6137a2d9860 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 11:54:39 +0100 Subject: [PATCH 21/46] Add code gen for multiplication and division --- .../ifi/pascal2100/parser/FactorOperator.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/FactorOperator.java b/no/uio/ifi/pascal2100/parser/FactorOperator.java index b722967..bc65133 100644 --- a/no/uio/ifi/pascal2100/parser/FactorOperator.java +++ b/no/uio/ifi/pascal2100/parser/FactorOperator.java @@ -1,5 +1,6 @@ package no.uio.ifi.pascal2100.parser; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; import no.uio.ifi.pascal2100.scanner.TokenKind; @@ -57,4 +58,23 @@ void prettyPrint() { Main.log.prettyPrint(symbol); } + + @Override + void genCode(CodeFile f) { + f.genInstr("", "movl", "%eax,%ecx"); + f.genInstr("", "popl", "%eax"); + + switch (kind) { + case multiplyToken: + f.genInstr("", "imull", "%ecx,%eax", "*"); + break; + case divToken: + f.genInstr("", "cdq"); + f.genInstr("", "idivl", "%ecx", "/"); + break; + default: + f.genInstr("", "", "", kind.name()); + break; + } + } } \ No newline at end of file From af182431ec25ee11f103ab268f592153174ff43f Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 11:59:18 +0100 Subject: [PATCH 22/46] Add modulo operation code generation --- no/uio/ifi/pascal2100/parser/FactorOperator.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/no/uio/ifi/pascal2100/parser/FactorOperator.java b/no/uio/ifi/pascal2100/parser/FactorOperator.java index bc65133..67a8e81 100644 --- a/no/uio/ifi/pascal2100/parser/FactorOperator.java +++ b/no/uio/ifi/pascal2100/parser/FactorOperator.java @@ -72,6 +72,11 @@ void genCode(CodeFile f) { f.genInstr("", "cdq"); f.genInstr("", "idivl", "%ecx", "/"); break; + case modToken: + f.genInstr("", "cdq"); + f.genInstr("", "idivl", "%ecx"); + f.genInstr("", "movl", "%edx,%eax", "mod"); + break; default: f.genInstr("", "", "", kind.name()); break; From 13102868eb9c9e6a3d6c82e92118c875e507b77a Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Fri, 27 Nov 2015 12:26:10 +0100 Subject: [PATCH 23/46] Add code gen for logical and --- no/uio/ifi/pascal2100/parser/FactorOperator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/FactorOperator.java b/no/uio/ifi/pascal2100/parser/FactorOperator.java index 67a8e81..28d377d 100644 --- a/no/uio/ifi/pascal2100/parser/FactorOperator.java +++ b/no/uio/ifi/pascal2100/parser/FactorOperator.java @@ -78,7 +78,7 @@ void genCode(CodeFile f) { f.genInstr("", "movl", "%edx,%eax", "mod"); break; default: - f.genInstr("", "", "", kind.name()); + f.genInstr("", "andl", "%ecx,%eax", "and"); break; } } From b039ba479c6d7efcb30f0b8aa201aa7e4882e58f Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sat, 28 Nov 2015 14:51:10 +0100 Subject: [PATCH 24/46] Implement write call logic * Implement write call for each param passed to the write function * Extend checking logic to set the value type to expect from an expression --- no/uio/ifi/pascal2100/parser/CharLiteral.java | 9 +++-- no/uio/ifi/pascal2100/parser/Constant.java | 2 ++ no/uio/ifi/pascal2100/parser/Expression.java | 33 ++++++++++++++++--- no/uio/ifi/pascal2100/parser/Factor.java | 2 ++ no/uio/ifi/pascal2100/parser/FuncCall.java | 13 ++++++-- no/uio/ifi/pascal2100/parser/InnerExpr.java | 4 +++ no/uio/ifi/pascal2100/parser/NamedConst.java | 7 +++- no/uio/ifi/pascal2100/parser/Negation.java | 5 +++ .../ifi/pascal2100/parser/NumberLiteral.java | 4 +++ .../ifi/pascal2100/parser/ProcCallStatm.java | 27 +++++++++++++-- no/uio/ifi/pascal2100/parser/SimpleExpr.java | 6 ++++ .../ifi/pascal2100/parser/StringLiteral.java | 6 ++++ no/uio/ifi/pascal2100/parser/Term.java | 6 ++++ no/uio/ifi/pascal2100/parser/Variable.java | 15 +++++++++ 14 files changed, 128 insertions(+), 11 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/CharLiteral.java b/no/uio/ifi/pascal2100/parser/CharLiteral.java index ba6a064..a8f574b 100644 --- a/no/uio/ifi/pascal2100/parser/CharLiteral.java +++ b/no/uio/ifi/pascal2100/parser/CharLiteral.java @@ -29,6 +29,11 @@ public static CharLiteral parse(Scanner s) { return sl; } + @Override + public void check(Block curScope, Library lib, Expression e) { + e.isChar = true; + } + @Override public void check(Block curScope, Library lib) { } @@ -39,8 +44,8 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - int intVal = Character.getNumericValue(val); + int intVal = (int) val; - f.genInstr("", "movl", "$" + intVal + ",%eax", "" + val); + f.genInstr("", "movl", "$" + intVal + ",%eax", "char " + intVal + " (" + val + ")"); } } diff --git a/no/uio/ifi/pascal2100/parser/Constant.java b/no/uio/ifi/pascal2100/parser/Constant.java index 1ddd038..cb4d294 100644 --- a/no/uio/ifi/pascal2100/parser/Constant.java +++ b/no/uio/ifi/pascal2100/parser/Constant.java @@ -30,4 +30,6 @@ static Constant parse(Scanner s) { leaveParser("constant"); return c; } + + abstract void check(Block curScope, Library lib, Expression e); } diff --git a/no/uio/ifi/pascal2100/parser/Expression.java b/no/uio/ifi/pascal2100/parser/Expression.java index ff7973f..3a760ca 100644 --- a/no/uio/ifi/pascal2100/parser/Expression.java +++ b/no/uio/ifi/pascal2100/parser/Expression.java @@ -9,6 +9,11 @@ public class Expression extends PascalSyntax { public RelOperator relOperator = null; public SimpleExpr trailing = null; + public boolean isNumeric = false; + public boolean isString = false; + public boolean isChar = false; + public StringLiteral string; + Expression(int n) { super(n); } @@ -35,16 +40,36 @@ public static Expression parse(Scanner s) { return e; } - @Override - public void check(Block curScope, Library lib) { - leading.check(curScope, lib); + public boolean isString() { + return this.isString; + } + + public String getString() { + return string.val; + } + + public boolean isChar() { + return this.isChar; + } + + public boolean isNumeric() { + return this.isNumeric; + } + + public void check(Block curScope, Library lib, Expression e) { + leading.check(curScope, lib, e); if (relOperator != null) { relOperator.check(curScope, lib); - trailing.check(curScope, lib); + trailing.check(curScope, lib, e); } } + @Override + public void check(Block curScope, Library lib) { + check(curScope, lib, this); + } + @Override void prettyPrint() { leading.prettyPrint(); diff --git a/no/uio/ifi/pascal2100/parser/Factor.java b/no/uio/ifi/pascal2100/parser/Factor.java index 00ccc4e..9ff45da 100644 --- a/no/uio/ifi/pascal2100/parser/Factor.java +++ b/no/uio/ifi/pascal2100/parser/Factor.java @@ -42,4 +42,6 @@ static Factor parse(Scanner s) { return f; } + + abstract void check(Block curScope, Library lib, Expression e); } \ No newline at end of file diff --git a/no/uio/ifi/pascal2100/parser/FuncCall.java b/no/uio/ifi/pascal2100/parser/FuncCall.java index ae34f63..01bc1bb 100644 --- a/no/uio/ifi/pascal2100/parser/FuncCall.java +++ b/no/uio/ifi/pascal2100/parser/FuncCall.java @@ -55,14 +55,23 @@ public static FuncCall parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression expr) { curScope.findDecl(name, this); for (Expression e : exprs) { - e.check(curScope, lib); + if (expr == null) { + e.check(curScope, lib); + } else { + e.check(curScope, lib, expr); + } } } + @Override + public void check(Block curScope, Library lib) { + check(curScope, lib, null); + } + @Override void prettyPrint() { Main.log.prettyPrint(name); diff --git a/no/uio/ifi/pascal2100/parser/InnerExpr.java b/no/uio/ifi/pascal2100/parser/InnerExpr.java index 2ee6b8a..c9a688a 100644 --- a/no/uio/ifi/pascal2100/parser/InnerExpr.java +++ b/no/uio/ifi/pascal2100/parser/InnerExpr.java @@ -35,6 +35,10 @@ public static InnerExpr parse(Scanner s) { return ie; } + public void check(Block curScope, Library lib, Expression e) { + expr.check(curScope, lib, e); + } + @Override public void check(Block curScope, Library lib) { expr.check(curScope, lib); diff --git a/no/uio/ifi/pascal2100/parser/NamedConst.java b/no/uio/ifi/pascal2100/parser/NamedConst.java index 9d21dc5..396fb24 100644 --- a/no/uio/ifi/pascal2100/parser/NamedConst.java +++ b/no/uio/ifi/pascal2100/parser/NamedConst.java @@ -32,10 +32,15 @@ public static NamedConst parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + void check(Block curScope, Library lib, Expression e) { curScope.findDecl(name, this); } + @Override + public void check(Block curScope, Library lib) { + check(curScope, lib, null); + } + @Override void prettyPrint() { Main.log.prettyPrint(name); diff --git a/no/uio/ifi/pascal2100/parser/Negation.java b/no/uio/ifi/pascal2100/parser/Negation.java index 7e20cdd..cd5036b 100644 --- a/no/uio/ifi/pascal2100/parser/Negation.java +++ b/no/uio/ifi/pascal2100/parser/Negation.java @@ -32,6 +32,11 @@ public static Negation parse(Scanner s) { return n; } + @Override + void check(Block curScope, Library lib, Expression e) { + factor.check(curScope, lib, e); + } + @Override public void check(Block curScope, Library lib) { factor.check(curScope, lib); diff --git a/no/uio/ifi/pascal2100/parser/NumberLiteral.java b/no/uio/ifi/pascal2100/parser/NumberLiteral.java index 5790cac..72602ef 100644 --- a/no/uio/ifi/pascal2100/parser/NumberLiteral.java +++ b/no/uio/ifi/pascal2100/parser/NumberLiteral.java @@ -32,6 +32,10 @@ public static NumberLiteral parse(Scanner s) { return nl; } + public void check(Block curScope, Library lib, Expression e) { + e.isNumeric = true; + } + @Override public void check(Block curScope, Library lib) { } diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index 581ffde..08603d0 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -83,14 +83,37 @@ void prettyPrint() { } private void genWriteCode(CodeFile f) { - + Expression e; + String funcName = ""; + + for (int i = 0; i < exprs.size(); i++) { + e = exprs.get(i); + + if (e.isNumeric()) { + e.genCode(f); + funcName = "write_int"; + } else if (e.isChar()) { + e.genCode(f); + funcName = "write_char"; + } else if (e.isString()) { + String label = f.getLocalLabel(); + f.genString(label, e.getString(), ""); + f.genInstr("", "leal", label + ",%eax", "Addr(\"" + e.getString() + "\")"); + } else { + f.genInstr("", "", "", "write call, but not sure what type of value to write"); + } + + f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); + f.genInstr("", "call", "write_string"); + f.genInstr("", "addl", "$4,%esp", "Pop parameter"); + } } public void genCode(CodeFile f) { System.out.println("fooo " + name); // Check this is a call on the library function write - if (name.toLowerCase() == "write") { + if (name.toLowerCase().equals("write")) { genWriteCode(f); return; } diff --git a/no/uio/ifi/pascal2100/parser/SimpleExpr.java b/no/uio/ifi/pascal2100/parser/SimpleExpr.java index 1cbc78a..9dc20e3 100644 --- a/no/uio/ifi/pascal2100/parser/SimpleExpr.java +++ b/no/uio/ifi/pascal2100/parser/SimpleExpr.java @@ -46,6 +46,12 @@ public static SimpleExpr parse(Scanner s) { return se; } + public void check(Block curScope, Library lib, Expression e) { + for (Term t : terms) { + t.check(curScope, lib, e); + } + } + @Override public void check(Block curScope, Library lib) { for (Term t : terms) { diff --git a/no/uio/ifi/pascal2100/parser/StringLiteral.java b/no/uio/ifi/pascal2100/parser/StringLiteral.java index 333f8e8..9fb9dbd 100644 --- a/no/uio/ifi/pascal2100/parser/StringLiteral.java +++ b/no/uio/ifi/pascal2100/parser/StringLiteral.java @@ -28,6 +28,12 @@ public static StringLiteral parse(Scanner s) { return sl; } + @Override + public void check(Block curScope, Library lib, Expression e) { + e.isString = true; + e.string = this; + } + @Override public void check(Block curScope, Library lib) { } diff --git a/no/uio/ifi/pascal2100/parser/Term.java b/no/uio/ifi/pascal2100/parser/Term.java index 9c942d3..70bc74c 100644 --- a/no/uio/ifi/pascal2100/parser/Term.java +++ b/no/uio/ifi/pascal2100/parser/Term.java @@ -44,6 +44,12 @@ static Term parse(Scanner s) { return t; } + public void check(Block curScope, Library lib, Expression e) { + for (Factor f : factors) { + f.check(curScope, lib, e); + } + } + @Override public void check(Block curScope, Library lib) { for (Factor f : factors) { diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index f153d7e..559890f 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -45,6 +45,21 @@ public static Variable parse(Scanner s) { return v; } + @Override + public void check(Block curScope, Library lib, Expression e) { + nameDecl = curScope.findDecl(name, this); + + if (expr == null) { + return; + } + + if (e != null) { + expr.check(curScope, lib, e); + } else { + expr.check(curScope, lib); + } + } + @Override public void check(Block curScope, Library lib) { nameDecl = curScope.findDecl(name, this); From bdb5b4f38cadedb5e71f0528755f6bcce0dfbe49 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sun, 29 Nov 2015 20:05:51 +0100 Subject: [PATCH 25/46] Implement proc calls and decls * Moved decl level/offset logic to check stage * Add proc calls * Add proc decls --- no/uio/ifi/pascal2100/parser/AssignStatm.java | 2 +- no/uio/ifi/pascal2100/parser/Block.java | 11 ++++++---- .../ifi/pascal2100/parser/ProcCallStatm.java | 18 ++++++++++----- no/uio/ifi/pascal2100/parser/ProcDecl.java | 22 ++++++++++++++++++- no/uio/ifi/pascal2100/parser/Variable.java | 8 ++----- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/AssignStatm.java b/no/uio/ifi/pascal2100/parser/AssignStatm.java index fbcd108..5cc9baa 100644 --- a/no/uio/ifi/pascal2100/parser/AssignStatm.java +++ b/no/uio/ifi/pascal2100/parser/AssignStatm.java @@ -56,6 +56,6 @@ public void genCode(CodeFile f) { // Simple variable assignment f.genInstr("", "movl", "-" + (4 * var.nameDecl.declLevel) + "(%ebp),%edx"); - f.genInstr("", "movl", "%eax,-" + (36 + (4 * var.nameDecl.declOffset)) + "(%edx)", var.name + " :="); + f.genInstr("", "movl", "%eax," + var.nameDecl.declOffset + "(%edx)", var.name + " :="); } } diff --git a/no/uio/ifi/pascal2100/parser/Block.java b/no/uio/ifi/pascal2100/parser/Block.java index 2393ba5..939c984 100644 --- a/no/uio/ifi/pascal2100/parser/Block.java +++ b/no/uio/ifi/pascal2100/parser/Block.java @@ -100,9 +100,6 @@ public void addDecl(String id, PascalDecl decl) { decl.error(id + " declared twice in same block"); } - decl.declLevel = blockLevel; - decl.declOffset = decls.size(); - decls.put(id.toLowerCase(), decl); } @@ -126,7 +123,13 @@ public void check(Block curScope, Library lib) { if (varDeclPart != null) { varDeclPart.check(this, lib); - for (VarDecl vd: varDeclPart.decls) { + VarDecl vd; + for (int i = 0; i < varDeclPart.decls.size(); i++) { + vd = varDeclPart.decls.get(i); + + vd.declLevel = blockLevel; + vd.declOffset = -36 - (i * 4); + this.addDecl(vd.name, vd); } } diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index 08603d0..4cbd04d 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -14,6 +14,7 @@ public class ProcCallStatm extends Statement { public String name; public LinkedList exprs = new LinkedList(); + public ProcDecl decl; ProcCallStatm(String id, int lNum) { super(lNum); @@ -56,7 +57,7 @@ public static ProcCallStatm parse(Scanner s) { @Override public void check(Block curScope, Library lib) { - curScope.findDecl(name, this); + decl = (ProcDecl) curScope.findDecl(name, this); for (Expression e : exprs) { e.check(curScope, lib); @@ -104,22 +105,27 @@ private void genWriteCode(CodeFile f) { } f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); - f.genInstr("", "call", "write_string"); + f.genInstr("", "call", funcName); f.genInstr("", "addl", "$4,%esp", "Pop parameter"); } } public void genCode(CodeFile f) { - System.out.println("fooo " + name); - // Check this is a call on the library function write if (name.toLowerCase().equals("write")) { genWriteCode(f); return; } - for (int i = exprs.size() -1; i > 0; i--) { - + for (int i = exprs.size() - 1; i >= 0; i--) { + exprs.get(i).genCode(f); + f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); + } + + f.genInstr("", "call", decl.label); + + if (exprs.size() > 0) { + f.genInstr("", "addl", "$" + exprs.size() * 4 + ",%esp", "Pop parameters"); } } } diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index e623636..042cddb 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -5,12 +5,14 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.semicolonToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.leftParToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; public class ProcDecl extends PascalDecl { public ParamDeclList paramDeclList = null; public Block block; + public String label; ProcDecl(String id, int lNum) { super(id, lNum); @@ -48,10 +50,17 @@ public static ProcDecl parse(Scanner s) { @Override public void check(Block curScope, Library lib) { + ParamDecl pd; + curScope.addDecl(name, this); if (paramDeclList != null) { - for (ParamDecl pd: paramDeclList.decls) { + for (int i = 0; i < paramDeclList.decls.size(); i++) { + pd = paramDeclList.decls.get(i); + + pd.declLevel = curScope.blockLevel + 1; + pd.declOffset = 8 + (4 * i); + block.addDecl(pd.name, pd); } @@ -74,4 +83,15 @@ public void prettyPrint() { block.prettyPrint(); Main.log.prettyPrintLn("; {" + name + "}"); } + + @Override + public void genCode(CodeFile f) { + label = f.getLabel("proc$" + name); + + f.genInstr(label, ""); + f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); + block.genCode(f); + f.genInstr("", "leave"); + f.genInstr("", "ret"); + } } diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index 559890f..8681f6d 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -62,11 +62,7 @@ public void check(Block curScope, Library lib, Expression e) { @Override public void check(Block curScope, Library lib) { - nameDecl = curScope.findDecl(name, this); - - if (expr != null) { - expr.check(curScope, lib); - } + check(curScope, lib, null); } @Override @@ -101,6 +97,6 @@ void genCode(CodeFile f) { } f.genInstr("", "movl", "-" + (4 * nameDecl.declLevel) + "(%ebp),%edx"); - f.genInstr("", "movl", "-" + (36 + (4 * nameDecl.declOffset)) + "(%edx),%eax", name + "(" + nameDecl.identify() + ")"); + f.genInstr("", "movl", nameDecl.declOffset + "(%edx),%eax", name); } } From 03855011e4f970cda14fe8c8af14c0349f4f47fb Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sun, 29 Nov 2015 23:09:00 +0100 Subject: [PATCH 26/46] Fix various bugs --- no/uio/ifi/pascal2100/parser/Library.java | 4 ++-- no/uio/ifi/pascal2100/parser/ProcCallStatm.java | 1 + no/uio/ifi/pascal2100/parser/Variable.java | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/Library.java b/no/uio/ifi/pascal2100/parser/Library.java index 5810d97..73dc816 100644 --- a/no/uio/ifi/pascal2100/parser/Library.java +++ b/no/uio/ifi/pascal2100/parser/Library.java @@ -4,14 +4,14 @@ public class Library extends Block { public Library() { super(-1); - addEof(); + addEol(); addChar(); addBoolean(); addInteger(); addWrite(); } - void addEof() { + void addEol() { ConstDecl eol = new ConstDecl("eol", -1); eol.constant = new CharLiteral('\n', -1); diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index 4cbd04d..d6ca045 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -102,6 +102,7 @@ private void genWriteCode(CodeFile f) { f.genInstr("", "leal", label + ",%eax", "Addr(\"" + e.getString() + "\")"); } else { f.genInstr("", "", "", "write call, but not sure what type of value to write"); + return; } f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index 8681f6d..6835d6c 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -96,6 +96,12 @@ void genCode(CodeFile f) { return; } + // Check if this is a reference to a constant + if (nameDecl instanceof ConstDecl) { + ((ConstDecl) nameDecl).constant.genCode(f); + return; + } + f.genInstr("", "movl", "-" + (4 * nameDecl.declLevel) + "(%ebp),%edx"); f.genInstr("", "movl", nameDecl.declOffset + "(%edx),%eax", name); } From 509045f1b328669611e637622d2335982c708b0e Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sun, 29 Nov 2015 23:34:10 +0100 Subject: [PATCH 27/46] Add isBindingNoted bool to pascal syntax instances to avoid logging the binding check twice --- no/uio/ifi/pascal2100/main/LogFile.java | 4 +++- no/uio/ifi/pascal2100/parser/PascalSyntax.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/main/LogFile.java b/no/uio/ifi/pascal2100/main/LogFile.java index d518087..2b34be0 100644 --- a/no/uio/ifi/pascal2100/main/LogFile.java +++ b/no/uio/ifi/pascal2100/main/LogFile.java @@ -121,7 +121,9 @@ public void noteToken(Token tok) { */ public void noteBinding(String id, PascalSyntax where, PascalDecl decl) { - if (doLogBinding) { + if (doLogBinding && !where.isBindingNoted) { + where.isBindingNoted = true; + writeLogLine( "Binding on line " + where.lineNum + ": " + id + " was declared in " + decl.identify() diff --git a/no/uio/ifi/pascal2100/parser/PascalSyntax.java b/no/uio/ifi/pascal2100/parser/PascalSyntax.java index c4061e4..3043848 100644 --- a/no/uio/ifi/pascal2100/parser/PascalSyntax.java +++ b/no/uio/ifi/pascal2100/parser/PascalSyntax.java @@ -4,6 +4,7 @@ public abstract class PascalSyntax { public int lineNum; + public boolean isBindingNoted = false; PascalSyntax(int n) { lineNum = n; From 9a1836a50dc43ae4d33e102d431de1cde6ee7eaf Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Sun, 29 Nov 2015 23:34:25 +0100 Subject: [PATCH 28/46] Remove redundant checking --- no/uio/ifi/pascal2100/parser/TypeDecl.java | 4 ---- no/uio/ifi/pascal2100/parser/VarDecl.java | 5 +---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/TypeDecl.java b/no/uio/ifi/pascal2100/parser/TypeDecl.java index 47ef788..7892b5d 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDecl.java +++ b/no/uio/ifi/pascal2100/parser/TypeDecl.java @@ -37,10 +37,6 @@ public static TypeDecl parse(Scanner s) { @Override public void check(Block curScope, Library lib) { type.check(curScope, lib); - - if (type instanceof TypeName) { - curScope.findDecl(((TypeName) type).name, this); - } } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/VarDecl.java b/no/uio/ifi/pascal2100/parser/VarDecl.java index 4ddcd2f..161a408 100644 --- a/no/uio/ifi/pascal2100/parser/VarDecl.java +++ b/no/uio/ifi/pascal2100/parser/VarDecl.java @@ -38,10 +38,7 @@ public static VarDecl parse(Scanner s) { @Override public void check(Block curScope, Library lib) { type.check(curScope, lib); - - if (type instanceof TypeName) { - curScope.findDecl(((TypeName) type).name, this); - } + type.check(curScope, lib); } public void prettyPrint() { From aaf75152654645b907be155c34c1c66f88b88b14 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 00:12:33 +0100 Subject: [PATCH 29/46] Add checking for constants and library stuff --- no/uio/ifi/pascal2100/parser/ArrayType.java | 8 ++++++-- no/uio/ifi/pascal2100/parser/CharLiteral.java | 2 +- no/uio/ifi/pascal2100/parser/ConstDecl.java | 7 ++++++- no/uio/ifi/pascal2100/parser/EnumLiteral.java | 5 +++++ no/uio/ifi/pascal2100/parser/EnumType.java | 6 ++++++ no/uio/ifi/pascal2100/parser/FuncCall.java | 10 ++++------ no/uio/ifi/pascal2100/parser/FuncDecl.java | 11 ++++++++--- no/uio/ifi/pascal2100/parser/NumberLiteral.java | 4 ++++ no/uio/ifi/pascal2100/parser/ParamDecl.java | 9 +++++++-- no/uio/ifi/pascal2100/parser/PascalDecl.java | 2 ++ no/uio/ifi/pascal2100/parser/ProcDecl.java | 5 +++++ no/uio/ifi/pascal2100/parser/Program.java | 5 +++++ no/uio/ifi/pascal2100/parser/RangeType.java | 8 ++++++-- no/uio/ifi/pascal2100/parser/SimpleExpr.java | 6 ++---- no/uio/ifi/pascal2100/parser/Term.java | 6 ++---- no/uio/ifi/pascal2100/parser/Type.java | 2 ++ no/uio/ifi/pascal2100/parser/TypeDecl.java | 6 +++++- no/uio/ifi/pascal2100/parser/TypeName.java | 10 +++++++++- no/uio/ifi/pascal2100/parser/VarDecl.java | 8 ++++++-- no/uio/ifi/pascal2100/parser/Variable.java | 7 ++----- 20 files changed, 93 insertions(+), 34 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/ArrayType.java b/no/uio/ifi/pascal2100/parser/ArrayType.java index 5e0edf7..93f21ed 100644 --- a/no/uio/ifi/pascal2100/parser/ArrayType.java +++ b/no/uio/ifi/pascal2100/parser/ArrayType.java @@ -64,10 +64,14 @@ public int getHigh() { return high.val; } + public void check(Block curScope, Library lib, Expression e) { + type.check(curScope, lib); + ofType.check(curScope, lib, e); + } + @Override public void check(Block curScope, Library lib) { - type.check(curScope, lib); - ofType.check(curScope, lib); + check(curScope, lib, null); } void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/CharLiteral.java b/no/uio/ifi/pascal2100/parser/CharLiteral.java index a8f574b..c582cef 100644 --- a/no/uio/ifi/pascal2100/parser/CharLiteral.java +++ b/no/uio/ifi/pascal2100/parser/CharLiteral.java @@ -46,6 +46,6 @@ void prettyPrint() { void genCode(CodeFile f) { int intVal = (int) val; - f.genInstr("", "movl", "$" + intVal + ",%eax", "char " + intVal + " (" + val + ")"); + f.genInstr("", "movl", "$" + intVal + ",%eax", "char " + intVal); } } diff --git a/no/uio/ifi/pascal2100/parser/ConstDecl.java b/no/uio/ifi/pascal2100/parser/ConstDecl.java index a5b7f2d..1a65d0d 100644 --- a/no/uio/ifi/pascal2100/parser/ConstDecl.java +++ b/no/uio/ifi/pascal2100/parser/ConstDecl.java @@ -35,9 +35,14 @@ public static ConstDecl parse(Scanner s) { return cd; } + @Override + public void check(Block curScope, Library lib, Expression e) { + constant.check(curScope, lib, e != null ? e : null); + } + @Override public void check(Block curScope, Library lib) { - constant.check(curScope, lib); + check(curScope, lib, null); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/EnumLiteral.java b/no/uio/ifi/pascal2100/parser/EnumLiteral.java index 8dd997a..d2f612a 100644 --- a/no/uio/ifi/pascal2100/parser/EnumLiteral.java +++ b/no/uio/ifi/pascal2100/parser/EnumLiteral.java @@ -28,6 +28,11 @@ public static EnumLiteral parse(Scanner s) { return cd; } + @Override + public void check(Block curScope, Library lib, Expression e) { + e.isNumeric = true; + } + @Override public void check(Block curScope, Library lib) { } diff --git a/no/uio/ifi/pascal2100/parser/EnumType.java b/no/uio/ifi/pascal2100/parser/EnumType.java index 1dfb1e7..e32d71d 100644 --- a/no/uio/ifi/pascal2100/parser/EnumType.java +++ b/no/uio/ifi/pascal2100/parser/EnumType.java @@ -37,6 +37,12 @@ public static EnumType parse(Scanner s) { return et; } + public void check(Block curScope, Library lib, Expression e) { + check(curScope, lib); + + e.isNumeric = true; + } + @Override public void check(Block curScope, Library lib) { for (EnumLiteral el : literals) { diff --git a/no/uio/ifi/pascal2100/parser/FuncCall.java b/no/uio/ifi/pascal2100/parser/FuncCall.java index 01bc1bb..14e2136 100644 --- a/no/uio/ifi/pascal2100/parser/FuncCall.java +++ b/no/uio/ifi/pascal2100/parser/FuncCall.java @@ -56,15 +56,13 @@ public static FuncCall parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression expr) { - curScope.findDecl(name, this); + FuncDecl fd = (FuncDecl) curScope.findDecl(name, this); for (Expression e : exprs) { - if (expr == null) { - e.check(curScope, lib); - } else { - e.check(curScope, lib, expr); - } + e.check(curScope, lib); } + + fd.check(curScope, lib, expr); } @Override diff --git a/no/uio/ifi/pascal2100/parser/FuncDecl.java b/no/uio/ifi/pascal2100/parser/FuncDecl.java index 2ba5b31..8bce102 100644 --- a/no/uio/ifi/pascal2100/parser/FuncDecl.java +++ b/no/uio/ifi/pascal2100/parser/FuncDecl.java @@ -50,13 +50,18 @@ public static FuncDecl parse(Scanner s) { return fd; } - @Override - public void check(Block curScope, Library lib) { - curScope.findDecl(typeName.name, this); + public void check(Block curScope, Library lib, Expression e) { + TypeDecl td = (TypeDecl) curScope.findDecl(typeName.name, this); + td.check(curScope, lib, e); super.check(curScope, lib); } + @Override + public void check(Block curScope, Library lib) { + check(curScope, lib, null); + } + public void prettyPrint() { Main.log.prettyPrintLn(); Main.log.prettyPrint("function " + name); diff --git a/no/uio/ifi/pascal2100/parser/NumberLiteral.java b/no/uio/ifi/pascal2100/parser/NumberLiteral.java index 72602ef..fdd1bca 100644 --- a/no/uio/ifi/pascal2100/parser/NumberLiteral.java +++ b/no/uio/ifi/pascal2100/parser/NumberLiteral.java @@ -33,6 +33,10 @@ public static NumberLiteral parse(Scanner s) { } public void check(Block curScope, Library lib, Expression e) { + if (e == null) { + return; + } + e.isNumeric = true; } diff --git a/no/uio/ifi/pascal2100/parser/ParamDecl.java b/no/uio/ifi/pascal2100/parser/ParamDecl.java index 1bb0666..9157c6d 100644 --- a/no/uio/ifi/pascal2100/parser/ParamDecl.java +++ b/no/uio/ifi/pascal2100/parser/ParamDecl.java @@ -34,9 +34,14 @@ public static ParamDecl parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression e) { curScope.findDecl(typeName.name, this); - typeName.check(curScope, lib); + typeName.check(curScope, lib, e != null ? e : null); + } + + @Override + public void check(Block curScope, Library lib) { + check(curScope, lib, null); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/PascalDecl.java b/no/uio/ifi/pascal2100/parser/PascalDecl.java index 2883e09..c411e7e 100644 --- a/no/uio/ifi/pascal2100/parser/PascalDecl.java +++ b/no/uio/ifi/pascal2100/parser/PascalDecl.java @@ -39,4 +39,6 @@ public abstract class PascalDecl extends PascalSyntax { abstract void checkWhetherProcedure(PascalSyntax where); abstract void checkWhetherValue(PascalSyntax where); */ + + abstract void check(Block curScope, Library lib, Expression e); } diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index 042cddb..6b66be2 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -48,6 +48,11 @@ public static ProcDecl parse(Scanner s) { return pd; } + @Override + public void check(Block curScope, Library lib, Expression e) { + check(curScope, lib); + } + @Override public void check(Block curScope, Library lib) { ParamDecl pd; diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index f6183f4..210d08d 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -43,6 +43,11 @@ public static Program parse(Scanner s) { return p; } + @Override + public void check(Block curScope, Library lib, Expression e) { + check(curScope, lib); + } + @Override public void check(Block curScope, Library lib) { progBlock.check(curScope, progBlock, lib); diff --git a/no/uio/ifi/pascal2100/parser/RangeType.java b/no/uio/ifi/pascal2100/parser/RangeType.java index 335f2e2..a3ac025 100644 --- a/no/uio/ifi/pascal2100/parser/RangeType.java +++ b/no/uio/ifi/pascal2100/parser/RangeType.java @@ -33,10 +33,14 @@ public static RangeType parse(Scanner s) { return rt; } + public void check(Block curScope, Library lib, Expression e) { + from.check(curScope, lib, e); + from.check(curScope, lib, e); + } + @Override public void check(Block curScope, Library lib) { - from.check(curScope, lib); - to.check(curScope, lib); + check(curScope, lib, null); } void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/SimpleExpr.java b/no/uio/ifi/pascal2100/parser/SimpleExpr.java index 9dc20e3..6aff642 100644 --- a/no/uio/ifi/pascal2100/parser/SimpleExpr.java +++ b/no/uio/ifi/pascal2100/parser/SimpleExpr.java @@ -48,15 +48,13 @@ public static SimpleExpr parse(Scanner s) { public void check(Block curScope, Library lib, Expression e) { for (Term t : terms) { - t.check(curScope, lib, e); + t.check(curScope, lib, e != null ? e : null); } } @Override public void check(Block curScope, Library lib) { - for (Term t : terms) { - t.check(curScope, lib); - } + check(curScope, lib, null); } @Override diff --git a/no/uio/ifi/pascal2100/parser/Term.java b/no/uio/ifi/pascal2100/parser/Term.java index 70bc74c..837f827 100644 --- a/no/uio/ifi/pascal2100/parser/Term.java +++ b/no/uio/ifi/pascal2100/parser/Term.java @@ -46,15 +46,13 @@ static Term parse(Scanner s) { public void check(Block curScope, Library lib, Expression e) { for (Factor f : factors) { - f.check(curScope, lib, e); + f.check(curScope, lib, e != null ? e : null); } } @Override public void check(Block curScope, Library lib) { - for (Factor f : factors) { - f.check(curScope, lib); - } + check(curScope, lib); } @Override diff --git a/no/uio/ifi/pascal2100/parser/Type.java b/no/uio/ifi/pascal2100/parser/Type.java index 2883fa0..46589f7 100644 --- a/no/uio/ifi/pascal2100/parser/Type.java +++ b/no/uio/ifi/pascal2100/parser/Type.java @@ -35,4 +35,6 @@ public static Type parse(Scanner s) { return t; } + + abstract void check(Block curScope, Library lib, Expression e); } diff --git a/no/uio/ifi/pascal2100/parser/TypeDecl.java b/no/uio/ifi/pascal2100/parser/TypeDecl.java index 7892b5d..71a922a 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDecl.java +++ b/no/uio/ifi/pascal2100/parser/TypeDecl.java @@ -34,9 +34,13 @@ public static TypeDecl parse(Scanner s) { return td; } + public void check(Block curScope, Library lib, Expression e) { + type.check(curScope, lib, e != null ? e : null); + } + @Override public void check(Block curScope, Library lib) { - type.check(curScope, lib); + check(curScope, lib, null); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/TypeName.java b/no/uio/ifi/pascal2100/parser/TypeName.java index 9fc8c08..ba0ad2a 100644 --- a/no/uio/ifi/pascal2100/parser/TypeName.java +++ b/no/uio/ifi/pascal2100/parser/TypeName.java @@ -7,7 +7,7 @@ public class TypeName extends Type { public String name; - public PascalDecl decl; + public PascalDecl decl = null; TypeName(String id, int lNum) { super(lNum); @@ -32,6 +32,14 @@ public static TypeName parse(Scanner s) { return tn; } + public void check(Block curScope, Library lib, Expression e) { + check(curScope, lib); + + if (decl.type != null) { + decl.type.check(curScope, lib, e); + } + } + @Override public void check(Block curScope, Library lib) { decl = curScope.findDecl(name, this); diff --git a/no/uio/ifi/pascal2100/parser/VarDecl.java b/no/uio/ifi/pascal2100/parser/VarDecl.java index 161a408..6f0d7b2 100644 --- a/no/uio/ifi/pascal2100/parser/VarDecl.java +++ b/no/uio/ifi/pascal2100/parser/VarDecl.java @@ -35,10 +35,14 @@ public static VarDecl parse(Scanner s) { return vd; } + @Override + public void check(Block curScope, Library lib, Expression e) { + type.check(curScope, lib, e != null ? e : null); + } + @Override public void check(Block curScope, Library lib) { - type.check(curScope, lib); - type.check(curScope, lib); + check(curScope, lib, null); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index 6835d6c..18b6fb4 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -48,16 +48,13 @@ public static Variable parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { nameDecl = curScope.findDecl(name, this); + nameDecl.check(curScope, lib, e != null ? e : null); if (expr == null) { return; } - if (e != null) { - expr.check(curScope, lib, e); - } else { - expr.check(curScope, lib); - } + expr.check(curScope, lib, e != null ? e : null); } @Override From fc38beaa3b7c0199c875dc697956b7f447b8af8f Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 19:06:34 +0100 Subject: [PATCH 30/46] Add commen tto negation prefix --- no/uio/ifi/pascal2100/parser/PrefixOperator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/PrefixOperator.java b/no/uio/ifi/pascal2100/parser/PrefixOperator.java index 1c6274e..fceb71e 100644 --- a/no/uio/ifi/pascal2100/parser/PrefixOperator.java +++ b/no/uio/ifi/pascal2100/parser/PrefixOperator.java @@ -60,6 +60,6 @@ void genCode(CodeFile f) { return; } - f.genInstr("", "negl", "%eax"); + f.genInstr("", "negl", "%eax", "- (prefix)"); } } From 3f44c666f7404280b8d19438c4ad6c43c749cd67 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 19:07:05 +0100 Subject: [PATCH 31/46] Jump to next element in iteration instead of returning when encountering an unknown param type for write operation --- no/uio/ifi/pascal2100/parser/ProcCallStatm.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index d6ca045..fa5a9c6 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -102,7 +102,7 @@ private void genWriteCode(CodeFile f) { f.genInstr("", "leal", label + ",%eax", "Addr(\"" + e.getString() + "\")"); } else { f.genInstr("", "", "", "write call, but not sure what type of value to write"); - return; + continue; } f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); From 485f6866c97796b6be57395458326f845d1269d1 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 19:07:46 +0100 Subject: [PATCH 32/46] Generate code for proc block decls --- no/uio/ifi/pascal2100/parser/ProcDecl.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index 6b66be2..f5ce1ee 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -92,7 +92,11 @@ public void prettyPrint() { @Override public void genCode(CodeFile f) { label = f.getLabel("proc$" + name); - + + for (PascalDecl pd : block.decls.values()) { + pd.genCode(f); + } + f.genInstr(label, ""); f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); block.genCode(f); From a7a8a716508b611425e2a49d46eef91693281830 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 19:08:05 +0100 Subject: [PATCH 33/46] Add end of comment to leave instruction --- no/uio/ifi/pascal2100/parser/ProcDecl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index f5ce1ee..dda03ac 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -100,7 +100,7 @@ public void genCode(CodeFile f) { f.genInstr(label, ""); f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); block.genCode(f); - f.genInstr("", "leave"); + f.genInstr("", "leave", "", "End of " + name); f.genInstr("", "ret"); } } From 572d3a8134cfdbfbf6d2584dbcbfab8eb032ef9a Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 21:10:42 +0100 Subject: [PATCH 34/46] Fix bug that stopped propagation of type checking to resolve expression value type --- no/uio/ifi/pascal2100/parser/EnumType.java | 4 +++- no/uio/ifi/pascal2100/parser/ProcCallStatm.java | 1 + no/uio/ifi/pascal2100/parser/TypeName.java | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/EnumType.java b/no/uio/ifi/pascal2100/parser/EnumType.java index e32d71d..0407fe4 100644 --- a/no/uio/ifi/pascal2100/parser/EnumType.java +++ b/no/uio/ifi/pascal2100/parser/EnumType.java @@ -40,7 +40,9 @@ public static EnumType parse(Scanner s) { public void check(Block curScope, Library lib, Expression e) { check(curScope, lib); - e.isNumeric = true; + if (e != null) { + e.isNumeric = true; + } } @Override diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index fa5a9c6..21e9e66 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -98,6 +98,7 @@ private void genWriteCode(CodeFile f) { funcName = "write_char"; } else if (e.isString()) { String label = f.getLocalLabel(); + funcName = "write_string"; f.genString(label, e.getString(), ""); f.genInstr("", "leal", label + ",%eax", "Addr(\"" + e.getString() + "\")"); } else { diff --git a/no/uio/ifi/pascal2100/parser/TypeName.java b/no/uio/ifi/pascal2100/parser/TypeName.java index ba0ad2a..0d59e27 100644 --- a/no/uio/ifi/pascal2100/parser/TypeName.java +++ b/no/uio/ifi/pascal2100/parser/TypeName.java @@ -35,8 +35,8 @@ public static TypeName parse(Scanner s) { public void check(Block curScope, Library lib, Expression e) { check(curScope, lib); - if (decl.type != null) { - decl.type.check(curScope, lib, e); + if (decl != null) { + decl.check(curScope, lib, e); } } From 566eee8a4f44e09f201805019d70ec528552da9d Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 23:00:35 +0100 Subject: [PATCH 35/46] Generate code for negation factor --- no/uio/ifi/pascal2100/parser/Negation.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/Negation.java b/no/uio/ifi/pascal2100/parser/Negation.java index cd5036b..d2b5254 100644 --- a/no/uio/ifi/pascal2100/parser/Negation.java +++ b/no/uio/ifi/pascal2100/parser/Negation.java @@ -51,6 +51,7 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - f.genInstr("", "xorl", "%1,%eax"); + factor.genCode(f); + f.genInstr("", "xorl", "$1,%eax", "not"); } } From 7d3d7a30de266bfce02c0cd51fb38be26cbe210e Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Mon, 30 Nov 2015 23:30:41 +0100 Subject: [PATCH 36/46] Move block decls code gen to block --- no/uio/ifi/pascal2100/parser/Block.java | 6 ++++++ no/uio/ifi/pascal2100/parser/ProcDecl.java | 4 +--- no/uio/ifi/pascal2100/parser/Program.java | 4 +--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/Block.java b/no/uio/ifi/pascal2100/parser/Block.java index 939c984..a8f693d 100644 --- a/no/uio/ifi/pascal2100/parser/Block.java +++ b/no/uio/ifi/pascal2100/parser/Block.java @@ -204,6 +204,12 @@ int getSize() { return 32 + (varDeclPart != null ? varDeclPart.decls.size() * 4 : 0); } + public void genDeclCode(CodeFile f) { + for (PascalDecl pd : decls.values()) { + pd.genCode(f); + } + } + @Override public void genCode(CodeFile f) { if (stmtList != null) { diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index dda03ac..e201ff8 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -93,9 +93,7 @@ public void prettyPrint() { public void genCode(CodeFile f) { label = f.getLabel("proc$" + name); - for (PascalDecl pd : block.decls.values()) { - pd.genCode(f); - } + block.genDeclCode(f); f.genInstr(label, ""); f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index 210d08d..3aa9ca4 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -75,9 +75,7 @@ public void genCode(CodeFile f) { f.genInstr("", "movl", "$0,%eax", "Set status 0"); f.genInstr("", "ret", "", "terminate the program"); - for (PascalDecl pd : progBlock.decls.values()) { - pd.genCode(f); - } + progBlock.genDeclCode(f); f.genInstr(progLabel, ""); f.genInstr("", "enter", "$" + progBlockSize + ",$" + progBlock.blockLevel, "Start of " + name); From a4b2dae4517560361d358c108cb8facc7c314f26 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 10:12:05 +0100 Subject: [PATCH 37/46] Unify checking logic by adding Expression as last param and remove the old func signature --- no/uio/ifi/pascal2100/main/Main.java | 2 +- no/uio/ifi/pascal2100/parser/ArrayType.java | 7 +------ no/uio/ifi/pascal2100/parser/AssignStatm.java | 6 +++--- no/uio/ifi/pascal2100/parser/Block.java | 17 +++++++++-------- no/uio/ifi/pascal2100/parser/CharLiteral.java | 7 +++---- no/uio/ifi/pascal2100/parser/CompoundStatm.java | 4 ++-- no/uio/ifi/pascal2100/parser/ConstDecl.java | 7 +------ no/uio/ifi/pascal2100/parser/ConstDeclPart.java | 4 ++-- no/uio/ifi/pascal2100/parser/Constant.java | 4 +--- no/uio/ifi/pascal2100/parser/EmptyStatm.java | 2 +- no/uio/ifi/pascal2100/parser/EnumLiteral.java | 7 +++---- no/uio/ifi/pascal2100/parser/EnumType.java | 11 +++-------- no/uio/ifi/pascal2100/parser/Expression.java | 13 +++++-------- no/uio/ifi/pascal2100/parser/Factor.java | 2 -- .../ifi/pascal2100/parser/FactorOperator.java | 2 +- no/uio/ifi/pascal2100/parser/IfStatm.java | 8 ++++---- no/uio/ifi/pascal2100/parser/InnerExpr.java | 5 ----- no/uio/ifi/pascal2100/parser/NamedConst.java | 5 ----- no/uio/ifi/pascal2100/parser/Negation.java | 5 ----- no/uio/ifi/pascal2100/parser/NumberLiteral.java | 9 ++------- no/uio/ifi/pascal2100/parser/ParamDecl.java | 7 +------ no/uio/ifi/pascal2100/parser/ParamDeclList.java | 4 ++-- no/uio/ifi/pascal2100/parser/PascalDecl.java | 2 -- no/uio/ifi/pascal2100/parser/PascalSyntax.java | 2 +- .../ifi/pascal2100/parser/PrefixOperator.java | 2 +- no/uio/ifi/pascal2100/parser/ProcCallStatm.java | 4 ++-- no/uio/ifi/pascal2100/parser/ProcDecl.java | 9 ++------- no/uio/ifi/pascal2100/parser/Program.java | 7 +------ no/uio/ifi/pascal2100/parser/RangeType.java | 5 ----- no/uio/ifi/pascal2100/parser/RelOperator.java | 2 +- no/uio/ifi/pascal2100/parser/SimpleExpr.java | 7 +------ no/uio/ifi/pascal2100/parser/StatmList.java | 4 ++-- no/uio/ifi/pascal2100/parser/StringLiteral.java | 9 ++++----- no/uio/ifi/pascal2100/parser/Term.java | 7 +------ no/uio/ifi/pascal2100/parser/TermOperator.java | 2 +- no/uio/ifi/pascal2100/parser/Type.java | 2 -- no/uio/ifi/pascal2100/parser/TypeDecl.java | 7 +------ no/uio/ifi/pascal2100/parser/TypeDeclPart.java | 4 ++-- no/uio/ifi/pascal2100/parser/TypeName.java | 7 +------ no/uio/ifi/pascal2100/parser/VarDecl.java | 7 +------ no/uio/ifi/pascal2100/parser/VarDeclPart.java | 4 ++-- no/uio/ifi/pascal2100/parser/Variable.java | 9 ++------- no/uio/ifi/pascal2100/parser/WhileStatm.java | 6 +++--- 43 files changed, 74 insertions(+), 172 deletions(-) diff --git a/no/uio/ifi/pascal2100/main/Main.java b/no/uio/ifi/pascal2100/main/Main.java index b44d6a6..ba536ed 100644 --- a/no/uio/ifi/pascal2100/main/Main.java +++ b/no/uio/ifi/pascal2100/main/Main.java @@ -113,7 +113,7 @@ private static void doRunRealCompiler(Scanner s) { System.out.print(" checking..."); library = new Library(); - prog.check(library, library); + prog.check(library, library, null); System.out.print(" generating code..."); CodeFile code = new CodeFile(baseFileName+".s"); diff --git a/no/uio/ifi/pascal2100/parser/ArrayType.java b/no/uio/ifi/pascal2100/parser/ArrayType.java index 93f21ed..695e534 100644 --- a/no/uio/ifi/pascal2100/parser/ArrayType.java +++ b/no/uio/ifi/pascal2100/parser/ArrayType.java @@ -65,15 +65,10 @@ public int getHigh() { } public void check(Block curScope, Library lib, Expression e) { - type.check(curScope, lib); + type.check(curScope, lib, e); ofType.check(curScope, lib, e); } - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); - } - void prettyPrint() { Main.log.prettyPrint("array ["); type.prettyPrint(); diff --git a/no/uio/ifi/pascal2100/parser/AssignStatm.java b/no/uio/ifi/pascal2100/parser/AssignStatm.java index 5cc9baa..aa6bd61 100644 --- a/no/uio/ifi/pascal2100/parser/AssignStatm.java +++ b/no/uio/ifi/pascal2100/parser/AssignStatm.java @@ -34,9 +34,9 @@ public static AssignStatm parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { - var.check(curScope, lib); - expr.check(curScope, lib); + public void check(Block curScope, Library lib, Expression e) { + var.check(curScope, lib, e); + expr.check(curScope, lib, e); } void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/Block.java b/no/uio/ifi/pascal2100/parser/Block.java index a8f693d..f7529cf 100644 --- a/no/uio/ifi/pascal2100/parser/Block.java +++ b/no/uio/ifi/pascal2100/parser/Block.java @@ -109,19 +109,20 @@ public void addDecl(String id, PascalDecl decl) { * @param outerScope * @param curScope * @param lib + * @param e */ - public void check(Block outerScope, Block curScope, Library lib) { + public void check(Block outerScope, Block curScope, Library lib, Expression e) { curScope.blockLevel = outerScope.blockLevel + 1; curScope.outerScope = outerScope; - check(curScope, lib); + check(curScope, lib, e); } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression e) { // Variable declarations if (varDeclPart != null) { - varDeclPart.check(this, lib); + varDeclPart.check(this, lib, e); VarDecl vd; for (int i = 0; i < varDeclPart.decls.size(); i++) { @@ -136,7 +137,7 @@ public void check(Block curScope, Library lib) { // Constant declarations if (constDeclPart != null) { - constDeclPart.check(this, lib); + constDeclPart.check(this, lib, e); for (ConstDecl cd: constDeclPart.decls) { this.addDecl(cd.name, cd); @@ -145,7 +146,7 @@ public void check(Block curScope, Library lib) { // Type declarations if (typeDeclPart != null) { - typeDeclPart.check(this, lib); + typeDeclPart.check(this, lib, e); for (TypeDecl td: typeDeclPart.decls) { this.addDecl(td.name.name, td); @@ -154,10 +155,10 @@ public void check(Block curScope, Library lib) { // Procedure declarations for (ProcDecl pd : procDeclList) { - pd.check(curScope, lib); + pd.check(curScope, lib, e); } - stmtList.check(this, lib); + stmtList.check(this, lib, e); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/CharLiteral.java b/no/uio/ifi/pascal2100/parser/CharLiteral.java index c582cef..c1ed0ce 100644 --- a/no/uio/ifi/pascal2100/parser/CharLiteral.java +++ b/no/uio/ifi/pascal2100/parser/CharLiteral.java @@ -31,12 +31,11 @@ public static CharLiteral parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { - e.isChar = true; + if (e != null) { + e.isChar = true; + } } - @Override - public void check(Block curScope, Library lib) { } - @Override void prettyPrint() { Main.log.prettyPrint("'" + String.valueOf(val) + "'"); diff --git a/no/uio/ifi/pascal2100/parser/CompoundStatm.java b/no/uio/ifi/pascal2100/parser/CompoundStatm.java index cf1d82c..76ab2da 100644 --- a/no/uio/ifi/pascal2100/parser/CompoundStatm.java +++ b/no/uio/ifi/pascal2100/parser/CompoundStatm.java @@ -34,8 +34,8 @@ public static CompoundStatm parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { - stmtList.check(curScope, lib); + public void check(Block curScope, Library lib, Expression e) { + stmtList.check(curScope, lib, e); } @Override diff --git a/no/uio/ifi/pascal2100/parser/ConstDecl.java b/no/uio/ifi/pascal2100/parser/ConstDecl.java index 1a65d0d..688d3a5 100644 --- a/no/uio/ifi/pascal2100/parser/ConstDecl.java +++ b/no/uio/ifi/pascal2100/parser/ConstDecl.java @@ -37,12 +37,7 @@ public static ConstDecl parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { - constant.check(curScope, lib, e != null ? e : null); - } - - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); + constant.check(curScope, lib, e); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/ConstDeclPart.java b/no/uio/ifi/pascal2100/parser/ConstDeclPart.java index 415b24d..ab50ebe 100644 --- a/no/uio/ifi/pascal2100/parser/ConstDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/ConstDeclPart.java @@ -37,9 +37,9 @@ public static ConstDeclPart parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression e) { for (ConstDecl cd : decls) { - cd.check(curScope, lib); + cd.check(curScope, lib, e); } } diff --git a/no/uio/ifi/pascal2100/parser/Constant.java b/no/uio/ifi/pascal2100/parser/Constant.java index cb4d294..4fdbde1 100644 --- a/no/uio/ifi/pascal2100/parser/Constant.java +++ b/no/uio/ifi/pascal2100/parser/Constant.java @@ -3,7 +3,7 @@ import no.uio.ifi.pascal2100.scanner.Scanner; abstract class Constant extends Factor { - Constant(int lNum) { + Constant(int lNum) { super(lNum); } @@ -30,6 +30,4 @@ static Constant parse(Scanner s) { leaveParser("constant"); return c; } - - abstract void check(Block curScope, Library lib, Expression e); } diff --git a/no/uio/ifi/pascal2100/parser/EmptyStatm.java b/no/uio/ifi/pascal2100/parser/EmptyStatm.java index f7b0605..b7f7abf 100644 --- a/no/uio/ifi/pascal2100/parser/EmptyStatm.java +++ b/no/uio/ifi/pascal2100/parser/EmptyStatm.java @@ -28,7 +28,7 @@ public static EmptyStatm parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { } + public void check(Block curScope, Library lib, Expression e) { } @Override void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/EnumLiteral.java b/no/uio/ifi/pascal2100/parser/EnumLiteral.java index d2f612a..aa21803 100644 --- a/no/uio/ifi/pascal2100/parser/EnumLiteral.java +++ b/no/uio/ifi/pascal2100/parser/EnumLiteral.java @@ -30,12 +30,11 @@ public static EnumLiteral parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { - e.isNumeric = true; + if (e != null) { + e.isNumeric = true; + } } - @Override - public void check(Block curScope, Library lib) { } - public void prettyPrint() { Main.log.prettyPrint(name); } diff --git a/no/uio/ifi/pascal2100/parser/EnumType.java b/no/uio/ifi/pascal2100/parser/EnumType.java index 0407fe4..299fff6 100644 --- a/no/uio/ifi/pascal2100/parser/EnumType.java +++ b/no/uio/ifi/pascal2100/parser/EnumType.java @@ -38,20 +38,15 @@ public static EnumType parse(Scanner s) { } public void check(Block curScope, Library lib, Expression e) { - check(curScope, lib); + for (EnumLiteral el : literals) { + el.check(curScope, lib, e); + } if (e != null) { e.isNumeric = true; } } - @Override - public void check(Block curScope, Library lib) { - for (EnumLiteral el : literals) { - el.check(curScope, lib); - } - } - void prettyPrint() { Main.log.prettyPrint("("); diff --git a/no/uio/ifi/pascal2100/parser/Expression.java b/no/uio/ifi/pascal2100/parser/Expression.java index 3a760ca..4b9a1f9 100644 --- a/no/uio/ifi/pascal2100/parser/Expression.java +++ b/no/uio/ifi/pascal2100/parser/Expression.java @@ -57,19 +57,16 @@ public boolean isNumeric() { } public void check(Block curScope, Library lib, Expression e) { - leading.check(curScope, lib, e); + Expression checkExpr = e != null ? e : this; + + leading.check(curScope, lib, checkExpr); if (relOperator != null) { - relOperator.check(curScope, lib); - trailing.check(curScope, lib, e); + relOperator.check(curScope, lib, checkExpr); + trailing.check(curScope, lib, checkExpr); } } - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, this); - } - @Override void prettyPrint() { leading.prettyPrint(); diff --git a/no/uio/ifi/pascal2100/parser/Factor.java b/no/uio/ifi/pascal2100/parser/Factor.java index 9ff45da..00ccc4e 100644 --- a/no/uio/ifi/pascal2100/parser/Factor.java +++ b/no/uio/ifi/pascal2100/parser/Factor.java @@ -42,6 +42,4 @@ static Factor parse(Scanner s) { return f; } - - abstract void check(Block curScope, Library lib, Expression e); } \ No newline at end of file diff --git a/no/uio/ifi/pascal2100/parser/FactorOperator.java b/no/uio/ifi/pascal2100/parser/FactorOperator.java index 28d377d..1113101 100644 --- a/no/uio/ifi/pascal2100/parser/FactorOperator.java +++ b/no/uio/ifi/pascal2100/parser/FactorOperator.java @@ -35,7 +35,7 @@ public static FactorOperator parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { } + public void check(Block curScope, Library lib, Expression e) { } @Override void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/IfStatm.java b/no/uio/ifi/pascal2100/parser/IfStatm.java index 7168163..9b339cb 100644 --- a/no/uio/ifi/pascal2100/parser/IfStatm.java +++ b/no/uio/ifi/pascal2100/parser/IfStatm.java @@ -44,12 +44,12 @@ public static IfStatm parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { - expr.check(curScope, lib); - thenStatm.check(curScope, lib); + public void check(Block curScope, Library lib, Expression e) { + expr.check(curScope, lib, e); + thenStatm.check(curScope, lib, e); if (elseStatm != null) { - elseStatm.check(curScope, lib); + elseStatm.check(curScope, lib, e); } } diff --git a/no/uio/ifi/pascal2100/parser/InnerExpr.java b/no/uio/ifi/pascal2100/parser/InnerExpr.java index c9a688a..83a563f 100644 --- a/no/uio/ifi/pascal2100/parser/InnerExpr.java +++ b/no/uio/ifi/pascal2100/parser/InnerExpr.java @@ -39,11 +39,6 @@ public void check(Block curScope, Library lib, Expression e) { expr.check(curScope, lib, e); } - @Override - public void check(Block curScope, Library lib) { - expr.check(curScope, lib); - } - @Override void prettyPrint() { Main.log.prettyPrint("("); diff --git a/no/uio/ifi/pascal2100/parser/NamedConst.java b/no/uio/ifi/pascal2100/parser/NamedConst.java index 396fb24..9162e3f 100644 --- a/no/uio/ifi/pascal2100/parser/NamedConst.java +++ b/no/uio/ifi/pascal2100/parser/NamedConst.java @@ -36,11 +36,6 @@ void check(Block curScope, Library lib, Expression e) { curScope.findDecl(name, this); } - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); - } - @Override void prettyPrint() { Main.log.prettyPrint(name); diff --git a/no/uio/ifi/pascal2100/parser/Negation.java b/no/uio/ifi/pascal2100/parser/Negation.java index d2b5254..52ee910 100644 --- a/no/uio/ifi/pascal2100/parser/Negation.java +++ b/no/uio/ifi/pascal2100/parser/Negation.java @@ -37,11 +37,6 @@ void check(Block curScope, Library lib, Expression e) { factor.check(curScope, lib, e); } - @Override - public void check(Block curScope, Library lib) { - factor.check(curScope, lib); - } - @Override void prettyPrint() { Main.log.prettyPrint("not "); diff --git a/no/uio/ifi/pascal2100/parser/NumberLiteral.java b/no/uio/ifi/pascal2100/parser/NumberLiteral.java index fdd1bca..384a9ed 100644 --- a/no/uio/ifi/pascal2100/parser/NumberLiteral.java +++ b/no/uio/ifi/pascal2100/parser/NumberLiteral.java @@ -33,16 +33,11 @@ public static NumberLiteral parse(Scanner s) { } public void check(Block curScope, Library lib, Expression e) { - if (e == null) { - return; + if (e != null) { + e.isNumeric = true; } - - e.isNumeric = true; } - @Override - public void check(Block curScope, Library lib) { } - @Override void prettyPrint() { Main.log.prettyPrint(Integer.toString(val)); diff --git a/no/uio/ifi/pascal2100/parser/ParamDecl.java b/no/uio/ifi/pascal2100/parser/ParamDecl.java index 9157c6d..f57f712 100644 --- a/no/uio/ifi/pascal2100/parser/ParamDecl.java +++ b/no/uio/ifi/pascal2100/parser/ParamDecl.java @@ -36,12 +36,7 @@ public static ParamDecl parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { curScope.findDecl(typeName.name, this); - typeName.check(curScope, lib, e != null ? e : null); - } - - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); + typeName.check(curScope, lib, e); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/ParamDeclList.java b/no/uio/ifi/pascal2100/parser/ParamDeclList.java index 04bf68c..01c2a22 100644 --- a/no/uio/ifi/pascal2100/parser/ParamDeclList.java +++ b/no/uio/ifi/pascal2100/parser/ParamDeclList.java @@ -46,9 +46,9 @@ public static ParamDeclList parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression e) { for (ParamDecl pd : decls) { - pd.check(curScope, lib); + pd.check(curScope, lib, e); } } diff --git a/no/uio/ifi/pascal2100/parser/PascalDecl.java b/no/uio/ifi/pascal2100/parser/PascalDecl.java index c411e7e..2883e09 100644 --- a/no/uio/ifi/pascal2100/parser/PascalDecl.java +++ b/no/uio/ifi/pascal2100/parser/PascalDecl.java @@ -39,6 +39,4 @@ public abstract class PascalDecl extends PascalSyntax { abstract void checkWhetherProcedure(PascalSyntax where); abstract void checkWhetherValue(PascalSyntax where); */ - - abstract void check(Block curScope, Library lib, Expression e); } diff --git a/no/uio/ifi/pascal2100/parser/PascalSyntax.java b/no/uio/ifi/pascal2100/parser/PascalSyntax.java index 3043848..37dec9b 100644 --- a/no/uio/ifi/pascal2100/parser/PascalSyntax.java +++ b/no/uio/ifi/pascal2100/parser/PascalSyntax.java @@ -22,7 +22,7 @@ String getSourceLocation() { } } - abstract void check(Block curScope, Library lib); + abstract void check(Block curScope, Library lib, Expression e); //abstract void genCode(CodeFile f); void genCode(CodeFile f) { } diff --git a/no/uio/ifi/pascal2100/parser/PrefixOperator.java b/no/uio/ifi/pascal2100/parser/PrefixOperator.java index fceb71e..21b5041 100644 --- a/no/uio/ifi/pascal2100/parser/PrefixOperator.java +++ b/no/uio/ifi/pascal2100/parser/PrefixOperator.java @@ -36,7 +36,7 @@ public static PrefixOperator parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { } + public void check(Block curScope, Library lib, Expression e) { } @Override void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index 21e9e66..6b4139a 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -56,11 +56,11 @@ public static ProcCallStatm parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression expr) { decl = (ProcDecl) curScope.findDecl(name, this); for (Expression e : exprs) { - e.check(curScope, lib); + e.check(curScope, lib, expr); } } diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index e201ff8..227f963 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -50,11 +50,6 @@ public static ProcDecl parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { - check(curScope, lib); - } - - @Override - public void check(Block curScope, Library lib) { ParamDecl pd; curScope.addDecl(name, this); @@ -69,10 +64,10 @@ public void check(Block curScope, Library lib) { block.addDecl(pd.name, pd); } - paramDeclList.check(curScope, lib); + paramDeclList.check(curScope, lib, e); } - block.check(curScope, block, lib); + block.check(curScope, block, lib, e); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index 3aa9ca4..8a1c9a3 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -45,12 +45,7 @@ public static Program parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { - check(curScope, lib); - } - - @Override - public void check(Block curScope, Library lib) { - progBlock.check(curScope, progBlock, lib); + progBlock.check(curScope, progBlock, lib, e); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/RangeType.java b/no/uio/ifi/pascal2100/parser/RangeType.java index a3ac025..b6407c9 100644 --- a/no/uio/ifi/pascal2100/parser/RangeType.java +++ b/no/uio/ifi/pascal2100/parser/RangeType.java @@ -38,11 +38,6 @@ public void check(Block curScope, Library lib, Expression e) { from.check(curScope, lib, e); } - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); - } - void prettyPrint() { from.prettyPrint(); Main.log.prettyPrint(" .. "); diff --git a/no/uio/ifi/pascal2100/parser/RelOperator.java b/no/uio/ifi/pascal2100/parser/RelOperator.java index 23ae958..dfdbd00 100644 --- a/no/uio/ifi/pascal2100/parser/RelOperator.java +++ b/no/uio/ifi/pascal2100/parser/RelOperator.java @@ -35,7 +35,7 @@ public static RelOperator parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { } + public void check(Block curScope, Library lib, Expression e) { } @Override void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/SimpleExpr.java b/no/uio/ifi/pascal2100/parser/SimpleExpr.java index 6aff642..109dbc7 100644 --- a/no/uio/ifi/pascal2100/parser/SimpleExpr.java +++ b/no/uio/ifi/pascal2100/parser/SimpleExpr.java @@ -48,15 +48,10 @@ public static SimpleExpr parse(Scanner s) { public void check(Block curScope, Library lib, Expression e) { for (Term t : terms) { - t.check(curScope, lib, e != null ? e : null); + t.check(curScope, lib, e); } } - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); - } - @Override void prettyPrint() { Iterator termOpersIter = termOpers.iterator(); diff --git a/no/uio/ifi/pascal2100/parser/StatmList.java b/no/uio/ifi/pascal2100/parser/StatmList.java index 364ca79..287d2bb 100644 --- a/no/uio/ifi/pascal2100/parser/StatmList.java +++ b/no/uio/ifi/pascal2100/parser/StatmList.java @@ -49,9 +49,9 @@ public static StatmList parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression e) { for (Statement s : statements) { - s.check(curScope, lib); + s.check(curScope, lib, e); } } diff --git a/no/uio/ifi/pascal2100/parser/StringLiteral.java b/no/uio/ifi/pascal2100/parser/StringLiteral.java index 9fb9dbd..cd2bd4a 100644 --- a/no/uio/ifi/pascal2100/parser/StringLiteral.java +++ b/no/uio/ifi/pascal2100/parser/StringLiteral.java @@ -30,13 +30,12 @@ public static StringLiteral parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { - e.isString = true; - e.string = this; + if (e != null) { + e.isString = true; + e.string = this; + } } - @Override - public void check(Block curScope, Library lib) { } - @Override void prettyPrint() { Main.log.prettyPrint("'" + val + "'"); diff --git a/no/uio/ifi/pascal2100/parser/Term.java b/no/uio/ifi/pascal2100/parser/Term.java index 837f827..c733085 100644 --- a/no/uio/ifi/pascal2100/parser/Term.java +++ b/no/uio/ifi/pascal2100/parser/Term.java @@ -46,15 +46,10 @@ static Term parse(Scanner s) { public void check(Block curScope, Library lib, Expression e) { for (Factor f : factors) { - f.check(curScope, lib, e != null ? e : null); + f.check(curScope, lib, e); } } - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib); - } - @Override void prettyPrint() { Iterator factorOpersIter = factorOpers.iterator(); diff --git a/no/uio/ifi/pascal2100/parser/TermOperator.java b/no/uio/ifi/pascal2100/parser/TermOperator.java index 45a285a..6fd3c35 100644 --- a/no/uio/ifi/pascal2100/parser/TermOperator.java +++ b/no/uio/ifi/pascal2100/parser/TermOperator.java @@ -35,7 +35,7 @@ public static TermOperator parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { } + public void check(Block curScope, Library lib, Expression e) { } @Override void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/Type.java b/no/uio/ifi/pascal2100/parser/Type.java index 46589f7..2883fa0 100644 --- a/no/uio/ifi/pascal2100/parser/Type.java +++ b/no/uio/ifi/pascal2100/parser/Type.java @@ -35,6 +35,4 @@ public static Type parse(Scanner s) { return t; } - - abstract void check(Block curScope, Library lib, Expression e); } diff --git a/no/uio/ifi/pascal2100/parser/TypeDecl.java b/no/uio/ifi/pascal2100/parser/TypeDecl.java index 71a922a..b4b131b 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDecl.java +++ b/no/uio/ifi/pascal2100/parser/TypeDecl.java @@ -35,12 +35,7 @@ public static TypeDecl parse(Scanner s) { } public void check(Block curScope, Library lib, Expression e) { - type.check(curScope, lib, e != null ? e : null); - } - - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); + type.check(curScope, lib, e); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/TypeDeclPart.java b/no/uio/ifi/pascal2100/parser/TypeDeclPart.java index 3379595..6e48663 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/TypeDeclPart.java @@ -37,9 +37,9 @@ public static TypeDeclPart parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression e) { for (TypeDecl td : decls) { - td.check(curScope, lib); + td.check(curScope, lib, e); } } diff --git a/no/uio/ifi/pascal2100/parser/TypeName.java b/no/uio/ifi/pascal2100/parser/TypeName.java index 0d59e27..40cf8c6 100644 --- a/no/uio/ifi/pascal2100/parser/TypeName.java +++ b/no/uio/ifi/pascal2100/parser/TypeName.java @@ -33,18 +33,13 @@ public static TypeName parse(Scanner s) { } public void check(Block curScope, Library lib, Expression e) { - check(curScope, lib); + decl = curScope.findDecl(name, this); if (decl != null) { decl.check(curScope, lib, e); } } - @Override - public void check(Block curScope, Library lib) { - decl = curScope.findDecl(name, this); - } - void prettyPrint() { Main.log.prettyPrint(name); } diff --git a/no/uio/ifi/pascal2100/parser/VarDecl.java b/no/uio/ifi/pascal2100/parser/VarDecl.java index 6f0d7b2..a9d88a3 100644 --- a/no/uio/ifi/pascal2100/parser/VarDecl.java +++ b/no/uio/ifi/pascal2100/parser/VarDecl.java @@ -37,12 +37,7 @@ public static VarDecl parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { - type.check(curScope, lib, e != null ? e : null); - } - - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); + type.check(curScope, lib, e); } public void prettyPrint() { diff --git a/no/uio/ifi/pascal2100/parser/VarDeclPart.java b/no/uio/ifi/pascal2100/parser/VarDeclPart.java index bb22c32..5349a5d 100644 --- a/no/uio/ifi/pascal2100/parser/VarDeclPart.java +++ b/no/uio/ifi/pascal2100/parser/VarDeclPart.java @@ -37,9 +37,9 @@ public static VarDeclPart parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { + public void check(Block curScope, Library lib, Expression e) { for (VarDecl vd : decls) { - vd.check(curScope, lib); + vd.check(curScope, lib, e); } } diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index 18b6fb4..03a4c7b 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -48,18 +48,13 @@ public static Variable parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression e) { nameDecl = curScope.findDecl(name, this); - nameDecl.check(curScope, lib, e != null ? e : null); + nameDecl.check(curScope, lib, e); if (expr == null) { return; } - expr.check(curScope, lib, e != null ? e : null); - } - - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); + expr.check(curScope, lib, e); } @Override diff --git a/no/uio/ifi/pascal2100/parser/WhileStatm.java b/no/uio/ifi/pascal2100/parser/WhileStatm.java index 3a97294..4780716 100644 --- a/no/uio/ifi/pascal2100/parser/WhileStatm.java +++ b/no/uio/ifi/pascal2100/parser/WhileStatm.java @@ -36,9 +36,9 @@ public static WhileStatm parse(Scanner s) { } @Override - public void check(Block curScope, Library lib) { - expr.check(curScope, lib); - body.check(curScope, lib); + public void check(Block curScope, Library lib, Expression e) { + expr.check(curScope, lib, e); + body.check(curScope, lib, e); } @Override From 769007824e4e1cd1a9697c0094e7e187ef841405 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 21:54:18 +0100 Subject: [PATCH 38/46] Fix bug in enum type parser --- no/uio/ifi/pascal2100/parser/EnumType.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/EnumType.java b/no/uio/ifi/pascal2100/parser/EnumType.java index 299fff6..4dce3b0 100644 --- a/no/uio/ifi/pascal2100/parser/EnumType.java +++ b/no/uio/ifi/pascal2100/parser/EnumType.java @@ -4,7 +4,10 @@ import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; +import no.uio.ifi.pascal2100.scanner.TokenKind; +import static no.uio.ifi.pascal2100.scanner.TokenKind.commaToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.leftParToken; +import static no.uio.ifi.pascal2100.scanner.TokenKind.rightParToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.nameToken; public class EnumType extends Type { @@ -28,9 +31,13 @@ public static EnumType parse(Scanner s) { do { et.literals.add(EnumLiteral.parse(s)); + + if (s.curToken.kind == commaToken) { + s.skip(commaToken); + } } while (s.curToken.kind == nameToken); - s.skip(leftParToken); + s.skip(rightParToken); leaveParser("enum type"); @@ -38,8 +45,15 @@ public static EnumType parse(Scanner s) { } public void check(Block curScope, Library lib, Expression e) { - for (EnumLiteral el : literals) { + EnumLiteral el; + + for (int i = 0; i < literals.size(); i++) { + el = literals.get(i); + el.check(curScope, lib, e); + el.index = i; + + curScope.addDecl(el.name, el); } if (e != null) { From ed942854bae6bea6012eb5d017e71549baa2df71 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 22:36:23 +0100 Subject: [PATCH 39/46] Fix various stuff related to the check flow to avoid infinite loops and multiple addDecl call errors --- no/uio/ifi/pascal2100/main/Main.java | 2 + no/uio/ifi/pascal2100/parser/Block.java | 27 ++++++--- no/uio/ifi/pascal2100/parser/EmptyStatm.java | 7 --- no/uio/ifi/pascal2100/parser/EnumLiteral.java | 4 +- no/uio/ifi/pascal2100/parser/EnumType.java | 1 - .../ifi/pascal2100/parser/FactorOperator.java | 3 - no/uio/ifi/pascal2100/parser/Library.java | 60 +++++++++++-------- .../ifi/pascal2100/parser/PascalSyntax.java | 2 +- .../ifi/pascal2100/parser/PrefixOperator.java | 3 - no/uio/ifi/pascal2100/parser/ProcDecl.java | 6 +- no/uio/ifi/pascal2100/parser/Program.java | 4 +- no/uio/ifi/pascal2100/parser/RelOperator.java | 3 - .../ifi/pascal2100/parser/TermOperator.java | 3 - no/uio/ifi/pascal2100/parser/TypeDecl.java | 1 + 14 files changed, 69 insertions(+), 57 deletions(-) diff --git a/no/uio/ifi/pascal2100/main/Main.java b/no/uio/ifi/pascal2100/main/Main.java index ba536ed..b5f0f43 100644 --- a/no/uio/ifi/pascal2100/main/Main.java +++ b/no/uio/ifi/pascal2100/main/Main.java @@ -113,6 +113,8 @@ private static void doRunRealCompiler(Scanner s) { System.out.print(" checking..."); library = new Library(); + library.check(library, library, null); + prog.check(library, library, null); System.out.print(" generating code..."); diff --git a/no/uio/ifi/pascal2100/parser/Block.java b/no/uio/ifi/pascal2100/parser/Block.java index f7529cf..b5d1ddf 100644 --- a/no/uio/ifi/pascal2100/parser/Block.java +++ b/no/uio/ifi/pascal2100/parser/Block.java @@ -96,6 +96,14 @@ public PascalDecl findDecl(String id, PascalSyntax where) { } public void addDecl(String id, PascalDecl decl) { + // Because we're recursing through the tree, different check paths may call addDecl + // on the same object, but since the AST contains only unique nodes and the tree has been + // validated we can safely skip adding them if we already have the exact same node in + // the decls map for this particular scope + if (decls.containsValue(decl)) { + return; + } + if (decls.containsKey(id.toLowerCase())) { decl.error(id + " declared twice in same block"); } @@ -112,6 +120,13 @@ public void addDecl(String id, PascalDecl decl) { * @param e */ public void check(Block outerScope, Block curScope, Library lib, Expression e) { + // If we're trying to set the outer scope on an block that has the outer scope + // set already, we're about to check the same subtree over again. Return here to + // prevent an infinite loop + if (curScope.outerScope != null) { + return; + } + curScope.blockLevel = outerScope.blockLevel + 1; curScope.outerScope = outerScope; @@ -148,17 +163,21 @@ public void check(Block curScope, Library lib, Expression e) { if (typeDeclPart != null) { typeDeclPart.check(this, lib, e); + System.out.println("type decls at line " + lineNum + ": " + typeDeclPart.decls.size() + " decls"); + for (TypeDecl td: typeDeclPart.decls) { this.addDecl(td.name.name, td); } } + System.out.println("proc decls at line " + lineNum + ": " + procDeclList.size() + " decls"); + // Procedure declarations for (ProcDecl pd : procDeclList) { pd.check(curScope, lib, e); } - stmtList.check(this, lib, e); + stmtList.check(curScope, lib, e); } public void prettyPrint() { @@ -205,12 +224,6 @@ int getSize() { return 32 + (varDeclPart != null ? varDeclPart.decls.size() * 4 : 0); } - public void genDeclCode(CodeFile f) { - for (PascalDecl pd : decls.values()) { - pd.genCode(f); - } - } - @Override public void genCode(CodeFile f) { if (stmtList != null) { diff --git a/no/uio/ifi/pascal2100/parser/EmptyStatm.java b/no/uio/ifi/pascal2100/parser/EmptyStatm.java index b7f7abf..b1921f5 100644 --- a/no/uio/ifi/pascal2100/parser/EmptyStatm.java +++ b/no/uio/ifi/pascal2100/parser/EmptyStatm.java @@ -1,7 +1,6 @@ package no.uio.ifi.pascal2100.parser; import static no.uio.ifi.pascal2100.scanner.TokenKind.semicolonToken; -import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -27,14 +26,8 @@ public static EmptyStatm parse(Scanner s) { return es; } - @Override - public void check(Block curScope, Library lib, Expression e) { } - @Override void prettyPrint() { Main.log.prettyPrint(";"); } - - @Override - void genCode(CodeFile f) { } } diff --git a/no/uio/ifi/pascal2100/parser/EnumLiteral.java b/no/uio/ifi/pascal2100/parser/EnumLiteral.java index aa21803..5b8f238 100644 --- a/no/uio/ifi/pascal2100/parser/EnumLiteral.java +++ b/no/uio/ifi/pascal2100/parser/EnumLiteral.java @@ -6,7 +6,9 @@ import no.uio.ifi.pascal2100.scanner.Scanner; public class EnumLiteral extends PascalDecl { - EnumLiteral(String id, int lNum) { + public int index; + + EnumLiteral(String id, int lNum) { super(id, lNum); } diff --git a/no/uio/ifi/pascal2100/parser/EnumType.java b/no/uio/ifi/pascal2100/parser/EnumType.java index 4dce3b0..a78c63d 100644 --- a/no/uio/ifi/pascal2100/parser/EnumType.java +++ b/no/uio/ifi/pascal2100/parser/EnumType.java @@ -4,7 +4,6 @@ import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; -import no.uio.ifi.pascal2100.scanner.TokenKind; import static no.uio.ifi.pascal2100.scanner.TokenKind.commaToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.leftParToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.rightParToken; diff --git a/no/uio/ifi/pascal2100/parser/FactorOperator.java b/no/uio/ifi/pascal2100/parser/FactorOperator.java index 1113101..e24bafb 100644 --- a/no/uio/ifi/pascal2100/parser/FactorOperator.java +++ b/no/uio/ifi/pascal2100/parser/FactorOperator.java @@ -34,9 +34,6 @@ public static FactorOperator parse(Scanner s) { return fo; } - @Override - public void check(Block curScope, Library lib, Expression e) { } - @Override void prettyPrint() { String symbol; diff --git a/no/uio/ifi/pascal2100/parser/Library.java b/no/uio/ifi/pascal2100/parser/Library.java index 73dc816..470d4cd 100644 --- a/no/uio/ifi/pascal2100/parser/Library.java +++ b/no/uio/ifi/pascal2100/parser/Library.java @@ -4,6 +4,10 @@ public class Library extends Block { public Library() { super(-1); + typeDeclPart = new TypeDeclPart(lineNum); + constDeclPart = new ConstDeclPart(lineNum); + stmtList = new StatmList(lineNum); + addEol(); addChar(); addBoolean(); @@ -12,53 +16,57 @@ public Library() { } void addEol() { - ConstDecl eol = new ConstDecl("eol", -1); - eol.constant = new CharLiteral('\n', -1); + ConstDecl eol = new ConstDecl("eol", lineNum); + eol.constant = new CharLiteral('\n', lineNum); - addDecl(eol.name, eol); + constDeclPart.decls.add(eol); } void addChar() { - RangeType charRange = new RangeType(-1); - charRange.from = new CharLiteral('␀', -1); // NUL - charRange.to = new CharLiteral('␡', -1); // DEL + RangeType charRange = new RangeType(lineNum); + charRange.from = new CharLiteral('␀', lineNum); // NUL + charRange.to = new CharLiteral('␡', lineNum); // DEL - TypeDecl charType = new TypeDecl(-1); - charType.name = new TypeName("char", -1); + TypeDecl charType = new TypeDecl(lineNum); + charType.name = new TypeName("char", lineNum); charType.type = charRange; - addDecl(charType.name.name, charType); + typeDeclPart.decls.add(charType); } void addBoolean() { - EnumType enumType = new EnumType(-1); - enumType.literals.add(new EnumLiteral("false", -1)); - enumType.literals.add(new EnumLiteral("true", -1)); + EnumType enumType = new EnumType(lineNum); + enumType.literals.add(new EnumLiteral("false", lineNum)); + enumType.literals.add(new EnumLiteral("true", lineNum)); - TypeDecl boolType = new TypeDecl(-1); - boolType.name = new TypeName("Boolean", -1); + TypeDecl boolType = new TypeDecl(lineNum); + boolType.name = new TypeName("Boolean", lineNum); boolType.type = enumType; - addDecl(boolType.name.name, boolType); - - for (EnumLiteral el : enumType.literals) { - addDecl(el.name, boolType); - } + typeDeclPart.decls.add(boolType); } void addInteger() { - RangeType integer = new RangeType(-1); - integer.from = new NumberLiteral(-2147483648, -1); - integer.to = new NumberLiteral(2147483647, -1); + RangeType integer = new RangeType(lineNum); + integer.from = new NumberLiteral(-2147483648, lineNum); + integer.to = new NumberLiteral(2147483647, lineNum); - TypeDecl integerType = new TypeDecl(-1); - integerType.name = new TypeName("Integer", -1); + TypeDecl integerType = new TypeDecl(lineNum); + integerType.name = new TypeName("Integer", lineNum); integerType.type = integer; - addDecl(integerType.name.name, integerType); + typeDeclPart.decls.add(integerType); } void addWrite() { - addDecl("write", new ProcDecl("write", -1)); + StatmList sl = new StatmList(lineNum); + + Block b = new Block(lineNum); + b.stmtList = sl; + + ProcDecl write = new ProcDecl("write", lineNum); + write.block = b; + + procDeclList.add(write); } } diff --git a/no/uio/ifi/pascal2100/parser/PascalSyntax.java b/no/uio/ifi/pascal2100/parser/PascalSyntax.java index 37dec9b..a494f8a 100644 --- a/no/uio/ifi/pascal2100/parser/PascalSyntax.java +++ b/no/uio/ifi/pascal2100/parser/PascalSyntax.java @@ -22,7 +22,7 @@ String getSourceLocation() { } } - abstract void check(Block curScope, Library lib, Expression e); + void check(Block curScope, Library lib, Expression e) { } //abstract void genCode(CodeFile f); void genCode(CodeFile f) { } diff --git a/no/uio/ifi/pascal2100/parser/PrefixOperator.java b/no/uio/ifi/pascal2100/parser/PrefixOperator.java index 21b5041..16f1375 100644 --- a/no/uio/ifi/pascal2100/parser/PrefixOperator.java +++ b/no/uio/ifi/pascal2100/parser/PrefixOperator.java @@ -35,9 +35,6 @@ public static PrefixOperator parse(Scanner s) { return t; } - @Override - public void check(Block curScope, Library lib, Expression e) { } - @Override void prettyPrint() { String symbol; diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index 227f963..ba2c39c 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -88,7 +88,11 @@ public void prettyPrint() { public void genCode(CodeFile f) { label = f.getLabel("proc$" + name); - block.genDeclCode(f); + if (paramDeclList != null) { + for (PascalDecl pd : paramDeclList.decls) { + pd.genCode(f); + } + } f.genInstr(label, ""); f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index 8a1c9a3..53cb615 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -70,7 +70,9 @@ public void genCode(CodeFile f) { f.genInstr("", "movl", "$0,%eax", "Set status 0"); f.genInstr("", "ret", "", "terminate the program"); - progBlock.genDeclCode(f); + for (PascalDecl pd : progBlock.decls.values()) { + pd.genCode(f); + } f.genInstr(progLabel, ""); f.genInstr("", "enter", "$" + progBlockSize + ",$" + progBlock.blockLevel, "Start of " + name); diff --git a/no/uio/ifi/pascal2100/parser/RelOperator.java b/no/uio/ifi/pascal2100/parser/RelOperator.java index dfdbd00..5a6e021 100644 --- a/no/uio/ifi/pascal2100/parser/RelOperator.java +++ b/no/uio/ifi/pascal2100/parser/RelOperator.java @@ -34,9 +34,6 @@ public static RelOperator parse(Scanner s) { return r; } - @Override - public void check(Block curScope, Library lib, Expression e) { } - @Override void prettyPrint() { String symbol; diff --git a/no/uio/ifi/pascal2100/parser/TermOperator.java b/no/uio/ifi/pascal2100/parser/TermOperator.java index 6fd3c35..7bb1246 100644 --- a/no/uio/ifi/pascal2100/parser/TermOperator.java +++ b/no/uio/ifi/pascal2100/parser/TermOperator.java @@ -34,9 +34,6 @@ public static TermOperator parse(Scanner s) { return t; } - @Override - public void check(Block curScope, Library lib, Expression e) { } - @Override void prettyPrint() { String symbol; diff --git a/no/uio/ifi/pascal2100/parser/TypeDecl.java b/no/uio/ifi/pascal2100/parser/TypeDecl.java index b4b131b..ffa8a7e 100644 --- a/no/uio/ifi/pascal2100/parser/TypeDecl.java +++ b/no/uio/ifi/pascal2100/parser/TypeDecl.java @@ -36,6 +36,7 @@ public static TypeDecl parse(Scanner s) { public void check(Block curScope, Library lib, Expression e) { type.check(curScope, lib, e); + curScope.addDecl(name.name, this); } public void prettyPrint() { From ea160f0bbdb795471082114c09829fcfe90e367f Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 22:37:01 +0100 Subject: [PATCH 40/46] Add code gen for func decls and func calls --- no/uio/ifi/pascal2100/parser/FuncCall.java | 26 +++++++++++++++------- no/uio/ifi/pascal2100/parser/FuncDecl.java | 24 +++++++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/FuncCall.java b/no/uio/ifi/pascal2100/parser/FuncCall.java index 14e2136..bf4da18 100644 --- a/no/uio/ifi/pascal2100/parser/FuncCall.java +++ b/no/uio/ifi/pascal2100/parser/FuncCall.java @@ -7,12 +7,14 @@ import java.util.LinkedList; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; public class FuncCall extends Factor { public String name; public LinkedList exprs = new LinkedList(); + public FuncDecl decl; FuncCall(String id, int lNum) { super(lNum); @@ -56,18 +58,13 @@ public static FuncCall parse(Scanner s) { @Override public void check(Block curScope, Library lib, Expression expr) { - FuncDecl fd = (FuncDecl) curScope.findDecl(name, this); + decl = (FuncDecl) curScope.findDecl(name, this); for (Expression e : exprs) { - e.check(curScope, lib); + e.check(curScope, lib, expr); } - fd.check(curScope, lib, expr); - } - - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); + decl.check(curScope, lib, expr); } @Override @@ -89,4 +86,17 @@ void prettyPrint() { Main.log.prettyPrint(")"); } } + + public void genCode(CodeFile f) { + for (int i = exprs.size() - 1; i >= 0; i--) { + exprs.get(i).genCode(f); + f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); + } + + f.genInstr("", "call", decl.label); + + if (exprs.size() > 0) { + f.genInstr("", "addl", "$" + exprs.size() * 4 + ",%esp", "Pop parameters"); + } + } } diff --git a/no/uio/ifi/pascal2100/parser/FuncDecl.java b/no/uio/ifi/pascal2100/parser/FuncDecl.java index 8bce102..3c458c9 100644 --- a/no/uio/ifi/pascal2100/parser/FuncDecl.java +++ b/no/uio/ifi/pascal2100/parser/FuncDecl.java @@ -6,6 +6,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.semicolonToken; import static no.uio.ifi.pascal2100.scanner.TokenKind.leftParToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -54,12 +55,7 @@ public void check(Block curScope, Library lib, Expression e) { TypeDecl td = (TypeDecl) curScope.findDecl(typeName.name, this); td.check(curScope, lib, e); - super.check(curScope, lib); - } - - @Override - public void check(Block curScope, Library lib) { - check(curScope, lib, null); + super.check(curScope, lib, null); } public void prettyPrint() { @@ -79,4 +75,20 @@ public void prettyPrint() { Main.log.prettyPrintLn("; {" + name + "}"); } + + @Override + public void genCode(CodeFile f) { + label = f.getLabel("func$" + name); + + for (PascalDecl pd : paramDeclList.decls) { + pd.genCode(f); + } + + f.genInstr(label, ""); + f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); + block.genCode(f); + f.genInstr("", "movl", "-32(%ebp),%eax", "Fetch return value"); + f.genInstr("", "leave", "", "End of " + name); + f.genInstr("", "ret"); + } } From cb547c951624cc7ea9ede08ea9045cd2da7ce0d7 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 22:37:14 +0100 Subject: [PATCH 41/46] Fix enum var references --- no/uio/ifi/pascal2100/parser/Variable.java | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index 03a4c7b..d8cc747 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -70,21 +70,12 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - TypeDecl td; - EnumType et; + EnumLiteral el; // Check if this is a reference to a enumerated type value - if (nameDecl instanceof TypeDecl) { - td = (TypeDecl) nameDecl; - et = (EnumType) td.type; - - for (int i = 0; i < et.literals.size(); i++) { - if (name.equals(et.literals.get(i).name)) { - f.genInstr("", "movl", "$" + i + ",%eax", "enum value " + name + "(=" + i + ")"); - return; - } - } - + if (nameDecl instanceof EnumLiteral) { + el = (EnumLiteral) nameDecl; + f.genInstr("", "movl", "$" + el.index + ",%eax", "enum value " + name + " (=" + el.index + ")"); return; } From 49dffb02a2be01abd4241d4ee446b50f95cb64b7 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 23:04:27 +0100 Subject: [PATCH 42/46] Remove comments --- no/uio/ifi/pascal2100/parser/Block.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/Block.java b/no/uio/ifi/pascal2100/parser/Block.java index b5d1ddf..d6b12ef 100644 --- a/no/uio/ifi/pascal2100/parser/Block.java +++ b/no/uio/ifi/pascal2100/parser/Block.java @@ -163,15 +163,11 @@ public void check(Block curScope, Library lib, Expression e) { if (typeDeclPart != null) { typeDeclPart.check(this, lib, e); - System.out.println("type decls at line " + lineNum + ": " + typeDeclPart.decls.size() + " decls"); - for (TypeDecl td: typeDeclPart.decls) { this.addDecl(td.name.name, td); } } - System.out.println("proc decls at line " + lineNum + ": " + procDeclList.size() + " decls"); - // Procedure declarations for (ProcDecl pd : procDeclList) { pd.check(curScope, lib, e); From 6a75dc4b8a29f05231ec9734d3b5f34b424dad83 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 23:05:22 +0100 Subject: [PATCH 43/46] Fix proc/func generation. Now printing sub-methods correctly --- no/uio/ifi/pascal2100/parser/FuncDecl.java | 4 ++++ no/uio/ifi/pascal2100/parser/ProcDecl.java | 4 ++++ no/uio/ifi/pascal2100/parser/Program.java | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/parser/FuncDecl.java b/no/uio/ifi/pascal2100/parser/FuncDecl.java index 3c458c9..3dd4089 100644 --- a/no/uio/ifi/pascal2100/parser/FuncDecl.java +++ b/no/uio/ifi/pascal2100/parser/FuncDecl.java @@ -80,6 +80,10 @@ public void prettyPrint() { public void genCode(CodeFile f) { label = f.getLabel("func$" + name); + for (ProcDecl pd : block.procDeclList) { + pd.genCode(f); + } + for (PascalDecl pd : paramDeclList.decls) { pd.genCode(f); } diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index ba2c39c..eceac87 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -88,6 +88,10 @@ public void prettyPrint() { public void genCode(CodeFile f) { label = f.getLabel("proc$" + name); + for (ProcDecl pd : block.procDeclList) { + pd.genCode(f); + } + if (paramDeclList != null) { for (PascalDecl pd : paramDeclList.decls) { pd.genCode(f); diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index 53cb615..ded0a21 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -70,7 +70,7 @@ public void genCode(CodeFile f) { f.genInstr("", "movl", "$0,%eax", "Set status 0"); f.genInstr("", "ret", "", "terminate the program"); - for (PascalDecl pd : progBlock.decls.values()) { + for (PascalDecl pd : progBlock.procDeclList) { pd.genCode(f); } From 1c92f94d4a31520fd15e85a2f636a711e8faca6a Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 23:05:42 +0100 Subject: [PATCH 44/46] Lowercase all names in the tokenizer step --- no/uio/ifi/pascal2100/scanner/Scanner.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/no/uio/ifi/pascal2100/scanner/Scanner.java b/no/uio/ifi/pascal2100/scanner/Scanner.java index 8c7c18d..c2fc22f 100644 --- a/no/uio/ifi/pascal2100/scanner/Scanner.java +++ b/no/uio/ifi/pascal2100/scanner/Scanner.java @@ -236,7 +236,7 @@ private boolean testRegexp(Pattern pattern, TokenKind tokenKind) { tok = new Token("", matcher.group(1), getFileLineNum()); break; case nameToken: - tok = new Token(matcher.group(), getFileLineNum()); + tok = new Token(matcher.group().toLowerCase(), getFileLineNum()); break; case intValToken: tok = new Token(Integer.parseInt(matcher.group()), getFileLineNum()); From 38b43c90e00308b0de12bc62cf98e3ab71869042 Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 23:47:04 +0100 Subject: [PATCH 45/46] Tweak code gen formatting to comparing with the reference compiler results easier --- no/uio/ifi/pascal2100/parser/CharLiteral.java | 2 +- no/uio/ifi/pascal2100/parser/EnumLiteral.java | 5 +++++ no/uio/ifi/pascal2100/parser/FactorOperator.java | 8 ++++---- no/uio/ifi/pascal2100/parser/FuncCall.java | 9 +++++++-- no/uio/ifi/pascal2100/parser/FuncDecl.java | 2 +- no/uio/ifi/pascal2100/parser/IfStatm.java | 6 ++++-- no/uio/ifi/pascal2100/parser/Negation.java | 2 +- no/uio/ifi/pascal2100/parser/NumberLiteral.java | 2 +- no/uio/ifi/pascal2100/parser/PrefixOperator.java | 2 +- no/uio/ifi/pascal2100/parser/ProcCallStatm.java | 13 +++++++++---- no/uio/ifi/pascal2100/parser/ProcDecl.java | 2 +- no/uio/ifi/pascal2100/parser/Program.java | 2 +- no/uio/ifi/pascal2100/parser/TermOperator.java | 6 +++--- no/uio/ifi/pascal2100/parser/Variable.java | 5 ++--- 14 files changed, 41 insertions(+), 25 deletions(-) diff --git a/no/uio/ifi/pascal2100/parser/CharLiteral.java b/no/uio/ifi/pascal2100/parser/CharLiteral.java index c1ed0ce..d7d932a 100644 --- a/no/uio/ifi/pascal2100/parser/CharLiteral.java +++ b/no/uio/ifi/pascal2100/parser/CharLiteral.java @@ -45,6 +45,6 @@ void prettyPrint() { void genCode(CodeFile f) { int intVal = (int) val; - f.genInstr("", "movl", "$" + intVal + ",%eax", "char " + intVal); + f.genInstr("", "movl", "$" + intVal + ",%eax", " char " + intVal); } } diff --git a/no/uio/ifi/pascal2100/parser/EnumLiteral.java b/no/uio/ifi/pascal2100/parser/EnumLiteral.java index 5b8f238..1807e32 100644 --- a/no/uio/ifi/pascal2100/parser/EnumLiteral.java +++ b/no/uio/ifi/pascal2100/parser/EnumLiteral.java @@ -2,6 +2,7 @@ import static no.uio.ifi.pascal2100.scanner.TokenKind.nameToken; +import no.uio.ifi.pascal2100.main.CodeFile; import no.uio.ifi.pascal2100.main.Main; import no.uio.ifi.pascal2100.scanner.Scanner; @@ -40,4 +41,8 @@ public void check(Block curScope, Library lib, Expression e) { public void prettyPrint() { Main.log.prettyPrint(name); } + + public void genCode(CodeFile f) { + f.genInstr("", "movl", "$" + index + ",%eax", " enum value " + name + " (=" + index + ")"); + } } diff --git a/no/uio/ifi/pascal2100/parser/FactorOperator.java b/no/uio/ifi/pascal2100/parser/FactorOperator.java index e24bafb..3be625c 100644 --- a/no/uio/ifi/pascal2100/parser/FactorOperator.java +++ b/no/uio/ifi/pascal2100/parser/FactorOperator.java @@ -63,19 +63,19 @@ void genCode(CodeFile f) { switch (kind) { case multiplyToken: - f.genInstr("", "imull", "%ecx,%eax", "*"); + f.genInstr("", "imull", "%ecx,%eax", " *"); break; case divToken: f.genInstr("", "cdq"); - f.genInstr("", "idivl", "%ecx", "/"); + f.genInstr("", "idivl", "%ecx", " /"); break; case modToken: f.genInstr("", "cdq"); f.genInstr("", "idivl", "%ecx"); - f.genInstr("", "movl", "%edx,%eax", "mod"); + f.genInstr("", "movl", "%edx,%eax", " mod"); break; default: - f.genInstr("", "andl", "%ecx,%eax", "and"); + f.genInstr("", "andl", "%ecx,%eax", " and"); break; } } diff --git a/no/uio/ifi/pascal2100/parser/FuncCall.java b/no/uio/ifi/pascal2100/parser/FuncCall.java index bf4da18..02be333 100644 --- a/no/uio/ifi/pascal2100/parser/FuncCall.java +++ b/no/uio/ifi/pascal2100/parser/FuncCall.java @@ -90,13 +90,18 @@ void prettyPrint() { public void genCode(CodeFile f) { for (int i = exprs.size() - 1; i >= 0; i--) { exprs.get(i).genCode(f); - f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); + f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1) + "."); } f.genInstr("", "call", decl.label); if (exprs.size() > 0) { - f.genInstr("", "addl", "$" + exprs.size() * 4 + ",%esp", "Pop parameters"); + f.genInstr( + "", + "addl", + "$" + exprs.size() * 4 + ",%esp", + "Pop parameter" + (exprs.size() > 1 ? "s" : "") + "." + ); } } } diff --git a/no/uio/ifi/pascal2100/parser/FuncDecl.java b/no/uio/ifi/pascal2100/parser/FuncDecl.java index 3dd4089..25a5cf2 100644 --- a/no/uio/ifi/pascal2100/parser/FuncDecl.java +++ b/no/uio/ifi/pascal2100/parser/FuncDecl.java @@ -89,7 +89,7 @@ public void genCode(CodeFile f) { } f.genInstr(label, ""); - f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); + f.genInstr("", "enter", "$" + block.getSize() + ",$" + block.blockLevel, "Start of " + name); block.genCode(f); f.genInstr("", "movl", "-32(%ebp),%eax", "Fetch return value"); f.genInstr("", "leave", "", "End of " + name); diff --git a/no/uio/ifi/pascal2100/parser/IfStatm.java b/no/uio/ifi/pascal2100/parser/IfStatm.java index 9b339cb..9fd3793 100644 --- a/no/uio/ifi/pascal2100/parser/IfStatm.java +++ b/no/uio/ifi/pascal2100/parser/IfStatm.java @@ -73,8 +73,8 @@ void prettyPrint() { } void genCode(CodeFile f) { - String elseLabel = f.getLocalLabel(), - endLabel = f.getLocalLabel(); + String endLabel = f.getLocalLabel(); + String elseLabel; f.genInstr("", "", "", "Start if-statement"); expr.genCode(f); @@ -84,6 +84,8 @@ void genCode(CodeFile f) { f.genInstr("", "je", endLabel, ""); thenStatm.genCode(f); } else { + elseLabel = f.getLocalLabel(); + f.genInstr("", "je", elseLabel, ""); thenStatm.genCode(f); f.genInstr("", "jmp", endLabel, ""); diff --git a/no/uio/ifi/pascal2100/parser/Negation.java b/no/uio/ifi/pascal2100/parser/Negation.java index 52ee910..03cda93 100644 --- a/no/uio/ifi/pascal2100/parser/Negation.java +++ b/no/uio/ifi/pascal2100/parser/Negation.java @@ -47,6 +47,6 @@ void prettyPrint() { @Override void genCode(CodeFile f) { factor.genCode(f); - f.genInstr("", "xorl", "$1,%eax", "not"); + f.genInstr("", "xorl", "$1,%eax", " not"); } } diff --git a/no/uio/ifi/pascal2100/parser/NumberLiteral.java b/no/uio/ifi/pascal2100/parser/NumberLiteral.java index 384a9ed..3eef6ed 100644 --- a/no/uio/ifi/pascal2100/parser/NumberLiteral.java +++ b/no/uio/ifi/pascal2100/parser/NumberLiteral.java @@ -45,6 +45,6 @@ void prettyPrint() { @Override void genCode(CodeFile f) { - f.genInstr("", "movl", "$" + val + ",%eax", "" + val); + f.genInstr("", "movl", "$" + val + ",%eax", " " + val); } } diff --git a/no/uio/ifi/pascal2100/parser/PrefixOperator.java b/no/uio/ifi/pascal2100/parser/PrefixOperator.java index 16f1375..05cee19 100644 --- a/no/uio/ifi/pascal2100/parser/PrefixOperator.java +++ b/no/uio/ifi/pascal2100/parser/PrefixOperator.java @@ -57,6 +57,6 @@ void genCode(CodeFile f) { return; } - f.genInstr("", "negl", "%eax", "- (prefix)"); + f.genInstr("", "negl", "%eax", " - (prefix)"); } } diff --git a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java index 6b4139a..e0a9958 100644 --- a/no/uio/ifi/pascal2100/parser/ProcCallStatm.java +++ b/no/uio/ifi/pascal2100/parser/ProcCallStatm.java @@ -106,9 +106,9 @@ private void genWriteCode(CodeFile f) { continue; } - f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); + f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1) + "."); f.genInstr("", "call", funcName); - f.genInstr("", "addl", "$4,%esp", "Pop parameter"); + f.genInstr("", "addl", "$4,%esp", "Pop parameter."); } } @@ -121,13 +121,18 @@ public void genCode(CodeFile f) { for (int i = exprs.size() - 1; i >= 0; i--) { exprs.get(i).genCode(f); - f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1)); + f.genInstr("", "pushl", "%eax", "Push param #" + (i + 1) + "."); } f.genInstr("", "call", decl.label); if (exprs.size() > 0) { - f.genInstr("", "addl", "$" + exprs.size() * 4 + ",%esp", "Pop parameters"); + f.genInstr( + "", + "addl", + "$" + exprs.size() * 4 + ",%esp", + "Pop parameter." + ); } } } diff --git a/no/uio/ifi/pascal2100/parser/ProcDecl.java b/no/uio/ifi/pascal2100/parser/ProcDecl.java index eceac87..2975c6d 100644 --- a/no/uio/ifi/pascal2100/parser/ProcDecl.java +++ b/no/uio/ifi/pascal2100/parser/ProcDecl.java @@ -99,7 +99,7 @@ public void genCode(CodeFile f) { } f.genInstr(label, ""); - f.genInstr("", "enter", "$" + block.getSize() + "," + block.blockLevel, "Start of " + name); + f.genInstr("", "enter", "$" + block.getSize() + ",$" + block.blockLevel, "Start of " + name); block.genCode(f); f.genInstr("", "leave", "", "End of " + name); f.genInstr("", "ret"); diff --git a/no/uio/ifi/pascal2100/parser/Program.java b/no/uio/ifi/pascal2100/parser/Program.java index ded0a21..09c929b 100644 --- a/no/uio/ifi/pascal2100/parser/Program.java +++ b/no/uio/ifi/pascal2100/parser/Program.java @@ -67,7 +67,7 @@ public void genCode(CodeFile f) { f.genInstr("_main", "", ""); f.genInstr("main", "call", progLabel, "Start program"); - f.genInstr("", "movl", "$0,%eax", "Set status 0"); + f.genInstr("", "movl", "$0,%eax", "Set status 0 and"); f.genInstr("", "ret", "", "terminate the program"); for (PascalDecl pd : progBlock.procDeclList) { diff --git a/no/uio/ifi/pascal2100/parser/TermOperator.java b/no/uio/ifi/pascal2100/parser/TermOperator.java index 7bb1246..f38f817 100644 --- a/no/uio/ifi/pascal2100/parser/TermOperator.java +++ b/no/uio/ifi/pascal2100/parser/TermOperator.java @@ -60,13 +60,13 @@ void genCode(CodeFile f) { switch (kind) { case addToken: - f.genInstr("", "addl", "%ecx,%eax", "+"); + f.genInstr("", "addl", "%ecx,%eax", " +"); break; case subtractToken: - f.genInstr("", "subl", "%ecx,%eax", "-"); + f.genInstr("", "subl", "%ecx,%eax", " -"); break; default: - f.genInstr("", "orl", "%ecx,%eax", "or"); + f.genInstr("", "orl", "%ecx,%eax", " or"); break; } } diff --git a/no/uio/ifi/pascal2100/parser/Variable.java b/no/uio/ifi/pascal2100/parser/Variable.java index d8cc747..c0487f6 100644 --- a/no/uio/ifi/pascal2100/parser/Variable.java +++ b/no/uio/ifi/pascal2100/parser/Variable.java @@ -74,8 +74,7 @@ void genCode(CodeFile f) { // Check if this is a reference to a enumerated type value if (nameDecl instanceof EnumLiteral) { - el = (EnumLiteral) nameDecl; - f.genInstr("", "movl", "$" + el.index + ",%eax", "enum value " + name + " (=" + el.index + ")"); + nameDecl.genCode(f); return; } @@ -86,6 +85,6 @@ void genCode(CodeFile f) { } f.genInstr("", "movl", "-" + (4 * nameDecl.declLevel) + "(%ebp),%edx"); - f.genInstr("", "movl", nameDecl.declOffset + "(%edx),%eax", name); + f.genInstr("", "movl", nameDecl.declOffset + "(%edx),%eax", " " + name); } } From 7e58325a20acb669d8c02fbf30317e1ee01f373a Mon Sep 17 00:00:00 2001 From: Kristoffer Brabrand Date: Tue, 1 Dec 2015 23:48:24 +0100 Subject: [PATCH 46/46] Add library files --- libpas2100.a | Bin 0 -> 2042 bytes libpas2100.o | Bin 0 -> 1864 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 libpas2100.a create mode 100644 libpas2100.o diff --git a/libpas2100.a b/libpas2100.a new file mode 100644 index 0000000000000000000000000000000000000000..f566e8cebbe8af5895f1dea2552ba72b7ffcd4fd GIT binary patch literal 2042 zcmbVMPfrt35T8;6tU#*qpuxjpQX^5;?n;Rgf>na-!4QKXdeG2qS!^TyligQE6XLC)_2P)bP1&l0Hkrf3%T<-Plp?QHFD zFYI@|{ifK?_b&$i^%LQ{w3!UAv$f}KFYI}p1X6AdauLAd{QQh{b*0>Hh_+R*UE7&- z-FED{6B4l8yi;)UPM&H2oRlCrIpD@_>~t(ptPX6x*q!VfJ**PXmtfdb2YE-tV*mgE literal 0 HcmV?d00001 diff --git a/libpas2100.o b/libpas2100.o new file mode 100644 index 0000000000000000000000000000000000000000..85262941ab4ed8e65f9dd49e0e005c4b697a0e94 GIT binary patch literal 1864 zcmbVMPiqrV5TDJ~HrBR8@u1X06e1SwV|S$oi|A^I-GdaV1U)F}Cf%A~^M~CHMxl5R zgr%2$15bX3-V_AIvlqXB;K7Tx9s+e{_f50BY&>+rdo%N!-@JJ{vzxb-hYxa^1}qxf zfsw~3Kz?du+p%fGJj_DXiGDfn{uH9!;a+7Jefv$Z{qJ8C{Oc#e4`@>vPPBXI>{Sk( zD1%hmf>HpmRxYm=uT|^)PS`K52uqmDmer44YbgPWmSL`#hG|d@fb)q0?ac;ggMzj= zHirpq0L2S993!)wbApCCPG`j7!*TGn)8+u}Gy}BHPZsR2qD9vluwtctl zQOW-a&29?*9%XU#%*g`i_{$OiM{D zOAgMq)p-GMmd-%Q;hM!YJW6DVKor8#UAq~h34ac4=C4bf`8=7Y6ZpjxPPJKVX%I$> z`?IOw+y@DtSQRmo-aU1qHew{TL>=$MP0J!W^tRb)5N$^`U?A}`s417RA^jum zq->S@1Y?A$_x~w#xc__v_n-X@G_vORSvJ&>M4BJvaz4+Gz8|&sl=4Vk)Fme+>ix6s cPV@sw@NPgqv{yaexJJS}Sde^O(hssI20 literal 0 HcmV?d00001