Commit d50f760f authored by Kevin Modzelewski's avatar Kevin Modzelewski

Convert parts of the import system to use BoxedStrings

Reduces boxing during the import process
parent 2e409bdd
......@@ -417,7 +417,7 @@ extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, cons
}
}
BoxedModule* module = createModule(name, NULL, doc);
BoxedModule* module = createModule(boxString(name), NULL, doc);
// Pass self as is, even if NULL we are not allowed to change it to None
Box* passthrough = static_cast<Box*>(self);
......
......@@ -711,8 +711,8 @@ class BoxedClass;
// TODO these shouldn't be here
void setupRuntime();
void teardownRuntime();
Box* createAndRunModule(const std::string& name, const std::string& fn);
BoxedModule* createModule(const std::string& name, const char* fn = NULL, const char* doc = NULL);
Box* createAndRunModule(BoxedString* name, const std::string& fn);
BoxedModule* createModule(BoxedString* name, const char* fn = NULL, const char* doc = NULL);
Box* moduleInit(BoxedModule* self, Box* name, Box* doc = NULL);
// TODO where to put this
......
......@@ -417,7 +417,7 @@ static int main(int argc, char** argv) {
// if the user invoked `pyston -c command`
if (command != NULL) {
try {
main_module = createModule("__main__", "<string>");
main_module = createModule(boxString("__main__"), "<string>");
AST_Module* m = parse_string(command);
compileAndRunModule(m, main_module);
rtncode = 0;
......@@ -428,7 +428,7 @@ static int main(int argc, char** argv) {
}
} else if (module != NULL) {
// TODO: CPython uses the same main module for all code paths
main_module = createModule("__main__", "<string>");
main_module = createModule(boxString("__main__"), "<string>");
rtncode = (RunModule(module, 1) != 0);
} else {
rtncode = 0;
......@@ -455,7 +455,7 @@ static int main(int argc, char** argv) {
prependToSysPath(real_path);
free(real_path);
main_module = createModule("__main__", fn);
main_module = createModule(boxString("__main__"), fn);
try {
AST_Module* ast = caching_parse_file(fn);
compileAndRunModule(ast, main_module);
......@@ -474,7 +474,7 @@ static int main(int argc, char** argv) {
Py_InspectFlag = 0;
if (!main_module) {
main_module = createModule("__main__", "<stdin>");
main_module = createModule(boxString("__main__"), "<stdin>");
} else {
// main_module->fn = "<stdin>";
}
......
......@@ -66,7 +66,7 @@ extern "C" int PyAST_Check(PyObject* o) noexcept {
}
void setupAST() {
BoxedModule* ast_module = createModule("_ast", "__builtin__");
BoxedModule* ast_module = createModule(boxString("_ast"), "__builtin__");
ast_module->giveAttr("PyCF_ONLY_AST", boxInt(PyCF_ONLY_AST));
......
......@@ -1449,9 +1449,9 @@ Box* builtinFormat(Box* value, Box* format_spec) {
}
void setupBuiltins() {
builtins_module
= createModule("__builtin__", NULL, "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is "
"the `nil' object; Ellipsis represents `...' in slices.");
builtins_module = createModule(boxString("__builtin__"), NULL,
"Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is "
"the `nil' object; Ellipsis represents `...' in slices.");
BoxedHeapClass* ellipsis_cls
= BoxedHeapClass::create(type_cls, object_cls, NULL, 0, 0, sizeof(Box), false, "ellipsis");
......
......@@ -43,7 +43,7 @@ static Box* enable() {
}
void setupGC() {
BoxedModule* gc_module = createModule("gc");
BoxedModule* gc_module = createModule(boxString("gc"));
gc_module->giveAttr("collect",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)gcCollect, NONE, 0), "collect"));
......
......@@ -64,7 +64,7 @@ static Box* dumpStats(Box* includeZeros) {
}
void setupPyston() {
pyston_module = createModule("__pyston__");
pyston_module = createModule(boxString("__pyston__"));
pyston_module->giveAttr("setOption",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setOption, UNKNOWN, 2), "setOption"));
......
......@@ -586,7 +586,7 @@ void setupSys() {
gc::registerPermanentRoot(sys_modules_dict);
// This is ok to call here because we've already created the sys_modules_dict
sys_module = createModule("sys");
sys_module = createModule(boxString("sys"));
sys_module->giveAttr("modules", sys_modules_dict);
......
This diff is collapsed.
......@@ -21,7 +21,7 @@ namespace pyston {
extern "C" Box* import(int level, Box* from_imports, llvm::StringRef module_name);
extern Box* importModuleLevel(llvm::StringRef module_name, Box* globals, Box* from_imports, int level);
BoxedModule* importCExtension(const std::string& full_name, const std::string& last_name, const std::string& path);
BoxedModule* importCExtension(BoxedString* full_name, const std::string& last_name, const std::string& path);
}
#endif
......@@ -3816,26 +3816,25 @@ void setupRuntime() {
TRACK_ALLOCATIONS = true;
}
BoxedModule* createModule(const std::string& name, const char* fn, const char* doc) {
BoxedModule* createModule(BoxedString* name, const char* fn, const char* doc) {
assert((!fn || strlen(fn)) && "probably wanted to set the fn to <stdin>?");
BoxedDict* d = getSysModulesDict();
Box* b_name = boxString(name);
// Surprisingly, there are times that we need to return the existing module if
// one exists:
Box* existing = d->getOrNull(b_name);
Box* existing = d->getOrNull(name);
if (existing && PyModule_Check(existing)) {
return static_cast<BoxedModule*>(existing);
}
BoxedModule* module = new BoxedModule();
moduleInit(module, boxString(name), boxString(doc ? doc : ""));
moduleInit(module, name, boxString(doc ? doc : ""));
if (fn)
module->giveAttr("__file__", boxString(fn));
d->d[b_name] = module;
if (name == "__main__")
d->d[name] = module;
if (name->s() == "__main__")
module->giveAttr("__builtins__", builtins_module);
return module;
}
......
......@@ -40,7 +40,7 @@ TEST_F(AnalysisTest, augassign) {
FutureFlags future_flags = getFutureFlags(module->body, fn.c_str());
SourceInfo* si = new SourceInfo(createModule("augassign", fn.c_str()), scoping, future_flags, func,
SourceInfo* si = new SourceInfo(createModule(boxString("augassign"), fn.c_str()), scoping, future_flags, func,
func->body, boxString(fn));
CFG* cfg = computeCFG(si, func->body);
......@@ -70,7 +70,7 @@ void doOsrTest(bool is_osr, bool i_maybe_undefined) {
FutureFlags future_flags = getFutureFlags(module->body, fn.c_str());
ScopeInfo* scope_info = scoping->getScopeInfoForNode(func);
std::unique_ptr<SourceInfo> si(new SourceInfo(createModule("osr" + std::to_string((is_osr << 1) + i_maybe_undefined),
std::unique_ptr<SourceInfo> si(new SourceInfo(createModule(boxString("osr" + std::to_string((is_osr << 1) + i_maybe_undefined)),
fn.c_str()), scoping, future_flags, func, func->body, boxString(fn)));
CLFunction* clfunc = new CLFunction(0, 0, false, false, std::move(si));
......
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