forked from The-OpenROAD-Project-Attic/Resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefReader.cc
347 lines (322 loc) · 9.39 KB
/
DefReader.cc
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Resizer, LEF/DEF gate resizer
// Copyright (c) 2019, Parallax Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Machine.hh"
#include "Report.hh"
#include "Error.hh"
#include "PortDirection.hh"
#include "ParseBus.hh"
#include "LefDefNetwork.hh"
#include "defrReader.hpp"
// Use Cadence DEF parser to build ConcreteNetwork based objects.
namespace sta {
static void
registerDefCallbacks();
static int
defDesignCbk(defrCallbackType_e,
const char *divider,
defiUserData user);
static int
defDividerCbk(defrCallbackType_e,
const char *divider,
defiUserData user);
static int
defBusBitCbk(defrCallbackType_e,
const char *bus_chars,
defiUserData user);
static int
defUnitsCbk(defrCallbackType_e,
double units,
defiUserData user);
static int
defDieAreaCbk(defrCallbackType_e,
defiBox *box,
defiUserData user);
static int
defComponentCbk(defrCallbackType_e,
defiComponent *def_component,
defiUserData user);
static int
defNetCbk(defrCallbackType_e,
defiNet *def_net,
defiUserData user);
static int
defPinCbk(defrCallbackType_e,
defiPin *def_pin,
defiUserData user);
static int
defPinEndCbk(defrCallbackType_e,
void *,
defiUserData user);
static const char *
defToSta(const char *token,
Network *network);
////////////////////////////////////////////////////////////////
// DEF parser callback routine state.
class DefReader
{
public:
DefReader(bool save_def_data,
LefDefNetwork *network);
bool saveDefData() { return save_def_data_; }
LefDefNetwork *network() { return network_; }
private:
bool save_def_data_;
LefDefNetwork *network_;
};
void
readDef(const char *filename,
bool save_def_data,
LefDefNetwork *network)
{
network->setDefFilename(filename);
// Make top_instance to act as parent to components.
// Note that top ports are not known yet because PINS section has not been parsed.
Library *lef_library = network->lefLibrary();
if (lef_library) {
defrInitSession();
registerDefCallbacks();
DefReader reader(save_def_data, network);
FILE *stream = fopen(filename, "r");
if (stream) {
bool case_sensitive = true;
defrRead(stream, filename, &reader, case_sensitive);
defrClear();
fclose(stream);
}
else
throw FileNotReadable(filename);
}
else
network->report()->printError("Error; no LEF library has been read.\n");
}
static void
registerDefCallbacks()
{
defrSetDesignCbk(defDesignCbk);
defrSetDividerCbk(defDividerCbk);
defrSetBusBitCbk(defBusBitCbk);
defrSetUnitsCbk(defUnitsCbk);
defrSetDieAreaCbk(defDieAreaCbk);
defrSetComponentCbk(defComponentCbk);
defrSetNetCbk(defNetCbk);
defrSetPinCbk(defPinCbk);
defrSetPinEndCbk(defPinEndCbk);
}
DefReader::DefReader(bool save_def_data,
LefDefNetwork *network) :
save_def_data_(save_def_data),
network_(network)
{
}
#define getDefReader(user) (reinterpret_cast<DefReader *>(user))
#define getNetwork(user) (getDefReader(user)->network())
#define saveDefData(user) (getDefReader(user)->saveDefData())
static void
defError(defiUserData user,
const char *fmt, ...)
{
Report *report = getNetwork(user)->report();
va_list args;
va_start(args, fmt);
report->vprintError(fmt, args);
va_end(args);
}
static int
defDesignCbk(defrCallbackType_e,
const char *design,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
Library *lef_library = network->lefLibrary();
Cell *top_cell = network->makeCell(lef_library, design, false,
network->defFilename());
Instance *top_instance = network->makeInstance(top_cell, "", nullptr);
network->setTopInstance(top_instance);
return 0;
}
static int
defDividerCbk(defrCallbackType_e,
const char *divider,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
network->setDivider(divider[0]);
return 0;
}
static int
defBusBitCbk(defrCallbackType_e,
const char *bus_chars,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
Library *lef_lib = network->lefLibrary();
ConcreteLibrary *lef_clib = reinterpret_cast<ConcreteLibrary*>(lef_lib);
lef_clib->setBusBrkts(bus_chars[0], bus_chars[1]);
return 0;
}
static int
defUnitsCbk(defrCallbackType_e,
double units,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
network->setDefUnits(units);
return 0;
}
static int
defDieAreaCbk(defrCallbackType_e,
defiBox *box,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
defiPoints points = box->getPoint();
if (points.numPoints == 2)
network->setDieArea(points.x[0], points.y[0],
points.x[1], points.y[1]);
return 0;
}
static int
defComponentCbk(defrCallbackType_e,
defiComponent *def_component,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
Library *lef_lib = network->lefLibrary();
const char *def_name = def_component->id();
const char *sta_name = defToSta(def_name, network);
const char *macro_name = def_component->name();
Cell *cell = network->findCell(lef_lib, macro_name);
if (cell)
network->makeDefComponent(cell, sta_name,
saveDefData(user)
? new defiComponent(*def_component)
: nullptr);
else
defError(user, "Error: component %s macro %s not found.\n", def_name, macro_name);
return 0;
}
static int
defPinCbk(defrCallbackType_e,
defiPin *def_pin,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
const char *pin_name = def_pin->pinName();
// Make LEF ports in the top cell.
// The corresponding top level pins are made when the NET section is parsed.
Cell *top_cell = network->cell(network->topInstance());
Port *port = network->makePort(top_cell, pin_name);
PortDirection *dir = PortDirection::unknown();
if (def_pin->hasDirection()) {
const char *def_dir = def_pin->direction();
if (stringEq(def_dir, "INPUT"))
dir = PortDirection::input();
else if (stringEq(def_dir, "OUTPUT"))
dir = PortDirection::output();
else if (stringEq(def_dir, "OUTPUT TRISTATE"))
dir = PortDirection::tristate();
else if (stringEq(def_dir, "INOUT"))
dir = PortDirection::bidirect();
}
if (def_pin->hasUse()) {
const char *use = def_pin->use();
if (stringEq(use, "POWER"))
dir = PortDirection::power();
else if (stringEq(use, "GROUND"))
dir = PortDirection::ground();
}
network->setDirection(port, dir);
if (def_pin->isPlaced() || def_pin->isFixed() || def_pin->isCover())
network->setLocation(port, DefPt(def_pin->placementX(), def_pin->placementY()));
return 0;
}
// Finished PINS section so all of the top instance ports are defined.
// Now top_instance::initPins() can be called.
static int
defPinEndCbk(defrCallbackType_e,
void *,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
network->initTopInstancePins();
Cell *top_cell = network->cell(network->topInstance());
network->groupBusPorts(top_cell);
return 0;
}
static int
defNetCbk(defrCallbackType_e,
defiNet *def_net,
defiUserData user)
{
LefDefNetwork *network = getNetwork(user);
const char *def_net_name = def_net->name();
const char *sta_net_name = defToSta(def_net_name, network);
Net *net = network->makeNet(sta_net_name, network->topInstance());
for (int i = 0; i < def_net->numConnections(); i++) {
const char *def_inst_name = def_net->instance(i);
const char *sta_inst_name = defToSta(def_inst_name, network);
const char *pin_name = def_net->pin(i);
if (stringEq(def_inst_name, "PIN")) {
Instance *top_inst = network->topInstance();
Pin *pin = network->findPin(top_inst, pin_name);
if (pin == nullptr) {
Cell *cell = network->cell(top_inst);
Port *port = network->findPort(cell, pin_name);
if (port)
pin = network->makePin(top_inst, port, nullptr);
else
defError(user, "Error: net %s connection to PIN %s not found\n",
def_net_name, pin_name);
}
if (pin)
network->makeTerm(pin, net);
}
else {
Instance *top_inst = network->topInstance();
Instance *inst = network->findChild(top_inst, sta_inst_name);
if (inst) {
Cell *cell = network->cell(inst);
Port *port = network->findPort(cell, pin_name);
if (port)
network->connect(inst, port, net);
else
defError(user, "Error: net %s connection to component %s/%s pin %s not found.\n",
def_net_name,
def_inst_name,
network->name(cell),
pin_name);
}
else
defError(user, "Error: net %s connection component %s not found.\n",
def_net_name,
def_inst_name);
}
}
return 0;
}
// Nada.
// Note that the DEF names violate the normal sta namespace conventions
// because the netlist is flattend.
// Both of the following are valid in DEF and correspond to different Verilog names.
// h1/r1
// h1\/r1
static const char *
defToSta(const char *token,
Network *)
{
return token;
}
} // namespace