Skip to content

Commit

Permalink
Migrate the C templates to use the new BitLengthSet API from OpenCyph…
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko committed Apr 5, 2021
1 parent 34cc926 commit 170dcf7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ package_dir=
=src
packages=find:
install_requires=
pydsdl ~= 1.9
pydsdl @ git+https://github.com/UAVCAN/pydsdl@combinatorial-explosion

zip_safe = False

Expand Down
7 changes: 3 additions & 4 deletions src/nunavut/lang/c/templates/deserialization.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
return -NUNAVUT_ERROR_INVALID_ARGUMENT;
}
{% if t.inner_type.bit_length_set|max > 0 %}
{% if t.inner_type.bit_length_set.max > 0 %}
{{ _deserialize_impl(t) }}
{% else %}
*inout_buffer_size_bytes = 0U;
Expand Down Expand Up @@ -46,7 +46,7 @@
{% for f, offset in t.inner_type.iterate_fields_with_offsets(0) %}
{{ 'if' if loop.first else 'else if' }} ({{ loop.index0 }}U == out_obj->_tag_) // {{ f }}
{
{%- assert f.data_type.alignment_requirement <= (offset|min) %}
{%- assert f.data_type.alignment_requirement <= (offset.min) %}
{{ _deserialize_any(f.data_type, 'out_obj->' + (f|id), offset)|trim|remove_blank_lines|indent }}
}
{%- endfor %}
Expand Down Expand Up @@ -195,10 +195,9 @@

{# COMPUTE THE ARRAY ELEMENT OFFSETS #}
{# NOTICE: The offset is no longer valid at this point because we just emitted the array length prefix. #}
{# TODO: The following transformations are computationally taxing; see https://github.com/UAVCAN/pydsdl/issues/23 #}
{% set element_offset = offset + t.bit_length_set %}
{% set first_element_offset = offset + t.length_field_type.bit_length %}
{% assert (element_offset|min) == (first_element_offset|min) %}
{% assert (element_offset.min) == (first_element_offset.min) %}
{% if first_element_offset.is_aligned_at_byte() %}
{{ assert('offset_bits % 8U == 0U') }}
{% endif %}
Expand Down
40 changes: 20 additions & 20 deletions src/nunavut/lang/c/templates/serialization.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
return -NUNAVUT_ERROR_INVALID_ARGUMENT;
}
{% if t.inner_type.bit_length_set|max > 0 %}
{% if t.inner_type.bit_length_set.max > 0 %}
{{ _serialize_impl(t) }}
{% else %}
*inout_buffer_size_bytes = 0U;
Expand All @@ -31,7 +31,7 @@
{%- if options.enable_override_variable_array_capacity %}
#ifndef {{ t | full_reference_name }}_DISABLE_SERIALIZATION_BUFFER_CHECK_
{% endif %}
if ((8U * ({{ typename_unsigned_bit_length }}) capacity_bytes) < {{ t.inner_type.bit_length_set|max }}UL)
if ((8U * ({{ typename_unsigned_bit_length }}) capacity_bytes) < {{ t.inner_type.bit_length_set.max }}UL)
{
return -NUNAVUT_ERROR_SERIALIZATION_BUFFER_TOO_SMALL;
}
Expand Down Expand Up @@ -62,7 +62,7 @@
{% for f, offset in t.inner_type.iterate_fields_with_offsets(0) %}
{{ 'if' if loop.first else 'else if' }} ({{ loop.index0 }}U == obj->_tag_) // {{ f }}
{
{%- assert f.data_type.alignment_requirement <= (offset|min) %}
{%- assert f.data_type.alignment_requirement <= (offset.min) %}
{{ _serialize_any(f.data_type, 'obj->' + (f|id), offset)|trim|remove_blank_lines|indent }}
}
{%- endfor %}
Expand All @@ -74,11 +74,11 @@
{% endif %}
{{ _pad_to_alignment(t.inner_type.alignment_requirement)|trim|remove_blank_lines }}
// It is assumed that we know the exact type of the serialized entity, hence we expect the size to match.
{% if t.inner_type.bit_length_set|length > 1 %}
{{ assert('offset_bits >= %sULL'|format(t.inner_type.bit_length_set|min)) }}
{{ assert('offset_bits <= %sULL'|format(t.inner_type.bit_length_set|max)) }}
{% if not t.inner_type.bit_length_set.fixed_length %}
{{ assert('offset_bits >= %sULL'|format(t.inner_type.bit_length_set.min)) }}
{{ assert('offset_bits <= %sULL'|format(t.inner_type.bit_length_set.max)) }}
{% else %}
{{ assert('offset_bits == %sULL'|format(t.inner_type.bit_length_set|sum)) }}
{{ assert('offset_bits == %sULL'|format(t.inner_type.bit_length_set.max)) }}
{% endif %}
{{ assert('offset_bits % 8U == 0U') }}
*inout_buffer_size_bytes = ({{ typename_unsigned_length }}) (offset_bits / 8U);
Expand Down Expand Up @@ -117,7 +117,7 @@
{% endif %}
{# NOTICE: If this is a delimited type, we will be requiring the buffer to be at least extent-sized.
# This is a bit wasteful because when serializing we can often use a smaller buffer. #}
{{ assert('(offset_bits + %dULL) <= (capacity_bytes * 8U)'|format(t.bit_length_set|max)) }}
{{ assert('(offset_bits + %dULL) <= (capacity_bytes * 8U)'|format(t.bit_length_set.max)) }}

{% if t is VoidType %} {{- _serialize_void(t, offset) }}
{% elif t is BooleanType %} {{- _serialize_boolean(t, reference, offset) }}
Expand Down Expand Up @@ -312,11 +312,11 @@
}
{% endfor %}
// It is assumed that we know the exact type of the serialized entity, hence we expect the size to match.
{% if t.bit_length_set|length > 1 %}
{{ assert('(offset_bits - %s) >= %sULL'|format(ref_origin_offset, t.bit_length_set|min)) }}
{{ assert('(offset_bits - %s) <= %sULL'|format(ref_origin_offset, t.bit_length_set|max)) }}
{% if not t.bit_length_set.fixed_length %}
{{ assert('(offset_bits - %s) >= %sULL'|format(ref_origin_offset, t.bit_length_set.min)) }}
{{ assert('(offset_bits - %s) <= %sULL'|format(ref_origin_offset, t.bit_length_set.max)) }}
{% else %}
{{ assert('(offset_bits - %s) == %sULL'|format(ref_origin_offset, t.bit_length_set|sum)) }}
{{ assert('(offset_bits - %s) == %sULL'|format(ref_origin_offset, t.bit_length_set.max)) }}
{% endif %}
(void) {{ ref_origin_offset }};
{% endif %}
Expand All @@ -338,7 +338,7 @@
{# TODO: The following transformations are computationally taxing; see https://github.com/UAVCAN/pydsdl/issues/23 #}
{% set element_offset = offset + t.bit_length_set %}
{% set first_element_offset = offset + t.length_field_type.bit_length %}
{% assert (element_offset|min) == (first_element_offset|min) %}
{% assert (element_offset.min) == (first_element_offset.min) %}
{% if first_element_offset.is_aligned_at_byte() %}
{{ assert('offset_bits % 8U == 0U') }}
{% endif %}
Expand Down Expand Up @@ -394,16 +394,16 @@
{% macro _serialize_composite(t, reference, offset) %}
{% set ref_err = 'err' |to_template_unique_name %}
{% set ref_size_bytes = 'size_bytes' |to_template_unique_name %}
{% set is_variable_size = (t.inner_type.bit_length_set|length) > 1 %}
{% set size_bytes = t.inner_type.bit_length_set|max|bits2bytes_ceil %}
{% set is_variable_size = not t.inner_type.bit_length_set.fixed_length %}
{% set size_bytes = t.inner_type.bit_length_set.max|bits2bytes_ceil %}
{{ typename_unsigned_length }} {{ ref_size_bytes }} = {{ size_bytes }}UL; // Nested object (max) size, in bytes.

{# PROLOGUE #}
{% if t is DelimitedType %}
{% if is_variable_size %}
offset_bits += {{ t.delimiter_header_type.bit_length }}U; // Reserve space for the delimiter header.
{% else %}
{% assert size_bytes * 8 == (t.inner_type.bit_length_set|min) == (t.inner_type.bit_length_set|max) %}
{% assert size_bytes * 8 == (t.inner_type.bit_length_set.min) == (t.inner_type.bit_length_set.max) %}
// Constant delimiter header can be written ahead of the nested object.
{{ _serialize_integer(t.delimiter_header_type, ref_size_bytes, offset)|trim }}
{% endif %}
Expand All @@ -419,11 +419,11 @@
return {{ ref_err }};
}
// It is assumed that we know the exact type of the serialized entity, hence we expect the size to match.
{% if t.inner_type.bit_length_set|length > 1 %}
{{ assert('(%s * 8U) >= %sULL'|format(ref_size_bytes, t.inner_type.bit_length_set|min)) }}
{{ assert('(%s * 8U) <= %sULL'|format(ref_size_bytes, t.inner_type.bit_length_set|max)) }}
{% if not t.inner_type.bit_length_set.fixed_length %}
{{ assert('(%s * 8U) >= %sULL'|format(ref_size_bytes, t.inner_type.bit_length_set.min)) }}
{{ assert('(%s * 8U) <= %sULL'|format(ref_size_bytes, t.inner_type.bit_length_set.max)) }}
{% else %}
{{ assert('(%s * 8U) == %sULL'|format(ref_size_bytes, t.inner_type.bit_length_set|sum)) }}
{{ assert('(%s * 8U) == %sULL'|format(ref_size_bytes, t.inner_type.bit_length_set.max)) }}
{% endif %}

{# EPILOGUE #}
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ show-source = True


[pytest]
log_file = pytest.log
log_level = DEBUG
log_cli = true
log_cli_level = WARNING
norecursedirs = submodules
Expand Down

0 comments on commit 170dcf7

Please sign in to comment.