Commit d5d5ace0 authored by Chris Toshok's avatar Chris Toshok

cache BoxedString*'s for a module's constant AST_TYPE::Str nodes. emit the...

cache BoxedString*'s for a module's constant AST_TYPE::Str nodes.  emit the pointers to these BoxedString's in the generated IR
parent 1e98809f
......@@ -1013,7 +1013,7 @@ Value ASTInterpreter::visit_set(AST_Set* node) {
Value ASTInterpreter::visit_str(AST_Str* node) {
if (node->str_type == AST_Str::STR) {
return boxString(node->str_data);
return source_info->parent_module->getStringConstant(node->str_data);
} else if (node->str_type == AST_Str::UNICODE) {
return decodeUTF8StringPtr(&node->str_data);
} else {
......
......@@ -72,7 +72,7 @@ struct GlobalState {
llvm::Type* llvm_str_type_ptr;
llvm::Type* frame_info_type;
llvm::Type* llvm_clfunction_type_ptr, *llvm_closure_type_ptr, *llvm_generator_type_ptr;
llvm::Type* llvm_module_type_ptr, *llvm_bool_type_ptr;
llvm::Type* llvm_module_type_ptr, *llvm_bool_type_ptr, *llvm_boxed_string_type_ptr;
llvm::Type* llvm_excinfo_type;
llvm::Type* i1, *i8, *i8_ptr, *i32, *i64, *void_, *double_;
llvm::Type* vector_ptr;
......
......@@ -1013,7 +1013,12 @@ private:
CompilerVariable* evalStr(AST_Str* node, UnwindInfo unw_info) {
if (node->str_type == AST_Str::STR) {
return makeStr(&node->str_data);
llvm::Value* rtn
= embedConstantPtr(irstate->getSourceInfo()->parent_module->getStringConstant(node->str_data),
g.llvm_boxed_string_type_ptr);
return new ConcreteCompilerVariable(STR, rtn, true);
} else if (node->str_type == AST_Str::UNICODE) {
return makeUnicode(emitter, &node->str_data);
} else {
......
......@@ -153,6 +153,9 @@ void initGlobalFuncs(GlobalState& g) {
g.frame_info_type = g.stdlib_module->getTypeByName("struct.pyston::FrameInfo");
assert(g.frame_info_type);
g.llvm_boxed_string_type_ptr = g.stdlib_module->getTypeByName("class.pyston::BoxedString")->getPointerTo();
assert(g.llvm_boxed_string_type_ptr);
#define GET(N) g.funcs.N = getFunc((void*)N, STRINGIFY(N))
g.funcs.printf = addFunc((void*)printf, g.i8_ptr, true);
......
......@@ -390,6 +390,25 @@ std::string BoxedModule::name() {
}
}
Box* BoxedModule::getStringConstant(const std::string& ast_str) {
auto idx_iter = str_const_index.find(ast_str);
if (idx_iter != str_const_index.end())
return str_constants[idx_iter->second];
Box* box = boxString(ast_str);
str_const_index[ast_str] = str_constants.size();
str_constants.push_back(box);
return box;
}
extern "C" void moduleGCHandler(GCVisitor* v, Box* b) {
boxGCHandler(v, b);
BoxedModule* d = (BoxedModule*)b;
v->visitRange((void* const*)&d->str_constants[0], (void* const*)&d->str_constants[d->str_constants.size()]);
}
extern "C" Box* boxCLFunction(CLFunction* f, BoxedClosure* closure, bool isGenerator,
std::initializer_list<Box*> defaults) {
if (closure)
......@@ -1859,8 +1878,8 @@ void setupRuntime() {
function_cls->simple_destructor = builtin_function_or_method_cls->simple_destructor = functionDtor;
module_cls = new BoxedHeapClass(object_cls, NULL, offsetof(BoxedModule, attrs), 0, sizeof(BoxedModule), false,
new BoxedString("module"));
module_cls = new BoxedHeapClass(object_cls, &moduleGCHandler, offsetof(BoxedModule, attrs), 0, sizeof(BoxedModule),
false, new BoxedString("module"));
member_cls
= new BoxedHeapClass(object_cls, NULL, 0, 0, sizeof(BoxedMemberDescriptor), false, new BoxedString("member"));
capifunc_cls
......
......@@ -529,6 +529,11 @@ public:
BoxedModule(const std::string& name, const std::string& fn, const char* doc = NULL);
std::string name();
Box* getStringConstant(const std::string& ast_str);
llvm::StringMap<int> str_const_index;
std::vector<Box*> str_constants;
DEFAULT_CLASS(module_cls);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment