Did anyone ever use the Parser as a stand-alone SV parser in a C++ project?
Added by Jan Seyler about 1 year ago
Hey there,
did anyone ever use the Parser of Verilog-Perl outside of Verilog-Perl in an autonomous C++ application? I'm trying to do this right now and I have some problems creating an instance of the Parser and using it.
It would be great if someone could help me!
Thanking you in anticipation
Replies (64)
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
Not to my knowledge, but I've seen pieces of it's grammar in other projects, with everything else redone around it. Note also it's a superset of the one in Verilator uses which is all C++.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hey,
basically that is exactly what I want. Using the grammar, making a parser out of it and use this in my programm. Lets see, what I can pull out of it :D
Thank you! Especially for the code and the project itself
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hi,
now as I am trying to get things done I encountered some problems. Maybe someone can help me with this:
1) When I am trying to recompile VParseLex.l and VParseBison.y, flex is working but VParseBison.y is not compiling. I assume I need to call bisonpre first? 2) When I use all the cpp code, that is already created and create a file like the one attached (I just want to create a file, where I can invoke the callbacks, because I am trying to use them later on) and compile it like this:
g++ My_Parser.cpp VFileLine.o VParse.o Parser_cleaned.o VParseLex.o VParseBison.o VSymTable.o VAst.o -fpermissive -o My_Parser
I am getting a lot of errors concerning AV and others in VSymTable and VAst like:
VSymTable.cpp: In static member function ‘static void VSymStack::selftest()’: VSymTable.cpp:118:5: error: ‘AV’ was not declared in this scope VSymTable.cpp:118:9: error: ‘topavp’ was not declared in this scope VSymTable.cpp:118:24: error: ‘newAV’ was not declared in this scope VSymTable.cpp:165:20: error: ‘av_undef’ was not declared in this scope ...
Did I forget something to link? I don't see my mistake (shame on me)
Thanking you in anticipation
My_Parser.cpp
(15.6 KB)
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
1. Yes, you need bisonpre.
2. The references to AV are part of the perl library. VAst is making a symbol table, using the perl data structures of hashes (HV) and array (AV). You'll need to either link with perl or provide different data structures to do something similar (Verilator for example makes an AST tree instead). Unfortunately it can't parse SystemVerilog efficiently without resolving some data type symbols.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
P.S. When you get this working, if you're willing, I'll take it back and keep it working moving forward as an example.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hi,
thank you for the reply! In the Verilator Parser, there are no callback I could use, right? Basically this parser is generating a netlist, isn't it? Is there a way to use the callback from Verilog-Perl in the Verilator Parser?
I guess, I will try to embedd an own data structure or port the AST Tree from Verilator. What kind of information exactly do you store in the AST Tree?
And yes, if I get this working you can use it as an example.
Regards
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
The Verilator parser only does a subset of the language; I think you're better off just looking at how it does the symbol table rather than trying to copy code directly from it.
WRT the symbol table, all that is really needed to get the Verilog-Perl code to work is roughly replace the guts for HV and AV with map and dequeue. See "man perlguts" for what these structures do. Basicially, it's just making functions to answer "in scope Y, add identifer FOO as a TYPEDEF". Then later "in scope X which is under scope Y, what is identifier FOO" and it returns "TYPEDEF".
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hey,
thanks for the reply, again. I will try to use Inline::C and if this is not working do something myself.
What do you store in the AST Tree in the Verilator btw? What kind of information do you pull out of the SV-File?
Regards
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
Verilator compiles a subset of the language, all of which goes through the AST; download the kit and look at src/V3AstNodes.h.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
So basically a net list and variables, right?
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hi,
what exactly is addStmtp() doing? I see, it is calling addOp2p() which is calling addNext() or op2p(). But what is the meaning of the abbreviations Stmtp and assOp2p?
What I am trying to do is either porting the AstTree from the verilator code to work with the verilog-perl code or extend the verilator grammar to support full SV functionality. Another possibility is still porting the perl datastructures to C++.
Thank you
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
Each AST node has up to 4 children. It can also be a member of a doubly linked list. For example an AstOR has a left-hand-side and right-hand-side child, lhsp and rhsp, which happen to map to child 1 and 2. An AstModule's addStmtp adds to child 2, which is the list of statements under that module. If you run with --debug you'll get .tree files which are a text description well illustrating this layout; the leading 1/2/3/4 indicate the child numbers. More detail is in internals.txt.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hi,
I came so far, that it is compiling without errors. I derived a new class My_Parser from VParse and created VFileLineParseXs as a new file. I attached the files. Now there is no error while compiling. But how do you use or call the parser with a file? Is there something liek parser->parseFile(filename)?
Thank you so much for all the answers!
Main.cpp
(815 Bytes)
My_Parser.cpp
(14.5 KB)
Makefile (623 Bytes)
My_Parser.h
(3.32 KB)
VFileLineParseXs.cpp
(298 Bytes)
VFileLineParseXs.h
(1001 Bytes)
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
If you look at Parser.pm you'll see the Perl methods. These call methods (_use_cb for example) which are the C routines defined in Parser.xs; some other methods call straight through to the Parser.xs file. So looking at Parser.pm there's the parse function, tracing down this just calls ParserObject->parse().
Note also you'll probably need to preprocess the code to have any usefulness, and read in the std:: package (see Parser.xs). You can preprocess outside of your tool with vppreproc, or use the Preproc C class (which unfortunately is also not well isolated for C standalone use.)
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
BTW long term I'd be happy to move towards a better isolation between the C++ parser and the Perl one, so if you see easy changes there that you can provide patches for I'd be receptive. You can see there was some thought of that at the beginning, but it wasn't needed so I didn't keep it up.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hey,
how can I create a av* value? I do always get seg fault after starting my program.
Thank you so much for you help!
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
See "man perlguts", you want "newAV".
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hey,
if I am doing symsp = newAV();
It does compile but I do get a segfault. Do you know why?
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
There are of course many possible reasons. Try compiling/linking with -O0 -ggdb, then run under gdb and post the backtrace if the reason isn't obvious.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hey,
thanks! What I get is:
Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7b1d8de in Perl_newSV_type () from /usr/lib/libperl.so.5.12
So the reason is newAV(). The backtrace is:
#0 0x00007ffff7b1d8de in Perl_newSV_type () from /usr/lib/libperl.so.5.12 #1 0x000000000040ef1b in main (argc=1, argv=0x7fffffffe358) at Main.cpp:11
It would be great if you may help me.
Regards
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
If I change
newAV() to new av; I get a seg fault while creating the parser itself. The backtrace here is:
#0 0x0000000000411974 in My_Parser::cbFileline (this=0x0, filelinep=0x711010) at My_Parser.h:18
#1 0x00000000004128e3 in VFileLineParseXs::error (this=0x711010, msg=...) at VFileLineParseXs.cpp:9
#2 0x0000000000477f3a in VAstEnt::initNetlist (this=0x711040, fl=0x711010) at VAst.cpp:95
#3 0x0000000000476b08 in VSymStack::VSymStack (this=0x7155f8, fl=0x711010, symp=0x711040) at VSymTable.cpp:96
#4 0x0000000000412a4b in VParse::VParse (this=0x715560, filelinep=0x711010, symsp=0x711040, sigParser=false, useUnreadbackFlag=false) at VParse.cpp:42
#5 0x000000000040f4a1 in My_Parser::My_Parser (this=0x715560, filelinep=0x711010, symsp=0x711040, sigparser=false, useUnreadback=false) at My_Parser.h:22
#6 0x000000000040f0b5 in main (argc=1, argv=0x7fffffffe358) at Main.cpp:35
Regards
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Now I removed cbFileLine() for now. The new segfault backtrace is:
#0 0x00007ffff7b2450e in Perl_newSViv () from /usr/lib/libperl.so.5.12
#1 0x000000000047804c in VAstEnt::initAVEnt (avp=0x711040, type=..., parentp=0x0) at VAst.cpp:110
#2 0x0000000000477ec7 in VAstEnt::initNetlist (this=0x711040, fl=0x711010) at VAst.cpp:97
#3 0x0000000000476a34 in VSymStack::VSymStack (this=0x7155f8, fl=0x711010, symp=0x711040) at VSymTable.cpp:96
#4 0x0000000000412977 in VParse::VParse (this=0x715560, filelinep=0x711010, symsp=0x711040, sigParser=false, useUnreadbackFlag=false) at VParse.cpp:42
#5 0x000000000040f461 in My_Parser::My_Parser (this=0x715560, filelinep=0x711010, symsp=0x711040, sigparser=false, useUnreadback=false) at My_Parser.h:22
#6 0x000000000040f075 in main (argc=1, argv=0x7fffffffe358) at Main.cpp:35
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
If you could please post a whole tarball or better have a github area I could checkout it would make my setting up your testcases easier (not that I want to get into that business ;). When I copy what I think the right files are and make I get "VFileLineParseXs.cpp:11: error: 'class My_Parser' has no member named 'call'".
Anyhow looking at the backtrace and the code I suspect you're not initializing perl. See "perlembed" especially what goes in main(); perl_alloc() etc. BTW linking Perl in isn't a particurally stable process as new perl versions change the process. If you want your tool to be stable, I'd suggest calling your own symbol table functions, or abstracting HV/AV functions into C++ classes.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Jan Seyler about 1 year ago
Hey,
there you go: https://github.com/JRS1986/SV_Parser I just took my .sv files out of it, because I'm not sure what I am allowed to release.
Thank you for your help! I really appreciate it.
RE: Did anyone ever use the Parser as a stand-alone SV parser in a C++ project? - Added by Wilson Snyder about 1 year ago
I comment out the second file, make and
$ echo "module x; endmodule" > files/dut.sv $ ./My_Parser Perl init done init done dut.sv opened correctly filelinep created dut.sv start parsing My_Parser::keywordCb called My_Parser::symbolCb called My_Parser::operatorCb called My_Parser::keywordCb called My_Parser::endparseCb called dut.sv parsed
So I think it's working fine for me. Sorry.
![[logo]](/img/veripool_small.png)