Commit 893dbbb7 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Have the rewriter check for CAPI excs directly

Previously it would have to call out to checkAndThrowCAPIException(),
which is quite a bit slower than what it now can do, which is directly
checking the return value.
parent 779cb5b5
......@@ -19,6 +19,7 @@
#include "codegen/unwinding.h"
#include "core/common.h"
#include "core/stats.h"
#include "runtime/types.h"
namespace pyston {
......@@ -1522,6 +1523,29 @@ void Rewriter::_allocateAndCopyPlus1(RewriterVar* result, RewriterVar* first_ele
assertConsistent();
}
void Rewriter::checkAndThrowCAPIException(RewriterVar* r) {
STAT_TIMER(t0, "us_timer_rewriter", 10);
addAction([=]() { this->_checkAndThrowCAPIException(r); }, { r }, ActionType::MUTATION);
}
void Rewriter::_checkAndThrowCAPIException(RewriterVar* r) {
assembler->comment("_checkAndThrowCAPIException");
assembler::Register var_reg = r->getInReg();
assembler->test(var_reg, var_reg);
{
assembler::ForwardJump jnz(*assembler, assembler::COND_NOT_ZERO);
assembler->mov(assembler::Immediate((void*)throwCAPIException), assembler::R11);
assembler->callq(assembler::R11);
}
r->bumpUse();
assertConsistent();
}
assembler::Indirect Rewriter::indirectFor(Location l) {
assert(l.type == Location::Scratch || l.type == Location::Stack);
......
......@@ -430,6 +430,7 @@ protected:
int _allocate(RewriterVar* result, int n);
void _allocateAndCopy(RewriterVar* result, RewriterVar* array, int n);
void _allocateAndCopyPlus1(RewriterVar* result, RewriterVar* first_elem, RewriterVar* rest, int n_rest);
void _checkAndThrowCAPIException(RewriterVar* r);
// The public versions of these are in RewriterVar
void _addGuard(RewriterVar* var, RewriterVar* val_constant);
......@@ -514,6 +515,9 @@ public:
RewriterVar* allocateAndCopy(RewriterVar* array, int n);
RewriterVar* allocateAndCopyPlus1(RewriterVar* first_elem, RewriterVar* rest, int n_rest);
// This emits `if (!r) throwCAPIException()`
void checkAndThrowCAPIException(RewriterVar* r);
void abort();
void commit();
void commitReturning(RewriterVar* rtn);
......
......@@ -1375,8 +1375,7 @@ Box* dataDescriptorInstanceSpecialCases(GetattrRewriteArgs* rewrite_args, BoxedS
/* has_side_effects */ true, (void*)getset_descr->get, rewrite_args->obj, r_closure);
if (descr->cls == capi_getset_cls)
// TODO I think we are supposed to check the return value?
rewrite_args->rewriter->call(true, (void*)checkAndThrowCAPIException);
rewrite_args->rewriter->checkAndThrowCAPIException(rewrite_args->out_rtn);
rewrite_args->out_success = true;
}
......@@ -1434,7 +1433,7 @@ Box* getattrInternalEx(Box* obj, BoxedString* attr, GetattrRewriteArgs* rewrite_
auto r_rtn = rewrite_args->rewriter->call(true, (void*)obj->cls->tp_getattro, rewrite_args->obj, r_box);
if (S == CXX)
rewrite_args->rewriter->call(true, (void*)checkAndThrowCAPIException);
rewrite_args->rewriter->checkAndThrowCAPIException(r_rtn);
else
rewrite_args->rewriter->call(false, (void*)ensureValidCapiReturn, r_rtn);
......@@ -3658,7 +3657,7 @@ Box* callCLFunc(CLFunction* f, CallRewriteArgs* rewrite_args, int num_output_arg
rewrite_args->out_rtn = rewrite_args->rewriter->call(true, func_ptr, arg_vec);
if (S == CXX && chosen_cf->exception_style == CAPI)
rewrite_args->rewriter->call(true, (void*)checkAndThrowCAPIException);
rewrite_args->rewriter->checkAndThrowCAPIException(rewrite_args->out_rtn);
rewrite_args->out_success = true;
}
......@@ -4649,7 +4648,7 @@ Box* getitemInternal(Box* target, Box* slice, GetitemRewriteArgs* rewrite_args)
r_m->addAttrGuard(offsetof(PyMappingMethods, mp_subscript), (intptr_t)m->mp_subscript);
RewriterVar* r_rtn = rewrite_args->rewriter->call(true, (void*)m->mp_subscript, r_obj, r_slice);
if (S == CXX)
rewrite_args->rewriter->call(true, (void*)checkAndThrowCAPIException);
rewrite_args->rewriter->checkAndThrowCAPIException(r_rtn);
rewrite_args->out_success = true;
rewrite_args->out_rtn = r_rtn;
}
......
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