annotate src/parallel_execution/generate_stub.pl @ 253:ebc13549394c

generate stubArgs
author mir3636
date Thu, 26 Jan 2017 19:52:08 +0900
parents 682b1195e604
children edb3aff688d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
1 #!/usr/bin/perl
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
2
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
3 use strict;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
4
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
5 # interface.cbc
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
6 # typedef struct Worker {
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
7 # int id;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
8 # struct Context* contexts;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
9 # enum Code execute;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
10 # enum Code taskSend;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
11 # enum Code taskRecive;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
12 # enum Code shutdown;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
13 # struct Queue* tasks;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
14 # } Worker;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
15
252
682b1195e604 fix stub
mir3636
parents: 251
diff changeset
16 #while (<*.cbc>) {
682b1195e604 fix stub
mir3636
parents: 251
diff changeset
17 while (<SingleLinkedStack.cbc>) {
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
18 my $fn = $_;
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
19 &getDataGear($fn);
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
20 &generateDataGear($fn);
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
21 }
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
22
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
23 my %var;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
24 my %type;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
25 my %code;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
26 my %dataGearVar;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
27 my %dataGear;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
28 my %dataGearName;
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
29 my $implementation;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
30 my $interface;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
31
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
32 sub getDataGear {
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
33 my ($filename) = @_;
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
34 my ($codeGearName, $name, $inTypedef);
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
35 open my $fd,"<",$filename or die("can't open $filename $!");
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
36 while (<$fd>) {
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
37 if (! $inTypedef) {
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
38 if (/^typedef struct (\w+) {/) {
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
39 $inTypedef = 1;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
40 $name = $1;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
41 $dataGear{$name} = $_;
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
42 $code{$name} = [];
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
43 } elsif (/^(\w+)\* create(\w+)\(/) {
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
44 if (defined $interface) {
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
45 die "duplicate interface $interface\n";
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
46 }
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
47 $interface = $1;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
48 $implementation = $2;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
49 if ( -f "$interface.cbc") {
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
50 &getDataGear("$interface.cbc");
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
51 }
226
68c03e7057d9 get arg
mir3636
parents: 201
diff changeset
52 }
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
53 next;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
54 }
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
55 # gather type name and type
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
56 $dataGear{$name} .= $_;
196
bd96dffaa76a generate_stub
mir3636
parents: 194
diff changeset
57 if (/(\w+);$/ and !/^} (\w+)/) {
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
58 my $tmp = $1 . "\n";
200
207aff30ed34 fix generate_stub.pl
mir3636
parents: 196
diff changeset
59 if (/{/) {
207aff30ed34 fix generate_stub.pl
mir3636
parents: 196
diff changeset
60 $tmp = "{" . $';
201
mir3636
parents: 200
diff changeset
61 $tmp =~ s/;$//;
200
207aff30ed34 fix generate_stub.pl
mir3636
parents: 196
diff changeset
62 }
207aff30ed34 fix generate_stub.pl
mir3636
parents: 196
diff changeset
63 $var{$name} .= $tmp;
196
bd96dffaa76a generate_stub
mir3636
parents: 194
diff changeset
64 $tmp = $`;
bd96dffaa76a generate_stub
mir3636
parents: 194
diff changeset
65 $tmp =~ s/^\s*//;
bd96dffaa76a generate_stub
mir3636
parents: 194
diff changeset
66 $type{$name} .= $tmp . "\n";
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
67 } elsif (/\_\_code (\w+)\(/) {
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
68 push $code{$name}, $1;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
69 }
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
70 if (/^}/) {
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
71 $inTypedef = 0;
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
72 }
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
73 }
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
74 }
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
75
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
76 sub generateStub {
251
0ab52d6e2fd9 fix sub generateStub
mir3636
parents: 250
diff changeset
77 my($fd,$prevCodeGearName,$dataGearName) = @_;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
78 print $fd "__code ", $prevCodeGearName ,"_stub (struct Context* context) {\n";
251
0ab52d6e2fd9 fix sub generateStub
mir3636
parents: 250
diff changeset
79 print $fd $dataGearName;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
80 print $fd "\n} \n\n";
251
0ab52d6e2fd9 fix sub generateStub
mir3636
parents: 250
diff changeset
81 return 1;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
82 }
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
83
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
84 sub generateStubArgs {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
85 my($codeGearName, $varName, $typeName, $typeField, $interface) = @_;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
86 push @{$dataGearVar{$codeGearName}},$varName;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
87 if ($typeField ne $varName) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
88 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $typeName, $varName);\n";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
89 # print STDOUT "$codeGearName \t$typeName* $varName = ($typeName*)GearImpl(context, $typeName, $varName);\n";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
90 } else {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
91 for my $ivar ($var{$interface}) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
92 if ($varName eq $ivar) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
93 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
94 # print STDOUT "$codeGearName \t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
95 return;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
96 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
97 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
98 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName)->$typeField;\n";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
99 # print STDOUT "$codeGearName \t$typeName* $varName = Gearef(context, $typeName)->$typeField;\n";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
100 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
101 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
102
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
103 sub generateDataGear {
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
104 my ($filename) = @_;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
105 my $fn1 = $filename;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
106 $fn1 =~ s/\.cbc/.c/;
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
107 open my $in,"<",$filename or die("can't open $filename $!");
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
108 my $i = 1;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
109 my $fn = $fn1;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
110 while ( -f $fn) {
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
111 $fn = "$fn1.$i";
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
112 $i++;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
113 }
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
114 open my $fd,">",$fn or die("can't write $fn $!");
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
115 my $prevCodeGearName;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
116 my $inTypedef = 0;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
117 my %stub;
251
0ab52d6e2fd9 fix sub generateStub
mir3636
parents: 250
diff changeset
118 my $codeGearName;
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
119 while (<$in>) {
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
120 if (! $inTypedef) {
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
121 if (/^typedef struct (\w+) {/) {
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
122 $inTypedef = 1;
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
123 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
251
0ab52d6e2fd9 fix sub generateStub
mir3636
parents: 250
diff changeset
124 $codeGearName = $1;
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
125 my $args = $2;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
126 my $tail = $3;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
127 if ($codeGearName =~ /_stub$/) {
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
128 $stub{$codeGearName} = 1;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
129 print $fd $_;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
130 next;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
131 }
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
132 if (defined $prevCodeGearName) {
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
133 if (defined $stub{$prevCodeGearName."_stub"}) {
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
134 undef $prevCodeGearName;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
135 print $fd $_;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
136 next;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
137 }
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
138 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$prevCodeGearName});
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
139 }
253
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
140 $prevCodeGearName = $codeGearName;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
141 $dataGearVar{$codeGearName} = [];
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
142 my $newArgs = "";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
143 while($args) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
144 if ($args =~ s/(^\s*,\s*)//) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
145 $newArgs .= $1;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
146 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
147 # replace __code next
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
148 if ($args =~ s/^\_\_code\s(\w+)\([^)]*\)//) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
149 my $next = $1;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
150 my @args = split(/,/,$2);
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
151 $newArgs .= "enum Code $next";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
152 for my $arg (@args) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
153 $arg =~ s/^\s*//;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
154 $arg =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
155 my $structType = $1;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
156 my $typeName = $2;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
157 my $varName = $4;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
158 my $typeField = lcfirst($typeName);
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
159 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface);
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
160 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
161 } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
162 my $structType = $1;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
163 my $typeName = $2;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
164 my $varName = $4;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
165 my $typeField = lcfirst($typeName);
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
166 $newArgs .= $&;
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
167 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface);
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
168 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
169 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
170 $dataGearName{$codeGearName} .= "\tgoto $codeGearName(context";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
171 for my $arg ( @{$dataGearVar{$codeGearName}}) {
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
172 $dataGearName{$codeGearName} .= ", $arg";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
173 }
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
174 $dataGearName{$codeGearName} .= ");";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
175 print $fd "__code $codeGearName($newArgs)$tail\n";
ebc13549394c generate stubArgs
mir3636
parents: 252
diff changeset
176 next;
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
177 } elsif (/^(.*)goto next\(\.\.\.(.*)\);/) {
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
178 my $prev = $1;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
179 my $args = $2;
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
180 print $fd "${prev}goto meta(context, next);\n";
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
181 next;
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
182 }
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
183 print $fd $_;
249
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
184 next;
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
185 }
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
186 # gather type name and type
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
187 if (/^}/) {
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
188 $inTypedef = 0;
afa1e02e4386 fix generate stub
mir3636
parents: 243
diff changeset
189 }
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
190 print $fd $_;
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
191 }
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
192 if (defined $prevCodeGearName) {
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
193 if (!defined $stub{$prevCodeGearName."_stub"}) {
251
0ab52d6e2fd9 fix sub generateStub
mir3636
parents: 250
diff changeset
194 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName});
250
8a8963ce9858 fix generate_stub
mir3636
parents: 249
diff changeset
195 }
228
55260ff44c8c generate stub
mir3636
parents: 226
diff changeset
196 }
194
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
197 }
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
198
081607dcf893 create generate_stub.pl
mir3636
parents:
diff changeset
199 # end