本文整理汇总了Python中pykit.ir.Builder.cbranch方法的典型用法代码示例。如果您正苦于以下问题:Python Builder.cbranch方法的具体用法?Python Builder.cbranch怎么用?Python Builder.cbranch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pykit.ir.Builder
的用法示例。
在下文中一共展示了Builder.cbranch方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PykitIRVisitor
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import cbranch [as 别名]
#.........这里部分代码省略.........
if not global_val:
error(node, "Not a local or global: %r" % node.name)
return global_val
def visit_Cast(self, node):
type = self.visit(node.to_type)
if isinstance(node.expr, c_ast.FuncCall):
op = self.visit(node.expr, type=type)
op.type = type
return op
else:
result = self.visit(node.expr)
if result.type == type:
return result
return self.builder.convert(type, [result])
def visit_Assignment(self, node):
if node.op != '=':
error(node, "Only assignment with '=' is supported")
if not isinstance(node.lvalue, c_ast.ID):
error(node, "Canot only assign to a name")
self.assign(node.lvalue.name, node.rvalue)
def visit_Constant(self, node):
type = self.type_env[node.type]
const = types.convert(node.value, types.resolve_typedef(type))
if isinstance(const, basestring):
const = const[1:-1] # slice away quotes
return Const(const)
def visit_UnaryOp(self, node):
op = defs.unary_defs[node.op]
buildop = getattr(self.builder, op)
arg = self.visit(node.expr)
type = self.type or arg.type
return buildop(type, [arg])
def visit_BinaryOp(self, node):
op = binary_defs[node.op]
buildop = getattr(self.builder, op)
left, right = self.visits([node.left, node.right])
type = self.type
if not type:
l, r = map(types.resolve_typedef, [left.type, right.type])
assert l == r, (l, r)
if node.op in defs.compare_defs:
type = types.Bool
return buildop(type or left.type, [left, right])
def visit_If(self, node):
cond = self.visit(node.cond)
ifpos, elsepos, exit_block = self.builder.ifelse(cond)
with ifpos:
self.visit(node.iftrue)
self.builder.jump(exit_block)
with elsepos:
if node.iffalse:
self.visit(node.iffalse)
self.builder.jump(exit_block)
self.builder.position_at_end(exit_block)
def _loop(self, init, cond, next, body):
_, exit_block = self.builder.splitblock(self.func.temp("exit"))
_, body_block = self.builder.splitblock(self.func.temp("body"))
_, cond_block = self.builder.splitblock(self.func.temp("cond"))
self.visitif(init)
self.builder.jump(cond_block)
with self.builder.at_front(cond_block):
cond = self.visit(cond, type=types.Bool)
self.builder.cbranch(cond, body_block, exit_block)
with self.builder.at_front(body_block):
self.visit(body)
self.visitif(next)
bb = self.builder.basic_block
if not bb.tail or not ops.is_terminator(bb.tail.opcode):
self.builder.jump(cond_block)
self.builder.position_at_end(exit_block)
def visit_While(self, node):
self._loop(None, node.cond, None, node.stmt)
def visit_For(self, node):
# avoid silly 2to3 rewrite to 'node.__next__'
next = getattr(node, 'next')
self._loop(node.init, node.cond, next, node.stmt)
def visit_Return(self, node):
b = self.builder
value = self.visit(node.expr)
t = self.func.temp
b.ret(b.convert(self.func.type.restype, [value]))
示例2: PykitIRVisitor
# 需要导入模块: from pykit.ir import Builder [as 别名]
# 或者: from pykit.ir.Builder import cbranch [as 别名]
#.........这里部分代码省略.........
name = node.decl.name
type = self.visit(node.decl.type)
argnames = [p.name for p in node.decl.type.args.params]
self.func = Function(name, argnames, type)
self.func.add_block('entry')
self.builder = Builder(self.func)
self.builder.position_at_end(self.func.blocks[0])
self.generic_visit(node.body)
self.leave_func()
# ______________________________________________________________________
def visit_FuncCall(self, node):
name = node.name.name
if not self.in_typed_context:
error(node, "Expected a type for sub-expression "
"(add a cast or assignment)")
if not hasattr(self.builder, name):
error(node, "No opcode %s" % (name,))
self.in_typed_context = False
buildop = getattr(self.builder, name)
args = self.visits(node.args.exprs)
return buildop, args
def visit_ID(self, node):
if self.in_function:
if node.name not in self.local_vars:
error(node, "Not a local: %r" % node.name)
result = self.alloca(node.name)
return self.builder.load(result.type, result)
def visit_Cast(self, node):
type = self.visit(node.to_type)
if isinstance(node.expr, c_ast.FuncCall):
self.in_typed_context = True
buildop, args = self.visit(node.expr)
return buildop(type, args, "temp")
else:
result = self.visit(node.expr)
if result.type == type:
return result
return self.builder.convert(type, [result], "temp")
def visit_Assignment(self, node):
if node.op != '=':
error(node, "Only assignment with '=' is supported")
if not isinstance(node.lvalue, c_ast.ID):
error(node, "Canot only assign to a name")
self.assign(node.lvalue.name, node.rvalue)
def visit_Constant(self, node):
type = self.type_env[node.type]
const = types.convert(node.value, type)
return Const(const)
def visit_UnaryOp(self, node):
op = defs.unary_defs[node.op]
buildop = getattr(self.builder, op)
arg = self.visit(node.expr)
type = self.type or arg.type
return buildop(type, [arg])
def visit_BinaryOp(self, node):
op = binary_defs[node.op]
buildop = getattr(self.builder, op)
left, right = self.visits([node.left, node.right])
if not self.type:
assert left.type == right.type, (left, right)
return buildop(self.type or left.type, [left, right], "temp")
def _loop(self, init, cond, next, body):
_, exit_block = self.builder.splitblock("exit")
_, body_block = self.builder.splitblock("body")
_, cond_block = self.builder.splitblock("cond")
self.visitif(init)
self.builder.jump(cond_block)
with self.builder.at_front(cond_block):
cond = self.visit(cond, type=types.Bool)
self.builder.cbranch(cond, cond_block, exit_block)
with self.builder.at_front(body_block):
self.visit(body)
self.visitif(next)
self.builder.jump(cond_block)
self.builder.position_at_end(exit_block)
def visit_While(self, node):
self._loop(None, node.cond, None, node.stmt)
def visit_For(self, node):
self._loop(node.init, node.cond, node.next, node.stmt)
def visit_Return(self, node):
self.builder.ret(self.visit(node.expr))