Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8128c60f authored by Henri Chataing's avatar Henri Chataing Committed by Gerrit Code Review
Browse files

Merge "pdl: Complete the python backend generator"

parents 207b3e45 89fdc510
Loading
Loading
Loading
Loading
+27 −10
Original line number Diff line number Diff line
@@ -82,18 +82,35 @@ rust_test_host {
    ],
}

// Python generator.
python_binary_host {
    name: "pdl_python_generator",
    main: "scripts/generate_python_backend.py",
    srcs: [
        "scripts/generate_python_backend.py",
        "scripts/pdl/ast.py",
        "scripts/pdl/core.py",
    ]
}

// Defaults for PDL python backend generation.
genrule_defaults {
    name: "pdl_python_generator_defaults",
    tools: [
        ":pdl",
        ":pdl_python_generator",
    ],
}

// Generate the python parser+serializer backend for the
// little endian test file located at tests/canonical/le_test_file.pdl.
genrule {
    name: "pdl_python_generator_le_test_gen",
    defaults: [ "pdl_python_generator_defaults" ],
    cmd: "$(location :pdl) $(in) |" +
        " $(location scripts/generate_python_backend.py)" +
        " $(location :pdl_python_generator)" +
        " --output $(out) --custom-type-location tests.custom_types",
    tools: [ ":pdl" ],
    tool_files: [
        "scripts/generate_python_backend.py",
        "scripts/pdl/core.py",
        "scripts/pdl/ast.py",
        "tests/custom_types.py",
    ],
    srcs: [
@@ -108,14 +125,11 @@ genrule {
// big endian test file located at tests/canonical/be_test_file.pdl.
genrule {
    name: "pdl_python_generator_be_test_gen",
    defaults: [ "pdl_python_generator_defaults" ],
    cmd: "$(location :pdl) $(in) |" +
        " $(location scripts/generate_python_backend.py)" +
        " $(location :pdl_python_generator)" +
        " --output $(out) --custom-type-location tests.custom_types",
    tools: [ ":pdl" ],
    tool_files: [
        "scripts/generate_python_backend.py",
        "scripts/pdl/core.py",
        "scripts/pdl/ast.py",
        "tests/custom_types.py",
    ],
    srcs: [
@@ -141,6 +155,9 @@ python_test_host {
        "tests/canonical/le_test_vectors.json",
        "tests/canonical/be_test_vectors.json",
    ],
    libs: [
        "typing_extensions",
    ],
    test_options: {
        unit_test: true,
    },
+322 −29

File changed.

Preview size limit exceeded, changes collapsed.

+52 −0
Original line number Diff line number Diff line
@@ -54,6 +54,11 @@ def desugar(file: File):
    file.group_scope = {}


def make_reserved_field(width: int) -> ReservedField:
    """Create a reserved field of specified width."""
    return ReservedField(kind='reserved_field', loc=None, width=width)


def get_packet_field(packet: Union[PacketDeclaration, StructDeclaration], id: str) -> Optional[Field]:
    """Return the field with selected identifier declared in the provided
    packet or its ancestors."""
@@ -70,6 +75,53 @@ def get_packet_field(packet: Union[PacketDeclaration, StructDeclaration], id: st
        return None


def get_packet_shift(packet: Union[PacketDeclaration, StructDeclaration]) -> int:
    """Return the bit shift of the payload or body field in the parent packet.

    When using packet derivation on bit fields, the body may be shifted.
    The shift is handled statically in the implementation of child packets,
    and the incomplete field is included in the body.
    ```
    packet Basic {
        type: 1,
        _body_
    }
    ```
    """

    # Traverse empty parents.
    parent = packet.parent
    while parent and len(parent.fields) == 1:
        parent = parent.parent

    if not parent:
        return 0

    shift = 0
    for f in packet.parent.fields:
        if isinstance(f, (BodyField, PayloadField)):
            return 0 if (shift % 8) == 0 else shift
        else:
            # Fields that do not have a constant size are assumed to start
            # on a byte boundary, and measure an integral number of bytes.
            # Start the count over.
            size = get_field_size(f)
            shift = 0 if size is None else shift + size

    # No payload or body in parent packet.
    # Not raising an error, the generation will fail somewhere else.
    return 0


def get_packet_ancestor(
        decl: Union[PacketDeclaration, StructDeclaration]) -> Union[PacketDeclaration, StructDeclaration]:
    """Return the root ancestor of the selected packet or struct."""
    if decl.parent_id is None:
        return decl
    else:
        return get_packet_ancestor(decl.grammar.packet_scope[decl.parent_id])


def get_derived_packets(decl: Union[PacketDeclaration, StructDeclaration]
                       ) -> List[Tuple[List[Constraint], Union[PacketDeclaration, StructDeclaration]]]:
    """Return the list of packets or structs that immediately derive from the
+29 −0
Original line number Diff line number Diff line
@@ -143,6 +143,29 @@
      }
    ]
  },
  {
    "packet": "Packet_Payload_Field_SizeModifier",
    "tests": [
      {
        "packed": "02",
        "unpacked": {
          "payload": []
        }
      },
      {
        "packed": "070001020304",
        "unpacked": {
          "payload": [
            0,
            1,
            2,
            3,
            4
          ]
        }
      }
    ]
  },
  {
    "packet": "Packet_Payload_Field_UnknownSize",
    "tests": [
@@ -593,6 +616,7 @@
    "packet": "ScalarParent",
    "tests": [
      {
        "packet": "ScalarChild_A",
        "packed": "0001da",
        "unpacked": {
          "a": 0,
@@ -600,6 +624,7 @@
        }
      },
      {
        "packet": "ScalarChild_B",
        "packed": "0102dedc",
        "unpacked": {
          "a": 1,
@@ -607,6 +632,7 @@
        }
      },
      {
        "packet": "AliasedChild_A",
        "packed": "0201d8",
        "unpacked": {
          "a": 2,
@@ -614,6 +640,7 @@
        }
      },
      {
        "packet": "AliasedChild_B",
        "packed": "0302e70a",
        "unpacked": {
          "a": 3,
@@ -626,6 +653,7 @@
    "packet": "EnumParent",
    "tests": [
      {
        "packet": "EnumChild_A",
        "packed": "aabb01dd",
        "unpacked": {
          "a": 43707,
@@ -633,6 +661,7 @@
        }
      },
      {
        "packet": "EnumChild_B",
        "packed": "ccdd02def7",
        "unpacked": {
          "a": 52445,
+38 −0

File changed.

Preview size limit exceeded, changes collapsed.

Loading