void setupTuple() { tuple_iterator_cls = BoxedHeapClass::create(type_cls, object_cls, &BoxedTupleIterator::gcHandler, 0, 0, sizeof(BoxedTupleIterator), false, "tuple"); tuple_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)tupleNew, UNKNOWN, 1, 0, true, true))); CLFunction* getitem = createRTFunction(2, 0, 0, 0); addRTFunction(getitem, (void*)tupleGetitemInt, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, BOXED_INT }); addRTFunction(getitem, (void*)tupleGetitemSlice, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, SLICE }); addRTFunction(getitem, (void*)tupleGetitem<CXX>, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, UNKNOWN }, CXX); addRTFunction(getitem, (void*)tupleGetitem<CAPI>, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, UNKNOWN }, CAPI); tuple_cls->giveAttr("__getitem__", new BoxedFunction(getitem)); tuple_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)tupleContains, BOXED_BOOL, 2))); tuple_cls->giveAttr("index", new BoxedFunction(boxRTFunction((void*)tupleIndex, BOXED_INT, 4, 2, false, false), { boxInt(0), boxInt(std::numeric_limits<Py_ssize_t>::max()) })); tuple_cls->giveAttr("count", new BoxedFunction(boxRTFunction((void*)tupleCount, BOXED_INT, 2))); tuple_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)tupleIter, typeFromClass(tuple_iterator_cls), 1))); tuple_cls->tp_richcompare = tuplerichcompare; tuple_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)tupleNonzero, BOXED_BOOL, 1))); tuple_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)tupleLen, BOXED_INT, 1))); tuple_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)tupleRepr, STR, 1))); tuple_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)tupleAdd, BOXED_TUPLE, 2))); tuple_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)tupleMul, BOXED_TUPLE, 2))); tuple_cls->giveAttr("__rmul__", new BoxedFunction(boxRTFunction((void*)tupleMul, BOXED_TUPLE, 2))); tuple_cls->tp_hash = (hashfunc)tuple_hash; tuple_cls->tp_as_sequence->sq_slice = (ssizessizeargfunc)&tupleslice; add_operators(tuple_cls); tuple_cls->freeze(); tuple_cls->tp_as_sequence->sq_item = (ssizeargfunc)tupleitem; tuple_cls->tp_as_sequence->sq_length = (lenfunc)tuplelength; tuple_cls->tp_iter = tupleIter; CLFunction* hasnext = boxRTFunction((void*)tupleiterHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)tupleiterHasnext, BOXED_BOOL); tuple_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); tuple_iterator_cls->giveAttr( "__iter__", new BoxedFunction(boxRTFunction((void*)tupleIterIter, typeFromClass(tuple_iterator_cls), 1))); tuple_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)tupleiterNext, UNKNOWN, 1))); tuple_iterator_cls->freeze(); tuple_iterator_cls->tpp_hasnext = tupleiterHasnextUnboxed; }
void setupDict() { dict_iterator_cls = new BoxedClass(object_cls, 0, sizeof(BoxedDict), false); dict_cls->giveAttr("__name__", boxStrConstant("dict")); // dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, NULL, 1))); // dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, NULL, 2))); // dict_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)dictNew, NULL, 1))); // dict_cls->giveAttr("__init__", new BoxedFunction(boxRTFunction((void*)dictInit, NULL, 1))); dict_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)dictRepr, STR, 1))); dict_cls->giveAttr("__str__", dict_cls->getattr("__repr__")); dict_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)dictIterKeys, typeFromClass(dict_iterator_cls), 1))); dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, LIST, 1))); dict_cls->giveAttr("iteritems", new BoxedFunction(boxRTFunction((void*)dictIterItems, typeFromClass(dict_iterator_cls), 1))); dict_cls->giveAttr("values", new BoxedFunction(boxRTFunction((void*)dictValues, LIST, 1))); dict_cls->giveAttr("itervalues", new BoxedFunction(boxRTFunction((void*)dictIterValues, typeFromClass(dict_iterator_cls), 1))); dict_cls->giveAttr("keys", new BoxedFunction(boxRTFunction((void*)dictKeys, LIST, 1))); dict_cls->giveAttr("iterkeys", dict_cls->getattr("__iter__")); dict_cls->giveAttr("pop", new BoxedFunction(boxRTFunction((void*)dictPop, UNKNOWN, 3, 1, false, false), { NULL })); dict_cls->giveAttr("get", new BoxedFunction(boxRTFunction((void*)dictGet, UNKNOWN, 3, 1, false, false), { None })); dict_cls->giveAttr("setdefault", new BoxedFunction(boxRTFunction((void*)dictSetdefault, UNKNOWN, 3, 1, false, false), { None })); dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, UNKNOWN, 2))); dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NONE, 3))); dict_cls->freeze(); gc::registerStaticRootObj(dict_iterator_cls); dict_iterator_cls->giveAttr("__name__", boxStrConstant("dictiterator")); CLFunction* hasnext = boxRTFunction((void*)dictIterHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)dictIterHasnext, BOXED_BOOL); dict_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); dict_iterator_cls->giveAttr( "__iter__", new BoxedFunction(boxRTFunction((void*)dictIterIter, typeFromClass(dict_iterator_cls), 1))); dict_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)dictIterNext, UNKNOWN, 1))); dict_iterator_cls->freeze(); }
void setupGenerator() { generator_cls = BoxedHeapClass::create(type_cls, object_cls, &BoxedGenerator::gcHandler, 0, offsetof(BoxedGenerator, weakreflist), sizeof(BoxedGenerator), false, "generator"); generator_cls->tp_dealloc = generatorDestructor; generator_cls->has_safe_tp_dealloc = true; generator_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)generatorIter, typeFromClass(generator_cls), 1))); generator_cls->giveAttr("close", new BoxedFunction(boxRTFunction((void*)generatorClose, UNKNOWN, 1))); auto generator_next = boxRTFunction((void*)generatorNext<CXX>, UNKNOWN, 1, ParamNames::empty(), CXX); addRTFunction(generator_next, (void*)generatorNext<CAPI>, UNKNOWN, CAPI); generator_cls->giveAttr("next", new BoxedFunction(generator_next)); CLFunction* hasnext = boxRTFunction((void*)generatorHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)generatorHasnext, BOXED_BOOL); generator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); generator_cls->giveAttr("send", new BoxedFunction(boxRTFunction((void*)generatorSend<CXX>, UNKNOWN, 2))); auto gthrow = new BoxedFunction(boxRTFunction((void*)generatorThrow, UNKNOWN, 4, 2, false, false), { NULL, NULL }); generator_cls->giveAttr("throw", gthrow); generator_cls->giveAttr("__name__", new (pyston_getset_cls) BoxedGetsetDescriptor(generatorName, NULL, NULL)); generator_cls->freeze(); generator_cls->tp_iter = PyObject_SelfIter; }
void setupTuple() { tuple_iterator_cls = BoxedHeapClass::create(type_cls, object_cls, &tupleIteratorGCHandler, 0, 0, sizeof(BoxedTupleIterator), false, "tuple"); tuple_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)tupleNew, UNKNOWN, 1, 0, true, true))); CLFunction* getitem = createRTFunction(2, 0, 0, 0); addRTFunction(getitem, (void*)tupleGetitemInt, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, BOXED_INT }); addRTFunction(getitem, (void*)tupleGetitemSlice, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, SLICE }); addRTFunction(getitem, (void*)tupleGetitem, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, UNKNOWN }); tuple_cls->giveAttr("__getitem__", new BoxedFunction(getitem)); tuple_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)tupleContains, BOXED_BOOL, 2))); tuple_cls->giveAttr("index", new BoxedFunction(boxRTFunction((void*)tupleIndex, BOXED_INT, 2))); tuple_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)tupleIter, typeFromClass(tuple_iterator_cls), 1))); tuple_cls->tp_richcompare = tuplerichcompare; tuple_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)tupleNonzero, BOXED_BOOL, 1))); tuple_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)tupleLen, BOXED_INT, 1))); tuple_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)tupleRepr, STR, 1))); tuple_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)tupleAdd, BOXED_TUPLE, 2))); tuple_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)tupleMul, BOXED_TUPLE, 2))); tuple_cls->giveAttr("__rmul__", new BoxedFunction(boxRTFunction((void*)tupleMul, BOXED_TUPLE, 2))); tuple_cls->tp_hash = (hashfunc)tuple_hash; add_operators(tuple_cls); tuple_cls->freeze(); CLFunction* hasnext = boxRTFunction((void*)tupleiterHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)tupleiterHasnext, BOXED_BOOL); tuple_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); tuple_iterator_cls->giveAttr( "__iter__", new BoxedFunction(boxRTFunction((void*)tupleIterIter, typeFromClass(tuple_iterator_cls), 1))); tuple_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)tupleiterNext, UNKNOWN, 1))); tuple_iterator_cls->freeze(); tuple_iterator_cls->tpp_hasnext = tupleiterHasnextUnboxed; }
void setupGenerator() { generator_cls = BoxedHeapClass::create(type_cls, object_cls, &generatorGCHandler, 0, offsetof(BoxedGenerator, weakreflist), sizeof(BoxedGenerator), false, "generator"); generator_cls->simple_destructor = generatorDestructor; generator_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)generatorIter, typeFromClass(generator_cls), 1))); generator_cls->giveAttr("close", new BoxedFunction(boxRTFunction((void*)generatorClose, UNKNOWN, 1))); generator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)generatorNext, UNKNOWN, 1))); CLFunction* hasnext = boxRTFunction((void*)generatorHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)generatorHasnext, BOXED_BOOL); generator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); generator_cls->giveAttr("send", new BoxedFunction(boxRTFunction((void*)generatorSend, UNKNOWN, 2))); auto gthrow = new BoxedFunction(boxRTFunction((void*)generatorThrow, UNKNOWN, 4, 2, false, false), { NULL, NULL }); generator_cls->giveAttr("throw", gthrow); generator_cls->giveAttr("__name__", new (pyston_getset_cls) BoxedGetsetDescriptor(generatorName, NULL, NULL)); generator_cls->freeze(); }
void setupRuntime() { HiddenClass::getRoot(); object_cls = new BoxedClass(NULL, 0, sizeof(Box), false); type_cls = new BoxedClass(object_cls, offsetof(BoxedClass, attrs), sizeof(BoxedClass), false); type_cls->cls = type_cls; object_cls->cls = type_cls; none_cls = new BoxedClass(object_cls, 0, sizeof(Box), false); None = new Box(&none_flavor, none_cls); str_cls = new BoxedClass(object_cls, 0, sizeof(BoxedString), false); // It wasn't safe to add __base__ attributes until object+type+str are set up, so do that now: type_cls->giveAttr("__base__", object_cls); str_cls->giveAttr("__base__", object_cls); none_cls->giveAttr("__base__", object_cls); object_cls->giveAttr("__base__", None); tuple_cls = new BoxedClass(object_cls, 0, sizeof(BoxedTuple), false); EmptyTuple = new BoxedTuple({}); gc::registerStaticRootObj(EmptyTuple); module_cls = new BoxedClass(object_cls, offsetof(BoxedModule, attrs), sizeof(BoxedModule), false); // TODO it'd be nice to be able to do these in the respective setupType methods, // but those setup methods probably want access to these objects. // We could have a multi-stage setup process, but that seems overkill for now. bool_cls = new BoxedClass(object_cls, 0, sizeof(BoxedBool), false); int_cls = new BoxedClass(object_cls, 0, sizeof(BoxedInt), false); float_cls = new BoxedClass(object_cls, 0, sizeof(BoxedFloat), false); function_cls = new BoxedClass(object_cls, offsetof(BoxedFunction, attrs), sizeof(BoxedFunction), false); instancemethod_cls = new BoxedClass(object_cls, 0, sizeof(BoxedInstanceMethod), false); list_cls = new BoxedClass(object_cls, 0, sizeof(BoxedList), false); slice_cls = new BoxedClass(object_cls, 0, sizeof(BoxedSlice), false); dict_cls = new BoxedClass(object_cls, 0, sizeof(BoxedDict), false); file_cls = new BoxedClass(object_cls, 0, sizeof(BoxedFile), false); set_cls = new BoxedClass(object_cls, 0, sizeof(BoxedSet), false); member_cls = new BoxedClass(object_cls, 0, sizeof(BoxedMemberDescriptor), false); STR = typeFromClass(str_cls); BOXED_INT = typeFromClass(int_cls); BOXED_FLOAT = typeFromClass(float_cls); BOXED_BOOL = typeFromClass(bool_cls); NONE = typeFromClass(none_cls); LIST = typeFromClass(list_cls); SLICE = typeFromClass(slice_cls); MODULE = typeFromClass(module_cls); DICT = typeFromClass(dict_cls); SET = typeFromClass(set_cls); BOXED_TUPLE = typeFromClass(tuple_cls); object_cls->giveAttr("__name__", boxStrConstant("object")); object_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)objectNew, UNKNOWN, 1, 0, true, false))); object_cls->freeze(); auto typeCallObj = boxRTFunction((void*)typeCall, UNKNOWN, 1, 0, true, false); typeCallObj->internal_callable = &typeCallInternal; type_cls->giveAttr("__call__", new BoxedFunction(typeCallObj)); type_cls->giveAttr("__name__", boxStrConstant("type")); type_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)typeNew, UNKNOWN, 2))); type_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)typeRepr, STR, 1))); type_cls->giveAttr("__str__", type_cls->getattr("__repr__")); type_cls->freeze(); none_cls->giveAttr("__name__", boxStrConstant("NoneType")); none_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)noneRepr, STR, 1))); none_cls->giveAttr("__str__", none_cls->getattr("__repr__")); none_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)noneHash, UNKNOWN, 1))); none_cls->freeze(); module_cls->giveAttr("__name__", boxStrConstant("module")); module_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)moduleRepr, STR, 1))); module_cls->giveAttr("__str__", module_cls->getattr("__repr__")); module_cls->freeze(); member_cls->giveAttr("__name__", boxStrConstant("member")); member_cls->freeze(); setupBool(); setupInt(); setupFloat(); setupStr(); setupList(); setupDict(); setupSet(); setupTuple(); setupFile(); function_cls->giveAttr("__name__", boxStrConstant("function")); function_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)functionRepr, STR, 1))); function_cls->giveAttr("__str__", function_cls->getattr("__repr__")); function_cls->freeze(); instancemethod_cls->giveAttr("__name__", boxStrConstant("instancemethod")); instancemethod_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)instancemethodRepr, STR, 1))); instancemethod_cls->freeze(); slice_cls->giveAttr("__name__", boxStrConstant("slice")); slice_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)sliceNew, UNKNOWN, 4, 2, false, false), { NULL, None })); slice_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)sliceRepr, STR, 1))); slice_cls->giveAttr("__str__", slice_cls->getattr("__repr__")); slice_cls->giveAttr("start", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, SLICE_START_OFFSET)); slice_cls->giveAttr("stop", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, SLICE_STOP_OFFSET)); slice_cls->giveAttr("step", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, SLICE_STEP_OFFSET)); slice_cls->freeze(); // sys is the first module that needs to be set up, due to modules // being tracked in sys.modules: setupSys(); setupBuiltins(); setupMath(); setupTime(); setupThread(); setupCAPI(); TRACK_ALLOCATIONS = true; }
void setupList() { list_iterator_cls = new BoxedHeapClass(object_cls, &listIteratorGCHandler, 0, sizeof(BoxedList), false); list_cls->giveAttr("__name__", boxStrConstant("list")); list_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)listLen, BOXED_INT, 1))); CLFunction* getitem = createRTFunction(2, 0, 0, 0); addRTFunction(getitem, (void*)listGetitemInt, UNKNOWN, std::vector<ConcreteCompilerType*>{ LIST, BOXED_INT }); addRTFunction(getitem, (void*)listGetitemSlice, LIST, std::vector<ConcreteCompilerType*>{ LIST, SLICE }); addRTFunction(getitem, (void*)listGetitem, UNKNOWN, std::vector<ConcreteCompilerType*>{ LIST, UNKNOWN }); list_cls->giveAttr("__getitem__", new BoxedFunction(getitem)); list_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)listIter, typeFromClass(list_iterator_cls), 1))); list_cls->giveAttr("__eq__", new BoxedFunction(boxRTFunction((void*)listEq, UNKNOWN, 2))); list_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)listNe, UNKNOWN, 2))); list_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)listRepr, STR, 1))); list_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)listNonzero, BOXED_BOOL, 1))); list_cls->giveAttr("pop", new BoxedFunction(boxRTFunction((void*)listPop, UNKNOWN, 2, 1, false, false), { None })); list_cls->giveAttr("append", new BoxedFunction(boxRTFunction((void*)listAppend, NONE, 2))); list_cls->giveAttr("extend", new BoxedFunction(boxRTFunction((void*)listIAdd, NONE, 2))); CLFunction* setitem = createRTFunction(3, 0, false, false); addRTFunction(setitem, (void*)listSetitemInt, NONE, std::vector<ConcreteCompilerType*>{ LIST, BOXED_INT, UNKNOWN }); addRTFunction(setitem, (void*)listSetitemSlice, NONE, std::vector<ConcreteCompilerType*>{ LIST, SLICE, UNKNOWN }); addRTFunction(setitem, (void*)listSetitem, NONE, std::vector<ConcreteCompilerType*>{ LIST, UNKNOWN, UNKNOWN }); list_cls->giveAttr("__setitem__", new BoxedFunction(setitem)); CLFunction* delitem = createRTFunction(2, 0, false, false); addRTFunction(delitem, (void*)listDelitemInt, NONE, std::vector<ConcreteCompilerType*>{ LIST, BOXED_INT }); addRTFunction(delitem, (void*)listDelitemSlice, NONE, std::vector<ConcreteCompilerType*>{ LIST, SLICE }); addRTFunction(delitem, (void*)listDelitem, NONE, std::vector<ConcreteCompilerType*>{ LIST, UNKNOWN }); list_cls->giveAttr("__delitem__", new BoxedFunction(delitem)); list_cls->giveAttr("insert", new BoxedFunction(boxRTFunction((void*)listInsert, NONE, 3))); list_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)listMul, LIST, 2))); list_cls->giveAttr("__rmul__", new BoxedFunction(boxRTFunction((void*)listMul, LIST, 2))); list_cls->giveAttr("__iadd__", new BoxedFunction(boxRTFunction((void*)listIAdd, UNKNOWN, 2))); list_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)listAdd, UNKNOWN, 2))); list_cls->giveAttr("sort", new BoxedFunction(boxRTFunction((void*)listSort1, NONE, 1))); list_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)listContains, BOXED_BOOL, 2))); list_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)listNew, UNKNOWN, 2, 1, false, false), { None })); list_cls->giveAttr("count", new BoxedFunction(boxRTFunction((void*)listCount, BOXED_INT, 2))); list_cls->giveAttr("index", new BoxedFunction(boxRTFunction((void*)listIndex, BOXED_INT, 2))); list_cls->giveAttr("remove", new BoxedFunction(boxRTFunction((void*)listRemove, NONE, 2))); list_cls->giveAttr("reverse", new BoxedFunction(boxRTFunction((void*)listReverse, NONE, 1))); list_cls->freeze(); list_iterator_cls->giveAttr("__name__", boxStrConstant("listiterator")); CLFunction* hasnext = boxRTFunction((void*)listiterHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)listiterHasnext, BOXED_BOOL); list_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); list_iterator_cls->giveAttr( "__iter__", new BoxedFunction(boxRTFunction((void*)listIterIter, typeFromClass(list_iterator_cls), 1))); list_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)listiterNext, UNKNOWN, 1))); list_iterator_cls->freeze(); }
void setupFile() { file_cls->giveAttr("__name__", boxStrConstant("file")); file_cls->giveAttr("read", new BoxedFunction(boxRTFunction((void*)fileRead, STR, 2, 1, false, false), { boxInt(-1) })); CLFunction* readline = boxRTFunction((void*)fileReadline1, STR, 1); file_cls->giveAttr("readline", new BoxedFunction(readline)); file_cls->giveAttr("write", new BoxedFunction(boxRTFunction((void*)fileWrite, NONE, 2))); file_cls->giveAttr("close", new BoxedFunction(boxRTFunction((void*)fileClose, NONE, 1))); file_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)fileRepr, STR, 1))); file_cls->giveAttr("__str__", file_cls->getattr("__repr__")); file_cls->giveAttr("__enter__", new BoxedFunction(boxRTFunction((void*)fileEnter, typeFromClass(file_cls), 1))); file_cls->giveAttr("__exit__", new BoxedFunction(boxRTFunction((void*)fileExit, UNKNOWN, 4))); file_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)fileNew, UNKNOWN, 3, 1, false, false), { boxStrConstant("r") })); file_cls->freeze(); }
void setupRuntime() { HiddenClass::getRoot(); type_cls = new BoxedClass(true, NULL); type_cls->cls = type_cls; none_cls = new BoxedClass(false, NULL); None = new Box(&none_flavor, none_cls); gc::registerStaticRootObj(None); module_cls = new BoxedClass(true, NULL); bool_cls = new BoxedClass(false, NULL); int_cls = new BoxedClass(false, NULL); float_cls = new BoxedClass(false, NULL); str_cls = new BoxedClass(false, (BoxedClass::Dtor)str_dtor); function_cls = new BoxedClass(true, NULL); instancemethod_cls = new BoxedClass(false, (BoxedClass::Dtor)instancemethod_dtor); list_cls = new BoxedClass(false, (BoxedClass::Dtor)list_dtor); slice_cls = new BoxedClass(true, NULL); dict_cls = new BoxedClass(false, (BoxedClass::Dtor)dict_dtor); tuple_cls = new BoxedClass(false, (BoxedClass::Dtor)tuple_dtor); file_cls = new BoxedClass(false, (BoxedClass::Dtor)file_dtor); STR = typeFromClass(str_cls); BOXED_INT = typeFromClass(int_cls); BOXED_FLOAT = typeFromClass(float_cls); BOXED_BOOL = typeFromClass(bool_cls); NONE = typeFromClass(none_cls); LIST = typeFromClass(list_cls); SLICE = typeFromClass(slice_cls); MODULE = typeFromClass(module_cls); DICT = typeFromClass(dict_cls); BOXED_TUPLE = typeFromClass(tuple_cls); type_cls->giveAttr("__name__", boxStrConstant("type")); type_cls->giveAttr("__call__", new BoxedFunction(boxRTFunction((void*)typeCall, NULL, 1, true))); type_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)typeNew, NULL, 2, true))); type_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)typeRepr, NULL, 1, true))); type_cls->setattr("__str__", type_cls->peekattr("__repr__"), NULL, NULL); type_cls->freeze(); none_cls->giveAttr("__name__", boxStrConstant("NoneType")); none_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)noneRepr, NULL, 1, false))); none_cls->setattr("__str__", none_cls->peekattr("__repr__"), NULL, NULL); none_cls->freeze(); module_cls->giveAttr("__name__", boxStrConstant("module")); module_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)moduleRepr, NULL, 1, false))); module_cls->setattr("__str__", module_cls->peekattr("__repr__"), NULL, NULL); module_cls->freeze(); setupBool(); setupInt(); setupFloat(); setupStr(); setupList(); setupDict(); setupTuple(); setupFile(); function_cls->giveAttr("__name__", boxStrConstant("function")); function_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)functionRepr, NULL, 1, false))); function_cls->setattr("__str__", function_cls->peekattr("__repr__"), NULL, NULL); function_cls->freeze(); instancemethod_cls->giveAttr("__name__", boxStrConstant("instancemethod")); instancemethod_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)instancemethodRepr, NULL, 1, true))); instancemethod_cls->freeze(); slice_cls->giveAttr("__name__", boxStrConstant("slice")); slice_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)sliceRepr, NULL, 1, true))); slice_cls->setattr("__str__", slice_cls->peekattr("__repr__"), NULL, NULL); slice_cls->freeze(); setupMath(); gc::registerStaticRootObj(math_module); setupTime(); gc::registerStaticRootObj(time_module); setupBuiltins(); gc::registerStaticRootObj(builtins_module); setupCAPI(); TRACK_ALLOCATIONS = true; }
CompiledFunction* resolveCLFunc(CLFunction *f, int64_t nargs, Box* arg1, Box* arg2, Box* arg3, Box** args) { static StatCounter slowpath_resolveclfunc("slowpath_resolveclfunc"); slowpath_resolveclfunc.log(); FunctionList &versions = f->versions; for (int i = 0; i < versions.size(); i++) { CompiledFunction *cf = versions[i]; FunctionSignature* sig = cf->sig; if (sig->rtn_type->llvmType() != g.llvm_value_type_ptr) continue; if ((!sig->is_vararg && sig->arg_types.size() != nargs) || (sig->is_vararg && nargs < sig->arg_types.size())) continue; int nsig_args = sig->arg_types.size(); if (nsig_args >= 1) { if (sig->arg_types[0]->isFitBy(arg1->cls)) { // pass } else { continue; } } if (nsig_args >= 2) { if (sig->arg_types[1]->isFitBy(arg2->cls)) { // pass } else { continue; } } if (nsig_args >= 3) { if (sig->arg_types[2]->isFitBy(arg3->cls)) { // pass } else { continue; } } bool bad = false; for (int j = 3; j < nsig_args; j++) { if (sig->arg_types[j]->isFitBy(args[j-3]->cls)) { // pass } else { bad = true; break; } } if (bad) continue; assert(cf); assert(!cf->entry_descriptor); assert(cf->is_interpreted == (cf->code == NULL)); return cf; } if (f->source == NULL) { printf("Error: couldn't find suitable function version and no source to recompile!\n"); printf("%ld args:", nargs); for (int i = 0; i < nargs; i++) { Box* firstargs[] = {arg1, arg2, arg3}; printf(" %s", getTypeName(firstargs[i])->c_str()); if (i == 3) { printf(" [and more]"); break; } } printf("\n"); for (int j = 0; j < f->versions.size(); j++) { std::string func_name = g.func_addr_registry.getFuncNameAtAddress(f->versions[j]->code, true); printf("Version %d, %s:", j, func_name.c_str()); FunctionSignature *sig = f->versions[j]->sig; for (int i = 0; i < sig->arg_types.size(); i++) { printf(" %s", sig->arg_types[i]->debugName().c_str()); } if (sig->is_vararg) printf(" *vararg"); printf("\n"); //printf(" @%p %s\n", f->versions[j]->code, func_name.c_str()); } abort(); } assert(f->source->getArgsAST()->vararg.size() == 0); bool is_vararg = false; std::vector<ConcreteCompilerType*> arg_types; if (nargs >= 1) { arg_types.push_back(typeFromClass(arg1->cls)); } if (nargs >= 2) { arg_types.push_back(typeFromClass(arg2->cls)); } if (nargs >= 3) { arg_types.push_back(typeFromClass(arg3->cls)); } for (int j = 3; j < nargs; j++) { arg_types.push_back(typeFromClass(args[j-3]->cls)); } FunctionSignature *sig = new FunctionSignature(UNKNOWN, arg_types, is_vararg); EffortLevel::EffortLevel new_effort = initialEffort(); CompiledFunction *cf = _doCompile(f, sig, new_effort, NULL); // this pushes the new CompiledVersion to the back of the version list assert(cf->is_interpreted == (cf->code == NULL)); return cf; }
void setupTuple() { static PySequenceMethods tuple_as_sequence; tuple_cls->tp_as_sequence = &tuple_as_sequence; static PyMappingMethods tuple_as_mapping; tuple_cls->tp_as_mapping = &tuple_as_mapping; tuple_iterator_cls = BoxedClass::create(type_cls, object_cls, &BoxedTupleIterator::gcHandler, 0, 0, sizeof(BoxedTupleIterator), false, "tuple"); tuple_cls->giveAttr("__new__", new BoxedFunction(FunctionMetadata::create((void*)tupleNew, UNKNOWN, 1, true, true))); FunctionMetadata* getitem = new FunctionMetadata(2, 0, 0); getitem->addVersion((void*)tupleGetitemInt, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, BOXED_INT }); getitem->addVersion((void*)tupleGetitemSlice, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, SLICE }); getitem->addVersion((void*)tupleGetitem<CXX>, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, UNKNOWN }, CXX); getitem->addVersion((void*)tupleGetitem<CAPI>, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, UNKNOWN }, CAPI); tuple_cls->giveAttr("__getitem__", new BoxedFunction(getitem)); tuple_cls->giveAttr("__contains__", new BoxedFunction(FunctionMetadata::create((void*)tupleContains, BOXED_BOOL, 2))); tuple_cls->giveAttr("index", new BoxedFunction(FunctionMetadata::create((void*)tupleIndex, BOXED_INT, 4, false, false), { boxInt(0), boxInt(std::numeric_limits<Py_ssize_t>::max()) })); tuple_cls->giveAttr("count", new BoxedFunction(FunctionMetadata::create((void*)tupleCount, BOXED_INT, 2))); tuple_cls->giveAttr("__iter__", new BoxedFunction(FunctionMetadata::create((void*)tupleIter, typeFromClass(tuple_iterator_cls), 1))); tuple_cls->tp_richcompare = tuplerichcompare; tuple_cls->giveAttr("__nonzero__", new BoxedFunction(FunctionMetadata::create((void*)tupleNonzero, BOXED_BOOL, 1))); tuple_cls->giveAttr("__len__", new BoxedFunction(FunctionMetadata::create((void*)tupleLen, BOXED_INT, 1))); tuple_cls->giveAttr("__repr__", new BoxedFunction(FunctionMetadata::create((void*)tupleRepr, STR, 1))); tuple_cls->giveAttr("__add__", new BoxedFunction(FunctionMetadata::create((void*)tupleAdd, BOXED_TUPLE, 2))); tuple_cls->giveAttr("__mul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, BOXED_TUPLE, 2))); tuple_cls->giveAttr("__rmul__", new BoxedFunction(FunctionMetadata::create((void*)tupleMul, BOXED_TUPLE, 2))); tuple_cls->giveAttr("__getnewargs__", new BoxedFunction(FunctionMetadata::create((void*)tuple_getnewargs, UNKNOWN, 1, ParamNames::empty(), CAPI))); tuple_cls->tp_hash = (hashfunc)tuple_hash; tuple_cls->tp_as_sequence->sq_slice = (ssizessizeargfunc)&tupleslice; add_operators(tuple_cls); tuple_cls->freeze(); tuple_cls->tp_as_sequence->sq_item = (ssizeargfunc)tupleitem; tuple_cls->tp_as_sequence->sq_length = (lenfunc)tuplelength; tuple_cls->tp_as_sequence->sq_concat = (binaryfunc)tupleconcat; tuple_cls->tp_as_sequence->sq_repeat = (ssizeargfunc)tuplerepeat; tuple_cls->tp_iter = tupleIter; FunctionMetadata* hasnext = FunctionMetadata::create((void*)tupleiterHasnextUnboxed, BOOL, 1); hasnext->addVersion((void*)tupleiterHasnext, BOXED_BOOL); tuple_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); tuple_iterator_cls->giveAttr("__iter__", new BoxedFunction(FunctionMetadata::create( (void*)tupleIterIter, typeFromClass(tuple_iterator_cls), 1))); tuple_iterator_cls->giveAttr("next", new BoxedFunction(FunctionMetadata::create((void*)tupleiterNext, UNKNOWN, 1))); tuple_iterator_cls->freeze(); tuple_iterator_cls->tpp_hasnext = tupleiterHasnextUnboxed; tuple_iterator_cls->tp_iternext = tupleiter_next; tuple_iterator_cls->tp_iter = PyObject_SelfIter; }