Mercurial > hg > Members > Moririn
annotate src/parallel_execution/generate_stub.pl @ 261:0cd43e22aee1
merge
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 28 Jan 2017 15:48:54 +0900 |
parents | 6b5444bbea8a 4fe19a06d666 |
children | 2c56a9536c0d |
rev | line source |
---|---|
194 | 1 #!/usr/bin/perl |
2 | |
250 | 3 use strict; |
254 | 4 use Getopt::Std; |
250 | 5 |
194 | 6 # interface.cbc |
7 # typedef struct Worker { | |
8 # int id; | |
9 # struct Context* contexts; | |
10 # enum Code execute; | |
11 # enum Code taskSend; | |
12 # enum Code taskRecive; | |
13 # enum Code shutdown; | |
14 # struct Queue* tasks; | |
15 # } Worker; | |
16 | |
255 | 17 our($opt_o,$opt_d,$opt_h); |
18 getopts('o:d:h'); | |
19 | |
20 my $dir = "."; | |
21 if ($opt_d) { | |
22 $dir = $opt_d; | |
23 if (! -d $dir) { | |
24 mkdir $dir; | |
25 } | |
26 } | |
254 | 27 |
28 for my $fn (@ARGV) { | |
255 | 29 next if ($fn !~ /\.cbc$/); |
249 | 30 &getDataGear($fn); |
31 &generateDataGear($fn); | |
194 | 32 } |
33 | |
250 | 34 my %var; |
35 my %code; | |
36 my %dataGearVar; | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
37 my %outputVar; # output var initializer |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
38 my %outputArgs; # continuation's output variables |
250 | 39 my %dataGear; |
40 my %dataGearName; | |
253 | 41 my $implementation; |
42 my $interface; | |
250 | 43 |
194 | 44 sub getDataGear { |
45 my ($filename) = @_; | |
253 | 46 my ($codeGearName, $name, $inTypedef); |
194 | 47 open my $fd,"<",$filename or die("can't open $filename $!"); |
48 while (<$fd>) { | |
49 if (! $inTypedef) { | |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
50 if (/^typedef struct (\w+)/) { |
194 | 51 $inTypedef = 1; |
52 $name = $1; | |
53 $dataGear{$name} = $_; | |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
54 $var{$name} = {}; |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
55 $code{$name} = {}; |
249 | 56 } elsif (/^(\w+)\* create(\w+)\(/) { |
57 if (defined $interface) { | |
58 die "duplicate interface $interface\n"; | |
59 } | |
60 $interface = $1; | |
250 | 61 $implementation = $2; |
62 if ( -f "$interface.cbc") { | |
63 &getDataGear("$interface.cbc"); | |
64 } | |
226 | 65 } |
194 | 66 next; |
67 } | |
249 | 68 # gather type name and type |
194 | 69 $dataGear{$name} .= $_; |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
70 if (/^\s*(.*)\s+(\w+);$/ ) { |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
71 $var{$name}->{$2} = $1; |
250 | 72 } elsif (/\_\_code (\w+)\(/) { |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
73 $code{$name}->{$1} = 1; |
250 | 74 } |
194 | 75 if (/^}/) { |
76 $inTypedef = 0; | |
77 } | |
78 } | |
79 } | |
80 | |
250 | 81 sub generateStub { |
251 | 82 my($fd,$prevCodeGearName,$dataGearName) = @_; |
250 | 83 print $fd "__code ", $prevCodeGearName ,"_stub (struct Context* context) {\n"; |
251 | 84 print $fd $dataGearName; |
250 | 85 print $fd "\n} \n\n"; |
251 | 86 return 1; |
250 | 87 } |
88 | |
253 | 89 sub generateStubArgs { |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
90 my($codeGearName, $varName, $typeName, $typeField, $interface,$output) = @_; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
91 my $varname1 = $output?"O_$varName":$varName; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
92 for my $n ( @{$dataGearVar{$codeGearName}} ) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
93 # we already have it |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
94 return 0 if ( $n eq $varname1); |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
95 } |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
96 push @{$dataGearVar{$codeGearName}}, $varname1; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
97 if ($typeName eq $implementation) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
98 # get implementation |
258 | 99 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $interface, $varName);\n"; |
253 | 100 } else { |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
101 for my $ivar (keys %{$var{$interface}}) { |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
102 # input data gear field |
253 | 103 if ($varName eq $ivar) { |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
104 if ($output) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
105 $dataGearName{$codeGearName} .= "\t$typeName** O_$varName = &Gearef(context, $interface)->$varName;\n"; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
106 $outputVar{$codeGearName} .= "\t$typeName* $varName;\n"; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
107 return 1; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
108 } |
253 | 109 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $interface)->$varName;\n"; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
110 return 1; |
253 | 111 } |
112 } | |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
113 for my $cName (keys %{$code{$interface}}) { |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
114 if ($varName eq $cName) { |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
115 # continuation field |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
116 $dataGearName{$codeGearName} .= "\tenum Code $varName = Gearef(context, $interface)->$varName;\n"; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
117 return 1; |
253 | 118 } |
119 } | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
120 # global variable case |
253 | 121 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName)->$typeField;\n"; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
122 return 1; |
253 | 123 } |
124 } | |
125 | |
194 | 126 sub generateDataGear { |
249 | 127 my ($filename) = @_; |
128 open my $in,"<",$filename or die("can't open $filename $!"); | |
254 | 129 |
130 my $fn; | |
131 if ($opt_o) { | |
132 $fn = $opt_o; | |
133 } else { | |
134 my $fn1 = $filename; | |
135 $fn1 =~ s/\.cbc/.c/; | |
136 my $i = 1; | |
255 | 137 $fn = "$dir/$fn1"; |
254 | 138 while ( -f $fn) { |
255 | 139 $fn = "$dir/$fn1.$i"; |
254 | 140 $i++; |
141 } | |
250 | 142 } |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
143 if ( $fn =~ m=(.*)/[^/]+$= ) { |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
144 if (! -d $1) { |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
145 mkdir $1; |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
146 } |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
147 } |
249 | 148 open my $fd,">",$fn or die("can't write $fn $!"); |
254 | 149 |
249 | 150 my $prevCodeGearName; |
250 | 151 my $inTypedef = 0; |
152 my %stub; | |
251 | 153 my $codeGearName; |
254 | 154 |
249 | 155 while (<$in>) { |
156 if (! $inTypedef) { | |
157 if (/^typedef struct (\w+) {/) { | |
158 $inTypedef = 1; | |
253 | 159 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) { |
251 | 160 $codeGearName = $1; |
253 | 161 my $args = $2; |
162 my $tail = $3; | |
250 | 163 if ($codeGearName =~ /_stub$/) { |
164 $stub{$codeGearName} = 1; | |
165 print $fd $_; | |
166 next; | |
167 } | |
249 | 168 if (defined $prevCodeGearName) { |
250 | 169 if (defined $stub{$prevCodeGearName."_stub"}) { |
170 undef $prevCodeGearName; | |
171 print $fd $_; | |
172 next; | |
173 } | |
253 | 174 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$prevCodeGearName}); |
249 | 175 } |
253 | 176 $prevCodeGearName = $codeGearName; |
177 $dataGearVar{$codeGearName} = []; | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
178 $outputVar{$codeGearName} = ""; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
179 $outputArgs{$codeGearName} = {}; |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
180 my $newArgs = "struct Context *context,"; |
253 | 181 while($args) { |
182 if ($args =~ s/(^\s*,\s*)//) { | |
183 $newArgs .= $1; | |
184 } | |
185 # replace __code next | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
186 if ($args =~ s/^\_\_code\s+(\w+)\(([^)]*)\)//) { |
253 | 187 my $next = $1; |
188 my @args = split(/,/,$2); | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
189 if ( &generateStubArgs($codeGearName, $next, "Code", $next, $interface,0) ) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
190 $newArgs .= "enum Code $next"; |
258 | 191 } |
253 | 192 for my $arg (@args) { |
193 $arg =~ s/^\s*//; | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
194 last if ($arg =~ /\.\.\./); |
253 | 195 $arg =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//; |
196 my $structType = $1; | |
197 my $typeName = $2; | |
198 my $varName = $4; | |
199 my $typeField = lcfirst($typeName); | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
200 push(@{$outputArgs{$codeGearName}->{$next}}, $varName); |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
201 if (&generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface,1)) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
202 $newArgs .= ",$structType $typeName **O_$varName"; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
203 } |
253 | 204 } |
205 } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) { | |
206 my $structType = $1; | |
207 my $typeName = $2; | |
208 my $varName = $4; | |
209 my $typeField = lcfirst($typeName); | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
210 $newArgs .= $&; # assuming no duplicate |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
211 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface,0); |
255 | 212 } elsif ($args =~ s/(.*,)//) { |
213 $newArgs .= $1; | |
214 } else { | |
215 $newArgs .= $args; | |
216 last; | |
253 | 217 } |
218 } | |
219 $dataGearName{$codeGearName} .= "\tgoto $codeGearName(context"; | |
220 for my $arg ( @{$dataGearVar{$codeGearName}}) { | |
221 $dataGearName{$codeGearName} .= ", $arg"; | |
222 } | |
223 $dataGearName{$codeGearName} .= ");"; | |
224 print $fd "__code $codeGearName($newArgs)$tail\n"; | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
225 if ($outputVar{$codeGearName} ne "") { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
226 print $fd $outputVar{$codeGearName}; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
227 } |
250 | 228 next; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
229 } elsif (/^(.*)goto (\w+)\((.*)\);/) { |
250 | 230 my $prev = $1; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
231 my $next = $2; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
232 my @args = split(/,/,$3); |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
233 # write continuation's arguments |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
234 # we may need a commit for a shared DataGear |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
235 for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
236 my $v = shift(@args); |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
237 print $fd "\t*O_$arg = $v;\n"; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
238 } |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
239 print $fd "${prev}goto meta(context, $next);\n"; |
250 | 240 next; |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
241 } else { |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
242 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new |
249 | 243 } |
244 # gather type name and type | |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
245 } elsif (/^}/) { |
249 | 246 $inTypedef = 0; |
247 } | |
250 | 248 print $fd $_; |
194 | 249 } |
250 | 250 if (defined $prevCodeGearName) { |
251 if (!defined $stub{$prevCodeGearName."_stub"}) { | |
251 | 252 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName}); |
250 | 253 } |
228 | 254 } |
194 | 255 } |
256 | |
257 # end |