Skip to content

Commit d7d19e8

Browse files
authored
Merge pull request #1271 from diffblue/ref1
Verilog: `ref` port direction
2 parents 591ddea + 53d9442 commit d7d19e8

File tree

10 files changed

+80
-36
lines changed

10 files changed

+80
-36
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Verilog: $isunknown
88
* SystemVerilog: fix for #-# and #=# for empty matches
99
* SystemVerilog: fix for |-> and |=> for empty matches
10+
* SystemVerilog: ref module port direction
1011
* LTL/SVA to Buechi with --buechi
1112
* SMV: abs, bool, count, max, min, toint, word1
1213
* BMC: new encoding for F, avoiding spurious traces
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
ref1.sv
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring

regression/verilog/modules/ref1.sv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module M(ref [31:0] some_ref);
2+
initial some_ref = 123;
3+
endmodule
4+
5+
module main;
6+
bit [31:0] some_var;
7+
M m(some_var);
8+
assert final (some_var == 123);
9+
endmodule

src/ebmc/output_verilog.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Author: Daniel Kroening, kroening@kroening.com
1616
#include <verilog/verilog_language.h>
1717
#include <verilog/verilog_synthesis.h>
1818
#include <verilog/verilog_typecheck.h>
19+
#include <verilog/verilog_types.h>
1920

2021
#include "output_verilog.h"
2122

@@ -585,17 +586,17 @@ void output_verilog_baset::module_header(const symbolt &symbol)
585586
{
586587
out << "module " << symbol.base_name;
587588

588-
const irept &ports=symbol.type.find(ID_ports);
589-
589+
const auto &ports = to_module_type(symbol.type).ports();
590+
590591
//
591592
// print port in module statement
592593
//
593-
if(!ports.get_sub().empty())
594+
if(!ports.empty())
594595
{
595596
out << '(';
596597

597598
bool first = true;
598-
for(auto &port : ports.get_sub())
599+
for(auto &port : ports)
599600
{
600601
if(first)
601602
first = false;
@@ -615,10 +616,10 @@ void output_verilog_baset::module_header(const symbolt &symbol)
615616
//
616617
// port declarations
617618
//
618-
for(auto &port : ports.get_sub())
619+
for(auto &port : ports)
619620
{
620-
bool is_input = port.get_bool(ID_input);
621-
bool is_output = port.get_bool(ID_output);
621+
bool is_input = port.input();
622+
bool is_output = port.output();
622623

623624
out << " ";
624625

@@ -629,7 +630,7 @@ void output_verilog_baset::module_header(const symbolt &symbol)
629630
else
630631
out << "output";
631632

632-
const typet &type = static_cast<const typet &>(port.find(ID_type));
633+
const typet &type = port.type();
633634

634635
if(type.id()==ID_named_block)
635636
continue;

src/ebmc/transition_system.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Author: Daniel Kroening, dkr@amazon.com
2222
#include <langapi/mode.h>
2323
#include <trans-word-level/show_module_hierarchy.h>
2424
#include <trans-word-level/show_modules.h>
25+
#include <verilog/verilog_types.h>
2526

2627
#include "ebmc_error.h"
2728
#include "ebmc_version.h"
@@ -424,17 +425,14 @@ std::vector<symbol_exprt> transition_systemt::inputs() const
424425
DATA_INVARIANT(
425426
module_symbol.type.id() == ID_module, "main_symbol must be module");
426427

427-
const auto &ports_irep = module_symbol.type.find(ID_ports);
428-
429428
// filter out the inputs
430-
auto &ports = static_cast<const exprt &>(ports_irep).operands();
429+
auto &ports = to_module_type(module_symbol.type).ports();
431430
for(auto &port : ports)
432431
{
433432
DATA_INVARIANT(port.id() == ID_symbol, "port must be a symbol");
434-
if(port.get_bool(ID_input) && !port.get_bool(ID_output))
433+
if(port.input() && !port.output())
435434
{
436-
symbol_exprt input_symbol(
437-
to_symbol_expr(port).get_identifier(), port.type());
435+
symbol_exprt input_symbol(port.identifier(), port.type());
438436
input_symbol.add_source_location() = port.source_location();
439437
inputs.push_back(std::move(input_symbol));
440438
}

src/hw_cbmc_irep_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ IREP_ID_ONE(module_instance)
107107
IREP_ID_TWO(C_const, #const)
108108
IREP_ID_TWO(C_offset, #offset)
109109
IREP_ID_TWO(C_increasing, #increasing)
110+
IREP_ID_ONE(direction)
110111
IREP_ID_ONE(ports)
111112
IREP_ID_ONE(inst)
112113
IREP_ID_ONE(Verilog)

src/verilog/verilog_elaborate.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ void verilog_typecheckt::collect_port_symbols(const verilog_declt &decl)
5656
new_symbol.is_input = true;
5757
new_symbol.is_output = true;
5858
}
59+
else if(port_class == ID_verilog_ref)
60+
{
61+
new_symbol.is_input = false;
62+
new_symbol.is_output = false;
63+
new_symbol.is_state_var = true;
64+
}
65+
else
66+
DATA_INVARIANT(false, "unexpected port class");
5967

6068
new_symbol.module = module_identifier;
6169
new_symbol.value.make_nil();

src/verilog/verilog_interfaces.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,24 @@ void verilog_typecheckt::check_module_ports(
9292
<< "port `" << base_name << "' not declared";
9393
}
9494

95-
if(!port_symbol->is_input && !port_symbol->is_output)
95+
irep_idt direction = decl.get_class();
96+
97+
if(direction.empty())
9698
{
97-
throw errort().with_location(declarator.source_location())
98-
<< "port `" << base_name << "' not declared as input or output";
99+
if(!port_symbol->is_input && !port_symbol->is_output)
100+
{
101+
throw errort().with_location(declarator.source_location())
102+
<< "port `" << base_name << "' not declared as input or output";
103+
}
104+
else if(port_symbol->is_input && !port_symbol->is_output)
105+
direction = ID_input;
106+
else if(!port_symbol->is_input && port_symbol->is_output)
107+
direction = ID_output;
108+
else
109+
direction = ID_inout;
99110
}
100111

101-
ports.emplace_back(
102-
identifier,
103-
port_symbol->type,
104-
port_symbol->is_input,
105-
port_symbol->is_output);
112+
ports.emplace_back(identifier, port_symbol->type, direction);
106113

107114
ports.back().set("#name", base_name);
108115
ports.back().set(ID_C_source_location, declarator.source_location());

src/verilog/verilog_typecheck.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ void verilog_typecheckt::typecheck_port_connection(
4343
{
4444
const symbolt &symbol = ns.lookup(port.identifier());
4545

46-
if(!symbol.is_input && !symbol.is_output)
46+
if(port.direction() != ID_verilog_ref)
4747
{
48-
throw errort().with_location(op.source_location())
49-
<< "port `" << symbol.name << "' is neither input nor output";
48+
if(!symbol.is_input && !symbol.is_output)
49+
{
50+
throw errort().with_location(op.source_location())
51+
<< "port `" << symbol.name << "' is neither input nor output";
52+
}
5053
}
5154

5255
if(op.is_nil())

src/verilog/verilog_types.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,12 @@ class module_typet:public typet
145145
class portt : public irept
146146
{
147147
public:
148-
portt(irep_idt _identifier, typet _type, bool _input, bool _output)
148+
portt(irep_idt _identifier, typet _type, irep_idt _direction)
149149
: irept(ID_symbol)
150150
{
151-
type() = std::move(_type);
152151
identifier(_identifier);
153-
input(_input);
154-
output(_output);
152+
type() = std::move(_type);
153+
direction(_direction);
155154
}
156155

157156
typet &type()
@@ -174,24 +173,34 @@ class module_typet:public typet
174173
set(ID_identifier, _identifier);
175174
}
176175

177-
bool output() const
176+
irep_idt direction() const
178177
{
179-
return get_bool(ID_output);
178+
return get(ID_direction);
180179
}
181180

182-
void output(bool _output)
181+
void direction(irep_idt _direction)
183182
{
184-
set(ID_output, _output);
183+
set(ID_direction, _direction);
184+
}
185+
186+
bool output() const
187+
{
188+
return direction() == ID_output || direction() == ID_inout;
185189
}
186190

187191
bool input() const
188192
{
189-
return get_bool(ID_input);
193+
return direction() == ID_input || direction() == ID_inout;
190194
}
191195

192-
void input(bool _input)
196+
bool ref() const
197+
{
198+
return direction() == ID_verilog_ref;
199+
}
200+
201+
const source_locationt &source_location() const
193202
{
194-
set(ID_input, _input);
203+
return (const source_locationt &)find(ID_C_source_location);
195204
}
196205
};
197206

0 commit comments

Comments
 (0)