Skip to content
This repository
Browse code

AOT compiler: added support for some immutable literals

  • Loading branch information...
commit 907bd816d110f44ada74668d43d243b3bb8c724c 1 parent 290673a
Laurent Sansonetti authored

Showing 3 changed files with 111 additions and 12 deletions. Show diff stats Hide diff stats

  1. +99 12 compiler.cpp
  2. +6 0 compiler.h
  3. +6 0 parse.y
111 compiler.cpp
@@ -149,6 +149,7 @@ RoxorAOTCompiler::RoxorAOTCompiler(const char *_fname)
149 149 : RoxorCompiler(_fname)
150 150 {
151 151 cObject_gvar = NULL;
  152 + name2symFunc = NULL;
152 153 }
153 154
154 155 inline SEL
@@ -403,7 +404,8 @@ RoxorCompiler::compile_when_splat(Value *comparedToVal, Value *splatVal)
403 404 }
404 405
405 406 GlobalVariable *
406   -RoxorCompiler::compile_const_global_string(const char *str, const size_t str_len)
  407 +RoxorCompiler::compile_const_global_string(const char *str,
  408 + const size_t str_len)
407 409 {
408 410 assert(str_len > 0);
409 411
@@ -426,7 +428,7 @@ RoxorCompiler::compile_const_global_string(const char *str, const size_t str_len
426 428 true,
427 429 GlobalValue::InternalLinkage,
428 430 ConstantArray::get(str_type, ary_elements),
429   - std::string("global_string.") + str,
  431 + "",
430 432 RoxorCompiler::module);
431 433
432 434 static_strings[s] = gvar;
@@ -461,7 +463,7 @@ RoxorAOTCompiler::compile_mcache(SEL sel, bool super)
461 463 false,
462 464 GlobalValue::InternalLinkage,
463 465 Constant::getNullValue(PtrTy),
464   - std::string("mcache_") + sel_getName(sel),
  466 + "",
465 467 RoxorCompiler::module);
466 468 assert(gvar != NULL);
467 469 mcaches[sel] = gvar;
@@ -492,7 +494,7 @@ RoxorAOTCompiler::compile_ccache(ID name)
492 494 false,
493 495 GlobalValue::InternalLinkage,
494 496 Constant::getNullValue(PtrTy),
495   - std::string("ccache_") + rb_id2name(name),
  497 + "",
496 498 RoxorCompiler::module);
497 499 assert(gvar != NULL);
498 500 ccaches[name] = gvar;
@@ -514,7 +516,7 @@ RoxorAOTCompiler::compile_sel(SEL sel, bool add_to_bb)
514 516 false,
515 517 GlobalValue::InternalLinkage,
516 518 Constant::getNullValue(PtrTy),
517   - std::string("sel_") + sel_getName(sel),
  519 + "",
518 520 RoxorCompiler::module);
519 521 assert(gvar != NULL);
520 522 sels[sel] = gvar;
@@ -952,7 +954,7 @@ RoxorAOTCompiler::gen_slot_cache(ID id)
952 954 false,
953 955 GlobalValue::InternalLinkage,
954 956 Constant::getNullValue(Int32PtrTy),
955   - rb_id2name(id),
  957 + "",
956 958 RoxorCompiler::module);
957 959
958 960 ivar_slots.push_back(gvar);
@@ -1145,7 +1147,7 @@ RoxorAOTCompiler::compile_id(ID id)
1145 1147 false,
1146 1148 GlobalValue::InternalLinkage,
1147 1149 ConstantInt::get(IntTy, 0),
1148   - rb_id2name(id),
  1150 + "",
1149 1151 RoxorCompiler::module);
1150 1152 ids[id] = gvar;
1151 1153 }
@@ -1372,7 +1374,7 @@ RoxorCompiler::compile_dstr(NODE *node)
1372 1374 std::vector<Value *> params;
1373 1375
1374 1376 if (node->nd_lit != 0) {
1375   - params.push_back(ConstantInt::get(RubyObjTy, node->nd_lit));
  1377 + params.push_back(compile_literal(node->nd_lit));
1376 1378 }
1377 1379
1378 1380 NODE *n = node->nd_next;
@@ -2440,12 +2442,43 @@ RoxorCompiler::compile_literal(VALUE val)
2440 2442 }
2441 2443 }
2442 2444
2443   - // The other types are supposedly immutables.
2444   - // TODO AOT compile!
  2445 + return compile_immutable_literal(val);
  2446 +}
  2447 +
  2448 +Value *
  2449 +RoxorCompiler::compile_immutable_literal(VALUE val)
  2450 +{
2445 2451 return ConstantInt::get(RubyObjTy, (long)val);
2446 2452 }
2447 2453
2448 2454 Value *
  2455 +RoxorAOTCompiler::compile_immutable_literal(VALUE val)
  2456 +{
  2457 + if (SPECIAL_CONST_P(val)) {
  2458 + return RoxorCompiler::compile_immutable_literal(val);
  2459 + }
  2460 +
  2461 + std::map<VALUE, GlobalVariable *>::iterator iter = literals.find(val);
  2462 + GlobalVariable *gvar = NULL;
  2463 +
  2464 + if (iter == literals.end()) {
  2465 + gvar = new GlobalVariable(
  2466 + RubyObjTy,
  2467 + false,
  2468 + GlobalValue::InternalLinkage,
  2469 + nilVal,
  2470 + "",
  2471 + RoxorCompiler::module);
  2472 + literals[val] = gvar;
  2473 + }
  2474 + else {
  2475 + gvar = iter->second;
  2476 + }
  2477 +
  2478 + return new LoadInst(gvar, "", bb);
  2479 +}
  2480 +
  2481 +Value *
2449 2482 RoxorCompiler::compile_global_entry(NODE *node)
2450 2483 {
2451 2484 return compile_const_pointer(node->nd_entry);
@@ -2465,7 +2498,7 @@ RoxorAOTCompiler::compile_global_entry(NODE *node)
2465 2498 false,
2466 2499 GlobalValue::InternalLinkage,
2467 2500 Constant::getNullValue(PtrTy),
2468   - rb_id2name(name),
  2501 + "",
2469 2502 RoxorCompiler::module);
2470 2503 global_entries[name] = gvar;
2471 2504 }
@@ -4590,6 +4623,59 @@ RoxorAOTCompiler::compile_main_function(NODE *node)
4590 4623 list.insert(list.begin(), load);
4591 4624 }
4592 4625
  4626 + // Compile literals.
  4627 +
  4628 + for (std::map<VALUE, GlobalVariable *>::iterator i = literals.begin();
  4629 + i != literals.end();
  4630 + ++i) {
  4631 +
  4632 + VALUE val = i->first;
  4633 + GlobalVariable *gvar = i->second;
  4634 +
  4635 + switch (TYPE(val)) {
  4636 + case T_SYMBOL:
  4637 + {
  4638 + if (name2symFunc == NULL) {
  4639 + name2symFunc =
  4640 + cast<Function>(module->getOrInsertFunction(
  4641 + "rb_name2sym",
  4642 + RubyObjTy, PtrTy, NULL));
  4643 + }
  4644 +
  4645 + const char *symname = rb_id2name(SYM2ID(val));
  4646 +
  4647 + GlobalVariable *symname_gvar =
  4648 + compile_const_global_string(symname);
  4649 +
  4650 + std::vector<Value *> idxs;
  4651 + idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
  4652 + idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
  4653 + Instruction *load = GetElementPtrInst::Create(symname_gvar,
  4654 + idxs.begin(), idxs.end(), "");
  4655 +
  4656 + std::vector<Value *> params;
  4657 + params.push_back(load);
  4658 +
  4659 + Instruction *call = CallInst::Create(name2symFunc,
  4660 + params.begin(), params.end(), "");
  4661 +
  4662 + Instruction *assign = new StoreInst(call, gvar, "");
  4663 +
  4664 + list.insert(list.begin(), assign);
  4665 + list.insert(list.begin(), call);
  4666 + list.insert(list.begin(), load);
  4667 + }
  4668 + break;
  4669 +
  4670 + default:
  4671 + printf("unrecognized literal `%s' (class `%s' type %d)\n",
  4672 + RSTRING_PTR(rb_inspect(val)),
  4673 + rb_obj_classname(val),
  4674 + TYPE(val));
  4675 + abort();
  4676 + }
  4677 + }
  4678 +
4593 4679 // Compile global entries.
4594 4680
4595 4681 Function *globalEntryFunc = cast<Function>(module->getOrInsertFunction(
@@ -4634,7 +4720,8 @@ RoxorAOTCompiler::compile_main_function(NODE *node)
4634 4720 ID name = i->first;
4635 4721 GlobalVariable *gvar = i->second;
4636 4722
4637   - GlobalVariable *name_gvar = compile_const_global_string(rb_id2name(name));
  4723 + GlobalVariable *name_gvar =
  4724 + compile_const_global_string(rb_id2name(name));
4638 4725
4639 4726 std::vector<Value *> idxs;
4640 4727 idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
6 compiler.h
@@ -241,6 +241,7 @@ class RoxorCompiler {
241 241 }
242 242 Value *compile_arity(rb_vm_arity_t &arity);
243 243 Value *compile_literal(VALUE val);
  244 + virtual Value *compile_immutable_literal(VALUE val);
244 245 virtual Value *compile_global_entry(NODE *node);
245 246
246 247 void compile_landing_pad_header(void);
@@ -292,8 +293,12 @@ class RoxorAOTCompiler : public RoxorCompiler {
292 293 std::map<ID, GlobalVariable *> ids;
293 294 std::map<ID, GlobalVariable *> global_entries;
294 295 std::vector<GlobalVariable *> ivar_slots;
  296 + std::map<VALUE, GlobalVariable *> literals;
  297 +
295 298 GlobalVariable *cObject_gvar;
296 299
  300 + Function *name2symFunc;
  301 +
297 302 Value *compile_mcache(SEL sel, bool super);
298 303 Value *compile_ccache(ID id);
299 304 Instruction *compile_sel(SEL sel, bool add_to_bb=true);
@@ -302,6 +307,7 @@ class RoxorAOTCompiler : public RoxorCompiler {
302 307 Value *compile_prepare_block_args(Function *func, int *flags);
303 308 Value *compile_nsobject(void);
304 309 Value *compile_id(ID id);
  310 + Value *compile_immutable_literal(VALUE val);
305 311 Value *compile_global_entry(NODE *node);
306 312
307 313 Instruction *gen_slot_cache(ID id);
6 parse.y
@@ -9797,6 +9797,12 @@ rb_id2str(ID id)
9797 9797 return 0;
9798 9798 }
9799 9799
  9800 +VALUE
  9801 +rb_name2sym(const char *name)
  9802 +{
  9803 + return rb_id2str(rb_intern(name));
  9804 +}
  9805 +
9800 9806 const char *
9801 9807 ruby_node_name(int node)
9802 9808 {

0 comments on commit 907bd81

Please sign in to comment.
Something went wrong with that request. Please try again.