Mercurial > hg > Members > Moririn
annotate src/parallel_execution/generate_stub.pl @ 280:2c2e4e597eb0
generate no compile errors
author | mir3636 |
---|---|
date | Fri, 03 Feb 2017 17:47:27 +0900 |
parents | 23767f714f4a |
children | ec0a5b4fba05 |
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} = {}; |
269 | 56 } elsif (/^(\w+)(\*)+ create(\w+)\(/) { |
249 | 57 if (defined $interface) { |
58 die "duplicate interface $interface\n"; | |
59 } | |
60 $interface = $1; | |
269 | 61 $implementation = $3; |
250 | 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+);$/ ) { |
280 | 71 my $ttype = $1; |
72 my $tname = $2; | |
73 if ($ttype =~ /^(union|struct) (\w+)/) { | |
74 $ttype = $2; | |
75 } | |
76 $var{$name}->{$tname} = $ttype; | |
250 | 77 } 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
|
78 $code{$name}->{$1} = 1; |
250 | 79 } |
194 | 80 if (/^}/) { |
81 $inTypedef = 0; | |
82 } | |
83 } | |
84 } | |
85 | |
250 | 86 sub generateStub { |
251 | 87 my($fd,$prevCodeGearName,$dataGearName) = @_; |
274 | 88 print $fd "__code ", $prevCodeGearName ,"_stub(struct Context* context) {\n"; |
251 | 89 print $fd $dataGearName; |
250 | 90 print $fd "\n} \n\n"; |
251 | 91 return 1; |
250 | 92 } |
93 | |
253 | 94 sub generateStubArgs { |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
95 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
|
96 my $varname1 = $output?"O_$varName":$varName; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
97 for my $n ( @{$dataGearVar{$codeGearName}} ) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
98 # we already have it |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
99 return 0 if ( $n eq $varname1); |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
100 } |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
101 push @{$dataGearVar{$codeGearName}}, $varname1; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
102 if ($typeName eq $implementation) { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
103 # get implementation |
258 | 104 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $interface, $varName);\n"; |
253 | 105 } else { |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
106 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
|
107 # input data gear field |
253 | 108 if ($varName eq $ivar) { |
280 | 109 if ($typeName eq $var{$interface}->{$ivar}) { |
110 if ($output) { | |
111 $dataGearName{$codeGearName} .= "\t$typeName** O_$varName = &Gearef(context, $interface)->$varName;\n"; | |
112 $outputVar{$codeGearName} .= "\t$typeName* $varName;\n"; | |
113 return 1; | |
114 } | |
115 | |
116 $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
|
117 return 1; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
118 } |
253 | 119 } |
120 } | |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
121 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
|
122 if ($varName eq $cName) { |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
123 # 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
|
124 $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
|
125 return 1; |
253 | 126 } |
127 } | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
128 # global variable case |
268 | 129 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName);\n"; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
130 return 1; |
253 | 131 } |
132 } | |
133 | |
194 | 134 sub generateDataGear { |
249 | 135 my ($filename) = @_; |
136 open my $in,"<",$filename or die("can't open $filename $!"); | |
254 | 137 |
138 my $fn; | |
139 if ($opt_o) { | |
140 $fn = $opt_o; | |
141 } else { | |
142 my $fn1 = $filename; | |
143 $fn1 =~ s/\.cbc/.c/; | |
144 my $i = 1; | |
255 | 145 $fn = "$dir/$fn1"; |
254 | 146 while ( -f $fn) { |
255 | 147 $fn = "$dir/$fn1.$i"; |
254 | 148 $i++; |
149 } | |
250 | 150 } |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
151 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
|
152 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
|
153 mkdir $1; |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
154 } |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
155 } |
249 | 156 open my $fd,">",$fn or die("can't write $fn $!"); |
254 | 157 |
249 | 158 my $prevCodeGearName; |
250 | 159 my $inTypedef = 0; |
278 | 160 my $inStub = 0; |
250 | 161 my %stub; |
251 | 162 my $codeGearName; |
254 | 163 |
249 | 164 while (<$in>) { |
278 | 165 if (! $inTypedef && ! $inStub) { |
249 | 166 if (/^typedef struct (\w+) {/) { |
167 $inTypedef = 1; | |
253 | 168 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) { |
251 | 169 $codeGearName = $1; |
253 | 170 my $args = $2; |
171 my $tail = $3; | |
250 | 172 if ($codeGearName =~ /_stub$/) { |
262 | 173 # don't touch already existing stub |
278 | 174 $inStub = 1; |
250 | 175 $stub{$codeGearName} = 1; |
176 print $fd $_; | |
177 next; | |
178 } | |
249 | 179 if (defined $prevCodeGearName) { |
262 | 180 # stub is generated just before next CodeGear |
250 | 181 if (defined $stub{$prevCodeGearName."_stub"}) { |
182 undef $prevCodeGearName; | |
278 | 183 } else { |
184 &generateStub($fd,$prevCodeGearName,$dataGearName{$prevCodeGearName}); | |
185 $stub{$prevCodeGearName."_stub"} = 1; | |
250 | 186 } |
249 | 187 } |
262 | 188 # analyzing CodeGear argument |
189 # these arguments are extract from current context's arugment DataGear Interface | |
190 # and passed to the CodeGear | |
191 # struct Implementaion needs special handling | |
192 # __code next(...) ---> enum Code next | |
253 | 193 $prevCodeGearName = $codeGearName; |
194 $dataGearVar{$codeGearName} = []; | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
195 $outputVar{$codeGearName} = ""; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
196 $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
|
197 my $newArgs = "struct Context *context,"; |
253 | 198 while($args) { |
199 if ($args =~ s/(^\s*,\s*)//) { | |
200 $newArgs .= $1; | |
201 } | |
262 | 202 # continuation case |
280 | 203 if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(([^)]*)\)//) { |
204 my $next = $2; | |
205 my @args = split(/,/,$3); | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
206 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
|
207 $newArgs .= "enum Code $next"; |
258 | 208 } |
262 | 209 # analyze continuation arguments |
210 # output arguments are defined in the Interface take the pointer of these | |
211 # output arguments are put into the Interface DataGear just before the goto | |
253 | 212 for my $arg (@args) { |
213 $arg =~ s/^\s*//; | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
214 last if ($arg =~ /\.\.\./); |
253 | 215 $arg =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//; |
216 my $structType = $1; | |
217 my $typeName = $2; | |
218 my $varName = $4; | |
219 my $typeField = lcfirst($typeName); | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
220 push(@{$outputArgs{$codeGearName}->{$next}}, $varName); |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
221 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
|
222 $newArgs .= ",$structType $typeName **O_$varName"; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
223 } |
253 | 224 } |
225 } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) { | |
226 my $structType = $1; | |
227 my $typeName = $2; | |
228 my $varName = $4; | |
229 my $typeField = lcfirst($typeName); | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
230 $newArgs .= $&; # assuming no duplicate |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
231 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface,0); |
255 | 232 } elsif ($args =~ s/(.*,)//) { |
233 $newArgs .= $1; | |
234 } else { | |
235 $newArgs .= $args; | |
236 last; | |
253 | 237 } |
238 } | |
262 | 239 # generate goto statement from stub to the CodeGear in the buffer |
253 | 240 $dataGearName{$codeGearName} .= "\tgoto $codeGearName(context"; |
241 for my $arg ( @{$dataGearVar{$codeGearName}}) { | |
242 $dataGearName{$codeGearName} .= ", $arg"; | |
243 } | |
244 $dataGearName{$codeGearName} .= ");"; | |
262 | 245 # generate CodeGear header with new arguments |
253 | 246 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
|
247 if ($outputVar{$codeGearName} ne "") { |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
248 print $fd $outputVar{$codeGearName}; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
249 } |
250 | 250 next; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
251 } elsif (/^(.*)goto (\w+)\((.*)\);/) { |
278 | 252 # handling goto statement |
262 | 253 # convert it to the meta call form with two arugments, that is context and enum Code |
250 | 254 my $prev = $1; |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
255 my $next = $2; |
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
256 my @args = split(/,/,$3); |
278 | 257 if (defined $code{$interface}->{$next}) { |
258 # write continuation's arguments into the interface arguments | |
259 # we may need a commit for a shared DataGear | |
260 for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) { | |
261 my $v = shift(@args); | |
262 print $fd "\t*O_$arg = $v;\n"; | |
263 } | |
264 print $fd "${prev}goto meta(context, $next);\n"; | |
265 next; | |
260
6b5444bbea8a
generated stub no comiple errors
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
259
diff
changeset
|
266 } |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
267 } else { |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
255
diff
changeset
|
268 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new |
249 | 269 } |
270 # 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
|
271 } elsif (/^}/) { |
278 | 272 $inStub = 0; |
249 | 273 $inTypedef = 0; |
274 } | |
250 | 275 print $fd $_; |
194 | 276 } |
250 | 277 if (defined $prevCodeGearName) { |
278 if (!defined $stub{$prevCodeGearName."_stub"}) { | |
251 | 279 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName}); |
250 | 280 } |
228 | 281 } |
194 | 282 } |
283 | |
284 # end |