WIP Code Gen - finished implementation
This commit is contained in:
parent
7fd97ed0b3
commit
c7b835dd1a
5
.idea/inspectionProfiles/Project_Default.xml
generated
5
.idea/inspectionProfiles/Project_Default.xml
generated
@ -1,6 +1,11 @@
|
|||||||
<component name="InspectionProjectProfileManager">
|
<component name="InspectionProjectProfileManager">
|
||||||
<profile version="1.0">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<Languages>
|
||||||
|
<language minSize="51" name="Java" />
|
||||||
|
</Languages>
|
||||||
|
</inspection_tool>
|
||||||
<inspection_tool class="GrazieInspection" enabled="false" level="GRAMMAR_ERROR" enabled_by_default="false" />
|
<inspection_tool class="GrazieInspection" enabled="false" level="GRAMMAR_ERROR" enabled_by_default="false" />
|
||||||
<inspection_tool class="LanguageDetectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
<inspection_tool class="LanguageDetectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||||
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
@ -50,8 +50,7 @@ public class CodeGen {
|
|||||||
* @param attrCode Attribut: seznam ukazov, ki predstavljajo kodo programa.
|
* @param attrCode Attribut: seznam ukazov, ki predstavljajo kodo programa.
|
||||||
* @param attrData Attribut: seznam ukazov, ki predstavljajo podatke programa.
|
* @param attrData Attribut: seznam ukazov, ki predstavljajo podatke programa.
|
||||||
*/
|
*/
|
||||||
public AttrAST(final Memory.AttrAST attrAST, final Map<AST.Node, List<PDM.CodeInstr>> attrCode,
|
public AttrAST(final Memory.AttrAST attrAST, final Map<AST.Node, List<PDM.CodeInstr>> attrCode, final Map<AST.Node, List<PDM.DataInstr>> attrData) {
|
||||||
final Map<AST.Node, List<PDM.DataInstr>> attrData) {
|
|
||||||
super(attrAST);
|
super(attrAST);
|
||||||
this.attrCode = attrCode;
|
this.attrCode = attrCode;
|
||||||
this.attrData = attrData;
|
this.attrData = attrData;
|
||||||
@ -83,12 +82,10 @@ public class CodeGen {
|
|||||||
if (attrCode.get(node) != null) {
|
if (attrCode.get(node) != null) {
|
||||||
List<PDM.CodeInstr> instrs = attrCode.get(node);
|
List<PDM.CodeInstr> instrs = attrCode.get(node);
|
||||||
if (instrs != null) {
|
if (instrs != null) {
|
||||||
if (indent > 0)
|
if (indent > 0) System.out.printf("%" + indent + "c", ' ');
|
||||||
System.out.printf("%" + indent + "c", ' ');
|
|
||||||
System.out.printf("--- Code: ---\n");
|
System.out.printf("--- Code: ---\n");
|
||||||
for (final PDM.CodeInstr instr : instrs) {
|
for (final PDM.CodeInstr instr : instrs) {
|
||||||
if (indent > 0)
|
if (indent > 0) System.out.printf("%" + indent + "c", ' ');
|
||||||
System.out.printf("%" + indent + "c", ' ');
|
|
||||||
System.out.println((instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
System.out.println((instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,12 +93,10 @@ public class CodeGen {
|
|||||||
if (attrData.get(node) != null) {
|
if (attrData.get(node) != null) {
|
||||||
List<PDM.DataInstr> instrs = attrData.get(node);
|
List<PDM.DataInstr> instrs = attrData.get(node);
|
||||||
if (instrs != null) {
|
if (instrs != null) {
|
||||||
if (indent > 0)
|
if (indent > 0) System.out.printf("%" + indent + "c", ' ');
|
||||||
System.out.printf("%" + indent + "c", ' ');
|
|
||||||
System.out.printf("--- Data: ---\n");
|
System.out.printf("--- Data: ---\n");
|
||||||
for (final PDM.DataInstr instr : instrs) {
|
for (final PDM.DataInstr instr : instrs) {
|
||||||
if (indent > 0)
|
if (indent > 0) System.out.printf("%" + indent + "c", ' ');
|
||||||
System.out.printf("%" + indent + "c", ' ');
|
|
||||||
System.out.println((instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
System.out.println((instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,8 +116,7 @@ public class CodeGen {
|
|||||||
* predstavitve.
|
* predstavitve.
|
||||||
*/
|
*/
|
||||||
public static AttrAST generate(final Memory.AttrAST memoryAttrAST) {
|
public static AttrAST generate(final Memory.AttrAST memoryAttrAST) {
|
||||||
AttrAST attrAST = new AttrAST(memoryAttrAST, new HashMap<AST.Node, List<PDM.CodeInstr>>(),
|
AttrAST attrAST = new AttrAST(memoryAttrAST, new HashMap<AST.Node, List<PDM.CodeInstr>>(), new HashMap<AST.Node, List<PDM.DataInstr>>());
|
||||||
new HashMap<AST.Node, List<PDM.DataInstr>>());
|
|
||||||
(new CodeGenerator(attrAST)).generate();
|
(new CodeGenerator(attrAST)).generate();
|
||||||
return attrAST;
|
return attrAST;
|
||||||
}
|
}
|
||||||
@ -161,8 +155,7 @@ public class CodeGen {
|
|||||||
*/
|
*/
|
||||||
public AttrAST generate() {
|
public AttrAST generate() {
|
||||||
attrAST.ast.accept(new Generator(), null);
|
attrAST.ast.accept(new Generator(), null);
|
||||||
return new AttrAST(attrAST, Collections.unmodifiableMap(attrAST.attrCode),
|
return new AttrAST(attrAST, Collections.unmodifiableMap(attrAST.attrCode), Collections.unmodifiableMap(attrAST.attrData));
|
||||||
Collections.unmodifiableMap(attrAST.attrData));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -179,6 +172,8 @@ public class CodeGen {
|
|||||||
Report.Locatable loc = attrAST.attrLoc.get(funDef);
|
Report.Locatable loc = attrAST.attrLoc.get(funDef);
|
||||||
List<PDM.CodeInstr> code = new ArrayList<>();
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
|
frame = attrAST.attrFrame.get(funDef);
|
||||||
|
|
||||||
code.add(new PDM.LABEL(funDef.name, loc));
|
code.add(new PDM.LABEL(funDef.name, loc));
|
||||||
|
|
||||||
code.addAll(funDef.stmts.accept(this, frame));
|
code.addAll(funDef.stmts.accept(this, frame));
|
||||||
@ -190,6 +185,11 @@ public class CodeGen {
|
|||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.ParDef parDef, Mem.Frame frame) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PDM.CodeInstr> visit(AST.AtomExpr atomExpr, Mem.Frame frame) {
|
public List<PDM.CodeInstr> visit(AST.AtomExpr atomExpr, Mem.Frame frame) {
|
||||||
Report.Locatable loc = attrAST.attrLoc.get(atomExpr);
|
Report.Locatable loc = attrAST.attrLoc.get(atomExpr);
|
||||||
@ -210,7 +210,7 @@ public class CodeGen {
|
|||||||
}
|
}
|
||||||
attrAST.attrData.put(atomExpr, data);
|
attrAST.attrData.put(atomExpr, data);
|
||||||
code.add(new PDM.NAME(label, loc));
|
code.add(new PDM.NAME(label, loc));
|
||||||
code.add(new PDM.LOAD(loc));
|
// code.add(new PDM.LOAD(loc));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +224,6 @@ public class CodeGen {
|
|||||||
|
|
||||||
List<PDM.CodeInstr> code = new ArrayList<>();
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
// TODO: invert
|
|
||||||
code.addAll(binExpr.fstExpr.accept(this, frame));
|
code.addAll(binExpr.fstExpr.accept(this, frame));
|
||||||
code.addAll(binExpr.sndExpr.accept(this, frame));
|
code.addAll(binExpr.sndExpr.accept(this, frame));
|
||||||
|
|
||||||
@ -265,27 +264,60 @@ public class CodeGen {
|
|||||||
return exprStmt.expr.accept(this, frame);
|
return exprStmt.expr.accept(this, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private List<PDM.CodeInstr> resolveAddr(AST.Expr expr, Mem.Frame frame) {
|
||||||
public List<PDM.CodeInstr> visit(AST.VarExpr varExpr, Mem.Frame frame) {
|
Report.Locatable loc = attrAST.attrLoc.get(expr);
|
||||||
Report.Locatable loc = attrAST.attrLoc.get(varExpr);
|
|
||||||
List<PDM.CodeInstr> code = new ArrayList<>();
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
AST.VarDef def = (AST.VarDef) attrAST.attrDef.get(varExpr);
|
switch (expr) {
|
||||||
Mem.Access access = attrAST.attrVarAccess.get(def);
|
case AST.UnExpr unExpr:
|
||||||
|
if (unExpr.oper != AST.UnExpr.Oper.VALUEAT) {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
code.addAll(unExpr.expr.accept(this, frame));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AST.VarExpr varExpr:
|
||||||
|
AST.Def def = attrAST.attrDef.get(varExpr);
|
||||||
|
|
||||||
|
Mem.Access access = switch (def) {
|
||||||
|
case AST.VarDef varDef -> attrAST.attrVarAccess.get(varDef);
|
||||||
|
case AST.ParDef parDef -> attrAST.attrParAccess.get(parDef);
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
if (access == null) {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
switch (access) {
|
switch (access) {
|
||||||
case Mem.AbsAccess absAccess:
|
case Mem.AbsAccess absAccess:
|
||||||
code.add(new PDM.NAME(absAccess.name, loc));
|
code.add(new PDM.NAME(absAccess.name, loc));
|
||||||
code.add(new PDM.LOAD(loc));
|
|
||||||
break;
|
break;
|
||||||
case Mem.RelAccess relAccess:
|
case Mem.RelAccess relAccess:
|
||||||
code.add(new PDM.REGN(PDM.REGN.Reg.FP, loc));
|
code.add(new PDM.REGN(PDM.REGN.Reg.FP, loc));
|
||||||
|
for (int i = 0; i < frame.depth - relAccess.depth; i++) {
|
||||||
|
code.add(new PDM.LOAD(loc));
|
||||||
|
}
|
||||||
code.add(new PDM.PUSH(relAccess.offset, loc));
|
code.add(new PDM.PUSH(relAccess.offset, loc));
|
||||||
code.add(new PDM.OPER(PDM.OPER.Oper.ADD, loc));
|
code.add(new PDM.OPER(PDM.OPER.Oper.ADD, loc));
|
||||||
code.add(new PDM.LOAD(loc));
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.VarExpr varExpr, Mem.Frame frame) {
|
||||||
|
Report.Locatable loc = attrAST.attrLoc.get(varExpr);
|
||||||
|
|
||||||
|
List<PDM.CodeInstr> code = resolveAddr(varExpr, frame);
|
||||||
|
code.add(new PDM.LOAD(loc));
|
||||||
|
|
||||||
attrAST.attrCode.put(varExpr, code);
|
attrAST.attrCode.put(varExpr, code);
|
||||||
return code;
|
return code;
|
||||||
@ -295,15 +327,26 @@ public class CodeGen {
|
|||||||
public List<PDM.CodeInstr> visit(AST.UnExpr unExpr, Mem.Frame frame) {
|
public List<PDM.CodeInstr> visit(AST.UnExpr unExpr, Mem.Frame frame) {
|
||||||
Report.Locatable loc = attrAST.attrLoc.get(unExpr);
|
Report.Locatable loc = attrAST.attrLoc.get(unExpr);
|
||||||
|
|
||||||
List<PDM.CodeInstr> code = new ArrayList<>(unExpr.expr.accept(this, frame));
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
code.add(switch (unExpr.oper) {
|
if (unExpr.oper != AST.UnExpr.Oper.MEMADDR) {
|
||||||
case NOT -> new PDM.OPER(PDM.OPER.Oper.NOT, loc);
|
code.addAll(unExpr.expr.accept(this, frame));
|
||||||
case ADD -> new PDM.OPER(PDM.OPER.Oper.ADD, loc); // TODO
|
}
|
||||||
case SUB -> new PDM.OPER(PDM.OPER.Oper.NEG, loc);
|
|
||||||
case MEMADDR -> new PDM.REGN(PDM.REGN.Reg.SP, loc);
|
switch (unExpr.oper) {
|
||||||
case VALUEAT -> new PDM.LOAD(loc);
|
case NOT -> code.add(new PDM.OPER(PDM.OPER.Oper.NOT, loc));
|
||||||
});
|
case ADD -> code.add(new PDM.OPER(PDM.OPER.Oper.ADD, loc)); // TODO
|
||||||
|
case SUB -> code.add(new PDM.OPER(PDM.OPER.Oper.NEG, loc));
|
||||||
|
case VALUEAT -> code.add(new PDM.LOAD(loc));
|
||||||
|
case MEMADDR -> {
|
||||||
|
if (unExpr.expr instanceof AST.VarExpr varExpr) {
|
||||||
|
code.addAll(resolveAddr(varExpr, frame));
|
||||||
|
} else {
|
||||||
|
// TODO: are non variable expressions allowed?
|
||||||
|
throw new Report.Error(loc, "Expression not a variable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
attrAST.attrCode.put(unExpr, code);
|
attrAST.attrCode.put(unExpr, code);
|
||||||
return code;
|
return code;
|
||||||
@ -314,24 +357,156 @@ public class CodeGen {
|
|||||||
Report.Locatable loc = attrAST.attrLoc.get(callExpr);
|
Report.Locatable loc = attrAST.attrLoc.get(callExpr);
|
||||||
List<PDM.CodeInstr> code = new ArrayList<>();
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < callExpr.args.size(); i++) {
|
for (int i = callExpr.args.size() - 1; i >= 0; i--) {
|
||||||
AST.Expr expr = (AST.Expr) callExpr.args.get(i);
|
AST.Expr expr = (AST.Expr) callExpr.args.get(i);
|
||||||
code.addAll(expr.accept(this, frame));
|
code.addAll(expr.accept(this, frame));
|
||||||
code.add(new PDM.REGN(PDM.REGN.Reg.SP, loc));
|
|
||||||
|
|
||||||
code.add(new PDM.PUSH(0, loc));
|
|
||||||
code.add(new PDM.OPER(PDM.OPER.Oper.ADD, loc));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
code.add(new PDM.REGN(PDM.REGN.Reg.FP, loc)); // SL
|
code.add(new PDM.REGN(PDM.REGN.Reg.FP, loc)); // SL
|
||||||
|
|
||||||
code.add(new PDM.NAME(callExpr.name, loc));
|
code.add(new PDM.NAME(callExpr.name, loc));
|
||||||
code.add(new PDM.LOAD(loc));
|
|
||||||
code.add(new PDM.CALL(frame, loc));
|
code.add(new PDM.CALL(frame, loc));
|
||||||
|
|
||||||
attrAST.attrCode.put(callExpr, code);
|
attrAST.attrCode.put(callExpr, code);
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.VarDef varDef, Mem.Frame frame) {
|
||||||
|
Report.Locatable loc = attrAST.attrLoc.get(varDef);
|
||||||
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
List<PDM.DataInstr> data = new ArrayList<>();
|
||||||
|
|
||||||
|
Mem.Access access = attrAST.attrVarAccess.get(varDef);
|
||||||
|
|
||||||
|
switch (access) {
|
||||||
|
case Mem.AbsAccess absAccess:
|
||||||
|
data.add(new PDM.LABEL(absAccess.name, loc));
|
||||||
|
for (int i = 0; i < absAccess.size / 4; i++) {
|
||||||
|
data.add(new PDM.DATA(0, loc));
|
||||||
|
}
|
||||||
|
|
||||||
|
data.add(new PDM.LABEL(absAccess.name + ".init", loc));
|
||||||
|
for (Integer init : absAccess.inits) {
|
||||||
|
data.add(new PDM.DATA(init, loc));
|
||||||
|
}
|
||||||
|
|
||||||
|
code.add(new PDM.NAME(absAccess.name, loc));
|
||||||
|
code.add(new PDM.NAME(absAccess.name + ".init", loc));
|
||||||
|
code.add(new PDM.INIT(loc));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Mem.RelAccess relAccess:
|
||||||
|
String initLabel = String.valueOf(labelCounter++);
|
||||||
|
data.add(new PDM.LABEL(initLabel, loc));
|
||||||
|
for (Integer init : relAccess.inits) {
|
||||||
|
data.add(new PDM.DATA(init, loc));
|
||||||
|
}
|
||||||
|
|
||||||
|
code.add(new PDM.PUSH(-relAccess.size, loc));
|
||||||
|
code.add(new PDM.POPN(loc));
|
||||||
|
|
||||||
|
code.add(new PDM.REGN(PDM.REGN.Reg.SP, loc));
|
||||||
|
code.add(new PDM.NAME(initLabel, loc));
|
||||||
|
code.add(new PDM.INIT(loc));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
attrAST.attrCode.put(varDef, code);
|
||||||
|
attrAST.attrData.put(varDef, data);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.Init init, Mem.Frame frame) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.AssignStmt assignStmt, Mem.Frame frame) {
|
||||||
|
Report.Locatable loc = attrAST.attrLoc.get(assignStmt);
|
||||||
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
|
code.addAll(assignStmt.srcExpr.accept(this, frame));
|
||||||
|
code.addAll(resolveAddr(assignStmt.dstExpr, frame));
|
||||||
|
code.add(new PDM.SAVE(loc));
|
||||||
|
|
||||||
|
attrAST.attrCode.put(assignStmt, code);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.LetStmt letStmt, Mem.Frame frame) {
|
||||||
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
|
for (AST.MainDef def : letStmt.defs) {
|
||||||
|
code.addAll(def.accept(this, frame));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (AST.Stmt stmt : letStmt.stmts) {
|
||||||
|
code.addAll(stmt.accept(this, frame));
|
||||||
|
}
|
||||||
|
|
||||||
|
attrAST.attrCode.put(letStmt, code);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.IfStmt ifStmt, Mem.Frame frame) {
|
||||||
|
Report.Locatable loc = attrAST.attrLoc.get(ifStmt);
|
||||||
|
|
||||||
|
String thenLabel = String.valueOf(labelCounter++);
|
||||||
|
String elseLabel = String.valueOf(labelCounter++);
|
||||||
|
String endLabel = String.valueOf(labelCounter++);
|
||||||
|
|
||||||
|
List<PDM.CodeInstr> code = ifStmt.cond.accept(this, frame);
|
||||||
|
code.add(new PDM.NAME(thenLabel, loc));
|
||||||
|
code.add(new PDM.NAME(elseLabel, loc));
|
||||||
|
code.add(new PDM.CJMP(loc));
|
||||||
|
|
||||||
|
code.add(new PDM.LABEL(thenLabel, loc));
|
||||||
|
code.addAll(ifStmt.thenStmts.accept(this, frame));
|
||||||
|
code.add(new PDM.NAME(endLabel, loc));
|
||||||
|
code.add(new PDM.UJMP(loc));
|
||||||
|
|
||||||
|
code.add(new PDM.LABEL(elseLabel, loc));
|
||||||
|
code.addAll(ifStmt.elseStmts.accept(this, frame));
|
||||||
|
|
||||||
|
code.add(new PDM.LABEL(endLabel, loc));
|
||||||
|
|
||||||
|
attrAST.attrCode.put(ifStmt, code);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PDM.CodeInstr> visit(AST.WhileStmt whileStmt, Mem.Frame frame) {
|
||||||
|
Report.Locatable loc = attrAST.attrLoc.get(whileStmt);
|
||||||
|
List<PDM.CodeInstr> code = new ArrayList<>();
|
||||||
|
|
||||||
|
String condLabel = String.valueOf(labelCounter++);
|
||||||
|
String startLabel = String.valueOf(labelCounter++);
|
||||||
|
String endLabel = String.valueOf(labelCounter++);
|
||||||
|
|
||||||
|
code.add(new PDM.LABEL(condLabel, loc));
|
||||||
|
code.addAll(whileStmt.cond.accept(this, frame));
|
||||||
|
code.add(new PDM.NAME(startLabel, loc));
|
||||||
|
code.add(new PDM.NAME(endLabel, loc));
|
||||||
|
code.add(new PDM.CJMP(loc));
|
||||||
|
|
||||||
|
code.add(new PDM.LABEL(startLabel, loc));
|
||||||
|
code.addAll(whileStmt.stmts.accept(this, frame));
|
||||||
|
|
||||||
|
code.add(new PDM.NAME(condLabel, loc));
|
||||||
|
code.add(new PDM.UJMP(loc));
|
||||||
|
|
||||||
|
code.add(new PDM.LABEL(endLabel, loc));
|
||||||
|
|
||||||
|
attrAST.attrCode.put(whileStmt, code);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,8 +576,7 @@ public class CodeGen {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visit(final AST.FunDef funDef, final Object arg) {
|
public Object visit(final AST.FunDef funDef, final Object arg) {
|
||||||
if (funDef.stmts.size() == 0)
|
if (funDef.stmts.size() == 0) return null;
|
||||||
return null;
|
|
||||||
List<PDM.CodeInstr> code = attrAST.attrCode.get(funDef);
|
List<PDM.CodeInstr> code = attrAST.attrCode.get(funDef);
|
||||||
codeFunsSegment.addAll(code);
|
codeFunsSegment.addAll(code);
|
||||||
funDef.pars.accept(this, arg);
|
funDef.pars.accept(this, arg);
|
||||||
@ -429,9 +603,7 @@ public class CodeGen {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -482,8 +654,7 @@ public class CodeGen {
|
|||||||
@Override
|
@Override
|
||||||
public Object visit(final AST.VarDef varDef, final Object arg) {
|
public Object visit(final AST.VarDef varDef, final Object arg) {
|
||||||
List<PDM.DataInstr> data = attrAST.attrData.get(varDef);
|
List<PDM.DataInstr> data = attrAST.attrData.get(varDef);
|
||||||
if (data != null)
|
if (data != null) dataSegment.addAll(data);
|
||||||
dataSegment.addAll(data);
|
|
||||||
varDef.inits.accept(this, arg);
|
varDef.inits.accept(this, arg);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -491,8 +662,7 @@ public class CodeGen {
|
|||||||
@Override
|
@Override
|
||||||
public Object visit(final AST.AtomExpr atomExpr, final Object arg) {
|
public Object visit(final AST.AtomExpr atomExpr, final Object arg) {
|
||||||
List<PDM.DataInstr> data = attrAST.attrData.get(atomExpr);
|
List<PDM.DataInstr> data = attrAST.attrData.get(atomExpr);
|
||||||
if (data != null)
|
if (data != null) dataSegment.addAll(data);
|
||||||
dataSegment.addAll(data);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -511,10 +681,8 @@ public class CodeGen {
|
|||||||
System.out.println("This is PINS'25 compiler (code generation):");
|
System.out.println("This is PINS'25 compiler (code generation):");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (cmdLineArgs.length == 0)
|
if (cmdLineArgs.length == 0) throw new Report.Error("No source file specified in the command line.");
|
||||||
throw new Report.Error("No source file specified in the command line.");
|
if (cmdLineArgs.length > 1) Report.warning("Unused arguments in the command line.");
|
||||||
if (cmdLineArgs.length > 1)
|
|
||||||
Report.warning("Unused arguments in the command line.");
|
|
||||||
|
|
||||||
try (SynAn synAn = new SynAn(cmdLineArgs[0])) {
|
try (SynAn synAn = new SynAn(cmdLineArgs[0])) {
|
||||||
// abstraktna sintaksa:
|
// abstraktna sintaksa:
|
||||||
@ -533,8 +701,7 @@ public class CodeGen {
|
|||||||
{
|
{
|
||||||
System.out.println("\n\033[1mCODE SEGMENT:\033[0m");
|
System.out.println("\n\033[1mCODE SEGMENT:\033[0m");
|
||||||
for (final PDM.CodeInstr instr : codeSegment) {
|
for (final PDM.CodeInstr instr : codeSegment) {
|
||||||
System.out.printf("%8d [%s] %s\n", addr, instr.size(),
|
System.out.printf("%8d [%s] %s\n", addr, instr.size(), (instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
||||||
(instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
|
||||||
addr += instr.size();
|
addr += instr.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -542,8 +709,7 @@ public class CodeGen {
|
|||||||
{
|
{
|
||||||
System.out.println("\n\033[1mDATA SEGMENT:\033[0m");
|
System.out.println("\n\033[1mDATA SEGMENT:\033[0m");
|
||||||
for (final PDM.DataInstr instr : dataSegment) {
|
for (final PDM.DataInstr instr : dataSegment) {
|
||||||
System.out.printf("%8d [%s] %s\n", addr, (instr instanceof PDM.SIZE) ? " " : instr.size(),
|
System.out.printf("%8d [%s] %s\n", addr, (instr instanceof PDM.SIZE) ? " " : instr.size(), (instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
||||||
(instr instanceof PDM.LABEL ? "" : " ") + instr.toString());
|
|
||||||
addr += instr.size();
|
addr += instr.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import pins25.common.*;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Skladovni stroj.
|
* Skladovni stroj.
|
||||||
*
|
* <p>
|
||||||
* Naslovi 'sistemskih' funkcij:
|
* Naslovi 'sistemskih' funkcij:
|
||||||
* <ol>
|
* <ol>
|
||||||
* <li>{@code -1}: {@code fun exit(exitcode)}</li>
|
* <li>{@code -1}: {@code fun exit(exitcode)}</li>
|
||||||
@ -25,13 +25,19 @@ public class Machine {
|
|||||||
throw new Report.InternalError();
|
throw new Report.InternalError();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ali se opravi testni izpis ukazov. */
|
/**
|
||||||
public static boolean debugInstrsList = false;
|
* Ali se opravi testni izpis ukazov.
|
||||||
|
*/
|
||||||
|
public static boolean debugInstrsList = true;
|
||||||
|
|
||||||
/** Ali se opravi testni izpis vrednost oznak. */
|
/**
|
||||||
|
* Ali se opravi testni izpis vrednost oznak.
|
||||||
|
*/
|
||||||
public static boolean debugLabelsList = false;
|
public static boolean debugLabelsList = false;
|
||||||
|
|
||||||
/** Ali se opravi testni izpis dogajanja na skladu. */
|
/**
|
||||||
|
* Ali se opravi testni izpis dogajanja na skladu.
|
||||||
|
*/
|
||||||
public static boolean debugStack = false;
|
public static boolean debugStack = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,28 +45,44 @@ public class Machine {
|
|||||||
*/
|
*/
|
||||||
public static class Executor {
|
public static class Executor {
|
||||||
|
|
||||||
/** Seznam ukazov kode programa. */
|
/**
|
||||||
|
* Seznam ukazov kode programa.
|
||||||
|
*/
|
||||||
private final HashMap<Integer, PDM.CodeInstr> program = new HashMap<Integer, PDM.CodeInstr>();
|
private final HashMap<Integer, PDM.CodeInstr> program = new HashMap<Integer, PDM.CodeInstr>();
|
||||||
|
|
||||||
/** Pomnilnik (brez predstavitve ukazov. */
|
/**
|
||||||
|
* Pomnilnik (brez predstavitve ukazov.
|
||||||
|
*/
|
||||||
private final HashMap<Integer, Byte> memory = new HashMap<Integer, Byte>();
|
private final HashMap<Integer, Byte> memory = new HashMap<Integer, Byte>();
|
||||||
|
|
||||||
/** Preslikava imen oznak v fizicne naslove. */
|
/**
|
||||||
|
* Preslikava imen oznak v fizicne naslove.
|
||||||
|
*/
|
||||||
private final HashMap<String, Integer> labelToAddr = new HashMap<String, Integer>();
|
private final HashMap<String, Integer> labelToAddr = new HashMap<String, Integer>();
|
||||||
|
|
||||||
/** Preslikava fizicnih naslovov v imena oznak. */
|
/**
|
||||||
|
* Preslikava fizicnih naslovov v imena oznak.
|
||||||
|
*/
|
||||||
private final HashMap<Integer, String> addrToLabel = new HashMap<Integer, String>();
|
private final HashMap<Integer, String> addrToLabel = new HashMap<Integer, String>();
|
||||||
|
|
||||||
/** Velikost segmenta z ukazi kode programa. */
|
/**
|
||||||
|
* Velikost segmenta z ukazi kode programa.
|
||||||
|
*/
|
||||||
private final int codeSegmentSize;
|
private final int codeSegmentSize;
|
||||||
|
|
||||||
/** Velikost segmenta s staticnimi spremenljivkami. */
|
/**
|
||||||
|
* Velikost segmenta s staticnimi spremenljivkami.
|
||||||
|
*/
|
||||||
private final int dataSegmentSize;
|
private final int dataSegmentSize;
|
||||||
|
|
||||||
/** Preslikava naslova v lokacijo kode, ki je izvor vrednosti na naslovu. */
|
/**
|
||||||
|
* Preslikava naslova v lokacijo kode, ki je izvor vrednosti na naslovu.
|
||||||
|
*/
|
||||||
final HashMap<Integer, String> debugLocs = new HashMap<Integer, String>();
|
final HashMap<Integer, String> debugLocs = new HashMap<Integer, String>();
|
||||||
|
|
||||||
/** Preslikava naslova v pomen podatka, ki je shranjen na naslovu. */
|
/**
|
||||||
|
* Preslikava naslova v pomen podatka, ki je shranjen na naslovu.
|
||||||
|
*/
|
||||||
final HashMap<Integer, String> debugDscs = new HashMap<Integer, String>();
|
final HashMap<Integer, String> debugDscs = new HashMap<Integer, String>();
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -80,16 +102,24 @@ public class Machine {
|
|||||||
addrToLabel.put(-7, "del");
|
addrToLabel.put(-7, "del");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Programski stevec. */
|
/**
|
||||||
|
* Programski stevec.
|
||||||
|
*/
|
||||||
private int PC;
|
private int PC;
|
||||||
|
|
||||||
/** Klicni kazalec. */
|
/**
|
||||||
|
* Klicni kazalec.
|
||||||
|
*/
|
||||||
private int FP;
|
private int FP;
|
||||||
|
|
||||||
/** Skladovni kazalec. */
|
/**
|
||||||
|
* Skladovni kazalec.
|
||||||
|
*/
|
||||||
private int SP;
|
private int SP;
|
||||||
|
|
||||||
/** Kazalec na prvi prosti naslov na kopici. */
|
/**
|
||||||
|
* Kazalec na prvi prosti naslov na kopici.
|
||||||
|
*/
|
||||||
private int HP;
|
private int HP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -236,7 +266,8 @@ public class Machine {
|
|||||||
SP = SP + 0;
|
SP = SP + 0;
|
||||||
|
|
||||||
System.out.printf("\n");
|
System.out.printf("\n");
|
||||||
loop: while (true) {
|
loop:
|
||||||
|
while (true) {
|
||||||
|
|
||||||
if (debugStack) {
|
if (debugStack) {
|
||||||
for (int stackAddr = 0x10000 - 4; stackAddr >= SP; stackAddr -= 4) {
|
for (int stackAddr = 0x10000 - 4; stackAddr >= SP; stackAddr -= 4) {
|
||||||
|
@ -241,7 +241,7 @@ public class Memory {
|
|||||||
Mem.Frame frame = new Mem.Frame(
|
Mem.Frame frame = new Mem.Frame(
|
||||||
funDef.name,
|
funDef.name,
|
||||||
frameBuilder.depth,
|
frameBuilder.depth,
|
||||||
parOffset,
|
parOffset - 4,
|
||||||
-frameBuilder.varOffset,
|
-frameBuilder.varOffset,
|
||||||
debugPars,
|
debugPars,
|
||||||
frameBuilder.debugVars
|
frameBuilder.debugVars
|
||||||
|
Loading…
x
Reference in New Issue
Block a user