Skip to content

Commit

Permalink
SystemVerilog: chandle data type
Browse files Browse the repository at this point in the history
This adds 1800 2017 6.14 chandle.
  • Loading branch information
kroening committed Nov 23, 2024
1 parent 8642dd3 commit a87374e
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 3 deletions.
6 changes: 6 additions & 0 deletions regression/verilog/data-types/chandle1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CORE
chandle1.sv
--bound 0
^EXIT=0$
^SIGNAL=0$
--
9 changes: 9 additions & 0 deletions regression/verilog/data-types/chandle1.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module main;

// IEEE 1800-2017 6.14
chandle some_handle = null;

assert final (!some_handle);
assert final ($typename(some_handle) == "chandle");

endmodule
3 changes: 2 additions & 1 deletion src/hw_cbmc_irep_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ IREP_ID_ONE(verilog_property_declaration)
IREP_ID_ONE(verilog_value_range)
IREP_ID_ONE(verilog_streaming_concatenation_left_to_right)
IREP_ID_ONE(verilog_streaming_concatenation_right_to_left)
IREP_ID_ONE(chandle)
IREP_ID_ONE(verilog_chandle)
IREP_ID_ONE(verilog_null)
IREP_ID_ONE(event)
IREP_ID_ONE(reg)
IREP_ID_ONE(macromodule)
Expand Down
1 change: 1 addition & 0 deletions src/verilog/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SRC = aval_bval_encoding.cpp \
verilog_typecheck_base.cpp \
verilog_typecheck_expr.cpp \
verilog_typecheck_sva.cpp \
verilog_types.cpp \
verilog_y.tab.cpp \
vtype.cpp

Expand Down
4 changes: 4 additions & 0 deletions src/verilog/expr2verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,8 @@ std::string expr2verilogt::convert(const typet &type)

return dest;
}
else if(type.id() == ID_verilog_chandle)
return "chandle";
else if(type.id() == ID_verilog_genvar)
return "genvar";
else if(type.id()==ID_integer)
Expand All @@ -1888,6 +1890,8 @@ std::string expr2verilogt::convert(const typet &type)
return "real";
else if(type.id()==ID_verilog_realtime)
return "realtime";
else if(type.id() == ID_verilog_null)
return "null";
else if(type.id() == ID_verilog_enum)
{
return "enum";
Expand Down
4 changes: 2 additions & 2 deletions src/verilog/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ data_type:
| TOK_STRING
{ init($$, ID_string); }
| TOK_CHANDLE
{ init($$, ID_chandle); }
{ init($$, ID_verilog_chandle); }
| TOK_VIRTUAL interface_opt interface_identifier
{ init($$, "virtual_interface"); }
| /*scope_opt*/ type_identifier packed_dimension_brace
Expand Down Expand Up @@ -3955,7 +3955,7 @@ primary: primary_literal
| cast
| assignment_pattern_expression
| streaming_concatenation
| TOK_NULL { init($$, ID_NULL); }
| TOK_NULL { init($$, ID_verilog_null); }
| TOK_THIS { init($$, ID_this); }
;

Expand Down
4 changes: 4 additions & 0 deletions src/verilog/verilog_elaborate_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ typet verilog_typecheck_exprt::elaborate_type(const typet &src)
{
return src;
}
else if(src.id() == ID_verilog_chandle)
{
return src;
}
else
{
throw errort().with_location(source_location)
Expand Down
16 changes: 16 additions & 0 deletions src/verilog/verilog_lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
#include "aval_bval_encoding.h"
#include "verilog_bits.h"
#include "verilog_expr.h"
#include "verilog_types.h"

exprt extract(
const exprt &src,
Expand Down Expand Up @@ -157,9 +158,24 @@ exprt verilog_lowering(exprt expr)
{
return lower_to_aval_bval(to_constant_expr(expr));
}
else if(expr.type().id() == ID_verilog_chandle)
{
// this is 'null'
return to_verilog_chandle_type(expr.type()).null_expr();
}

return expr;
}
else if(expr.id() == ID_symbol)
{
auto &symbol_expr = to_symbol_expr(expr);
if(expr.type().id() == ID_verilog_chandle)
{
return symbol_exprt{symbol_expr.get_identifier(), unsignedbv_typet{32}};
}
else
return expr;
}
else if(expr.id() == ID_concatenation)
{
if(
Expand Down
16 changes: 16 additions & 0 deletions src/verilog/verilog_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,11 @@ exprt verilog_typecheck_exprt::convert_nullary_expr(nullary_exprt expr)
throw errort().with_location(expr.source_location())
<< "'this' outside of method";
}
else if(expr.id() == ID_verilog_null)
{
return constant_exprt{ID_NULL, typet{ID_verilog_null}}.with_source_location(
expr.source_location());
}
else
{
throw errort().with_location(expr.source_location())
Expand Down Expand Up @@ -2027,6 +2032,17 @@ void verilog_typecheck_exprt::implicit_typecast(
return;
}
}
else if(src_type.id() == ID_verilog_null)
{
if(dest_type.id() == ID_verilog_chandle)
{
if(expr.id() == ID_constant)
{
expr.type() = dest_type;
return;
}
}
}

throw errort().with_location(expr.source_location())
<< "failed to convert `" << to_string(src_type) << "' to `"
Expand Down
16 changes: 16 additions & 0 deletions src/verilog/verilog_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************\
Module: Verilog Types
Author: Daniel Kroening, [email protected]
\*******************************************************************/

#include "verilog_types.h"

#include <util/std_expr.h>

constant_exprt verilog_chandle_typet::null_expr() const
{
return encoding().all_zeros_expr();
}
37 changes: 37 additions & 0 deletions src/verilog/verilog_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,41 @@ inline verilog_union_typet &to_verilog_union_type(typet &type)
return static_cast<verilog_union_typet &>(type);
}

/// a pointer type
class verilog_chandle_typet : public typet
{
public:
inline verilog_chandle_typet() : typet(ID_verilog_chandle)
{
}

bv_typet encoding() const
{
return bv_typet{32};
}

constant_exprt null_expr() const;
};

/// \brief Cast a typet to a \ref verilog_chandle_typet
///
/// This is an unchecked conversion. \a type must be known to be \ref
/// verilog_chandle_typet. Will fail with a precondition violation if type
/// doesn't match.
///
/// \param type: Source type.
/// \return Object of type \ref verilog_chandle_typet
inline const verilog_chandle_typet &to_verilog_chandle_type(const typet &type)
{
PRECONDITION(type.id() == ID_verilog_chandle);
return static_cast<const verilog_chandle_typet &>(type);
}

/// \copydoc to_chandle_type(const typet &)
inline verilog_chandle_typet &to_verilog_chandle_type(typet &type)
{
PRECONDITION(type.id() == ID_verilog_chandle);
return static_cast<verilog_chandle_typet &>(type);
}

#endif

0 comments on commit a87374e

Please sign in to comment.