Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a5a47c
commit 5072e4a
Showing
6 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <llvm-c/Core.h> | ||
|
||
#include "codegen.h" | ||
|
||
|
||
//============================================================================== | ||
// | ||
// Functions | ||
// | ||
//============================================================================== | ||
|
||
//-------------------------------------- | ||
// Number | ||
//-------------------------------------- | ||
|
||
// Generates an LLVM value object for a Number AST. | ||
// | ||
// node - The node to generate code for. | ||
// | ||
// Returns an LLVM value reference. | ||
LLVMValueRef kal_codegen_number(kal_ast_node *node) | ||
{ | ||
return LLVMConstReal(LLVMDoubleType(), node->number.value); | ||
} | ||
|
||
|
||
//-------------------------------------- | ||
// Code Generation | ||
//-------------------------------------- | ||
|
||
// Recursively generates LLVM objects to build the code. | ||
// | ||
// node - The node to generate code for. | ||
// module - The module that the code is being generated for. | ||
// builder - The LLVM builder that is creating the IR. | ||
// | ||
// Returns an LLVM value reference. | ||
LLVMValueRef kal_codegen(kal_ast_node *node, LLVMModuleRef module, | ||
LLVMBuilderRef builder) | ||
{ | ||
// Recursively free dependent data. | ||
switch(node->type) { | ||
case KAL_AST_TYPE_NUMBER: { | ||
return kal_codegen_number(node); | ||
break; | ||
} | ||
case KAL_AST_TYPE_VARIABLE: { | ||
break; | ||
} | ||
case KAL_AST_TYPE_BINARY_EXPR: { | ||
break; | ||
} | ||
case KAL_AST_TYPE_CALL: { | ||
break; | ||
} | ||
case KAL_AST_TYPE_PROTOTYPE: { | ||
break; | ||
} | ||
case KAL_AST_TYPE_FUNCTION: { | ||
break; | ||
} | ||
} | ||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef _codegen_h | ||
#define _codegen_h | ||
|
||
#include <llvm-c/Core.h> | ||
#include "ast.h" | ||
|
||
//============================================================================== | ||
// | ||
// Functions | ||
// | ||
//============================================================================== | ||
|
||
LLVMValueRef kal_codegen(kal_ast_node *node, LLVMModuleRef module, | ||
LLVMBuilderRef builder); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <ast.h> | ||
#include <codegen.h> | ||
#include <llvm-c/Core.h> | ||
#include "minunit.h" | ||
|
||
|
||
//============================================================================== | ||
// | ||
// Test Cases | ||
// | ||
//============================================================================== | ||
|
||
//-------------------------------------- | ||
// Number | ||
//-------------------------------------- | ||
|
||
int test_kal_codegen_number() { | ||
kal_ast_node *node = kal_ast_number_create(10); | ||
LLVMValueRef value = kal_codegen(node, NULL, NULL); | ||
LLVMTypeRef type = LLVMTypeOf(value); | ||
mu_assert(LLVMGetTypeKind(type) == LLVMDoubleTypeKind, ""); | ||
mu_assert(LLVMIsConstant(value), ""); | ||
kal_ast_node_free(node); | ||
return 0; | ||
} | ||
|
||
|
||
//============================================================================== | ||
// | ||
// Setup | ||
// | ||
//============================================================================== | ||
|
||
int all_tests() { | ||
mu_run_test(test_kal_codegen_number); | ||
return 0; | ||
} | ||
|
||
RUN_TESTS() |