Compare commits

...

22 Commits

Author SHA1 Message Date
bcc9f29ff5 Merge pull request 'feature/char-support' (#3) from feature/char-support into master
Reviewed-on: #3
2023-03-24 17:03:23 +01:00
259ac49981 ContextAnalysis: Make comparing more generous
Previously we only allowed numeric types to be compared using >,<,<= and >=. Now we allow all primitives types to be compared using the operators mentioned above.

It is now also possible to compare and assign ints and chars to each other.
2023-03-24 00:17:38 +01:00
2aff9b3d0d Char: Add support for chars.
First try at implementing chars. There are some problems with the grammar where I was not able to separate the single quotes from the captured char, but I managed to hack it together in the ContextAnalysis.

It's currently not possible to do any 'math' with chars, but I will give it a try soon.
2023-03-23 23:04:09 +01:00
a7e93f4f01 GenASM: Set the bytesToClearFromStack variable earlier
If you set the variable after visiting the function body all children will see bytesToClearFromStack == 0.
2023-03-23 22:52:52 +01:00
5f0e84198a GenASM+Void: Generate an implicit return
Void functions are allowed to be completely free of return statements. The problem is that this does not work on its own in assembler. We have to generate a return statement at the end of the function body if there is no return statement already.
2023-03-23 22:49:36 +01:00
aef2c84fdc Void Type: Fix a problem where calls to void functions were ignored
The ContextAnalysis visitor for braced blocks was not updated during void support implementation which is why I do it now. Although we are re-using the functionCall production rule a different visitor is required for visiting function calls directly inside braced blocks. This is because the other place where functionCalls are used the functionCall is annotated with a label with wraps the functionCall context inside a label context. Fortunately this is in fact simple wrapping so we just implement a visitor for the wrapping class that unwraps the functionCall and passes it to the visitor that actually implements the functionCall.
2023-03-23 21:49:59 +01:00
4219f93021 Merge pull request 'Add support for void types' (#2) from 31-void-type into master
Reviewed-on: #2
2023-03-23 14:28:24 +01:00
7965c89a60 VoidType: Add tests and fix some bugs 2023-03-23 13:10:20 +01:00
26eff47057 Fix typos. 2023-03-23 13:09:52 +01:00
Marvin Kaiser
53976615e1 31: Add void type 2023-03-23 12:47:22 +01:00
c124587983 git: ignore idea designer file 2023-03-23 03:35:11 +01:00
07e5a338a4 GenASM: Fix segfaults when calling malloc
This was a tricky one. In order to call malloc the stack needs to be 16 byte aligned. GenASM up until this point was 8 byte aligned. This commit changes the code generation so that it is guaranteed that the stack is 16 byte aligned before visiting a child node. We need to do this before visiting *any* child node because we can not know whether somewhere downstream some child node calls malloc.

To make it possible for a visitor method to determine if it needs to align the stack it has to assume that the stack is currently aligned. This is the other reason we ensure that the stack is aligned before visiting any child node.

On top of that some visitor methods did not clean up after themselves. I am not sure why this did not result in segfaults already, but I changed the code so that each visitor method leaves the stack in the same state after completion.
2023-03-23 03:21:10 +01:00
76419d86bb GenASM: Remove a few warnings 2023-03-23 02:59:01 +01:00
f55f2661de Tests: Fix failing test because the error message changed. 2023-03-23 00:32:50 +01:00
06609ae899 GenASM: Get number of local variables from the "localVariables" attribute of FunctionDefinition. 2023-03-22 23:40:02 +01:00
c5c01041e4 ContextAnalysis: Track local variables and function parameters separately.
This enables us to add all local variable Definitions to the DAST so that downstream visitors don't need to compute the local variables of a function again.
2023-03-22 23:37:43 +01:00
9751a1da2f Eval: Add the corresponding named type to a struct Value. 2023-03-22 00:14:24 +01:00
534b507f7a Eval: Make a NullExpression evaluate to a Value with Type "NulLValue" instead of returning null. 2023-03-22 00:13:23 +01:00
cce58b6e38 ContextAnalysis: Make naught comparable to struct again. 2023-03-21 22:44:21 +01:00
8b17ced533 Build: Set Java to verison 17 2023-03-21 22:43:40 +01:00
bacc40d844 GenASM: Make sure the stack is 16 byte aligned before calling malloc or free.
The GenASM visitor only uses quad mnemonics so each manipulation to the stack is done in 8 byte chunks. This means our stack is always 8 byte aligned which is fine when only calling KLang code. libc functions like 'malloc' or 'free' require the stack to be 16 byte aligned which isn't automatically guaranteed when being 8 byte aligned. This is why we need to be careful when manipulating the stack (e.g. pushq, popq, subq x, %rsp). By assuming that we are stack aligned at the beginning of execution and by tracking the number of pushq operations we can deduce whether we misaligned the stack and align it again by adding another 8 byte to the stack.

This re-alignment of the stack is done directly before entering a function body or calling malloc. Since there are no stack manipulations involved when generating the code to calling 'free' there is reason to check for alignment there.
2023-03-21 22:21:14 +01:00
f38bd3d69e GenASM: Use leave instead of mov and pop for returning from a function. 2023-03-21 00:22:11 +01:00
43 changed files with 1147 additions and 675 deletions

3
.gitignore vendored
View File

@@ -16,4 +16,5 @@ src/main/antlr4/de/hsrm/compiler/Klang/.antlr
# build output
out
src/test/test
src/test/test
/.idea/uiDesigner.xml

2
.idea/compiler.xml generated
View File

@@ -10,7 +10,7 @@
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="klang" target="11" />
<module name="klang" target="17" />
</bytecodeTargetLevel>
</component>
</project>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_17">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">

View File

@@ -40,7 +40,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
<release>17</release>
</configuration>
</plugin>
<!-- Plugin to compile the g4 files ahead of the java files

View File

@@ -33,7 +33,7 @@ parameter
;
braced_block
: OBRK statement+ CBRK
: OBRK (statement | functionCall SCOL)+ CBRK
;
@@ -73,7 +73,7 @@ field_assignment
;
return_statement
: RETURN expression SCOL
: RETURN expression? SCOL
;
destroy_statement
@@ -107,6 +107,7 @@ atom
: INTEGER_LITERAL #intAtom
| BOOLEAN_LITERAL #boolAtom
| FLOAT_LITERAL #floatAtom
| CHAR_LITERAL #charAtom
| NULL # nullAtom
| IDENT #variable
;
@@ -119,7 +120,9 @@ type
: INTEGER
| BOOLEAN
| FLOAT
| CHAR
| IDENT
| VOID
;
functionCall
@@ -183,9 +186,13 @@ SUB: '-';
MOD: '%';
DIV: '/';
SQUOT: '\'';
BOOLEAN: 'bool';
INTEGER: 'int';
FLOAT: 'float';
CHAR: 'char';
VOID: 'void';
INTEGER_LITERAL
: [0-9]+
@@ -200,6 +207,10 @@ BOOLEAN_LITERAL
| 'false'
;
CHAR_LITERAL
: SQUOT [ -~] SQUOT
;
IDENT
: [a-zA-Z][a-zA-Z0-9]*
;

View File

@@ -8,10 +8,12 @@ import de.hsrm.compiler.Klang.nodes.loops.ForLoop;
import de.hsrm.compiler.Klang.nodes.loops.WhileLoop;
import de.hsrm.compiler.Klang.nodes.statements.*;
import de.hsrm.compiler.Klang.types.Type;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.*;
public class ContextAnalysis extends KlangBaseVisitor<Node> {
Map<String, VariableDeclaration> params = new HashMap<>();
Map<String, VariableDeclaration> vars = new HashMap<>();
Map<String, FunctionDefinition> functionDefs;
Map<String, StructDefinition> structDefs;
@@ -66,25 +68,32 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitBraced_block(KlangParser.Braced_blockContext ctx) {
int actualStatementCount = 0;
int declaredStatementCount = ctx.statement().size();
boolean hasReturn = false;
Statement[] statements = new Statement[declaredStatementCount];
var statementsOrFunctionCalls = new ArrayList<Node>();
var hasReturn = false;
for (int i = 0; i < declaredStatementCount; i++) {
Node currentStatement = this.visit(ctx.statement(i));
statements[i] = (Statement) currentStatement;
actualStatementCount += 1;
for (var child: ctx.children) {
var statementOrFunctionCall = visit(child);
// We use the existence of a type to indicate that this statement returns
// something for which the VariableDeclaration is an exception
if (currentStatement.type != null && !(currentStatement instanceof VariableDeclaration)) {
// The children array contains more than just the statements or function calls
// but everything else evaluates to null, so we can skip it.
if (statementOrFunctionCall == null) {
continue;
}
statementsOrFunctionCalls.add(statementOrFunctionCall);
if (
statementOrFunctionCall.type != null
&& !(statementOrFunctionCall instanceof VariableDeclaration)
&& !statementOrFunctionCall.type.equals(Type.getVoidType())
) {
// check whether the type matches
try {
this.currentDeclaredReturnType.combine(currentStatement.type);
currentDeclaredReturnType.combine(statementOrFunctionCall.type);
} catch (Exception e) {
throw new RuntimeException(
Helper.getErrorPrefix(currentStatement.line, currentStatement.col) + e.getMessage());
var line = statementOrFunctionCall.line;
var col = statementOrFunctionCall.col;
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
// since we have a return guaranteed, every statement
@@ -94,19 +103,12 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
}
}
// If there was unreachable code in this block,
// create a shorter statements array and copy the statements to there
if (actualStatementCount < declaredStatementCount) {
Statement[] newStatements = new Statement[actualStatementCount];
System.arraycopy(statements, 0, newStatements, 0, actualStatementCount);
statements = newStatements;
}
// if this block contains at least one statement that guarantees a return value,
// we indicate that this block guarantees a return value by setting result.type
Block result = new Block(statements);
var result = new Block(statementsOrFunctionCalls.toArray(new Node[0]));
if (hasReturn) {
result.type = this.currentDeclaredReturnType;
result.type = currentDeclaredReturnType;
}
result.line = ctx.start.getLine();
@@ -184,6 +186,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
if (declaredType.equals(Type.getVoidType())) {
var error = "Type " + declaredType.getName() + " can not be used to declare variables.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
if (!declaredType.isPrimitiveType() && !structDefs.containsKey(declaredType.getName()) && !enumDefs.containsKey(declaredType.getName())) {
var error = "Type " + declaredType.getName() + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
@@ -199,8 +206,8 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
if (vars.get(variableName) != null) {
var error = "Redeclaration of variable with name \"" + variableName + "\".";
if (vars.get(variableName) != null || params.get(variableName) != null) {
var error = "Redeclaration of variable or parameter with name \"" + variableName + "\".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
@@ -236,31 +243,27 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitVariable_assignment(KlangParser.Variable_assignmentContext ctx) {
String name = ctx.IDENT().getText();
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var name = ctx.IDENT().getText();
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
VariableDeclaration var = this.vars.get(name);
if (var == null) {
String error = "Variable with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
var variableOrParameter = getVariableOrParameter(name, line, col);
// Evaluate the expression
Expression expression = (Expression) this.visit(ctx.expression());
var expression = (Expression) visit(ctx.expression());
// Make sure expression can be assigned to the variable
try {
expression.type.combine(var.type);
expression.type.combine(variableOrParameter.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
// Since we assigned a value to this variable, we can consider it initialized
var.initialized = true;
variableOrParameter.initialized = true;
// Create a new node and add the type of the expression to it
Node result = new VariableAssignment(name, expression);
var result = new VariableAssignment(name, expression);
result.line = line;
result.col = col;
return result;
@@ -268,66 +271,55 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitReturn_statement(KlangParser.Return_statementContext ctx) {
Expression expression = (Expression) this.visit(ctx.expression());
ReturnStatement result = new ReturnStatement(expression);
if (currentDeclaredReturnType.equals(Type.getVoidType())) {
var result = new ReturnStatement();
result.type = Type.getVoidType();
result.line = ctx.start.getLine();
result.col = ctx.start.getCharPositionInLine();
if (ctx.expression() != null) {
var error = "Cannot return an expression from a void function.";
throw new RuntimeException(Helper.getErrorPrefix(result.line, result.col) + error);
}
return result;
}
var expression = (Expression) visit(ctx.expression());
// Check if this expression is a tail recursion
if (expression instanceof FunctionCall) {
var funCall = (FunctionCall) expression;
if (funCall.name.equals(this.currentFunctionDefinitionName)) {
if (expression instanceof FunctionCall funCall) {
if (funCall.name.equals(currentFunctionDefinitionName)) {
// Flag this function call
funCall.isTailRecursive = true;
}
}
result.type = expression.type;
var result = new ReturnStatement(expression);
result.line = ctx.start.getLine();
result.type = expression.type;
result.col = ctx.start.getCharPositionInLine();
return result;
}
@Override
public Node visitField_assignment(KlangParser.Field_assignmentContext ctx) {
String varName = ctx.IDENT(0).getText();
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
String[] path = new String[ctx.IDENT().size() - 1];
var varName = ctx.IDENT(0).getText();
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
var path = createStructPath(ctx.IDENT());
for (int i = 1; i < ctx.IDENT().size(); i++) {
path[i - 1] = ctx.IDENT(i).getText();
}
var variableOrParameter = getVariableOrParameter(varName, line, col);
ensureReferencesStruct(variableOrParameter, line, col);
var fieldType = drillType(variableOrParameter, path, line, col);
// Get the referenced variable, make sure it is defined
var variableDef = this.vars.get(varName);
if (variableDef == null) {
String error = "Variable with name " + varName + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Make sure it references a struct
if (variableDef.type.isPrimitiveType()) {
String error = "Variable must reference a struct but references " + variableDef.type.getName() + ".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Get the type of the result of this expression
String structName = variableDef.type.getName();
Type fieldType;
try {
fieldType = Helper.drillType(this.structDefs, structName, path, 0);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
// Get the expression and make sure the type combines properly
Expression expression = (Expression) this.visit(ctx.expression());
// Visit the expression and make sure the type combines properly
var expression = (Expression) visit(ctx.expression());
try {
fieldType.combine(expression.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
Node result = new FieldAssignment(varName, structName, path, expression);
var result = new FieldAssignment(varName, variableOrParameter.type.getName(), path, expression);
result.col = col;
result.line = line;
return result;
@@ -338,23 +330,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
var baseName = ctx.IDENT(0).getText();
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
// Create a list of member names. This excludes
// the first entry as it is the base name.
var path = new ArrayList<String>();
for (int i = 1; i < ctx.IDENT().size(); i++) {
path.add(ctx.IDENT(i).getText());
}
var path = createStructPath(ctx.IDENT());
// Determine if the base name points to an enum or a variable
var enumDef = enumDefs.get(baseName);
if (enumDef != null) {
if (path.size() != 1) {
if (path.length != 1) {
var error = "Illegal access to enum " + enumDef.name + ".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
var enumValueName = path.get(0);
var enumValueName = path[0];
var enumValue = Arrays.stream(enumDef.enums)
.filter(e -> e.value.equals(enumValueName))
.findFirst()
@@ -371,29 +357,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
return enumAccessExpression;
}
// Get the referenced variable, make sure it is defined
var variableDef = vars.get(baseName);
if (variableDef == null) {
var error = "Variable with name " + baseName + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
var variableOrParameter = getVariableOrParameter(baseName, line, col);
ensureReferencesStruct(variableOrParameter, line, col);
var resultType = drillType(variableOrParameter, path, line, col);
// Make sure it references a struct
if (variableDef.type.isPrimitiveType()) {
var error = "Variable must reference a struct but references " + variableDef.type.getName() + ".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Get the type of the result of this expression
var structName = variableDef.type.getName();
Type resultType;
try {
resultType = Helper.drillType(structDefs, structName, path.toArray(new String[0]), 0);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
var memberAccessExpression = new MemberAccessExpression(baseName, structName, path.toArray(new String[0]));
var memberAccessExpression = new MemberAccessExpression(baseName, variableOrParameter.type.getName(), path);
memberAccessExpression.type = resultType;
memberAccessExpression.line = line;
memberAccessExpression.col = col;
@@ -459,18 +427,21 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitEqualityExpression(KlangParser.EqualityExpressionContext ctx) {
Node lhs = this.visit(ctx.lhs);
Node rhs = this.visit(ctx.rhs);
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var lhs = visit(ctx.lhs);
var rhs = visit(ctx.rhs);
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
// Since there are countless combinations of types that are comparable
// to each other we use Type::combine to figure out if two types
// may be compared to each other
try {
lhs.type.combine(rhs.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
EqualityExpression result = new EqualityExpression((Expression) lhs, (Expression) rhs);
var result = new EqualityExpression((Expression) lhs, (Expression) rhs);
result.type = Type.getBooleanType();
result.line = line;
result.col = col;
@@ -479,18 +450,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitNotEqualityExpression(KlangParser.NotEqualityExpressionContext ctx) {
Node lhs = this.visit(ctx.lhs);
Node rhs = this.visit(ctx.rhs);
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var lhs = visit(ctx.lhs);
var rhs = visit(ctx.rhs);
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
try {
lhs.type.combine(rhs.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
if (!lhs.type.isPrimitiveType() || !rhs.type.isPrimitiveType()) {
var error = "Can only compare primitives.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
NotEqualityExpression result = new NotEqualityExpression((Expression) lhs, (Expression) rhs);
var result = new NotEqualityExpression((Expression) lhs, (Expression) rhs);
result.type = Type.getBooleanType();
result.line = line;
result.col = col;
@@ -499,20 +469,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitLessThanExpression(KlangParser.LessThanExpressionContext ctx) {
Node lhs = this.visit(ctx.lhs);
Node rhs = this.visit(ctx.rhs);
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var lhs = visit(ctx.lhs);
var rhs = visit(ctx.rhs);
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
try {
lhs.type.combine(rhs.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
if (!lhs.type.isPrimitiveType() || !rhs.type.isPrimitiveType()) {
var error = "Can only compare primitives.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
checkNumeric(lhs, rhs, line, col);
LTExpression result = new LTExpression((Expression) lhs, (Expression) rhs);
var result = new LTExpression((Expression) lhs, (Expression) rhs);
result.type = Type.getBooleanType();
result.line = line;
result.col = col;
@@ -521,23 +488,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitGreaterThanExpression(KlangParser.GreaterThanExpressionContext ctx) {
Node lhs = this.visit(ctx.lhs);
Node rhs = this.visit(ctx.rhs);
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var lhs = visit(ctx.lhs);
var rhs = visit(ctx.rhs);
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
try {
lhs.type.combine(rhs.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
if (!lhs.type.isNumericType() || !rhs.type.isNumericType()) {
String error = "Only numeric types are allowed for this expression.";
if (!lhs.type.isPrimitiveType() || !rhs.type.isPrimitiveType()) {
var error = "Can only compare primitives.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
GTExpression result = new GTExpression((Expression) lhs, (Expression) rhs);
var result = new GTExpression((Expression) lhs, (Expression) rhs);
result.type = Type.getBooleanType();
result.line = line;
result.col = col;
@@ -546,20 +507,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitLessThanOrEqualToExpression(KlangParser.LessThanOrEqualToExpressionContext ctx) {
Node lhs = this.visit(ctx.lhs);
Node rhs = this.visit(ctx.rhs);
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var lhs = visit(ctx.lhs);
var rhs = visit(ctx.rhs);
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
try {
lhs.type.combine(rhs.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
if (!lhs.type.isPrimitiveType() || !rhs.type.isPrimitiveType()) {
var error = "Can only compare primitives.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
checkNumeric(lhs, rhs, line, col);
LTEExpression result = new LTEExpression((Expression) lhs, (Expression) rhs);
var result = new LTEExpression((Expression) lhs, (Expression) rhs);
result.type = Type.getBooleanType();
result.line = line;
result.col = col;
@@ -568,20 +526,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitGreaterThanOrEqualToExpression(KlangParser.GreaterThanOrEqualToExpressionContext ctx) {
Node lhs = this.visit(ctx.lhs);
Node rhs = this.visit(ctx.rhs);
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var lhs = visit(ctx.lhs);
var rhs = visit(ctx.rhs);
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
try {
lhs.type.combine(rhs.type);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
if (!lhs.type.isPrimitiveType() || !rhs.type.isPrimitiveType()) {
var error = "Can only compare primitives.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
checkNumeric(lhs, rhs, line, col);
GTEExpression result = new GTEExpression((Expression) lhs, (Expression) rhs);
var result = new GTEExpression((Expression) lhs, (Expression) rhs);
result.type = Type.getBooleanType();
result.line = line;
result.col = col;
@@ -603,7 +558,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
}
checkNumeric(lhs, rhs, line, col);
result.line = line;
result.col = col;
return result;
@@ -637,7 +592,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
DivisionExpression result = new DivisionExpression((Expression) lhs, (Expression) rhs);
try {
result.type = lhs.type.combine(rhs.type);
} catch (Exception e) {
@@ -645,7 +600,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
}
checkNumeric(lhs, rhs, line, col);
result.line = line;
result.col = col;
return result;
@@ -671,7 +626,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
String error = "Only integers are allowed for modulo.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
result.line = line;
result.col = col;
return result;
@@ -684,7 +639,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
result.type = expression.type;
result.line = ctx.start.getLine();
result.col = ctx.start.getCharPositionInLine();
if (!result.type.isNumericType()) {
String error = "Only numeric types are allowed for this expression.";
throw new RuntimeException(Helper.getErrorPrefix(result.line, result.col) + error);
@@ -711,24 +666,20 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitVariable(KlangParser.VariableContext ctx) {
String name = ctx.IDENT().getText();
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
var variableName = ctx.IDENT().getText();
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
VariableDeclaration var = this.vars.get(name);
if (var == null) {
String error = "Variable with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
var variableOrParameter = getVariableOrParameter(variableName, line, col);
// Make sure the variable has been initialized before it can be used
if (!var.initialized) {
String error = "Variable with name \"" + name + "\" has not been initialized.";
if (!variableOrParameter.initialized) {
var error = "Variable with name \"" + variableName + "\" has not been initialized.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
Variable result = new Variable(ctx.IDENT().getText());
result.type = var.type;
var result = new Variable(ctx.IDENT().getText());
result.type = variableOrParameter.type;
result.line = line;
result.col = col;
return result;
@@ -766,6 +717,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
return n;
}
@Override
public Node visitCharAtom(KlangParser.CharAtomContext ctx) {
// I had no idea how to design the grammar so that the quotes
// are separate from the char, so I cheat here...
var n = new CharExpression(ctx.CHAR_LITERAL().getText().charAt(1));
n.type = Type.getCharType();
n.line = ctx.start.getLine();
n.col = ctx.start.getCharPositionInLine();
return n;
}
@Override
public Node visitNullAtom(KlangParser.NullAtomContext ctx) {
Node n = new NullExpression();
@@ -802,6 +764,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
if (structFieldType.equals(Type.getVoidType())) {
var error = "Type void can not be used as a struct field type.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
if (!structFieldType.isPrimitiveType() && !structDefs.containsKey(structFieldType.getName()) && !enumDefs.containsKey(structFieldType.getName())) {
var error = "Type " + structFieldType.getName() + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
@@ -832,42 +799,43 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
currentDeclaredReturnType = returnType;
currentFunctionDefinitionName = name;
if (!returnType.isPrimitiveType() && !structDefs.containsKey(returnType.getName()) && !enumDefs.containsKey(returnType.getName())) {
var typeNotDefined = !returnType.isPrimitiveType() && !structDefs.containsKey(returnType.getName()) && !enumDefs.containsKey(returnType.getName());
if (!returnType.equals(Type.getVoidType()) && typeNotDefined) {
var line = ctx.returnType.start.getLine();
var col = ctx.returnType.start.getCharPositionInLine();
var error = "Type " + returnType.getName() + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Create a new set for the variables of the current function
// Create a new set for the variables and parameters of the current function
// this will be filled in the variable declaration visitor as well
vars = new HashMap<>();
params = new HashMap<>();
// Process the parameter list by visiting every parameter in it
var paramCount = ctx.params.parameter().size();
var params = new Parameter[paramCount];
var functionParameters = new Parameter[paramCount]; // the list of parameters that get passed to FunctionDefinition
for (int i = 0; i < paramCount; i++) {
// Add the parameter to the list of parameters
var param = (Parameter) visit(ctx.params.parameter(i));
params[i] = param;
functionParameters[i] = (Parameter) visit(ctx.params.parameter(i));
// add the param as a variable
var var = new VariableDeclaration(param.name);
var.initialized = true; // parameters can always be considered initialized
var.type = param.type;
vars.put(param.name, var);
// add the param as a variable to the global parameter map so that
// child nodes can access them.
var param = new VariableDeclaration(functionParameters[i].name);
param.initialized = true; // parameters can always be considered initialized
param.type = functionParameters[i].type;
params.put(param.name, param);
}
// Visit the block, make sure that a return value is guaranteed
var block = visit(ctx.braced_block());
if (block.type == null) {
if (block.type == null && !returnType.equals(Type.getVoidType())) {
var line = ctx.braced_block().start.getLine();
var col = ctx.braced_block().start.getCharPositionInLine();
var error = "Function " + name + " has to return something of type " + returnType.getName() + ".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
var functionDef = new FunctionDefinition(name, params, (Block) block);
var functionDef = new FunctionDefinition(name, functionParameters, vars.values().toArray(new VariableDeclaration[0]), (Block) block);
functionDef.type = returnType;
functionDef.line = ctx.start.getLine();
functionDef.col = ctx.start.getCharPositionInLine();
@@ -879,26 +847,29 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
public Node visitParameter(KlangParser.ParameterContext ctx) {
var parameterName = ctx.IDENT().getText();
var parameterType = Type.getByName(ctx.type_annotation().type().getText());
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
if (parameterType.equals(Type.getVoidType())) {
var error = "Type " + parameterType.getName() + " cannot be used to declare a parameter.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
if (structDefs.containsKey(parameterName)) {
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
var error = "Parameter name " + parameterName + " duplicates a struct of the same name.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
if (enumDefs.containsKey(parameterName)) {
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
var error = "Parameter name " + parameterName + " duplicates an enum of the same name.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
if (!parameterType.isPrimitiveType() && !structDefs.containsKey(parameterType.getName()) && !enumDefs.containsKey(parameterType.getName())) {
var line = ctx.type_annotation().start.getLine();
var col = ctx.type_annotation().start.getCharPositionInLine();
var typeLine = ctx.type_annotation().start.getLine();
var typeCol = ctx.type_annotation().start.getCharPositionInLine();
var error = "Type " + parameterType.getName() + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
throw new RuntimeException(Helper.getErrorPrefix(typeLine, typeCol) + error);
}
var parameter = new Parameter(parameterName);
@@ -910,55 +881,60 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
}
@Override
public Node visitFunctionCallExpression(KlangParser.FunctionCallExpressionContext ctx) {
String name = ctx.functionCall().IDENT().getText();
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
public Node visitFunctionCall(KlangParser.FunctionCallContext ctx) {
var name = ctx.IDENT().getText();
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
var functionDef = this.functionDefs.get(name);
var functionDef = functionDefs.get(name);
if (functionDef == null) {
String error = "Function with name \"" + name + "\" not defined.";
var error = "Function with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Make sure the number of arguments matches the number of parameters
int argCount = ctx.functionCall().arguments().expression().size();
int paramCount = functionDef.parameters.length;
var argCount = ctx.arguments().expression().size();
var paramCount = functionDef.parameters.length;
if (argCount != paramCount) {
String error = "Function \"" + name + "\" expects " + paramCount + " parameters, but got " + argCount + ".";
var error = "Function \"" + name + "\" expects " + paramCount + " parameters, but got " + argCount + ".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Evaluate every argument
Expression[] args = new Expression[argCount];
var args = new Expression[argCount];
for (int i = 0; i < argCount; i++) {
Expression expression = (Expression) this.visit(ctx.functionCall().arguments().expression(i));
var expression = (Expression) visit(ctx.arguments().expression(i));
if (!expression.type.equals(functionDef.parameters[i].type)) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + "argument " + i + " Expected " + functionDef.parameters[i].type.getName() + " but got: " + expression.type.getName());
}
args[i] = expression;
}
FunctionCall result = new FunctionCall(name, args);
var result = new FunctionCall(name, args);
result.type = functionDef.type;
result.line = line;
result.col = col;
return result;
}
@Override
public Node visitFunctionCallExpression(KlangParser.FunctionCallExpressionContext ctx) {
return visit(ctx.functionCall());
}
@Override
public Node visitConstructorCallExpression(KlangParser.ConstructorCallExpressionContext ctx) {
String name = ctx.IDENT().getText();
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
// Get the corresponding struct definition
var struct = this.structDefs.get(name);
if (struct == null) {
String error = "Struct with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Make sure the number of arguments match the number of struct fields
int fieldCount = struct.fields.length;
int argCount = ctx.arguments().expression().size();
@@ -988,19 +964,52 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitDestroy_statement(KlangParser.Destroy_statementContext ctx) {
String name = ctx.IDENT().getText();
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
VariableDeclaration var = this.vars.get(name);
if (var == null) {
String error = "Variable with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
var varName = ctx.IDENT().getText();
var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine();
Node result = new DestructorCall(name);
var variableOrParameter = getVariableOrParameter(varName, line, col);
ensureReferencesStruct(variableOrParameter, line, col);
var result = new DestructorCall(varName);
result.line = line;
result.col = col;
return result;
}
}
private VariableDeclaration getVariableOrParameter(String name, int line, int col) {
var variable = vars.get(name);
var parameter = params.get(name);
if (variable == null && parameter == null) {
var error = "Variable or parameter with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
return variable != null ? variable : parameter;
}
private void ensureReferencesStruct(VariableDeclaration variableOrParameter, int line, int col) {
if (variableOrParameter.type.isPrimitiveType() || enumDefs.containsKey(variableOrParameter.type.getName())) {
var error = "Variable or parameter must reference a struct but references " + variableOrParameter.type.getName() + ".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
}
private Type drillType(VariableDeclaration variableOrParameter, String[] path, int line, int col) {
try {
var structName = variableOrParameter.type.getName();
return Helper.drillType(structDefs, structName, path, 0);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
}
private String[] createStructPath(List<TerminalNode> identifiers) {
// Create a list of member names. This excludes the first entry as it is the base name.
var path = new String[identifiers.size() - 1];
for (int i = 1; i < identifiers.size(); i++) {
path[i - 1] = identifiers.get(i).getText();
}
return path;
}
}

View File

@@ -210,7 +210,7 @@ public class GetDefinitions extends KlangBaseVisitor<Node> {
parameters.put(paramName, parameter);
}
var functionDef = new FunctionDefinition(funcName, parameters.values().toArray(new Parameter[0]), null);
var functionDef = new FunctionDefinition(funcName, parameters.values().toArray(new Parameter[0]), null, null);
functionDef.type = Type.getByName(ctx.returnType.type().getText());
functionDef.line = line;
functionDef.col = col;

View File

@@ -1,9 +1,11 @@
package de.hsrm.compiler.Klang;
import de.hsrm.compiler.Klang.helper.*;
import de.hsrm.compiler.Klang.nodes.EnumDefinition;
import de.hsrm.compiler.Klang.nodes.FunctionDefinition;
import de.hsrm.compiler.Klang.nodes.Node;
import de.hsrm.compiler.Klang.nodes.StructDefinition;
import de.hsrm.compiler.Klang.types.Type;
import de.hsrm.compiler.Klang.visitors.EvalVisitor;
import de.hsrm.compiler.Klang.visitors.GenASM;
import de.hsrm.compiler.Klang.visitors.PrettyPrintVisitor;
@@ -120,7 +122,11 @@ public class Klang {
System.out.println("\nEvaluating the source code:");
EvalVisitor evalVisitor = new EvalVisitor(structDefs);
Value result = root.welcome(evalVisitor);
generateOutput(out, "Result was: " + result.asObject().toString());
if (result.type.equals(Type.getVoidType())) {
generateOutput(out, "Result was void");
} else {
generateOutput(out, "Result was: " + result.asObject().toString());
}
return;
}

View File

@@ -4,8 +4,8 @@ import de.hsrm.compiler.Klang.types.Type;
import java.util.Map;
public class Value {
private final Object value;
public Type type;
private Object value;
public Value(Object value) {
this.value = value;
@@ -17,19 +17,23 @@ public class Value {
}
public Object asObject() {
return this.value;
return value;
}
public int asInteger() {
return (int) this.value;
return (int) value;
}
public double asFloat() {
return (double) this.value;
return (double) value;
}
public boolean asBoolean() {
return (boolean) this.value;
return (boolean) value;
}
public char asChar() {
return (char) value;
}
@SuppressWarnings("unchecked")

View File

@@ -172,6 +172,10 @@ public class ASM {
mnemonics.add(new Cqto());
}
public void leave() {
mnemonics.add(new Leave());
}
public void ret() {
mnemonics.add(new Ret());
}

View File

@@ -0,0 +1,8 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Leave extends NoOperandMnemonic {
@Override
public String toAsm() {
return "leave";
}
}

View File

@@ -1,14 +1,13 @@
package de.hsrm.compiler.Klang.nodes;
import de.hsrm.compiler.Klang.nodes.statements.Statement;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class Block extends Node {
public Statement[] statements;
public Node[] statementsOrFunctionCalls;
public Block(Statement[] statements) {
this.statements = statements;
public Block(Node[] statements) {
this.statementsOrFunctionCalls = statements;
}
@Override

View File

@@ -1,17 +1,20 @@
package de.hsrm.compiler.Klang.nodes;
import de.hsrm.compiler.Klang.nodes.statements.VariableDeclaration;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class FunctionDefinition extends Node {
public String name;
public Parameter[] parameters;
public VariableDeclaration[] localVariables;
public Block block;
public FunctionDefinition(String name, Parameter[] parameters, Block block) {
public FunctionDefinition(String name, Parameter[] parameters, VariableDeclaration[] localVariables, Block block) {
this.name = name;
this.parameters = parameters;
this.block = block;
this.localVariables = localVariables;
}
@Override

View File

@@ -0,0 +1,16 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class CharExpression extends Expression {
public char c;
public CharExpression(char c) {
this.c = c;
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -11,6 +11,10 @@ public class ReturnStatement extends Statement {
this.expression = expression;
}
public ReturnStatement() {
this.expression = null;
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);

View File

@@ -32,7 +32,7 @@ public class BooleanType extends PrimitiveType {
}
// Every remaining type will throw a RuntimeException
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
throw new RuntimeException("Type mismatch: cannot combine " + this.getName() + " and " + that.getName());
}
@Override

View File

@@ -0,0 +1,47 @@
package de.hsrm.compiler.Klang.types;
import de.hsrm.compiler.Klang.Value;
public class CharType extends PrimitiveType {
private static CharType instance = null;
public static CharType getType() {
if (instance != null) {
return instance;
}
instance = new CharType();
return instance;
}
@Override
public String getName() {
return "char";
}
@Override
public Type combine(Type that) {
if (this.equals(that)) {
return this;
}
if (that.equals(Type.getIntegerType())) {
return this;
}
throw new RuntimeException("Type mismatch: cannot combine " + getName() + " and " + that.getName());
}
@Override
public boolean valuesEqual(Value a, Value b) {
return ((int) a.asChar()) == ((int) b.asChar());
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
return that instanceof CharType;
}
}

View File

@@ -36,7 +36,7 @@ public class FloatType extends NumericType {
}
// Every remaining type will throw a RuntimeException
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
throw new RuntimeException("Type mismatch: cannot combine " + this.getName() + " and " + that.getName());
}
@Override

View File

@@ -35,8 +35,12 @@ public class IntegerType extends NumericType {
return Type.getFloatType();
}
if (that.equals(Type.getCharType())) {
return Type.getCharType();
}
// Every remaining type will throw a RuntimeException
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
throw new RuntimeException("Type mismatch: cannot combine " + this.getName() + " and " + that.getName());
}
@Override

View File

@@ -1,11 +1,6 @@
package de.hsrm.compiler.Klang.types;
import de.hsrm.compiler.Klang.Value;
import de.hsrm.compiler.Klang.nodes.EnumDefinition;
import de.hsrm.compiler.Klang.nodes.FunctionDefinition;
import de.hsrm.compiler.Klang.nodes.StructDefinition;
import java.util.Map;
public class NamedType extends Type {
public String name;
@@ -21,7 +16,7 @@ public class NamedType extends Type {
@Override
public Type combine(Type that) {
if(this.equals(that)) {
if(this.equals(that) || (that instanceof NullType)) {
return this;
}
@@ -49,8 +44,7 @@ public class NamedType extends Type {
return true;
}
if (that instanceof NamedType) {
var thatType = (NamedType) that;
if (that instanceof NamedType thatType) {
return getName().equals(thatType.getName());
}

View File

@@ -23,7 +23,7 @@ public class NullType extends Type {
public Type combine(Type that) {
// You can not combine null with a primitive type
if (that.isPrimitiveType()) {
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
throw new RuntimeException("Type mismatch: cannot combine " + this.getName() + " and " + that.getName());
}
// Everything else combines with null to the type it was before

View File

@@ -23,16 +23,26 @@ public abstract class Type {
return FloatType.getType();
}
public static CharType getCharType() {
return CharType.getType();
}
public static NullType getNullType() {
return NullType.getType();
}
public static VoidType getVoidType() {
return VoidType.getType();
}
public static Type getByName(String name) {
switch (name) {
case "bool": return getBooleanType();
case "int": return getIntegerType();
case "float": return getFloatType();
case "char": return getCharType();
case "null": return getNullType();
case "void": return getVoidType();
default: return new NamedType(name);
}
}
@@ -42,4 +52,4 @@ public abstract class Type {
public abstract boolean valuesEqual(Value a, Value b);
public abstract boolean isPrimitiveType();
public abstract boolean isNumericType();
}
}

View File

@@ -0,0 +1,45 @@
package de.hsrm.compiler.Klang.types;
import de.hsrm.compiler.Klang.Value;
public class VoidType extends Type {
private static VoidType instance;
public static VoidType getType() {
if (instance != null) {
return instance;
}
instance = new VoidType();
return instance;
}
@Override
public String getName() {
return "void";
}
@Override
public Type combine(Type that) {
if (that.equals(this)) {
return this;
}
throw new RuntimeException("Type mismatch: cannot combine " + this.getName() + " and " + that.getName());
}
@Override
public boolean valuesEqual(Value a, Value b) {
throw new RuntimeException("Can not compare void types.");
}
@Override
public boolean isPrimitiveType() {
return false;
}
@Override
public boolean isNumericType() {
return false;
}
}

View File

@@ -10,6 +10,7 @@ import de.hsrm.compiler.Klang.nodes.loops.DoWhileLoop;
import de.hsrm.compiler.Klang.nodes.loops.ForLoop;
import de.hsrm.compiler.Klang.nodes.loops.WhileLoop;
import de.hsrm.compiler.Klang.nodes.statements.*;
import de.hsrm.compiler.Klang.types.NullType;
import de.hsrm.compiler.Klang.types.Type;
public class EvalVisitor implements Visitor<Value> {
@@ -44,6 +45,11 @@ public class EvalVisitor implements Visitor<Value> {
return result;
}
@Override
public Value visit(CharExpression e) {
return new Value(e.c, Type.getCharType());
}
@Override
public Value visit(EqualityExpression e) {
var lhs = e.lhs.welcome(this);
@@ -394,13 +400,16 @@ public class EvalVisitor implements Visitor<Value> {
@Override
public Value visit(ReturnStatement e) {
if (e.expression == null) {
return new Value(null, Type.getVoidType());
}
return e.expression.welcome(this);
}
@Override
public Value visit(Block e) {
for (var stmt : e.statements) {
Value result = stmt.welcome(this);
for (var stmt : e.statementsOrFunctionCalls) {
var result = stmt.welcome(this);
if (result != null) {
return result;
}
@@ -428,7 +437,7 @@ public class EvalVisitor implements Visitor<Value> {
this.env = newEnv;
// Execute
Value result = func.block.welcome(this);
var result = func.block.welcome(this);
// Das alte env wiederherstellen
this.env = oldEnv;
@@ -502,12 +511,17 @@ public class EvalVisitor implements Visitor<Value> {
struct.put(structDef.fields[i].name, arg);
}
return new Value(struct);
var structValue = new Value(struct);
structValue.type = structDef.type;
return structValue;
}
@Override
public Value visit(NullExpression e) {
return null;
var nullValue = new Value(null);
nullValue.type = new NullType();
return nullValue;
}
@Override

View File

@@ -13,16 +13,16 @@ import de.hsrm.compiler.Klang.types.Type;
import java.util.*;
public class GenASM implements Visitor<Void> {
private class FloatWriter {
private StringBuilder sb = new StringBuilder();
private static class FloatWriter {
private final StringBuilder sb = new StringBuilder();
private int id = -1;
public String getFloat(double d) {
Long longBits = Double.doubleToRawLongBits(d);
String binary = Long.toBinaryString(longBits);
long longBits = Double.doubleToRawLongBits(d);
StringBuilder binary = new StringBuilder(Long.toBinaryString(longBits));
int padCount = 64 - binary.length();
while (padCount > 0) {
binary = "0" + binary;
binary.insert(0, "0");
padCount--;
}
String upper = binary.substring(0, 32);
@@ -59,49 +59,19 @@ public class GenASM implements Visitor<Void> {
}
}
private ASM asm;
private FloatWriter fw = new FloatWriter();
private String mainName;
private final ASM asm;
private final FloatWriter fw = new FloatWriter();
private final String mainName;
Map<String, Integer> env = new HashMap<>();
Map<String, StructDefinition> structs;
Set<String> vars;
String[] registers = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" };
String[] floatRegisters = { "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7" };
private int lCount = 0; // Invariant: lCount is used
private int currentFunctionStartLabel = 0;
private Parameter[] currentFunctionParams;
private boolean prepareRegisters(Expression lhs, Expression rhs) {
boolean lhsIsFloat = lhs.type.equals(Type.getFloatType());
boolean rhsIsFloat = rhs.type.equals(Type.getFloatType());
if (lhsIsFloat && rhsIsFloat) {
lhs.welcome(this);
asm.mov("sd", "%xmm0", "%xmm2");
rhs.welcome(this);
asm.mov("sd", "%xmm2", "%xmm0");
asm.mov("sd", "%xmm2", "%xmm0");
return true;
} else if (lhsIsFloat && !rhsIsFloat) {
lhs.welcome(this);
rhs.welcome(this);
asm.cvtsi2sd("%rax", "%xmm1");
return true;
} else if (!lhsIsFloat && rhsIsFloat) {
lhs.welcome(this);
asm.cvtsi2sd("%rax", "%xmm2");
rhs.welcome(this);
asm.mov("sd", "%xmm0", "%xmm1");
asm.mov("sd", "%xmm2", "%xmm0");
return true;
} else {
lhs.welcome(this);
asm.push("q", "%rax");
rhs.welcome(this);
asm.mov("q", "%rax", "%rbx");
asm.pop("q", "%rax");
return false;
}
}
private int lCount = 0;
private int currentFunctionStartLabel = 0;
private long bytesToClearFromTheStack = 0;
private FunctionDefinition currentFunctionDef;
public GenASM(String mainName, Map<String, StructDefinition> structs) {
this.mainName = mainName;
@@ -109,10 +79,6 @@ public class GenASM implements Visitor<Void> {
this.asm = new ASM();
}
public GenASM(Map<String, StructDefinition> structs) {
this("main", structs);
}
public String toAsm() {
return asm.toAsm();
}
@@ -136,6 +102,12 @@ public class GenASM implements Visitor<Void> {
return null;
}
@Override
public Void visit(CharExpression e) {
asm.mov("q", "$" + ((int) e.c), "%rax");
return null;
}
@Override
public Void visit(Variable e) {
if (e.type.equals(Type.getFloatType())) {
@@ -326,10 +298,19 @@ public class GenASM implements Visitor<Void> {
@Override
public Void visit(ModuloExpression e) {
e.lhs.welcome(this);
// A single pushq brings the stack out of 16 byte alignment,
// so we need to increase the stack by another 8 bytes
asm.sub("q", "$8", "%rsp");
asm.push("q", "%rax");
e.rhs.welcome(this);
asm.mov("q", "%rax", "%rbx");
asm.pop("q", "%rax");
// Which we remove afterwards
asm.add("q", "$8", "%rsp");
asm.cqto();
asm.idiv("q", "%rbx");
asm.mov("q", "%rdx", "%rax");
@@ -357,7 +338,7 @@ public class GenASM implements Visitor<Void> {
// Werte LHS aus
// Wenn LHS != 0 bedeutet das true
// also können wir direkt sagen dass das Ergebnis true ist
// also können wir direkt sagen, dass das Ergebnis true ist
e.lhs.welcome(this);
asm.cmp("q", 0, "%rax");
asm.jne(lblTrue);
@@ -375,11 +356,11 @@ public class GenASM implements Visitor<Void> {
asm.mov("q", 1, "%rax");
asm.jmp(lblEnd);
// Die Expressoin wertet zu false aus
// Die Expression wertet zu false aus
asm.label(lblFalse);
asm.mov("q", 0, "%rax");
// Das hier ist das ende
// Das hier ist das Ende
asm.label(lblEnd);
return null;
}
@@ -392,7 +373,7 @@ public class GenASM implements Visitor<Void> {
// Werte LHS aus
// Wenn LHS == 0, bedeutet das false
// also können wir direkt sagen dass das Ergebnis false ist
// also können wir direkt sagen, dass das Ergebnis false ist
e.lhs.welcome(this);
asm.cmp("q", 0, "%rax");
asm.je(lblFalse);
@@ -410,11 +391,11 @@ public class GenASM implements Visitor<Void> {
asm.mov("q", 1, "%rax");
asm.jmp(lblEnd);
// Die Expressoin wertet zu false aus
// Die Expression wertet zu false aus
asm.label(lblFalse);
asm.mov("q", 0, "%rax");
// Das hier ist das ende
// Das hier ist das Ende
asm.label(lblEnd);
return null;
}
@@ -424,9 +405,9 @@ public class GenASM implements Visitor<Void> {
int lblFalse = ++lCount;
int lblEnd = ++lCount;
// Werte LHS aus
// Wenn LHS != 0 bedeutet das true, also jumpe zum false Teil
// Wenn nicht, falle durch zum true Teil
// Werte LHS aus.
// Wenn LHS != 0 bedeutet das true, also springe zum false Teil.
// Wenn nicht, falle durch zum true Teil.
e.lhs.welcome(this);
asm.cmp("q", 0, "%rax");
asm.jne(lblFalse);
@@ -550,18 +531,41 @@ public class GenASM implements Visitor<Void> {
@Override
public Void visit(ReturnStatement e) {
e.expression.welcome(this);
asm.mov("q", "%rbp", "%rsp");
asm.pop("q", "%rbp");
if (e.expression != null) {
e.expression.welcome(this);
}
// The ReturnStatement visitor is kindly removing the
// stack space that was allocated by the FunctionDefinition
// before executing the function body.
if (bytesToClearFromTheStack > 0) {
asm.add("q", "$" + bytesToClearFromTheStack, "%rsp");
}
asm.leave();
asm.ret();
return null;
}
@Override
public Void visit(Block e) {
for (var statement : e.statements) {
statement.welcome(this);
for (var statementOrFunctionCall : e.statementsOrFunctionCalls) {
statementOrFunctionCall.welcome(this);
}
// It's possible (and allowed -> void functions) that there is no return statement
// in the outermost block of a function body. In this case, no
// direct descendant of Block will be a return statement.
// This means we have to generate an implicit return, otherwise
// the code would fall through to the next function below :D
// We also have to clean up the stack which is the ReturnStatement's job.
// Check if we have to generate an implicit return
var lastStatementOrFunctionCall = e.statementsOrFunctionCalls[e.statementsOrFunctionCalls.length - 1];
if (currentFunctionDef.block == e && !(lastStatementOrFunctionCall instanceof ReturnStatement)) {
visit(new ReturnStatement());
}
return null;
}
@@ -574,32 +578,29 @@ public class GenASM implements Visitor<Void> {
e.name = "main_by_user";
}
int lblStart = ++lCount;
this.currentFunctionStartLabel = lblStart;
this.currentFunctionParams = e.parameters;
// Remember the current function definition so everyone below us knows where they belong to.
currentFunctionDef = e;
var lblStart = ++lCount;
currentFunctionStartLabel = lblStart;
asm.functionHead(e.name);
asm.push("q", "%rbp");
asm.mov("q", "%rsp", "%rbp");
asm.label(lblStart);
// hole die anzahl der lokalen variablen
this.vars = new TreeSet<String>();
HashMap<String, Type> types = new HashMap<String, Type>();
GetVars getvars = new GetVars(vars, types);
getvars.visit(e);
// Create a new environment
env = new HashMap<>();
// Erzeuge ein environment
this.env = new HashMap<String, Integer>();
// Remember the offsets of the arguments that were placed on the stack.
var offset = 16; // Variables that were passed via the stack are located above our BSP
var ri = 0;
var fi = 0;
// Merke dir die offsets der parameter, die direkt auf den stack gelegt wurden
int offset = 16; // Per Stack übergebene Parameter liegen über unserem BSP
int ri = 0;
int fi = 0;
// Per stack übergebene variablen in env registrieren
// Add arguments that were passed via the stack to the environment
var registerParameters = new ArrayList<Parameter>();
for (int i = 0; i < e.parameters.length; i++) {
if (e.parameters[i].type.equals(Type.getFloatType())) {
if (fi >= this.floatRegisters.length) {
if (fi >= floatRegisters.length) {
// parameter is on stack already
env.put(e.parameters[i].name, offset);
offset += 8;
@@ -609,7 +610,7 @@ public class GenASM implements Visitor<Void> {
fi++;
}
} else {
if (ri >= this.registers.length) {
if (ri >= registers.length) {
// parameter is on stack already
env.put(e.parameters[i].name, offset);
offset += 8;
@@ -624,29 +625,58 @@ public class GenASM implements Visitor<Void> {
offset = 0;
ri = 0;
fi = 0;
// Push arguments that were passed via registers onto the stack
// and add them to the environment.
for (var param: registerParameters) {
if (param.type.equals(Type.getFloatType())) {
asm.mov("q", this.floatRegisters[fi], "%rax");
asm.mov("q", floatRegisters[fi], "%rax");
asm.push("q", "%rax");
offset -= 8;
this.env.put(param.name, offset); // negative, liegt unter aktuellem BP
env.put(param.name, offset); // negative, liegt unter aktuellem BP
fi++;
} else {
asm.push("q", this.registers[ri]);
asm.push("q", registers[ri]);
offset -= 8;
this.env.put(param.name, offset); // negative, liegt unter aktuellem BP
env.put(param.name, offset); // negative, liegt unter aktuellem BP
ri++;
}
}
// Reserviere Platz auf dem Stack für jede lokale variable
for (String lok_var : vars) {
offset -= 8;
asm.push("q", 0);
this.env.put(lok_var, offset);
// Reserve memory on the stack for the local variables.
if (e.localVariables.length > 0) {
// Each variable is at most 8 bytes in size.
asm.sub("q", "$" + (8 * e.localVariables.length), "%rsp");
// Save the offsets (they are relative to rbp)
for (var localVariable : e.localVariables) {
offset -= 8;
env.put(localVariable.name, offset);
}
}
// Check the stack alignment and correct if necessary
// The padding needs to happen after the register parameters
// and local variables were pushed to the stack because otherwise
// the relative offsets to rbp will be wrong.
if ((registerParameters.size() + e.localVariables.length) % 2 != 0) {
// Since each variable is 8 bytes and the stack is 16 byte aligned
// we need to add 8 bytes to the stack to make it aligned
// if there is an odd number of parameters and local variables.
asm.sub("q", "$8", "%rsp");
}
// I need to clear the stack here so that the top most element is the old rbp that
// ret uses to return to the caller but code that gets generated after visiting the
// function body will never be reached since the block node is guaranteed to
// contain a return node that results in a ret command being executed before this
// code would be reached. As a workaround (and a dirty, dirty hack) I indicate
// to the return node visitor how many bytes need to be cleared from the stack.
var wasStackPadded = (registerParameters.size() + e.localVariables.length) % 2 != 0;
bytesToClearFromTheStack = 8L * (registerParameters.size() + e.localVariables.length + (wasStackPadded ? 1 : 0));
e.block.welcome(this);
return null;
}
@@ -665,15 +695,18 @@ public class GenASM implements Visitor<Void> {
asm.push("q", "%rax");
}
// push args into local var locations, last arg is ontop of the stack
// push args into local var locations, last arg is on top of the stack
for (int i = e.arguments.length - 1; i >= 0; i--) {
asm.pop("q", this.env.get(this.currentFunctionParams[i].name) + "(%rbp)");
asm.pop("q", env.get(currentFunctionDef.parameters[i].name) + "(%rbp)");
}
asm.jmp(this.currentFunctionStartLabel);
asm.jmp(currentFunctionStartLabel);
return null;
}
// This holds the indices into the arguments array for
// arguments that need to be passed to the callee via the stack
var stackIndices = new ArrayList<Integer>();
if (e.arguments.length > 0) {
// Mapping arguments index -> xmm registers index
@@ -684,9 +717,6 @@ public class GenASM implements Visitor<Void> {
int[] rIdxs = new int[this.registers.length];
int ri = -1;
// Mapping arguments index -> stack
ArrayList<Integer> stackIdxs = new ArrayList<Integer>();
// Go through arguments
// sort them into the memory regions they go when being passed to functions
for (int i = 0; i < e.arguments.length; i++) {
@@ -698,7 +728,7 @@ public class GenASM implements Visitor<Void> {
xmmIdxs[fi] = i;
} else {
// Float onto stack
stackIdxs.add(i);
stackIndices.add(i);
}
} else {
if (ri < this.registers.length - 1) {
@@ -707,14 +737,39 @@ public class GenASM implements Visitor<Void> {
rIdxs[ri] = i;
} else {
// bool/int onto stack
stackIdxs.add(i);
stackIndices.add(i);
}
}
}
// Welcome the arguments in order, push everything onto the stack
for (var arg : e.arguments) {
// Make sure that the stack is aligned after all arguments
// have been pushed onto the stack. Keep in mind that we
// may remove the top n elements from our stack so they form
// the base of the callee's stack.
if ((e.arguments.length - stackIndices.size()) % 2 != 0) {
asm.sub("q", "$8", "%rsp");
}
// Welcome the arguments in order, push everything onto the stack.
// We can assume that we are currently 16 byte aligned which holds
// true until after the result of the first argument is pushed onto
// the stack. After that we have to pad the stack for every argument
// to ensure that the stack is aligned before welcoming the argument.
// We get rid of the padding directly after welcome() to
// "tightly pack" the arguments on the stack. This way the usual
// 8 byte offsets are kept intact.
for (int i = 0; i < e.arguments.length; i++) {
if (i % 2 == 0) {
asm.sub("q", "$8", "%rsp");
}
var arg = e.arguments[i];
arg.welcome(this);
if (i % 2 == 0) {
asm.add("q", "$8", "%rsp");
}
if (arg.type.equals(Type.getFloatType())) {
asm.mov("q", "%xmm0", "%rax");
asm.push("q", "%rax");
@@ -737,22 +792,38 @@ public class GenASM implements Visitor<Void> {
asm.mov("q", rspOffset, "%rsp", this.registers[i]);
}
// Move everything else from a higher stack position to our stack frame start
// Reorder the remaining n arguments that did not fit in a register,
// so that the remaining n arguments occupy the last n spots on our current stack
// This does not manipulate the stack in any way. It just reorders it.
int stackStartOffset = ((e.arguments.length) * 8);
for (int i = stackIdxs.size() - 1; i >= 0; i--) {
for (int i = stackIndices.size() - 1; i >= 0; i--) {
stackStartOffset -= 8;
int indexInArguments = stackIdxs.get(i);
int indexInArguments = stackIndices.get(i);
int rspOffset = (((e.arguments.length - indexInArguments) - 1) * 8);
asm.mov("q", rspOffset, "%rsp", "%rax");
asm.mov("q", "%rax", stackStartOffset, "%rsp");
}
// Rescue RSP
asm.add("q", stackStartOffset, "%rsp");
// The top n elements of the stack prepared in the step earlier will now become
// the base of the callee's stack by shrinking the stack by the size of the
// n arguments that did not fit in registers.
// This must only be done if there were any arguments that did not fit in the registers.
if (!stackIndices.isEmpty()) {
asm.add("q", stackStartOffset, "%rsp");
}
}
// We rename a function name if it is "main"
asm.call(e.name.equals("main") ? "main_by_user": e.name);
var mainName = e.name.equals("main") ? "main_by_user": e.name;
asm.call(mainName);
// Remove the arguments that remained on the stack and the stack padding if there was any
var wasStackPadded = (e.arguments.length - stackIndices.size()) % 2 != 0;
var bytesToRemove = 8 * (e.arguments.length - stackIndices.size() + (wasStackPadded ? 1 : 0));
if (bytesToRemove > 0) {
asm.add("q", "$" + bytesToRemove, "%rsp");
}
return null;
}
@@ -840,10 +911,29 @@ public class GenASM implements Visitor<Void> {
@Override
public Void visit(ConstructorCall e) {
// Make sure the stack is aligned before calling malloc
if (e.args.length % 2 != 0) {
// an odd number of arguments means we called pushq an odd number of times
// which results in an unaligned stack. Subtract 8 from the stack pointer to
// re-align the stack
asm.sub("q", "$8", "%rsp");
}
// push arguments onto the stack
for (var arg: e.args) {
for (int i = 0; i < e.args.length; i++) {
// if you want to know why this is done go and read visit(FunctionCall)
if (i % 2 != 0) {
asm.sub("q", "$8", "%rsp");
}
var arg = e.args[i];
arg.welcome(this);
if (i % 2 != 0) {
asm.add("q", "$8", "%rsp");
}
// move float values from xmm0 to rax first
if (arg.type.equals(Type.getFloatType())) {
asm.mov("q", "%xmm0", "%rax");
@@ -857,11 +947,16 @@ public class GenASM implements Visitor<Void> {
asm.mov("l", Helper.getFieldSizeBytes(structDef), "%edi");
asm.call("malloc@PLT");
// push args into struct memory, last arg is ontop of the stack
// push args into struct memory, last arg is on top of the stack
for (int i = e.args.length - 1; i >= 0; i--) {
asm.pop("q", Helper.getFieldOffset(structDef, i) + "(%rax)");
}
// Get rid of the stack alignment if there was any
if (e.args.length % 2 != 0) {
asm.add("q", "$8", "%rsp");
}
return null;
}
@@ -886,7 +981,7 @@ public class GenASM implements Visitor<Void> {
e.expression.welcome(this);
// Move it from xmm0 rax if its a flaot
// Move it from xmm0 rax if it's a float
if (e.expression.type.equals(Type.getFloatType())) {
asm.mov("q", "%xmm0", "%rax");
}
@@ -904,11 +999,49 @@ public class GenASM implements Visitor<Void> {
asm.mov("q", Helper.getFieldOffset(structDef, e.path[i]), "%rax", "%rax");
}
// pop the expression that is ontop of the stack into the field of the struct that has to be updated
// pop the expression that is on top of the stack into the field of the struct that has to be updated
asm.pop("q", Helper.getFieldOffset(structDef, fieldNameToUpdate) + "(%rax)");
asm.mov("q", 0 , "%rax"); // clear rax since an assignment has no result
return null;
}
}
private boolean prepareRegisters(Expression lhs, Expression rhs) {
boolean lhsIsFloat = lhs.type.equals(Type.getFloatType());
boolean rhsIsFloat = rhs.type.equals(Type.getFloatType());
if (lhsIsFloat && rhsIsFloat) {
lhs.welcome(this);
asm.mov("sd", "%xmm0", "%xmm2");
rhs.welcome(this);
asm.mov("sd", "%xmm2", "%xmm0");
asm.mov("sd", "%xmm2", "%xmm0");
return true;
} else if (lhsIsFloat) {
lhs.welcome(this);
rhs.welcome(this);
asm.cvtsi2sd("%rax", "%xmm1");
return true;
} else if (rhsIsFloat) {
lhs.welcome(this);
asm.cvtsi2sd("%rax", "%xmm2");
rhs.welcome(this);
asm.mov("sd", "%xmm0", "%xmm1");
asm.mov("sd", "%xmm2", "%xmm0");
return true;
} else {
lhs.welcome(this);
// A single pushq brings the stack out of 16 byte alignment,
// so we need to increase the stack by another 8 bytes
asm.sub("q", "$8", "%rsp");
asm.push("q", "%rax");
rhs.welcome(this);
asm.mov("q", "%rax", "%rbx");
asm.pop("q", "%rax");
// Which we remove afterwards
asm.add("q", "$8", "%rsp");
return false;
}
}
}

View File

@@ -1,286 +0,0 @@
package de.hsrm.compiler.Klang.visitors;
import java.util.Map;
import java.util.Set;
import de.hsrm.compiler.Klang.nodes.*;
import de.hsrm.compiler.Klang.nodes.expressions.*;
import de.hsrm.compiler.Klang.nodes.loops.DoWhileLoop;
import de.hsrm.compiler.Klang.nodes.loops.ForLoop;
import de.hsrm.compiler.Klang.nodes.loops.WhileLoop;
import de.hsrm.compiler.Klang.nodes.statements.*;
import de.hsrm.compiler.Klang.types.Type;
class GetVars implements Visitor<Void> {
public Set<String> vars;
public Map<String, Type> types;
public GetVars(Set<String> vars, Map<String, Type> types) {
this.vars = vars;
this.types = types;
}
@Override
public Void visit(IntegerExpression e) {
return null;
}
@Override
public Void visit(FloatExpression e) {
return null;
}
@Override
public Void visit(BooleanExpression e) {
return null;
}
@Override
public Void visit(Variable e) {
return null;
}
@Override
public Void visit(EqualityExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(NotEqualityExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(GTExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(GTEExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(LTExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(LTEExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(AdditionExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(SubstractionExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(MultiplicationExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(DivisionExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(ModuloExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(NegateExpression e) {
e.lhs.welcome(this);
return null;
}
@Override
public Void visit(OrExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(AndExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(NotExpression e) {
e.lhs.welcome(this);
return null;
}
@Override
public Void visit(IfStatement e) {
e.cond.welcome(this);
e.then.welcome(this);
if (e.alt != null) {
e.alt.welcome(this);
} else if (e.elif != null) {
e.elif.welcome(this);
}
return null;
}
@Override
public Void visit(WhileLoop e) {
e.cond.welcome(this);
e.block.welcome(this);
return null;
}
@Override
public Void visit(DoWhileLoop e) {
e.cond.welcome(this);
e.block.welcome(this);
return null;
}
@Override
public Void visit(ForLoop e) {
e.init.welcome(this);
e.condition.welcome(this);
e.step.welcome(this);
e.block.welcome(this);
return null;
}
@Override
public Void visit(VariableDeclaration e) {
vars.add(e.name);
types.put(e.name, e.type);
return null;
}
@Override
public Void visit(VariableAssignment e) {
e.expression.welcome(this);
return null;
}
@Override
public Void visit(ReturnStatement e) {
e.expression.welcome(this);
return null;
}
@Override
public Void visit(Block e) {
for (var statement : e.statements) {
statement.welcome(this);
}
return null;
}
@Override
public Void visit(FunctionDefinition e) {
e.block.welcome(this);
return null;
}
@Override
public Void visit(FunctionCall e) {
for (var expression : e.arguments) {
expression.welcome(this);
}
return null;
}
@Override
public Void visit(Program e) {
e.expression.welcome(this);
for (var func : e.funcs) {
func.welcome(this);
}
return null;
}
@Override
public Void visit(Parameter e) {
return null;
}
@Override
public Void visit(EnumDefinition e) {
return null;
}
@Override
public Void visit(EnumValue e) {
return null;
}
@Override
public Void visit(StructDefinition e) {
return null;
}
@Override
public Void visit(StructField e) {
return null;
}
@Override
public Void visit(MemberAccessExpression e) {
return null;
}
@Override
public Void visit(EnumAccessExpression e) {
return null;
}
@Override
public Void visit(ConstructorCall e) {
return null;
}
@Override
public Void visit(NullExpression e) {
return null;
}
@Override
public Void visit(DestructorCall e) {
return null;
}
@Override
public Void visit(FieldAssignment e) {
return null;
}
}

View File

@@ -97,6 +97,14 @@ public class PrettyPrintVisitor implements Visitor<Void> {
return null;
}
@Override
public Void visit(CharExpression e) {
ex.write("'");
ex.write(e.c);
ex.write("'");
return null;
}
@Override
public Void visit(EqualityExpression e) {
ex.write("(");
@@ -310,8 +318,11 @@ public class PrettyPrintVisitor implements Visitor<Void> {
@Override
public Void visit(ReturnStatement e) {
ex.write("return ");
e.expression.welcome(this);
ex.write("return");
if (e.expression != null) {
ex.write(" ");
e.expression.welcome(this);
}
ex.write(";");
return null;
}
@@ -320,7 +331,7 @@ public class PrettyPrintVisitor implements Visitor<Void> {
public Void visit(Block e) {
ex.write("{");
ex.addIndent();
for (Statement stmt : e.statements) {
for (var stmt : e.statementsOrFunctionCalls) {
ex.nl();
stmt.welcome(this);
if (stmt.getClass() == VariableAssignment.class || stmt.getClass() == VariableDeclaration.class) {

View File

@@ -12,6 +12,7 @@ public interface Visitor<R> {
R visit(IntegerExpression e);
R visit(FloatExpression e);
R visit(BooleanExpression e);
R visit(CharExpression e);
R visit(Variable e);
R visit(AdditionExpression e);
R visit(EqualityExpression e);

164
src/test/java/CharTest.java Normal file
View File

@@ -0,0 +1,164 @@
import de.hsrm.compiler.Klang.ContextAnalysis;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CharTest {
@Test
void shouldNotThrowIfComparingCharsEquality() {
// given
var tree = Helper.prepareParser("""
function bar(): int {
let b: char = 'b';
if ('a' == b) {
return 1;
}
return -1;
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldNotThrowIfComparingCharsInequality() {
// given
var tree = Helper.prepareParser("""
function bar(): int {
let b: char = 'b';
if ('a' > b) {
return 1;
}
return -1;
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldNotThrowIfCharLiteralIsAssignedToCharVariable() {
// given
var tree = Helper.prepareParser("""
function bar(): int {
let c: char = 'c';
return 1;
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldNotThrowIfCharLiteralIsReturnedFromFunction() {
// given
var tree = Helper.prepareParser("""
function bar(): char {
return 'c';
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldNotThrowIfCharIsUsedInStruct() {
// given
var tree = Helper.prepareParser("""
struct myChar { c: char; }
function bar(): myChar {
let x: myChar = create myChar('c');
return x;
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldThrowIfCharIsAssignedToOtherTypedVariable() {
// given
var tree = Helper.prepareParser("""
struct foo { f: float; }
function bar(): int {
let c: foo = 'c';
return 1;
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 3:4 Type mismatch: cannot combine foo and char", e.getMessage());
}
@Test
void shouldThrowWhenReturningCharFromOtherTypedFunction() {
// given
var tree = Helper.prepareParser("""
function bar(): float {
return 'c';
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 2:4 Type mismatch: cannot combine float and char", e.getMessage());
}
@Test
void shouldThrowPassingCharToOtherTypedFunctionParameter() {
// given
var tree = Helper.prepareParser("""
function foo(a: float): void {
let x: bool = false;
}
function bar(): void {
foo('c');
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 5:4 argument 0 Expected float but got: char", e.getMessage());
}
@Test
void shouldThrowWhenAssigningCharToOtherTypedStructField() {
// given
var tree = Helper.prepareParser("""
struct foo { a: bool; }
function bar(): void {
let x: foo = create foo(false);
x.a = 'c';
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 4:4 Type mismatch: cannot combine bool and char", e.getMessage());
}
}

View File

@@ -21,7 +21,7 @@ public class ConstructorCallTest {
}
@Test
void numConstructorParameterMissmatch() {
void numConstructorParametermismatch() {
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): bar { return create bar(1, false); } foo();");
var funcs = Helper.getFuncs(tree);
var structs = Helper.getStructs(tree);
@@ -41,6 +41,6 @@ public class ConstructorCallTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:63 argument 0 Type missmatch: cannot combine bool and int", e.getMessage());
assertEquals("Error in line 1:63 argument 0 Type mismatch: cannot combine bool and int", e.getMessage());
}
}

View File

@@ -17,6 +17,6 @@ public class DestroyStatementTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:45 Variable with name \"x\" not defined.", e.getMessage());
assertEquals("Error in line 1:45 Variable or parameter with name \"x\" not defined.", e.getMessage());
}
}

View File

@@ -17,7 +17,7 @@ public class FieldAssignmentTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:46 Variable with name str not defined.", e.getMessage());
assertEquals("Error in line 1:46 Variable or parameter with name \"str\" not defined.", e.getMessage());
}
@Test
@@ -29,6 +29,6 @@ public class FieldAssignmentTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:62 Variable must reference a struct but references int.", e.getMessage());
assertEquals("Error in line 1:62 Variable or parameter must reference a struct but references int.", e.getMessage());
}
}

View File

@@ -33,7 +33,7 @@ public class FunctionCallTest {
}
@Test
void parameterTypeMissmatch() {
void parameterTypeMismatch() {
ParseTree tree = Helper.prepareParser("function foo(x: int): int { return x; } foo(false);");
var funcs = Helper.getFuncs(tree);
var structs = Helper.getStructs(tree);

View File

@@ -92,4 +92,28 @@ public class FunctionDefinitionTest {
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:31 Parameter name Bar duplicates an enum of the same name.", e.getMessage());
}
@Test
void shouldResetVariablesAndParameterEnvironment() {
// given
var tree = Helper.prepareParser("""
function foo(x: int): int {
return 1;
}
function bar(): int {
let x: int = 0;
return x;
}
bar();
""");
var ctxAnal = new ContextAnalysis(
Helper.getFuncs(tree),
Helper.getStructs(tree),
Helper.getEnums(tree)
);
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
}

View File

@@ -0,0 +1,72 @@
import de.hsrm.compiler.Klang.ContextAnalysis;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class NaughtTest {
@Test
void shouldBeComparableToStruct() {
ParseTree tree = Helper.prepareParser("""
struct bar { a: int; }
function foo(): int {
let a: bar = create bar(1);
if (a == naught) {
return -1;
}
return 1;
}
foo();
""");
var ctxAnal = new ContextAnalysis(
Helper.getFuncs(tree),
Helper.getStructs(tree),
Helper.getEnums(tree)
);
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldBeAssignableToStruct() {
ParseTree tree = Helper.prepareParser("""
struct bar { a: int; }
function foo(): int {
let a: bar = naught;
return 1;
}
foo();
""");
var ctxAnal = new ContextAnalysis(
Helper.getFuncs(tree),
Helper.getStructs(tree),
Helper.getEnums(tree)
);
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldBeReturnable() {
ParseTree tree = Helper.prepareParser("""
struct bar { a: int; }
function foo(): bar {
return naught;
}
foo();
""");
var ctxAnal = new ContextAnalysis(
Helper.getFuncs(tree),
Helper.getStructs(tree),
Helper.getEnums(tree)
);
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
}

View File

@@ -16,7 +16,7 @@ public class StructFieldAccessTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:53 Variable with name str not defined.", e.getMessage());
assertEquals("Error in line 1:53 Variable or parameter with name \"str\" not defined.", e.getMessage());
}
@Test
@@ -28,6 +28,6 @@ public class StructFieldAccessTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:69 Variable must reference a struct but references int.", e.getMessage());
assertEquals("Error in line 1:69 Variable or parameter must reference a struct but references int.", e.getMessage());
}
}

View File

@@ -17,6 +17,6 @@ public class VariableAssignmentTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:22 Variable with name \"x\" not defined.", e.getMessage());
assertEquals("Error in line 1:22 Variable or parameter with name \"x\" not defined.", e.getMessage());
}
}

View File

@@ -89,7 +89,7 @@ public class VariableDeclarationTest {
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:34 Redeclaration of variable with name \"x\".", e.getMessage());
assertEquals("Error in line 1:34 Redeclaration of variable or parameter with name \"x\".", e.getMessage());
}
@Test

View File

@@ -17,7 +17,7 @@ public class VariableTest {
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:29 Variable with name \"x\" not defined.", e.getMessage());
assertEquals("Error in line 1:29 Variable or parameter with name \"x\" not defined.", e.getMessage());
}
@Test

101
src/test/java/VoidTest.java Normal file
View File

@@ -0,0 +1,101 @@
import de.hsrm.compiler.Klang.ContextAnalysis;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class VoidTest {
@Test
void shouldNotThrowIfVoidIsUsedAsReturnType() {
// given
var tree = Helper.prepareParser("function foo(): void { let a: int = 0; } foo();");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldNotThrowIfVoidFunctionIsCalled() {
// given
var tree = Helper.prepareParser("""
function foo(): void {
let a: int = 0;
}
function bar(): int {
foo();
return 1;
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
assertDoesNotThrow(() -> ctxAnal.visit(tree));
}
@Test
void shouldThrowIfVoidFunctionIsAssignedToVariable() {
// given
var tree = Helper.prepareParser("""
function foo(): void {
let a: int = 0;
}
function bar(): int {
let a: int = foo();
return 1;
}
bar();
""");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 5:4 Type mismatch: cannot combine int and void", e.getMessage());
}
@Test
void shouldThrowIfValueIsReturnedFromVoidFunction() {
// given
var tree = Helper.prepareParser("function foo(): void { return 1; } foo();");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:23 Cannot return an expression from a void function.", e.getMessage());
}
@Test
void shouldThrowIfVoidIsUsedAsParameterType() {
// given
var tree = Helper.prepareParser("function foo(a: void): int { return 1; } foo();");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:13 Type void cannot be used to declare a parameter.", e.getMessage());
}
@Test
void shouldThrowIfVoidIsUsedAsVariableType() {
// given
var tree = Helper.prepareParser("function foo(): int { let a: void; return 1; } foo();");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:22 Type void can not be used to declare variables.", e.getMessage());
}
@Test
void shouldThrowIfVoidIsUsedAsStructFieldType() {
// given
var tree = Helper.prepareParser("struct bar { a: void; } function foo(): int { return 1; } foo();");
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
// when / then
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
assertEquals("Error in line 1:13 Type void can not be used as a struct field type.", e.getMessage());
}
}

View File

@@ -94,6 +94,11 @@ int testStructCreation() {
struct_testExpected_l("init inner field a", 20, resultRec->b->a);
struct_testExpected_s("init inner field b", NULL, resultRec->b->b);
// The result of this test is always 1 because if the tests fail
// a segmentation fault occurs.
long alignmentResult = isStackAlignedBeforeFunctionCall();
struct_testExpected_l("stack alignment before malloc", 1, alignmentResult);
free(resultRec);
free(innerStruct);
}

View File

@@ -28,4 +28,6 @@ long getStructFieldRecA(struct testStructRec *t);
struct testStructRec *getStructFieldRecB(struct testStructRec *t);
struct testStructRec *setStructFieldRecA(struct testStructRec *t, long a);
struct testStructRec *setStructFieldRecB(struct testStructRec *t, struct testStructRec *b);
struct testStructRec *setStructFieldRecB(struct testStructRec *t, struct testStructRec *b);
long isStackAlignedBeforeFunctionCall();

View File

@@ -555,6 +555,72 @@ function getTestStructRec(a: int, b: testStructRec): testStructRec {
return create testStructRec(a, b);
}
struct evenFieldsStruct {
a: int;
b: int;
}
struct oddFieldsStruct {
a: int;
b: int;
c: int;
}
function stackAlignmentTestInternal1(): oddFieldsStruct {
// an odd amount of constructor parameters could lead to an unaligned stack
// which results in a segmentation fault when calling malloc
return create oddFieldsStruct(1, 1, 1);
}
function stackAlignmentTestInternal2(): evenFieldsStruct {
// an odd amount of local variables could lead to an unaligned stack
// which results in a segmentation fault when calling malloc
let a: evenFieldsStruct = create evenFieldsStruct(1, 1);
return a;
}
function stackAlignmentTestInternal3(a: int): evenFieldsStruct {
// an odd amount of function parameters could lead to an unaligned stack
// which results in a segmentation fault when calling malloc
return create evenFieldsStruct(1, 1);
}
function stackAlignmentTestInternal4(a: int): evenFieldsStruct {
// if (function parameters + local variables + constructor parameters) is odd
// then this could lead to an unaligned stack
// which results in a segmentation faul when calling malloc
let b: int = 0;
let c: int = 0;
return create evenFieldsStruct(1, 1);
}
function stackAlignmentTestInternal5(a: int, b: int): evenFieldsStruct {
// if (function parameters + local variables + constructor parameters) is odd
// then this could lead to an unaligned stack
// which results in a segmentation faul when calling malloc
let c: int = 0;
return create evenFieldsStruct(1, 1);
}
function stackAlignmentTestInternal6(a: int, b: int): oddFieldsStruct {
// if (function parameters + local variables + constructor parameters) is odd
// then this could lead to an unaligned stack
// which results in a segmentation faul when calling malloc
let c: int = 0;
let d: int = 0;
return create oddFieldsStruct(1, 1, 1);
}
function isStackAlignedBeforeFunctionCall(): int {
let a: oddFieldsStruct = stackAlignmentTestInternal1();
let b: evenFieldsStruct = stackAlignmentTestInternal2();
let c: evenFieldsStruct = stackAlignmentTestInternal3(1);
let d: evenFieldsStruct = stackAlignmentTestInternal4(1);
let e: evenFieldsStruct = stackAlignmentTestInternal5(1, 1);
let f: oddFieldsStruct = stackAlignmentTestInternal6(1, 1);
return 1;
}
function getStructFieldA(t: testStruct): int {
return t.a;
}