Added cli argument to either generate main method or not
This commit is contained in:
@@ -17,6 +17,7 @@ public class Klang {
|
|||||||
boolean evaluate = false;
|
boolean evaluate = false;
|
||||||
boolean prettyPrint = false;
|
boolean prettyPrint = false;
|
||||||
boolean compile = true;
|
boolean compile = true;
|
||||||
|
String mainName = "main";
|
||||||
|
|
||||||
List<String> arguments = Arrays.asList(args);
|
List<String> arguments = Arrays.asList(args);
|
||||||
if (arguments.contains("-h") || arguments.contains("--help") || arguments.contains("?")) {
|
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("--evaluate:\t Evaluates the given source code");
|
||||||
System.out.println("--pretty:\t Pretty print 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-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;
|
return;
|
||||||
}
|
}
|
||||||
if (arguments.contains("--evaluate")) {
|
if (arguments.contains("--evaluate")) {
|
||||||
@@ -40,6 +42,9 @@ public class Klang {
|
|||||||
if (arguments.contains("--no-compile")) {
|
if (arguments.contains("--no-compile")) {
|
||||||
compile = false;
|
compile = false;
|
||||||
}
|
}
|
||||||
|
if (arguments.contains("--no-main")) {
|
||||||
|
mainName = "start";
|
||||||
|
}
|
||||||
|
|
||||||
// create a CharStream that reads from standard input
|
// create a CharStream that reads from standard input
|
||||||
CharStream input = CharStreams.fromStream(System.in);
|
CharStream input = CharStreams.fromStream(System.in);
|
||||||
@@ -71,7 +76,7 @@ public class Klang {
|
|||||||
// System.out.println("\nPrinting the assembler code");
|
// System.out.println("\nPrinting the assembler code");
|
||||||
StringWriter wAsm = new StringWriter();
|
StringWriter wAsm = new StringWriter();
|
||||||
GenASM.ExWriter exAsm = new GenASM.ExWriter(wAsm);
|
GenASM.ExWriter exAsm = new GenASM.ExWriter(wAsm);
|
||||||
GenASM genasm = new GenASM(exAsm);
|
GenASM genasm = new GenASM(exAsm, mainName);
|
||||||
node.welcome(genasm);
|
node.welcome(genasm);
|
||||||
System.out.println(wAsm.toString());
|
System.out.println(wAsm.toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,13 +56,20 @@ public class GenASM implements Visitor<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ExWriter ex;
|
public ExWriter ex;
|
||||||
|
private String mainName;
|
||||||
Map<String, Integer> env = new HashMap<>();
|
Map<String, Integer> env = new HashMap<>();
|
||||||
Set<String> vars;
|
Set<String> vars;
|
||||||
String[] rs = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" };
|
String[] rs = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" };
|
||||||
private int lCount = 0; // Invariante: lCount ist benutzt
|
private int lCount = 0; // Invariante: lCount ist benutzt
|
||||||
|
|
||||||
|
public GenASM(ExWriter ex, String mainName) {
|
||||||
|
this.ex = ex;
|
||||||
|
this.mainName = mainName;
|
||||||
|
}
|
||||||
|
|
||||||
public GenASM(ExWriter ex) {
|
public GenASM(ExWriter ex) {
|
||||||
this.ex = ex;
|
this.ex = ex;
|
||||||
|
this.mainName = "main";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -445,9 +452,9 @@ public class GenASM implements Visitor<Void> {
|
|||||||
func.welcome(this);
|
func.welcome(this);
|
||||||
this.ex.write("\n");
|
this.ex.write("\n");
|
||||||
}
|
}
|
||||||
this.ex.write(".globl start\n");
|
this.ex.write(".globl " + mainName + "\n");
|
||||||
this.ex.write(".type start, @function\n");
|
this.ex.write(".type " +mainName + ", @function\n");
|
||||||
this.ex.write("start:\n");
|
this.ex.write(mainName + ":\n");
|
||||||
this.ex.write(" pushq %rbp\n");
|
this.ex.write(" pushq %rbp\n");
|
||||||
this.ex.write(" movq %rsp, %rbp\n");
|
this.ex.write(" movq %rsp, %rbp\n");
|
||||||
e.expression.welcome(this);
|
e.expression.welcome(this);
|
||||||
|
|||||||
Reference in New Issue
Block a user