-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscpp.pl
executable file
·2163 lines (1694 loc) · 51.5 KB
/
scpp.pl
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
#!/usr/bin/perl -w
##############################################################################
#
# scpp -- SystemC preprocessor
# Copyright(C) by DDS
#
##############################################################################
use strict 'vars';
use strict 'refs';
my $enum = 1;
my $ATTR_REF = $enum; # wire が参照された
my $ATTR_FIX = ( $enum <<= 1 ); # wire に出力された
my $ATTR_BYDIR = ( $enum <<= 1 ); # inout で接続された
my $ATTR_IN = ( $enum <<= 1 ); # 強制 I
my $ATTR_OUT = ( $enum <<= 1 ); # 強制 O
my $ATTR_INOUT = ( $enum <<= 1 ); # 強制 IO
my $ATTR_WIRE = ( $enum <<= 1 ); # 強制 W
my $ATTR_DEF = ( $enum <<= 1 ); # ポート・信号定義済み
my $ATTR_WEAK_DIM = ( $enum <<= 1 ); # array size は弱めの申告
my $ATTR_USED = ( $enum <<= 1 ); # この template が使用された
my $ATTR_IGNORE = ( $enum <<= 1 ); # `ifdef 切り等で本来無いポートを無視する等
my $ATTR_NC = ( $enum <<= 1 ); # 入力 0 固定,出力 open
$enum = 0;
my $BLKMODE_NORMAL = $enum; # ブロック外
my $BLKMODE_IF = $enum++; # if ブロック
my $BLKMODE_ELSE = $enum++; # else ブロック
$enum = 1;
my $EX_CPP = $enum; # マクロ展開
my $EX_STR = $enum <<= 1; # 文字列リテラル
my $EX_RMSTR = $enum <<= 1; # 文字列リテラル削除
my $EX_COMMENT = $enum <<= 1; # コメント
my $EX_RMCOMMENT = $enum <<= 1; # コメント削除
my $EX_SP = $enum <<= 1; # スペース正規化
my $EX_IF_EVAL = $enum <<= 1; # ifdef 用,defined 展開,存在しないシンボルは 0 に変換
my $CSymbol = qr/\b[_a-zA-Z]\w*\b/;
my $DefSkelPort = '(.*)';
my $DefSkelWire = '$1';
my $OpenClose;
$OpenClose = qr/\([^()]*(?:(??{$OpenClose})[^()]*)*\)/;
my $OpenCloseArg = qr/[^(),]*(?:(??{$OpenClose})[^(),]*)*/;
my $OpenCloseBlock;
$OpenCloseBlock = qr/\{[^{}]*(?:(??{$OpenCloseBlock})[^{}]*)*\}/;
my $OpenCloseType;
$OpenCloseType = qr/\<[^<>]*(?:(??{$OpenCloseType})[^<>]*)*\>/;
my $OpenCloseAry;
$OpenCloseAry = qr/\[[^\[\]]*(?:(??{$OpenCloseAry})[^\[]]*)*\]/;
use constant {
SEEK_SET => 0
};
my $ErrorCnt = 0;
# option
my $CppOnly = 0;
my $Debug = 0;
my $CleanDirective = 0;
my @IncludeList;
my @IncludePath;
# 定義テーブル関係
my $CppInfo;
my $FileInfo;
my $ModuleInfo;
my @TmpFileList;
main();
exit( $ErrorCnt != 0 );
### main procedure ###########################################################
sub main{
local( $_ );
if( $#ARGV < 0 ){
print( "usage: scpp.pl [-vE] [-I<path>] [-D<def>[=<val>]] [-o <dst_file>] [--clean] <src_file>\n" );
return;
}
my $DstFile;
while( 1 ){
$_ = $ARGV[ 0 ];
if ( m#^-I(.+)\/?$# ){ push( @IncludePath, $1 );
}elsif( $_ eq '-I' ){
shift( @ARGV );
$ARGV[ 0 ] =~ m#^-I(.+)\/?$#;
push( @IncludePath, $1 );
}elsif( /^-D(.+?)=(.+)/ ){ AddCppMacro( $1, $2 );
}elsif( /^-D(.+)/ ){ AddCppMacro( $1 );
}elsif( $_ eq '-o' ){
shift( @ARGV );
$DstFile = $ARGV[ 0 ];
}elsif( /^--clean$/ ){
$CleanDirective = 1;
}elsif( /^-/ ){
while( s/v// ){ ++$Debug; }
$CppOnly = 1 if( /E/ );
}else { last;
}
shift( @ARGV );
}
# set up default file name
my $SrcFile = $ARGV[ 0 ];
$DstFile = $SrcFile if( !defined( $DstFile ));
$SrcFile =~ /(.*?)(\.def)?(\.[^\.]+)$/;
my $ListFile = "$1.list";
my $DstFileTmp = ( $DstFile eq $SrcFile ) ?
"$SrcFile.$$.scpp.tmp" : $DstFile;
# ディレクティブ内部を消去
if( $CleanDirective ){
return CleanDirective( $SrcFile, $DstFile );
}
# -o 時は一旦コピーする
system( "cp '$SrcFile' '$DstFile'" ) if( $DstFile ne $SrcFile );
return if( !CPreprocessor( $SrcFile ));
if( $CppOnly ){
my $fp = $FileInfo->{ In };
while( <$fp> ){
print( $Debug ? $_ : ExpandMacro( $_, $EX_STR | $EX_COMMENT ));
}
}else{
close( $FileInfo->{ In });
undef( $FileInfo->{ In });
undef( $FileInfo->{ InFile });
# vpp
unlink( $ListFile );
ScppParser();
ScppOutput( $SrcFile, $DstFileTmp ) if( !$ErrorCnt );
OutputWireList( $ListFile ) if( !$ErrorCnt );
if( $DstFile eq $SrcFile ){
# -o 未指定時
# 生成後が SrcFile と同じなら,生成後を削除
# 生成後が SrcFile と差分があれば,生成後を SrcFile に上書き
system( << "-----" ) if( !$ErrorCnt );
if diff -q '$SrcFile' '$DstFileTmp' > /dev/null 2>&1; then
rm '$DstFileTmp'
else
mv -f '$SrcFile' '$SrcFile.bak'
mv -f '$DstFileTmp' '$SrcFile'
fi
-----
}else{
# -o 指定時,エラー時は DstFile 削除
push( @TmpFileList, $DstFile ) if( $ErrorCnt );
}
}
unlink( @TmpFileList );
}
### C プリプロセッサ ########################################################
sub CPreprocessor {
local $_;
$FileInfo = {
LineCnt => 0
};
( $FileInfo->{ InFile }) = @_;
$CppInfo = {
InFile => $FileInfo->{ InFile },
OutFile => "$FileInfo->{ InFile }.$$.cpp.tmp",
BlockNoOutput => 0,
ResetLinePos => 0,
};
push( @TmpFileList, $CppInfo->{ OutFile });
$FileInfo->{ DispFile } = $FileInfo->{ InFile };
if( -e $CppInfo->{ OutFile }){
print( "cpp: $CppInfo->{ OutFile } is exist, cpp skipped...\n" ) if( $Debug >= 2 );
}else{
# cpp 処理開始
if( !open( $FileInfo->{ In }, "< $FileInfo->{ InFile }" )){
Error( "can't open file \"$FileInfo->{ InFile }\"", 0 );
undef $FileInfo->{ In };
return;
}
if( !open( $CppInfo->{ Out }, "> $CppInfo->{ OutFile }" )){
Error( "can't open file \"$CppInfo->{ OutFile }\"", 0 );
close( $FileInfo->{ In } );
undef $FileInfo->{ In };
undef $FileInfo->{ Out };
return;
}
CppParser();
if( $Debug >= 4 ){
print( "=== macro ===\n" );
foreach $_ ( sort keys %{ $CppInfo->{ DefineTbl }} ){
printf( "$_%s\t%s\n",
$CppInfo->{ DefineTbl }{ $_ }{ args } eq 's' ? '' : '()',
$CppInfo->{ DefineTbl }{ $_ }{ macro }
);
}
print( "=== comment =\n" );
print( join( "\n", @{ $CppInfo->{ CommentPool }} ));
print( "\n=============\n" );
}
close( $CppInfo->{ Out } );
close( $FileInfo->{ In } );
}
$FileInfo->{ InFile } = $CppInfo->{ OutFile };
if( !open( $FileInfo->{ In }, "< $FileInfo->{ InFile }" )){
Error( "can't open file \"$FileInfo->{ InFile }\"", 0 );
return;
}
return $FileInfo->{ In };
}
### 1行読む #################################################################
sub ReadLine {
local $_ = ReadLineSub( $FileInfo->{ In });
my( $Cnt );
my( $Line );
my( $LineCnt ) = $.;
my $key;
while( m@(//\s*\$Scpp|//|/\*\s*\$Scpp|/\*|(?<!\\)")@ ){
$Cnt = $#{ $CppInfo->{ CommentPool }} + 1;
$key = $1;
$key =~ s/\s+//g;
if( $key eq '//$Scpp' ){
# // $Scpp のコメント外し
s#//\s*(\$Scpp)#$1#;
}elsif( $key =~ m#^//# ){
# // コメント退避
push( @{ $CppInfo->{ CommentPool }}, $1 ) if( s#(//.*)#<__COMMENT_${Cnt}__># );
}elsif( $key eq '"' ){
# string 退避
if( s/((?<!\\)".*?(?<!\\)")/<__STRING_${Cnt}__>/ ){
push( @{ $CppInfo->{ CommentPool }}, $1 );
}else{
Error( 'unterminated "' );
s/"//;
}
}elsif( $key eq '/*$Scpp' && s#/\*\s*(\$Scpp.*?)\*/#$1#s ){
# /* $Scpp */ コメント外し
}elsif( $key =~ m#/\*# && s#(/\*.*?\*/)#<__COMMENT_${Cnt}__>#s ){
# /* ... */ の組が発見されたら,置換
push( @{ $CppInfo->{ CommentPool }}, $1 );
$CppInfo->{ ResetLinePos } = $.;
}else{
# /* ... */ の組が発見されないので,発見されるまで行 cat
if( !( $Line = ReadLineSub( $FileInfo->{ In } ))){
Error( 'unterminated */', $LineCnt );
last;
}
$_ .= $Line;
}
}
$_;
}
sub ReadLineSub {
my( $fp ) = @_;
local( $_ );
while( <$fp> ){
if( /^#\s*(\d+)\s+"(.*)"/ ){
$. = $1 - 1;
$FileInfo->{ DispFile } = ( $2 eq "-" ) ? $FileInfo->{ InFile } : $2;
}else{
last;
}
}
return '' if( !defined( $_ ));
$_;
}
# 関数マクロ用に ( ... ) を取得
sub GetFuncArg {
local( $_ ) = @_;
my( $Line );
while( !/^$OpenClose/ ){
$CppInfo->{ ResetLinePos } = $.;
if( !( $Line = ReadLine())){
Error( "unmatched ')'" );
last;
}
$_ .= $Line;
}
$_;
}
### cpp parser ##############################################################
sub CppParser {
my( $BlockMode, $bNoOutput ) = @_;
$BlockMode = 0 if( !defined( $BlockMode ));
$bNoOutput = 0 if( !defined( $bNoOutput ));
local( $_ );
$CppInfo->{ BlockNoOutput } <<= 1;
$CppInfo->{ BlockNoOutput } |= $bNoOutput;
my $Line;
my $i;
my $LineCnt = $.;
my $arg;
my $tmp;
while( $_ = ReadLine()){
if( /^\s*#\s*(?:ifdef|ifndef|if|elif|else|endif|define|undef|include)\b/ ){
# \ で終わっている行を連結
while( /\\$/ ){
if( !( $Line = ReadLine())){
last;
}
$_ .= $Line;
}
$CppInfo->{ ResetLinePos } = $.;
# \ 削除
s/[\t ]*\\[\x0D\x0A]+[\t ]*/ /g;
s/\s+$//g;
s/^\s*#\s*//;
$_ = ExpandMacro( $_, $EX_RMCOMMENT );
# $CppInfo->{ DefineTbl }{ $1 }{ args }: >=0: 引数 <0: 可変引数 's': 単純マクロ
# $CppInfo->{ DefineTbl }{ $1 }{ macro }: マクロ定義本体
if( /^ifdef\b(.*)/ ){
CppParser( $BLKMODE_IF, !IfBlockEval( "defined $1" ));
}elsif( /^ifndef\b(.*)/ ){
CppParser( $BLKMODE_IF, IfBlockEval( "defined $1" ));
}elsif( /^if\b(.*)/ ){
CppParser( $BLKMODE_IF, !IfBlockEval( $1 ));
}elsif( /^elif\b(.*)/ ){
if( $BlockMode != $BLKMODE_IF ){
Error( "unexpected #elif" );
}elsif( $bNoOutput ){
# まだ出力していない
$bNoOutput = !IfBlockEval( $1 );
$CppInfo->{ BlockNoOutput } &= ~1;
$CppInfo->{ BlockNoOutput } |= 1 if( $bNoOutput );
}else{
# もう出力した
$CppInfo->{ BlockNoOutput } |= 1;
}
}elsif( /^else\b/ ){
# else
if( $BlockMode != $BLKMODE_IF ){
Error( "unexpected #else" );
}elsif( $bNoOutput ){
# まだ出力していない
$bNoOutput = 0;
$CppInfo->{ BlockNoOutput } &= ~1;
}else{
# もう出力した
$CppInfo->{ BlockNoOutput } |= 1;
}
}elsif( /^endif\b/ ){
# endif
if( $BlockMode != $BLKMODE_IF ){
Error( "unexpected #endif" );
}else{
last;
}
}elsif( !$CppInfo->{ BlockNoOutput } ){
if( /^define\s+($CSymbol)$/ ){
# 名前だけ定義
AddCppMacro( $1 );
}elsif( /^define\s+($CSymbol)\s+(.+)/ ){
# 名前と値定義
AddCppMacro( $1, $2 );
}elsif( /^define\s+($CSymbol)($OpenClose)\s*(.*)/ ){
# 関数マクロ
my( $Name, $ArgList, $Macro ) = ( $1, $2, $3 );
# ArgList 整形,分割
$ArgList =~ s/^\(\s*//;
$ArgList =~ s/\s*\)$//;
my( @ArgList ) = split( /\s*,\s*/, $ArgList );
# マクロ内の引数を特殊文字に置換
my $ArgNum = $#ArgList + 1;
for( $i = 0; $i <= $#ArgList; ++$i ){
if( $i == $#ArgList && $ArgList[ $i ] eq '...' ){
$ArgNum = -$ArgNum;
last;
}
$Macro =~ s/\b$ArgList[ $i ]\b/<__ARG_${i}__>/g;
}
AddCppMacro( $Name, $Macro, $ArgNum );
}elsif( /^undef\s+($CSymbol)$/ ){
# undef
delete( $CppInfo->{ DefineTbl }{ $1 } );
}elsif( /^include\s+(.*)/ ){
Include( $1 );
}else{
# ここには来ないと思う...
Error( "internal error: cpp: \"$1\"\n" );
}
}
}elsif( !$CppInfo->{ BlockNoOutput } ){
$_ = ExpandMacro( $_, $EX_CPP | $EX_RMCOMMENT );
PrintRTL( $_ );
# scpp directive 位置解析
$Line = $_ = ExpandMacro( $_, $EX_SP );
# SrcFile 処理時以外は下記をスキップ
if( $CppInfo->{ InFile } eq $FileInfo->{ InFile }){
if( /^(SC_MODULE)\(($CSymbol)\)/ ){
push( @{ $CppInfo->{ ScppInfo }}, {
Keyword => $1,
ModuleName => $2,
Line => $Line,
LineCnt => $.
});
$CppInfo->{ ModuleName } = $2;
}elsif( !/^\$Scpp(?:Method|Thread|Cthread)/ && /^(\$Scpp\w+)($OpenClose)?/ ){
( $tmp, $_ ) = ( $1, $2 );
# arg 分解
$arg = undef;
if( $_ ){
s/^\(//;
s/\)$//;
@$arg = map { ExpandMacro( $_, $EX_STR )} split( /,/, $_ );
}
push( @{ $CppInfo->{ ScppInfo }}, {
Keyword => $tmp,
Arg => $arg,
ModuleName => $CppInfo->{ ModuleName },
Line => $Line,
LineCnt => $.,
});
print "ScppInfo: $tmp\n" if( $Debug >= 3 );
}
}
}
}
if( $_ eq '' && $BlockMode != $BLKMODE_NORMAL ){
if( $BlockMode == $BLKMODE_IF ){
Error( "unterminated #if", $LineCnt );
}
}
$CppInfo->{ BlockNoOutput } >>= 1;
}
### output normal line #######################################################
sub PrintRTL{
local( $_ ) = @_;
if( $CppInfo->{ ResetLinePos } ){
# ここは根拠がわからない,まだバグってるかも
if( $CppInfo->{ ResetLinePos } == $. ){
$_ .= sprintf( "# %d \"$FileInfo->{ DispFile }\"\n", $. + 1 );
}else{
$_ = sprintf( "# %d \"$FileInfo->{ DispFile }\"\n", $. ) . $_;
}
$CppInfo->{ ResetLinePos } = 0;
}
print( { $CppInfo->{ Out }} $_ );
}
### CPP directive 処理 #######################################################
sub AddCppMacro {
my( $Name, $Macro, $Args, $bNoCheck ) = @_;
$Macro = '1' if( !defined( $Macro ));
$Args = 's' if( !defined( $Args ));
if(
( !defined( $bNoCheck ) || !$bNoCheck ) &&
defined( $CppInfo->{ DefineTbl }{ $Name } ) &&
( $CppInfo->{ DefineTbl }{ $Name }{ args } ne $Args || $CppInfo->{ DefineTbl }{ $Name }{ macro } ne $Macro )
){
Warning( "redefined macro '$Name'" );
}
$CppInfo->{ DefineTbl }{ $Name } = { 'args' => $Args, 'macro' => $Macro };
}
### if ブロック用 eval #######################################################
sub IfBlockEval {
local( $_ ) = @_;
$_ = eval( ExpandMacro( $_, $EX_CPP | $EX_STR | $EX_IF_EVAL ));
if( $@ ne '' ){
my $msg = $@;
$msg =~ s/\n+$//g;
Error( $msg );
}
$_;
}
### CPP マクロ展開 ###########################################################
sub ExpandMacro {
local $_;
my $Mode;
( $_, $Mode ) = @_;
my $Line;
my $Line2;
my $Name;
my( $ArgList, @ArgList );
my $ArgNum;
my $i;
$Mode = $EX_CPP if( !defined( $Mode ));
my $bReplaced = 1;
if( $Mode & $EX_CPP ){
while( $bReplaced ){
$bReplaced = 0;
$Line = '';
while( /(.*?)\b($CSymbol)\b(.*)/s ){
$Line .= $1;
( $Name, $_ ) = ( $2, $3 );
if( $Name eq '__FILE__' ){
$Line .= $FileInfo->{ DispFile };
}elsif( $Name eq '__LINE__' ){
$Line .= $.;
}elsif(
$Name eq 'defined' && $EX_IF_EVAL &&
( s/^\s*($CSymbol)\b// || s/^s*\(\s*($CSymbol)\s*\)// )
){
# defined マクロ
$Line .= defined( $CppInfo->{ DefineTbl }{ $1 } ) ? '1' : '0';
}elsif( !defined( $CppInfo->{ DefineTbl }{ $Name } )){
# マクロではない
# if 用の eval の場合,,未定義シンボルは 0 に変換
$Line .= ( $Mode & $EX_IF_EVAL ) ? '0' : $Name;
}elsif( $CppInfo->{ DefineTbl }{ $Name }{ args } eq 's' ){
# 単純マクロ
$Line .= $CppInfo->{ DefineTbl }{ $Name }{ macro };
$bReplaced = 1;
}else{
# 関数マクロ
s/^\s+//;
if( !/^\(/ ){
# hoge( になってない
Error( "invalid number of macro arg: $Name" );
$Line .= $Name;
}else{
# マクロ引数取得
$_ = GetFuncArg( $_ );
# マクロ引数解析
if( /^($OpenClose)(.*)/s ){
( $ArgList, $_ ) = ( $1, $2 );
$ArgList =~ s/<__COMMENT_\d+__>//g;
$ArgList =~ s/[\t ]*[\x0D\x0A]+[\t ]*/ /g;
$ArgList =~ s/^\(\s*//;
$ArgList =~ s/\s*\)$//;
undef( @ArgList );
while( $ArgList ne '' ){
last if( $ArgList !~ /^\s*($OpenCloseArg)\s*(,?)\s*(.*)/ );
push( @ArgList, $1 );
$ArgList = $3;
if( $2 ne '' && $ArgList eq '' ){
push( @ArgList, '' );
}
}
if( $ArgList eq '' ){
# 引数チェック
$ArgNum = $CppInfo->{ DefineTbl }{ $Name }{ args };
$ArgNum = -$ArgNum - 1 if( $ArgNum < 0 );
if( !(
$CppInfo->{ DefineTbl }{ $Name }{ args } >= 0 ?
( $ArgNum == $#ArgList + 1 ) : ( $ArgNum <= $#ArgList + 1 )
)){
Error( "invalid number of macro arg: $Name" );
$Line .= $Name . '()';
}else{
# 仮引数を実引数に置換
$Line2 = $CppInfo->{ DefineTbl }{ $Name }{ macro };
$Line2 =~ s/<__ARG_(\d+)__>/$ArgList[ $1 ]/g;
# 可変引数を置換
if( $CppInfo->{ DefineTbl }{ $Name }{ args } < 0 ){
if( $#ArgList + 1 <= $ArgNum ){
# 引数 0 個の時は,カンマもろとも消す
$Line2 =~ s/,?\s*(?:##)*\s*__VA_ARGS__\s*/ /g;
}else{
$Line2 =~ s/(?:##\s*)?__VA_ARGS__/join( ', ', @ArgList[ $ArgNum .. $#ArgList ] )/ge;
}
}
$Line .= $Line2;
$bReplaced = 1;
}
}else{
# $ArgList を全部消費しきれなかったらエラー
Error( "invalid macro arg: $Name" );
$Line .= $Name . '()';
}
}
}
}
}
$_ = $Line . $_;
}
# トークン連結演算子 ##
$bReplaced |= s/\s*##\s*//g;
}
# 1L とかの置換
s/\b(\d\w*)(?:L|U|LU|UL|F)\b/$1/gi if( $Mode & $EX_IF_EVAL );
# 文字列削除
if( $Mode & $EX_RMSTR ){
s/<__STRING_\d+__>/ /g;
}
# コメント削除
if( $Mode & $EX_RMCOMMENT ){
s/<__COMMENT_\d+__>/ /g;
}
if( $Mode & $EX_SP ){
# space 最適化
s/^\s+//; s/\s+$//;
s/(\w)\s+(\w)/$1 $2/g;
s/\s*(\W)\s*/$1/g;
}
# 文字列
if( $Mode & $EX_STR ){
# 文字列化
s/\$String($OpenClose)/Stringlize( $1 )/ge;
# 文字列定数復活
s/<__STRING_(\d+)__>/$CppInfo->{ CommentPool }[ $1 ]/g;
# 文字列リテラル連結
1 while( s/((?<!\\)".*?)(?<!\\)"\s*"(.*?(?<!\\)")/$1$2/g );
}
# コメント
if( $Mode & $EX_COMMENT ){
s/<__COMMENT_(\d+)__>/$CppInfo->{ CommentPool }[ $1 ]/g;
}
$_;
}
### sizeof / typeof ##########################################################
sub Stringlize {
local( $_ ) = @_;
s/^\(\s*//;
s/\s*\)$//;
return "\"$_\"";
}
### ファイル include #########################################################
sub SearchIncludeFile {
my( $File ) = @_;
local $_;
$File =~ s/^"(.*)"\s*$/$1/;
$File = ExpandEnv( $File );
# パスに / が含まれていたら何もしない
return $File if( $File =~ m#/# || -e $File );
foreach $_ ( @IncludePath ){
s#/$##;
return "$_/$File" if( -e "$_/$File" );
}
return $File;
}
# $_ == undef の場合,リワインド用の情報だけを push
sub PushFileInfo {
local( $_ ) = @_;
printf( "%d:pushing FileInfo %s -> $_\n",
$#IncludeList + 1,
$FileInfo->{ InFile } || '(undef)'
) if( $Debug >= 2 );
if( $FileInfo->{ InFile }){
$FileInfo->{ RewindPtr } = tell( $FileInfo->{ In } );
$FileInfo->{ LineCnt } = $.;
close( $FileInfo->{ In } ) if( $_ );
}
push( @IncludeList, $FileInfo );
if( $_ ){
$FileInfo = {
InFile => $_,
DispFile => $_,
};
}else{
# deep copy
%$_ = %$FileInfo;
$FileInfo = $_;
$FileInfo->{ Rewind } = 1;
}
}
sub PopFileInfo {
close( $FileInfo->{ In }) if( $FileInfo->{ In } && !$FileInfo->{ Rewind });
print( "$#IncludeList:poping FileInfo $FileInfo->{ InFile } -> " ) if( $Debug >= 2 );
$FileInfo = pop( @IncludeList );
print(( $FileInfo->{ InFile } || '(undef)' ) . "\n" ) if( $Debug >= 2 );
if( $FileInfo->{ InFile }){
open( $FileInfo->{ In }, "< $FileInfo->{ InFile }" );
seek( $FileInfo->{ In }, $FileInfo->{ RewindPtr }, SEEK_SET );
}
$. = $FileInfo->{ LineCnt };
$CppInfo->{ ResetLinePos } = $.;
}
sub Include {
local( $_ ) = @_;
$_ = ExpandMacro( $_, $EX_CPP | $EX_STR );
if( /<.+>/ ){
PrintRTL( "\n" );
return;
}
$_ = SearchIncludeFile( $_ );
PushFileInfo( $_ );
if( !open( $FileInfo->{ In }, "< $_" )){
Error( "can't open include file '$_'" );
}else{
$FileInfo->{ InFile } = $_;
PrintRTL( "# 1 \"$_\"\n" );
print( "including file '$_'...\n" ) if( $Debug >= 2 );
CppParser();
printf( "back to file '%s'...\n", $IncludeList[ $#IncludeList ]->{ InFile } ) if( $Debug >= 2 );
}
PopFileInfo();
}
### 環境変数展開 #############################################################
sub ExpandEnv {
local( $_ ) = @_;
s/(\$\{.+?\})/ExpandEnvSub( $1 )/ge;
s/(\$\(.+?\))/ExpandEnvSub( $1 )/ge;
$_;
}
sub ExpandEnvSub {
local( $_ ) = @_;
my( $org ) = $_;
s/^\$[\(\{]//;
s/[\}\)]$//;
$ENV{ $_ } || $org;
}
### Scpp パーザ ##############################################################
sub ScppParser {
local( $_ );
foreach $_ ( @{ $CppInfo->{ ScppInfo }} ){
$FileInfo->{ LineCnt } = $_->{ LineCnt };
print(
"######################## err=$ErrorCnt\n$_->{ Keyword }:" .
( defined( $_->{ Arg } ) ? join( ", ", @{ $_->{ Arg }} ) : '' ) .
"\n"
) if( $Debug >= 3 );
if( $_->{ Keyword } eq 'SC_MODULE' ){
StartModule( $_ );
}elsif( $_->{ Keyword } eq '$ScppSensitive' ){
GetSensitive( $_ );
}elsif( $_->{ Keyword } eq '$ScppInstance' ){
DefineInstance( $_ );
}elsif( $_->{ Keyword } =~ /^\$Scpp/ ){
#Error( "unknown scpp directive \"$_->{ Keyword }\"" );
}
}
}
### Scpp 最終出力 ############################################################
sub OutputToLineCnt {
my( $fpIn, $fpOut, $LineCnt ) = @_;
local $_;
while( !defined( $LineCnt ) || $fpIn->input_line_number() < $LineCnt ){
if( !( $_ = <$fpIn> )){
Error( "internal error: OutputToLine(): no more line" ) if( $LineCnt );
last;
}
print $fpOut $_ if( defined( $fpOut ));
}
}
sub ScppOutput {
my( $InFile, $OutFile ) = @_;
local( $_ );
my $fpIn;
my $fpOut;
my $SkipToEnd = 0;
my $Scpp;
my $Wire;
my $indent;
my $tmp;
my $ModInfo;
if( !open( $fpIn, "< $InFile" )){
Error( "can't open file \"$InFile\"" );
return;
}
if( !open( $fpOut, "> $OutFile" )){
Error( "can't open file \"$OutFile\"" );
close( $fpIn );
return;
}
foreach $Scpp ( @{ $CppInfo->{ ScppInfo }} ){
if( $SkipToEnd ){
$SkipToEnd = 0;
if( $Scpp->{ Keyword } eq '$ScppEnd' ){
# ScppEnd 直前までスキップ
OutputToLineCnt( $fpIn, undef, $Scpp->{ LineCnt } - 1 );
OutputToLineCnt( $fpIn, $fpOut, $Scpp->{ LineCnt });
next;
}
# ScppEnd が無いエラー
Error( "unexpected scpp directive: $Scpp->{ Keyword }" );
}
# $Scpp 直前まで出力
OutputToLineCnt( $fpIn, $fpOut, $Scpp->{ LineCnt } - 1 );
# $Scpp に Begin をつける
$_ = <$fpIn>;
/^(\s*)/; $indent = $1;
if( $Scpp->{ Keyword } ne 'SC_MODULE' ){
$SkipToEnd = ( $Scpp->{ Line } =~ /\bBegin\s*$/ ) ? 1 : 0;
if( !$SkipToEnd ){
s#\s*\*/\s*$# Begin */\n# || s/\s*$/ Begin\n/;
}
}
print $fpOut $_;
$ModInfo = $ModuleInfo->{ $Scpp->{ ModuleName }};
$FileInfo->{ LineCnt } = $Scpp->{ LineCnt };
print(
"@@@@@@@@@@@@@@@@@@@@@@ err=$ErrorCnt\n$Scpp->{ Keyword }:" .
( defined( $Scpp->{ Arg } ) ? join( ", ", @{ $Scpp->{ Arg }} ) : '' ) .
"\n"
) if( $Debug >= 3 );
if( $Scpp->{ Keyword } eq '$ScppSensitive' ){
# センシティビティリスト集約
OutputSensitive( $fpOut, $ModInfo, $Scpp, $indent );
}elsif( $Scpp->{ Keyword } eq '$ScppInstance' ){
# 自動インスタンス
$tmp = $ModInfo->{ InstanceHash }{ $Scpp->{ Arg }[ 1 ]}{ code };
$tmp =~ s/^/$indent/mg;
print $fpOut $tmp;
}elsif(
$Scpp->{ Keyword } eq '$ScppAutoMember' ||
$Scpp->{ Keyword } eq '$ScppAutoMemberSim'
){
# ScppAutoMember
OutputAutoMember( $fpOut, $ModInfo, $Scpp, $indent );
}elsif( $Scpp->{ Keyword } eq '$ScppSigTrace' ){
# signal trace 出力
OutputSigTrace( $fpOut, $ModInfo, $Scpp, $indent );
}elsif( $Scpp->{ Keyword } eq '$ScppInitializer' ){
# 信号名設定 (初期化子)
if( $#{ $ModInfo->{ WireList }} >= 0 ){
$_ = '';
foreach $Wire ( @{ $ModInfo->{ WireList }} ){
if( $Wire->{ dim } eq '' && !( $Wire->{ attr } & $ATTR_NC )){
$_ .= "$Wire->{ name }( \"$Wire->{ name }\" ),\n";
}
}
if( $_ ){
$_ = ": $_" if( $Scpp->{ Arg }[ 0 ] =~ /:/ );
s/,$// if( $Scpp->{ Arg }[ 0 ] !~ /,/ );
s/^/$indent/gm;
print $fpOut $_;
}
}
}elsif( $Scpp->{ Keyword } =~ /^\$Scpp/ ){
Error( "unknown scpp directive \"$Scpp->{ Keyword }\"" );
}
# $ScppEnd をつける
if( !$SkipToEnd && $Scpp->{ Keyword } ne 'SC_MODULE' ){
# インデント取得
print $fpOut "$indent// \$ScppEnd\n";
}
}
OutputToLineCnt( $fpIn, $fpOut );
close( $fpIn );
close( $fpOut );
}
### Start of the module #####################################################
sub StartModule {
local( $_ );
my( $Scpp ) = @_;
# 親 module の wire / port リストをget