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

source_changes at macosforge.org source_changes at macosforge.org
Thu Jul 2 19:28:51 PDT 2009


Revision: 1976
          http://trac.macosforge.org/projects/ruby/changeset/1976
Author:   lsansonetti at apple.com
Date:     2009-07-02 19:28:51 -0700 (Thu, 02 Jul 2009)
Log Message:
-----------
AOT compiler: added support for global variables

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

Modified: MacRuby/branches/experimental/compiler.cpp
===================================================================
--- MacRuby/branches/experimental/compiler.cpp	2009-07-03 01:26:21 UTC (rev 1975)
+++ MacRuby/branches/experimental/compiler.cpp	2009-07-03 02:28:51 UTC (rev 1976)
@@ -681,7 +681,7 @@
 	    break;
 
 	case NODE_GASGN:
-	    compile_gvar_assignment(node->nd_entry, val);
+	    compile_gvar_assignment(node, val);
 	    break;
 
 	case NODE_ATTRASGN:
@@ -1049,7 +1049,7 @@
 }
 
 Value *
-RoxorCompiler::compile_gvar_assignment(struct global_entry *entry, Value *val)
+RoxorCompiler::compile_gvar_assignment(NODE *node, Value *val)
 {
     if (gvarSetFunc == NULL) {
 	// VALUE rb_gvar_set(struct global_entry *entry, VALUE val);
@@ -1060,7 +1060,7 @@
 
     std::vector<Value *> params;
 
-    params.push_back(compile_const_pointer(entry));
+    params.push_back(compile_global_entry(node));
     params.push_back(val);
 
     return CallInst::Create(gvarSetFunc, params.begin(),
@@ -2445,6 +2445,37 @@
     return ConstantInt::get(RubyObjTy, (long)val); 
 }
 
+Value *
+RoxorCompiler::compile_global_entry(NODE *node)
+{
+    return compile_const_pointer(node->nd_entry);
+}
+
+Value *
+RoxorAOTCompiler::compile_global_entry(NODE *node)
+{
+    const ID name = node->nd_vid;
+    assert(name > 0);
+    
+    std::map<ID, GlobalVariable *>::iterator iter = global_entries.find(name);
+    GlobalVariable *gvar = NULL;
+    if (iter == global_entries.end()) {
+	gvar = new GlobalVariable(
+		PtrTy,
+		false,
+		GlobalValue::InternalLinkage,
+		Constant::getNullValue(PtrTy),
+		rb_id2name(name),
+		RoxorCompiler::module);
+	global_entries[name] = gvar;
+    }
+    else {
+	gvar = iter->second;
+    }
+
+    return new LoadInst(gvar, "", bb);
+}
+
 void
 RoxorCompiler::compile_ivar_slots(Value *klass,
 	BasicBlock::InstListType &list, 
@@ -2773,7 +2804,7 @@
 
 		std::vector<Value *> params;
 
-		params.push_back(compile_const_pointer(node->nd_entry));
+		params.push_back(compile_global_entry(node));
 
 		return CallInst::Create(gvarGetFunc, params.begin(), params.end(), "", bb);
 	    }
@@ -2785,8 +2816,7 @@
 		assert(node->nd_value != NULL);
 		assert(node->nd_entry != NULL);
 
-		return compile_gvar_assignment(
-			node->nd_entry,
+		return compile_gvar_assignment(node,
 			compile_node(node->nd_value));
 	    }
 	    break;
@@ -4560,6 +4590,37 @@
 	list.insert(list.begin(), load);
     }
 
+    // Compile global entries.
+
+    Function *globalEntryFunc = cast<Function>(module->getOrInsertFunction(
+		"rb_global_entry",
+		PtrTy, IntTy, NULL));
+
+    for (std::map<ID, GlobalVariable *>::iterator i = global_entries.begin();
+	 i != global_entries.end();
+	 ++i) {
+
+	ID name_id = i->first;
+	GlobalVariable *gvar = i->second;
+
+	Value *name_val = compile_id(name_id);
+	assert(Instruction::classof(name_val));
+	Instruction *name = cast<Instruction>(name_val);
+	name->removeFromParent();	
+
+	std::vector<Value *> params;
+	params.push_back(name);	
+
+	Instruction *call = CallInst::Create(globalEntryFunc, params.begin(),
+		params.end(), "");
+
+	Instruction *assign = new StoreInst(call, gvar, "");
+	
+	list.insert(list.begin(), assign);
+	list.insert(list.begin(), call);
+	list.insert(list.begin(), name);
+    }
+
     // Compile IDs.
 
     Function *rbInternFunc = cast<Function>(module->getOrInsertFunction(

Modified: MacRuby/branches/experimental/compiler.h
===================================================================
--- MacRuby/branches/experimental/compiler.h	2009-07-03 01:26:21 UTC (rev 1975)
+++ MacRuby/branches/experimental/compiler.h	2009-07-03 02:28:51 UTC (rev 1976)
@@ -214,7 +214,7 @@
 	Value *compile_ivar_read(ID vid);
 	Value *compile_ivar_assignment(ID vid, Value *val);
 	Value *compile_cvar_assignment(ID vid, Value *val);
-	Value *compile_gvar_assignment(struct global_entry *entry, Value *val);
+	Value *compile_gvar_assignment(NODE *node, Value *val);
 	Value *compile_constant_declaration(NODE *node, Value *val);
 	Value *compile_multiple_assignment(NODE *node, Value *val);
 	void compile_multiple_assignment_element(NODE *node, Value *val);
@@ -241,6 +241,7 @@
 	}
 	Value *compile_arity(rb_vm_arity_t &arity);
 	Value *compile_literal(VALUE val);
+	virtual Value *compile_global_entry(NODE *node);
 
 	void compile_landing_pad_header(void);
 	void compile_landing_pad_footer(void);
@@ -289,6 +290,7 @@
 	std::map<ID, GlobalVariable *> ccaches;
 	std::map<SEL, GlobalVariable *> sels;
 	std::map<ID, GlobalVariable *> ids;
+	std::map<ID, GlobalVariable *> global_entries;
 	std::vector<GlobalVariable *> ivar_slots;
 	GlobalVariable *cObject_gvar;
 
@@ -300,6 +302,7 @@
 	Value *compile_prepare_block_args(Function *func, int *flags);
 	Value *compile_nsobject(void);
 	Value *compile_id(ID id);
+	Value *compile_global_entry(NODE *node);
 
 	Instruction *gen_slot_cache(ID id);
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090702/c214d223/attachment-0001.html>


More information about the macruby-changes mailing list