Skip to content

Commit

Permalink
Added IMC
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanja-S committed May 12, 2023
1 parent 701381d commit b40d939
Show file tree
Hide file tree
Showing 31 changed files with 1,075 additions and 124 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"request": "launch",
"mainClass": "Main",
"projectName": "PINSCompiler_aaffa8a2",
"args": "PINS --exec FRM --dump FRM test.pns"
"args": "PINS --exec IMC --dump IMC test.pns"
},
{
"type": "java",
Expand Down
Binary file modified src/.DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import compiler.frm.Access;
import compiler.frm.Frame;
import compiler.frm.FrameEvaluator;
import compiler.ir.IRCodeGenerator;
import compiler.ir.IRPrettyPrint;
import compiler.ir.code.IRNode;
import compiler.lexer.Lexer;
import compiler.parser.Parser;
import compiler.parser.ast.def.Def;
Expand Down Expand Up @@ -124,5 +127,16 @@ private static void run(PINS cli, String sourceCode) {
if (cli.execPhase == Phase.FRM) {
return;
}
/**
* Generiranje vmesne kode.
*/
var generator = new IRCodeGenerator(new NodeDescription<IRNode>(), frames, accesses, definitions, types);
ast.accept(generator);
if (cli.dumpPhases.contains(Phase.IMC)) {
new IRPrettyPrint(System.out, 2).print(generator.chunks);
}
if (cli.execPhase == Phase.IMC) {
return;
}
}
}
Binary file added src/common/.DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions src/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ private Constants() {}
public static final String randIntLabel = "rand_int";
public static final String seedLabel = "seed";

// 'Registri'
public static final String framePointer = "{FP}";
public static final String stackPointer = "{SP}";

static {
/**
* Ciljna arhitektura je x86.
Expand Down
65 changes: 32 additions & 33 deletions src/common/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
* @Description: Razred, namenjen obveščanju o poteku prevajanja.
*/

package common;

import java.io.PrintStream;

import compiler.lexer.Position;

/**
* Obveščanje o napakah.
*/
public class Report {
/**
* NE SPREMINJAJ!
*/
private static final int exitErrorCode = 99;

/**
* Izhodni tok, kamor se izpišejo napake.
*/
public static PrintStream err = System.err;

private Report() {}

public static void error(String message) {
err.println(message);
System.exit(exitErrorCode);
}

public static void error(Position position, String message) {
err.println(position.toString() + ": " + message);
System.exit(exitErrorCode);
}
}

package common;

import java.io.PrintStream;

import compiler.lexer.Position;

/**
* Obveščanje o napakah.
*/
public class Report {
/**
* NE SPREMINJAJ!
*/
private static final int exitErrorCode = 99;

/**
* Izhodni tok, kamor se izpišejo napake.
*/
public static PrintStream err = System.err;

private Report() {}

public static void error(String message) {
err.println(message);
System.exit(exitErrorCode);
}

public static void error(Position position, String message) {
err.println(position.toString() + ": " + message);
System.exit(exitErrorCode);
}
}
21 changes: 10 additions & 11 deletions src/common/RequireNonNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
* @ Description:
*/

package common;
package common;

import java.util.Objects;

public class RequireNonNull {
public static void requireNonNull(Object... objects) {
for (var obj : objects) {
Objects.requireNonNull(obj);
}
}
}

import java.util.Objects;

public class RequireNonNull {
public static void requireNonNull(Object... objects) {
for (var obj : objects) {
Objects.requireNonNull(obj);
}
}
}
37 changes: 18 additions & 19 deletions src/common/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
* @ Description:
*/

package common;
package common;

import static common.RequireNonNull.requireNonNull;

public class StringUtil {
/**
* Kreira nov indentiran niz.
*/
public static String indented(String str, int indent) {
requireNonNull(str);
if (indent < 0) { throw new IllegalArgumentException("Indent must be at least 0!"); }
var sb = new StringBuilder(indent);
for (int i = 0; i < indent; i++) {
sb.append(" ");
}
sb.append(str);
return sb.toString();
}
}

import static common.RequireNonNull.requireNonNull;

public class StringUtil {
/**
* Kreira nov indentiran niz.
*/
public static String indented(String str, int indent) {
requireNonNull(str);
if (indent < 0) { throw new IllegalArgumentException("Indent must be at least 0!"); }
var sb = new StringBuilder(indent);
for (int i = 0; i < indent; i++) {
sb.append(" ");
}
sb.append(str);
return sb.toString();
}
}
Binary file modified src/compiler/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions src/compiler/frm/FrameEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static common.RequireNonNull.requireNonNull;

import compiler.common.Visitor;
import compiler.frm.Access.Local;
import compiler.parser.ast.def.*;
import compiler.parser.ast.def.FunDef.Parameter;
import compiler.parser.ast.expr.*;
Expand All @@ -20,7 +19,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class FrameEvaluator implements Visitor {
/**
Expand Down Expand Up @@ -180,6 +178,8 @@ public void visit(FunDef funDef) {
for (Integer Int : stackOffset.get(staticLevel - 1).locVarStackOffset) {
Builder.addLocalVariable(-Int.intValue());
}
// StaticLink
Builder.addParameter(4);
Builder.addFunctionCall(functionCalls.get(staticLevel - 1).intValue());

staticLevel--;
Expand Down
Loading

0 comments on commit b40d939

Please sign in to comment.