From e835bd0f06b8f37f41ad957e6b126edd4baabbf8 Mon Sep 17 00:00:00 2001 From: nitrix Date: Mon, 20 Mar 2023 21:05:24 +0100 Subject: [PATCH] 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. --- .../de/hsrm/compiler/Klang/visitors/GenASM.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java index e9572c6..5bc32f2 100644 --- a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java +++ b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java @@ -567,6 +567,13 @@ public class GenASM implements Visitor { @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 { 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 { @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 { @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; }