Beispiel #1
0
    def new(self):
        self.name = self.ll_mod.name
        n = "__main__" if self.hlnode.is_main else self.hlnode.python_name
        self.v.ctx.add(c.Comment('Create module "{}" with __name__ "{}"'.format(self.hlnode.python_name, n)))
        self.v.ctx.add(
            c.Assignment(
                "=", c.ID(self.ll_mod.name), c.FuncCall(c.ID("PyModule_New"), c.ExprList(c.Constant("string", n)))
            )
        )
        self.fail_if_null(self.ll_mod.name)

        # get the modules dict
        mods = PyDictLL(None, self.v)
        mods.declare_tmp(name="_modules")
        self.v.ctx.add(c.Comment("Insert into sys.modules"))
        self.v.ctx.add(c.Assignment("=", c.ID(mods.name), c.FuncCall(c.ID("PyImport_GetModuleDict"), c.ExprList())))
        self.fail_if_null(self.ll_mod.name)
        mods.incref()

        # add ourself to the modules dict
        mods.set_item_string(n, self)

        # clear the ref so we don't free it later
        mods.clear()

        # grab the module dict
        self.v.ctx.add(
            c.Assignment(
                "=", c.ID(self.ll_dict.name), c.FuncCall(c.ID("PyModule_GetDict"), c.ExprList(c.ID(self.ll_mod.name)))
            )
        )
        self.fail_if_null(self.ll_dict.name)

        # set the builtins on the module
        self.set_attr_string("__builtins__", self.v.builtins)

        # set builtin properties
        self.set_initial_string_attribute("__name__", n)
Beispiel #2
0
    def declare(self):
        # create the namespace dict
        self.ll_dict = PyDictLL(self.hlnode, self.v)
        self.ll_dict.declare(is_global=True, quals=["static"], name=self.hlnode.name + "_dict")

        # create the module creation function
        self.c_builder_func = c.FuncDef(
            c.Decl(
                self.c_builder_name,
                c.FuncDecl(c.ParamList(), c.PtrDecl(c.TypeDecl(self.c_builder_name, c.IdentifierType("PyObject")))),
                quals=["static"],
            ),
            c.Compound(),
        )
        self.v.tu.add_fwddecl(self.c_builder_func.decl)
        self.v.tu.add(self.c_builder_func)

        # declare the module
        self.ll_mod = PyObjectLL(self.hlnode, self.v)
        self.ll_mod.declare(is_global=True, quals=["static"])
Beispiel #3
0
	def attach_annotations(self, ret, args, vararg_name, vararg, kwonlyargs, kwarg_name, kwarg):
		if not (ret or args or vararg or kwonlyargs or kwarg):
			return

		self.v.ctx.add(c.Comment("build annotations dict"))
		tmp = PyDictLL(None, self.v)
		tmp.declare_tmp(name=self.hlnode.owner.name + '_annotations')
		tmp.new()

		if ret:
			tmp.set_item_string('return', ret)
		if vararg:
			tmp.set_item_string(vararg_name, vararg)
		if kwarg:
			tmp.set_item_string(kwarg_name, kwarg)
		for name, ann in args:
			if ann:
				tmp.set_item_string(str(name), ann)
		for name, ann in kwonlyargs:
			if ann:
				tmp.set_item_string(str(name), ann)

		self.c_obj.set_attr_string('__annotations__', tmp)
		tmp.decref()
Beispiel #4
0
	def stub_load_args(self, args, defaults, vararg, kwonlyargs, kw_defaults, kwarg):
		self.stub_arg_insts = []

		# get args ref
		args_tuple = self.stub_args_tuple
		kwargs_dict = self.stub_kwargs_dict

		# get copy of kwargs -- clear items out of it as we load them, or skip entirely
		if kwarg:
			if_have_kwargs = self.v.ctx.add(c.If(c.ID(kwargs_dict.name), c.Compound(), c.Compound()))
			with self.v.new_context(if_have_kwargs.iftrue):
				kwargs_inst = kwargs_dict.copy()
			with self.v.new_context(if_have_kwargs.iffalse):
				kwargs_dict.new()
				kwargs_inst = kwargs_dict
		else:
			kwargs_inst = None

		# load positional and normal keyword args
		if args:
			c_args_size = CIntegerLL(None, self.v)
			c_args_size.declare_tmp(name='_args_size')
			args_tuple.get_size_unchecked(c_args_size)

			arg_insts = [None] * len(args)
			for i, arg in enumerate(args):
				# Note: different scope than the actual args are declared in.. need to stub them out here
				#TODO: make this type pull from the arg.arg.hl.get_type() through lookup... maybe create dup_ll_type or something
				arg_insts[i] = PyObjectLL(arg.arg.hl, self.v)
				arg_insts[i].declare()

				# query if in positional args
				self.v.ctx.add(c.Comment("Grab arg {}".format(str(arg.arg))))
				query_inst = self.v.ctx.add(c.If(c.BinaryOp('>', c.ID(c_args_size.name), c.Constant('integer', i)), c.Compound(), c.Compound()))

				## get the positional arg on the true side
				with self.v.new_context(query_inst.iftrue):
					args_tuple.get_unchecked(i, arg_insts[i])

				## get the keyword arg on the false side
				with self.v.new_context(query_inst.iffalse):
					have_kwarg = self.v.ctx.add(c.If(c.ID('kwargs'), c.Compound(), None))

					### if we took kwargs, then get it directly
					with self.v.new_context(have_kwarg.iftrue):
						kwargs_dict.get_item_string_nofail(str(arg.arg), arg_insts[i])

					### if no kwargs passed or the item was not in the kwargs, load the default from defaults
					query_default_inst = self.v.ctx.add(c.If(c.UnaryOp('!', c.ID(arg_insts[i].name)), c.Compound(), c.Compound()))
					with self.v.new_context(query_default_inst.iftrue):
						kwstartoffset = len(args) - len(defaults)
						if i >= kwstartoffset:
							# try loading from defaults
							default_offset = i - kwstartoffset
							tmp = PyTupleLL(None, self.v)
							tmp.declare_tmp()
							self.c_obj.get_attr_string('__defaults__', tmp)
							#self.v.ctx.add(c.Assignment('=', c.ID(tmp.name),
							#		c.FuncCall(c.ID('PyObject_GetAttrString'), c.ExprList(c.ID(self.c_obj.name), c.Constant('string', '__defaults__')))))
							#
							tmp.get_unchecked(default_offset, arg_insts[i])
							#self.v.ctx.add(c.Assignment('=', c.ID(arg_insts[i].name),
							#		c.FuncCall(c.ID('PyTuple_GetItem'), c.ExprList(c.ID(tmp.name), c.Constant('integer', default_offset)))))
							arg_insts[i].incref()
							tmp.decref()
							#self.v.ctx.add(c.FuncCall(c.ID('Py_INCREF'), c.ID(arg_insts[i].name)))
						else:
							# emit an error for an unpassed arg
							with self.v.new_context(query_default_inst.iftrue):
								self.fail('PyExc_TypeError', 'Missing arg {}'.format(str(arg)))

					### if we did get the item out of the kwargs, delete it from the inst copy so it's not duped in the args we pass 
					with self.v.new_context(query_default_inst.iffalse):
						if kwargs_inst:
							kwargs_inst.del_item_string(str(arg.arg))
			self.stub_arg_insts.extend(arg_insts)

		# add unused args to varargs and pass if in taken args or error if not
		if vararg:
			self.v.ctx.add(c.Comment('load varargs'))
			vararg_inst = args_tuple.get_slice(len(args), args_tuple.get_length())
			self.stub_arg_insts.append(vararg_inst)
		else:
			len_inst = args_tuple.get_length()
			ifstmt = self.v.ctx.add(c.If(c.BinaryOp('>', c.ID(len_inst.name), c.Constant('integer', len(args))), c.Compound(), None))
			with self.v.new_context(ifstmt.iftrue):
				self.fail_formatted('PyExc_TypeError', "{}() takes exactly {} positional arguments (%d given)".format(self.hlnode.owner.name, len(args)), len_inst)

		# load all keyword only args
		if kwonlyargs:
			kwarg_insts = [None] * len(kwonlyargs)
			for i, arg in enumerate(kwonlyargs):
				kwarg_insts[i] = PyObjectLL(arg.arg.hl, self.v)
				kwarg_insts[i].declare()

			# ensure we have kwargs at all
			have_kwarg = self.v.ctx.add(c.If(c.ID('kwargs'), c.Compound(), c.Compound()))

			## in have_kwarg.iftrue, load all kwargs from the kwargs dict
			with self.v.new_context(have_kwarg.iftrue):
				for i, arg in enumerate(kwonlyargs):
					#FIXME: we can make this significantly more efficient with a bit of work
					kwargs_dict.get_item_string_nofail(str(arg.arg), kwarg_insts[i])
					need_default = self.v.ctx.add(c.If(c.UnaryOp('!', c.ID(kwarg_insts[i].name)), c.Compound(), None))

					### not found in kwdict, means we need to load from default
					with self.v.new_context(need_default.iftrue):
						kwdefaults0 = PyDictLL(None, self.v)
						kwdefaults0.declare_tmp(name='_kwdefaults')
						self.c_obj.get_attr_string('__kwdefaults__', kwdefaults0)
						kwdefaults0.get_item_string(str(arg.arg), kwarg_insts[i])
						kwdefaults0.decref()
					### found in kwdict, means we need to delete from kwdict to avoid passing duplicate arg in kwargs
					if kwargs_inst:
						need_default.iffalse = c.Compound()
						with self.v.new_context(need_default.iffalse):
							kwargs_inst.del_item_string(str(arg.arg))

			## if have_kwarg.iffalse, need to load from the kwdefaults dict
			#TODO: this is identical to the failure case from above
			with self.v.new_context(have_kwarg.iffalse):
				kwdefaults1 = PyDictLL(None, self.v)
				kwdefaults1.declare_tmp(name='_kwdefaults')
				self.c_obj.get_attr_string('__kwdefaults__', kwdefaults1)
				for i, arg in enumerate(kwonlyargs):
					#have_kwarg.iffalse.add(c.Assignment('=', c.ID(kwdefaults1.name),
					#	c.FuncCall(c.ID('PyObject_GetAttrString'), c.ExprList(c.ID(self.c_obj.name), c.Constant('string', '__kwdefaults__')))))
					kwdefaults1.get_item_string(str(arg.arg), kwarg_insts[i])
					#have_kwarg.iffalse.add(c.Assignment('=', c.ID(kwarg_insts[i].name),
					#	c.FuncCall(c.ID('PyDict_GetItemString'), c.ExprList(c.ID(kwdefaults1.name), c.Constant('string', str(arg.arg))))))
					#self.fail_if_null(kwarg_insts[i].name)
				kwdefaults1.decref()

			self.stub_arg_insts.extend(kwarg_insts)

		# pass remainder of kwargs dict in as the kwarg slot
		if kwarg:
			self.stub_arg_insts.append(kwargs_inst)
		'''
Beispiel #5
0
class PyModuleLL(PyObjectLL):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # the ll instance representing the dict of global variables
        self.ll_dict = None

        # the ll name and instance representing the function that builds the module
        self.c_builder_name = self.v.tu.reserve_global_name(self.hlnode.name + "_builder")
        self.c_builder_func = None

    def declare(self):
        # create the namespace dict
        self.ll_dict = PyDictLL(self.hlnode, self.v)
        self.ll_dict.declare(is_global=True, quals=["static"], name=self.hlnode.name + "_dict")

        # create the module creation function
        self.c_builder_func = c.FuncDef(
            c.Decl(
                self.c_builder_name,
                c.FuncDecl(c.ParamList(), c.PtrDecl(c.TypeDecl(self.c_builder_name, c.IdentifierType("PyObject")))),
                quals=["static"],
            ),
            c.Compound(),
        )
        self.v.tu.add_fwddecl(self.c_builder_func.decl)
        self.v.tu.add(self.c_builder_func)

        # declare the module
        self.ll_mod = PyObjectLL(self.hlnode, self.v)
        self.ll_mod.declare(is_global=True, quals=["static"])

    def return_existing(self):
        self.v.ctx.add(c.If(c.ID(self.ll_mod.name), c.Compound(c.Return(c.ID(self.ll_mod.name))), None))

    def new(self):
        self.name = self.ll_mod.name
        n = "__main__" if self.hlnode.is_main else self.hlnode.python_name
        self.v.ctx.add(c.Comment('Create module "{}" with __name__ "{}"'.format(self.hlnode.python_name, n)))
        self.v.ctx.add(
            c.Assignment(
                "=", c.ID(self.ll_mod.name), c.FuncCall(c.ID("PyModule_New"), c.ExprList(c.Constant("string", n)))
            )
        )
        self.fail_if_null(self.ll_mod.name)

        # get the modules dict
        mods = PyDictLL(None, self.v)
        mods.declare_tmp(name="_modules")
        self.v.ctx.add(c.Comment("Insert into sys.modules"))
        self.v.ctx.add(c.Assignment("=", c.ID(mods.name), c.FuncCall(c.ID("PyImport_GetModuleDict"), c.ExprList())))
        self.fail_if_null(self.ll_mod.name)
        mods.incref()

        # add ourself to the modules dict
        mods.set_item_string(n, self)

        # clear the ref so we don't free it later
        mods.clear()

        # grab the module dict
        self.v.ctx.add(
            c.Assignment(
                "=", c.ID(self.ll_dict.name), c.FuncCall(c.ID("PyModule_GetDict"), c.ExprList(c.ID(self.ll_mod.name)))
            )
        )
        self.fail_if_null(self.ll_dict.name)

        # set the builtins on the module
        self.set_attr_string("__builtins__", self.v.builtins)

        # set builtin properties
        self.set_initial_string_attribute("__name__", n)
        # self.ll_module.set_initial_string_attribute(self.context, '__name__', self.hl_module.owner.python_name)

    def set_initial_string_attribute(self, name: str, s: str):
        if s is not None:
            ps = PyStringLL(None, self.v)
            ps.declare_tmp()
            ps.new(s)
        else:
            ps = PyObjectLL(None, self.v)
            ps.declare_tmp()
            ps.assign_none()
        self.set_attr_string(name, ps)
        ps.decref()

    def intro(self):
        self.v.scope.ctx.add_variable(
            c.Decl(
                "__return_value__",
                c.PtrDecl(c.TypeDecl("__return_value__", c.IdentifierType("PyObject"))),
                init=c.ID("NULL"),
            ),
            False,
        )

    def outro(self):
        self.v.ctx.add(c.Assignment("=", c.ID("__return_value__"), c.ID(self.ll_mod.name)))
        self.v.ctx.add(c.Label("end"))
        for name in reversed(self.v.scope.ctx.cleanup):
            self.v.ctx.add(c.FuncCall(c.ID("Py_XDECREF"), c.ExprList(c.ID(name))))
        self.v.ctx.add(c.Return(c.ID("__return_value__")))

    @contextmanager
    def maybe_recursive_call(self):
        yield

    def del_attr_string(self, name: str):
        self.ll_dict.del_item_string(name)

    def set_attr_string(self, name: str, val: LLType):
        self.ll_dict.set_item_string(name, val)
        # FIXME: do we really need both dict and attr?  don't these go to the same place?
        # super().set_attr_string(name, val)

    def get_attr_string(self, attrname: str, out: LLType):
        if str(attrname) in PY_BUILTINS:
            mode = "likely"
        else:
            mode = "unlikely"

            # access globals first, fall back to builtins -- remember to ref the global if we get it, since dict get item borrows
            # out.xdecref()
        self.ll_dict.get_item_string_nofail(attrname, out)
        frombuiltins = self.v.ctx.add(
            c.If(c.FuncCall(c.ID(mode), c.ExprList(c.UnaryOp("!", c.ID(out.name)))), c.Compound(), None)
        )
        with self.v.new_context(frombuiltins.iftrue):
            self.v.builtins.get_attr_string_with_exception(
                attrname, out, "PyExc_NameError", "name '{}' is not defined".format(attrname)
            )