Skip to content

Commit

Permalink
Fix code generation for expressions with values outside type range
Browse files Browse the repository at this point in the history
Ref. #726
  • Loading branch information
treiher committed Aug 12, 2021
1 parent 8dd03b6 commit c3e2a52
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
30 changes: 19 additions & 11 deletions rflx/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3067,17 +3067,25 @@ def __create_to_context_procedure(message: Message) -> UnitPart:
ObjectDeclaration(
[field_size],
const.TYPES_BIT_LENGTH,
Call(
const.TYPES_BIT_LENGTH,
[
link.size.substituted(
lambda x: expr.Variable("Struct" * x.identifier)
if isinstance(x, expr.Variable)
and Field(x.identifier) in message.fields
else x
).ada_expr()
],
),
link.size.substituted(
lambda x: expr.Size(
expr.Variable("Struct" * x.prefix.identifier)
)
if isinstance(x, expr.Size)
and isinstance(x.prefix, expr.Variable)
and Field(x.prefix.identifier) in message.fields
else x
)
.substituted(
lambda x: expr.Call(
const.TYPES_BIT_LENGTH,
[expr.Variable("Struct" * x.identifier)],
)
if isinstance(x, expr.Variable)
and Field(x.identifier) in message.fields
else x
)
.ada_expr(),
constant=True,
)
],
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/specification_model_generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,24 @@ def test_definite_message_with_builtin_type(tmp_path: Path) -> None:
end Test;
"""
utils.assert_compilable_code_string(spec, tmp_path)


def test_message_expression_value_outside_type_range(tmp_path: Path) -> None:
spec = """
package Test is
type Length is mod 2 ** 8;
type Packet is
message
Length_1 : Length;
Length_2 : Length
then Payload
with Size => Length_2 * 256 + Length_1
if (Length_2 * 256 + Length_1) mod 8 = 0;
Payload : Opaque;
end message;
end Test;
"""
utils.assert_compilable_code_string(spec, tmp_path)
2 changes: 1 addition & 1 deletion tests/spark/generated/rflx-sequence-inner_message.adb
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ is
Reset (Ctx);
Set_Length (Ctx, Struct.Length);
declare
Payload_Size : constant RFLX_Types.Bit_Length := RFLX_Types.Bit_Length (Struct.Length * 8);
Payload_Size : constant RFLX_Types.Bit_Length := RFLX_Types.Bit_Length (Struct.Length) * 8;
begin
if Payload_Size = Field_Size (Ctx, F_Payload) then
Set_Payload (Ctx, Struct.Payload (Struct.Payload'First .. Struct.Payload'First + RFLX_Types.Index (RFLX_Types.To_Length (Payload_Size) + 1) - 2));
Expand Down
2 changes: 1 addition & 1 deletion tests/spark/generated/rflx-udp-datagram.adb
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ is
Set_Length (Ctx, Struct.Length);
Set_Checksum (Ctx, Struct.Checksum);
declare
Payload_Size : constant RFLX_Types.Bit_Length := RFLX_Types.Bit_Length ((Struct.Length - 8) * 8);
Payload_Size : constant RFLX_Types.Bit_Length := (RFLX_Types.Bit_Length (Struct.Length) - 8) * 8;
begin
if Payload_Size = Field_Size (Ctx, F_Payload) then
Set_Payload (Ctx, Struct.Payload (Struct.Payload'First .. Struct.Payload'First + RFLX_Types.Index (RFLX_Types.To_Length (Payload_Size) + 1) - 2));
Expand Down

0 comments on commit c3e2a52

Please sign in to comment.