Commit df55c251 authored by Boxiang Sun's avatar Boxiang Sun

Add co_names to BoxedCode object.

This field not used very often. So use this lazy way, just retrive
it from bytecode when needed. Add a field to BoxedCode will increase
 the memory usage.
parent cf8e55ef
......@@ -16,6 +16,8 @@
#include <sstream>
#include "codegen/baseline_jit.h"
#include "core/bst.h"
#include "core/cfg.h"
#include "runtime/objmodel.h"
#include "runtime/set.h"
......@@ -101,6 +103,43 @@ Box* BoxedCode::flags(Box* b, void*) noexcept {
return boxInt(flags);
}
Box* BoxedCode::co_names(Box* b, void*) noexcept {
RELEASE_ASSERT(b->cls == code_cls, "");
BoxedCode* code = static_cast<BoxedCode*>(b);
BoxedSet* names = (BoxedSet*)createSet();
for (auto block : code->source->cfg->blocks) {
for (BST_stmt* stmt : *block) {
if (stmt->type() == BST_TYPE::LoadName && bst_cast<BST_LoadName>(stmt)->lookup_type != ScopeInfo::VarScopeType::FAST) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_LoadName>(stmt)->index_id));
}
else if (stmt->type() == BST_TYPE::StoreName && bst_cast<BST_StoreName>(stmt)->lookup_type != ScopeInfo::VarScopeType::FAST) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_StoreName>(stmt)->index_id));
}
else if (stmt->type() == BST_TYPE::DeleteName && bst_cast<BST_DeleteName>(stmt)->lookup_type != ScopeInfo::VarScopeType::FAST) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_DeleteName>(stmt)->index_id));
}
else if (stmt->type() == BST_TYPE::LoadAttr) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_LoadAttr>(stmt)->index_attr));
}
else if (stmt->type() == BST_TYPE::StoreAttr) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_StoreAttr>(stmt)->index_attr));
}
else if (stmt->type() == BST_TYPE::DeleteAttr) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_DeleteAttr>(stmt)->index_attr));
}
else if (stmt->type() == BST_TYPE::ImportFrom) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_ImportFrom>(stmt)->index_id));
}
else if (stmt->type() == BST_TYPE::ImportName) {
PySet_Add(names, code->code_constants.getConstant(bst_cast<BST_ImportName>(stmt)->index_id));
}
}
}
return PySequence_Tuple(names);
}
void BoxedCode::dealloc(Box* b) noexcept {
BoxedCode* o = static_cast<BoxedCode*>(b);
......@@ -260,6 +299,7 @@ void setupCode() {
code_cls->giveAttrBorrowed("__new__", Py_None); // Hacky way of preventing users from instantiating this
code_cls->giveAttrDescriptor("co_name", BoxedCode::co_name, NULL);
code_cls->giveAttrDescriptor("co_names", BoxedCode::co_names, NULL);
code_cls->giveAttrDescriptor("co_filename", BoxedCode::co_filename, NULL);
code_cls->giveAttrDescriptor("co_firstlineno", BoxedCode::co_firstlineno, NULL);
code_cls->giveAttrDescriptor("co_argcount", BoxedCode::argcount, NULL);
......
......@@ -1195,6 +1195,7 @@ public:
// static BORROWED(Box*) name(Box* b, void*) noexcept;
// static BORROWED(Box*) filename(Box* b, void*) noexcept;
static Box* co_name(Box* b, void*) noexcept;
static Box* co_names(Box* b, void*) noexcept;
static Box* co_filename(Box* b, void*) noexcept;
static Box* co_firstlineno(Box* b, void*) noexcept;
static Box* argcount(Box* b, void*) noexcept;
......
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