本文整理汇总了Python中pykit.ir.Builder.position_at_beginning方法的典型用法代码示例。如果您正苦于以下问题:Python Builder.position_at_beginning方法的具体用法?Python Builder.position_at_beginning怎么用?Python Builder.position_at_beginning使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pykit.ir.Builder
的用法示例。
在下文中一共展示了Builder.position_at_beginning方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: move_allocas
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_at_beginning [as 别名]
def move_allocas(func, allocas):
"""Move all allocas to the start block"""
builder = Builder(func)
builder.position_at_beginning(func.startblock)
for alloca in allocas:
if alloca.block != func.startblock:
alloca.unlink()
builder.emit(alloca)
示例2: rewrite_lowlevel_constants
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_at_beginning [as 别名]
def rewrite_lowlevel_constants(func, env):
"""
Rewrite constant pointers.
"""
b = Builder(func)
b.position_at_beginning(func.startblock)
for op in func.ops:
constants = collect_constants(op)
new_constants = []
for c in constants:
new_constants.append(allocate_pointer_const(b, c))
substitute_args(op, constants, new_constants)
示例3: preserve_exceptions
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_at_beginning [as 别名]
def preserve_exceptions(oldblock, newblock):
"""
Preserve exc_setup instructions for block splits.
"""
from pykit.ir import Builder
func = oldblock.parent
b = Builder(func)
b.position_at_beginning(newblock)
for op in oldblock.leaders:
if op.opcode == 'exc_setup':
b.exc_setup(op.args[0], **op.metadata)
示例4: from_expr
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_at_beginning [as 别名]
def from_expr(graph, expr_context, env):
"""
Map a Blaze expression graph to blaze AIR
Parameters
----------
graph: blaze.expr.Op
Expression graph
expr_context: ExprContext
Context of the expression
ctx: ExecutionContext
"""
inputs = expr_context.params
# -------------------------------------------------
# Types
argtypes = [operand.dshape for operand in inputs]
signature = types.Function(graph.dshape, argtypes, varargs=False)
# -------------------------------------------------
# Setup function
name = "expr"
argnames = ["e%d" % i for i in range(len(inputs))]
f = Function(name, argnames, signature)
builder = Builder(f)
builder.position_at_beginning(f.new_block('entry'))
# -------------------------------------------------
# Generate function
valuemap = dict((expr, f.get_arg("e%d" % i))
for i, expr in enumerate(inputs))
_from_expr(graph, f, builder, valuemap)
retval = valuemap[graph]
builder.ret(retval)
# Update environment with runtime arguments
runtime_args = [expr_context.terms[input] for input in inputs]
env['runtime.args'] = dict(zip(f.args, runtime_args))
return f
示例5: from_expr
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_at_beginning [as 别名]
def from_expr(graph, expr_context, ctx):
"""
Map a Blaze expression graph to blaze AIR
Parameters
----------
graph: blaze.expr.Op
Expression graph
expr_context: ExprContext
Context of the expression
ctx: ExecutionContext
"""
inputs = expr_context.params
# -------------------------------------------------
# Types
argtypes = [operand.dshape for operand in inputs]
signature = types.Function(graph.dshape, argtypes)
# -------------------------------------------------
# Setup function
name = "expr%d" % ctx.incr()
argnames = ["e%d" % i for i in range(len(inputs))]
f = Function(name, argnames, signature)
builder = Builder(f)
builder.position_at_beginning(f.new_block('entry'))
# -------------------------------------------------
# Generate function
values = dict((expr, f.get_arg("e%d" % i))
for i, expr in enumerate(inputs))
_from_expr(graph, f, builder, values)
retval = values[graph]
builder.ret(retval)
return f, values
示例6: generate_copies
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_at_beginning [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
示例7: Translate
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import position_at_beginning [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
#.........这里部分代码省略.........