[macruby-changes] [1883] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Thu Jun 18 21:27:51 PDT 2009


Revision: 1883
          http://trac.macosforge.org/projects/ruby/changeset/1883
Author:   lsansonetti at apple.com
Date:     2009-06-18 21:27:50 -0700 (Thu, 18 Jun 2009)
Log Message:
-----------
fixed a few problems with blocks/binding

Modified Paths:
--------------
    MacRuby/branches/experimental/compiler.cpp
    MacRuby/branches/experimental/vm.cpp

Modified: MacRuby/branches/experimental/compiler.cpp
===================================================================
--- MacRuby/branches/experimental/compiler.cpp	2009-06-19 04:25:56 UTC (rev 1882)
+++ MacRuby/branches/experimental/compiler.cpp	2009-06-19 04:27:50 UTC (rev 1883)
@@ -855,10 +855,8 @@
 	 iter != lvars.end(); ++iter) {
 	ID name = iter->first;
 	Value *slot = iter->second;
-	if (std::find(dvars.begin(), dvars.end(), name) == dvars.end()) {
-	    params.push_back(ConstantInt::get(IntTy, (long)name));
-	    params.push_back(slot);
-	}
+	params.push_back(ConstantInt::get(IntTy, (long)name));
+	params.push_back(slot);
     }
 
     return CallInst::Create(prepareBlockFunc, params.begin(), params.end(),
@@ -869,9 +867,11 @@
 RoxorCompiler::compile_binding(void)
 {
     if (pushBindingFunc == NULL) {
-	// void rb_vm_push_binding(VALUE self, int lvars_size, ...);
+	// void rb_vm_push_binding(VALUE self, rb_vm_block_t *current_block,
+	//			   int lvars_size, ...);
 	std::vector<const Type *> types;
 	types.push_back(RubyObjTy);
+	types.push_back(PtrTy);
 	types.push_back(Type::Int32Ty);
 	FunctionType *ft = FunctionType::get(Type::VoidTy, types, true);
 	pushBindingFunc = cast<Function>
@@ -880,12 +880,16 @@
 
     std::vector<Value *> params;
     params.push_back(current_self);
+    params.push_back(running_block == NULL
+	    ? compile_const_pointer(NULL) : running_block);
 
     // Lvars.
     params.push_back(ConstantInt::get(Type::Int32Ty, (int)lvars.size()));
     for (std::map<ID, Value *>::iterator iter = lvars.begin();
 	 iter != lvars.end(); ++iter) {
-	params.push_back(ConstantInt::get(IntTy, (long)iter->first));
+
+	ID lvar = iter->first;
+	params.push_back(ConstantInt::get(IntTy, lvar));
 	params.push_back(iter->second);
     }
 
@@ -2424,6 +2428,7 @@
 
 		    if (has_vars_to_save) {
 			current_var_uses = new AllocaInst(PtrTy, "", bb);
+			//current_var_uses = new MallocInst(PtrTy, "", bb);
 			new StoreInst(compile_const_pointer(NULL), current_var_uses, bb);
 		    }
 
@@ -2500,8 +2505,8 @@
 		    std::vector<Value *> params;
 
 		    params.push_back(NULL); // will be filled later
-
-		    params.push_back(ConstantInt::get(Type::Int32Ty, (int)lvars.size()));
+		    params.push_back(NULL); // idem
+		    int vars_count = 0;
 		    for (std::map<ID, Value *>::iterator iter = lvars.begin();
 			 iter != lvars.end(); ++iter) {
 			ID name = iter->first;
@@ -2509,22 +2514,29 @@
 			if (std::find(dvars.begin(), dvars.end(), name) == dvars.end()) {
 			    params.push_back(ConstantInt::get(IntTy, (long)name));
 			    params.push_back(slot);
+			    vars_count++;
 			}
 		    }
+		    params[1] = ConstantInt::get(Type::Int32Ty, vars_count);
 
 		    // searches all ReturnInst in the function we just created and add before
 		    // a call to the function to save the local variables if necessary
 		    // (we can't do this before finishing compiling the whole function
 		    // because we can't be sure if a block is there or not before)
-		    for (Function::iterator block_it = f->begin(); block_it != f->end(); ++block_it) {
-			for (BasicBlock::iterator inst_it = block_it->begin(); inst_it != block_it->end(); ++inst_it) {
+		    for (Function::iterator block_it = f->begin();
+			 block_it != f->end();
+			 ++block_it) {
+			for (BasicBlock::iterator inst_it = block_it->begin();
+			     inst_it != block_it->end();
+			     ++inst_it) {
 			    if (dyn_cast<ReturnInst>(inst_it)) {
 				// LoadInst needs to be inserted in a BasicBlock
 				// so we has to wait before putting it in params
 				params[0] = new LoadInst(current_var_uses, "", inst_it);
 
 				// TODO: only call the function if current_use is not NULL
-				CallInst::Create(keepVarsFunc, params.begin(), params.end(), "", inst_it);
+				CallInst::Create(keepVarsFunc, params.begin(), params.end(), "",
+					inst_it);
 			    }
 			}
 		    }

Modified: MacRuby/branches/experimental/vm.cpp
===================================================================
--- MacRuby/branches/experimental/vm.cpp	2009-06-19 04:25:56 UTC (rev 1882)
+++ MacRuby/branches/experimental/vm.cpp	2009-06-19 04:27:50 UTC (rev 1883)
@@ -2806,7 +2806,7 @@
     b->self = self;
     b->node = node;
     b->parent_var_uses = parent_var_uses;
-    b->parent_block = parent_block;
+    GC_WB(&b->parent_block, parent_block);
 
     va_list ar;
     va_start(ar, dvars_size);
@@ -2865,8 +2865,8 @@
 	if ((*var_uses == NULL)
 	    || ((*var_uses)->uses_count == VM_LVAR_USES_SIZE)) {
 
-	    rb_vm_var_uses* new_uses =
-		(rb_vm_var_uses*)malloc(sizeof(rb_vm_var_uses));
+	    rb_vm_var_uses *new_uses =
+		(rb_vm_var_uses *)malloc(sizeof(rb_vm_var_uses));
 	    new_uses->next = *var_uses;
 	    new_uses->uses_count = 0;
 	    *var_uses = new_uses;
@@ -2878,6 +2878,7 @@
 
     // we should not keep references that won't be used
     block->parent_block = NULL;
+    block->parent_var_uses = NULL;
 }
 
 struct rb_vm_kept_local {
@@ -2932,6 +2933,7 @@
 			}
 		    }
 		}
+
 		// indicate to the GC that we do not have a reference here anymore
 		rb_gc_assign_weak_ref(NULL, &current->uses[use_index]);
 	    }
@@ -2943,27 +2945,44 @@
     }
 }
 
+static inline rb_vm_local_t **
+push_local(rb_vm_local_t **l, ID name, VALUE *value)
+{
+    GC_WB(l, xmalloc(sizeof(rb_vm_local_t)));
+    (*l)->name = name;
+    (*l)->value = value;
+    (*l)->next = NULL;
+    return &(*l)->next;
+}
+
 extern "C"
 void
-rb_vm_push_binding(VALUE self, int lvars_size, ...)
+rb_vm_push_binding(VALUE self, rb_vm_block_t *current_block,
+		   int lvars_size, ...)
 {
-    rb_vm_binding_t *b = (rb_vm_binding_t *)xmalloc(sizeof(rb_vm_binding_t));
-    GC_WB(&b->self, self);
+    rb_vm_binding_t *binding =
+	(rb_vm_binding_t *)xmalloc(sizeof(rb_vm_binding_t));
+    GC_WB(&binding->self, self);
 
+    rb_vm_local_t **l = &binding->locals;
+
+    for (rb_vm_block_t *b = current_block; b != NULL; b = b->parent_block) {
+	for (rb_vm_local_t *li = b->locals; li != NULL; li = li->next) {
+	    l = push_local(l, li->name, li->value);
+	}
+    }
+
     va_list ar;
     va_start(ar, lvars_size);
-    rb_vm_local_t **l = &b->locals;
     for (int i = 0; i < lvars_size; ++i) {
-	GC_WB(l, xmalloc(sizeof(rb_vm_local_t)));
-	(*l)->name = va_arg(ar, ID);
-	(*l)->value = va_arg(ar, VALUE *);
-	(*l)->next = NULL;
-	l = &(*l)->next;
+	ID name = va_arg(ar, ID);
+	VALUE *value = va_arg(ar, VALUE *);
+	l = push_local(l, name, value);
     }
     va_end(ar);
 
-    rb_objc_retain(b);
-    GET_VM()->bindings.push_back(b);
+    rb_objc_retain(binding);
+    GET_VM()->bindings.push_back(binding);
 }
 
 extern "C"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090618/87984976/attachment.html>


More information about the macruby-changes mailing list