-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVAst.cpp
205 lines (182 loc) · 6.71 KB
/
VAst.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// -*- 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
///
//*************************************************************************
#include "VSymTable.h"
#include "VAst.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_SV_DUMP(SVp) {printf("%s:%d:\n",__FILE__,__LINE__); Perl_sv_dump(aTHX_ (SV*)(SVp)); }
# define DBG_UINFO(z,msg) {printf("%s:%d: ", __FILE__,__LINE__); cout << msg; }
#else
# define DBG_SV_DUMP(SVp)
# define DBG_UINFO(z,msg)
#endif
int VAstEnt::s_debug = 0;
// ACCESSORS
VAstType VAstEnt::type() {
assert(this);
AV* avp = castAVp();
if (!avp || SvTYPE(avp) != SVt_PVAV || av_len(avp)<1) return VAstType::ERROR;
// $type_svpp = $this->[0]
SV** type_svpp = av_fetch(avp, 0, 0);
if (!type_svpp) return VAstType::ERROR;
VAstType type = (VAstType)(SvIV(*type_svpp));
return type;
}
HV* VAstEnt::subhash() {
assert(this);
AV* avp = castAVp();
if (!avp || SvTYPE(avp) != SVt_PVAV) return NULL; /*Error*/
// $type_svpp = $this->[2]
SV** hash_svpp = av_fetch(avp, 2, 0);
if (!hash_svpp || !SvROK(*hash_svpp) || SvTYPE(SvRV(*hash_svpp)) != SVt_PVHV) return NULL; /*Error*/
// $hash_hvp = %{$this->[2]}
HV* hash_hvp = (HV*)(SvRV(*hash_svpp));
return hash_hvp;
}
VAstEnt* VAstEnt::parentp() {
assert(this);
AV* avp = castAVp();
if (!avp || SvTYPE(avp) != SVt_PVAV) return NULL; /*Error*/
// $parent_svpp = $this->[1]
SV** parent_svpp = av_fetch(avp, 1, 0);
if (!parent_svpp || !SvROK(*parent_svpp) || SvTYPE(SvRV(*parent_svpp)) != SVt_PVAV) return NULL; /*Error*/
// $parent_svp = @{$this->[1]}
AV* parent_avp = (AV*)(SvRV(*parent_svpp));
return avToSymEnt(parent_avp);
}
// METHODS
void VAstEnt::initNetlist(VFileLine* fl) {
// Called on initial creation to check if this is a netlist, and/or create it
assert(this);
AV* avp = castAVp();
if (!avp || SvTYPE(avp) != SVt_PVAV) { fl->error("Parser->symbol_table isn't an array reference"); }
if (type() == VAstType::ERROR) { // Need init
initAVEnt(avp, VAstType::NETLIST, NULL);
} else if (type() == VAstType::NETLIST) { // Already inited
} else { fl->error("Parser->symbol_table isn't a netlist object (not created by the parser?)"); }
}
AV* VAstEnt::newAVEnt(VAstType type) {
AV* avp = newAV();
initAVEnt(avp, type, this->castAVp());
return avp;
}
void VAstEnt::initAVEnt(AV* avp, VAstType type, AV* parentp) {
// $avp = [type, parent, {}]
av_push(avp, newSViv(type));
if (parentp) {
SV* parentsv = newRV((SV*)parentp);
#ifdef SvWEAKREF // Newer perls
// We're making a circular reference, so to garbage collect properly we need to break it
// On older Perl's we'll just leak.
sv_rvweaken(parentsv);
#endif
av_push(avp, parentsv );
} else { // netlist top
av_push(avp, &PL_sv_undef);
}
av_push(avp, newRV_noinc((SV*)newHV()) );
}
void VAstEnt::replaceInsert(VAstEnt* newentp, const string& name) {
if (debug()) cout<<"VAstEnt::replaceInsert under="<<this<<" "<<newentp->ascii(name)<<"\"\n";
HV* hvp = subhash(); assert(hvp);
// $svpp = $table{$name}
SV** svpp = hv_fetch(hvp, name.c_str(), name.length(), 1/*create*/);
if (svpp) {} // unused
// $avp = $newentp (premade avp)
hv_store(hvp, name.c_str(), name.length(), newRV((SV*)newentp), 0);
}
VAstEnt* VAstEnt::replaceInsert(VAstType type, const string& name) {
if (debug()) cout<<"VAstEnt::replaceInsert under="<<this<<" "<<type.ascii()<<"-\""<<name<<"\"\n";
HV* hvp = subhash(); assert(hvp);
// $svpp = $table{$name}
SV** svpp = hv_fetch(hvp, name.c_str(), name.length(), 1/*create*/);
if (svpp) {} // unused
// $avp = [type, this, {}]
AV* sub_avp = newAVEnt(type);
hv_store(hvp, name.c_str(), name.length(), newRV_noinc((SV*)sub_avp), 0);
return avToSymEnt(sub_avp);
}
VAstEnt* VAstEnt::findSym (const string& name) {
HV* hvp = subhash(); assert(hvp);
// $svpp = $table{$name}
SV** svpp = hv_fetch(hvp, name.c_str(), name.length(), 0/*no-change*/);
if (!svpp) return NULL;
SV* svp = *svpp;
if (!svp || !SvROK(svp) || SvTYPE(SvRV(svp)) != SVt_PVAV) return NULL;
// $sub_avp = @{$table{$name}}
AV* sub_avp = (AV*)(SvRV(svp));
VAstEnt* entp = avToSymEnt(sub_avp);
if (debug()) cout<<"VAstEnt::find found under="<<this<<" "<<entp->ascii(name)<<"\n";
return entp;
}
VAstEnt* VAstEnt::findInsert (VAstType type, const string& name) {
if (debug()) cout<<"VAstEnt::findInsert under="<<this<<" "<<type.ascii()<<"-\""<<name<<"\"\n";
VAstEnt* symp = findSym(name);
if (!symp) {
symp = replaceInsert(type,name);
assert(symp && symp == findSym(name));
}
return symp;
}
void VAstEnt::import(VAstEnt* pkgEntp, const string& id_or_star) {
if (id_or_star != "*") { // Import single entry
if (VAstEnt* idEntp = pkgEntp->findSym(id_or_star)) {
// We can just add a second reference to the same AstEnt object
if (debug()) cout<<"VAstEnt::import under="<<this<<" "<<idEntp->ascii()<<"\n";
replaceInsert(idEntp, id_or_star);
}
} else {
// Walk old sym table
HV* hvp = pkgEntp->subhash(); assert(hvp);
hv_iterinit(hvp);
while (HE* hep = hv_iternext(hvp)) {
I32 retlen;
const char* namep = hv_iterkey(hep, &retlen);
string name = string(namep,retlen);
SV* svp = hv_iterval(hvp, hep);
VAstEnt* idEntp = avToSymEnt((AV*)(SvRV(svp)));
if (debug()) cout<<"VAstEnt::import under="<<this<<" "<<idEntp->ascii(name)<<"\n";
replaceInsert(idEntp, name);
}
}
}
string VAstEnt::ascii(const string& name) {
string out = cvtToStr((void*)this)+"-"+type().ascii();
if (name!="") out += "-\""+name+"\"";
return out;
}
#undef DBG_SV_DUMP
#undef DBG_UINFO