From 1e1bbbf51144d68dcef7d1abf8dab0f3ade3fb1a Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 2 Sep 2008 10:22:24 +0200 Subject: LLVM compiler: Access closure variables through the closure data pointer. --- MLKLLVMCompiler.mm | 63 ++++++++++++++++++++------------- MLKLexicalContext-MLKLLVMCompilation.h | 1 + MLKLexicalContext-MLKLLVMCompilation.mm | 13 +++++-- MLKLexicalContext.h | 7 ++-- MLKLexicalContext.m | 27 ++++++++++---- 5 files changed, 75 insertions(+), 36 deletions(-) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 17186e7..83246c1 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -589,8 +589,46 @@ static Constant BasicBlock *lambdaListNewBlock = BasicBlock::Create ("lambda_list_new"); BasicBlock *lambdaListUpdateBlock = BasicBlock::Create ("lambda_list_update"); - builder.SetInsertPoint (initBlock); + // ***** HANDLE CLOSURE VARIABLES ***** + builder.SetInsertPoint (outerBlock); + NSArray *freeVariables = [[self freeVariables] allObjects]; + Value *closure_data = builder.CreateMalloc (PointerTy, + ConstantInt::get(Type::Int32Ty, + (uint32_t)[freeVariables count], + false)); + int closure_data_size = 0; + unsigned int i; + for (i = 0; i < [freeVariables count]; i++) + { + // FIXME: We assume heap allocation for all closure variables. + MLKSymbol *symbol = [freeVariables objectAtIndex:i]; + if (![_context variableIsGlobal:symbol]) + { + Constant *position = ConstantInt::get(Type::Int32Ty, closure_data_size, false); + + // Fill in the closure data array. + builder.SetInsertPoint (outerBlock); + Value *binding = [_context bindingValueForSymbol:symbol]; + Value *closure_value_ptr = builder.CreateGEP (closure_data, position); + builder.CreateStore (binding, closure_value_ptr); + + // Access the closure data array from within the closure. + builder.SetInsertPoint (initBlock); + Value *local_closure_var = builder.CreateAlloca (PointerTy, NULL, "closure_variable"); + Value *local_closure_value_ptr = builder.CreateGEP (closure_data_arg, + position); + builder.CreateStore (local_closure_value_ptr, local_closure_var); + [_bodyContext locallySetBindingValue:local_closure_var + forSymbol:symbol]; + + closure_data_size++; + } + } + + + // ***** HANDLE ARGUMENTS ***** + builder.SetInsertPoint (initBlock); Value *endmarker = builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)MLKEndOfArgumentsMarker, false), @@ -696,29 +734,6 @@ static Constant builder.SetInsertPoint (outerBlock); - NSArray *freeVariables = [[self freeVariables] allObjects]; - Value *closure_data = builder.CreateMalloc (PointerTy, - ConstantInt::get(Type::Int32Ty, - (uint32_t)[freeVariables count], - false)); - int closure_data_size = 0; - unsigned int i; - for (i = 0; i < [freeVariables count]; i++) - { - // FIXME: We assume heap allocation for all closure variables. - MLKSymbol *symbol = [freeVariables objectAtIndex:i]; - if (![_context variableIsGlobal:symbol]) - { - Value *binding = [_context bindingValueForSymbol:symbol]; - Value *closure_value_ptr = builder.CreateGEP (closure_data, - ConstantInt::get(Type::Int32Ty, - closure_data_size, - false)); - builder.CreateStore (binding, closure_value_ptr); - closure_data_size++; - } - } - argv[0] = function; argv[1] = closure_data; argv.push_back (builder.CreateIntToPtr (ConstantInt::get(Type::Int32Ty, diff --git a/MLKLexicalContext-MLKLLVMCompilation.h b/MLKLexicalContext-MLKLLVMCompilation.h index b6fdf9d..3effb63 100644 --- a/MLKLexicalContext-MLKLLVMCompilation.h +++ b/MLKLexicalContext-MLKLLVMCompilation.h @@ -40,6 +40,7 @@ extern id MLKDummyUseLLVMLexicalContext; -(Instruction *) functionCellValueForSymbol:(id)name; -(Instruction *) closureDataPointerValueForSymbol:(id)name; -(Value *) bindingValueForSymbol:(id)name; +-(void) locallySetBindingValue:(Value *)value forSymbol:(id)name; -(void) setBindingValue:(Value *)value forSymbol:(id)name; -(Instruction *) globalBindingValueForSymbol:(id)name; -(Value *) valueValueForSymbol:(id)name; diff --git a/MLKLexicalContext-MLKLLVMCompilation.mm b/MLKLexicalContext-MLKLLVMCompilation.mm index e670151..4285d1e 100644 --- a/MLKLexicalContext-MLKLLVMCompilation.mm +++ b/MLKLexicalContext-MLKLLVMCompilation.mm @@ -44,7 +44,7 @@ id MLKDummyUseLLVMLexicalContext = nil; -(BOOL) variableHeapAllocationForSymbol:(id)name; { - id flag = [self deepPropertyForVariable:name + id flag = [self propertyForVariable:name key:@"LLVM.heap-flag"]; if (flag) @@ -85,11 +85,18 @@ id MLKDummyUseLLVMLexicalContext = nil; -(Value *) bindingValueForSymbol:(id)name { - return (Value *) [[self deepPropertyForVariable:name + return (Value *) [[self propertyForVariable:name key:@"LLVM.variable-binding"] pointerValue]; } +-(void) locallySetBindingValue:(Value *)value forSymbol:(id)name +{ + [self addShallowProperty:[NSValue valueWithPointer:value] + forVariable:name + key:@"LLVM.variable-binding"]; +} + -(void) setBindingValue:(Value *)value forSymbol:(id)name { [self setDeepProperty:[NSValue valueWithPointer:value] @@ -99,7 +106,7 @@ id MLKDummyUseLLVMLexicalContext = nil; -(Value *) valueValueForSymbol:(id)name { - return (Value *) [[self deepPropertyForVariable:name + return (Value *) [[self propertyForVariable:name key:@"LLVM.variable-value"] pointerValue]; } diff --git a/MLKLexicalContext.h b/MLKLexicalContext.h index 3e703e5..f8b7a14 100644 --- a/MLKLexicalContext.h +++ b/MLKLexicalContext.h @@ -96,12 +96,15 @@ -(BOOL) variableIsGlobal:(id)name; -(BOOL) functionIsInline:(MLKSymbol *)symbol; --(id) deepPropertyForVariable:(id)name key:(id)key; +-(id) propertyForVariable:(id)name key:(id)key; -(void) setDeepProperty:(id)object forVariable:(id)name key:(id)key; +-(void) addShallowProperty:(id)object + forVariable:(id)name + key:(id)key; --(id) deepPropertyForFunction:(id)name key:(id)key; +-(id) propertyForFunction:(id)name key:(id)key; -(void) setDeepProperty:(id)object forFunction:(id)name key:(id)key; diff --git a/MLKLexicalContext.m b/MLKLexicalContext.m index 5f694ff..5456aa3 100644 --- a/MLKLexicalContext.m +++ b/MLKLexicalContext.m @@ -331,7 +331,7 @@ static MLKLexicalContext *global_context; [_functions addObject:symbol]; } --(id) deepPropertyForVariable:(id)name key:(id)key +-(id) propertyForVariable:(id)name key:(id)key { NSDictionary *props = [_variableInfo objectForKey:nullify(name)]; id property; @@ -341,7 +341,7 @@ static MLKLexicalContext *global_context; else if (!_parent || [_variables containsObject:nullify(name)]) return nil; else - return [_parent deepPropertyForVariable:name key:key]; + return [_parent propertyForVariable:name key:key]; } -(void) setDeepProperty:(id)object @@ -368,7 +368,20 @@ static MLKLexicalContext *global_context; } } --(id) deepPropertyForFunction:(id)name key:(id)key +-(void) addShallowProperty:(id)object + forVariable:(id)name + key:(id)key +{ + NSMutableDictionary *props = [_variableInfo objectForKey:nullify(name)]; + if (!props) + { + props = [NSMutableDictionary dictionary]; + [_variableInfo setObject:props forKey:nullify(name)]; + } + [props setObject:object forKey:key]; +} + +-(id) propertyForFunction:(id)name key:(id)key { NSDictionary *props = [_functionInfo objectForKey:nullify(name)]; id property; @@ -378,7 +391,7 @@ static MLKLexicalContext *global_context; else if (!_parent || [_functions containsObject:nullify(name)]) return nil; else - return [_parent deepPropertyForFunction:name key:key]; + return [_parent propertyForFunction:name key:key]; } -(void) setDeepProperty:(id)object @@ -403,7 +416,7 @@ static MLKLexicalContext *global_context; -(void *) functionCellForSymbol:(id)name { - id prop = [self deepPropertyForFunction:name + id prop = [self propertyForFunction:name key:@"LEXCTX.function-cell"]; if (!prop) @@ -423,7 +436,7 @@ static MLKLexicalContext *global_context; -(void *) closureDataPointerForSymbol:(id)name { - id prop = [self deepPropertyForFunction:name + id prop = [self propertyForFunction:name key:@"LEXCTX.closure-data"]; if (!prop) @@ -443,7 +456,7 @@ static MLKLexicalContext *global_context; -(id) bindingForSymbol:(id)name { - id prop = [self deepPropertyForVariable:name + id prop = [self propertyForVariable:name key:@"LEXCTX.variable-binding"]; if (!prop) -- cgit v1.2.3