Commit dd25db82 authored by Michael Arntzenius's avatar Michael Arntzenius

make tuple indexing work in JIT mode

parent 386b688f
...@@ -2154,21 +2154,28 @@ public: ...@@ -2154,21 +2154,28 @@ public:
assert(v->getType() == g.i64); assert(v->getType() == g.i64);
if (llvm::ConstantInt* ci = llvm::dyn_cast<llvm::ConstantInt>(v)) { if (llvm::ConstantInt* ci = llvm::dyn_cast<llvm::ConstantInt>(v)) {
int64_t i = ci->getSExtValue(); int64_t i = ci->getSExtValue();
if (i >= 0 && i < var->getValue()->size()) { auto elts = var->getValue();
CompilerVariable* rtn = (*var->getValue())[i]; if (i >= 0 && i < elts->size()) {
CompilerVariable* rtn = (*elts)[i];
rtn->incvref();
return rtn;
} else if (i < 0 && -i <= elts->size()) {
CompilerVariable* rtn = (*elts)[elts->size() + i];
rtn->incvref(); rtn->incvref();
return rtn; return rtn;
} else { } else {
llvm::CallSite call = emitter.createCall2(info.unw_info, g.funcs.raiseAttributeErrorStr, llvm::CallSite call = emitter.createCall(info.unw_info, g.funcs.raiseIndexErrorStr,
getStringConstantPtr(debugName() + '\0'), getStringConstantPtr("tuple\0"));
getStringConstantPtr("__getitem__\0"));
call.setDoesNotReturn(); call.setDoesNotReturn();
return undefVariable(); return undefVariable();
} }
} }
} }
RELEASE_ASSERT(0, "");
// return getConstantInt(var->getValue()->size(), g.i64); ConcreteCompilerVariable* converted = var->makeConverted(emitter, BOXED_TUPLE);
CompilerVariable* rtn = converted->getitem(emitter, info, slice);
converted->decvref(emitter);
return rtn;
} }
ConcreteCompilerVariable* len(IREmitter& emitter, const OpInfo& info, VAR* var) override { ConcreteCompilerVariable* len(IREmitter& emitter, const OpInfo& info, VAR* var) override {
......
...@@ -214,6 +214,7 @@ void initGlobalFuncs(GlobalState& g) { ...@@ -214,6 +214,7 @@ void initGlobalFuncs(GlobalState& g) {
GET(unpackIntoArray); GET(unpackIntoArray);
GET(raiseAttributeError); GET(raiseAttributeError);
GET(raiseAttributeErrorStr); GET(raiseAttributeErrorStr);
GET(raiseIndexErrorStr);
GET(raiseNotIterableError); GET(raiseNotIterableError);
GET(assertNameDefined); GET(assertNameDefined);
GET(assertFailDerefNameDefined); GET(assertFailDerefNameDefined);
......
...@@ -41,7 +41,7 @@ struct GlobalFuncs { ...@@ -41,7 +41,7 @@ struct GlobalFuncs {
*exceptionMatches, *yield, *getiterHelper, *hasnext; *exceptionMatches, *yield, *getiterHelper, *hasnext;
llvm::Value* unpackIntoArray, *raiseAttributeError, *raiseAttributeErrorStr, *raiseNotIterableError, llvm::Value* unpackIntoArray, *raiseAttributeError, *raiseAttributeErrorStr, *raiseNotIterableError,
*assertNameDefined, *assertFail, *assertFailDerefNameDefined; *raiseIndexErrorStr, *assertNameDefined, *assertFail, *assertFailDerefNameDefined;
llvm::Value* printFloat, *listAppendInternal, *getSysStdout; llvm::Value* printFloat, *listAppendInternal, *getSysStdout;
llvm::Value* runtimeCall0, *runtimeCall1, *runtimeCall2, *runtimeCall3, *runtimeCall, *runtimeCallN; llvm::Value* runtimeCall0, *runtimeCall1, *runtimeCall2, *runtimeCall3, *runtimeCall, *runtimeCallN;
llvm::Value* callattr0, *callattr1, *callattr2, *callattr3, *callattr, *callattrN; llvm::Value* callattr0, *callattr1, *callattr2, *callattr3, *callattr, *callattrN;
......
...@@ -96,6 +96,7 @@ void force() { ...@@ -96,6 +96,7 @@ void force() {
FORCE(unpackIntoArray); FORCE(unpackIntoArray);
FORCE(raiseAttributeError); FORCE(raiseAttributeError);
FORCE(raiseAttributeErrorStr); FORCE(raiseAttributeErrorStr);
FORCE(raiseIndexErrorStr);
FORCE(raiseNotIterableError); FORCE(raiseNotIterableError);
FORCE(assertNameDefined); FORCE(assertNameDefined);
FORCE(assertFailDerefNameDefined); FORCE(assertFailDerefNameDefined);
......
...@@ -252,6 +252,10 @@ extern "C" void raiseAttributeError(Box* obj, const char* attr) { ...@@ -252,6 +252,10 @@ extern "C" void raiseAttributeError(Box* obj, const char* attr) {
} }
} }
extern "C" void raiseIndexErrorStr(const char* typeName) {
raiseExcHelper(IndexError, "%s index out of range", typeName);
}
extern "C" void raiseNotIterableError(const char* typeName) { extern "C" void raiseNotIterableError(const char* typeName) {
raiseExcHelper(TypeError, "'%s' object is not iterable", typeName); raiseExcHelper(TypeError, "'%s' object is not iterable", typeName);
} }
......
...@@ -132,6 +132,7 @@ Box* typeLookup(BoxedClass* cls, const std::string& attr, GetattrRewriteArgs* re ...@@ -132,6 +132,7 @@ Box* typeLookup(BoxedClass* cls, const std::string& attr, GetattrRewriteArgs* re
extern "C" void raiseAttributeErrorStr(const char* typeName, const char* attr) __attribute__((__noreturn__)); extern "C" void raiseAttributeErrorStr(const char* typeName, const char* attr) __attribute__((__noreturn__));
extern "C" void raiseAttributeError(Box* obj, const char* attr) __attribute__((__noreturn__)); extern "C" void raiseAttributeError(Box* obj, const char* attr) __attribute__((__noreturn__));
extern "C" void raiseNotIterableError(const char* typeName) __attribute__((__noreturn__)); extern "C" void raiseNotIterableError(const char* typeName) __attribute__((__noreturn__));
extern "C" void raiseIndexErrorStr(const char* typeName) __attribute__((__noreturn__));
Box* typeCall(Box*, BoxedTuple*, BoxedDict*); Box* typeCall(Box*, BoxedTuple*, BoxedDict*);
Box* typeNew(Box* cls, Box* arg1, Box* arg2, Box** _args); Box* typeNew(Box* cls, Box* arg1, Box* arg2, Box** _args);
......
def f():
print (1,2,3)[-1]
f()
def f2():
try:
print (1,2,3)[4]
except IndexError as e:
print e
try:
print (1,2,3)[-4]
except IndexError as e:
print e
f2()
def f3():
try:
print (1,2,3)['hello']
except TypeError as e:
print e
f3()
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