Skip to content

Commit

Permalink
move calculateNumVRegs
Browse files Browse the repository at this point in the history
  • Loading branch information
undingen committed Nov 18, 2015
1 parent ebb4e16 commit ca2384a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/codegen/ast_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1702,14 +1702,6 @@ static int calculateNumVRegs(FunctionMetadata* md) {
SourceInfo* source_info = md->source.get();

CFG* cfg = source_info->cfg;

// Note: due to some (avoidable) restrictions, this check is pretty constrained in where
// it can go, due to the fact that it can throw an exception.
// It can't go in the ASTInterpreter constructor, since that will cause the C++ runtime to
// delete the partially-constructed memory which we don't currently handle. It can't go into
// executeInner since we want the SyntaxErrors to happen *before* the stack frame is entered.
// (For instance, throwing the exception will try to fetch the current statement, but we determine
// that by looking at the cfg.)
if (!cfg)
cfg = source_info->cfg = computeCFG(source_info, source_info->body);

Expand Down Expand Up @@ -1786,7 +1778,7 @@ Box* astInterpretFunction(FunctionMetadata* md, Box* closure, Box* generator, Bo
}

Box** vregs = NULL;
int num_vregs = calculateNumVRegs(md);
int num_vregs = md->calculateNumVRegs();
if (num_vregs > 0) {
vregs = (Box**)alloca(sizeof(Box*) * num_vregs);
memset(vregs, 0, sizeof(Box*) * num_vregs);
Expand Down Expand Up @@ -1817,7 +1809,7 @@ Box* astInterpretFunctionEval(FunctionMetadata* md, Box* globals, Box* boxedLoca
++md->times_interpreted;

Box** vregs = NULL;
int num_vregs = calculateNumVRegs(md);
int num_vregs = md->calculateNumVRegs();
if (num_vregs > 0) {
vregs = (Box**)alloca(sizeof(Box*) * num_vregs);
memset(vregs, 0, sizeof(Box*) * num_vregs);
Expand Down Expand Up @@ -1848,7 +1840,7 @@ static Box* astInterpretDeoptInner(FunctionMetadata* md, AST_expr* after_expr, A
SourceInfo* source_info = md->source.get();

Box** vregs = NULL;
int num_vregs = calculateNumVRegs(md);
int num_vregs = md->calculateNumVRegs();
if (num_vregs > 0) {
vregs = (Box**)alloca(sizeof(Box*) * num_vregs);
memset(vregs, 0, sizeof(Box*) * num_vregs);
Expand Down Expand Up @@ -1973,15 +1965,13 @@ FrameInfo* getFrameInfoForInterpretedFrame(void* frame_ptr) {
return interpreter->getFrameInfo();
}

BoxedDict* localsForInterpretedFrame(void* frame_ptr, bool only_user_visible) {
ASTInterpreter* interpreter = getInterpreterFromFramePtr(frame_ptr);
assert(interpreter);
BoxedDict* localsForInterpretedFrame(Box** vregs, CFG* cfg, bool only_user_visible) {
BoxedDict* rtn = new BoxedDict();
for (auto& l : interpreter->getSymVRegMap()) {
for (auto& l : cfg->sym_vreg_map) {
if (only_user_visible && (l.first.s()[0] == '!' || l.first.s()[0] == '#'))
continue;

Box* val = interpreter->getVRegs()[l.second];
Box* val = vregs[l.second];
if (val) {
assert(gc::isValidGCObject(val));
rtn->d[l.first.getBox()] = val;
Expand All @@ -1991,6 +1981,12 @@ BoxedDict* localsForInterpretedFrame(void* frame_ptr, bool only_user_visible) {
return rtn;
}

BoxedDict* localsForInterpretedFrame(void* frame_ptr, bool only_user_visible) {
ASTInterpreter* interpreter = getInterpreterFromFramePtr(frame_ptr);
assert(interpreter);
return localsForInterpretedFrame(interpreter->getVRegs(), interpreter->getMD()->source->cfg, only_user_visible);
}

BoxedClosure* passedClosureForInterpretedFrame(void* frame_ptr) {
ASTInterpreter* interpreter = getInterpreterFromFramePtr(frame_ptr);
assert(interpreter);
Expand Down
1 change: 1 addition & 0 deletions src/codegen/ast_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct FrameInfo;
FrameInfo* getFrameInfoForInterpretedFrame(void* frame_ptr);
BoxedClosure* passedClosureForInterpretedFrame(void* frame_ptr);

BoxedDict* localsForInterpretedFrame(Box** vregs, CFG* cfg, bool only_user_visible);
BoxedDict* localsForInterpretedFrame(void* frame_ptr, bool only_user_visible);

// Executes the equivalent of CPython's PRINT_EXPR opcode (call sys.displayhook)
Expand Down
23 changes: 23 additions & 0 deletions src/codegen/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "codegen/baseline_jit.h"
#include "codegen/compvars.h"
#include "core/ast.h"
#include "core/cfg.h"
#include "core/util.h"
#include "runtime/code.h"
#include "runtime/types.h"
Expand Down Expand Up @@ -72,6 +73,28 @@ BoxedCode* FunctionMetadata::getCode() {
return code_obj;
}

int FunctionMetadata::calculateNumVRegs() {
SourceInfo* source_info = source.get();

CFG* cfg = source_info->cfg;

// Note: due to some (avoidable) restrictions, this check is pretty constrained in where
// it can go, due to the fact that it can throw an exception.
// It can't go in the ASTInterpreter constructor, since that will cause the C++ runtime to
// delete the partially-constructed memory which we don't currently handle. It can't go into
// executeInner since we want the SyntaxErrors to happen *before* the stack frame is entered.
// (For instance, throwing the exception will try to fetch the current statement, but we determine
// that by looking at the cfg.)
if (!cfg)
cfg = source_info->cfg = computeCFG(source_info, source_info->body);

if (!cfg->hasVregsAssigned()) {
ScopeInfo* scope_info = source->getScopeInfo();
cfg->assignVRegs(param_names, scope_info);
}
return cfg->sym_vreg_map.size();
}

void FunctionMetadata::addVersion(CompiledFunction* compiled) {
assert(compiled);
assert((compiled->spec != NULL) + (compiled->entry_descriptor != NULL) == 1);
Expand Down
2 changes: 2 additions & 0 deletions src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ class FunctionMetadata {
void addVersion(void* f, ConcreteCompilerType* rtn_type, const std::vector<ConcreteCompilerType*>& arg_types,
ExceptionStyle exception_style = CXX);

int calculateNumVRegs();

// Helper function, meant for the C++ runtime, which allocates a FunctionMetadata object and calls addVersion
// once to it.
static FunctionMetadata* create(void* f, ConcreteCompilerType* rtn_type, int nargs, bool takes_varargs,
Expand Down

0 comments on commit ca2384a

Please sign in to comment.