From 8d44d97a06275480e43e17a625f596870b9b9da2 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 09:36:55 +0200 Subject: LLVM compiler: Fix dynamic variable lookup. --- MLKLLVMCompiler.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index ae9773b..ba77ae6 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -485,7 +485,7 @@ static Constant if (![_context variableIsLexical:_form]) { //[_compiler insertTrace:@"Dynamic."]; - Value *mlkdynamiccontext = [_compiler insertClassLookup:@"MLKCons"]; + Value *mlkdynamiccontext = [_compiler insertClassLookup:@"MLKDynamicContext"]; Value *dynctx = [_compiler insertMethodCall:@"currentContext" onObject:mlkdynamiccontext]; @@ -922,7 +922,7 @@ static Constant value = [valueForm processForLLVM]; if (![_context variableIsLexical:variable]) { - Value *mlkdynamiccontext = [_compiler insertClassLookup:@"MLKCons"]; + Value *mlkdynamiccontext = [_compiler insertClassLookup:@"MLKDynamicContext"]; Value *dynctx = [_compiler insertMethodCall:@"currentContext" onObject:mlkdynamiccontext]; -- cgit v1.2.3 From bcfa5d70c25915502a3464252c80150aca42f937 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 11:00:31 +0200 Subject: Reader: Do not nullify quoted objects. --- MLKQuoteReader.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MLKQuoteReader.m b/MLKQuoteReader.m index a814c09..8072ddf 100644 --- a/MLKQuoteReader.m +++ b/MLKQuoteReader.m @@ -55,7 +55,7 @@ return [NSArray arrayWithObject: [MLKCons cons:[cl intern:@"QUOTE"] - with:[MLKCons cons:nullify(quoted_form) + with:[MLKCons cons:quoted_form with:nil]]]; } @end -- cgit v1.2.3 From 6efa469caf0146fb6aedb48619c4353d4c624ec4 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 11:12:58 +0200 Subject: APPLY, EVAL: Denullify return values. --- MLKRoot.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MLKRoot.m b/MLKRoot.m index 596b1e7..7eb9f14 100644 --- a/MLKRoot.m +++ b/MLKRoot.m @@ -756,7 +756,7 @@ apply (id *_data, id function, id arglist, id _marker) ? (id)[arglist array] : (id)[NSArray array])]; - return ([values count] > 0 ? [values objectAtIndex:0] : nil); + return ([values count] > 0 ? denullify([values objectAtIndex:0]) : nil); } static id @@ -770,7 +770,7 @@ eval (id *_data, id evaluand, id _marker) withEnvironment: [MLKLexicalEnvironment globalEnvironment]]; - return ([values count] > 0 ? [values objectAtIndex:0] : nil); + return ([values count] > 0 ? denullify([values objectAtIndex:0]) : nil); } static void -- cgit v1.2.3 From a91c396ba85243465acb7a073c58b3e3f8136587 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 11:34:39 +0200 Subject: SETQ (LLVM): Create new dynamic bindings on demand. --- MLKLLVMCompiler.mm | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index ba77ae6..8602ddd 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -927,17 +927,54 @@ static Constant onObject:mlkdynamiccontext]; LRETAIN (variable); // FIXME: release +#ifdef __OBJC_GC__ + // FIXME: proper memory management + if (variable && MLKInstanceP (variable)) + [[NSGarbageCollector defaultCollector] disableCollectorForPointer:variable]; +#endif + Value *symbolV = builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)variable, false), PointerTy); std::vector args; - args.push_back (value); args.push_back (symbolV); - [_compiler insertMethodCall:@"setValue:forSymbol:" - onObject:dynctx - withArgumentVector:&args]; + Value *binding = [_compiler insertMethodCall:@"bindingForSymbol:" + onObject:dynctx + withArgumentVector:&args]; + + // Test whether the binding is non-null. If so, set its value, else create a new one. + + Function *function = builder.GetInsertBlock()->getParent(); + BasicBlock *setBlock = BasicBlock::Create ("setq_set_existing_dynamic_binding", function); + BasicBlock *makeNewBlock = BasicBlock::Create ("setq_make_new_dynamic_binding"); + BasicBlock *joinBlock = BasicBlock::Create ("setq_join"); + + Value *test = builder.CreateICmpNE (binding, ConstantPointerNull::get (PointerTy)); + //Value *value = builder.CreateAlloca (PointerTy, NULL, "if_result"); + builder.CreateCondBr (test, setBlock, makeNewBlock); + + builder.SetInsertPoint (setBlock); + args[0] = value; + [_compiler insertMethodCall:@"setValue:" + onObject:binding + withArgumentVector:&args]; + builder.CreateBr (joinBlock); + + builder.SetInsertPoint (makeNewBlock); + function->getBasicBlockList().push_back (makeNewBlock); + Value *globalctx = [_compiler insertMethodCall:@"globalContext" + onObject:mlkdynamiccontext]; + args[0] = value; + args.push_back (symbolV); + [_compiler insertMethodCall:@"addValue:forSymbol:" + onObject:globalctx + withArgumentVector:&args]; + builder.CreateBr (joinBlock); + + builder.SetInsertPoint (joinBlock); + function->getBasicBlockList().push_back (joinBlock); } else if ([_context variableIsGlobal:variable]) { -- cgit v1.2.3 From a22ae1f747a03dd8cd0e283dca1929962c9cbcf6 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 11:48:28 +0200 Subject: LLVM compiler: Disable the freeing of temporary functions for now. --- MLKLLVMCompiler.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 8602ddd..377bee5 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -177,7 +177,8 @@ static Constant fn = (id (*)()) execution_engine->getPointerToFunction (function); // Execute. lambdaForm = fn(); - execution_engine->freeMachineCodeForFunction (function); + // FIXME: Free machine code when appropriate. (I.e. now? But this crashes after a LOAD.) + //execution_engine->freeMachineCodeForFunction (function); #else Interpreter *i = Interpreter::create (module_provider); lambdaForm = i->runFunction (function)->PointerVal; -- cgit v1.2.3 From 7e28d6c5e341e23a5b27799d67461c1aa2437df3 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 17:23:11 +0200 Subject: LLVM compiler: Rename PointerTy to VoidPointerTy. --- MLKLLVMCompiler.mm | 92 +++++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 377bee5..a37b4c0 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -64,7 +64,7 @@ static IRBuilder builder; static IRBuilder builder; #endif static FunctionPassManager *fpm; -static PointerType *PointerTy, *PointerPointerTy; +static PointerType *VoidPointerTy, *PointerPointerTy; static ModuleProvider *module_provider; @@ -109,8 +109,8 @@ static Constant //execution_engine = ExecutionEngine::create (module_provider, true); execution_engine = ExecutionEngine::create (module_provider, false); - PointerTy = PointerType::get(Type::Int8Ty, 0); - PointerPointerTy = PointerType::get(PointerTy, 0); + VoidPointerTy = PointerType::get(Type::Int8Ty, 0); + PointerPointerTy = PointerType::get(VoidPointerTy, 0); fpm = new FunctionPassManager (module_provider); fpm->add (new TargetData (*execution_engine->getTargetData())); @@ -141,7 +141,7 @@ static Constant Value *v = NULL; BasicBlock *block; std::vector noargs (0, Type::VoidTy); - FunctionType *function_type = FunctionType::get (PointerTy, + FunctionType *function_type = FunctionType::get (VoidPointerTy, noargs, false); Function *function = Function::Create (function_type, @@ -253,8 +253,8 @@ static Constant #else "sel_get_uid", #endif - PointerTy, - PointerTy, + VoidPointerTy, + VoidPointerTy, NULL); Constant *nameptr = createGlobalStringPtr ([name UTF8String]); @@ -291,7 +291,7 @@ static Constant onObject:object withArgumentVector:argv name:@"" - returnType:PointerTy]; + returnType:VoidPointerTy]; } +(Value *) insertMethodCall:(NSString *)messageName @@ -300,7 +300,7 @@ static Constant name:(NSString *)name returnType:(const Type *)returnType { - std::vector argtypes (2, PointerTy); + std::vector argtypes (2, VoidPointerTy); FunctionType *ftype = FunctionType::get (returnType, argtypes, true); Value *sel = [self insertSelectorLookup:messageName]; @@ -309,7 +309,7 @@ static Constant Constant *function = module->getOrInsertFunction ("objc_msgSend", ftype); #else - std::vector lookup_argtypes (2, PointerTy); + std::vector lookup_argtypes (2, VoidPointerTy); FunctionType *lookup_ftype = FunctionType::get (PointerType::get (ftype, 0), lookup_argtypes, false); @@ -362,8 +362,8 @@ static Constant #else "objc_get_class", #endif - PointerTy, - PointerTy, + VoidPointerTy, + VoidPointerTy, NULL); const char *cname = [className UTF8String]; @@ -378,7 +378,7 @@ static Constant Constant *function = module->getOrInsertFunction ("puts", Type::Int32Ty, - PointerTy, + VoidPointerTy, NULL); builder.CreateCall (function, createGlobalStringPtr ([message UTF8String])); @@ -389,13 +389,13 @@ static Constant Constant *function = module->getOrInsertFunction ("printf", Type::Int32Ty, - PointerTy, - PointerTy, + VoidPointerTy, + VoidPointerTy, NULL); builder.CreateCall2 (function, createGlobalStringPtr ("%p\n"), - builder.CreateBitCast (pointerValue, PointerTy)); + builder.CreateBitCast (pointerValue, VoidPointerTy)); } @end @@ -433,7 +433,7 @@ static Constant { NSEnumerator *e = [_bodyForms objectEnumerator]; MLKForm *form; - Value *value = ConstantPointerNull::get (PointerTy); + Value *value = ConstantPointerNull::get (VoidPointerTy); while ((form = [e nextObject])) { @@ -494,7 +494,7 @@ static Constant Value *symbolV = builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)_form, false), - PointerTy); + VoidPointerTy); std::vector args (1, symbolV); value = [_compiler insertMethodCall:@"valueForSymbol:" @@ -558,11 +558,11 @@ static Constant //GlobalVariable *endmarker = module->getGlobalVariable ("MLKEndOfArgumentsMarker", false); //endmarker->setConstant (true); - //GlobalVariable *endmarker = new GlobalVariable (PointerTy, true, GlobalValue::ExternalWeakLinkage); + //GlobalVariable *endmarker = new GlobalVariable (VoidPointerTy, true, GlobalValue::ExternalWeakLinkage); Value *endmarker = builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)MLKEndOfArgumentsMarker, false), - PointerTy); + VoidPointerTy); args.push_back (endmarker); // If the pointer output here is different from the one above, @@ -594,7 +594,7 @@ static Constant -(Value *) reallyProcessForLLVM { std::vector argtypes (1, PointerPointerTy); - FunctionType *ftype = FunctionType::get (PointerTy, argtypes, true); + FunctionType *ftype = FunctionType::get (VoidPointerTy, argtypes, true); Function *function = Function::Create (ftype, Function::InternalLinkage, "a_lisp_closure_body", @@ -616,7 +616,7 @@ static Constant builder.SetInsertPoint (outerBlock); NSArray *freeVariables = [[self freeVariables] allObjects]; - Value *closure_data = builder.CreateAlloca (PointerTy, + Value *closure_data = builder.CreateAlloca (VoidPointerTy, ConstantInt::get(Type::Int32Ty, (uint32_t)[freeVariables count], false)); @@ -657,43 +657,43 @@ static Constant false), PointerType::get(Type::Int8Ty, 0)); - Value *ap = builder.CreateAlloca (PointerTy, NULL, "ap"); - Value *ap2 = builder.CreateBitCast (ap, PointerTy); + Value *ap = builder.CreateAlloca (VoidPointerTy, NULL, "ap"); + Value *ap2 = builder.CreateBitCast (ap, VoidPointerTy); builder.CreateCall (module->getOrInsertFunction ("llvm.va_start", Type::VoidTy, - PointerTy, + VoidPointerTy, NULL), ap2); Value *mlkcons = [_compiler insertClassLookup:@"MLKCons"]; // FIXME: Heap-allocate if appropriate. - Value *lambdaList = builder.CreateAlloca (PointerTy, NULL, "lambda_list"); - Value *lambdaListTail = builder.CreateAlloca (PointerTy, NULL, "lambda_list_tail"); + Value *lambdaList = builder.CreateAlloca (VoidPointerTy, NULL, "lambda_list"); + Value *lambdaListTail = builder.CreateAlloca (VoidPointerTy, NULL, "lambda_list_tail"); - builder.CreateStore (ConstantPointerNull::get (PointerTy), lambdaList); - builder.CreateStore (ConstantPointerNull::get (PointerTy), lambdaListTail); + builder.CreateStore (ConstantPointerNull::get (VoidPointerTy), lambdaList); + builder.CreateStore (ConstantPointerNull::get (VoidPointerTy), lambdaListTail); builder.CreateBr (loopInitBlock); builder.SetInsertPoint (loopInitBlock); function->getBasicBlockList().push_back (loopInitBlock); - Value *arg = builder.CreateVAArg (ap, PointerTy, "arg"); + Value *arg = builder.CreateVAArg (ap, VoidPointerTy, "arg"); Value *cond = builder.CreateICmpEQ (arg, endmarker); builder.CreateCondBr (cond, joinBlock, loopBlock); builder.SetInsertPoint (loopBlock); function->getBasicBlockList().push_back (loopBlock); builder.CreateCondBr (builder.CreateICmpEQ (builder.CreateLoad (lambdaList), - ConstantPointerNull::get (PointerTy)), + ConstantPointerNull::get (VoidPointerTy)), lambdaListNewBlock, lambdaListUpdateBlock); builder.SetInsertPoint (lambdaListNewBlock); function->getBasicBlockList().push_back (lambdaListNewBlock); std::vector argv (1, arg); - argv.push_back (ConstantPointerNull::get (PointerTy)); + argv.push_back (ConstantPointerNull::get (VoidPointerTy)); Value *newLambdaList = [_compiler insertMethodCall:@"cons:with:" onObject:mlkcons withArgumentVector:&argv]; @@ -719,7 +719,7 @@ static Constant builder.CreateCall (module->getOrInsertFunction ("llvm.va_end", Type::VoidTy, - PointerTy, + VoidPointerTy, NULL), ap2); @@ -744,7 +744,7 @@ static Constant if ([_bodyForms count] == 0) { //NSLog (@"%LAMBDA: No body."); - value = ConstantPointerNull::get (PointerTy); + value = ConstantPointerNull::get (VoidPointerTy); } while ((form = [e nextObject])) @@ -772,11 +772,11 @@ static Constant builder.SetInsertPoint (outerBlock); argv[0] = function; - argv[1] = builder.CreateBitCast (closure_data, PointerTy); + argv[1] = builder.CreateBitCast (closure_data, VoidPointerTy); argv.push_back (builder.CreateIntToPtr (ConstantInt::get(Type::Int32Ty, closure_data_size, false), - PointerTy)); + VoidPointerTy)); Value *mlkcompiledclosure = [_compiler insertClassLookup:@"MLKCompiledClosure"]; Value *closure = @@ -793,7 +793,7 @@ static Constant -(Value *) reallyProcessForLLVM { NSEnumerator *e = [_variableBindingForms objectEnumerator]; - Value *value = ConstantPointerNull::get (PointerTy); + Value *value = ConstantPointerNull::get (VoidPointerTy); MLKForm *form; MLKVariableBindingForm *binding_form; @@ -813,7 +813,7 @@ static Constant } else { - Value *binding_variable = builder.CreateAlloca (PointerTy, + Value *binding_variable = builder.CreateAlloca (VoidPointerTy, NULL, [(MLKPrintToString([binding_form name])) UTF8String]); @@ -851,7 +851,7 @@ static Constant return builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)_quotedData, false), - PointerTy); + VoidPointerTy); } @end @@ -872,7 +872,7 @@ static Constant return builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)_form, false), - PointerTy); + VoidPointerTy); } @end @@ -886,8 +886,8 @@ static Constant BasicBlock *joinBlock = BasicBlock::Create ("if_join"); Value *test = builder.CreateICmpNE ([_conditionForm processForLLVM], - ConstantPointerNull::get (PointerTy)); - Value *value = builder.CreateAlloca (PointerTy, NULL, "if_result"); + ConstantPointerNull::get (VoidPointerTy)); + Value *value = builder.CreateAlloca (VoidPointerTy, NULL, "if_result"); builder.CreateCondBr (test, thenBlock, elseBlock); builder.SetInsertPoint (thenBlock); @@ -912,7 +912,7 @@ static Constant { NSEnumerator *var_e, *value_e; MLKForm *valueForm; - Value *value = ConstantPointerNull::get (PointerTy); + Value *value = ConstantPointerNull::get (VoidPointerTy); id variable; var_e = [_variables objectEnumerator]; @@ -937,7 +937,7 @@ static Constant Value *symbolV = builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)variable, false), - PointerTy); + VoidPointerTy); std::vector args; args.push_back (symbolV); @@ -952,8 +952,8 @@ static Constant BasicBlock *makeNewBlock = BasicBlock::Create ("setq_make_new_dynamic_binding"); BasicBlock *joinBlock = BasicBlock::Create ("setq_join"); - Value *test = builder.CreateICmpNE (binding, ConstantPointerNull::get (PointerTy)); - //Value *value = builder.CreateAlloca (PointerTy, NULL, "if_result"); + Value *test = builder.CreateICmpNE (binding, ConstantPointerNull::get (VoidPointerTy)); + //Value *value = builder.CreateAlloca (VoidPointerTy, NULL, "if_result"); builder.CreateCondBr (test, setBlock, makeNewBlock); builder.SetInsertPoint (setBlock); @@ -1019,6 +1019,6 @@ static Constant return builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)package, false), - PointerTy); + VoidPointerTy); } @end -- cgit v1.2.3 From c1c4ac24df8f201403b55eb95c0f9f1c11e3a9f1 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 17:24:28 +0200 Subject: Use ExecutionEngine::runFunction for immediate execution. --- MLKLLVMCompiler.mm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index a37b4c0..91d744d 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -37,6 +37,7 @@ #include #include #include +#include #include //#include #include @@ -174,9 +175,11 @@ static Constant #if 1 // JIT-compile. - fn = (id (*)()) execution_engine->getPointerToFunction (function); + std::vector nogenericargs; + lambdaForm = (id)execution_engine->runFunction (function, nogenericargs).PointerVal; + //fn = (id (*)()) execution_engine->getPointerToFunction (function); // Execute. - lambdaForm = fn(); + //lambdaForm = fn(); // FIXME: Free machine code when appropriate. (I.e. now? But this crashes after a LOAD.) //execution_engine->freeMachineCodeForFunction (function); #else -- cgit v1.2.3 From 08b09a4a194e26feeaacc3dfbb4637ea1f914708 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Mon, 29 Sep 2008 17:32:33 +0200 Subject: LLVM compiler: Cleanups. --- MLKLLVMCompiler.mm | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 91d744d..c62610d 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -55,6 +55,7 @@ #include using namespace llvm; +using namespace std; static ExecutionEngine *execution_engine; @@ -141,7 +142,7 @@ static Constant Value *v = NULL; BasicBlock *block; - std::vector noargs (0, Type::VoidTy); + vector noargs (0, Type::VoidTy); FunctionType *function_type = FunctionType::get (VoidPointerTy, noargs, false); @@ -175,7 +176,7 @@ static Constant #if 1 // JIT-compile. - std::vector nogenericargs; + vector nogenericargs; lambdaForm = (id)execution_engine->runFunction (function, nogenericargs).PointerVal; //fn = (id (*)()) execution_engine->getPointerToFunction (function); // Execute. @@ -266,7 +267,7 @@ static Constant +(Value *) insertMethodCall:(NSString *)messageName onObject:(Value *)object - withArgumentVector:(std::vector *)argv + withArgumentVector:(vector *)argv { return [self insertMethodCall:messageName onObject:object @@ -276,7 +277,7 @@ static Constant +(Value *) insertVoidMethodCall:(NSString *)messageName onObject:(Value *)object - withArgumentVector:(std::vector *)argv + withArgumentVector:(vector *)argv { return [self insertMethodCall:messageName onObject:object @@ -287,7 +288,7 @@ static Constant +(Value *) insertMethodCall:(NSString *)messageName onObject:(Value *)object - withArgumentVector:(std::vector *)argv + withArgumentVector:(vector *)argv name:(NSString *)name { return [self insertMethodCall:messageName @@ -299,11 +300,11 @@ static Constant +(Value *) insertMethodCall:(NSString *)messageName onObject:(Value *)object - withArgumentVector:(std::vector *)argv + withArgumentVector:(vector *)argv name:(NSString *)name returnType:(const Type *)returnType { - std::vector argtypes (2, VoidPointerTy); + vector argtypes (2, VoidPointerTy); FunctionType *ftype = FunctionType::get (returnType, argtypes, true); Value *sel = [self insertSelectorLookup:messageName]; @@ -312,7 +313,7 @@ static Constant Constant *function = module->getOrInsertFunction ("objc_msgSend", ftype); #else - std::vector lookup_argtypes (2, VoidPointerTy); + vector lookup_argtypes (2, VoidPointerTy); FunctionType *lookup_ftype = FunctionType::get (PointerType::get (ftype, 0), lookup_argtypes, false); @@ -323,14 +324,14 @@ static Constant #endif // XXX The following doesn't work. Why? - // std::deque argd (*argv); + // deque argd (*argv); // argd.push_front (sel); // argd.push_front (object); - std::vector argd; + vector argd; argd.push_back (object); argd.push_back (sel); - std::vector::iterator e; + vector::iterator e; for (e = argv->begin(); e != argv->end(); e++) argd.push_back (*e); @@ -341,7 +342,7 @@ static Constant onObject:(Value *)object withName:(NSString *)name { - std::vector argv; + vector argv; return [self insertMethodCall:messageName onObject:object withArgumentVector:&argv @@ -499,7 +500,7 @@ static Constant false), VoidPointerTy); - std::vector args (1, symbolV); + vector args (1, symbolV); value = [_compiler insertMethodCall:@"valueForSymbol:" onObject:dynctx withArgumentVector:&args]; @@ -533,7 +534,7 @@ static Constant Value *functionPtr; Value *closureDataCell; Value *closureDataPtr; - std::vector args; + vector args; if (![_context symbolNamesFunction:_head]) { @@ -596,7 +597,7 @@ static Constant @implementation MLKSimpleLambdaForm (MLKLLVMCompilation) -(Value *) reallyProcessForLLVM { - std::vector argtypes (1, PointerPointerTy); + vector argtypes (1, PointerPointerTy); FunctionType *ftype = FunctionType::get (VoidPointerTy, argtypes, true); Function *function = Function::Create (ftype, Function::InternalLinkage, @@ -695,7 +696,7 @@ static Constant builder.SetInsertPoint (lambdaListNewBlock); function->getBasicBlockList().push_back (lambdaListNewBlock); - std::vector argv (1, arg); + vector argv (1, arg); argv.push_back (ConstantPointerNull::get (VoidPointerTy)); Value *newLambdaList = [_compiler insertMethodCall:@"cons:with:" onObject:mlkcons @@ -710,7 +711,7 @@ static Constant Value *newCons = [_compiler insertMethodCall:@"cons:with:" onObject:mlkcons withArgumentVector:&argv]; - std::vector setcdr_argv (1, newCons); + vector setcdr_argv (1, newCons); [_compiler insertVoidMethodCall:@"setCdr:" onObject:builder.CreateLoad(lambdaListTail) withArgumentVector:&setcdr_argv]; @@ -730,7 +731,7 @@ static Constant { Value *mlkbinding = [_compiler insertClassLookup:@"MLKBinding"]; Value *currentLambdaList = builder.CreateLoad (lambdaList); - std::vector args (1, currentLambdaList); + vector args (1, currentLambdaList); Value *lambdaBinding = [_compiler insertMethodCall:@"bindingWithValue:" onObject:mlkbinding withArgumentVector:&args]; @@ -807,7 +808,7 @@ static Constant if ([_bodyContext variableHeapAllocationForSymbol:[binding_form name]]) { Value *mlkbinding = [_compiler insertClassLookup:@"MLKBinding"]; - std::vector args (1, binding_value); + vector args (1, binding_value); Value *binding = [_compiler insertMethodCall:@"bindingWithValue:" onObject:mlkbinding withArgumentVector:&args]; @@ -942,7 +943,7 @@ static Constant false), VoidPointerTy); - std::vector args; + vector args; args.push_back (symbolV); Value *binding = [_compiler insertMethodCall:@"bindingForSymbol:" onObject:dynctx @@ -983,7 +984,7 @@ static Constant else if ([_context variableIsGlobal:variable]) { Value *binding = builder.Insert ([_context globalBindingValueForSymbol:variable]); - std::vector args (1, value); + vector args (1, value); [_compiler insertVoidMethodCall:@"setValue:" onObject:binding @@ -992,7 +993,7 @@ static Constant else if ([_context variableHeapAllocationForSymbol:variable]) { Value *binding = [_context bindingValueForSymbol:variable]; - std::vector args (1, value); + vector args (1, value); [_compiler insertVoidMethodCall:@"setValue:" onObject:binding -- cgit v1.2.3 From f3cc34c256ed93e09541d1019e00e45012075920 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Wed, 1 Oct 2008 10:25:05 +0200 Subject: LLVM compiler: Fix %LOOP return value. --- MLKLLVMCompiler.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index c62610d..137598e 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -474,7 +474,7 @@ static Constant builder.CreateUnreachable (); - return NULL; + return ConstantPointerNull::get (VoidPointerTy);; } @end -- cgit v1.2.3 From 8c28cc5aaeeb3546e4c113734e18285699bf779e Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Wed, 1 Oct 2008 10:32:17 +0200 Subject: LLVM compiler: Support FUNCTION. --- MLKLLVMCompiler.mm | 68 +++++++++++++++++++++++++++++++++ MLKLexicalContext-MLKLLVMCompilation.h | 1 + MLKLexicalContext-MLKLLVMCompilation.mm | 9 +++++ MLKLexicalContext.h | 5 +++ MLKLexicalContext.m | 26 +++++++++++++ 5 files changed, 109 insertions(+) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 137598e..64b2423 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -1026,3 +1026,71 @@ static Constant VoidPointerTy); } @end + + +@implementation MLKSimpleFunctionForm (MLKLLVMCompilation) +-(Value *) reallyProcessForLLVM +{ + // For global functions, this is easy. For local functions, we need to create + // a new MLKCompiledClosure object. + if ([_context functionIsGlobal:_functionName]) + { + Value *mlklexicalenvironment = [_compiler insertClassLookup:@"MLKLexicalEnvironment"]; + Value *env = [_compiler insertMethodCall:@"globalEnvironment" + onObject:mlklexicalenvironment]; + + LRETAIN (_functionName); // FIXME: release +#ifdef __OBJC_GC__ + // FIXME: proper memory management + if (_functionName && MLKInstanceP (_functionName)) + [[NSGarbageCollector defaultCollector] disableCollectorForPointer:_functionName]; +#endif + + Value *symbolV = builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, + (uint64_t)_functionName, + false), + VoidPointerTy); + + vector args; + args.push_back (symbolV); + Value *fun = [_compiler insertMethodCall:@"functionForSymbol:" + onObject:env + withArgumentVector:&args]; + return fun; + } + else + { + Value *functionCell, *functionPtr; + Value *closureDataCell, *closureDataPtr; + Value *closureDataLengthCell, *closureDataLength; + + functionCell = builder.Insert ([_context functionCellValueForSymbol:_head]); + functionPtr = builder.CreateLoad (functionCell); + closureDataCell = builder.Insert ([_context closureDataPointerValueForSymbol:_head]); + closureDataPtr = builder.CreateLoad (closureDataCell); + closureDataLengthCell = builder.Insert ([_context closureDataLengthValueForSymbol:_head]); + closureDataLength = builder.CreateLoad (closureDataLengthCell); + + vector argv; + argv.push_back (builder.CreateBitCast (functionPtr, VoidPointerTy)); + argv.push_back (builder.CreateBitCast (closureDataPtr, VoidPointerTy)); + argv.push_back (builder.CreateBitCast (closureDataLength, VoidPointerTy)); + Value *mlkcompiledclosure = [_compiler + insertClassLookup:@"MLKCompiledClosure"]; + Value *closure = + [_compiler insertMethodCall:@"closureWithCode:data:length:" + onObject:mlkcompiledclosure + withArgumentVector:&argv]; + + return closure; + } +} +@end + + +@implementation MLKLambdaFunctionForm (MLKLLVMCompilation) +-(Value *) reallyProcessForLLVM +{ + return [_lambdaForm processForLLVM]; +} +@end diff --git a/MLKLexicalContext-MLKLLVMCompilation.h b/MLKLexicalContext-MLKLLVMCompilation.h index 3effb63..bbbe3fb 100644 --- a/MLKLexicalContext-MLKLLVMCompilation.h +++ b/MLKLexicalContext-MLKLLVMCompilation.h @@ -39,6 +39,7 @@ extern id MLKDummyUseLLVMLexicalContext; -(BOOL) variableHeapAllocationForSymbol:(id)name; -(Instruction *) functionCellValueForSymbol:(id)name; -(Instruction *) closureDataPointerValueForSymbol:(id)name; +-(Instruction *) closureDataLengthValueForSymbol:(id)name; -(Value *) bindingValueForSymbol:(id)name; -(void) locallySetBindingValue:(Value *)value forSymbol:(id)name; -(void) setBindingValue:(Value *)value forSymbol:(id)name; diff --git a/MLKLexicalContext-MLKLLVMCompilation.mm b/MLKLexicalContext-MLKLLVMCompilation.mm index 37f03f5..2d525a5 100644 --- a/MLKLexicalContext-MLKLLVMCompilation.mm +++ b/MLKLexicalContext-MLKLLVMCompilation.mm @@ -75,6 +75,15 @@ id MLKDummyUseLLVMLexicalContext = nil; PointerType::get(PointerType::get(PointerType::get(Type::Int8Ty, 0), 0), 0))); } +-(Instruction *) closureDataLengthValueForSymbol:(id)name +{ + // The length cell isn't really a void** but an intptr_t*. + return (new IntToPtrInst (ConstantInt::get(Type::Int64Ty, + (uint64_t)[self closureDataLengthForSymbol:name], + false), + PointerType::get(PointerType::get(Type::Int8Ty, 0), 0))); +} + -(Instruction *) globalBindingValueForSymbol:(id)name { return (new IntToPtrInst (ConstantInt::get(Type::Int64Ty, diff --git a/MLKLexicalContext.h b/MLKLexicalContext.h index f8b7a14..0e51f2c 100644 --- a/MLKLexicalContext.h +++ b/MLKLexicalContext.h @@ -22,6 +22,9 @@ #import #import +#include + + @class MLKEnvironment, MLKLexicalEnvironment, MLKSymbol, NSSet, NSMutableDictionary, NSString, MLKCons; @@ -95,6 +98,7 @@ -(BOOL) variableIsLexical:(MLKSymbol *)symbol; -(BOOL) variableIsGlobal:(id)name; -(BOOL) functionIsInline:(MLKSymbol *)symbol; +-(BOOL) functionIsGlobal:(id)name; -(id) propertyForVariable:(id)name key:(id)key; -(void) setDeepProperty:(id)object @@ -111,6 +115,7 @@ -(void *) functionCellForSymbol:(id)name; -(void *) closureDataPointerForSymbol:(id)name; +-(intptr_t *) closureDataLengthForSymbol:(id)name; -(id) bindingForSymbol:(id)name; -(void) dealloc; diff --git a/MLKLexicalContext.m b/MLKLexicalContext.m index acb5b4e..0afc66b 100644 --- a/MLKLexicalContext.m +++ b/MLKLexicalContext.m @@ -297,6 +297,12 @@ static MLKLexicalContext *global_context; || [self contextForVariable:name] == [MLKLexicalContext globalContext]); } +-(BOOL) functionIsGlobal:(id)name +{ + return (![self contextForFunction:name] + || [self contextForFunction:name] == [MLKLexicalContext globalContext]); +} + -(BOOL) functionIsInline:(MLKSymbol *)symbol { if ([_functions containsObject:symbol]) @@ -455,6 +461,26 @@ static MLKLexicalContext *global_context; } } +-(intptr_t *) closureDataLengthForSymbol:(id)name +{ + id prop = [self propertyForFunction:name + key:@"LEXCTX.closure-data-length"]; + + if (!prop) + { + intptr_t *cell = malloc (sizeof(intptr_t)); + prop = [NSValue valueWithPointer:cell]; + [self setDeepProperty:prop + forFunction:name + key:@"LEXCTX.closure-data-length"]; + return cell; + } + else + { + return (intptr_t*)[prop pointerValue]; + } +} + -(id) bindingForSymbol:(id)name { id prop = [self propertyForVariable:name -- cgit v1.2.3 From 8f594f969b0c495b098c833dba26d5cad93f7cd8 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Wed, 1 Oct 2008 16:44:43 +0200 Subject: MLKSplitDeclarationsDocAndForms: Don't crash if the body starts with a fixnum. --- functions.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions.m b/functions.m index a884c40..3c0c2f9 100644 --- a/functions.m +++ b/functions.m @@ -196,9 +196,10 @@ void MLKSplitDeclarationsDocAndForms (id *decls, id *doc, id *forms, id body, BO *doc = nil; declarations = nil; - while (([[body car] isKindOfClass:[MLKCons class]] - && [[body car] car] == DECLARE) - || (docp && [[body car] isKindOfClass:[NSString class]])) + while (MLKInstanceP ([body car]) + && (([[body car] isKindOfClass:[MLKCons class]] + && [[body car] car] == DECLARE) + || (docp && [[body car] isKindOfClass:[NSString class]]))) { id thing = [body car]; -- cgit v1.2.3 From ae56a5092dd6a39042a0518c59736cd9dbbeb0ba Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Wed, 1 Oct 2008 17:12:28 +0200 Subject: LLVM compiler: Split function compilation from %LAMBDA form handling. --- MLKForm.h | 2 ++ MLKForm.m | 10 ++++++++++ MLKLLVMCompiler.mm | 49 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/MLKForm.h b/MLKForm.h index 24d4920..60b5428 100644 --- a/MLKForm.h +++ b/MLKForm.h @@ -99,6 +99,8 @@ -(void) splitDeclarationsAndBody:(id)object; -(void) processBody:(id)object inContext:(MLKLexicalContext *)context; -(void) processBody:(id)object; +-(NSArray *) bodyForms; +-(MLKLexicalContext *) bodyContext; @end diff --git a/MLKForm.m b/MLKForm.m index c7ee3f5..30ae6ee 100644 --- a/MLKForm.m +++ b/MLKForm.m @@ -318,6 +318,16 @@ return _bodyForms; } +-(NSArray *) bodyForms +{ + return _bodyForms; +} + +-(MLKLexicalContext *) bodyContext +{ + return _bodyContext; +} + -(void) dealloc { LDESTROY (_body); diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 64b2423..281c50b 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -594,15 +594,24 @@ static Constant @end -@implementation MLKSimpleLambdaForm (MLKLLVMCompilation) --(Value *) reallyProcessForLLVM +static void +build_simple_function_definition (MLKBodyForm *processed_form, + id _lambdaListName, + Function*& function, + Value*& closure_data, + intptr_t& closure_data_size) { + NSArray *_bodyForms = [processed_form bodyForms]; + MLKLexicalContext *_bodyContext = [processed_form bodyContext]; + MLKLexicalContext *_context = [processed_form context]; + id _compiler = [MLKLLVMCompiler class]; + vector argtypes (1, PointerPointerTy); FunctionType *ftype = FunctionType::get (VoidPointerTy, argtypes, true); - Function *function = Function::Create (ftype, - Function::InternalLinkage, - "a_lisp_closure_body", - module); + function = Function::Create (ftype, + Function::InternalLinkage, + "a_lisp_closure_body", + module); Function::arg_iterator args = function->arg_begin(); Value *closure_data_arg = args++; @@ -619,12 +628,12 @@ static Constant // ***** HANDLE CLOSURE VARIABLES ***** builder.SetInsertPoint (outerBlock); - NSArray *freeVariables = [[self freeVariables] allObjects]; - Value *closure_data = builder.CreateAlloca (VoidPointerTy, - ConstantInt::get(Type::Int32Ty, - (uint32_t)[freeVariables count], - false)); - int closure_data_size = 0; + NSArray *freeVariables = [[processed_form freeVariables] allObjects]; + closure_data = builder.CreateAlloca (VoidPointerTy, + ConstantInt::get(Type::Int32Ty, + (uint32_t)[freeVariables count], + false)); + closure_data_size = 0; unsigned int i; for (i = 0; i < [freeVariables count]; i++) { @@ -774,9 +783,21 @@ static Constant //NSLog (@"Function built."); builder.SetInsertPoint (outerBlock); +} + + +@implementation MLKSimpleLambdaForm (MLKLLVMCompilation) +-(Value *) reallyProcessForLLVM +{ + intptr_t closure_data_size; + Function *function; + Value *closure_data; + + build_simple_function_definition (self, _lambdaListName, function, closure_data, closure_data_size); - argv[0] = function; - argv[1] = builder.CreateBitCast (closure_data, VoidPointerTy); + vector argv; + argv.push_back (function); + argv.push_back (builder.CreateBitCast (closure_data, VoidPointerTy)); argv.push_back (builder.CreateIntToPtr (ConstantInt::get(Type::Int32Ty, closure_data_size, false), -- cgit v1.2.3 From 922d23c9daafbda42596086757291ae5af914c68 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Wed, 1 Oct 2008 17:25:55 +0200 Subject: Support %FLET on Mac OS X 10.5. --- MLKLLVMCompiler.mm | 125 ++++++++++++++++++++++++-------- MLKLexicalContext-MLKLLVMCompilation.h | 3 + MLKLexicalContext-MLKLLVMCompilation.mm | 21 ++++++ MLKLexicalContext.m | 2 +- 4 files changed, 120 insertions(+), 31 deletions(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 281c50b..51ac0d3 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#import "MLKCompiledClosure.h" #import "MLKDynamicContext.h" #import "MLKLLVMCompiler.h" #import "MLKPackage.h" @@ -54,6 +55,9 @@ #include #include +#include +#include + using namespace llvm; using namespace std; @@ -530,9 +534,7 @@ static Constant @implementation MLKFunctionCallForm (MLKLLVMCompilation) -(Value *) reallyProcessForLLVM { - Value *functionCell; Value *functionPtr; - Value *closureDataCell; Value *closureDataPtr; vector args; @@ -542,10 +544,42 @@ static Constant // XXX Issue a style warning. } - functionCell = builder.Insert ([_context functionCellValueForSymbol:_head]); - functionPtr = builder.CreateLoad (functionCell); - closureDataCell = builder.Insert ([_context closureDataPointerValueForSymbol:_head]); - closureDataPtr = builder.CreateLoad (closureDataCell); + if ([_context functionIsGlobal:_head]) + { + Value *functionCell; + Value *closureDataCell; + + functionCell = builder.Insert ([_context functionCellValueForSymbol:_head]); + functionPtr = builder.CreateLoad (functionCell); + closureDataCell = builder.Insert ([_context closureDataPointerValueForSymbol:_head]); + closureDataPtr = builder.CreateLoad (closureDataCell); + } + else + { + Value *binding = [_context functionBindingValueForSymbol:_head]; + // It's important for closure to be an i8* because we need to calculate + // the GEP offset in terms of bytes. + Value *closure = builder.CreateBitCast ([_compiler insertMethodCall:@"value" onObject:binding], VoidPointerTy); + + //offsetof (MLKCompiledClosure, _code); + ptrdiff_t code_offset = ivar_getOffset (class_getInstanceVariable ([MLKCompiledClosure class], "_code")); + ptrdiff_t data_offset = ivar_getOffset (class_getInstanceVariable ([MLKCompiledClosure class], "_data")); + Constant *code_offset_value = ConstantInt::get (Type::Int32Ty, code_offset, false); + Constant *data_offset_value = ConstantInt::get (Type::Int32Ty, data_offset, false); + Value *codeptr = builder.CreateGEP (closure, code_offset_value); + Value *dataptr = builder.CreateGEP (closure, data_offset_value); + codeptr = builder.CreateBitCast (codeptr, PointerPointerTy, "closure_code_ptr"); + dataptr = builder.CreateBitCast (codeptr, PointerPointerTy, "closure_data_ptr"); + Value *code = builder.CreateLoad (codeptr, "closure_code"); + Value *data = builder.CreateLoad (dataptr, "closure_data"); + + std::vector types (1, PointerPointerTy); + functionPtr = builder.CreateBitCast (code, PointerType::get(FunctionType::get(VoidPointerTy, + types, + true), + 0)); + closureDataPtr = builder.CreateBitCast (data, PointerPointerTy); + } //[_compiler insertTrace:[NSString stringWithFormat:@"Call: %@", MLKPrintToString(_head)]]; //[_compiler insertPointerTrace:functionPtr]; @@ -860,6 +894,58 @@ build_simple_function_definition (MLKBodyForm *processed_form, @end +@implementation MLKSimpleFletForm (MLKLLVMCompilation) +-(Value *) reallyProcessForLLVM +{ + NSEnumerator *e = [_functionBindingForms objectEnumerator]; + Value *value = ConstantPointerNull::get (VoidPointerTy); + MLKForm *form; + MLKSimpleFunctionBindingForm *binding_form; + + while ((binding_form = [e nextObject])) + { + intptr_t closure_data_size; + Function *function; + Value *closure_data; + + build_simple_function_definition (binding_form, [binding_form lambdaListName], function, closure_data, closure_data_size); + + vector argv; + argv.push_back (function); + argv.push_back (builder.CreateBitCast (closure_data, VoidPointerTy)); + argv.push_back (builder.CreateIntToPtr (ConstantInt::get(Type::Int32Ty, + closure_data_size, + false), + VoidPointerTy)); + Value *mlkcompiledclosure = [_compiler + insertClassLookup:@"MLKCompiledClosure"]; + Value *closure = + [_compiler insertMethodCall:@"closureWithCode:data:length:" + onObject:mlkcompiledclosure + withArgumentVector:&argv]; + + Value *binding_value = closure; + + Value *mlkbinding = [_compiler insertClassLookup:@"MLKBinding"]; + vector args (1, binding_value); + Value *binding = [_compiler insertMethodCall:@"bindingWithValue:" + onObject:mlkbinding + withArgumentVector:&args]; + [_bodyContext setFunctionBindingValue:binding + forSymbol:[binding_form name]]; + } + + e = [_bodyForms objectEnumerator]; + while ((form = [e nextObject])) + { + value = [form processForLLVM]; + } + + return value; +} +@end + + @implementation MLKQuoteForm (MLKLLVMCompilation) -(Value *) reallyProcessForLLVM { @@ -1052,8 +1138,6 @@ build_simple_function_definition (MLKBodyForm *processed_form, @implementation MLKSimpleFunctionForm (MLKLLVMCompilation) -(Value *) reallyProcessForLLVM { - // For global functions, this is easy. For local functions, we need to create - // a new MLKCompiledClosure object. if ([_context functionIsGlobal:_functionName]) { Value *mlklexicalenvironment = [_compiler insertClassLookup:@"MLKLexicalEnvironment"]; @@ -1081,27 +1165,8 @@ build_simple_function_definition (MLKBodyForm *processed_form, } else { - Value *functionCell, *functionPtr; - Value *closureDataCell, *closureDataPtr; - Value *closureDataLengthCell, *closureDataLength; - - functionCell = builder.Insert ([_context functionCellValueForSymbol:_head]); - functionPtr = builder.CreateLoad (functionCell); - closureDataCell = builder.Insert ([_context closureDataPointerValueForSymbol:_head]); - closureDataPtr = builder.CreateLoad (closureDataCell); - closureDataLengthCell = builder.Insert ([_context closureDataLengthValueForSymbol:_head]); - closureDataLength = builder.CreateLoad (closureDataLengthCell); - - vector argv; - argv.push_back (builder.CreateBitCast (functionPtr, VoidPointerTy)); - argv.push_back (builder.CreateBitCast (closureDataPtr, VoidPointerTy)); - argv.push_back (builder.CreateBitCast (closureDataLength, VoidPointerTy)); - Value *mlkcompiledclosure = [_compiler - insertClassLookup:@"MLKCompiledClosure"]; - Value *closure = - [_compiler insertMethodCall:@"closureWithCode:data:length:" - onObject:mlkcompiledclosure - withArgumentVector:&argv]; + Value *binding = [_context functionBindingValueForSymbol:_functionName]; + Value *closure = builder.CreateBitCast ([_compiler insertMethodCall:@"value" onObject:binding], VoidPointerTy); return closure; } @@ -1114,4 +1179,4 @@ build_simple_function_definition (MLKBodyForm *processed_form, { return [_lambdaForm processForLLVM]; } -@end +@end \ No newline at end of file diff --git a/MLKLexicalContext-MLKLLVMCompilation.h b/MLKLexicalContext-MLKLLVMCompilation.h index bbbe3fb..429a79c 100644 --- a/MLKLexicalContext-MLKLLVMCompilation.h +++ b/MLKLexicalContext-MLKLLVMCompilation.h @@ -43,6 +43,9 @@ extern id MLKDummyUseLLVMLexicalContext; -(Value *) bindingValueForSymbol:(id)name; -(void) locallySetBindingValue:(Value *)value forSymbol:(id)name; -(void) setBindingValue:(Value *)value forSymbol:(id)name; +-(Value *) functionBindingValueForSymbol:(id)name; +-(void) locallySetFunctionBindingValue:(Value *)value forSymbol:(id)name; +-(void) setFunctionBindingValue:(Value *)value forSymbol:(id)name; -(Instruction *) globalBindingValueForSymbol:(id)name; -(Value *) valueValueForSymbol:(id)name; //-(void) setFunctionCellValue:(Value *)cellPtr forSymbol:(id)name; diff --git a/MLKLexicalContext-MLKLLVMCompilation.mm b/MLKLexicalContext-MLKLLVMCompilation.mm index 2d525a5..b387f62 100644 --- a/MLKLexicalContext-MLKLLVMCompilation.mm +++ b/MLKLexicalContext-MLKLLVMCompilation.mm @@ -120,6 +120,27 @@ id MLKDummyUseLLVMLexicalContext = nil; pointerValue]; } +-(Value *) functionBindingValueForSymbol:(id)name +{ + return (Value *) [[self propertyForVariable:name + key:@"LLVM.function-binding"] + pointerValue]; +} + +-(void) locallySetFunctionBindingValue:(Value *)value forSymbol:(id)name +{ + [self addShallowProperty:[NSValue valueWithPointer:value] + forVariable:name + key:@"LLVM.function-binding"]; +} + +-(void) setFunctionBindingValue:(Value *)value forSymbol:(id)name +{ + [self setDeepProperty:[NSValue valueWithPointer:value] + forVariable:name + key:@"LLVM.function-binding"]; +} + // -(void) setFunctionCellValue:(Value *)cellPtr forSymbol:(id)name // { // [self setDeepProperty:[NSValue valueWithPointer:cellPtr] diff --git a/MLKLexicalContext.m b/MLKLexicalContext.m index 0afc66b..4358402 100644 --- a/MLKLexicalContext.m +++ b/MLKLexicalContext.m @@ -81,7 +81,7 @@ static MLKLexicalContext *global_context; self = [super init]; LASSIGN (_parent, (aContext ? aContext : [MLKLexicalContext globalContext])); - + LASSIGN (_variables, [NSMutableSet setWithSet:vars]); LASSIGN (_functions, [NSMutableSet setWithSet:functions]); -- cgit v1.2.3 From a0dae1a2756c0f9a84c3a258f3a4a05e63afc1c6 Mon Sep 17 00:00:00 2001 From: Matthias Benkard Date: Wed, 1 Oct 2008 17:28:07 +0200 Subject: Support %FLET on the GNU runtime and pre-10.5 versions of Mac OS X. --- MLKCompiledClosure.h | 7 ++++--- MLKCompiledClosure.m | 26 +++++++++++++------------- MLKLLVMCompiler.mm | 13 ++++++++++--- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/MLKCompiledClosure.h b/MLKCompiledClosure.h index 772f29c..5ddd1a9 100644 --- a/MLKCompiledClosure.h +++ b/MLKCompiledClosure.h @@ -26,9 +26,10 @@ @interface MLKCompiledClosure : NSObject { - int _dataLength; - id (*_code)(); - id *_data; +@public + int m_dataLength; + id (*m_code)(); + id *m_data; } // Why intptr_t? Because it makes it easier to call this method from diff --git a/MLKCompiledClosure.m b/MLKCompiledClosure.m index fd643e3..b379dd3 100644 --- a/MLKCompiledClosure.m +++ b/MLKCompiledClosure.m @@ -40,18 +40,18 @@ { int i; - _dataLength = dataLength; - _code = code; + m_dataLength = dataLength; + m_code = code; #ifdef __OBJC_GC__ - _data = NSAllocateCollectable (dataLength * sizeof(id), NSScannedOption); + m_data = NSAllocateCollectable (dataLength * sizeof(id), NSScannedOption); #else - _data = malloc (dataLength * sizeof(id)); + m_data = malloc (dataLength * sizeof(id)); #endif - for (i = 0; i < _dataLength; i++) + for (i = 0; i < m_dataLength; i++) { - _data[i] = LRETAIN (data[i]); + m_data[i] = LRETAIN (data[i]); } return self; @@ -76,7 +76,7 @@ int i; arg_types[0] = &ffi_type_pointer; - argv[0] = &_data; + argv[0] = &m_data; for (i = 1; i < argc - 1; i++) { @@ -101,7 +101,7 @@ // NSLog (@"Argument %d: %p", i, *((void**)argv[i])); // } - ffi_call (&cif, FFI_FN (_code), &return_value, (void**)argv); + ffi_call (&cif, FFI_FN (m_code), &return_value, (void**)argv); // return_value = ((id (*)(void *, ...))_code) (_data, argpointers[0], argpointers[1], MLKEndOfArgumentsMarker); // FIXME: multiple values @@ -120,12 +120,12 @@ -(id (*)()) code { - return _code; + return m_code; } -(void *) closureData { - return _data; + return m_data; } -(void) dealloc @@ -135,10 +135,10 @@ [super dealloc]; // FIXME: Decrease refcount of _code. - for (i = 0; i < _dataLength; i++) + for (i = 0; i < m_dataLength; i++) { - LRELEASE (_data[i]); + LRELEASE (m_data[i]); } - free (_data); + free (m_data); } @end diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 51ac0d3..5428927 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -56,7 +56,10 @@ #include #include +#ifdef MACOSX #include +#include +#endif using namespace llvm; using namespace std; @@ -561,9 +564,13 @@ static Constant // the GEP offset in terms of bytes. Value *closure = builder.CreateBitCast ([_compiler insertMethodCall:@"value" onObject:binding], VoidPointerTy); - //offsetof (MLKCompiledClosure, _code); - ptrdiff_t code_offset = ivar_getOffset (class_getInstanceVariable ([MLKCompiledClosure class], "_code")); - ptrdiff_t data_offset = ivar_getOffset (class_getInstanceVariable ([MLKCompiledClosure class], "_data")); +#if defined(OBJC_API_VERSION) && OBJC_API_VERSION >= 2 + ptrdiff_t code_offset = ivar_getOffset (class_getInstanceVariable ([MLKCompiledClosure class], "m_code")); + ptrdiff_t data_offset = ivar_getOffset (class_getInstanceVariable ([MLKCompiledClosure class], "m_data")); +#else + ptrdiff_t code_offset = offsetof (MLKCompiledClosure, m_code); + ptrdiff_t data_offset = offsetof (MLKCompiledClosure, m_data); +#endif Constant *code_offset_value = ConstantInt::get (Type::Int32Ty, code_offset, false); Constant *data_offset_value = ConstantInt::get (Type::Int32Ty, data_offset, false); Value *codeptr = builder.CreateGEP (closure, code_offset_value); -- cgit v1.2.3