Skip to content

Commit

Permalink
Verilog: move array_type method into verilog_typecheck_exprt
Browse files Browse the repository at this point in the history
This moves the verilog_typecheckt::array_type method to
verilog_typecheck_exprt, together with the other type conversion methods.
  • Loading branch information
kroening committed Mar 21, 2024
1 parent de6e7e7 commit 975fb1e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 78 deletions.
74 changes: 0 additions & 74 deletions src/verilog/verilog_typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,6 @@ Author: Daniel Kroening, [email protected]

/*******************************************************************\
Function: verilog_typecheckt::array_type
Inputs:
Outputs:
Purpose:
\*******************************************************************/

array_typet verilog_typecheckt::array_type(
const irept &src,
const typet &element_type)
{
// int whatnot[x:y];
// 'src' is yet to be converted, but 'element_type' is already converted.
PRECONDITION(src.id() == ID_verilog_unpacked_array);

// Unpacked arrays may have a range [x:y],
// or a size [s], equivalent to [0:s-1]
const exprt &range_expr = static_cast<const exprt &>(src.find(ID_range));
const exprt &size_expr = static_cast<const exprt &>(src.find(ID_size));

mp_integer size, offset;
bool little_endian;

if(range_expr.is_not_nil())
{
// these may be negative
mp_integer msb, lsb;
convert_range(range_expr, msb, lsb);
little_endian = (lsb <= msb);
size = (little_endian ? msb - lsb : lsb - msb) + 1;
offset = little_endian ? lsb : msb;
}
else if(size_expr.is_not_nil())
{
little_endian = true;
size = convert_integer_constant_expression(size_expr);
offset = 0;
if(size < 0)
{
throw errort().with_location(size_expr.find_source_location())
<< "array size must be nonnegative";
}
}
else
{
throw errort() << "array must have range or size";
}

const typet src_subtype =
static_cast<const typet &>(src).has_subtype()
? static_cast<const type_with_subtypet &>(src).subtype()
: typet(ID_nil);

typet array_subtype;

// may need to go recursive
if(src_subtype.is_nil())
array_subtype=element_type;
else
array_subtype=array_type(src_subtype, element_type);

const exprt final_size_expr = from_integer(size, integer_typet());
array_typet result(array_subtype, final_size_expr);
result.set(ID_offset, from_integer(offset, integer_typet()));
result.set(ID_C_little_endian, little_endian);

return result;
}

/*******************************************************************\
Function: verilog_typecheckt::typecheck_port_connection
Inputs:
Expand Down
4 changes: 0 additions & 4 deletions src/verilog/verilog_typecheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ class verilog_typecheckt:
void interface_generate_block(const class verilog_generate_blockt &);
void interface_statement(const class verilog_statementt &);

array_typet array_type(
const irept &src,
const typet &element_type);

// type checking

typedef enum { A_CONTINUOUS, A_BLOCKING, A_NON_BLOCKING } vassignt;
Expand Down
1 change: 1 addition & 0 deletions src/verilog/verilog_typecheck_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class verilog_typecheck_exprt:public verilog_typecheck_baset

typet convert_type(const typet &);
typet convert_enum(const class verilog_enum_typet &);
array_typet array_type(const irept &src, const typet &element_type);

void convert_range(
const exprt &range,
Expand Down
73 changes: 73 additions & 0 deletions src/verilog/verilog_typecheck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,79 @@ Author: Daniel Kroening, [email protected]

/*******************************************************************\
Function: verilog_typecheck_exprt::array_type
Inputs:
Outputs:
Purpose:
\*******************************************************************/

array_typet
verilog_typecheck_exprt::array_type(const irept &src, const typet &element_type)
{
// int whatnot[x:y];
// 'src' is yet to be converted, but 'element_type' is already converted.
PRECONDITION(src.id() == ID_verilog_unpacked_array);

// Unpacked arrays may have a range [x:y],
// or a size [s], equivalent to [0:s-1]
const exprt &range_expr = static_cast<const exprt &>(src.find(ID_range));
const exprt &size_expr = static_cast<const exprt &>(src.find(ID_size));

mp_integer size, offset;
bool little_endian;

if(range_expr.is_not_nil())
{
// these may be negative
mp_integer msb, lsb;
convert_range(range_expr, msb, lsb);
little_endian = (lsb <= msb);
size = (little_endian ? msb - lsb : lsb - msb) + 1;
offset = little_endian ? lsb : msb;
}
else if(size_expr.is_not_nil())
{
little_endian = true;
size = convert_integer_constant_expression(size_expr);
offset = 0;
if(size < 0)
{
throw errort().with_location(size_expr.find_source_location())
<< "array size must be nonnegative";
}
}
else
{
throw errort() << "array must have range or size";
}

const typet src_subtype =
static_cast<const typet &>(src).has_subtype()
? static_cast<const type_with_subtypet &>(src).subtype()
: typet(ID_nil);

typet array_subtype;

// may need to go recursive
if(src_subtype.is_nil())
array_subtype = element_type;
else
array_subtype = array_type(src_subtype, element_type);

const exprt final_size_expr = from_integer(size, integer_typet());
array_typet result(array_subtype, final_size_expr);
result.set(ID_offset, from_integer(offset, integer_typet()));
result.set(ID_C_little_endian, little_endian);

return result;
}

/*******************************************************************\
Function: verilog_typecheck_exprt::convert_type
Inputs:
Expand Down

0 comments on commit 975fb1e

Please sign in to comment.