This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
RtlFile.cpp
1985 lines (1653 loc) · 44.7 KB
/
RtlFile.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2022 Intel Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include <limits.h>
#include "common.h"
#include "RtlFile.h"
#define FI_SINGLE_BIT 0
static size_t uuidGet()
{
static size_t counter = 2; // 1 is reserved, see GlobalFiModInstNumberTop_
return counter++;
}
static void backslashToDoubleBackslash(std::string * out, const std::string & in)
{
out->resize(0);
out->reserve(2 * in.size());
for(const auto &elem: in)
{
if('\\' == elem)
{
out->append("\\\\");
}
else
{
out->push_back(elem);
}
}
}
RtlFile::RtlFile() {
// TODO Auto-generated constructor stub
}
RtlFile::~RtlFile() {
if(nullptr != Content_)
{
free(Content_);
}
}
int RtlFile::Get(const char * fileName, const std::string &topModule)
{
if(nullptr != Content_)
{
nfiError("RtlFile already contains content\n");
return -1;
}
Name_ = fileName;
FILE * pFile = fopen(fileName, "r");
if(nullptr == pFile)
{
nfiError("failed to open file %s\n", fileName);
return -1;
}
if(fseek(pFile, 0L, SEEK_END))
{
nfiError("SEEK_END failed on file %s\n", fileName);
fclose(pFile); // no write performed, so no need to check
return -1;
}
const long nFile = ftell(pFile);
if(0 > nFile)
{
nfiError("ftell failed on file %s\n", fileName);
fclose(pFile); // no write performed, so no need to check
return -1;
}
rewind(pFile);
Content_ = (char *) malloc(nFile + 1);
if(NULL == Content_)
{
nfiError("Could not malloc %lu bytes for %s\n", nFile + 1, fileName);
fclose(pFile); // no write performed, so no need to check
return -3;
}
Size_ = fread(
(void *) Content_,
sizeof(char), nFile, pFile);
fclose(pFile); // no write performed, so no need to check
if(Size_ != nFile)
{
nfiError("Could not read %s\n", fileName);
return -4;
}
Content_[Size_] = '\0';
Size_++;
TopModule_ = topModule;
return 0;
}
static const char * strrstr(const char * haystack, const char * pos, const char * needle)
{
const size_t needleLen = strlen(needle);
if(needleLen > pos - haystack)
{
return nullptr;
}
const char * currPos = pos - needleLen;
while(currPos - needleLen > haystack)
{
if(0 == strncmp(currPos, needle, needleLen))
{
return currPos;
}
currPos--;
}
return nullptr;
}
bool RtlFile::PosInsideComment(const char * pos, const char * start, const char * end)
{
// Inside block comment?
for(size_t blockType = 0; blockType < sizeof(blockCommentStartStr) / sizeof(blockCommentStartStr[0]); blockType++)
{
// Find next block end
const char * blockEnd = strstr(pos, blockCommentEndStr[blockType]);
if((nullptr != blockEnd) && (blockEnd < end))
{
// Is that blockEnd opened before pos?
const char * blockStart = strrstr(pos, blockEnd, blockCommentStartStr[blockType]);
if(nullptr == blockStart)
{
return true;
}
}
}
// Inside line comment?
const char * currPos = pos;
do {
if('\n' == *currPos)
{
break;
}
if(('/' == *currPos) && ('/' == *(currPos - 1)))
{
return true;
}
currPos--;
} while(currPos > start);
return false;
}
// Returns negative on error, positive when module was found
// start Pointer to module start (after name)
// end Pointer to module end (after "endmodule")
int RtlFile::ModuleFind(std::string * name, const char ** start, const char ** end, const char * pFile, size_t nFile)
{
const char modStartStr[] = "module ";
const char modEndStr[] = "endmodule";
const char * modStart = strstr(pFile, modStartStr); // TODO: no strnstr?!?
if(nullptr == modStart)
{
return 0; // no module in this file
}
modStart += sizeof(modStartStr) - 1; // -1 for '\0'
// Module name either ends with # or with (
const char * modNameEndHash = strchr(modStart, '#');
const char * modNameEndParenth = strchr(modStart, '(');
const char * modNameEnd = NULL;
if((NULL != modNameEndHash) && (NULL != modNameEndParenth))
{
modNameEnd = modNameEndHash < modNameEndParenth ? modNameEndHash : modNameEndParenth;
}
else if(NULL != modNameEndHash)
{
modNameEnd = modNameEndHash;
}
else if(NULL != modNameEndParenth)
{
modNameEnd = modNameEndParenth;
}
else
{
nfiError("Could not find module name in %.30s\n", modStart);
return -5;
}
if(modNameEnd >= pFile + nFile)
{
nfiError("Module name ends after file in %.30s\n", modStart);
return -6;
}
// Remove whitespace
while(modStart < modNameEnd)
{
if(' ' != *modStart)
{
break;
}
modStart++;
}
modNameEnd--; // don't be one after end
while(modStart < modNameEnd)
{
if((' ' != *modNameEnd) && ('\n' != *modNameEnd) &&
('\r' != *modNameEnd))
{
break;
}
modNameEnd--;
}
modNameEnd++; // be one after end again
// Module inside comment?
if(PosInsideComment(modStart, pFile, pFile + nFile))
{
return ModuleFind(name, start, end, modStart, nFile - (modStart - pFile));
}
// Save module name
if(modNameEnd == modStart)
{
nfiError("Empty module name\n");
return -6;
}
char tmpName[200];
if(sizeof(tmpName) < modNameEnd - modStart + 1) // +1 for '\0'
{
nfiError("Module name '%.30s' too large\n", modStart);
return -7;
}
memcpy(tmpName, modStart, modNameEnd - modStart);
tmpName[modNameEnd - modStart] = '\0';
*name = tmpName;
*start = modNameEnd;
// Find module end
const char * modEnd = strstr(pFile, modEndStr); // TODO: no strnstr?!?
if(nullptr == modEnd)
{
nfiError("module doesn't end: %s\n", name->c_str());
return -1;
}
modEnd += sizeof(modEndStr) - 1; // -1 for '\0'
if(modEnd >= pFile + nFile)
{
nfiError("module ends after end of file: %s\n", name->c_str());
return -2;
}
if(nullptr != strrstr(modNameEnd, modEnd, modStartStr))
{
nfiError("Module declaration inside module: %s\n", name->c_str());
return -3;
}
*end = modEnd;
return 1;
}
const char * RtlFile::NextNeedle(size_t * needleNr, const char * pHaystack, size_t nHaystack)
{
const size_t nNeedles = sizeof(fiNeedles) / sizeof(fiNeedles[0]);
std::vector<const char *> fiNeedle(nNeedles);
for(size_t needle = 0; needle < nNeedles; needle++)
{
fiNeedle[needle] = strstr(pHaystack, fiNeedles[needle]);
if(fiNeedle[needle] > pHaystack + nHaystack)
{
fiNeedle[needle] = nullptr;
}
}
const char * ret = pHaystack + nHaystack;
for(size_t needle = 0; needle < nNeedles; needle++)
{
if((nullptr != fiNeedle[needle]) && (fiNeedle[needle] < ret))
{
ret = fiNeedle[needle];
*needleNr = needle;
}
}
if(ret == pHaystack + nHaystack)
{
return nullptr;
}
// Needle inside comment?
if(PosInsideComment(ret, pHaystack, pHaystack + nHaystack))
{
return NextNeedle(needleNr, ret + 1, nHaystack - (ret + 1 - pHaystack));
}
return ret;
}
static bool isSpace(char c)
{
return (' ' == c) || ('\t' == c);
}
static const char * firstNonSpaceGet(const char * in)
{
const char * out = in;
while(isSpace(*out))
{
out++;
}
return out;
}
static const char * lastNonSpaceGet(const char * in)
{
const char * out = in;
while(isSpace(*out))
{
out--;
}
return out;
}
static const char * lastCharAfterGet(const char * in, const char * pNeedles, size_t nNeedles)
{
const char * out = in;
while(*out)
{
for(size_t needle = 0; needle < nNeedles; needle++)
{
if(*out == pNeedles[needle])
{
return out + 1;
}
}
out--;
}
return out;
}
// searches before in for type declaration
RtlFile::signalType_t RtlFile::TypeGet(const char ** declStart, const char * in, const char * inModuleStart)
{
const char * typeStart[SIGNAL_TYPE_NROF];
for(size_t type = 0; type < SIGNAL_TYPE_NROF; type++)
{
typeStart[type] = strrstr(inModuleStart, in, SignalTypes[type]);
}
*declStart = inModuleStart;
signalType_t ret = SIGNAL_TYPE_NROF;
for(size_t type = 0; type < SIGNAL_TYPE_NROF; type++)
{
if((nullptr != typeStart[type]) && (*declStart < typeStart[type]))
{
*declStart = typeStart[type];
ret = (signalType_t) type;
}
}
// Check if the type declaration belongs to signal
if(SIGNAL_TYPE_NROF != ret)
{
const char * illegalSemiColon = strchr(*declStart, ';');
if((nullptr != illegalSemiColon) && (illegalSemiColon <= in))
{
return SIGNAL_TYPE_NROF; // This is not a declaration
}
}
return ret;
}
const char * RtlFile::SignalDeclarationGet(const std::string &signalName, const char * inModuleStart)
{
const char * moduleEnd = strstr(inModuleStart, "endmodule");
if(nullptr == moduleEnd)
{
nfiError("Module without endmodule\n");
return nullptr;
}
// find end of module input list
const char * moduleInputEnd = strchr(inModuleStart, ')');
if(nullptr == moduleInputEnd)
{
nfiError("Module input list doesn't end\n");
return nullptr;
}
if(moduleInputEnd >= moduleEnd)
{
nfiError("Module input list doesn't end\n");
return nullptr;
}
// Search declaration
bool foundDecl = false;
const char * signalIoDecl = nullptr;
const char * signalDecl = moduleInputEnd;
while(signalDecl < moduleEnd)
{
signalDecl = strstr(signalDecl, signalName.c_str());
if(nullptr == signalDecl)
{
break;
}
if(!PosInsideComment(signalDecl, inModuleStart, moduleEnd))
{
const char * typeStart;
const signalType_t type = TypeGet(&typeStart, signalDecl, inModuleStart);
if((SIGNAL_TYPE_INPUT == type) || (SIGNAL_TYPE_OUTPUT == type))
{
signalIoDecl = typeStart;
}
else if(SIGNAL_TYPE_NROF != type)
{
foundDecl = true;
signalDecl = typeStart;
break;
}
}
signalDecl++;
}
if(!foundDecl && (nullptr != signalIoDecl))
{
signalDecl = signalIoDecl; // e.g. "input [a:b] signal" will default to wire
}
else if(!foundDecl)
{
nfiError("Could not find signal declaration\n");
return nullptr;
}
nfiDebug("declaration '%.30s' (io decl '%.30s')\n", signalDecl, signalIoDecl != nullptr ? signalIoDecl : "N.A.");
return signalDecl;
}
// returns <= 0 on error, else signal width
static int signalWidthGet(const char ** endWidth, const char * in)
{
if('[' != in[0])
{
nfiError("First char expected to be [\n");
return -1;
}
char * widthColon;
long widthHigh = strtol(in + 1, &widthColon, 10);
if(in + 1 == widthColon)
{
nfiError("Found no number\n");
return -1;
}
if((LONG_MIN == widthHigh) || (LONG_MAX == widthHigh))
{
nfiError("Width doesn't fit long\n");
return -1;
}
if(':' != *widthColon)
{
nfiError("Expected colon after width high: %.30s\n", in);
return -1;
}
char * widthEnd;
long widthLow = strtol(widthColon + 1, &widthEnd, 10);
if(widthColon + 1 == widthEnd)
{
nfiError("Found no number\n");
return -1;
}
if((LONG_MIN == widthLow) || (LONG_MAX == widthLow))
{
nfiError("Width doesn't fit long\n");
return -1;
}
if(']' != *widthEnd)
{
nfiError("Expected ] after width low: %.30s\n", in);
return -1;
}
*endWidth = widthEnd;
return abs(widthHigh - widthLow) + 1;
}
// returns <= 0 on error, else signal width
static int signalArraySizeGet(const char ** endArray, const char * in)
{
if('[' != in[0])
{
nfiError("First char expected to be [\n");
return -1;
}
char * widthHighEnd;
long widthHigh = strtol(in + 1, &widthHighEnd, 10);
if(in + 1 == widthHighEnd)
{
nfiError("Found no number\n");
return -1;
}
if((LONG_MIN == widthHigh) || (LONG_MAX == widthHigh))
{
nfiError("Width doesn't fit long\n");
return -1;
}
if(']' == *widthHighEnd)
{
// System Verilog array declaration 'signal[arraySize]'
*endArray = widthHighEnd;
return widthHigh;
}
if(':' != *widthHighEnd)
{
nfiError("Expected colon after width high: %.30s\n", in);
return -1;
}
char * widthEnd;
long widthLow = strtol(widthHighEnd + 1, &widthEnd, 10);
if(widthHighEnd + 1 == widthEnd)
{
nfiError("Found no number\n");
return -1;
}
if((LONG_MIN == widthLow) || (LONG_MAX == widthLow))
{
nfiError("Width doesn't fit long\n");
return -1;
}
if(']' != *widthEnd)
{
nfiError("Expected ] after width low: %.30s\n", in);
return -1;
}
*endArray = widthEnd;
return abs(widthHigh - widthLow) + 1;
}
// returns <= 0 on error, else signal width
static int subSignalArraySizeGet(const char ** endArray, const char * in)
{
if('[' != in[0])
{
nfiError("First char expected to be [\n");
return -1;
}
char * widthHighEnd;
long widthHigh = strtol(in + 1, &widthHighEnd, 10);
if(in + 1 == widthHighEnd)
{
nfiError("Found no number\n");
return -1;
}
if((LONG_MIN == widthHigh) || (LONG_MAX == widthHigh))
{
nfiError("Width doesn't fit long\n");
return -1;
}
if(']' == *widthHighEnd)
{
*endArray = widthHighEnd;
return 1; // only one element chosen
}
if(':' != *widthHighEnd)
{
nfiError("Expected colon after width high: %.30s\n", in);
return -1;
}
char * widthEnd;
long widthLow = strtol(widthHighEnd + 1, &widthEnd, 10);
if(widthHighEnd + 1 == widthEnd)
{
nfiError("Found no number\n");
return -1;
}
if((LONG_MIN == widthLow) || (LONG_MAX == widthLow))
{
nfiError("Width doesn't fit long\n");
return -1;
}
if(']' != *widthEnd)
{
nfiError("Expected ] after width low: %.30s\n", in);
return -1;
}
*endArray = widthEnd;
return abs(widthHigh - widthLow) + 1;
}
int RtlFile::SignalDeclarationParse(signal_t * signal, const char * declaration)
{
// Get Type
const char * typeStart[SIGNAL_TYPE_NROF];
for(size_t type = 0; type < SIGNAL_TYPE_NROF; type++)
{
typeStart[type] = strstr(declaration, SignalTypes[type]);
}
signal->Type = SIGNAL_TYPE_NROF;
const char * pType = declaration + 1000; // TODO: DIRTY
for(size_t type = 0; type < SIGNAL_TYPE_NROF; type++)
{
if((nullptr != typeStart[type]) && (pType > typeStart[type]))
{
pType = typeStart[type];
signal->Type = (signalType_t) type;
}
}
if(SIGNAL_TYPE_NROF == signal->Type)
{
nfiError("Couldn't get signal type\n");
return -1;
}
// Get width
const char * afterType = typeStart[signal->Type] + strlen(SignalTypes[signal->Type]);
afterType = firstNonSpaceGet(afterType);
if('[' == *afterType)
{
int width = signalWidthGet(&afterType, afterType);
if(0 >= width)
{
nfiError("signalWidthGet failed: %.30s\n", declaration);
return -1;
}
signal->Width = width;
afterType++; // after ]
afterType = firstNonSpaceGet(afterType);
}
else
{
signal->Width = 1;
}
// Get Name
const char * endNameSemiColon = strchr(afterType, ';');
const char * endNameSpace = strchr(afterType, ' ');
const char * endName = nullptr;
if((nullptr != endNameSemiColon) && (nullptr != endNameSpace))
{
endName = (endNameSemiColon < endNameSpace) ? endNameSemiColon : endNameSpace;
}
else if(nullptr != endNameSemiColon)
{
endName = endNameSemiColon;
}
else if(nullptr != endNameSpace)
{
endName = endNameSpace;
}
else
{
nfiError("Name doesn't end\n");
return -1;
}
const size_t nameLen = endName - afterType;
std::vector<char> name(nameLen + 1);
memcpy(name.data(), afterType, nameLen);
name[nameLen] = '\0';
signal->Name = name.data();
const char * afterName = afterType + nameLen;
// Get Array Element Count
const char * arrayStart = strchr(afterName, '[');
if((nullptr != arrayStart) && (arrayStart < endNameSemiColon))
{
const char * tmp;
int arraySize = signalArraySizeGet(&tmp, arrayStart);
if(0 >= arraySize)
{
nfiError("signalArraySizeGet failed\n");
return -1;
}
signal->ElemCnt = arraySize;
}
else
{
signal->ElemCnt = 1;
}
return 0;
}
// Returns < 1 on error, else the signal width
int RtlFile::SubSignalWidthGet(const std::string &inSubSignal, const char * inModuleStart)
{
// Extract signal name
const char * widthStart = nullptr;
if('\\' != inSubSignal[0])
{
widthStart = strchr(inSubSignal.c_str(), '[');
}
else
{
widthStart = strstr(inSubSignal.c_str(), " [");
if(nullptr != widthStart)
{
widthStart++; // i.e. point to '['
}
}
if(nullptr == widthStart)
{
nfiError("Could not find '['\n");
return -1;
}
const size_t nameLen = widthStart - inSubSignal.c_str();
std::vector<char> signalName(nameLen + 1);
memcpy(signalName.data(), inSubSignal.c_str(), nameLen);
signalName[nameLen] = '\0';
// Find signal declaration
const char * signalDecl = SignalDeclarationGet(signalName.data(), inModuleStart);
if(nullptr == signalDecl)
{
nfiError("SignalDeclarationGet failed\n");
return -1;
}
signal_t signal;
if(SignalDeclarationParse(&signal, signalDecl))
{
nfiError("SignalDeclarationParse failed\n");
return -1;
}
// What width is subsignal
const char * tmp;
int arraySize = subSignalArraySizeGet(&tmp, widthStart);
if(0 >= arraySize)
{
nfiError("signalArraySizeGet failed\n");
return -1;
}
// TODO: Only supporting single dimensional sub and arrays
// Is signal an array?
if(1 < signal.ElemCnt)
{
// Assume the sub is an array index
if(arraySize > signal.ElemCnt)
{
nfiError("subsignal is larger than array declaration\n");
return -1;
}
return arraySize * signal.Width;
}
if(arraySize > signal.Width)
{
nfiError("subsignal is larger than width declaration\n");
return -1;
}
return arraySize;
}
int RtlFile::NeedleCorrupt(
fiMode_t fiMode, module_t * module, std::map<const char *, diff_t> * diff,
const std::string &fiPrefix, const char * moduleStart, const char * moduleEnd, const char * needle, fiNeedle_t needleNr)
{
nfiDebug("Needle '%.30s'\n", needle);
// Find target signal
const char * targetSignalStart = nullptr;
switch(needleNr)
{
case FI_NEEDLE_ASSIGN:
targetSignalStart = needle + strlen(fiNeedles[FI_NEEDLE_ASSIGN]);
break;
case FI_NEEDLE_ASSIGN_NON_BLOCKIN:
{
const char * assigneeEnd = lastNonSpaceGet(needle - 1);
targetSignalStart = lastCharAfterGet(assigneeEnd, "\n )", 3);
}
break;
default:
nfiError("Unknown fiNeedle_t %i\n", needleNr);
return -1;
}
if((nullptr == targetSignalStart) || (moduleStart > targetSignalStart))
{
nfiError("Couldn't find signal name\n");
return -1;
}
// Remove spaces
targetSignalStart = firstNonSpaceGet(targetSignalStart);
nfiDebug("Needle expression: %.70s\n", targetSignalStart);
// Find end of signal name
const char * targetSignalEndEqual = nullptr;
switch(needleNr)
{
case FI_NEEDLE_ASSIGN:
targetSignalEndEqual = strchr(targetSignalStart, '=');
break;
case FI_NEEDLE_ASSIGN_NON_BLOCKIN:
targetSignalEndEqual = strchr(targetSignalStart, '<');
break;
// coverity[DEADCODE]
default:
nfiError("Unknown fiNeedle_t %i\n", needleNr);
return -1;
}
if((nullptr == targetSignalEndEqual) || (moduleEnd < targetSignalEndEqual))
{
nfiError("Couldn't find signal name\n");
return -1;
}
// Remove spaces
targetSignalEndEqual -= 1; // remove char found above
targetSignalEndEqual = lastNonSpaceGet(targetSignalEndEqual);
const char * targetSignalEnd = targetSignalEndEqual + 1; // one after last char
std::vector<char> targetSignalName(targetSignalEnd - targetSignalStart + 1);
memcpy(targetSignalName.data(), targetSignalStart, targetSignalEnd - targetSignalStart);
targetSignalName[targetSignalEnd - targetSignalStart] = '\0';
// Is it a compound signal? I.e. {signal1, signal2,..}
std::vector<std::string> signalNames;
if('{' == targetSignalName[0])
{
// Extract the signals
const char * compoundEnd = strchr(targetSignalName.data(), '}');
if(nullptr == compoundEnd)
{
nfiError("Compound signal that doesn't end\n");
return -1;
}
const char * currSignalStart = targetSignalName.data() + 1;
while(currSignalStart < compoundEnd)
{
currSignalStart = firstNonSpaceGet(currSignalStart);
const char * nextSignalStart = strchr(currSignalStart, ',');
bool lastRound = false;
if(nullptr == nextSignalStart)
{
lastRound = true;
nextSignalStart = compoundEnd;
}
const char * currSignalEnd = nextSignalStart - 1;
currSignalEnd = lastNonSpaceGet(currSignalEnd);
std::vector<char> tmpBuff(currSignalEnd + 1 - currSignalStart + 1);
memcpy(tmpBuff.data(), currSignalStart, currSignalEnd + 1 - currSignalStart);
tmpBuff[currSignalEnd + 1 - currSignalStart] = '\0';
signalNames.push_back(tmpBuff.data());
if(!lastRound)
{
currSignalStart = nextSignalStart + 1; // one after ','
}
else
{
break;
}
}
}
else
{
signalNames.push_back(targetSignalName.data());
}
// Get required signal width
int compoundSignalWidth = 0;
for(size_t sigIndex = 0; sigIndex < signalNames.size(); sigIndex++)
{
if(200 < signalNames[sigIndex].size())
{
nfiError("signalName longer than 200 chars: %.200s\n", signalNames[sigIndex].c_str());
return -1;
}
nfiDebug("targetSignal '%s'\n", signalNames[sigIndex].c_str());
int fiSignalWidth;
// Subsignal?
// TODO: Subsignal with escape name?!?
// if signal identifier starts with escape character, '[' is allowed as part of name.
// In that case, an actual subsignal '[' will be separated by a space
const char * targetSignalEndBracket = nullptr;
if('\\' != signalNames[sigIndex][0])
{
targetSignalEndBracket = strchr(signalNames[sigIndex].c_str(), '[');
}
else
{
targetSignalEndBracket = strstr(signalNames[sigIndex].c_str(), " [");
}
if(nullptr != targetSignalEndBracket)
{
fiSignalWidth = SubSignalWidthGet(signalNames[sigIndex].c_str(), moduleStart);