Skip to content

Commit

Permalink
codec: fix "prefix of array attribute must be an object name" (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
eine authored Mar 26, 2020
1 parent f1c4102 commit 3124d97
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 77 deletions.
26 changes: 13 additions & 13 deletions vunit/com/codec_vhdl_array_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
function encode (
constant data : $type)
return string is
constant length : positive := encode(data(data'left))'length;
constant length : positive := get_encoded_length(encode(data(data'left)));
variable index : positive := 1;
variable ret_val : string(1 to data'length * length);
begin
Expand Down Expand Up @@ -233,7 +233,7 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
function encode (
constant data : $type)
return string is
constant length : positive := encode(data(data'left(1), data'left(2)))'length;
constant length : positive := get_encoded_length(encode(data(data'left(1), data'left(2))));
variable index : positive := 1;
variable ret_val : string(1 to data'length(1) * data'length(2) * length);
begin
Expand Down Expand Up @@ -305,11 +305,11 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
if data'length = 0 then
return 0;
else
return encode(data(data'left))'length;
return get_encoded_length(encode(data(data'left)));
end if;
end;
constant length : natural := element_length(data);
constant range_length : positive := encode(data'left)'length;
constant range_length : positive := get_encoded_length(encode(data'left));
variable index : positive := 2 + 2 * range_length;
variable ret_val : string(1 to 1 + 2 * range_length + data'length * length);
begin
Expand All @@ -328,7 +328,7 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
constant code : string;
variable index : inout positive;
variable result : out $array_type) is
constant range_length : positive := encode($range_type'left)'length;
constant range_length : positive := get_encoded_length(encode($range_type'left));
begin
index := index + 1 + 2 * range_length;
for i in result'range loop
Expand All @@ -339,7 +339,7 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
function decode (
constant code : string)
return $array_type is
constant range_length : positive := encode($range_type'left)'length;
constant range_length : positive := get_encoded_length(encode($range_type'left));
function ret_val_range (
constant code : string)
return $array_type is
Expand Down Expand Up @@ -401,12 +401,12 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
if data'length(1) * data'length(2) = 0 then
return 0;
else
return encode(data(data'left(1), data'left(2)))'length;
return get_encoded_length(encode(data(data'left(1), data'left(2))));
end if;
end;
constant length : natural := element_length(data);
constant range1_length : positive := encode(data'left(1))'length;
constant range2_length : positive := encode(data'left(2))'length;
constant range1_length : positive := get_encoded_length(encode(data'left(1)));
constant range2_length : positive := get_encoded_length(encode(data'left(2)));
variable index : positive := 3 + 2 * range1_length + 2 * range2_length;
variable ret_val : string(1 to 2 + 2 * range1_length + 2 * range2_length +
data'length(1) * data'length(2) * length);
Expand All @@ -428,8 +428,8 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
constant code : string;
variable index : inout positive;
variable result : out $array_type) is
constant range1_length : positive := encode($range_type1'left)'length;
constant range2_length : positive := encode($range_type2'left)'length;
constant range1_length : positive := get_encoded_length(encode($range_type1'left));
constant range2_length : positive := get_encoded_length(encode($range_type2'left));
begin
index := index + 2 + 2 * range1_length + 2 * range2_length;
for i in result'range(1) loop
Expand All @@ -442,8 +442,8 @@ class ArrayCodecTemplate(DatatypeCodecTemplate):
function decode (
constant code : string)
return $array_type is
constant range1_length : positive := encode($range_type1'left)'length;
constant range2_length : positive := encode($range_type2'left)'length;
constant range1_length : positive := get_encoded_length(encode($range_type1'left));
constant range2_length : positive := get_encoded_length(encode($range_type2'left));
function ret_val_range (
constant code : string)
return $array_type is
Expand Down
7 changes: 6 additions & 1 deletion vunit/com/codec_vhdl_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ def _generate_array_codec_and_to_string_functions(self):
"""Generate codecs and to_string functions for all array data types."""

declarations = ""
definitions = ""
definitions = """
-- Helper function to make tests pass GHDL v0.37
function get_encoded_length ( constant vec: string ) return integer is
begin return vec'length; end;
"""
for array in self.array_types:
(
new_declarations,
Expand Down
72 changes: 43 additions & 29 deletions vunit/vhdl/data_types/test/tb_codec-2008p.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,30 @@ begin
variable real_vector_5_downto_3 : real_vector(5 downto 3);
variable time_vector_5_downto_3 : time_vector(5 downto 3);

-- Temp variables to make test case pass Riviera-PRO 2016.10
variable range_left, range_right : integer;
-- Helper functions to make tests pass GHDL v0.37 and Riviera-PRO 2016.10
function get_decoded_range_left ( constant vec: boolean_vector ) return integer is
begin return vec'left; end;

function get_decoded_range_right ( constant vec: boolean_vector ) return integer is
begin return vec'right; end;

function get_decoded_range_left ( constant vec: integer_vector ) return integer is
begin return vec'left; end;

function get_decoded_range_right ( constant vec: integer_vector ) return integer is
begin return vec'right; end;

function get_decoded_range_left ( constant vec: real_vector ) return integer is
begin return vec'left; end;

function get_decoded_range_right ( constant vec: real_vector ) return integer is
begin return vec'right; end;

function get_decoded_range_left ( constant vec: time_vector ) return integer is
begin return vec'left; end;

function get_decoded_range_right ( constant vec: time_vector ) return integer is
begin return vec'right; end;

begin
test_runner_setup(runner, runner_cfg);
Expand All @@ -63,57 +85,49 @@ begin
if run("Test that boolean_vector can be encoded and decoded") then
boolean_vector_5_downto_3 := (true, false, true);
check_relation(decode_boolean_vector(encode_boolean_vector((true, false, true))) = boolean_vector'(true, false, true));
check_relation(decode_boolean_vector(encode_boolean_vector((0 => true))) = boolean_vector'(0 => true));
check_relation(decode_boolean_vector(encode_boolean_vector((0 => true))) = boolean_vector'(0 => true));
check_relation(decode_boolean_vector(encode_boolean_vector(null_boolean_vector)) = null_boolean_vector);
check_relation(decode_boolean_vector(encode_boolean_vector(boolean_vector_5_downto_3)) = boolean_vector'(true, false, true));
range_left := decode_boolean_vector(encode_boolean_vector(boolean_vector_5_downto_3))'left;
range_right := decode_boolean_vector(encode_boolean_vector(boolean_vector_5_downto_3))'right;
check_relation(range_left = 5);
check_relation(range_right = 3);
check_relation(get_decoded_range_left(decode_boolean_vector(encode_boolean_vector(boolean_vector_5_downto_3))) = 5);
check_relation(get_decoded_range_right(decode_boolean_vector(encode_boolean_vector(boolean_vector_5_downto_3))) = 3);
elsif run("Test that integer_vector can be encoded and decoded") then
integer_vector_5_downto_3 := (-42, 0, 17);
check_relation(decode_integer_vector(encode_integer_vector((-2147483648, -2147483648, -2147483648))) = integer_vector'(-2147483648, -2147483648, -2147483648));
check_relation(decode_integer_vector(encode_integer_vector((-42, 0, 17))) = integer_vector'(-42, 0, 17));
check_relation(decode_integer_vector(encode_integer_vector((0 => -42))) = integer_vector'(0 => -42));
check_relation(decode_integer_vector(encode_integer_vector((0 => -42))) = integer_vector'(0 => -42));
check_relation(decode_integer_vector(encode_integer_vector(null_integer_vector)) = null_integer_vector);
check_relation(decode_integer_vector(encode_integer_vector(integer_vector_5_downto_3)) = integer_vector'(-42, 0, 17));
range_left := decode_integer_vector(encode_integer_vector(integer_vector_5_downto_3))'left;
range_right := decode_integer_vector(encode_integer_vector(integer_vector_5_downto_3))'right;
check_relation(range_left = 5);
check_relation(range_right = 3);
check_relation(get_decoded_range_left(decode_integer_vector(encode_integer_vector(integer_vector_5_downto_3))) = 5);
check_relation(get_decoded_range_right(decode_integer_vector(encode_integer_vector(integer_vector_5_downto_3))) = 3);
elsif run("Test that real_vector can be encoded and decoded") then
real_vector_5_downto_3 := (-42.42, 0.001, 17.17);
check_relation(decode_real_vector(encode_real_vector((-42.42, 0.001, 17.17))) = real_vector'(-42.42, 0.001, 17.17));
check_relation(decode_real_vector(encode_real_vector((0 => -42.42))) = real_vector'(0 => -42.42));
check_relation(decode_real_vector(encode_real_vector(null_real_vector)) = null_real_vector);
check_relation(decode_real_vector(encode_real_vector(real_vector_5_downto_3)) = real_vector'(-42.42, 0.001, 17.17));
range_left := decode_real_vector(encode_real_vector(real_vector_5_downto_3))'left;
range_right := decode_real_vector(encode_real_vector(real_vector_5_downto_3))'right;
check_relation(range_left = 5);
check_relation(range_right = 3);
check_relation(get_decoded_range_left(decode_real_vector(encode_real_vector(real_vector_5_downto_3))) = 5);
check_relation(get_decoded_range_right(decode_real_vector(encode_real_vector(real_vector_5_downto_3))) = 3);
elsif run("Test that time_vector can be encoded and decoded") then
time_vector_5_downto_3 := (-42 ms, 0 sec, 17 min);
check_relation(decode_time_vector(encode_time_vector((-42 ms, 0 sec, 17 min))) = time_vector'(-42 ms, 0 sec, 17 min));
check_relation(decode_time_vector(encode_time_vector((0 => -42 ms))) = time_vector'(0 => -42 ms));
check_relation(decode_time_vector(encode_time_vector((0 => -42 ms))) = time_vector'(0 => -42 ms));
check_relation(decode_time_vector(encode_time_vector(null_time_vector)) = null_time_vector);
check_relation(decode_time_vector(encode_time_vector(time_vector_5_downto_3)) = time_vector'(-42 ms, 0 sec, 17 min));
range_left := decode_time_vector(encode_time_vector(time_vector_5_downto_3))'left;
range_right := decode_time_vector(encode_time_vector(time_vector_5_downto_3))'right;
check_relation(range_left = 5);
check_relation(range_right = 3);
check_relation(get_decoded_range_left(decode_time_vector(encode_time_vector(time_vector_5_downto_3))) = 5);
check_relation(get_decoded_range_right(decode_time_vector(encode_time_vector(time_vector_5_downto_3))) = 3);
elsif run("Test that ufixed can be encoded and decoded") then
check_relation(decode_ufixed(encode_ufixed(to_ufixed(6.5, 3, -3))) = to_ufixed(6.5, 3, -3));
check_relation(decode_ufixed(encode_ufixed(to_ufixed(8.0, 3, 1))) = to_ufixed(8.0, 3, 1));
check_relation(decode_ufixed(encode_ufixed(to_ufixed( 6.5, 3, -3))) = to_ufixed(6.5, 3, -3));
check_relation(decode_ufixed(encode_ufixed(to_ufixed( 8.0, 3, 1))) = to_ufixed(8.0, 3, 1));
check_relation(decode_ufixed(encode_ufixed(to_ufixed(0.25, -2, -4))) = to_ufixed(0.25, -2, -4));
elsif run("Test that sfixed can be encoded and decoded") then
check_relation(decode_sfixed(encode_sfixed(to_sfixed(6.5, 3, -3))) = to_sfixed(6.5, 3, -3));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(8.0, 4, 1))) = to_sfixed(8.0, 4, 1));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(0.25, -1, -4))) = to_sfixed(0.25, -1, -4));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(-6.5, 3, -3))) = to_sfixed(-6.5, 3, -3));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(-8.0, 4, 1))) = to_sfixed(-8.0, 4, 1));
check_relation(decode_sfixed(encode_sfixed(to_sfixed( 6.5, 3, -3))) = to_sfixed(6.5, 3, -3));
check_relation(decode_sfixed(encode_sfixed(to_sfixed( 8.0, 4, 1))) = to_sfixed(8.0, 4, 1));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(0.25, -1, -4))) = to_sfixed(0.25, -1, -4));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(-6.5, 3, -3))) = to_sfixed(-6.5, 3, -3));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(-8.0, 4, 1))) = to_sfixed(-8.0, 4, 1));
check_relation(decode_sfixed(encode_sfixed(to_sfixed(-0.25, -1, -4))) = to_sfixed(-0.25, -1, -4));
elsif run("Test that float can be encoded and decoded") then
check_relation(decode_float(encode_float(to_float(real'low, 11, 52))) = to_float(real'low, 11, 52));
check_relation(decode_float(encode_float(to_float(real'low, 11, 52))) = to_float(real'low, 11, 52));
check_relation(decode_float(encode_float(to_float(real'high, 11, 52))) = to_float(real'high, 11, 52));

check_relation(to_string(decode_float(encode_float(positive_zero))) = to_string(positive_zero));
Expand Down
Loading

0 comments on commit 3124d97

Please sign in to comment.