Example #1
0
 def test_class_methods(self):
     source = """
 class Rectangle {
   int width;
   int height;
   int getArea(){
     return width * height;
   }
 };
 void Rectangle::set_values(int w, int h) {
   width = w;
   height = h;
 }
 int Rectangle::get_width(){
   return width;
 }
 int Rectangle::get_height(){
   return height;
 }
 int test(int a, int b) {
   Rectangle rect;
   rect.width = 0;
   rect.height = 0;
   rect.set_values(a, b);
   return rect.getArea() + rect.get_height() + rect.get_width();
 }
 """
     f = self.parse_and_get_function(source)
     aval = ir_value.Value(bits_mod.SBits(value=int(6), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(3), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(27, result_int)
Example #2
0
    def test_mandelbrot(self):
        translator = xlscc_translator.Translator("mypackage")

        my_parser = ext_c_parser.XLSccParser()

        source_path = runfiles.get_path(
            "xls/contrib/xlscc_obsolete/translate/testdata/mandelbrot_test.cc")
        binary_path = runfiles.get_path(
            "xls/contrib/xlscc_obsolete/translate/mandelbrot_test")

        nx = 48
        ny = 32

        with open(source_path) as f:
            content = f.read()

        # Hackily do the preprocessing, since in build environments we do not have a
        # cpp binary available (and if we did relying on it here without a
        # build-system-noted dependency would be non-hermetic).
        content = re.sub("^#if !NO_TESTBENCH$.*^#endif$", "", content, 0,
                         re.MULTILINE | re.DOTALL)
        content = re.sub(re.compile("//.*?\n"), "", content)

        f = self.create_tempfile(content=content)
        ast = pycparser.parse_file(f.full_path,
                                   use_cpp=False,
                                   parser=my_parser)

        cpp_out = None
        with os.popen(binary_path) as osf:
            cpp_out = osf.read()
        parsed_cpp_out = eval(cpp_out)

        translator.parse(ast)

        p = translator.gen_ir()
        f = p.get_function("mandelbrot")

        result_arr = parsed_cpp_out

        for y in range(0, ny):
            for x in range(0, nx):
                xx = float(x) / nx
                yy = float(y) / ny

                xi = int(xx * 2.5 - 1.8) * (1 << 16)
                yi = int(yy * 2.2 - 1.1) * (1 << 16)

                args = dict(c_r=ir_value.Value(
                    bits_mod.SBits(value=int(xi), bit_count=32)),
                            c_i=ir_value.Value(
                                bits_mod.SBits(value=int(yi), bit_count=32)))

                result = ir_interpreter.run_function_kwargs(f, args)

                result_sai32 = int(str(result))
                result_arr[y][x] = result_sai32

        self.assertEqual(parsed_cpp_out, result_arr)
Example #3
0
 def one_in_one_out(self, source, a_input, b_input, expected_output):
     f = self.parse_and_get_function("""
   int test(sai32 a, sai32 b) {
     """ + source + """
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(a_input), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(b_input), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     self.assertEqual(expected_output,
                      int(ctypes.c_int32(int(str(result))).value))
Example #4
0
 def test_enum_autocount(self):
     source = """
 enum states{w, x, y=5, z};
 int test(int a, int b){
     return a+z+b+x;
 }
 """
     f = self.parse_and_get_function(source)
     aval = ir_value.Value(bits_mod.SBits(value=int(2), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(3), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(12, result_int)
Example #5
0
def _int_to_bits(value: int, bit_count: int) -> bits_mod.Bits:
  """Converts a Python arbitrary precision int to a Bits type."""
  if bit_count <= 64:
    return bits_mod.UBits(value, bit_count) if value >= 0 else bits_mod.SBits(
        value, bit_count)
  return number_parser.bits_from_string(
      bit_helpers.to_hex_string(value, bit_count), bit_count=bit_count)
Example #6
0
 def test_arrayref(self):
     f = self.parse_and_get_function("""
   void array_update(int o[2], int x) {
     o[0] = x;
   }
   int test(int a, int b) {
     int s[2] = {0,b};
     array_update(s, a);
     return s[0] + s[1];
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(22), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(10), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(32, result_int)
Example #7
0
def int_to_bits(value: int, bit_count: int) -> ir_bits.Bits:
    """Converts a Python arbitrary precision int to a Bits type."""
    if bit_count <= WORD_SIZE:
        return ir_bits.UBits(value,
                             bit_count) if value >= 0 else ir_bits.SBits(
                                 value, bit_count)
    return number_parser.bits_from_string(bit_helpers.to_hex_string(
        value, bit_count),
                                          bit_count=bit_count)
Example #8
0
    def test_structref(self):
        somestruct = hls_types_pb2.HLSStructType()

        int_type = hls_types_pb2.HLSIntType()
        int_type.signed = True
        int_type.width = 18

        translated_hls_type = hls_types_pb2.HLSType()
        translated_hls_type.as_int.CopyFrom(int_type)

        hls_field = hls_types_pb2.HLSNamedType()
        hls_field.name = "x"
        hls_field.hls_type.CopyFrom(translated_hls_type)
        somestruct.fields.add().CopyFrom(hls_field)

        hls_field = hls_types_pb2.HLSNamedType()
        hls_field.name = "y"
        hls_field.hls_type.CopyFrom(translated_hls_type)
        somestruct.fields.add().CopyFrom(hls_field)

        somestructtype = hls_types_pb2.HLSType()
        somestructtype.as_struct.CopyFrom(somestruct)
        hls_types_by_name = {"SomeStruct": somestructtype}

        f = self.parse_and_get_function(
            """
      void struct_update(SomeStruct &o, sai18 x) {
        o.x.set_slc(0, x);
      }
      int test(sai32 a, int b) {
        SomeStruct s;
        s.x = 0;
        s.y = b;
        struct_update(s, a);
        return s.x + s.y;
      }
    """, hls_types_by_name)
        aval = ir_value.Value(bits_mod.SBits(value=int(22), bit_count=32))
        bval = ir_value.Value(bits_mod.SBits(value=int(10), bit_count=32))
        args = dict(a=aval, b=bval)
        result = ir_interpreter.run_function_kwargs(f, args)
        result_int = int(ctypes.c_int32(int(str(result))).value)
        self.assertEqual(32, result_int)
Example #9
0
    def test_bits(self):
        self.assertEqual(43, bits.UBits(43, 7).to_uint())
        self.assertEqual(53, bits.UBits(53, 7).to_int())
        self.assertEqual(33, bits.SBits(33, 8).to_uint())
        self.assertEqual(83, bits.SBits(83, 8).to_int())

        self.assertEqual(255, bits.UBits(255, 8).to_uint())
        self.assertEqual(-1, bits.UBits(255, 8).to_int())
        self.assertEqual(-1, bits.UBits(2**64 - 1, 64).to_int())
        self.assertEqual(-2**63, bits.SBits(-2**63, 64).to_int())
        self.assertEqual(-2**31, bits.SBits(-2**31, 32).to_int())
        self.assertEqual(2**31 - 1, bits.SBits(2**31 - 1, 32).to_int())
        self.assertEqual(-2**31, bits.UBits(2**31, 32).to_int())

        self.assertEqual(-1, bits.SBits(-1, 1).to_int())
        self.assertEqual(-1, bits.SBits(-1, 8).to_int())
        self.assertEqual(-1, bits.SBits(-1, 63).to_int())
        self.assertEqual(-2, bits.SBits(-2, 64).to_int())
        self.assertEqual(-83, bits.SBits(-83, 8).to_int())
Example #10
0
 def test_ref_params(self):
     f = self.parse_and_get_function("""
   void add_in_place(int &o, int x) {
     o += x;
   }
   int add_in_place_2(int x, int &o) {
     o += x;
     return o*2;
   }
   int test(int a, int b){
     add_in_place(a, b);
     return add_in_place_2(b, a) + a;
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(11), bit_count=32))
     bval = ir_value.Value(bits_mod.SBits(value=int(3), bit_count=32))
     args = dict(a=aval, b=bval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(51, result_int)
Example #11
0
 def test_globalconst(self):
     f = self.parse_and_get_function("""
   const int foo[6] = {2,4,5,3,2,1};
   int test(int a) {
     return foo[a];
   }
 """)
     aval = ir_value.Value(bits_mod.SBits(value=int(2), bit_count=32))
     args = dict(a=aval)
     result = ir_interpreter.run_function_kwargs(f, args)
     result_int = int(ctypes.c_int32(int(str(result))).value)
     self.assertEqual(5, result_int)
Example #12
0
 def v32(n):
     return ir_value.Value(bits_mod.SBits(value=int(n), bit_count=32))
Example #13
0
 def test_bits(self):
   self.assertEqual(43, bits.UBits(43, 7).to_uint())
   self.assertEqual(53, bits.UBits(53, 7).to_int())
   self.assertEqual(33, bits.SBits(33, 8).to_uint())
   self.assertEqual(83, bits.SBits(83, 8).to_int())
   self.assertEqual(-83, bits.SBits(-83, 8).to_int())