Added cli argument to either generate main method or not

This commit is contained in:
Marvin Kaiser
2020-01-14 18:37:34 +01:00
parent 5275b63940
commit 34d0e1bb0e
2 changed files with 16 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ public class Klang {
boolean evaluate = false;
boolean prettyPrint = false;
boolean compile = true;
String mainName = "main";
List<String> arguments = Arrays.asList(args);
if (arguments.contains("-h") || arguments.contains("--help") || arguments.contains("?")) {
@@ -29,6 +30,7 @@ public class Klang {
System.out.println("--evaluate:\t Evaluates the given source code");
System.out.println("--pretty:\t Pretty print the given source code");
System.out.println("--no-compile:\t Do not compile the source code");
System.out.println("--no-main:\t Do not generate main function, will be generated as 'start'. Useful for testing");
return;
}
if (arguments.contains("--evaluate")) {
@@ -40,6 +42,9 @@ public class Klang {
if (arguments.contains("--no-compile")) {
compile = false;
}
if (arguments.contains("--no-main")) {
mainName = "start";
}
// create a CharStream that reads from standard input
CharStream input = CharStreams.fromStream(System.in);
@@ -71,7 +76,7 @@ public class Klang {
// System.out.println("\nPrinting the assembler code");
StringWriter wAsm = new StringWriter();
GenASM.ExWriter exAsm = new GenASM.ExWriter(wAsm);
GenASM genasm = new GenASM(exAsm);
GenASM genasm = new GenASM(exAsm, mainName);
node.welcome(genasm);
System.out.println(wAsm.toString());
}

View File

@@ -56,13 +56,20 @@ public class GenASM implements Visitor<Void> {
}
public ExWriter ex;
private String mainName;
Map<String, Integer> env = new HashMap<>();
Set<String> vars;
String[] rs = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" };
private int lCount = 0; // Invariante: lCount ist benutzt
public GenASM(ExWriter ex, String mainName) {
this.ex = ex;
this.mainName = mainName;
}
public GenASM(ExWriter ex) {
this.ex = ex;
this.mainName = "main";
}
@Override
@@ -445,9 +452,9 @@ public class GenASM implements Visitor<Void> {
func.welcome(this);
this.ex.write("\n");
}
this.ex.write(".globl start\n");
this.ex.write(".type start, @function\n");
this.ex.write("start:\n");
this.ex.write(".globl " + mainName + "\n");
this.ex.write(".type " +mainName + ", @function\n");
this.ex.write(mainName + ":\n");
this.ex.write(" pushq %rbp\n");
this.ex.write(" movq %rsp, %rbp\n");
e.expression.welcome(this);