Kite Programming Language

The Kite Compiler: part 2 of many

Written by Mooneer Salem on Friday 14th of May, 2010 in General

This week, I worked some more on learning how LLVM works. Ive created basic method and constant value classes to represent those values in the AST, and wrote a test app to ensure this works. Im happy to tell you all that I have some initial success doing this. :)

Sample application:


using namespace kite::parse_tree;

int main (int argc, char * const argv[])
{
    // TODO: kite compiler driver.
    InitializeNativeTarget();
    llvm_start_multithreaded();
    
    ConstantValue<double> *constant = new ConstantValue<double>(42.0);
    MethodValue *method = new MethodValue("life_universe_everything");
    method->push_instruction(constant);
    CompilerState *state = new CompilerState();
    
    LLVMContext &context = getGlobalContext();
    state->push_module(new Module("test_module", context));
    Value *v = method->codegen(state);
    
    Module *mod = state->pop_module();
    
    ExecutionEngine *engine = EngineBuilder(mod).create();
    void *fptr = engine->getPointerToFunction(static_cast<Function*>(v));
    double (*FP)() = (double (*)())(intptr_t)fptr;
    std::cout << "Evaluates to: " << (*FP)() << std::endl;
    mod->dump();
    
    return 0;
}

Results:


[Session started at 2010-05-14 22:18:51 -0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1461.2) (Fri Mar  5 04:43:10 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
sharedlibrary apply-load-rules all
Program loaded.
run
[Switching to process 19016]
Running…
Evaluates to: 42
; ModuleID = 'test_module'

define double @life_universe_everything() {
entry:
  ret double 4.200000e+01
}

Debugger stopped.
Program exited with status value:0.

Add comment