Skip to content

Commit

Permalink
Add LLVM support for Number.
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed May 18, 2012
1 parent 6a5a47c commit 5072e4a
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 3 deletions.
21 changes: 19 additions & 2 deletions Makefile
Expand Up @@ -3,6 +3,7 @@
################################################################################

CFLAGS=-g -Wall -Wextra -Wno-self-assign -std=c99
CXXFLAGS=-g -Wall -Wextra -Wno-self-assign

LEX_SOURCES=$(wildcard src/*.l)
LEX_OBJECTS=$(patsubst %.l,%.c,${LEX_SOURCES}) $(patsubst %.l,%.h,${LEX_SOURCES})
Expand All @@ -15,12 +16,14 @@ OBJECTS=$(patsubst %.c,%.o,${SOURCES}) $(patsubst %.l,%.o,${LEX_SOURCES}) $(pats
LIB_SOURCES=$(filter-out kaleidoscope.c,${SOURCES})
LIB_OBJECTS=$(filter-out kaleidoscope.o,${OBJECTS})
TEST_SOURCES=$(wildcard tests/*_tests.c)
TEST_OBJECTS=$(patsubst %.c,%,${TEST_SOURCES})
TEST_OBJECTS=$(filter-out tests/codegen_tests,$(patsubst %.c,%,${TEST_SOURCES}))

LEX=flex
YACC=bison
YFLAGS=-dv

LLVM_CC_FLAGS=`llvm-config --cflags`
LLVM_LINK_FLAGS=`llvm-config --libs --cflags --ldflags core analysis executionengine jit interpreter native`

################################################################################
# Default Target
Expand Down Expand Up @@ -58,12 +61,20 @@ src/parser.c: src/parser.y
${YACC} ${YFLAGS} --report-file=build/bison/report.txt -o $@ $^


################################################################################
# LLVM
################################################################################

src/codegen.o: src/codegen.c
${CC} ${LLVM_CC_FLAGS} ${CFLAGS} -c -o $@ $^


################################################################################
# Tests
################################################################################

.PHONY: test
test: $(TEST_OBJECTS)
test: $(TEST_OBJECTS) build/tests/codegen_tests
@sh ./tests/runtests.sh

build/tests:
Expand All @@ -72,6 +83,12 @@ build/tests:
$(TEST_OBJECTS): %: %.c build/tests build/libkaleidoscope.a
$(CC) $(CFLAGS) -Isrc -o build/$@ $< build/libkaleidoscope.a

build/tests/codegen_tests.o: tests/codegen_tests.c build/libkaleidoscope.a
$(CC) $(LLVM_CC_FLAGS) $(CFLAGS) -Isrc -c -o $@ tests/codegen_tests.c build/libkaleidoscope.a

build/tests/codegen_tests: build/tests/codegen_tests.o build/libkaleidoscope.a
$(CXX) $(LLVM_LINK_FLAGS) $(CXXFLAGS) -Isrc -o $@ build/tests/codegen_tests.o build/libkaleidoscope.a


################################################################################
# Clean up
Expand Down
2 changes: 2 additions & 0 deletions src/ast.c
@@ -1,7 +1,9 @@
#include <stdlib.h>
#include <string.h>

#include "ast.h"


//==============================================================================
//
// Functions
Expand Down
7 changes: 6 additions & 1 deletion src/ast.h
@@ -1,3 +1,6 @@
#ifndef _ast_h
#define _ast_h

//==============================================================================
//
// Definitions
Expand Down Expand Up @@ -99,4 +102,6 @@ kal_ast_node *kal_ast_prototype_create(char *name, char **args,
kal_ast_node *kal_ast_function_create(kal_ast_node *prototype,
kal_ast_node *body);

void kal_ast_node_free(kal_ast_node *node);
void kal_ast_node_free(kal_ast_node *node);

#endif
67 changes: 67 additions & 0 deletions src/codegen.c
@@ -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;
}
16 changes: 16 additions & 0 deletions src/codegen.h
@@ -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
41 changes: 41 additions & 0 deletions tests/codegen_tests.c
@@ -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()

0 comments on commit 5072e4a

Please sign in to comment.