|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Verilog Initializer |
| 4 | +
|
| 5 | +Author: Daniel Kroening, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "verilog_initializer.h" |
| 10 | + |
| 11 | +#include <util/arith_tools.h> |
| 12 | +#include <util/std_expr.h> |
| 13 | + |
| 14 | +std::optional<exprt> verilog_default_initializer(const typet &type) |
| 15 | +{ |
| 16 | + if(type.id() == ID_signedbv || type.id() == ID_unsignedbv) |
| 17 | + return from_integer(0, type); |
| 18 | + else if(type.id() == ID_bool) |
| 19 | + return false_exprt{}; |
| 20 | + else if(type.id() == ID_array) |
| 21 | + { |
| 22 | + auto &array_type = to_array_type(type); |
| 23 | + auto default_element_opt = |
| 24 | + verilog_default_initializer(array_type.element_type()); |
| 25 | + if(!default_element_opt.has_value()) |
| 26 | + return {}; |
| 27 | + else |
| 28 | + return array_of_exprt{*default_element_opt, array_type}; |
| 29 | + } |
| 30 | + else if(type.id() == ID_struct) |
| 31 | + { |
| 32 | + auto &struct_type = to_struct_type(type); |
| 33 | + exprt::operandst member_values; |
| 34 | + for(auto &component : struct_type.components()) |
| 35 | + { |
| 36 | + auto member_value_opt = verilog_default_initializer(component.type()); |
| 37 | + if(!member_value_opt.has_value()) |
| 38 | + return {}; |
| 39 | + member_values.push_back(*member_value_opt); |
| 40 | + } |
| 41 | + return struct_exprt{std::move(member_values), struct_type}; |
| 42 | + } |
| 43 | + else if(type.id() == ID_verilog_chandle) |
| 44 | + { |
| 45 | + return constant_exprt{ID_NULL, type}; |
| 46 | + } |
| 47 | + else |
| 48 | + return {}; |
| 49 | +} |
0 commit comments