forked from cosmonaut/wraith-lang
starting a heap alloc system
parent
8e7cc00789
commit
ca1835f98d
11
ast.c
11
ast.c
|
@ -22,6 +22,7 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
|
||||||
switch(syntaxKind)
|
switch(syntaxKind)
|
||||||
{
|
{
|
||||||
case AccessExpression: return "AccessExpression";
|
case AccessExpression: return "AccessExpression";
|
||||||
|
case AllocExpression: return "Alloc";
|
||||||
case Assignment: return "Assignment";
|
case Assignment: return "Assignment";
|
||||||
case BinaryExpression: return "BinaryExpression";
|
case BinaryExpression: return "BinaryExpression";
|
||||||
case Comment: return "Comment";
|
case Comment: return "Comment";
|
||||||
|
@ -365,6 +366,16 @@ Node* MakeAccessExpressionNode(
|
||||||
return node;
|
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)
|
static const char* PrimitiveTypeToString(PrimitiveType type)
|
||||||
{
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
|
|
4
ast.h
4
ast.h
|
@ -6,6 +6,7 @@
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
AccessExpression,
|
AccessExpression,
|
||||||
|
AllocExpression,
|
||||||
Assignment,
|
Assignment,
|
||||||
BinaryExpression,
|
BinaryExpression,
|
||||||
Comment,
|
Comment,
|
||||||
|
@ -170,6 +171,9 @@ Node* MakeAccessExpressionNode(
|
||||||
Node *accessee,
|
Node *accessee,
|
||||||
Node *accessor
|
Node *accessor
|
||||||
);
|
);
|
||||||
|
Node* MakeAllocNode(
|
||||||
|
Node *typeNode
|
||||||
|
);
|
||||||
|
|
||||||
void PrintTree(Node *node, uint32_t tabCount);
|
void PrintTree(Node *node, uint32_t tabCount);
|
||||||
|
|
||||||
|
|
21
compiler.c
21
compiler.c
|
@ -544,6 +544,14 @@ static LLVMValueRef CompileAccessExpression(
|
||||||
return LLVMBuildLoad(builder, access, accessor->value.string);
|
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(
|
static LLVMValueRef CompileExpression(
|
||||||
LLVMBuilderRef builder,
|
LLVMBuilderRef builder,
|
||||||
Node *expression
|
Node *expression
|
||||||
|
@ -553,6 +561,9 @@ static LLVMValueRef CompileExpression(
|
||||||
case AccessExpression:
|
case AccessExpression:
|
||||||
return CompileAccessExpression(builder, expression);
|
return CompileAccessExpression(builder, expression);
|
||||||
|
|
||||||
|
case AllocExpression:
|
||||||
|
return CompileAllocExpression(builder, expression);
|
||||||
|
|
||||||
case BinaryExpression:
|
case BinaryExpression:
|
||||||
return CompileBinaryExpression(builder, expression);
|
return CompileBinaryExpression(builder, expression);
|
||||||
|
|
||||||
|
@ -877,13 +888,13 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
LLVMPassManagerRef passManager = LLVMCreatePassManager();
|
LLVMPassManagerRef passManager = LLVMCreatePassManager();
|
||||||
|
|
||||||
LLVMAddInstructionCombiningPass(passManager);
|
//LLVMAddInstructionCombiningPass(passManager);
|
||||||
LLVMAddCFGSimplificationPass(passManager);
|
//LLVMAddCFGSimplificationPass(passManager);
|
||||||
LLVMAddReassociatePass(passManager);
|
//LLVMAddReassociatePass(passManager);
|
||||||
LLVMAddPromoteMemoryToRegisterPass(passManager);
|
//LLVMAddPromoteMemoryToRegisterPass(passManager);
|
||||||
|
|
||||||
LLVMPassManagerBuilderRef passManagerBuilder = LLVMPassManagerBuilderCreate();
|
LLVMPassManagerBuilderRef passManagerBuilder = LLVMPassManagerBuilderCreate();
|
||||||
LLVMPassManagerBuilderSetOptLevel(passManagerBuilder, 3);
|
LLVMPassManagerBuilderSetOptLevel(passManagerBuilder, 0);
|
||||||
LLVMPassManagerBuilderPopulateModulePassManager(passManagerBuilder, passManager);
|
LLVMPassManagerBuilderPopulateModulePassManager(passManagerBuilder, passManager);
|
||||||
|
|
||||||
LLVMRunPassManager(passManager, module);
|
LLVMRunPassManager(passManager, module);
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
"return" return RETURN;
|
"return" return RETURN;
|
||||||
"static" return STATIC;
|
"static" return STATIC;
|
||||||
"Reference" return REFERENCE;
|
"Reference" return REFERENCE;
|
||||||
|
"alloc" return ALLOC;
|
||||||
[0-9]+ return NUMBER;
|
[0-9]+ return NUMBER;
|
||||||
[a-zA-Z][a-zA-Z0-9]* return ID;
|
[a-zA-Z][a-zA-Z0-9]* return ID;
|
||||||
\"[a-zA-Z][a-zA-Z0-9]*\" return STRING_LITERAL;
|
\"[a-zA-Z][a-zA-Z0-9]*\" return STRING_LITERAL;
|
||||||
|
|
7
wraith.y
7
wraith.y
|
@ -32,6 +32,7 @@ extern Node *rootNode;
|
||||||
%token RETURN
|
%token RETURN
|
||||||
%token STATIC
|
%token STATIC
|
||||||
%token REFERENCE
|
%token REFERENCE
|
||||||
|
%token ALLOC
|
||||||
%token NUMBER
|
%token NUMBER
|
||||||
%token ID
|
%token ID
|
||||||
%token STRING_LITERAL
|
%token STRING_LITERAL
|
||||||
|
@ -132,6 +133,11 @@ Identifier : ID
|
||||||
$$ = MakeIdentifierNode(yytext);
|
$$ = MakeIdentifierNode(yytext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HeapAllocation : ALLOC Type
|
||||||
|
{
|
||||||
|
$$ = MakeAllocNode($2);
|
||||||
|
}
|
||||||
|
|
||||||
AccessExpression : Identifier POINT AccessExpression
|
AccessExpression : Identifier POINT AccessExpression
|
||||||
{
|
{
|
||||||
$$ = MakeAccessExpressionNode($1, $3);
|
$$ = MakeAccessExpressionNode($1, $3);
|
||||||
|
@ -178,6 +184,7 @@ BinaryExpression : Expression PLUS Expression
|
||||||
Expression : PrimaryExpression
|
Expression : PrimaryExpression
|
||||||
| UnaryExpression
|
| UnaryExpression
|
||||||
| BinaryExpression
|
| BinaryExpression
|
||||||
|
| HeapAllocation
|
||||||
;
|
;
|
||||||
|
|
||||||
VariableDeclaration : Identifier COLON Type
|
VariableDeclaration : Identifier COLON Type
|
||||||
|
|
Loading…
Reference in New Issue