From ca1835f98d373785bbe7dea3ab90dd612ea3960c Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 28 Apr 2021 12:49:45 -0700 Subject: [PATCH] starting a heap alloc system --- ast.c | 11 +++++++++++ ast.h | 4 ++++ compiler.c | 21 ++++++++++++++++----- wraith.lex | 1 + wraith.y | 7 +++++++ 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/ast.c b/ast.c index c239901..ae2d254 100644 --- a/ast.c +++ b/ast.c @@ -22,6 +22,7 @@ const char* SyntaxKindString(SyntaxKind syntaxKind) switch(syntaxKind) { case AccessExpression: return "AccessExpression"; + case AllocExpression: return "Alloc"; case Assignment: return "Assignment"; case BinaryExpression: return "BinaryExpression"; case Comment: return "Comment"; @@ -365,6 +366,16 @@ Node* MakeAccessExpressionNode( return node; } +Node* MakeAllocNode(Node *typeNode) +{ + Node* node = (Node*) malloc(sizeof(Node)); + node->syntaxKind = AllocExpression; + node->childCount = 1; + node->children = (Node**) malloc(sizeof(Node*)); + node->children[0] = typeNode; + return node; +} + static const char* PrimitiveTypeToString(PrimitiveType type) { switch (type) diff --git a/ast.h b/ast.h index 6f46530..51fe040 100644 --- a/ast.h +++ b/ast.h @@ -6,6 +6,7 @@ typedef enum { AccessExpression, + AllocExpression, Assignment, BinaryExpression, Comment, @@ -170,6 +171,9 @@ Node* MakeAccessExpressionNode( Node *accessee, Node *accessor ); +Node* MakeAllocNode( + Node *typeNode +); void PrintTree(Node *node, uint32_t tabCount); diff --git a/compiler.c b/compiler.c index 4a61699..296f894 100644 --- a/compiler.c +++ b/compiler.c @@ -544,6 +544,14 @@ static LLVMValueRef CompileAccessExpression( return LLVMBuildLoad(builder, access, accessor->value.string); } +static LLVMValueRef CompileAllocExpression( + LLVMBuilderRef builder, + Node *expression +) { + LLVMTypeRef type = ResolveType(expression->children[0]); + return LLVMBuildMalloc(builder, type, "allocation"); +} + static LLVMValueRef CompileExpression( LLVMBuilderRef builder, Node *expression @@ -553,6 +561,9 @@ static LLVMValueRef CompileExpression( case AccessExpression: return CompileAccessExpression(builder, expression); + case AllocExpression: + return CompileAllocExpression(builder, expression); + case BinaryExpression: return CompileBinaryExpression(builder, expression); @@ -877,13 +888,13 @@ int main(int argc, char *argv[]) LLVMPassManagerRef passManager = LLVMCreatePassManager(); - LLVMAddInstructionCombiningPass(passManager); - LLVMAddCFGSimplificationPass(passManager); - LLVMAddReassociatePass(passManager); - LLVMAddPromoteMemoryToRegisterPass(passManager); + //LLVMAddInstructionCombiningPass(passManager); + //LLVMAddCFGSimplificationPass(passManager); + //LLVMAddReassociatePass(passManager); + //LLVMAddPromoteMemoryToRegisterPass(passManager); LLVMPassManagerBuilderRef passManagerBuilder = LLVMPassManagerBuilderCreate(); - LLVMPassManagerBuilderSetOptLevel(passManagerBuilder, 3); + LLVMPassManagerBuilderSetOptLevel(passManagerBuilder, 0); LLVMPassManagerBuilderPopulateModulePassManager(passManagerBuilder, passManager); LLVMRunPassManager(passManager, module); diff --git a/wraith.lex b/wraith.lex index 5a446e2..e571f31 100644 --- a/wraith.lex +++ b/wraith.lex @@ -16,6 +16,7 @@ "return" return RETURN; "static" return STATIC; "Reference" return REFERENCE; +"alloc" return ALLOC; [0-9]+ return NUMBER; [a-zA-Z][a-zA-Z0-9]* return ID; \"[a-zA-Z][a-zA-Z0-9]*\" return STRING_LITERAL; diff --git a/wraith.y b/wraith.y index 93315f2..94f754e 100644 --- a/wraith.y +++ b/wraith.y @@ -32,6 +32,7 @@ extern Node *rootNode; %token RETURN %token STATIC %token REFERENCE +%token ALLOC %token NUMBER %token ID %token STRING_LITERAL @@ -132,6 +133,11 @@ Identifier : ID $$ = MakeIdentifierNode(yytext); } +HeapAllocation : ALLOC Type + { + $$ = MakeAllocNode($2); + } + AccessExpression : Identifier POINT AccessExpression { $$ = MakeAccessExpressionNode($1, $3); @@ -178,6 +184,7 @@ BinaryExpression : Expression PLUS Expression Expression : PrimaryExpression | UnaryExpression | BinaryExpression + | HeapAllocation ; VariableDeclaration : Identifier COLON Type