Пример #1
0
 def test_to_vtype_with_layout_with_one_field(self):
     layout_to_convert = layout.Layout(
         bit_size=32,
         bit_alignment=16,
         fields=[
             layout.Field(bit_offset=0,
                          name='x',
                          layout=layout.Layout(
                              bit_size=32,
                              bit_alignment=16,
                          ))
         ],
     )
     type_definition = c_ast.CStruct([
         c_ast.CField(
             name='x',
             type_definition=c_ast.CTypeReference('some_type'),
         ),
     ])
     self.type_description_visitor.get_description.return_value = (
         'some_description')
     actual = self.layout_to_vtype_converter.to_vtype(
         layout_to_convert,
         type_definition,
         'some_types',
     )
     self.type_description_visitor.get_description.assert_called_with(
         c_ast.CTypeReference('some_type'),
         'some_types',
     )
     expected = [4, {'x': [0, 'some_description']}]
     self.assertEqual(actual, expected)
Пример #2
0
 def test_to_vtype_with_layout_with_bit_field(self):
     layout_to_convert = layout.Layout(
         bit_size=48,
         bit_alignment=16,
         fields=[
             layout.Field(bit_offset=33,
                          name='x',
                          layout=layout.Layout(
                              bit_size=9,
                              bit_alignment=16,
                              bit_field=True,
                          ))
         ],
     )
     type_definition = c_ast.CStruct([
         c_ast.CField(
             name='x',
             type_definition=c_ast.CTypeReference('some_type'),
             bit_size=9,
         ),
     ])
     self.type_description_visitor.get_description.return_value = [
         'some_type'
     ]
     actual = self.layout_to_vtype_converter.to_vtype(
         layout_to_convert,
         type_definition,
         'some_types',
     )
     self.type_description_visitor.get_description.assert_called_with(
         c_ast.CTypeReference('some_type'),
         'some_types',
     )
     expected = [
         6,
         {
             'x': [
                 4,
                 [
                     'BitField', {
                         'start_bit': 1,
                         'end_bit': 10,
                         'target': 'some_type',
                     }
                 ],
             ],
         },
     ]
     self.assertEqual(actual, expected)
Пример #3
0
    def visit_c_union(self, union):
        """A method visiting a union definition and returing the layout.

    Args:
      union: an object representing a union definition in AST.

    Returns:
      An object representing the layout of the union.
    """
        fields = self._field_collector.collect_fields(union.content)
        packed = self._is_packed(union.attributes)
        bit_alignment = self._get_attributes_alignment(union.attributes)
        bit_size = 0
        for field, type_definition in zip(fields, union.content):
            if not packed or self._is_alignment_overriden(type_definition):
                bit_alignment = self._lcm(bit_alignment,
                                          field.layout.bit_alignment)
            field.bit_offset = 0
            bit_size = max(bit_size, field.layout.bit_size)
        bit_size = self._align(bit_size, bit_alignment)
        return layouts.Layout(
            bit_size=bit_size,
            bit_alignment=bit_alignment,
            fields=fields,
        )
Пример #4
0
    def visit_c_struct(self, struct):
        """A method visiting a struct definition and returing the layout.

    Args:
      struct: an object representing a struct definition in AST.

    Returns:
      An object representing the layout of the struct.
    """
        fields = self._field_collector.collect_fields(struct.content)
        packed = self._is_packed(struct.attributes)
        bit_alignment = self._get_attributes_alignment(struct.attributes)
        bit_offset = 0
        for field, type_definition in zip(fields, struct.content):
            bit_offset = self._align_field(
                bit_offset,
                field.layout,
                type_definition,
                packed,
            )
            if not packed or self._is_alignment_overriden(type_definition):
                bit_alignment = self._lcm(bit_alignment,
                                          field.layout.bit_alignment)
            field.bit_offset = bit_offset
            bit_offset += field.layout.bit_size
        bit_size = self._align(bit_offset, bit_alignment)
        return layouts.Layout(
            bit_size=bit_size,
            bit_alignment=bit_alignment,
            fields=fields,
        )
Пример #5
0
 def visit_c_pointer(self, pointer):
     _ = pointer
     return layouts.Layout(
         bit_size=self._pointer_bit_size(),
         bit_alignment=self._pointer_bit_alignment(),
         fields=[],
     )
Пример #6
0
 def test_to_vtype_with_layout_with_no_fields(self):
     layout_to_convert = layout.Layout(
         bit_size=40,
         bit_alignment=16,
         fields=[],
     )
     type_definition = c_ast.CStruct([])
     actual = self.layout_to_vtype_converter.to_vtype(
         layout_to_convert,
         type_definition,
         'some_types',
     )
     expected = [5, {}]
     self.assertEqual(actual, expected)
Пример #7
0
 def visit_c_simple_type(self, simple_type):
     return layouts.Layout(
         bit_size=simple_type.bit_size,
         bit_alignment=simple_type.bit_alignment,
         fields=[],
     )
Пример #8
0
 def test_to_vtype_with_layout_with_three_fields(self):
     layout_to_convert = layout.Layout(
         bit_size=48,
         bit_alignment=32,
         fields=[
             layout.Field(bit_offset=0,
                          name='x',
                          layout=layout.Layout(
                              bit_size=16,
                              bit_alignment=8,
                          )),
             layout.Field(bit_offset=16,
                          name='y',
                          layout=layout.Layout(
                              bit_size=8,
                              bit_alignment=32,
                          )),
             layout.Field(bit_offset=24,
                          name='z',
                          layout=layout.Layout(
                              bit_size=24,
                              bit_alignment=16,
                          )),
         ],
     )
     some_type = c_ast.CTypeReference('some_type')
     some_other_type = c_ast.CTypeReference('some_other_type')
     type_definition = c_ast.CStruct([
         c_ast.CField(
             name='x',
             type_definition=some_type,
         ),
         c_ast.CField(
             name='y',
             type_definition=some_other_type,
         ),
         c_ast.CField(
             name='z',
             type_definition=some_type,
         ),
     ])
     self.type_description_visitor.get_description.side_effect = (
         'some_description',
         'some_other_description',
         'some_description',
     )
     actual = self.layout_to_vtype_converter.to_vtype(
         layout_to_convert,
         type_definition,
         'some_types',
     )
     expected_get_description_calls = [
         mock.call(some_type, 'some_types'),
         mock.call(some_other_type, 'some_types'),
         mock.call(some_type, 'some_types'),
     ]
     self.assertEqual(
         self.type_description_visitor.get_description.call_args_list,
         expected_get_description_calls,
     )
     expected = [
         6, {
             'x': [0, 'some_description'],
             'y': [2, 'some_other_description'],
             'z': [3, 'some_description'],
         }
     ]
     self.assertEqual(actual, expected)