-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSymTable.cpp
168 lines (149 loc) · 5.59 KB
/
VSymTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// -*- C++ -*-
//*************************************************************************
//
// Copyright 2009-2012 by Wilson Snyder. This program is free software;
// you can redistribute it and/or modify it under the terms of either the
// GNU Lesser General Public License Version 3 or the Perl Artistic License
// Version 2.0.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//*************************************************************************
/// \file
/// \brief Verilog::Parse: Symbol table accessing
///
/// Authors: Wilson Snyder
///
/// Code available from: http://www.veripool.org/verilog-perl
///
//*************************************************************************
//
// Verilog Name spaces:
// Namespace Under Holds
// (*attributes*) objects Not implemented
// `textmacros comp-unit Preproc handles
// .ports/inout module/cell ports
// packages Global package
// definitions comp-unit Non-nested (macro)module, primitive, program, interface
// comp-unit comp-unit Func, task, param, events, nets, vars, types
// module (macro)module,primitive,program,interface,package
// module, intf, program,
// blocks, func, task, inst, param, events,
// vars, nets, types
// block blocks(begin/fork), specify, func, task
// blocks, specify, func, task, inst*, param, events,
// vars, nets*, types
// (*IEEE doesn't list but can be under generate blocks)
//
// Testcase: "generate if (1) begin wire b; ..." proves b is local.
//
// Turning this on its head:
//
// Object Namespace Under
// ports/inouts .ports module/cell
//
// package packages comp-unit
//
// module, program, interface definitions comp-unit
// [primitive(defs)] OR module module,primitive,program,interface,package
//
// func, task, param, events, comp-unit comp-unit
// vars, nets, types, [blocks] module module,primitive,program,interface,package
// [inst], [[specify]] OR block blocks(begin/fork), specify, func, task
//
// [...] indicates can only be under one of the lower name spaces, but won't
// matter if the others are searched since the grammar won't create the
// conflicting case.
//
// Since packages can't be declared under modules, and modules under
// blocks. we can just have one hierarchical namespace that combines
// comp-unit, module and block.
//*************************************************************************
#include "VSymTable.h"
#include <cassert>
#include <iostream>
/* Perl */
// extern "C" {
# include <EXTERN.h>
# include <perl.h>
# include <XSUB.h>
// }
//######################################################################
// VAstEnt
//
// A symtable is simply a hash(HV) by name of the object
// Under each hash entry (HE) is an array (AV)
// Under each array (AV) is an [SV with type, SV with HV of lower symtab]
#if 0
# define DBG_UINFO(z,msg) {printf("%s:%d: ", __FILE__,__LINE__); cout << msg; }
#else
# define DBG_UINFO(z,msg)
#endif
//######################################################################
// VSymStack
VSymStack::VSymStack(VFileLine* fl, struct av* symp) {
assert(symp);
((VAstEnt*)(symp))->initNetlist(fl);
pushScope((VAstEnt*)(symp));
}
//######################################################################
// Self test
class VFileLineTest : public VFileLine {
public:
VFileLineTest(int called_only_for_default) : VFileLine(called_only_for_default) {}
virtual ~VFileLineTest() { }
virtual VFileLine* create(const string& filename, int lineno) { return new VFileLineTest(true); }
virtual void error(const string& msg) { cout << msg; }
};
void VSymStack::selftest() {
// GCC 3.3.5 requires this temporary string, so can't be a one liner
{ string max = VAstType(VAstType::_MAX).ascii(); assert(max == "_MAX"); } // Else probably missing word in ascii()
//
VFileLineTest flt(1); // GCC 3.3.5 requires temporary
VFileLine* fl = flt.create(__FILE__,__LINE__);
AV* topavp = newAV();
VSymStack stack (fl, topavp);
//
DBG_UINFO(9,"=============\n");
assert(stack.objofUpward() == "netlist");
assert(stack.findTypeUpward("missing") == VAstType::NOT_FOUND);
DBG_UINFO(9,"=============\n");
stack.pushScope(stack.findInsert(VAstType::PACKAGE, "top"));
{
assert(stack.objofUpward() == "package");
assert(stack.findTypeUpward("top") == VAstType::PACKAGE);
stack.findInsert(VAstType::TYPE, "a");
DBG_UINFO(9,"=============\n");
stack.pushScope(stack.findInsert(VAstType::MODULE, "lower"));
{
assert(stack.findTypeUpward("lower") == VAstType::MODULE); // IE ../lower exists.
DBG_UINFO(9,"=============\n");
stack.pushScope(stack.findInsert(VAstType::FORK, "fork"));
{
assert(stack.findTypeUpward("lower") == VAstType::MODULE); // Ignores the fork
}
stack.popScope(fl);
DBG_UINFO(9,"=============\n");
stack.pushScope(stack.findInsert(VAstType::CLASS, "a")); // Hides upper a
{
assert(stack.objofUpward() == "class");
assert(stack.findTypeUpward("a") == VAstType::CLASS);
assert(stack.findTypeUpward("top") == VAstType::PACKAGE);
}
stack.popScope(fl);
DBG_UINFO(9,"=============\n");
assert(stack.objofUpward() == "module");
assert(stack.findTypeUpward("a") == VAstType::CLASS);
assert(stack.findTypeUpward("top") == VAstType::PACKAGE);
}
stack.popScope(fl);
DBG_UINFO(9,"=============\n");
assert(stack.findTypeUpward("a") == VAstType::TYPE);
}
//
av_undef(topavp); topavp=NULL;
};
#undef DBG_UINFO