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

source_changes at macosforge.org source_changes at macosforge.org
Fri Jul 3 17:38:04 PDT 2009


Revision: 1978
          http://trac.macosforge.org/projects/ruby/changeset/1978
Author:   lsansonetti at apple.com
Date:     2009-07-03 17:38:03 -0700 (Fri, 03 Jul 2009)
Log Message:
-----------
AOT compiler: added support for some immutable literals

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

Modified: MacRuby/branches/experimental/compiler.cpp
===================================================================
--- MacRuby/branches/experimental/compiler.cpp	2009-07-03 03:33:50 UTC (rev 1977)
+++ MacRuby/branches/experimental/compiler.cpp	2009-07-04 00:38:03 UTC (rev 1978)
@@ -149,6 +149,7 @@
     : RoxorCompiler(_fname)
 {
     cObject_gvar = NULL;
+    name2symFunc = NULL;
 }
 
 inline SEL
@@ -403,7 +404,8 @@
 }
 
 GlobalVariable *
-RoxorCompiler::compile_const_global_string(const char *str, const size_t str_len)
+RoxorCompiler::compile_const_global_string(const char *str,
+	const size_t str_len)
 {
     assert(str_len > 0);
 
@@ -426,7 +428,7 @@
 		true,
 		GlobalValue::InternalLinkage,
 		ConstantArray::get(str_type, ary_elements),
-		std::string("global_string.") + str,
+		"",
 		RoxorCompiler::module);
 
 	static_strings[s] = gvar;
@@ -461,7 +463,7 @@
 		    false,
 		    GlobalValue::InternalLinkage,
 		    Constant::getNullValue(PtrTy),
-		    std::string("mcache_") + sel_getName(sel),
+		    "",
 		    RoxorCompiler::module);
 	    assert(gvar != NULL);
 	    mcaches[sel] = gvar;
@@ -492,7 +494,7 @@
 		false,
 		GlobalValue::InternalLinkage,
 		Constant::getNullValue(PtrTy),
-		std::string("ccache_") + rb_id2name(name),
+		"",
 		RoxorCompiler::module);
 	assert(gvar != NULL);
 	ccaches[name] = gvar;
@@ -514,7 +516,7 @@
 		false,
 		GlobalValue::InternalLinkage,
 		Constant::getNullValue(PtrTy),
-		std::string("sel_") + sel_getName(sel),
+		"",
 		RoxorCompiler::module);
 	assert(gvar != NULL);
 	sels[sel] = gvar;
@@ -952,7 +954,7 @@
 	    false,
 	    GlobalValue::InternalLinkage,
 	    Constant::getNullValue(Int32PtrTy),
-	    rb_id2name(id),
+	    "",
 	    RoxorCompiler::module);
 
     ivar_slots.push_back(gvar);
@@ -1145,7 +1147,7 @@
 		false,
 		GlobalValue::InternalLinkage,
 		ConstantInt::get(IntTy, 0),
-		rb_id2name(id),
+		"",
 		RoxorCompiler::module);
 	ids[id] = gvar;
     }
@@ -1372,7 +1374,7 @@
     std::vector<Value *> params;
 
     if (node->nd_lit != 0) {
-	params.push_back(ConstantInt::get(RubyObjTy, node->nd_lit));
+	params.push_back(compile_literal(node->nd_lit));
     }
 
     NODE *n = node->nd_next;
@@ -2440,12 +2442,43 @@
 	}
     }
 
-    // The other types are supposedly immutables.
-    // TODO AOT compile!
+    return compile_immutable_literal(val);
+}
+
+Value *
+RoxorCompiler::compile_immutable_literal(VALUE val)
+{
     return ConstantInt::get(RubyObjTy, (long)val); 
 }
 
 Value *
+RoxorAOTCompiler::compile_immutable_literal(VALUE val)
+{
+    if (SPECIAL_CONST_P(val)) {
+	return RoxorCompiler::compile_immutable_literal(val);
+    }
+
+    std::map<VALUE, GlobalVariable *>::iterator iter = literals.find(val);
+    GlobalVariable *gvar = NULL;
+
+    if (iter == literals.end()) {
+	gvar = new GlobalVariable(
+		RubyObjTy,
+		false,
+		GlobalValue::InternalLinkage,
+		nilVal,
+		"",
+		RoxorCompiler::module);
+	literals[val] = gvar;
+    }
+    else {
+	gvar = iter->second;
+    }
+
+    return new LoadInst(gvar, "", bb);
+}
+
+Value *
 RoxorCompiler::compile_global_entry(NODE *node)
 {
     return compile_const_pointer(node->nd_entry);
@@ -2465,7 +2498,7 @@
 		false,
 		GlobalValue::InternalLinkage,
 		Constant::getNullValue(PtrTy),
-		rb_id2name(name),
+		"",
 		RoxorCompiler::module);
 	global_entries[name] = gvar;
     }
@@ -4590,6 +4623,59 @@
 	list.insert(list.begin(), load);
     }
 
+    // Compile literals.
+
+    for (std::map<VALUE, GlobalVariable *>::iterator i = literals.begin();
+	 i != literals.end();
+	 ++i) {
+
+	VALUE val = i->first;
+	GlobalVariable *gvar = i->second;
+
+	switch (TYPE(val)) {
+	    case T_SYMBOL:
+		{
+		    if (name2symFunc == NULL) {
+			name2symFunc =
+			    cast<Function>(module->getOrInsertFunction(
+					"rb_name2sym",
+					RubyObjTy, PtrTy, NULL));
+		    }
+
+		    const char *symname = rb_id2name(SYM2ID(val));
+		    			 
+		    GlobalVariable *symname_gvar =
+			compile_const_global_string(symname);
+
+		    std::vector<Value *> idxs;
+		    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    Instruction *load = GetElementPtrInst::Create(symname_gvar,
+			    idxs.begin(), idxs.end(), "");
+
+		    std::vector<Value *> params;
+		    params.push_back(load);
+
+		    Instruction *call = CallInst::Create(name2symFunc,
+			    params.begin(), params.end(), "");
+
+		    Instruction *assign = new StoreInst(call, gvar, "");
+
+		    list.insert(list.begin(), assign);
+		    list.insert(list.begin(), call);
+		    list.insert(list.begin(), load);
+		}
+		break;
+
+	    default:
+		printf("unrecognized literal `%s' (class `%s' type %d)\n",
+			RSTRING_PTR(rb_inspect(val)),
+			rb_obj_classname(val),
+			TYPE(val));
+		abort();
+	}
+    }
+
     // Compile global entries.
 
     Function *globalEntryFunc = cast<Function>(module->getOrInsertFunction(
@@ -4634,7 +4720,8 @@
 	ID name = i->first;
 	GlobalVariable *gvar = i->second;
 	
-	GlobalVariable *name_gvar = compile_const_global_string(rb_id2name(name));
+	GlobalVariable *name_gvar =
+	    compile_const_global_string(rb_id2name(name));
 
 	std::vector<Value *> idxs;
 	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));

Modified: MacRuby/branches/experimental/compiler.h
===================================================================
--- MacRuby/branches/experimental/compiler.h	2009-07-03 03:33:50 UTC (rev 1977)
+++ MacRuby/branches/experimental/compiler.h	2009-07-04 00:38:03 UTC (rev 1978)
@@ -241,6 +241,7 @@
 	}
 	Value *compile_arity(rb_vm_arity_t &arity);
 	Value *compile_literal(VALUE val);
+	virtual Value *compile_immutable_literal(VALUE val);
 	virtual Value *compile_global_entry(NODE *node);
 
 	void compile_landing_pad_header(void);
@@ -292,8 +293,12 @@
 	std::map<ID, GlobalVariable *> ids;
 	std::map<ID, GlobalVariable *> global_entries;
 	std::vector<GlobalVariable *> ivar_slots;
+	std::map<VALUE, GlobalVariable *> literals;
+
 	GlobalVariable *cObject_gvar;
 
+	Function *name2symFunc;
+
 	Value *compile_mcache(SEL sel, bool super);
 	Value *compile_ccache(ID id);
 	Instruction *compile_sel(SEL sel, bool add_to_bb=true);
@@ -302,6 +307,7 @@
 	Value *compile_prepare_block_args(Function *func, int *flags);
 	Value *compile_nsobject(void);
 	Value *compile_id(ID id);
+	Value *compile_immutable_literal(VALUE val);
 	Value *compile_global_entry(NODE *node);
 
 	Instruction *gen_slot_cache(ID id);

Modified: MacRuby/branches/experimental/parse.y
===================================================================
--- MacRuby/branches/experimental/parse.y	2009-07-03 03:33:50 UTC (rev 1977)
+++ MacRuby/branches/experimental/parse.y	2009-07-04 00:38:03 UTC (rev 1978)
@@ -9797,6 +9797,12 @@
     return 0;
 }
 
+VALUE
+rb_name2sym(const char *name)
+{
+    return rb_id2str(rb_intern(name));
+}
+
 const char *
 ruby_node_name(int node)
 {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090703/a0cbba89/attachment.html>


More information about the macruby-changes mailing list