GenASM: Make GenASM quietly rewrite a user's function if it's called main.
We generate our own main function that executes the user's specified expression at the end of his file. This auto generated function has to be called "main" in order for it to be executed on startup. If the user chooses to call one of his own functions "main" our auto generated function would collide with the user's function. That's why we quietly rewrite the user's function to "main_by_user". This way we prevent a collision. Note for the future: We should change our Syntax to allow for no expression as the last definition of a file. This way the user can choose if a particular source file needs to contain a main function or not (just like c does it). This is also one of the requirements for modules to work.
This commit is contained in:
@@ -567,6 +567,13 @@ public class GenASM implements Visitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visit(FunctionDefinition e) {
|
||||
// If the user chooses "main" as one of his function names then
|
||||
// rename it and hope that they didn't use the renamed function name
|
||||
// as well :D
|
||||
if (e.name.equals("main")) {
|
||||
e.name = "main_by_user";
|
||||
}
|
||||
|
||||
int lblStart = ++lCount;
|
||||
this.currentFunctionStartLabel = lblStart;
|
||||
this.currentFunctionParams = e.parameters;
|
||||
@@ -744,7 +751,8 @@ public class GenASM implements Visitor<Void> {
|
||||
asm.add("q", stackStartOffset, "%rsp");
|
||||
}
|
||||
|
||||
asm.call(e.name);
|
||||
// We rename a function name if it is "main"
|
||||
asm.call(e.name.equals("main") ? "main_by_user": e.name);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -769,7 +777,7 @@ public class GenASM implements Visitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visit(Parameter e) {
|
||||
// The work for a paremeter node is implement
|
||||
// The work for a parameter node is implement
|
||||
// in the function definition visitor
|
||||
return null;
|
||||
}
|
||||
@@ -824,6 +832,9 @@ public class GenASM implements Visitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visit(EnumAccessExpression e) {
|
||||
// Since the access to an enum simply results in an integer (i.e. the index
|
||||
// of the enum value), we can just let IntegerExpression handle the expected behaviour.
|
||||
new IntegerExpression(e.enumValue.index).welcome(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user