Exemplo n.º 1
0
def _check_that_array_base_types_are_fixed_size(type_ir, source_file_name,
                                                errors, ir):
    """Checks that the sizes of array elements are known at compile time."""
    if type_ir.base_type.HasField("array_type"):
        # An array is fixed size if its base_type is fixed size and its array
        # dimension is constant.  This function will be called again on the inner
        # array, and we do not want to cascade errors if the inner array's base_type
        # is not fixed size.  The array dimensions are separately checked by
        # _check_that_inner_array_dimensions_are_constant, which will provide an
        # appropriate error message for that case.
        return
    assert type_ir.base_type.HasField("atomic_type")
    if type_ir.base_type.HasField("size_in_bits"):
        # If the base_type has a size_in_bits, then it is fixed size.
        return
    base_type = ir_util.find_object(type_ir.base_type.atomic_type.reference,
                                    ir)
    base_type_fixed_size = ir_util.get_integer_attribute(
        base_type.attribute, attributes.FIXED_SIZE)
    if base_type_fixed_size is None:
        errors.append([
            error.error(source_file_name,
                        type_ir.base_type.atomic_type.source_location,
                        "Array elements must be fixed size.")
        ])
Exemplo n.º 2
0
def _add_addressable_unit_to_external(external, type_definition):
  """Sets the addressable_unit field for an external TypeDefinition."""
  # Strictly speaking, addressable_unit isn't an "attribute," but it's close
  # enough that it makes sense to handle it with attributes.
  del external  # Unused.
  size = ir_util.get_integer_attribute(type_definition.attribute,
                                       attributes.ADDRESSABLE_UNIT_SIZE)
  if size == 1:
    type_definition.addressable_unit = ir_pb2.TypeDefinition.BIT
  elif size == 8:
    type_definition.addressable_unit = ir_pb2.TypeDefinition.BYTE
Exemplo n.º 3
0
def _verify_width_attribute_on_enum(enum, type_definition, source_file_name,
                                    errors):
  """Verifies the maximum_bits attribute for an enum TypeDefinition."""
  max_bits_value = ir_util.get_integer_attribute(type_definition.attribute,
                                                attributes.ENUM_MAXIMUM_BITS)
  # The attribute should already have been defaulted, if not originally present.
  assert max_bits_value is not None, "maximum_bits not set"
  if max_bits_value > 64 or max_bits_value < 1:
    max_bits_attr = ir_util.get_attribute(type_definition.attribute,
                                          attributes.ENUM_MAXIMUM_BITS)
    errors.append([
        error.error(source_file_name, max_bits_attr.source_location,
                    "'maximum_bits' on an 'enum' must be between 1 and 64.")
    ])
Exemplo n.º 4
0
def _verify_addressable_unit_attribute_on_external(external, type_definition,
                                                   source_file_name, errors):
  """Verifies the addressable_unit_size attribute on an external."""
  del external  # Unused.
  addressable_unit_size_attr = ir_util.get_integer_attribute(
      type_definition.attribute, attributes.ADDRESSABLE_UNIT_SIZE)
  if addressable_unit_size_attr is None:
    errors.append([error.error(
        source_file_name, type_definition.source_location,
        "Expected '{}' attribute for external type.".format(
            attributes.ADDRESSABLE_UNIT_SIZE))])
  elif addressable_unit_size_attr not in (1, 8):
    errors.append([
        error.error(source_file_name, type_definition.source_location,
                    "Only values '1' (bit) and '8' (byte) are allowed for the "
                    "'{}' attribute".format(attributes.ADDRESSABLE_UNIT_SIZE))
    ])
Exemplo n.º 5
0
def _check_size_of_bits(type_ir, type_definition, source_file_name, errors):
    """Checks that `bits` types are fixed size, less than 64 bits."""
    del type_ir  # Unused
    if type_definition.addressable_unit != ir_pb2.TypeDefinition.BIT:
        return
    fixed_size = ir_util.get_integer_attribute(type_definition.attribute,
                                               attributes.FIXED_SIZE)
    if fixed_size is None:
        errors.append([
            error.error(source_file_name, type_definition.source_location,
                        "`bits` types must be fixed size.")
        ])
        return
    if fixed_size > 64:
        errors.append([
            error.error(source_file_name, type_definition.source_location,
                        "`bits` types must be 64 bits or smaller.")
        ])
Exemplo n.º 6
0
def _add_missing_width_and_sign_attributes_on_enum(enum, type_definition):
  """Sets the maximum_bits and is_signed attributes for an enum, if needed."""
  max_bits_attr = ir_util.get_integer_attribute(type_definition.attribute,
                                                attributes.ENUM_MAXIMUM_BITS)
  if max_bits_attr is None:
    type_definition.attribute.extend([
        _construct_integer_attribute(attributes.ENUM_MAXIMUM_BITS,
                                     _DEFAULT_ENUM_MAXIMUM_BITS,
                                     type_definition.source_location)])
  signed_attr = ir_util.get_boolean_attribute(type_definition.attribute,
                                              attributes.IS_SIGNED)
  if signed_attr is None:
    for value in enum.value:
      numeric_value = ir_util.constant_value(value.value)
      if numeric_value < 0:
        is_signed = True
        break
    else:
      is_signed = False
    type_definition.attribute.extend([
        _construct_boolean_attribute(attributes.IS_SIGNED, is_signed,
                                     type_definition.source_location)])
Exemplo n.º 7
0
 def test_get_integer_attribute(self):
     type_def = ir_pb2.TypeDefinition(attribute=[
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(type=ir_pb2.ExpressionType(
                 integer=ir_pb2.IntegerType()))),
                          name=ir_pb2.Word(text="phil")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="20"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="20", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="10"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="10", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="5"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="5", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob2")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="0"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="0", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob2"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="30"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="30", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob3"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(function=ir_pb2.Function(
                 function=ir_pb2.Function.ADDITION,
                 args=[
                     ir_pb2.Expression(
                         constant=ir_pb2.NumericConstant(value="100"),
                         type=ir_pb2.ExpressionType(
                             integer=ir_pb2.IntegerType(
                                 modular_value="100", modulus="infinity"))),
                     ir_pb2.Expression(
                         constant=ir_pb2.NumericConstant(value="100"),
                         type=ir_pb2.ExpressionType(
                             integer=ir_pb2.IntegerType(
                                 modular_value="100", modulus="infinity")))
                 ]),
                                          type=ir_pb2.ExpressionType(
                                              integer=ir_pb2.IntegerType(
                                                  modular_value="200",
                                                  modulus="infinity")))),
                          name=ir_pb2.Word(text="bob4")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="40"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="40", modulus="infinity")))),
                          name=ir_pb2.Word()),
     ])
     self.assertEqual(
         10, ir_util.get_integer_attribute(type_def.attribute, "bob"))
     self.assertEqual(
         5, ir_util.get_integer_attribute(type_def.attribute, "bob2"))
     self.assertIsNone(
         ir_util.get_integer_attribute(type_def.attribute, "Bob"))
     self.assertEqual(
         10,
         ir_util.get_integer_attribute(type_def.attribute,
                                       "Bob",
                                       default_value=10))
     self.assertIsNone(
         ir_util.get_integer_attribute(type_def.attribute, "bob3"))
     self.assertEqual(
         200, ir_util.get_integer_attribute(type_def.attribute, "bob4"))