From 73e1113764a1f7d336a09b8dc70d71ec5049bf6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Tue, 3 Jun 2025 18:26:19 +0200 Subject: [PATCH] Skip right expression evaluation in OR if left returns true --- .gitignore | 1 - prg/test.pins25 | 12 ++++++++++++ src/pins25/phase/CodeGen.java | 34 +++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 prg/test.pins25 diff --git a/.gitignore b/.gitignore index 585d773..5545717 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ out/ *.zip prg/**/*.ast prg/**/*.out -prg/**/*.pins25 \ No newline at end of file diff --git a/prg/test.pins25 b/prg/test.pins25 new file mode 100644 index 0000000..e5bfa86 --- /dev/null +++ b/prg/test.pins25 @@ -0,0 +1,12 @@ +fun putstr(s) + +fun f1() = + putstr("funkcija 1\n\00"), + 0 + +fun f2() = + putstr("funkcija 2\n\00"), + 0 + +fun main() = + f1() || f2() \ No newline at end of file diff --git a/src/pins25/phase/CodeGen.java b/src/pins25/phase/CodeGen.java index 0df12ac..d46a9a0 100644 --- a/src/pins25/phase/CodeGen.java +++ b/src/pins25/phase/CodeGen.java @@ -227,13 +227,10 @@ public class CodeGen { @Override public List visit(AST.BinExpr binExpr, Mem.Frame frame) { - Report.Locatable loc = attrAST.attrLoc.get(binExpr); + Report.Location loc = attrAST.attrLoc.get(binExpr).location(); List code = new ArrayList<>(); - code.addAll(binExpr.fstExpr.accept(this, frame)); - code.addAll(binExpr.sndExpr.accept(this, frame)); - PDM.OPER.Oper oper = switch (binExpr.oper) { case OR -> PDM.OPER.Oper.OR; case AND -> PDM.OPER.Oper.AND; @@ -250,8 +247,35 @@ public class CodeGen { case MOD -> PDM.OPER.Oper.MOD; }; - code.add(new PDM.OPER(oper, loc)); + if (oper == PDM.OPER.Oper.OR) { + String locStr = loc.begLine() + ":" + loc.begColumn(); + String rightLabel = "$or:right" + locStr; + String skipLabel = "$or:skip@" + locStr; + String endLabel = "$or:end@" + locStr; + code.addAll(binExpr.fstExpr.accept(this, frame)); + code.add(new PDM.NAME(skipLabel, loc)); + code.add(new PDM.NAME(rightLabel, loc)); + code.add(new PDM.CJMP(loc)); + + code.add(new PDM.LABEL(rightLabel, loc)); + code.add(new PDM.PUSH(0, loc)); + code.addAll(binExpr.sndExpr.accept(this, frame)); + code.add(new PDM.OPER(oper, loc)); + code.add(new PDM.NAME(endLabel, loc)); + code.add(new PDM.UJMP(loc)); + + code.add(new PDM.LABEL(skipLabel, loc)); + code.add(new PDM.PUSH(1, loc)); + + code.add(new PDM.LABEL(endLabel, loc)); + } else { + code.addAll(binExpr.fstExpr.accept(this, frame)); + code.addAll(binExpr.sndExpr.accept(this, frame)); + + code.add(new PDM.OPER(oper, loc)); + } + attrAST.attrCode.put(binExpr, code); return code; }