本文整理汇总了Python中pykit.ir.Builder.alloca方法的典型用法代码示例。如果您正苦于以下问题:Python Builder.alloca方法的具体用法?Python Builder.alloca怎么用?Python Builder.alloca使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pykit.ir.Builder
的用法示例。
在下文中一共展示了Builder.alloca方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import alloca [as 别名]
def run(func, env=None, return_block=None):
"""
Rewrite 'ret' operations into jumps to a return block and assignments
to a return variable.
"""
b = Builder(func)
return_block = return_block or func.new_block("pykit.return")
# Allocate return variable
if not func.type.restype.is_void:
with b.at_front(func.startblock):
return_var = b.alloca(types.Pointer(func.type.restype))
b.store(Undef(func.type.restype), return_var)
else:
return_var = None
# Repace 'ret' instructions with jumps and assignments
for op in func.ops:
if op.opcode == "ret":
b.position_after(op)
if return_var:
b.store(op.args[0], return_var)
b.jump(return_block)
op.delete()
with b.at_end(return_block):
if return_var:
result = b.load(return_var)
else:
result = None
b.ret(result)
示例2: rewrite_obj_return
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import alloca [as 别名]
def rewrite_obj_return(func, env):
"""
Handle returning stack-allocated objects.
"""
if should_skip(env):
return
context = env['flypy.typing.context']
restype = env['flypy.typing.restype']
envs = env['flypy.state.envs']
builder = Builder(func)
stack_alloc = representation.byref(restype)
if stack_alloc:
out = func.add_arg(func.temp("out"), opaque_t)
context[out] = Pointer[restype]
func.type = types.Function(types.Void, func.type.argtypes, False)
for arg in func.args:
arg.type = opaque_t
func.type = types.Function(func.type.restype, (opaque_t,) * len(func.args),
False)
is_generator = env['flypy.state.generator']
for op in func.ops:
if (op.opcode == 'ret' and op.args[0] is not None and
stack_alloc and not is_generator):
# ret val =>
# store (load val) out ; ret void
[val] = op.args
builder.position_before(op)
newval = builder.load(val)
builder.store(newval, out)
op.set_args([None])
# Update context
context[newval] = StackVar[context[val]]
elif op.opcode == 'call' and op.type != types.Void:
# result = call(f, ...) =>
# alloca result ; call(f, ..., &result)
ty = context[op]
if conversion.byref(ty):
f, args = op.args
if not is_flypy_cc(f) or should_skip(envs[f]):
continue
builder.position_before(op)
retval = builder.alloca(opaque_t)
builder.position_after(op)
op.replace_uses(retval)
newargs = args + [retval]
op.set_args([f, newargs])
# Update context
context[retval] = context[op]
context[op] = void
示例3: TestBuilder
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import alloca [as 别名]
class TestBuilder(unittest.TestCase):
def setUp(self):
self.f = Function("testfunc", ['a'],
types.Function(types.Float32, [types.Int32]))
self.b = Builder(self.f)
self.b.position_at_end(self.f.add_block('entry'))
self.a = self.f.get_arg('a')
def test_basic_builder(self):
v = self.b.alloca(types.Pointer(types.Float32), [])
result = self.b.mul(types.Int32, [self.a, self.a], result='r')
c = self.b.convert(types.Float32, [result])
self.b.store(c, v)
val = self.b.load(types.Float32, [v])
self.b.ret(val)
# print(string(self.f))
self.assertEqual(str(self.f).strip(), basic_expected)
def test_splitblock(self):
old, new = self.b.splitblock('newblock')
with self.b.at_front(old):
self.b.add(types.Int32, [self.a, self.a])
with self.b.at_end(new):
self.b.div(types.Int32, [self.a, self.a])
# print(string(self.f))
self.assertEqual(split_expected, string(self.f))
def test_loop_builder(self):
square = self.b.mul(types.Int32, [self.a, self.a])
c = self.b.convert(types.Float32, [square])
self.b.position_after(square)
_, block = self.b.splitblock('start', terminate=True)
self.b.position_at_end(block)
const = partial(Const, type=types.Int32)
cond, body, exit = self.b.gen_loop(const(5), const(10), const(2))
with self.b.at_front(body):
self.b.print_(c)
with self.b.at_end(exit):
self.b.ret(c)
# print(string(self.f))
# verify.verify(self.f)
# self.assertEqual(loop_expected, string(self.f))
# TestBuilder('test_basic_builder').debug()
# TestBuilder('test_splitblock').debug()
# TestBuilder('test_loop_builder').debug()
# unittest.main()
示例4: TestBuilder
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import alloca [as 别名]
class TestBuilder(unittest.TestCase):
def setUp(self):
self.f = Function("testfunc", ['a'],
types.Function(types.Float32, [types.Int32]))
self.b = Builder(self.f)
self.b.position_at_end(self.f.new_block('entry'))
self.a = self.f.get_arg('a')
def test_basic_builder(self):
v = self.b.alloca(types.Pointer(types.Float32), [])
result = self.b.mul(types.Int32, [self.a, self.a], result='r')
c = self.b.convert(types.Float32, [result])
self.b.store(c, v)
val = self.b.load(types.Float32, [v])
self.b.ret(val)
# print(string(self.f))
assert interp.run(self.f, args=[10]) == 100
def test_splitblock(self):
old, new = self.b.splitblock('newblock')
with self.b.at_front(old):
self.b.add(types.Int32, [self.a, self.a])
with self.b.at_end(new):
self.b.div(types.Int32, [self.a, self.a])
self.assertEqual(opcodes(self.f), ['add', 'div'])
def test_loop_builder(self):
square = self.b.mul(types.Int32, [self.a, self.a])
c = self.b.convert(types.Float32, [square])
self.b.position_after(square)
_, block = self.b.splitblock('start', terminate=True)
self.b.position_at_end(block)
const = partial(Const, type=types.Int32)
cond, body, exit = self.b.gen_loop(const(5), const(10), const(2))
with self.b.at_front(body):
self.b.print(c)
with self.b.at_end(exit):
self.b.ret(c)
self.assertEqual(interp.run(self.f, args=[10]), 100.0)
示例5: generate_copies
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import alloca [as 别名]
def generate_copies(func, phis):
"""
Emit stores to stack variables in predecessor blocks.
"""
builder = Builder(func)
vars = {}
loads = {}
# Allocate a stack variable for each phi
builder.position_at_beginning(func.startblock)
for block in phis:
for phi in phis[block]:
vars[phi] = builder.alloca(types.Pointer(phi.type))
# Generate loads in blocks containing the phis
for block in phis:
leaders = list(block.leaders)
last_leader = leaders[-1] if leaders else block.head
builder.position_after(last_leader)
for phi in phis[block]:
loads[phi] = builder.load(vars[phi])
# Generate copies (store to stack variables)
for block in phis:
for phi in phis[block]:
preds, args = phi.args
var = vars[phi]
phi_args = [loads.get(arg, arg) for arg in args]
for pred, arg in zip(preds, phi_args):
builder.position_before(pred.terminator)
builder.store(arg, var)
# Replace phis
for block in phis:
for phi in phis[block]:
phi.replace_uses(loads[phi])
phi.delete()
return vars, loads
示例6: Translate
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import alloca [as 别名]
class Translate(object):
"""
Translate bytecode to untypes pykit IR.
"""
def __init__(self, func, env):
self.func = func
self.env = env
self.bytecode = ByteCode(func)
# -------------------------------------------------
# Find predecessors
self.blocks = {} # offset -> Block
self.block2offset = {} # Block -> offset
self.allocas = {} # varname -> alloca
self.stacks = {} # Block -> value stack
self.exc_handlers = set() # { Block }
# -------------------------------------------------
# Block stacks
self.block_stack = []
self.loop_stack = []
self.except_stack = []
self.finally_stack = []
# -------------------------------------------------
# CFG
self.predecessors = collections.defaultdict(set)
self.phis = collections.defaultdict(list)
# -------------------------------------------------
# Variables and scoping
self.code = self.bytecode.code
self.varnames = self.bytecode.code.co_varnames
self.consts = self.bytecode.code.co_consts
self.names = self.bytecode.code.co_names
self.argnames = list(self.varnames[:self.bytecode.code.co_argcount])
self.globals = dict(vars(__builtin__))
self.builtins = set(self.globals.values())
self.globals.update(self.func.func_globals)
self.call_annotations = collections.defaultdict(dict)
# -------------------------------------------------
# Error checks
argspec = inspect.getargspec(self.func)
if argspec.varargs:
self.argnames.append(argspec.varargs)
if argspec.keywords:
self.argnames.append(argspec.keywords)
assert not argspec.keywords, "keywords not yet supported"
def initialize(self):
"""Initialize pykit untypes structures"""
# Setup Function
sig = types.Function(types.Opaque, [types.Opaque] * len(self.argnames),
False)
self.dst = Function(func_name(self.func), self.argnames, sig)
# Setup Builder
self.builder = Builder(self.dst)
# Setup Blocks
for offset in self.bytecode.labels:
name = blockname(self.func, offset)
block = self.dst.new_block(name)
self.blocks[offset] = block
self.stacks[block] = []
# Setup Variables
self.builder.position_at_beginning(self.dst.startblock)
for varname in self.varnames:
stackvar = self.builder.alloca(types.Pointer(types.Opaque),
result=self.dst.temp(varname))
self.allocas[varname] = stackvar
# Initialize function arguments
if varname in self.argnames:
self.builder.store(self.dst.get_arg(varname), stackvar)
def interpret(self):
self.curblock = self.dst.startblock
for inst in self.bytecode:
if inst.offset in self.blocks:
# Block switch
newblock = self.blocks[inst.offset]
if self.curblock != newblock:
self.switchblock(newblock)
elif self.curblock.is_terminated():
continue
#.........这里部分代码省略.........
示例7: TestBuilder
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import alloca [as 别名]
class TestBuilder(unittest.TestCase):
def setUp(self):
self.f = Function("testfunc", ['a'],
types.Function(types.Float32, [types.Int32], False))
self.b = Builder(self.f)
self.b.position_at_end(self.f.new_block('entry'))
self.a = self.f.get_arg('a')
def test_basic_builder(self):
v = self.b.alloca(types.Pointer(types.Float32))
result = self.b.mul(self.a, self.a, result='r')
c = self.b.convert(types.Float32, result)
self.b.store(c, v)
val = self.b.load(v)
self.b.ret(val)
# print(string(self.f))
assert interp.run(self.f, args=[10]) == 100
def test_splitblock(self):
old, new = self.b.splitblock('newblock')
with self.b.at_front(old):
self.b.add(self.a, self.a)
with self.b.at_end(new):
self.b.div(self.a, self.a)
self.assertEqual(opcodes(self.f), ['add', 'div'])
def test_loop_builder(self):
square = self.b.mul(self.a, self.a)
c = self.b.convert(types.Float32, square)
self.b.position_after(square)
_, block = self.b.splitblock('start', terminate=True)
self.b.position_at_end(block)
const = partial(Const, type=types.Int32)
cond, body, exit = self.b.gen_loop(const(5), const(10), const(2))
with self.b.at_front(body):
self.b.print(c)
with self.b.at_end(exit):
self.b.ret(c)
self.assertEqual(interp.run(self.f, args=[10]), 100.0)
def test_splitblock_preserve_phis(self):
"""
block1:
%0 = mul a a
jump(newblock)
newblock:
%1 = phi([block1], [%0])
ret %1
"""
square = self.b.mul(self.a, self.a)
old, new = self.b.splitblock('newblock', terminate=True)
with self.b.at_front(new):
phi = self.b.phi(types.Int32, [self.f.startblock], [square])
self.b.ret(phi)
# Now split block1
self.b.position_after(square)
block1, split = self.b.splitblock(terminate=True)
phi, ret = new.ops
blocks, values = phi.args
self.assertEqual(blocks, [split])