Skip to content

Commit

Permalink
Removed peephole optimizer as it causes bugs
Browse files Browse the repository at this point in the history
Mindcode:

    n += 1

Generated:

    op add n n 1

Optimized:

    op add 0 0 1

Oops! I knew there was something fishy with the optimizer... We must be
super careful when optimizing programs.
  • Loading branch information
francois committed Mar 28, 2021
1 parent 2ef61b9 commit 1c6afb3
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 428 deletions.

This file was deleted.

9 changes: 5 additions & 4 deletions src/main/java/info/teksol/mindcode/webapp/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import info.teksol.mindcode.ast.Seq;
import info.teksol.mindcode.grammar.MindcodeLexer;
import info.teksol.mindcode.grammar.MindcodeParser;
import info.teksol.mindcode.mindustry.*;
import info.teksol.mindcode.mindustry.LogicInstruction;
import info.teksol.mindcode.mindustry.LogicInstructionGenerator;
import info.teksol.mindcode.mindustry.LogicInstructionLabelResolver;
import info.teksol.mindcode.mindustry.LogicInstructionPrinter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -146,9 +149,7 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int
final MindcodeParser.ProgramContext context = parser.program();
final Seq prog = AstNodeBuilder.generate(context);
final List<LogicInstruction> result = LogicInstructionLabelResolver.resolve(
LogicInstructionPeepholeOptimizer.optimize(
LogicInstructionGenerator.generateFrom(prog)
)
LogicInstructionGenerator.generateFrom(prog)
);

instructions = LogicInstructionPrinter.toString(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ class LogicInstructionLabelResolverTest extends AbstractAstTest {
@Test
void resolvesAbsoluteAddressesOfLabels() {
assertEquals(
List.of(
new LogicInstruction("jump", "3", "notEqual", "true", "true"),
new LogicInstruction("op", "add", "n", "n", "1"),
new LogicInstruction("jump", "0", "always"),
new LogicInstruction("end")
prettyPrint(
List.of(
new LogicInstruction("jump", "5", "notEqual", "true", "true"),
new LogicInstruction("set", "tmp0", "1"),
new LogicInstruction("op", "add", "tmp1", "n", "tmp0"),
new LogicInstruction("set", "n", "tmp1"),
new LogicInstruction("jump", "0", "always"),
new LogicInstruction("end")
)
),
LogicInstructionLabelResolver.resolve(
LogicInstructionPeepholeOptimizer.optimize(
prettyPrint(
LogicInstructionLabelResolver.resolve(
LogicInstructionGenerator.generateFrom(
(Seq) translateToAst("while true\nn = n + 1\nend\n")
)
Expand All @@ -33,38 +37,58 @@ void resolvesFunctionCallsAndReturns() {
assertEquals(
prettyPrint(
List.of(
new LogicInstruction("write", "63", "cell1", "63"),
new LogicInstruction("read", "tmp6", "cell1", "63"),
new LogicInstruction("op", "sub", "tmp6", "tmp6", "1"),
new LogicInstruction("write", "6", "cell1", "tmp6"),
new LogicInstruction("write", "tmp6", "cell1", "63"),
new LogicInstruction("set", "@counter", "12"),
new LogicInstruction("read", "tmp16", "cell1", "63"), // label1
new LogicInstruction("read", "tmp17", "cell1", "tmp16"),
new LogicInstruction("set", "tmp2", "63"),
new LogicInstruction("set", "tmp3", "63"),
new LogicInstruction("write", "tmp2", "cell1", "tmp3"),
new LogicInstruction("set", "tmp5", "63"),
new LogicInstruction("read", "tmp6", "cell1", "tmp5"),
new LogicInstruction("set", "tmp4", "tmp6"),
new LogicInstruction("set", "tmp7", "1"),
new LogicInstruction("op", "sub", "tmp8", "tmp4", "tmp7"),
new LogicInstruction("set", "tmp4", "tmp8"),
new LogicInstruction("write", "13", "cell1", "tmp4"),
new LogicInstruction("set", "tmp12", "63"),
new LogicInstruction("write", "tmp4", "cell1", "tmp12"),
new LogicInstruction("set", "@counter", "24"),
new LogicInstruction("set", "tmp15", "63"),
new LogicInstruction("read", "tmp16", "cell1", "tmp15"),
new LogicInstruction("set", "tmp14", "tmp16"),
new LogicInstruction("read", "tmp17", "cell1", "tmp14"),
new LogicInstruction("set", "tmp13", "tmp17"),
new LogicInstruction("op", "add", "tmp16", "tmp16", "1"),
new LogicInstruction("write", "tmp16", "cell1", "63"),
new LogicInstruction("set", "tmp18", "1"),
new LogicInstruction("op", "add", "tmp19", "tmp14", "tmp18"),
new LogicInstruction("set", "tmp14", "tmp19"),
new LogicInstruction("set", "tmp22", "63"),
new LogicInstruction("write", "tmp14", "cell1", "tmp22"),
new LogicInstruction("end"),
new LogicInstruction("set", "tmp23", "1"), // label0
new LogicInstruction("read", "tmp27", "cell1", "63"),
new LogicInstruction("read", "tmp28", "cell1", "tmp27"),
new LogicInstruction("set", "tmp23", "1"),
new LogicInstruction("set", "tmp26", "63"),
new LogicInstruction("read", "tmp27", "cell1", "tmp26"),
new LogicInstruction("set", "tmp25", "tmp27"),
new LogicInstruction("read", "tmp28", "cell1", "tmp25"),
new LogicInstruction("set", "tmp24", "tmp28"),
new LogicInstruction("op", "add", "tmp27", "tmp27", "1"),
new LogicInstruction("write", "tmp27", "cell1", "63"),
new LogicInstruction("read", "tmp36", "cell1", "63"),
new LogicInstruction("op", "sub", "tmp36", "tmp36", "1"),
new LogicInstruction("write", "tmp23", "cell1", "tmp36"),
new LogicInstruction("write", "tmp36", "cell1", "63"),
new LogicInstruction("set", "tmp29", "1"),
new LogicInstruction("op", "add", "tmp30", "tmp25", "tmp29"),
new LogicInstruction("set", "tmp25", "tmp30"),
new LogicInstruction("set", "tmp33", "63"),
new LogicInstruction("write", "tmp25", "cell1", "tmp33"),
new LogicInstruction("set", "tmp35", "63"),
new LogicInstruction("read", "tmp36", "cell1", "tmp35"),
new LogicInstruction("set", "tmp34", "tmp36"),
new LogicInstruction("set", "tmp37", "1"),
new LogicInstruction("op", "sub", "tmp38", "tmp34", "tmp37"),
new LogicInstruction("set", "tmp34", "tmp38"),
new LogicInstruction("write", "tmp23", "cell1, tmp34"),
new LogicInstruction("set", "tmp42, 63"),
new LogicInstruction("write", "tmp34", "cell1", "tmp42"),
new LogicInstruction("set", "@counter", "tmp24"),
new LogicInstruction("end")
)
),
prettyPrint(
LogicInstructionLabelResolver.resolve(
LogicInstructionPeepholeOptimizer.optimize(
LogicInstructionGenerator.generateFrom(
(Seq) translateToAst("allocate stack in cell1\n\ndef bar\n1\nend\n\nbar()")
)
LogicInstructionGenerator.generateFrom(
(Seq) translateToAst("allocate stack in cell1\n\ndef bar\n1\nend\n\nbar()")
)
)
)
Expand Down

0 comments on commit 1c6afb3

Please sign in to comment.