Skip to content

Commit 3e86eac

Browse files
committed
fx
1 parent afc7f58 commit 3e86eac

File tree

1 file changed

+104
-13
lines changed

1 file changed

+104
-13
lines changed

src/verilog/verilog_indexer.cpp

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
1111
#include <util/cmdline.h>
1212
#include <util/suffix.h>
1313
#include <util/unicode.h>
14+
#include <util/cout_message.h>
1415

1516
#include "verilog_parser.h"
1617
#include "verilog_preprocessor.h"
@@ -36,7 +37,16 @@ class verilog_indexer_parsert
3637
protected:
3738
using idt = verilog_indexert::idt;
3839
verilog_indexert &indexer;
39-
void rModule();
40+
41+
// modules, classes, primitives, packages, interfaces
42+
void rModule(verilog_indexert::idt::kindt, int end_token);
43+
44+
void rConstruct(); // always, initial, ...
45+
void rDeclaration(); // reg, wire, input, typedef ...
46+
void rTaskFunction(); // task ... endtask
47+
void rContinuousAssign(); // assign
48+
void rGenerate(); // generate ... endgenerate
49+
void rModuleInstance(); // module instance(...);
4050

4151
struct tokent
4252
{
@@ -50,6 +60,8 @@ class verilog_indexer_parsert
5060
{
5161
return kind == TOK_NON_TYPE_IDENTIFIER;
5262
}
63+
bool operator==(int other) const { return kind == other; }
64+
bool operator!=(int other) const { return kind != other; }
5365
};
5466

5567
const tokent &next_token()
@@ -94,11 +106,17 @@ std::string verilog_indexert::preprocess(const std::string &file_name)
94106
return std::string();
95107
}
96108

97-
null_message_handlert message_handler;
109+
console_message_handlert message_handler;
98110
verilog_preprocessort preprocessor(
99111
in_stream, preprocessed, message_handler, file_name);
100112

101-
preprocessor.preprocessor();
113+
try
114+
{
115+
preprocessor.preprocessor();
116+
}
117+
catch(...)
118+
{
119+
}
102120

103121
return preprocessed.str();
104122
}
@@ -130,26 +148,99 @@ void verilog_indexer_parsert::rDescription()
130148
auto &token = next_token();
131149
if(token.is_eof())
132150
break;
133-
if(token.text == "module")
134-
rModule();
151+
if(token == TOK_MODULE)
152+
rModule(verilog_indexert::idt::MODULE, TOK_ENDMODULE);
153+
else if(token == TOK_CLASS)
154+
rModule(verilog_indexert::idt::CLASS, TOK_ENDCLASS);
155+
else if(token == TOK_PRIMITIVE)
156+
rModule(verilog_indexert::idt::PRIMITIVE, TOK_ENDPRIMITIVE);
157+
else if(token == TOK_INTERFACE)
158+
rModule(verilog_indexert::idt::INTERFACE, TOK_ENDINTERFACE);
159+
else if(token == TOK_PACKAGE)
160+
rModule(verilog_indexert::idt::PACKAGE, TOK_ENDPACKAGE);
161+
else if(token == TOK_TYPEDEF)
162+
rDeclaration();
135163
}
136164
}
137165

138-
void verilog_indexer_parsert::rModule()
166+
/// Covers the various 'definition-like' constructs in SystemVerilog, i.e.,
167+
/// modules, interfaces, classes, primitives, packages
168+
void verilog_indexer_parsert::rModule(verilog_indexert::idt::kindt kind, int end_token)
139169
{
140170
auto name = next_token();
141-
if(name.is_identifier())
171+
if(!name.is_identifier())
172+
return; // give up
173+
174+
idt id;
175+
id.kind = verilog_indexert::idt::MODULE;
176+
id.name = name.text;
177+
id.file_name = verilog_parser.get_file();
178+
indexer.add(std::move(id));
179+
180+
// now look for the 'endmodule', given as end_token
181+
while(peek() != YYEOF)
142182
{
143-
idt id;
144-
id.kind = verilog_indexert::idt::MODULE;
145-
id.name = name.text;
146-
id.file_name = verilog_parser.get_file();
147-
indexer.add(std::move(id));
183+
auto &token = peek();
184+
if(token == end_token)
185+
{
186+
next_token();
187+
break; // done with the module
188+
}
189+
else if(token == TOK_ALWAYS || token == TOK_ALWAYS_FF || token == TOK_ALWAYS_COMB || token == TOK_ALWAYS_LATCH || token == TOK_FINAL)
190+
rConstruct();
191+
else if(token == TOK_VAR || token == TOK_WIRE || token == TOK_REG || token == TOK_INPUT || token == TOK_INOUT || token == TOK_OUTPUT || token == TOK_GENVAR || token == TOK_TYPEDEF || token == TOK_ENUM)
192+
rDeclaration();
193+
else if(token == TOK_FUNCTION || token == TOK_TASK)
194+
rTaskFunction();
195+
else if(token == TOK_ASSIGN)
196+
rContinuousAssign();
197+
else if(token == TOK_GENERATE)
198+
rGenerate();
199+
else if(token.is_identifier())
200+
{
201+
// declaration, label, module instance
202+
rModuleInstance();
203+
}
204+
else
205+
{
206+
// something else
207+
next_token();
208+
std::cout << "ELSE: " << token.text << "\n";
209+
}
148210
}
149211
}
150212

151-
std::vector<std::filesystem::path> verilog_files()
213+
void verilog_indexer_parsert::rConstruct()
152214
{
215+
next_token();
216+
}
217+
218+
void verilog_indexer_parsert::rDeclaration()
219+
{
220+
next_token();
221+
}
222+
223+
void verilog_indexer_parsert::rTaskFunction()
224+
{
225+
next_token();
226+
}
227+
228+
void verilog_indexer_parsert::rContinuousAssign()
229+
{
230+
next_token();
231+
}
232+
233+
void verilog_indexer_parsert::rGenerate()
234+
{
235+
next_token();
236+
}
237+
238+
void verilog_indexer_parsert::rModuleInstance()
239+
{
240+
next_token();
241+
}
242+
243+
std::vector<std::filesystem::path> verilog_files() {
153244
std::vector<std::filesystem::path> result;
154245

155246
auto current = std::filesystem::current_path();

0 commit comments

Comments
 (0)