1 Commits

Author SHA1 Message Date
Gašper Dobrovoljc
73e1113764 Skip right expression evaluation in OR if left returns true 2025-06-03 18:26:19 +02:00
3 changed files with 43 additions and 84 deletions

View File

@@ -70,27 +70,3 @@ initializer -> INTCONST intconstmult | CHARCONST | STRINGCONST .
intconstmult -> MUL const | . intconstmult -> MUL const | .
const -> INTCONST | CHARCONST | STRINGCONST . const -> INTCONST | CHARCONST | STRINGCONST .
--------------------------------------------------------------------
//LEFT ASOC.
exp0 = exp0 op other | other
|
v
exp0 = other exp0'
exp0' = op other exp0' | .
//RIGHT ASOC.
exp0 = other op exp0 | other
|
v
exp0 = other exp0'
exp0' = op exp0 | .

View File

@@ -1,13 +1,12 @@
fun putstr(n) fun putstr(s)
fun putint(b)
fun a() = putstr("a called\n\00"), 40 fun f1() =
fun b() = putstr("b called\n\00"), 7 putstr("funkcija 1\n\00"),
0
fun f2() =
putstr("funkcija 2\n\00"),
0
fun main() = fun main() =
putint(10 % 7), f1() || f2()
putstr("\n\00"),
putint(30 % 25),
putstr("\n\00"),
putint(a() % b()),
putstr("\n\00")

View File

@@ -227,46 +227,10 @@ public class CodeGen {
@Override @Override
public List<PDM.CodeInstr> visit(AST.BinExpr binExpr, Mem.Frame frame) { public List<PDM.CodeInstr> visit(AST.BinExpr binExpr, Mem.Frame frame) {
Report.Locatable loc = attrAST.attrLoc.get(binExpr); Report.Location loc = attrAST.attrLoc.get(binExpr).location();
List<PDM.CodeInstr> code = new ArrayList<>(); List<PDM.CodeInstr> code = new ArrayList<>();
if (binExpr.oper == AST.BinExpr.Oper.MOD) {
// a - a / b * b
String tmpLabel = "$tmp@" + loc.location().begLine() + ":" + loc.location().begColumn();
code.addAll(binExpr.fstExpr.accept(this, frame));
// dup a
code.add(new PDM.REGN(PDM.REGN.Reg.SP, loc));
code.add(new PDM.LOAD(loc));
code.addAll(binExpr.sndExpr.accept(this, frame));
// dup b
code.add(new PDM.REGN(PDM.REGN.Reg.SP, loc));
code.add(new PDM.LOAD(loc));
// save b
code.add(new PDM.NAME(tmpLabel, loc));
code.add(new PDM.SAVE(loc));
code.add(new PDM.OPER(PDM.OPER.Oper.DIV, loc));
// load second b
code.add(new PDM.NAME(tmpLabel, loc));
code.add(new PDM.LOAD(loc));
code.add(new PDM.OPER(PDM.OPER.Oper.MUL, loc));
code.add(new PDM.OPER(PDM.OPER.Oper.SUB, loc));
List<PDM.DataInstr> data = new ArrayList<>();
data.add(new PDM.LABEL(tmpLabel, loc));
data.add(new PDM.DATA(0, loc));
attrAST.attrData.put(binExpr, data);
} else {
code.addAll(binExpr.fstExpr.accept(this, frame));
code.addAll(binExpr.sndExpr.accept(this, frame));
PDM.OPER.Oper oper = switch (binExpr.oper) { PDM.OPER.Oper oper = switch (binExpr.oper) {
case OR -> PDM.OPER.Oper.OR; case OR -> PDM.OPER.Oper.OR;
case AND -> PDM.OPER.Oper.AND; case AND -> PDM.OPER.Oper.AND;
@@ -283,6 +247,32 @@ public class CodeGen {
case MOD -> PDM.OPER.Oper.MOD; case MOD -> PDM.OPER.Oper.MOD;
}; };
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)); code.add(new PDM.OPER(oper, loc));
} }
@@ -717,12 +707,6 @@ public class CodeGen {
return null; return null;
} }
@Override
public Object visit(AST.BinExpr binExpr, Object arg) {
List<PDM.DataInstr> data = attrAST.attrData.get(binExpr);
if (data != null) dataSegment.addAll(data);
return null;
}
} }
} }