1 // *****************************************************************************
2 // *****************************************************************************
3 // Copyright 2013 - 2016, Cadence Design Systems
4 //
5 // This  file  is  part  of  the  Cadence  LEF/DEF  Open   Source
6 // Distribution,  Product Version 5.8.
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 //    you may not use this file except in compliance with the License.
10 //    You may obtain a copy of the License at
11 //
12 //        http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //    Unless required by applicable law or agreed to in writing, software
15 //    distributed under the License is distributed on an "AS IS" BASIS,
16 //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 //    implied. See the License for the specific language governing
18 //    permissions and limitations under the License.
19 //
20 // For updates, support, or to become part of the LEF/DEF Community,
21 // check www.openeda.org for details.
22 //
23 //  $Author: dell $
24 //  $Revision: #1 $
25 //  $Date: 2017/06/06 $
26 //  $State:  $
27 // *****************************************************************************
28 // *****************************************************************************
29 
30 #include <stdlib.h>
31 #include <string.h>
32 #include "defiComponent.hpp"
33 #include "defiDebug.hpp"
34 #include "lex.h"
35 #include "defiUtil.hpp"
36 
37 BEGIN_LEFDEF_PARSER_NAMESPACE
38 
39 /*********************************************************
40 * class defiComponentMaskShiftLayer
41 **********************************************************/
defiComponentMaskShiftLayer(defrData * data)42 defiComponentMaskShiftLayer::defiComponentMaskShiftLayer(defrData *data)
43  : defData(data)
44 {
45     Init();
46 }
47 
defiComponentMaskShiftLayer()48 defiComponentMaskShiftLayer::defiComponentMaskShiftLayer() {
49     defData = NULL;
50     layersAllocated_ = 0;
51     numLayers_ = 0;
52     layers_ = 0;
53 }
54 
DEF_COPY_CONSTRUCTOR_C(defiComponentMaskShiftLayer)55 DEF_COPY_CONSTRUCTOR_C( defiComponentMaskShiftLayer ) {
56     layersAllocated_ = 0;
57     numLayers_ = 0;
58     layers_ = 0;
59     DEF_COPY_FUNC( layersAllocated_ );
60     DEF_COPY_FUNC( numLayers_ );
61     DEF_MALLOC_FUNC_FOR_2D_STR( layers_ , numLayers_  );
62 
63 }
64 
DEF_ASSIGN_OPERATOR_C(defiComponentMaskShiftLayer)65 DEF_ASSIGN_OPERATOR_C( defiComponentMaskShiftLayer ) {
66     CHECK_SELF_ASSIGN
67     layersAllocated_ = 0;
68     numLayers_ = 0;
69     layers_ = 0;
70     DEF_COPY_FUNC( layersAllocated_ );
71     DEF_COPY_FUNC( numLayers_ );
72     DEF_MALLOC_FUNC_FOR_2D_STR( layers_ , numLayers_ );
73     return *this;
74 }
75 
~defiComponentMaskShiftLayer()76 defiComponentMaskShiftLayer::~defiComponentMaskShiftLayer() {
77     Destroy();
78 }
79 
Init()80 void defiComponentMaskShiftLayer::Init() {
81     layersAllocated_ = 0;
82     numLayers_ = 0;
83     layers_ = 0;
84 
85     bumpLayers(16);
86 }
87 
Destroy()88 void defiComponentMaskShiftLayer::Destroy() {
89     if (numLayers_) {
90         for (int i = 0; i < numLayers_; i++) {
91             if (layers_[i]) {
92                 free(layers_[i]);
93             }
94         }
95         free((char*)(layers_));
96     } else {
97         if (layersAllocated_) {
98             free((char*)(layers_));
99         }
100     }
101     layersAllocated_ = 0;
102     numLayers_ = 0;
103     layers_ = 0;
104 }
105 
addMaskShiftLayer(const char * layer)106 void defiComponentMaskShiftLayer::addMaskShiftLayer(const char* layer) {
107     int len = strlen(layer) + 1;
108     if (numLayers_ == layersAllocated_)
109         bumpLayers(numLayers_ * 2);
110     layers_[numLayers_] = (char*)malloc(len);
111     strcpy(layers_[numLayers_], defData->DEFCASE(layer));
112     (numLayers_)++;
113 }
114 
bumpLayers(int size)115 void defiComponentMaskShiftLayer::bumpLayers(int size) {
116     int i;
117     char** newLayers = (char**)malloc(sizeof(char*)* size);
118     for (i = 0; i < numLayers_; i++) {
119         newLayers[i] = layers_[i];
120     }
121     if (layers_) {
122         free((char*)(layers_));
123     }
124     layers_ = newLayers;
125     layersAllocated_ = size;
126 }
127 
clear()128 void defiComponentMaskShiftLayer::clear() {
129     for (int i = 0; i < numLayers_; i++) {
130         free(layers_[i]);
131     }
132     numLayers_ = 0;
133 }
134 
numMaskShiftLayers() const135 int defiComponentMaskShiftLayer::numMaskShiftLayers() const {
136     return numLayers_;
137 }
138 
maskShiftLayer(int index) const139 const char* defiComponentMaskShiftLayer::maskShiftLayer(int index) const {
140     if (index >= 0 && index < numLayers_) {
141         return layers_[index];
142     }
143 
144     return 0;
145 }
146 
147 
148 /*********************************************************
149 * class defiComponent
150 **********************************************************/
defiComponent(defrData * data)151 defiComponent::defiComponent(defrData *data)
152  : defData(data)
153 {
154   Init();
155 }
156 
157 
Init()158 void defiComponent::Init() {
159   id_ = 0;
160   name_ = 0;
161   regionName_ = 0;
162   foreignName_ = 0;
163   Fori_ = 0;
164   EEQ_ = 0;
165   generateName_ = 0;
166   macroName_ = 0;
167   generateNameSize_ = 0;
168   maskShiftSize_ = 0;
169   maskShift_ = 0;
170   macroNameSize_ = 0;
171   minLayerSize_ = 0;
172   maxLayerSize_ = 0;
173   minLayer_ = 0;
174   maxLayer_ = 0;
175   nets_ = 0;
176   source_ = 0;
177   numNets_ = 0;
178   bumpName(16);
179   bumpId(16);
180   bumpRegionName(16);
181   bumpEEQ(16);
182   bumpNets(16);
183   bumpForeignName(16);
184   bumpMinLayer(16);
185   bumpMaxLayer(16);
186   numProps_ = 0;
187   propsAllocated_ = 2;
188   orient_ = 0;
189   names_ = (char**)malloc(sizeof(char*)* 2);
190   values_ = (char**)malloc(sizeof(char*)* 2);
191   dvalues_ = (double*)malloc(sizeof(double)* 2);
192   types_ = (char*)malloc(sizeof(char)* 2);
193   clear();
194 
195   numRects_ = 0;
196   rectsAllocated_ = 1;
197   rectXl_ = (int*)malloc(sizeof(int)*1);
198   rectYl_ = (int*)malloc(sizeof(int)*1);
199   rectXh_ = (int*)malloc(sizeof(int)*1);
200   rectYh_ = (int*)malloc(sizeof(int)*1);
201 }
202 
DEF_COPY_CONSTRUCTOR_C(defiComponent)203 DEF_COPY_CONSTRUCTOR_C( defiComponent )
204 {
205     defData = NULL;
206     this->Init();
207 
208     DEF_COPY_FUNC( idSize_ );
209     DEF_COPY_FUNC( nameSize_ );
210 //    printf("nameSize_:  %d\n", nameSize_);
211 //    fflush(stdout);
212     DEF_MALLOC_FUNC( id_, char, sizeof(char) * (strlen(prev.id_)+1));
213     DEF_MALLOC_FUNC( name_, char, sizeof(char) * (strlen(prev.name_)+1));
214     DEF_COPY_FUNC( ForiSize_ );
215     DEF_COPY_FUNC( status_ );
216     DEF_COPY_FUNC( hasRegionName_ );
217     DEF_COPY_FUNC( hasEEQ_ );
218     DEF_COPY_FUNC( hasGenerate_ );
219     DEF_COPY_FUNC( hasWeight_ );
220     DEF_COPY_FUNC( hasFori_ );
221     DEF_COPY_FUNC( orient_ );
222     DEF_COPY_FUNC( x_ );
223     DEF_COPY_FUNC( y_ );
224     DEF_COPY_FUNC( numRects_ );
225     DEF_COPY_FUNC( rectsAllocated_ );
226     DEF_MALLOC_FUNC( rectXl_, int, sizeof(int) * numRects_);
227     DEF_MALLOC_FUNC( rectYl_, int, sizeof(int) * numRects_);
228     DEF_MALLOC_FUNC( rectXh_, int, sizeof(int) * numRects_);
229     DEF_MALLOC_FUNC( rectYh_, int, sizeof(int) * numRects_);
230 
231     DEF_COPY_FUNC( regionNameSize_ );
232     DEF_MALLOC_FUNC( regionName_, char, sizeof(char) * (regionNameSize_));
233     DEF_COPY_FUNC( EEQSize_ );
234     DEF_MALLOC_FUNC( EEQ_, char, sizeof(char) * (EEQSize_));
235     DEF_COPY_FUNC( numNets_ );
236     DEF_COPY_FUNC( netsAllocated_ );
237 
238     DEF_MALLOC_FUNC_FOR_2D_STR( nets_, numNets_ );
239 
240     DEF_COPY_FUNC( weight_ );
241     DEF_COPY_FUNC( maskShiftSize_ );
242     DEF_MALLOC_FUNC( maskShift_, int, sizeof(int) * maskShiftSize_);
243 
244     DEF_MALLOC_FUNC( source_, char, sizeof(char) * (strlen(prev.source_) +1));
245     DEF_COPY_FUNC( hasForeignName_ );
246     DEF_COPY_FUNC( foreignNameSize_ );
247     DEF_MALLOC_FUNC( foreignName_, char, sizeof(char) * foreignNameSize_);
248     DEF_COPY_FUNC( Fx_ );
249     DEF_COPY_FUNC( Fy_ );
250     DEF_COPY_FUNC( Fori_ );
251     DEF_COPY_FUNC( generateNameSize_ );
252     DEF_MALLOC_FUNC( generateName_, char, sizeof(char) * (generateNameSize_));
253     DEF_COPY_FUNC( macroNameSize_ );
254     DEF_MALLOC_FUNC( macroName_, char, sizeof(char) * (macroNameSize_));
255     DEF_COPY_FUNC( hasHalo_ );
256     DEF_COPY_FUNC( hasHaloSoft_ );
257     DEF_COPY_FUNC( leftHalo_ );
258     DEF_COPY_FUNC( bottomHalo_ );
259     DEF_COPY_FUNC( rightHalo_ );
260     DEF_COPY_FUNC( topHalo_ );
261     DEF_COPY_FUNC( haloDist_ );
262     DEF_COPY_FUNC( minLayerSize_ );
263     DEF_MALLOC_FUNC( minLayer_, char, sizeof(char) * (minLayerSize_));
264     DEF_COPY_FUNC( maxLayerSize_ );
265     DEF_MALLOC_FUNC( maxLayer_, char, sizeof(char) * (maxLayerSize_));
266     DEF_COPY_FUNC( numProps_ );
267     DEF_COPY_FUNC( propsAllocated_ );
268     DEF_MALLOC_FUNC_FOR_2D_STR( names_, numProps_);
269     DEF_MALLOC_FUNC_FOR_2D_STR( values_, numProps_);
270     DEF_MALLOC_FUNC( dvalues_, double, sizeof(double) * numProps_);
271     DEF_MALLOC_FUNC( types_, char, sizeof(char) * numProps_);
272 }
273 
Destroy()274 void defiComponent::Destroy() {
275   free(name_);
276   free(regionName_);
277   free(id_);
278   free(EEQ_);
279   free(minLayer_);
280   free(maxLayer_);
281   free((char*)(nets_));
282   netsAllocated_ = 0;      // avoid freeing again later
283   if (source_) free(source_);
284   if (foreignName_) free(foreignName_);
285   if (generateName_) free(generateName_);
286   if (macroName_) free(macroName_);
287   if (netsAllocated_) free((char*)(nets_));
288   free((char*)(maskShift_));
289   free((char*)(names_));
290   free((char*)(values_));
291   free((char*)(dvalues_));
292   free((char*)(types_));
293   free((char*)(rectXl_));
294   free((char*)(rectYl_));
295   free((char*)(rectXh_));
296   free((char*)(rectYh_));
297 }
298 
299 
~defiComponent()300 defiComponent::~defiComponent() {
301   Destroy();
302 }
303 
304 
IdAndName(const char * id,const char * name)305 void defiComponent::IdAndName(const char* id, const char* name) {
306   int len;
307 
308   clear();
309 
310   if ((len = strlen(id)+1) > idSize_)
311     bumpId(len);
312   strcpy(id_, defData->DEFCASE(id));
313 
314   if ((len = strlen(name)+1) > nameSize_)
315     bumpName(len);
316   strcpy(name_, defData->DEFCASE(name));
317 }
318 
319 
source() const320 const char* defiComponent::source() const {
321   return source_;
322 }
323 
324 
weight() const325 int defiComponent::weight() const {
326   return weight_;
327 }
328 
329 
setWeight(int w)330 void defiComponent::setWeight(int w) {
331   weight_ = w;
332   hasWeight_ = 1;
333 }
334 
maskShift(int index) const335 int defiComponent::maskShift(int index) const {
336     if (index < 0 || index >= maskShiftSize_) {
337         defiError(1, 0, "bad index for component maskShift", defData);
338         return 0;
339     }
340 
341     return maskShift_[index];
342 }
343 
setMaskShift(const char * shiftMask)344 void defiComponent::setMaskShift(const char *shiftMask) {
345     int shiftMaskLength = strlen(shiftMask);
346 
347     maskShift_ = (int*)malloc(sizeof(int)* shiftMaskLength);
348     maskShiftSize_ = shiftMaskLength;
349 
350     for (int i = 0; i < shiftMaskLength; i++) {
351         int curShift = shiftMask[i] - '0';
352 
353         // Strip possible error data.
354         if (curShift > 9 || curShift < 0) {
355             curShift = 0;
356         }
357 
358         maskShift_[shiftMaskLength - i - 1] = curShift;
359     }
360 }
361 
setGenerate(const char * newName,const char * macroName)362 void defiComponent::setGenerate(const char* newName, const char* macroName) {
363   int len = strlen(newName) + 1;
364 
365   if (generateNameSize_ < len) {
366     if (generateName_) free(generateName_);
367     generateName_ = (char*)malloc(len);
368     generateNameSize_ = len;
369   }
370   strcpy(generateName_, defData->DEFCASE(newName));
371 
372   len = strlen(macroName) + 1;
373   if (macroNameSize_ < len) {
374     if (macroName_) free(macroName_);
375     macroName_ = (char*)malloc(len);
376     macroNameSize_ = len;
377   }
378   strcpy(macroName_, defData->DEFCASE(macroName));
379 
380   hasGenerate_ = 1;  // Ying Tan fix at 20010918
381 }
382 
383 
setSource(const char * name)384 void defiComponent::setSource(const char* name) {
385   int len = strlen(name) + 1;
386   source_ = (char*)malloc(len);
387   strcpy(source_, defData->DEFCASE(name));
388 }
389 
390 
setRegionName(const char * name)391 void defiComponent::setRegionName(const char* name) {
392   int len;
393 
394   if ((len = strlen(name)+1) > regionNameSize_)
395     bumpRegionName(len);
396   strcpy(regionName_, defData->DEFCASE(name));
397   hasRegionName_ = 1;
398 }
399 
400 
setEEQ(const char * name)401 void defiComponent::setEEQ(const char* name) {
402   int len;
403 
404   if ((len = strlen(name)+1) > EEQSize_)
405     bumpEEQ(len);
406   strcpy(EEQ_, defData->DEFCASE(name));
407   hasEEQ_ = 1;
408 }
409 
410 
setPlacementStatus(int n)411 void defiComponent::setPlacementStatus(int n) {
412   status_= n;
413 }
414 
415 
setPlacementLocation(int x,int y,int orient)416 void defiComponent::setPlacementLocation(int x, int y, int orient) {
417   x_ = x;
418   y_ = y;
419 
420   if( orient != -1 ) { // mgwoo
421       orient_ = orient;
422   }
423 }
424 
425 
setRegionBounds(int xl,int yl,int xh,int yh)426 void defiComponent::setRegionBounds(int xl, int yl, int xh, int yh) {
427   int i;
428   i = numRects_;
429   if (i == rectsAllocated_) {
430     int max = rectsAllocated_ * 2;
431     int* nxl = (int*)malloc(sizeof(int)*max);
432     int* nyl = (int*)malloc(sizeof(int)*max);
433     int* nxh = (int*)malloc(sizeof(int)*max);
434     int* nyh = (int*)malloc(sizeof(int)*max);
435     for (i = 0; i < numRects_; i++) {
436       nxl[i] = rectXl_[i];
437       nyl[i] = rectYl_[i];
438       nxh[i] = rectXh_[i];
439       nyh[i] = rectYh_[i];
440     }
441     free((char*)(rectXl_));
442     free((char*)(rectYl_));
443     free((char*)(rectXh_));
444     free((char*)(rectYh_));
445     rectXl_ = nxl;
446     rectYl_ = nyl;
447     rectXh_ = nxh;
448     rectYh_ = nyh;
449     rectsAllocated_ = max;
450   }
451   rectXl_[i] = xl;
452   rectYl_[i] = yl;
453   rectXh_[i] = xh;
454   rectYh_[i] = yh;
455   numRects_ += 1;
456 }
457 
458 
459 // 5.6
setHalo(int left,int bottom,int right,int top)460 void defiComponent::setHalo(int left, int bottom, int right, int top) {
461   hasHalo_ = 1;
462   leftHalo_ = left;
463   bottomHalo_ = bottom;
464   rightHalo_ = right;
465   topHalo_ = top;
466 }
467 
468 // 5.7
setHaloSoft()469 void defiComponent::setHaloSoft() {
470   hasHaloSoft_ = 1;
471 }
472 
473 // 5.7
setRouteHalo(int haloDist,const char * minLayer,const char * maxLayer)474 void defiComponent::setRouteHalo(int haloDist, const char* minLayer,
475                                  const char* maxLayer) {
476   int len;
477 
478   haloDist_ = haloDist;
479   if ((len = strlen(minLayer)+1) > minLayerSize_)
480     bumpMinLayer(len);
481   strcpy(minLayer_, defData->DEFCASE(minLayer));
482   if ((len = strlen(maxLayer)+1) > maxLayerSize_)
483     bumpMaxLayer(len);
484   strcpy(maxLayer_, defData->DEFCASE(maxLayer));
485 }
486 
changeIdAndName(const char * id,const char * name)487 void defiComponent::changeIdAndName(const char* id, const char* name) {
488   int len;
489 
490   if ((len = strlen(id)+1) > idSize_)
491     bumpId(len);
492   strcpy(id_, defData->DEFCASE(id));
493 
494   if ((len = strlen(name)+1) > nameSize_)
495     bumpName(len);
496   strcpy(name_, defData->DEFCASE(name));
497 }
498 
499 
id() const500 const char* defiComponent::id() const {
501   return id_;
502 }
503 
504 
name() const505 const char* defiComponent::name() const {
506   return name_;
507 }
508 
509 
placementStatus() const510 int defiComponent::placementStatus() const {
511   return status_;
512 }
513 
514 
placementX() const515 int defiComponent::placementX() const {
516   return x_;
517 }
518 
519 
placementY() const520 int defiComponent::placementY() const {
521   return y_;
522 }
523 
524 
placementOrient() const525 int defiComponent::placementOrient() const {
526   return orient_;
527 }
528 
529 
placementOrientStr() const530 const char* defiComponent::placementOrientStr() const {
531   return (defiOrientStr(orient_));
532 }
533 
534 
regionName() const535 const char* defiComponent::regionName() const {
536   return regionName_;
537 }
538 
539 
EEQ() const540 const char* defiComponent::EEQ() const {
541   return EEQ_;
542 }
543 
544 
generateName() const545 const char* defiComponent::generateName() const {
546   return generateName_;
547 }
548 
549 
macroName() const550 const char* defiComponent::macroName() const {
551   return macroName_;
552 }
553 
554 
regionBounds(int * size,int ** xl,int ** yl,int ** xh,int ** yh) const555 void defiComponent::regionBounds(int* size,
556 	  int** xl, int** yl, int** xh, int** yh) const {
557   *size = numRects_;
558   *xl = rectXl_;
559   *yl = rectYl_;
560   *xh = rectXh_;
561   *yh = rectYh_;
562 }
563 
564 
bumpId(int size)565 void defiComponent::bumpId(int size) {
566   if (id_) free(id_);
567   id_ = (char*)malloc(size);
568   idSize_ = size;
569   *(id_) = '\0';
570 }
571 
572 
bumpName(int size)573 void defiComponent::bumpName(int size) {
574   if (name_) free(name_);
575   name_ = (char*)malloc(size);
576   nameSize_ = size;
577   *(name_) = '\0';
578 }
579 
580 
bumpRegionName(int size)581 void defiComponent::bumpRegionName(int size) {
582   if (regionName_) free(regionName_);
583   regionName_ = (char*)malloc(size);
584   regionNameSize_ = size;
585   *(regionName_) = '\0';
586 }
587 
588 
bumpEEQ(int size)589 void defiComponent::bumpEEQ(int size) {
590   if (EEQ_) free(EEQ_);
591   EEQ_ = (char*)malloc(size);
592   EEQSize_ = size;
593   *(EEQ_) = '\0';
594 }
595 
596 
bumpMinLayer(int size)597 void defiComponent::bumpMinLayer(int size) {
598   if (minLayer_) free(minLayer_);
599   minLayer_ = (char*)malloc(size);
600   minLayerSize_ = size;
601   *(minLayer_) = '\0';
602 }
603 
604 
bumpMaxLayer(int size)605 void defiComponent::bumpMaxLayer(int size) {
606   if (maxLayer_) free(maxLayer_);
607   maxLayer_ = (char*)malloc(size);
608   maxLayerSize_ = size;
609   *(maxLayer_) = '\0';
610 }
611 
clear()612 void defiComponent::clear() {
613   int i;
614 
615   if (id_)
616      *(id_) = '\0';
617   if (name_)
618      *(name_) = '\0';
619   if (regionName_)
620      *(regionName_) = '\0';
621   if (foreignName_)
622      *(foreignName_) = '\0';
623   if (EEQ_)
624      *(EEQ_) = '\0';
625   if (minLayer_)
626      *(minLayer_) = '\0';
627   if (maxLayer_)
628      *(maxLayer_) = '\0';
629   Fori_ = 0;
630   status_ = 0;
631   hasRegionName_ = 0;
632   hasForeignName_ = 0;
633   hasFori_ = 0;
634   hasEEQ_ = 0;
635   hasWeight_ = 0;
636   hasGenerate_ = 0;
637   if (maskShiftSize_) {
638      free((int*)(maskShift_));
639   }
640   maskShift_ = 0;
641   maskShiftSize_ = 0;
642   weight_ = 0;
643   if (source_) free(source_);
644   for (i = 0; i < numNets_; i++) {
645     free(nets_[i]);
646   }
647   numNets_ = 0;
648   source_ = 0;
649   hasHalo_ = 0;
650   hasHaloSoft_ = 0;
651   haloDist_ = 0;
652   leftHalo_ = 0;
653   bottomHalo_ = 0;
654   rightHalo_ = 0;
655   topHalo_ = 0;
656   for (i = 0; i < numProps_; i++) {
657     free(names_[i]);
658     free(values_[i]);
659     dvalues_[i] = 0;
660   }
661   numProps_ = 0;
662   numRects_ = 0;
663 }
664 
665 
isUnplaced() const666 int defiComponent::isUnplaced() const {
667   return status_ == DEFI_COMPONENT_UNPLACED ? 1 : 0 ;
668 }
669 
670 
isPlaced() const671 int defiComponent::isPlaced() const {
672   return status_ == DEFI_COMPONENT_PLACED ? 1 : 0 ;
673 }
674 
675 
isFixed() const676 int defiComponent::isFixed() const {
677   return status_ == DEFI_COMPONENT_FIXED ? 1 : 0 ;
678 }
679 
680 
isCover() const681 int defiComponent::isCover() const {
682   return status_ == DEFI_COMPONENT_COVER ? 1 : 0 ;
683 }
684 
685 
print(FILE * fout) const686 void defiComponent::print(FILE* fout) const {
687   fprintf(fout, "Component id '%s' name '%s'",
688       id(),
689       name());
690   if (isPlaced()) {
691     fprintf(fout, " Placed at %d,%d orient %s",
692     placementX(),
693     placementY(),
694     placementOrientStr());
695   }
696   if (isFixed()) {
697     fprintf(fout, " Fixed at %d,%d orient %s",
698     placementX(),
699     placementY(),
700     placementOrientStr());
701   }
702   if (isCover()) {
703     fprintf(fout, " Cover at %d,%d orient %s",
704     placementX(),
705     placementY(),
706     placementOrientStr());
707   }
708   fprintf(fout, "\n");
709 
710   if (hasGenerate()) {
711     fprintf(fout, "  generate %s %s\n", generateName(),
712     macroName());
713   }
714   if (hasWeight()) {
715     fprintf(fout, "  weight %d\n", weight());
716   }
717   if (maskShiftSize()) {
718       fprintf(fout, "  maskShift ");
719 
720       for (int i = 0; i < maskShiftSize(); i++) {
721         fprintf(fout, " %d", maskShift(i));
722       }
723       fprintf(fout, "\n");
724   }
725   if (hasSource()) {
726     fprintf(fout, "  source '%s'\n", source());
727   }
728   if (hasEEQ()) {
729     fprintf(fout, "  EEQ '%s'\n", EEQ());
730   }
731 
732   if (hasRegionName()) {
733     fprintf(fout, "  Region '%s'\n", regionName());
734   }
735   if (hasRegionBounds()) {
736     int size;
737     int *xl, *yl, *xh, *yh;
738     int j;
739     regionBounds(&size, &xl, &yl, &xh, &yh);
740     for (j = 0; j < size; j++)
741       fprintf(fout, "  Region bounds %d,%d %d,%d\n", xl[j], yl[j], xh[j], yh[j]);
742   }
743   if (hasNets()) {
744     int i;
745     fprintf(fout, " Net connections:\n");
746     for (i = 0; i < numNets(); i++) {
747       fprintf(fout, "  '%s'\n", net(i));
748     }
749   }
750 }
751 
752 
hasRegionName() const753 int defiComponent::hasRegionName() const {
754   return (int)(hasRegionName_);
755 }
756 
757 
hasGenerate() const758 int defiComponent::hasGenerate() const {
759   return (int)(hasGenerate_);
760 }
761 
762 
hasWeight() const763 int defiComponent::hasWeight() const {
764   return (int)(hasWeight_);
765 }
766 
maskShiftSize() const767 int defiComponent::maskShiftSize() const {
768     return maskShiftSize_;
769 }
770 
hasSource() const771 int defiComponent::hasSource() const {
772   return source_ ? 1 : 0;
773 }
774 
775 
hasRegionBounds() const776 int defiComponent::hasRegionBounds() const {
777   return numRects_ ? 1 : 0 ;
778 }
779 
780 
hasEEQ() const781 int defiComponent::hasEEQ() const {
782   return (int)(hasEEQ_);
783 }
784 
785 
hasNets() const786 int defiComponent::hasNets() const {
787   return numNets_ ? 1 : 0;
788 }
789 
790 
numNets() const791 int defiComponent::numNets() const {
792   return numNets_;
793 }
794 
795 
796 // 5.6
hasHalo() const797 int defiComponent::hasHalo() const {
798   return hasHalo_;
799 }
800 
801 
802 // 5.7
hasHaloSoft() const803 int defiComponent::hasHaloSoft() const {
804   return hasHaloSoft_;
805 }
806 
807 
808 // 5.7
hasRouteHalo() const809 int defiComponent::hasRouteHalo() const {
810   return haloDist_;
811 }
812 
813 // 5.7
haloDist() const814 int defiComponent::haloDist() const {
815   return haloDist_;
816 }
817 
818 // 5.7
minLayer() const819 const char* defiComponent::minLayer() const {
820   return minLayer_;
821 }
822 
823 // 5.7
maxLayer() const824 const char* defiComponent::maxLayer() const {
825   return maxLayer_;
826 }
827 
haloEdges(int * left,int * bottom,int * right,int * top)828 void defiComponent::haloEdges(int* left, int* bottom, int* right, int* top) {
829   *left = leftHalo_;
830   *bottom = bottomHalo_;
831   *right = rightHalo_;
832   *top = topHalo_;
833 }
834 
reverseNetOrder()835 void defiComponent::reverseNetOrder() {
836   // Reverse the order of the items in the nets array.
837   int one = 0;
838   int two = numNets_ - 1;
839   char* t;
840   while (one < two) {
841     t = nets_[one];
842     nets_[one] = nets_[two];
843     nets_[two] = t;
844     one++;
845     two--;
846   }
847 }
848 
849 
propName(int index) const850 char* defiComponent::propName(int index) const {
851   if (index < 0 || index >= numProps_) {
852     defiError(1, 0, "bad index for component property", defData);
853     return 0;
854   }
855   return names_[index];
856 }
857 
858 
propValue(int index) const859 char* defiComponent::propValue(int index) const {
860   if (index < 0 || index >= numProps_) {
861     defiError(1, 0, "bad index for component property", defData);
862     return 0;
863   }
864   return values_[index];
865 }
866 
867 
propNumber(int index) const868 double defiComponent::propNumber(int index) const {
869   if (index < 0 || index >= numProps_) {
870     defiError(1, 0, "bad index for component property", defData);
871     return 0;
872   }
873   return dvalues_[index];
874 }
875 
876 
propType(int index) const877 char defiComponent::propType(int index) const {
878   if (index < 0 || index >= numProps_) {
879     defiError(1, 0, "bad index for component property", defData);
880     return 0;
881   }
882   return types_[index];
883 }
884 
885 
propIsNumber(int index) const886 int defiComponent::propIsNumber(int index) const {
887   if (index < 0 || index >= numProps_) {
888     defiError(1, 0, "bad index for component property", defData);
889     return 0;
890   }
891   return dvalues_[index] ? 1 : 0;
892 }
893 
propIsString(int index) const894 int defiComponent::propIsString(int index) const {
895   if (index < 0 || index >= numProps_) {
896     defiError(1, 0, "bad index for component property", defData);
897     return 0;
898   }
899   return dvalues_[index] ? 0 : 1;
900 }
901 
numProps() const902 int defiComponent::numProps() const {
903   return numProps_;
904 }
905 
906 
addProperty(const char * name,const char * value,const char type)907 void defiComponent::addProperty(const char* name, const char* value,
908                                 const char type) {
909   int len = strlen(name) + 1;
910   if (numProps_ == propsAllocated_) {
911     int i;
912     char**  nn;
913     char**  nv;
914     double* nd;
915     char*   nt;
916 
917     propsAllocated_ *= 2;
918     nn = (char**)malloc(sizeof(char*)*propsAllocated_);
919     nv = (char**)malloc(sizeof(char*)*propsAllocated_);
920     nd = (double*)malloc(sizeof(double)*propsAllocated_);
921     nt = (char*)malloc(sizeof(char)*propsAllocated_);
922     for (i = 0; i < numProps_; i++) {
923       nn[i] = names_[i];
924       nv[i] = values_[i];
925       nd[i] = dvalues_[i];
926       nt[i] = types_[i];
927     }
928     free((char*)(names_));
929     free((char*)(values_));
930     free((char*)(dvalues_));
931     free((char*)(types_));
932     names_ = nn;
933     values_ = nv;
934     dvalues_ = nd;
935     types_ = nt;
936   }
937   names_[numProps_] = (char*)malloc(len);
938   strcpy(names_[numProps_], defData->DEFCASE(name));
939   len = strlen(value) + 1;
940   values_[numProps_] = (char*)malloc(len);
941   strcpy(values_[numProps_], defData->DEFCASE(value));
942   dvalues_[numProps_] = 0;
943   types_[numProps_] = type;
944   numProps_ += 1;
945 }
946 
947 
addNumProperty(const char * name,const double d,const char * value,const char type)948 void defiComponent::addNumProperty(const char* name, const double d,
949                                    const char* value, const char type) {
950   int len = strlen(name) + 1;
951   if (numProps_ == propsAllocated_) {
952     int i;
953     char**  nn;
954     char**  nv;
955     double* nd;
956     char*   nt;
957 
958     propsAllocated_ *= 2;
959     nn = (char**)malloc(sizeof(char*)*propsAllocated_);
960     nv = (char**)malloc(sizeof(char*)*propsAllocated_);
961     nd = (double*)malloc(sizeof(double)*propsAllocated_);
962     nt = (char*)malloc(sizeof(char)*propsAllocated_);
963     for (i = 0; i < numProps_; i++) {
964       nn[i] = names_[i];
965       nv[i] = values_[i];
966       nd[i] = dvalues_[i];
967       nt[i] = types_[i];
968     }
969     free((char*)(names_));
970     free((char*)(values_));
971     free((char*)(dvalues_));
972     free((char*)(types_));
973     names_ = nn;
974     values_ = nv;
975     dvalues_ = nd;
976     types_ = nt;
977   }
978   names_[numProps_] = (char*)malloc(len);
979   strcpy(names_[numProps_], defData->DEFCASE(name));
980   len = strlen(value) + 1;
981   values_[numProps_] = (char*)malloc(len);
982   strcpy(values_[numProps_], defData->DEFCASE(value));
983   dvalues_[numProps_] = d;
984   types_[numProps_] = type;
985   numProps_ += 1;
986 }
987 
988 
addNet(const char * net)989 void defiComponent::addNet(const char* net) {
990   int len = strlen(net) + 1;
991   if (numNets_ == netsAllocated_)
992     bumpNets(numNets_ * 2);
993   nets_[numNets_] = (char*)malloc(len);
994   strcpy(nets_[numNets_], defData->DEFCASE(net));
995   (numNets_)++;
996 }
997 
998 
bumpNets(int size)999 void defiComponent::bumpNets(int size) {
1000   int i;
1001   char** newNets = (char**)malloc(sizeof(char*)* size);
1002   for (i = 0; i < numNets_; i++) {
1003     newNets[i] = nets_[i];
1004   }
1005   free((char*)(nets_));
1006   nets_ = newNets;
1007   netsAllocated_ = size;
1008 }
1009 
1010 
net(int index) const1011 const char* defiComponent::net(int index) const {
1012   if (index >= 0 && index < numNets_) {
1013     return nets_[index];
1014   }
1015   return 0;
1016 }
1017 
1018 
bumpForeignName(int size)1019 void defiComponent::bumpForeignName(int size) {
1020   if (foreignName_) free(foreignName_);
1021   foreignName_ = (char*)malloc(sizeof(char) * size);
1022   foreignNameSize_ = size;
1023   *(foreignName_) = '\0';
1024 }
1025 
1026 
setForeignName(const char * name)1027 void defiComponent::setForeignName(const char* name) {
1028   int len;
1029 
1030   if (hasForeignName())
1031       defiError(1, 0,
1032       "Multiple define of '+ FOREIGN' in COMPONENT is not supported.\n", defData);
1033   if ((len = strlen(name)+1) > foreignNameSize_)
1034     bumpForeignName(len);
1035   strcpy(foreignName_, defData->DEFCASE(name));
1036   hasForeignName_ = 1;
1037 }
1038 
1039 
setForeignLocation(int x,int y,int orient)1040 void defiComponent::setForeignLocation(int x, int y, int orient) {
1041   Fx_ = x;
1042   Fy_ = y;
1043   Fori_ = orient;
1044   hasFori_ = 1;
1045 }
1046 
1047 
hasForeignName() const1048 int defiComponent::hasForeignName() const {
1049   return (int)(hasForeignName_);
1050 }
1051 
1052 
foreignName() const1053 const char* defiComponent::foreignName() const {
1054   return foreignName_;
1055 }
1056 
1057 
foreignX() const1058 int defiComponent::foreignX() const {
1059   return Fx_;
1060 }
1061 
1062 
foreignY() const1063 int defiComponent::foreignY() const {
1064   return Fy_;
1065 }
1066 
1067 
hasFori() const1068 int defiComponent::hasFori() const {
1069   return (int)(hasFori_);
1070 }
1071 
foreignOri() const1072 const char* defiComponent::foreignOri() const {
1073   switch (Fori_) {
1074     case 0: return ("N");
1075     case 1: return ("W");
1076     case 2: return ("S");
1077     case 3: return ("E");
1078     case 4: return ("FN");
1079     case 5: return ("FW");
1080     case 6: return ("FS");
1081     case 7: return ("FE");
1082   }
1083   return 0;
1084 }
1085 
foreignOrient() const1086 int defiComponent::foreignOrient() const {
1087   return Fori_;
1088 }
1089 END_LEFDEF_PARSER_NAMESPACE
1090 
1091