commit baa278de8a2083423688cee8186f163ba72fabbf
parent 6ada7c27a872d8559ace49e545f7ac17f1c11de5
Author: kocotian <kocotian@kocotian.pl>
Date: Sun, 1 Aug 2021 23:10:31 +0000
Conditional statements
Diffstat:
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/target/x86_64-linux.c b/target/x86_64-linux.c
@@ -151,6 +151,7 @@ newLiteralIdentifierTree(void)
static Array(struct LiteralIdentifierTree) globalLiteralIdentifierTree;
static Array(ASTExpressionLiteral *) literalStrings;
+static int ifcnt;
static struct LiteralIdentifier *
identifierCheck(ASTExpressionLiteral expr)
@@ -386,7 +387,23 @@ compileStatementCompound(Compiler *compiler, ASTStatementCompound stat)
static void
compileStatementConditional(Compiler *compiler, ASTStatementConditional stat)
{
- /* TODO! */
+ int iifcnt = ifcnt;
+ compileExpression(compiler, stat.condition, 0);
+ asmTextAppend(compiler, "\tcmp r15, 0");
+ if (stat.elsebody == NULL) {
+ ++ifcnt;
+ asmTextAppend(compiler, "\tje .IF.%d", iifcnt);
+ compileStatement(compiler, stat.body);
+ asmTextAppend(compiler, ".IF.%d:", iifcnt);
+ } else {
+ ifcnt += 2;
+ asmTextAppend(compiler, "\tje .IF.%d", iifcnt);
+ compileStatement(compiler, stat.body);
+ asmTextAppend(compiler, "\tjmp .IF.%d", iifcnt + 1);
+ asmTextAppend(compiler, ".IF.%d:", iifcnt);
+ compileStatement(compiler, stat.elsebody);
+ asmTextAppend(compiler, ".IF.%d:", iifcnt + 1);
+ }
}
static void
@@ -459,6 +476,7 @@ compileGlobalFunction(Compiler *compiler, ASTGlobalFunction func)
pushVector(lastArray(globalLiteralIdentifierTree).identifiers,
newLiteralIdentifierFunction(func.name, func));
+ /* TODO: parameters */
asmTextAppend(compiler, "%s:", func.name.value);
asmTextAppend(compiler, "\tpush rbp");
asmTextAppend(compiler, "\tmov rbp, rsp");
@@ -479,6 +497,7 @@ compileModule(ASTModule module)
/* initialization of global containers */
newVector(globalLiteralIdentifierTree);
newVector(literalStrings);
+ ifcnt = 0;
pushVector(globalLiteralIdentifierTree, newLiteralIdentifierTree());