summaryrefslogtreecommitdiff
path: root/MLKLLVMCompiler.mm
diff options
context:
space:
mode:
Diffstat (limited to 'MLKLLVMCompiler.mm')
-rw-r--r--MLKLLVMCompiler.mm90
1 files changed, 74 insertions, 16 deletions
diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm
index 745be44..2aaa0e8 100644
--- a/MLKLLVMCompiler.mm
+++ b/MLKLLVMCompiler.mm
@@ -33,6 +33,7 @@
#include <llvm/DerivedTypes.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Instructions.h>
+//#include <llvm/Interpreter.h>
#include <llvm/Module.h>
#include <llvm/ModuleProvider.h>
#include <llvm/PassManager.h>
@@ -40,6 +41,7 @@
#include <llvm/Target/TargetData.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/IPO.h>
+#include <llvm/Transforms/Utils/Cloning.h> // InlineFunction
#include <llvm/Transforms/Utils/UnifyFunctionExitNodes.h>
#include <llvm/Value.h>
@@ -80,6 +82,20 @@ static Constant
@implementation MLKLLVMCompiler
++(void) load
+{
+ if (!MLKDefaultCompiler)
+ {
+ MLKDefaultCompiler = self;
+ MLKLoadCompilesP = YES;
+ }
+
+ // GNU ld optimises the MLKLLVMCompilation category on
+ // MLKLexicalContext away unless we do this. Man, the crappiness of
+ // this Unix stuff is amazing...
+ MLKDummyUseLLVMLexicalContext = nil;
+}
+
+(void) initialize
{
module = new llvm::Module ("MLKLLVMModule");
@@ -89,6 +105,7 @@ static Constant
fpm = new FunctionPassManager (module_provider);
fpm->add (new TargetData (*execution_engine->getTargetData()));
//fpm->add (new TargetData (module));
+ fpm->add (createScalarReplAggregatesPass());
fpm->add (createInstructionCombiningPass());
fpm->add (createReassociatePass());
fpm->add (createGVNPass());
@@ -138,16 +155,22 @@ static Constant
//function->dump();
- // JIT-compile.
- fn = (id (*)()) execution_engine->getPointerToFunction (function);
//module->dump();
//NSLog (@"%p", fn);
[pool release];
//NSLog (@"Code compiled.");
+#if 1
+ // JIT-compile.
+ fn = (id (*)()) execution_engine->getPointerToFunction (function);
// Execute.
lambdaForm = fn();
+ execution_engine->freeMachineCodeForFunction (function);
+#else
+ Interpreter *i = Interpreter::create (module_provider);
+ lambdaForm = i->runFunction (function)->PointerVal;
+#endif
//NSLog (@"Closure built.");
@@ -316,15 +339,43 @@ static Constant
Type::Int32Ty,
PointerTy,
NULL);
-
+
builder.CreateCall (function, createGlobalStringPtr ([message UTF8String]));
}
+
++(void) insertPointerTrace:(Value *)pointerValue
+{
+ Constant *function =
+ module->getOrInsertFunction ("printf",
+ Type::Int32Ty,
+ PointerTy,
+ PointerTy,
+ NULL);
+
+ builder.CreateCall2 (function,
+ createGlobalStringPtr ("%p\n"),
+ builder.CreateBitCast (pointerValue, PointerTy));
+}
@end
@implementation MLKForm (MLKLLVMCompilation)
-(Value *) processForLLVM
{
+ //[_compiler insertTrace:
+ // [NSString stringWithFormat:
+ // @"Executing: %@", MLKPrintToString(_form)]];
+
+ Value *result = [self reallyProcessForLLVM];
+
+ //[_compiler insertTrace:
+ // [NSString stringWithFormat:
+ // @"Done: %@", MLKPrintToString(_form)]];
+ return result;
+}
+
+-(Value *) reallyProcessForLLVM
+{
NSLog (@"WARNING: Unrecognised form type: %@", self);
return NULL;
}
@@ -332,7 +383,7 @@ static Constant
@implementation MLKProgNForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
NSEnumerator *e = [_bodyForms objectEnumerator];
MLKForm *form;
@@ -349,7 +400,7 @@ static Constant
@implementation MLKSimpleLoopForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
NSEnumerator *e = [_bodyForms objectEnumerator];
MLKForm *form;
@@ -379,7 +430,7 @@ static Constant
@implementation MLKSymbolForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
Value *value;
@@ -422,7 +473,7 @@ static Constant
@implementation MLKFunctionCallForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
static MLKPackage *sys = [MLKPackage findPackage:@"TOILET-SYSTEM"];
@@ -496,13 +547,21 @@ static Constant
args.end(),
[MLKPrintToString(_head) UTF8String]);
+ // XXX
+ if (NO && [_context functionInline:_head])
+ {
+ InlineFunction (call);
+ }
+
+ //[_compiler insertTrace:[NSString stringWithFormat:@"%@ done.", MLKPrintToString(_head)]];
+
return call;
}
@end
@implementation MLKSimpleLambdaForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
std::vector <const Type *> argtypes (1, PointerTy);
FunctionType *ftype = FunctionType::get (PointerTy, argtypes, true);
@@ -625,6 +684,7 @@ static Constant
execution_engine->getPointerToFunction (function);
//NSLog (@"Done.");
//function->dump();
+ //function->viewCFG();
//NSLog (@"Function built.");
builder.SetInsertPoint (outerBlock);
@@ -644,15 +704,13 @@ static Constant
onObject:mlkcompiledclosure
withArgumentVector:&argv];
- //function->viewCFG();
-
return closure;
}
@end
@implementation MLKLetForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
NSEnumerator *e = [_variableBindingForms objectEnumerator];
Value *value = ConstantPointerNull::get (PointerTy);
@@ -684,7 +742,7 @@ static Constant
@implementation MLKQuoteForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
// FIXME: When to release _quotedData? At the same time the code is
// released, probably...
@@ -698,7 +756,7 @@ static Constant
@implementation MLKSelfEvaluatingForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
// FIXME: When to release _form? At the same time the code is
// released, probably...
@@ -712,7 +770,7 @@ static Constant
@implementation MLKIfForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
Function *function = builder.GetInsertBlock()->getParent();
BasicBlock *thenBlock = BasicBlock::Create ("if_then", function);
@@ -742,7 +800,7 @@ static Constant
@implementation MLKSetQForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
NSEnumerator *var_e, *value_e;
MLKForm *valueForm;
@@ -796,7 +854,7 @@ static Constant
@implementation MLKInPackageForm (MLKLLVMCompilation)
--(Value *) processForLLVM
+-(Value *) reallyProcessForLLVM
{
id package = [MLKPackage findPackage:stringify(_packageDesignator)];