-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verilog: extract .lower() method for part select expressions
This moves the lowering for Verilog's part select expressions into an explicit .lower() method.
- Loading branch information
Showing
3 changed files
with
58 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,10 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include "verilog_expr.h" | ||
|
||
#include <util/arith_tools.h> | ||
#include <util/bitvector_expr.h> | ||
#include <util/bitvector_types.h> | ||
#include <util/mathematical_types.h> | ||
#include <util/prefix.h> | ||
|
||
#include "verilog_typecheck_base.h" | ||
|
@@ -95,3 +99,53 @@ std::vector<irep_idt> verilog_module_sourcet::submodules() const | |
|
||
return result; | ||
} | ||
|
||
exprt verilog_non_indexed_part_select_exprt::lower() const | ||
{ | ||
PRECONDITION(msb().is_constant()); | ||
PRECONDITION(lsb().is_constant()); | ||
|
||
auto op1 = numeric_cast_v<mp_integer>(to_constant_expr(msb())); | ||
auto op2 = numeric_cast_v<mp_integer>(to_constant_expr(lsb())); | ||
|
||
mp_integer this_width = to_bitvector_type(type()).get_width(); | ||
mp_integer this_offset = string2integer(type().get_string(ID_C_offset)); | ||
|
||
// 1800-2017 sec 11.5.1: out-of-bounds bit-select is | ||
// x for 4-state and 0 for 2-state values. We | ||
// achieve that by padding the operand from either end, | ||
// or both. | ||
exprt this_padded = *this; | ||
|
||
if(op2 < this_offset) | ||
{ | ||
// lsb too small, pad below | ||
auto padding_width = this_offset - op2; | ||
auto padding = from_integer( | ||
0, unsignedbv_typet{numeric_cast_v<std::size_t>(padding_width)}); | ||
auto new_type = unsignedbv_typet{numeric_cast_v<std::size_t>( | ||
to_bitvector_type(this_padded.type()).get_width() + padding_width)}; | ||
this_padded = concatenation_exprt(this_padded, padding, new_type); | ||
op2 += padding_width; | ||
op1 += padding_width; | ||
} | ||
|
||
if(op1 >= this_width + this_offset) | ||
{ | ||
// msb too large, pad above | ||
auto padding_width = op1 - (this_width + this_offset) + 1; | ||
auto padding = from_integer( | ||
0, unsignedbv_typet{numeric_cast_v<std::size_t>(padding_width)}); | ||
auto new_type = unsignedbv_typet{numeric_cast_v<std::size_t>( | ||
to_bitvector_type(this_padded.type()).get_width() + padding_width)}; | ||
this_padded = concatenation_exprt(padding, this_padded, new_type); | ||
} | ||
|
||
op2 -= this_offset; | ||
op1 -= this_offset; | ||
|
||
// Construct the extractbits expression | ||
return extractbits_exprt{ | ||
this_padded, from_integer(op2, integer_typet()), type()} | ||
.with_source_location(source_location()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters