From ad05662ae05336aa5db20f9d34d5c1d5bba9d6c2 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sat, 23 Aug 2008 16:34:08 +0200 Subject: Make compilers pluggable at runtime. --- MLKInterpreter.m | 58 +++++++++++++++++++++++++++++------------------------- MLKLLVMCompiler.h | 2 ++ MLKLLVMCompiler.mm | 9 +++++++++ MLKRoot.m | 11 +++++++---- globals.h | 6 ++++++ globals.m | 3 +++ 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/MLKInterpreter.m b/MLKInterpreter.m index 0cdf904..7fd59ad 100644 --- a/MLKInterpreter.m +++ b/MLKInterpreter.m @@ -35,6 +35,7 @@ #import "MLKRoot.h" #import "MLKSymbol.h" #import "NSObject-MLKPrinting.h" +#import "globals.h" #import "runtime-compatibility.h" #import "special-symbols.h" #import "util.h" @@ -1230,34 +1231,37 @@ fprintf (stderr, "| "); fprintf (stderr, "LOAD: %s\n", [formdesc UTF8String]); -#ifdef USE_LLVM - expansion = code; - result = [MLKLLVMCompiler eval:code]; -#else // !USE_LLVM - expansion = denullify([[MLKInterpreter - eval:code - inLexicalContext:[MLKLexicalContext - globalContext] - withEnvironment:[MLKLexicalEnvironment - globalEnvironment] - mode:not_compile_time_mode] - objectAtIndex:0]); - - if ([code isKindOfClass:[MLKCons class]] && [code cdr]) - formdesc = [NSString stringWithFormat:@"(%@ %@ ...)", - MLKPrintToString([expansion car]), - MLKPrintToString([[expansion cdr] car])]; + if (MLKLoadCompilesP) + { + expansion = code; + result = [MLKDefaultCompiler eval:code]; + } else - formdesc = MLKPrintToString(expansion); - - //fprintf (stderr, "; LOAD: %s\n", [formdesc UTF8String]); - result = [MLKInterpreter - eval:expansion - inLexicalContext:[MLKLexicalContext globalContext] - withEnvironment:[MLKLexicalEnvironment globalEnvironment] - expandOnly:NO]; - //NSLog (@"; LOAD: Top-level form evaluated."); -#endif //!USE_LLVM + { + expansion = denullify([[MLKInterpreter + eval:code + inLexicalContext:[MLKLexicalContext + globalContext] + withEnvironment:[MLKLexicalEnvironment + globalEnvironment] + mode:not_compile_time_mode] + objectAtIndex:0]); + + if ([code isKindOfClass:[MLKCons class]] && [code cdr]) + formdesc = [NSString stringWithFormat:@"(%@ %@ ...)", + MLKPrintToString([expansion car]), + MLKPrintToString([[expansion cdr] car])]; + else + formdesc = MLKPrintToString(expansion); + + //fprintf (stderr, "; LOAD: %s\n", [formdesc UTF8String]); + result = [MLKInterpreter + eval:expansion + inLexicalContext:[MLKLexicalContext globalContext] + withEnvironment:[MLKLexicalEnvironment globalEnvironment] + expandOnly:NO]; + //NSLog (@"; LOAD: Top-level form evaluated."); + } LRELEASE (pool); diff --git a/MLKLLVMCompiler.h b/MLKLLVMCompiler.h index cf2823b..8a10c86 100644 --- a/MLKLLVMCompiler.h +++ b/MLKLLVMCompiler.h @@ -32,6 +32,8 @@ using namespace llvm; #endif @interface MLKLLVMCompiler : NSObject ++(void) load; + +(void) initialize; +(id) compile:(id)object diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index aa0ab79..4b63a89 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -76,6 +76,15 @@ static Constant @implementation MLKLLVMCompiler ++(void) load +{ + if (!MLKDefaultCompiler) + { + MLKDefaultCompiler = self; + MLKLoadCompilesP = YES; + } +} + +(void) initialize { module = new llvm::Module ("MLKLLVMModule"); diff --git a/MLKRoot.m b/MLKRoot.m index 0db7483..361ce16 100644 --- a/MLKRoot.m +++ b/MLKRoot.m @@ -32,6 +32,7 @@ #import "MLKSingleFloat.h" #import "MLKDoubleFloat.h" #import "NSObject-MLKPrinting.h" +#import "globals.h" #import "runtime-compatibility.h" #import "util.h" @@ -708,17 +709,19 @@ as provided by method %@ of object %@", with:nil]]); } -#ifdef USE_LLVM +(NSArray *) compile:(NSArray *)args { + if (!MLKDefaultCompiler) + [NSException raise:@"MLKNotImplementedException" + format:@"It seems as though there is no compiler here."]; + //NSLog (@"Compiling lambda form."); - id thing = [MLKLLVMCompiler compile:denullify([args objectAtIndex:0]) - inContext:[MLKLexicalContext globalContext]]; + id thing = [MLKDefaultCompiler compile:denullify([args objectAtIndex:0]) + inContext:[MLKLexicalContext globalContext]]; //NSLog (@"Compilation done."); //NSLog (@"Compiled: %@", thing); RETURN_VALUE (thing); } -#endif +(NSArray *) fset:(NSArray *)args { diff --git a/globals.h b/globals.h index 076a1ee..3dbda38 100644 --- a/globals.h +++ b/globals.h @@ -16,4 +16,10 @@ * along with this program. If not, see . */ +#import + + extern id MLKEndOfArgumentsMarker; + +extern id MLKDefaultCompiler; +extern BOOL MLKLoadCompilesP; diff --git a/globals.m b/globals.m index 2e8ed17..8781620 100644 --- a/globals.m +++ b/globals.m @@ -23,6 +23,9 @@ id MLKEndOfArgumentsMarker; +id MLKDefaultCompiler = nil; +BOOL MLKLoadCompilesP = NO; + @interface MLKGlobalManager : NSObject +(void) load; -- cgit v1.2.3