starting a heap alloc system

generics
cosmonaut 2021-04-28 12:49:45 -07:00
parent 8e7cc00789
commit ca1835f98d
5 changed files with 39 additions and 5 deletions

11
ast.c
View File

@ -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)

4
ast.h
View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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