Project

General

Profile

Getting Started » History » Version 4

Alexander Kamkin, 05/06/2014 08:35 AM

1 1 Alexander Kamkin
h1. Getting Started
2
3 4 Alexander Kamkin
This is a step-by-step instruction for getting started with developing a "VeriTrans":http://forge.ispras.ru/projects/veritrans backend and using it within the Verilog translator environment. The term _backend_ refers to a component that traverses an _abstract syntax tree_ (_AST_) of the Verilog description and processes it in some way (e.g., constructs the internal representation and/or converts the description into some other language). The document is illustrated by the example of @VerilogPrinter@ (see the package @ru.ispras.verilog.parser.sample@).
4 3 Alexander Kamkin
5 1 Alexander Kamkin
h2. Developing a Backend
6
7 3 Alexander Kamkin
Technically, a backend is a Java object that implements the @VerilogBackend@ interface (the method @start@). Here is an example:
8 1 Alexander Kamkin
9 3 Alexander Kamkin
<pre><code class="java">
10 1 Alexander Kamkin
import ru.ispras.verilog.parser.VerilogBackend;
11
import ru.ispras.verilog.parser.model.*;
12 3 Alexander Kamkin
...
13 1 Alexander Kamkin
14
/**
15 3 Alexander Kamkin
 * This class illustrates development of a Verilog backend.
16 1 Alexander Kamkin
 */
17
public final class VerilogPrinter extends VerilogBackend
18
{
19
    /**
20 3 Alexander Kamkin
     * Processes the abstract syntax tree (AST).
21
     *
22
     * @param root the AST''s root.
23 1 Alexander Kamkin
     */
24
    public void start(final VerilogNode root)
25
    {
26 3 Alexander Kamkin
        ...
27 1 Alexander Kamkin
    }
28 3 Alexander Kamkin
}
29
</code></pre>
30
31 4 Alexander Kamkin
To ease development of a backend, one can use @VerilogTreeWalker@, a "VeriTrans":http://forge.ispras.ru/projects/veritrans class that implements AST traversal. The @VerilogTreeWalker@''s constructor takes two parameters: (1) a reference to the tree''s root and (2) a visitor to be applied to the tree nodes:
32 3 Alexander Kamkin
33 1 Alexander Kamkin
<pre><code class="java">
34 3 Alexander Kamkin
import ru.ispras.verilog.parser.walker.*;
35 4 Alexander Kamkin
...
36 3 Alexander Kamkin
37 1 Alexander Kamkin
public void start(final VerilogNode root)
38 3 Alexander Kamkin
{
39 4 Alexander Kamkin
    // Create the AST traverser.
40 3 Alexander Kamkin
    VerilogTreeWalker walker = new VerilogTreeWalker(root, new VerilogNodePrinter());
41
    walker.start();
42 1 Alexander Kamkin
}
43
</code></pre>
44 4 Alexander Kamkin
45
The most substantial part of backend development concerns creation of the AST nodes’ visitor, a subclass of the abstract class @VerilogNodeVisitor@. The visitor should implement two methods for each of the node types:
46
47
<pre><code class="java">
48
// Pre-visitor: it is invoked before the child nodes are visited.
49
public void on<NodeType>Begin (final <NodeType> node);
50
51
// Post-visitor: it is invoked after the child nodes are visited.
52
public void on<NodeType>End   (final <NodeType> node);
53
</code></pre>
54
55
Supported node types include:
56
57
* Activity
58
* AssignBegin
59
* AssignStatement
60
* Assignment
61
* Attribute
62
* BlockGenerate
63
* BlockStatement
64
* CaseGenerate
65
* CaseGenerateItem
66
* CaseStatement
67
* CaseStatementItem
68
* Code
69
* Declaration
70
* DelayedStatement
71
* DisableStatement
72
* Generate
73
* IfGenerate
74
* IfGenerateBranch
75
* IfStatement
76
* IfStatementBranch
77
* Instantiation
78
* LoopGenerate
79
* LoopStatement
80
* Module
81
* NullStatement
82
* PathDeclaration
83
* Port
84
* PortConnection
85
* Procedure
86
* PulseStyle
87
* ShowCancelled
88
* Specify
89
* Table
90
* TableEntry
91
* TaskStatement
92
* TriggerStatement
93
* WaitStatement
94 2 Alexander Kamkin
95
h2. Registering a Backend