Esempio n. 1
0
def _add_size_virtuals(structure, type_definition):
    """Adds a $size_in_bits or $size_in_bytes virtual field to structure."""
    names = {
        ir_pb2.TypeDefinition.BIT: "$size_in_bits",
        ir_pb2.TypeDefinition.BYTE: "$size_in_bytes",
    }
    size_field_name = names[type_definition.addressable_unit]
    size_clauses = []
    for field in structure.field:
        # Virtual fields do not have a physical location, and thus do not contribute
        # to the size of the structure.
        if ir_util.field_is_virtual(field):
            continue
        size_clause = ir_pb2.Expression()
        size_clause.CopyFrom(_SIZE_CLAUSE_SKELETON)
        # Copy the appropriate clauses into `existence_condition ? start + size : 0`
        size_clause.function.args[0].CopyFrom(field.existence_condition)
        size_clause.function.args[1].function.args[0].CopyFrom(
            field.location.start)
        size_clause.function.args[1].function.args[1].CopyFrom(
            field.location.size)
        size_clauses.append(size_clause)
    size_expression = ir_pb2.Expression()
    size_expression.CopyFrom(_SIZE_SKELETON)
    size_expression.function.args.extend(size_clauses)
    _mark_as_synthetic(size_expression)
    size_field = ir_pb2.Field(
        read_transform=size_expression,
        name=ir_pb2.NameDefinition(name=ir_pb2.Word(text=size_field_name)),
        existence_condition=ir_pb2.Expression(
            boolean_constant=ir_pb2.BooleanConstant(value=True)),
        attribute=[_skip_text_output_attribute()])
    structure.field.extend([size_field])
Esempio n. 2
0
 def test_is_constant_enum(self):
     self.assertTrue(
         ir_util.is_constant(
             ir_pb2.Expression(
                 constant_reference=ir_pb2.Reference(),
                 type=ir_pb2.ExpressionType(enumeration=ir_pb2.EnumType(
                     value="12")))))
Esempio n. 3
0
 def test_constant_value_of_builtin_reference(self):
     self.assertEqual(
         12,
         ir_util.constant_value(
             ir_pb2.Expression(builtin_reference=ir_pb2.Reference(
                 canonical_name=ir_pb2.CanonicalName(
                     object_path=["$foo"]))), {"$foo": 12}))
Esempio n. 4
0
 def test_constant_value_of_boolean_reference(self):
     self.assertTrue(
         ir_util.constant_value(
             ir_pb2.Expression(
                 constant_reference=ir_pb2.Reference(),
                 type=ir_pb2.ExpressionType(boolean=ir_pb2.BooleanType(
                     value=True)))))
Esempio n. 5
0
 def test_constant_value_of_integer_reference(self):
     self.assertEqual(
         12,
         ir_util.constant_value(
             ir_pb2.Expression(
                 constant_reference=ir_pb2.Reference(),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modulus="infinity", modular_value="12")))))
Esempio n. 6
0
 def test_constant_value_of_enum(self):
     self.assertEqual(
         12,
         ir_util.constant_value(
             ir_pb2.Expression(
                 constant_reference=ir_pb2.Reference(),
                 type=ir_pb2.ExpressionType(enumeration=ir_pb2.EnumType(
                     value="12")))))
Esempio n. 7
0
def _construct_boolean_attribute(name, value, source_location):
  """Constructs a boolean Attribute with the given name and value."""
  attr_value = ir_pb2.AttributeValue(
      expression=ir_pb2.Expression(
          boolean_constant=ir_pb2.BooleanConstant(
              value=value, source_location=source_location),
          type=ir_pb2.ExpressionType(boolean=ir_pb2.BooleanType(value=value)),
          source_location=source_location),
      source_location=source_location)
  return ir_pb2.Attribute(name=ir_pb2.Word(text=name,
                                           source_location=source_location),
                          value=attr_value,
                          source_location=source_location)
Esempio n. 8
0
def _construct_integer_attribute(name, value, source_location):
  """Constructs an integer Attribute with the given name and value."""
  attr_value = ir_pb2.AttributeValue(
      expression=ir_pb2.Expression(
          constant=ir_pb2.NumericConstant(value=str(value),
                                          source_location=source_location),
          type=ir_pb2.ExpressionType(
              integer=ir_pb2.IntegerType(modular_value=str(value),
                                         modulus="infinity",
                                         minimum_value=str(value),
                                         maximum_value=str(value))),
          source_location=source_location),
      source_location=source_location)
  return ir_pb2.Attribute(name=ir_pb2.Word(text=name),
                          value=attr_value,
                          source_location=source_location)
Esempio n. 9
0
 def test_get_boolean_attribute(self):
     type_def = ir_pb2.TypeDefinition(attribute=[
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression()),
                          name=ir_pb2.Word(text="phil")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("true")),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob2")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("true")),
                          name=ir_pb2.Word(text="bob2"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob3"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word()),
     ])
     self.assertTrue(
         ir_util.get_boolean_attribute(type_def.attribute, "bob"))
     self.assertTrue(
         ir_util.get_boolean_attribute(type_def.attribute,
                                       "bob",
                                       default_value=False))
     self.assertFalse(
         ir_util.get_boolean_attribute(type_def.attribute, "bob2"))
     self.assertFalse(
         ir_util.get_boolean_attribute(type_def.attribute,
                                       "bob2",
                                       default_value=True))
     self.assertIsNone(
         ir_util.get_boolean_attribute(type_def.attribute, "Bob"))
     self.assertTrue(
         ir_util.get_boolean_attribute(type_def.attribute,
                                       "Bob",
                                       default_value=True))
     self.assertIsNone(
         ir_util.get_boolean_attribute(type_def.attribute, "bob3"))
Esempio n. 10
0
 def test_get_duplicate_attribute(self):
     type_def = ir_pb2.TypeDefinition(attribute=[
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression()),
                          name=ir_pb2.Word(text="phil")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("true")),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word()),
     ])
     self.assertRaises(AssertionError, ir_util.get_attribute,
                       type_def.attribute, "bob")
Esempio n. 11
0
 def test_constant_value_of_none(self):
     self.assertIsNone(ir_util.constant_value(ir_pb2.Expression()))
Esempio n. 12
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"))
Esempio n. 13
0
def _invert_expression(expression, ir):
  """For the given expression, searches for an algebraic inverse expression.

  That is, it takes the notional equation:

      $logical_value = expression

  and, if there is exactly one `field_reference` in `expression`, it will
  attempt to solve the equation for that field.  For example, if the expression
  is `x + 1`, it will iteratively transform:

      $logical_value = x + 1
      $logical_value - 1 = x + 1 - 1
      $logical_value - 1 = x

  and finally return `x` and `$logical_value - 1`.

  The purpose of this transformation is to find an assignment statement that can
  be used to write back through certain virtual fields.  E.g., given:

      struct Foo:
        0 [+1]  UInt  raw_value
        let actual_value = raw_value + 100

  it should be possible to write a value to the `actual_value` field, and have
  it set `raw_value` to the appropriate value.

  Arguments:
    expression: an ir_pb2.Expression to be inverted.
    ir: the full IR, for looking up symbols.

  Returns:
    (field_reference, inverse_expression) if expression can be inverted,
    otherwise None.
  """
  reference_path = _find_field_reference_path(expression)
  if reference_path is None:
    return None
  subexpression = expression
  result = ir_pb2.Expression(
      builtin_reference=ir_pb2.Reference(
          canonical_name=ir_pb2.CanonicalName(
              module_file="",
              object_path=["$logical_value"]
          ),
          source_name=[ir_pb2.Word(
              text="$logical_value",
              source_location=ir_pb2.Location(is_synthetic=True)
          )],
          source_location=ir_pb2.Location(is_synthetic=True)
      ),
      type=expression.type,
      source_location=ir_pb2.Location(is_synthetic=True)
  )

  # This loop essentially starts with:
  #
  #     f(g(x)) == $logical_value
  #
  # and ends with
  #
  #     x == g_inv(f_inv($logical_value))
  #
  # At each step, `subexpression` has one layer removed, and `result` has a
  # corresponding inverse function applied.  So, for example, it might start
  # with:
  #
  #     2 + ((3 - x) - 10)  ==  $logical_value
  #
  # On each iteration, `subexpression` and `result` will become:
  #
  #     (3 - x) - 10  ==  $logical_value - 2    [subtract 2 from both sides]
  #     (3 - x)  ==  ($logical_value - 2) + 10  [add 10 to both sides]
  #     x  ==  3 - (($logical_value - 2) + 10)  [subtract both sides from 3]
  #
  # This is an extremely limited algebraic solver, but it covers common-enough
  # cases.
  #
  # Note that any equation that can be solved here becomes part of Emboss's
  # contract, forever, so be conservative in expanding its solving capabilities!
  for index in reference_path:
    if subexpression.function.function == ir_pb2.Function.ADDITION:
      result = ir_pb2.Expression(
          function=ir_pb2.Function(
              function=ir_pb2.Function.SUBTRACTION,
              args=[
                  result,
                  subexpression.function.args[1 - index],
              ]
          ),
          type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType())
      )
    elif subexpression.function.function == ir_pb2.Function.SUBTRACTION:
      if index == 0:
        result = ir_pb2.Expression(
            function=ir_pb2.Function(
                function=ir_pb2.Function.ADDITION,
                args=[
                    result,
                    subexpression.function.args[1],
                ]
            ),
            type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType())
        )
      else:
        result = ir_pb2.Expression(
            function=ir_pb2.Function(
                function=ir_pb2.Function.SUBTRACTION,
                args=[
                    subexpression.function.args[0],
                    result,
                ]
            ),
            type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType())
        )
    else:
      return None
    subexpression = subexpression.function.args[index]
  expression_bounds.compute_constraints_of_expression(result, ir)
  return subexpression, result
Esempio n. 14
0
def _add_anonymous_aliases(structure, type_definition):
    """Adds synthetic alias fields for all fields in anonymous fields.

  This essentially completes the rewrite of this:

      struct Foo:
        0  [+4]  bits:
          0  [+1]  Flag  low
          31 [+1]  Flag  high

  Into this:

      struct Foo:
        bits EmbossReservedAnonymous0:
          [text_output: "Skip"]
          0  [+1]  Flag  low
          31 [+1]  Flag  high
        0 [+4]  EmbossReservedAnonymous0  emboss_reserved_anonymous_1
        let low = emboss_reserved_anonymous_1.low
        let high = emboss_reserved_anonymous_1.high

  Note that this pass runs very, very early -- even before symbols have been
  resolved -- so very little in ir_util will work at this point.

  Arguments:
      structure: The ir_pb2.Structure on which to synthesize fields.
      type_definition: The ir_pb2.TypeDefinition containing structure.

  Returns:
      None
  """
    new_fields = []
    for field in structure.field:
        new_fields.append(field)
        if not field.name.is_anonymous:
            continue
        field.attribute.extend([_skip_text_output_attribute()])
        for subtype in type_definition.subtype:
            if (subtype.name.name.text ==
                    field.type.atomic_type.reference.source_name[-1].text):
                field_type = subtype
                break
        else:
            assert False, (
                "Unable to find corresponding type {} for anonymous field "
                "in {}.".format(field.type.atomic_type.reference,
                                type_definition))
        anonymous_reference = ir_pb2.Reference(source_name=[field.name.name])
        anonymous_field_reference = ir_pb2.FieldReference(
            path=[anonymous_reference])
        for subfield in field_type.structure.field:
            alias_field_reference = ir_pb2.FieldReference(path=[
                anonymous_reference,
                ir_pb2.Reference(source_name=[subfield.name.name]),
            ])
            new_existence_condition = ir_pb2.Expression()
            new_existence_condition.CopyFrom(
                _ANONYMOUS_BITS_ALIAS_EXISTENCE_SKELETON)
            existence_clauses = new_existence_condition.function.args
            existence_clauses[0].function.args[0].field_reference.CopyFrom(
                anonymous_field_reference)
            existence_clauses[1].function.args[0].field_reference.CopyFrom(
                alias_field_reference)
            new_read_transform = ir_pb2.Expression(
                field_reference=alias_field_reference)
            # This treats *most* of the alias field as synthetic, but not its name(s):
            # leaving the name(s) as "real" means that symbol collisions with the
            # surrounding structure will be properly reported to the user.
            _mark_as_synthetic(new_existence_condition)
            _mark_as_synthetic(new_read_transform)
            new_alias = ir_pb2.Field(
                read_transform=new_read_transform,
                existence_condition=new_existence_condition,
                name=subfield.name)
            if subfield.HasField("abbreviation"):
                new_alias.abbreviation.CopyFrom(subfield.abbreviation)
            _mark_as_synthetic(new_alias.existence_condition)
            _mark_as_synthetic(new_alias.read_transform)
            new_fields.append(new_alias)
            # Since the alias field's name(s) are "real," it is important to mark the
            # original field's name(s) as synthetic, to avoid duplicate error
            # messages.
            _mark_as_synthetic(subfield.name)
            if subfield.HasField("abbreviation"):
                _mark_as_synthetic(subfield.abbreviation)
    del structure.field[:]
    structure.field.extend(new_fields)