comparison src/parallel_execution/generate_stub.pl @ 396:bba401f93dcd

Add handle par goto statement
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Thu, 24 Aug 2017 15:32:38 +0900
parents 99c50356d917
children c43ec0e3fa84
comparison
equal deleted inserted replaced
395:864cd4e346e9 396:bba401f93dcd
39 my %outputArgs; # continuation's output variables 39 my %outputArgs; # continuation's output variables
40 my %dataGear; 40 my %dataGear;
41 my %dataGearName; 41 my %dataGearName;
42 my %generic; 42 my %generic;
43 my %dataGearVarType; 43 my %dataGearVarType;
44 my %codeGear;
44 my $implementation; 45 my $implementation;
45 my $interface; 46 my $interface;
46 47
47 # interface definision 48 # interface definision
48 # 49 #
105 #my $intfn = ucfirst($2); 106 #my $intfn = ucfirst($2);
106 my $impln = $5; 107 my $impln = $5;
107 if ( -f "$impln.cbc") { 108 if ( -f "$impln.cbc") {
108 &getCodeGear("$impln.cbc"); 109 &getCodeGear("$impln.cbc");
109 } 110 }
111 } elsif(/^(.*)par goto (\w+)\((.*)\)/) {
112 my $codeGearName = $2;
113 if ($filename =~ /^(.*)\/(.*)/) {
114 $codeGearName = "$1/$codeGearName";
115 }
116 if ( -f "$codeGearName.cbc") {
117 &getCodeGear("$codeGearName.cbc");
118 }
110 } 119 }
111 next; 120 next;
112 } 121 }
113 # gather type name and type 122 # gather type name and type
114 $dataGear{$name} .= $_; 123 $dataGear{$name} .= $_;
158 } else { 167 } else {
159 last; 168 last;
160 } 169 }
161 } 170 }
162 } 171 }
172 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
173 my $codeGearName = $1;
174 my $args = $2;
175 my $inputCount = 0;
176 my $outputCount = 0;
177 my $inputIncFlag = 1;
178 while($args) {
179 if ($args =~ s/(^\s*,\s*)//) {
180 }
181 if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(//) {
182 $inputIncFlag = 0;
183 } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) {
184 if($inputIncFlag) {
185 $inputCount++;
186 }
187 else {
188 $outputCount++;
189 }
190 } elsif ($args =~ s/(.*,)//) {
191 } else {
192 last;
193 }
194 }
195 $codeGear{$codeGearName}->{"input"} = $inputCount;
196 $codeGear{$codeGearName}->{"output"} = $outputCount;
163 } 197 }
164 } 198 }
165 } 199 }
166 200
167 sub generateStub { 201 sub generateStub {
192 if ($output) { 226 if ($output) {
193 $dataGearName{$codeGearName} .= "\t$typeName** O_$varName = &Gearef(context, $interface)->$varName;\n"; 227 $dataGearName{$codeGearName} .= "\t$typeName** O_$varName = &Gearef(context, $interface)->$varName;\n";
194 $outputVar{$codeGearName} .= "\t$typeName* $varName;\n"; 228 $outputVar{$codeGearName} .= "\t$typeName* $varName;\n";
195 return 1; 229 return 1;
196 } 230 }
197 231
198 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $interface)->$varName;\n"; 232 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
199 return 1; 233 return 1;
200 } 234 }
201 } 235 }
202 } 236 }
207 return 1; 241 return 1;
208 } 242 }
209 } 243 }
210 # global or local variable case 244 # global or local variable case
211 if ($typeName eq "Code") { 245 if ($typeName eq "Code") {
212 $dataGearName{$codeGearName} .= "\tenum $typeName $varName = Gearef(context, $interface)->$varName;\n"; 246 $dataGearName{$codeGearName} .= "\tenum $typeName $varName = Gearef(context, $interface)->$varName;\n";
213 return 1; 247 return 1;
214 } 248 }
215 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName);\n"; 249 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName);\n";
216 return 1; 250 return 1;
217 } 251 }
221 my ($filename) = @_; 255 my ($filename) = @_;
222 open my $in,"<",$filename or die("can't open $filename $!"); 256 open my $in,"<",$filename or die("can't open $filename $!");
223 257
224 my $fn; 258 my $fn;
225 if ($opt_o) { 259 if ($opt_o) {
226 $fn = $opt_o; 260 $fn = $opt_o;
227 } else { 261 } else {
228 my $fn1 = $filename; 262 my $fn1 = $filename;
229 $fn1 =~ s/\.cbc/.c/; 263 $fn1 =~ s/\.cbc/.c/;
230 my $i = 1; 264 my $i = 1;
231 $fn = "$dir/$fn1"; 265 $fn = "$dir/$fn1";
242 open my $fd,">",$fn or die("can't write $fn $!"); 276 open my $fd,">",$fn or die("can't write $fn $!");
243 277
244 my $prevCodeGearName; 278 my $prevCodeGearName;
245 my $inTypedef = 0; 279 my $inTypedef = 0;
246 my $inStub = 0; 280 my $inStub = 0;
281 my $inParGoto = 0;
247 my %stub; 282 my %stub;
248 my $codeGearName; 283 my $codeGearName;
249 284
250 while (<$in>) { 285 while (<$in>) {
251 if (! $inTypedef && ! $inStub) { 286 if (! $inTypedef && ! $inStub) {
374 } 409 }
375 $i++; 410 $i++;
376 } 411 }
377 print $fd "${prev}goto meta(context, $next->$next->$ntype.$method);\n"; 412 print $fd "${prev}goto meta(context, $next->$next->$ntype.$method);\n";
378 next; 413 next;
414 } elsif(/^(.*)par goto (\w+)\((.*)\);/) {
415 # handling par goto statement
416 # convert it to the parallel
417 my $prev = $1;
418 my $codeGearName = $2;
419 my @dataGears = split(/,\s*/, $3);
420 my $nextCodeGear = pop(@dataGears);
421 my $inputCount = $codeGear{$codeGearName}->{'input'};
422 my $outputCount = $codeGear{$codeGearName}->{'output'};
423 if (! $inParGoto) {
424 $inParGoto = 1;
425 my $initTasks = << "EOFEOF";
426 ${prev}struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, ?);
427 ${prev}int taskCount = 0;
428 EOFEOF
429 print $fd $initTasks;
430 }
431
432 my $initTask = << "EOFEOF";
433 ${prev}struct Context* task = NEW(struct Context);
434 ${prev}initContext(task);
435 ${prev}task->next = C_$codeGearName;
436 ${prev}task->idgCount = $inputCount;
437 ${prev}task->idg = task->dataNum;
438 ${prev}task->maxIdg = task->idg + $inputCount;
439 ${prev}task->odg = task->maxIdg;
440 ${prev}task->maxOdg = task->odg + $outputCount;
441 EOFEOF
442 print $fd $initTask;
443 for my $i (0..$inputCount-1) {
444 print $fd "${prev}task->data[task->idg+$i] = (union Data*)@dataGears[$i];\n";
445 }
446
447 for my $i (0..$outputCount-1) {
448 print $fd "${prev}task->data[task->odg+$i] = (union Data*)@dataGears[$inputCount+$i];\n";
449 }
450
451 print $fd "${prev}tasks[taskCount] = task;\n";
452 print $fd "${prev}taskCount++;\n";
453 next;
379 } elsif (/^(.*)goto (\w+)\((.*)\);/) { 454 } elsif (/^(.*)goto (\w+)\((.*)\);/) {
380 # handling goto statement 455 # handling goto statement
381 # convert it to the meta call form with two arugments, that is context and enum Code 456 # convert it to the meta call form with two arugments, that is context and enum Code
382 my $prev = $1; 457 my $prev = $1;
383 my $next = $2; 458 my $next = $2;
384 my @args = split(/,/,$3); 459 my @args = split(/,/,$3);
385 my $v = 0; 460 my $v = 0;
386 for my $n ( @{$dataGearVar{$codeGearName}} ) { 461 for my $n ( @{$dataGearVar{$codeGearName}} ) {
387 # continuation arguments 462 # continuation arguments
388 $v = 1 if ( $n eq $next); 463 $v = 1 if ( $n eq $next);
389 } 464 }
390 if ($v || defined $code{$interface}->{$next}) { 465 if ($v || defined $code{$interface}->{$next}) {
391 # write continuation's arguments into the interface arguments 466 # write continuation's arguments into the interface arguments
392 # we may need a commit for a shared DataGear 467 # we may need a commit for a shared DataGear
393 for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) { 468 for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) {
394 my $v = shift(@args); 469 my $v = shift(@args);
395 print $fd "\t*O_$arg = $v;\n"; 470 print $fd "\t*O_$arg = $v;\n";
396 } 471 }
397 print $fd "${prev}goto meta(context, $next);\n"; 472 print $fd "${prev}goto meta(context, $next);\n";
398 next; 473 next;
399 } 474 }
400 } else { 475 }
476 else {
401 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new 477 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new
402 } 478 }
403 # gather type name and type 479 # gather type name and type
404 } elsif (/^}/) { 480 } elsif (/^}/) {
481 $inParGoto = 0;
405 $inStub = 0; 482 $inStub = 0;
406 $inTypedef = 0; 483 $inTypedef = 0;
407 } 484 }
408 print $fd $_; 485 print $fd $_;
409 } 486 }