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

@@ -69,28 +69,4 @@ restinits -> COMMA initializer restinits | .
initializer -> INTCONST intconstmult | CHARCONST | STRINGCONST .
intconstmult -> MUL const | .
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 | .
const -> INTCONST | CHARCONST | STRINGCONST .

View File

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

View File

@@ -227,65 +227,55 @@ public class CodeGen {
@Override
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<>();
if (binExpr.oper == AST.BinExpr.Oper.MOD) {
// a - a / b * b
PDM.OPER.Oper oper = switch (binExpr.oper) {
case OR -> PDM.OPER.Oper.OR;
case AND -> PDM.OPER.Oper.AND;
case EQU -> PDM.OPER.Oper.EQU;
case NEQ -> PDM.OPER.Oper.NEQ;
case GTH -> PDM.OPER.Oper.GTH;
case LTH -> PDM.OPER.Oper.LTH;
case GEQ -> PDM.OPER.Oper.GEQ;
case LEQ -> PDM.OPER.Oper.LEQ;
case ADD -> PDM.OPER.Oper.ADD;
case SUB -> PDM.OPER.Oper.SUB;
case MUL -> PDM.OPER.Oper.MUL;
case DIV -> PDM.OPER.Oper.DIV;
case MOD -> PDM.OPER.Oper.MOD;
};
String tmpLabel = "$tmp@" + loc.location().begLine() + ":" + loc.location().begColumn();
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));
// dup a
code.add(new PDM.REGN(PDM.REGN.Reg.SP, loc));
code.add(new PDM.LOAD(loc));
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));
// 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(oper, loc));
code.add(new PDM.NAME(endLabel, loc));
code.add(new PDM.UJMP(loc));
code.add(new PDM.OPER(PDM.OPER.Oper.DIV, loc));
code.add(new PDM.LABEL(skipLabel, loc));
code.add(new PDM.PUSH(1, 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);
code.add(new PDM.LABEL(endLabel, loc));
} else {
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;
case EQU -> PDM.OPER.Oper.EQU;
case NEQ -> PDM.OPER.Oper.NEQ;
case GTH -> PDM.OPER.Oper.GTH;
case LTH -> PDM.OPER.Oper.LTH;
case GEQ -> PDM.OPER.Oper.GEQ;
case LEQ -> PDM.OPER.Oper.LEQ;
case ADD -> PDM.OPER.Oper.ADD;
case SUB -> PDM.OPER.Oper.SUB;
case MUL -> PDM.OPER.Oper.MUL;
case DIV -> PDM.OPER.Oper.DIV;
case MOD -> PDM.OPER.Oper.MOD;
};
code.add(new PDM.OPER(oper, loc));
}
attrAST.attrCode.put(binExpr, code);
return code;
}
@@ -717,12 +707,6 @@ public class CodeGen {
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;
}
}
}