Skip to content

Commit 132151e

Browse files
authored
Merge pull request #436 from diffblue/verilog-ND
Verilog: expand $ND during synthesis, not typechecking
2 parents 3d28944 + 1bfcf28 commit 132151e

File tree

4 files changed

+34
-66
lines changed

4 files changed

+34
-66
lines changed

src/verilog/verilog_synthesis.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,32 @@ exprt verilog_synthesist::expand_function_call(const function_call_exprt &call)
152152
// Is it a 'system function call'?
153153
if(call.is_system_function_call())
154154
{
155-
// Attempt to constant fold.
156-
verilog_typecheck_exprt verilog_typecheck_expr(ns, get_message_handler());
157-
auto result = verilog_typecheck_expr.elaborate_constant_system_function_call(call);
158-
if(!result.is_constant())
155+
auto identifier = to_symbol_expr(call.function()).get_identifier();
156+
if(identifier == "$ND")
159157
{
160-
throw errort().with_location(call.source_location())
161-
<< "cannot synthesise system function " << to_string(call.function());
158+
std::string identifier =
159+
id2string(module) + "::nondet::" + std::to_string(nondet_count++);
160+
161+
auto arguments = call.arguments();
162+
exprt select_one(
163+
ID_constraint_select_one, call.type(), std::move(arguments));
164+
select_one.set(ID_identifier, identifier);
165+
return select_one.with_source_location<exprt>(call);
162166
}
167+
else
168+
{
169+
// Attempt to constant fold.
170+
verilog_typecheck_exprt verilog_typecheck_expr(ns, get_message_handler());
171+
auto result =
172+
verilog_typecheck_expr.elaborate_constant_system_function_call(call);
173+
if(!result.is_constant())
174+
{
175+
throw errort().with_location(call.source_location())
176+
<< "cannot synthesise system function " << to_string(call.function());
177+
}
163178

164-
return result;
179+
return result;
180+
}
165181
}
166182

167183
// check some restrictions

src/verilog/verilog_synthesis_class.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ class verilog_synthesist:
7070

7171
protected:
7272
const optionst &options;
73-
73+
74+
// For $ND(...)
75+
std::size_t nondet_count = 0;
76+
7477
enum class event_guardt { NONE, CLOCK, COMBINATIONAL };
7578

7679
inline std::string as_string(event_guardt g)

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,6 @@ void verilog_typecheck_exprt::propagate_type(
200200
}
201201
}
202202
}
203-
else if(expr.id()==ID_constraint_select_one)
204-
{
205-
expr.type()=type;
206-
207-
Forall_operands(it, expr)
208-
propagate_type(*it, type);
209-
210-
return;
211-
}
212203
}
213204

214205
implicit_typecast(expr, type);
@@ -327,10 +318,6 @@ exprt verilog_typecheck_exprt::convert_expr_rec(exprt expr)
327318
{
328319
return convert_expr_function_call(to_function_call_expr(expr));
329320
}
330-
else if(expr.id()==ID_constraint_select_one)
331-
{
332-
return convert_constraint_select_one(std::move(expr));
333-
}
334321
else
335322
{
336323
std::size_t no_op;
@@ -435,29 +422,6 @@ exprt verilog_typecheck_exprt::convert_expr_function_call(
435422

436423
/*******************************************************************\
437424
438-
Function: verilog_typecheck_exprt::convert_constraint_select_one
439-
440-
Inputs:
441-
442-
Outputs:
443-
444-
Purpose:
445-
446-
\*******************************************************************/
447-
448-
exprt verilog_typecheck_exprt::convert_constraint_select_one(exprt expr)
449-
{
450-
if(expr.operands().size()<2)
451-
{
452-
throw errort().with_location(expr.source_location())
453-
<< "constraint_select_one takes at least two operands";
454-
}
455-
456-
return expr;
457-
}
458-
459-
/*******************************************************************\
460-
461425
Function: verilog_typecheck_exprt::bits
462426
463427
Inputs:
@@ -632,20 +596,9 @@ exprt verilog_typecheck_exprt::convert_system_function(
632596
throw errort().with_location(expr.source_location())
633597
<< "$ND takes at least one argument";
634598
}
635-
636-
if(arguments.size()==1)
637-
return arguments.front(); // remove
638599

639-
std::string identifier=
640-
id2string(module_identifier)+"::nondet::"+std::to_string(nondet_count++);
641-
642-
typet type=arguments.front().type();
643-
644-
exprt select_one(ID_constraint_select_one, type);
645-
select_one.operands()=arguments;
646-
select_one.set(ID_identifier, identifier);
647-
648-
return select_one;
600+
expr.type() = arguments.front().type();
601+
return std::move(expr);
649602
}
650603
else if(identifier == "$bits")
651604
{

src/verilog/verilog_typecheck_expr.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ class verilog_typecheck_exprt:public verilog_typecheck_baset
2626
public:
2727
verilog_typecheck_exprt(
2828
const namespacet &_ns,
29-
message_handlert &_message_handler):
30-
verilog_typecheck_baset(_ns, _message_handler),
31-
nondet_count(0)
29+
message_handlert &_message_handler)
30+
: verilog_typecheck_baset(_ns, _message_handler)
3231
{ }
3332

3433
verilog_typecheck_exprt(
3534
const namespacet &_ns,
3635
const std::string &_module_identifier,
37-
message_handlert &_message_handler):
38-
verilog_typecheck_baset(_ns, _message_handler),
39-
module_identifier(_module_identifier),
40-
nondet_count(0)
36+
message_handlert &_message_handler)
37+
: verilog_typecheck_baset(_ns, _message_handler),
38+
module_identifier(_module_identifier)
4139
{ }
4240

4341
virtual void convert_expr(exprt &expr)
@@ -52,7 +50,6 @@ class verilog_typecheck_exprt:public verilog_typecheck_baset
5250
protected:
5351
irep_idt module_identifier;
5452
irep_idt function_or_task_name;
55-
unsigned nondet_count;
5653

5754
// module_identifier.function.block.base_name
5855
// including the Verilog:: prefix.
@@ -127,7 +124,6 @@ class verilog_typecheck_exprt:public verilog_typecheck_baset
127124
[[nodiscard]] exprt convert_expr_function_call(function_call_exprt);
128125
[[nodiscard]] exprt
129126
convert_system_function(const irep_idt &identifier, function_call_exprt);
130-
[[nodiscard]] exprt convert_constraint_select_one(exprt);
131127
[[nodiscard]] exprt convert_bit_select_expr(binary_exprt);
132128
[[nodiscard]] exprt convert_replication_expr(replication_exprt);
133129
[[nodiscard]] exprt convert_shl_expr(shl_exprt);

0 commit comments

Comments
 (0)