Exemple #1
0
  def _def(self, node: ast.AstNode, ir_func: Callable[..., BValue], *args,
           **kwargs) -> BValue:
    # TODO(leary): 2019-07-19 When everything is Python 3 we can switch to use a
    # single kwarg "span" after vararg with a normal pytype annotation.
    span = kwargs.pop('span', None)
    assert not kwargs
    assert isinstance(span,
                      (type(None), Span)), 'Expect span kwarg as Span or None.'

    if span is None and hasattr(node, 'span') and isinstance(node.span, Span):
      span = node.span
    loc = None
    if span is not None and self.emit_positions:
      start_pos = span.start
      lineno = fileno_mod.Lineno(start_pos.lineno)
      colno = fileno_mod.Colno(start_pos.colno)
      loc = source_location.SourceLocation(self.fileno, lineno, colno)

    try:
      ir = ir_func(*args, loc=loc)
    except TypeError:
      logging.error('Failed to call IR-creating function %s with args: %s',
                    ir_func, args)
      raise
    assert isinstance(
        ir, BValue), 'Expect ir_func to return a BValue; got {!r}'.format(ir)
    logging.vlog(4, 'Define node "%s" to be %s', node, ir)
    self.node_to_ir[node] = ir
    return ir
Exemple #2
0
  def test_simple_build_and_dump_package(self):
    p = ir_package.Package('test_package')
    fileno = p.get_or_create_fileno('my_file.x')
    fb = function_builder.FunctionBuilder('test_function', p)
    t = p.get_bits_type(32)
    x = fb.add_param('x', t)

    lineno = fileno_mod.Lineno(42)
    colno = fileno_mod.Colno(64)
    loc = source_location.SourceLocation(fileno, lineno, colno)
    fb.add_or(x, x, loc=loc, name='my_or')
    fb.add_not(x, loc=loc, name='why_not')

    f = fb.build()
    self.assertEqual(f.name, 'test_function')
    self.assertEqual(
        f.dump_ir(), """\
fn test_function(x: bits[32]) -> bits[32] {
  my_or: bits[32] = or(x, x, id=2, pos=0,42,64)
  ret why_not: bits[32] = not(x, id=3, pos=0,42,64)
}
""")

    self.assertMultiLineEqual(
        p.dump_ir(), """\
package test_package

fn test_function(x: bits[32]) -> bits[32] {
  my_or: bits[32] = or(x, x, id=2, pos=0,42,64)
  ret why_not: bits[32] = not(x, id=3, pos=0,42,64)
}
""")
    def test_all_add_methods(self):
        # This test is mainly about checking that pybind11 is able to map parameter
        # and return types properly. Because of this it's not necessary to check
        # the result at the end; that methods don't throw when called is enough.
        p = ir_package.Package('test_package')
        fileno = p.get_or_create_fileno('my_file.x')
        lineno = fileno_mod.Lineno(42)
        colno = fileno_mod.Colno(64)
        loc = source_location.SourceLocation(fileno, lineno, colno)
        fb = function_builder.FunctionBuilder('test_function', p)

        input_function_builder = function_builder.FunctionBuilder('fn', p)
        input_function_builder.add_literal_value(
            ir_value.Value(bits_mod.UBits(7, 8)))
        input_function = input_function_builder.build()

        single_zero_bit = fb.add_literal_value(
            ir_value.Value(bits_mod.UBits(value=0, bit_count=1)))
        t = p.get_bits_type(32)
        x = fb.add_param('x', t)

        fb.add_shra(x, x, loc=loc)
        fb.add_shra(x, x, loc=loc)
        fb.add_shrl(x, x, loc=loc)
        fb.add_shll(x, x, loc=loc)
        fb.add_or(x, x, loc=loc)
        fb.add_nary_or([x], loc=loc)
        fb.add_xor(x, x, loc=loc)
        fb.add_and(x, x, loc=loc)
        fb.add_smul(x, x, loc=loc)
        fb.add_umul(x, x, loc=loc)
        fb.add_udiv(x, x, loc=loc)
        fb.add_sub(x, x, loc=loc)
        fb.add_add(x, x, loc=loc)

        fb.add_concat([x], loc=loc)

        fb.add_ule(x, x, loc=loc)
        fb.add_ult(x, x, loc=loc)
        fb.add_uge(x, x, loc=loc)
        fb.add_ugt(x, x, loc=loc)

        fb.add_sle(x, x, loc=loc)
        fb.add_slt(x, x, loc=loc)
        fb.add_sge(x, x, loc=loc)
        fb.add_sgt(x, x, loc=loc)

        fb.add_eq(x, x, loc=loc)
        fb.add_ne(x, x, loc=loc)

        fb.add_neg(x, loc=loc)
        fb.add_not(x, loc=loc)
        fb.add_clz(x, loc=loc)

        fb.add_one_hot(x, lsb_or_msb.LsbOrMsb.LSB, loc=loc)
        fb.add_one_hot_sel(x, [x], loc=loc)

        fb.add_literal_bits(bits_mod.UBits(value=2, bit_count=32), loc=loc)
        fb.add_literal_value(ir_value.Value(
            bits_mod.UBits(value=5, bit_count=32)),
                             loc=loc)

        fb.add_sel(x, x, x, loc=loc)
        fb.add_sel_multi(x, [x], x, loc=loc)
        fb.add_match_true([single_zero_bit], [x], x, loc=loc)

        tuple_node = fb.add_tuple([x], loc=loc)
        fb.add_array([x], t, loc=loc)

        fb.add_tuple_index(tuple_node, 0, loc=loc)

        fb.add_counted_for(x, 1, 1, input_function, [x], loc=loc)

        fb.add_map(fb.add_array([x], t, loc=loc), input_function, loc=loc)

        fb.add_invoke([x], input_function, loc=loc)

        fb.add_array_index(fb.add_array([x], t, loc=loc), x, loc=loc)
        fb.add_reverse(fb.add_array([x], t, loc=loc), loc=loc)
        fb.add_identity(x, loc=loc)
        fb.add_signext(x, 10, loc=loc)
        fb.add_zeroext(x, 10, loc=loc)
        fb.add_bit_slice(x, 4, 2, loc=loc)

        fb.build()
Exemple #4
0
 def test_big_numbers(self):
     fileno.Fileno(123456789)
     fileno.Lineno(2**20)
     fileno.Colno(2**30)
Exemple #5
0
 def test_numbers(self):
     fileno.Fileno(10)
     fileno.Lineno(11)
     fileno.Colno(12)