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(-) (limited to 'MLKLLVMCompiler.mm') 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 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(-) (limited to 'MLKLLVMCompiler.mm') 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(-) (limited to 'MLKLLVMCompiler.mm') 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(-) (limited to 'MLKLLVMCompiler.mm') 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(-) (limited to 'MLKLLVMCompiler.mm') 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(-) (limited to 'MLKLLVMCompiler.mm') 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(-) (limited to 'MLKLLVMCompiler.mm') 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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'MLKLLVMCompiler.mm') 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 -- 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. --- MLKLLVMCompiler.mm | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'MLKLLVMCompiler.mm') 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 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 95 insertions(+), 30 deletions(-) (limited to 'MLKLLVMCompiler.mm') 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 -- 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. --- MLKLLVMCompiler.mm | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'MLKLLVMCompiler.mm') 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