comparison src/parallel_execution/generate_stub.pl @ 461:6b71cf5b1c22

Change Interface files from cbc to header
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Wed, 20 Dec 2017 17:54:15 +0900
parents 3025d00eb87d
children 8d7e5d48cad3
comparison
equal deleted inserted replaced
459:57c715bd6283 461:6b71cf5b1c22
24 if (! -d $dir) { 24 if (! -d $dir) {
25 make_path $dir; 25 make_path $dir;
26 } 26 }
27 } 27 }
28 28
29 for my $fn (@ARGV) { 29 for my $fn (@ARGV) {
30 next if ($fn !~ /\.cbc$/); 30 next if ($fn !~ /\.cbc$/);
31 &getDataGear($fn); 31 &getDataGear($fn);
32 &generateDataGear($fn); 32 &generateDataGear($fn);
33 } 33 }
34 34
93 $var{$name} = {}; 93 $var{$name} = {};
94 $code{$name} = {}; 94 $code{$name} = {};
95 $generic{$name} = []; 95 $generic{$name} = [];
96 } elsif (/^(\w+)(\*)+ create(\w+)\(/) { 96 } elsif (/^(\w+)(\*)+ create(\w+)\(/) {
97 if (defined $interface) { 97 if (defined $interface) {
98 die "duplicate interface $interface\n"; 98 die "duplicate interface $interface\n";
99 } 99 }
100 $interface = $1; 100 $interface = $1;
101 $implementation = $3; 101 $implementation = $3;
102 if ( -f "$interface.cbc") { 102 if ( -f "$interface.cbc") {
103 &getDataGear("$interface.cbc"); 103 &getDataGear("$interface.cbc");
104 } 104 }
105 } elsif (/\s*\=\s*(.*)create(\w+)\((.*)\);$/) {
106 #my $intfn = ucfirst($2);
107 my $impln = $2;
108 if ( -f "$impln.cbc") {
109 &getCodeGear("$impln.cbc");
110 }
111 } elsif(/^(.*)par goto (\w+)\((.*)\)/) { 105 } elsif(/^(.*)par goto (\w+)\((.*)\)/) {
112 my $codeGearName = $2; 106 my $codeGearName = $2;
113 if ($filename =~ /^(.*)\/(.*)/) { 107 if ($filename =~ /^(.*)\/(.*)/) {
114 $codeGearName = "$1/$codeGearName"; 108 $codeGearName = "$1/$codeGearName";
115 } 109 }
116 if ( -f "$codeGearName.cbc") { 110 if ( -f "$codeGearName.cbc") {
117 &getCodeGear("$codeGearName.cbc"); 111 &getCodeGear("$codeGearName.cbc");
112 }
113 } elsif(/^#include "(.*)"/) {
114 # interface include
115 my $interfaceHeader = $1;
116 next if ($interfaceHeader =~ /context.h/);
117 if (-f $interfaceHeader) {
118 &getDataGear("$interfaceHeader");
119 &getCodeGear("$interfaceHeader");
118 } 120 }
119 } 121 }
120 next; 122 next;
121 } 123 }
122 # gather type name and type 124 # gather type name and type
131 } 133 }
132 if (/^}/) { 134 if (/^}/) {
133 $inTypedef = 0; 135 $inTypedef = 0;
134 } 136 }
135 } 137 }
138
136 } 139 }
137 140
138 sub getCodeGear { 141 sub getCodeGear {
139 my ($filename) = @_; 142 my ($filename) = @_;
140 open my $fd,"<",$filename or die("can't open $filename $!"); 143 open my $fd,"<",$filename or die("can't open $filename $!");
141 my ($name,$impln); 144 my ($name,$impln);
142 while (<$fd>) { 145 while (<$fd>) {
143 if (/^(\w+)(\*)+ create(\w+)\(/) { 146 if (/^(\w+)(\*)+ create(\w+)\(/) {
144 $name = $1; 147 $name = $1;
145 $impln = $3; 148 $impln = $3;
149 } elsif(/^typedef struct (.*)<.*>\s*{/) {
150 $name = $1;
146 } 151 }
147 if (defined $name) { 152 if (defined $name) {
148 if (/^\_\_code (\w+)$impln\((.*)\)(.*)/) { 153 if (/^\s*\_\_code (\w+)\((.*)\);/) {
149 my $args = $2; 154 my $args = $2;
150 my $method = $1; 155 my $method = $1;
151 $code{$name}->{$method} = []; 156 $code{$name}->{$method} = [];
152 while($args) { 157 while($args) {
153 if ($args =~ s/(^\s*,\s*)//) { 158 # replace comma
154 } 159 $args =~ s/(^\s*,\s*)//;
155 # continuation case 160 # continuation case
156 if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(([^)]*)\)//) { 161 if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(([^)]*)\)//) {
157 my $next = $2; 162 my $next = $2;
158 my @args = split(/,/,$3); 163 my @args = split(/,/,$3);
159 push(@{$code{$name}->{$method}},"\_\_code $next"); 164 push(@{$code{$name}->{$method}},"\_\_code $next");
160 } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) { 165 } elsif ($args =~ s/^(struct|union)?\s*(\w+)(\*)?+\s(\w+)//) {
161 my $structType = $1; 166 my $structType = $1;
162 my $typeName = $2; 167 my $typeName = $2;
163 my $varName = $4; 168 my $varName = $4;
164 my $typeField = lcfirst($typeName); 169 my $typeField = lcfirst($typeName);
165 push(@{$code{$name}->{$method}},"$typeName $varName"); 170 push(@{$code{$name}->{$method}},"$typeName $varName");
166 } elsif ($args =~ s/(.*,)//) { 171 } elsif ($args =~ s/(.*,)//) {
167 } else { 172 } else {
168 last; 173 last;
169 } 174 }
170 } 175 }
205 return 1; 210 return 1;
206 } 211 }
207 212
208 sub generateStubArgs { 213 sub generateStubArgs {
209 my($codeGearName, $varName, $typeName, $typeField, $interface,$output) = @_; 214 my($codeGearName, $varName, $typeName, $typeField, $interface,$output) = @_;
210 my $varname1 = $output?"O_$varName":$varName; 215 my $varname1 = $output?"O_$varName":$varName;
211 for my $n ( @{$dataGearVar{$codeGearName}} ) { 216 for my $n ( @{$dataGearVar{$codeGearName}} ) {
212 # we already have it 217 # we already have it
213 return 0 if ( $n eq $varname1); 218 return 0 if ( $n eq $varname1);
214 } 219 }
215 push @{$dataGearVar{$codeGearName}}, $varname1; 220 push @{$dataGearVar{$codeGearName}}, $varname1;
217 if ($typeName eq $implementation) { 222 if ($typeName eq $implementation) {
218 # get implementation 223 # get implementation
219 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $interface, $varName);\n"; 224 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $interface, $varName);\n";
220 } else { 225 } else {
221 for my $ivar (keys %{$var{$interface}}) { 226 for my $ivar (keys %{$var{$interface}}) {
222 # input data gear field 227 # input data gear field
223 if ($varName eq $ivar) { 228 if ($varName eq $ivar) {
224 if ($typeName eq $var{$interface}->{$ivar}) { 229 if ($typeName eq $var{$interface}->{$ivar}) {
225 if ($output) { 230 if ($output) {
226 $dataGearName{$codeGearName} .= "\t$typeName** O_$varName = &Gearef(context, $interface)->$varName;\n"; 231 $dataGearName{$codeGearName} .= "\t$typeName** O_$varName = &Gearef(context, $interface)->$varName;\n";
227 $outputVar{$codeGearName} .= "\t$typeName* $varName;\n"; 232 $outputVar{$codeGearName} .= "\t$typeName* $varName;\n";
356 my $varName = $4; 361 my $varName = $4;
357 my $typeField = lcfirst($typeName); 362 my $typeField = lcfirst($typeName);
358 $newArgs .= $&; # assuming no duplicate 363 $newArgs .= $&; # assuming no duplicate
359 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface,0); 364 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface,0);
360 } elsif ($args =~ s/(.*,)//) { 365 } elsif ($args =~ s/(.*,)//) {
361 $newArgs .= $1; 366 $newArgs .= $1;
362 } else { 367 } else {
363 $newArgs .= $args; 368 $newArgs .= $args;
364 last; 369 last;
365 } 370 }
366 } 371 }
377 # it should be initialze by gearef 382 # it should be initialze by gearef
378 print $fd $outputVar{$codeGearName}; 383 print $fd $outputVar{$codeGearName};
379 } 384 }
380 next; 385 next;
381 } elsif (/^(.*)goto (\w+)\-\>(\w+)\((.*)\);/) { 386 } elsif (/^(.*)goto (\w+)\-\>(\w+)\((.*)\);/) {
382 # handling goto statement 387 # handling goto statement
383 # convert it to the meta call form with two arugments, that is context and enum Code 388 # convert it to the meta call form with two arugments, that is context and enum Code
384 my $prev = $1; 389 my $prev = $1;
385 my $next = $2; 390 my $next = $2;
386 my $method = $3; 391 my $method = $3;
387 my @args = split(/,/,$4); 392 my @args = split(/,/,$4);
416 if ($arg =~ /(\w+)\(.*\)/) { 421 if ($arg =~ /(\w+)\(.*\)/) {
417 print $fd "\tGearef(context, $ntype)->$pName = $1;\n"; 422 print $fd "\tGearef(context, $ntype)->$pName = $1;\n";
418 } else { 423 } else {
419 print $fd "\tGearef(context, $ntype)->$pName = C_$arg;\n"; 424 print $fd "\tGearef(context, $ntype)->$pName = C_$arg;\n";
420 } 425 }
421 } elsif ($pType =~ s/Data$//){ 426 } elsif ($pType =~ s/Data$//){
422 print $fd "\tGearef(context, $ntype)->$pName = (union Data*) $arg;\n"; 427 print $fd "\tGearef(context, $ntype)->$pName = (union Data*) $arg;\n";
423 } else { 428 } else {
424 print $fd "\tGearef(context, $ntype)->$pName = $arg;\n"; 429 print $fd "\tGearef(context, $ntype)->$pName = $arg;\n";
425 } 430 }
426 $i++; 431 $i++;
430 } elsif(/^(.*)par goto (\w+)\((.*)\);/) { 435 } elsif(/^(.*)par goto (\w+)\((.*)\);/) {
431 # handling par goto statement 436 # handling par goto statement
432 # convert it to the parallel 437 # convert it to the parallel
433 my $prev = $1; 438 my $prev = $1;
434 my $codeGearName = $2; 439 my $codeGearName = $2;
435 my $args = $3; 440 my $args = $3;
436 my $inputCount = $codeGear{$codeGearName}->{'input'}; 441 my $inputCount = $codeGear{$codeGearName}->{'input'};
437 my $outputCount = $codeGear{$codeGearName}->{'output'}; 442 my $outputCount = $codeGear{$codeGearName}->{'output'};
438 my @iterateCounts; 443 my @iterateCounts;
439 # parse examples 'par goto(.., iterate(10), exit);' 444 # parse examples 'par goto(.., iterate(10), exit);'
440 if ($args =~ /iterate\((.*)?\),/) { 445 if ($args =~ /iterate\((.*)?\),/) {
441 @iterateCounts = split(/,/,$1);; 446 @iterateCounts = split(/,/,$1);;
442 $inputCount--; 447 $inputCount--;
443 } 448 }
444 # replace iterate keyword 449 # replace iterate keyword
445 $args =~ s/iterate\((.*)?\),//; 450 $args =~ s/iterate\((.*)?\),//;
446 my @dataGears = split(/,\s*/, $args); 451 my @dataGears = split(/,\s*/, $args);
447 my $nextCodeGear = pop(@dataGears); 452 my $nextCodeGear = pop(@dataGears);
448 if (! $inParGoto) { 453 if (! $inParGoto) {
449 $inParGoto = 1; 454 $inParGoto = 1;
450 print $fd "${prev}struct Element* element;\n"; 455 print $fd "${prev}struct Element* element;\n";
451 } 456 }
452 my $initTask = << "EOFEOF"; 457 my $initTask = << "EOFEOF";
453 ${prev}context->task = NEW(struct Context); 458 ${prev}context->task = NEW(struct Context);
454 ${prev}initContext(context->task); 459 ${prev}initContext(context->task);
455 ${prev}context->task->next = C_$codeGearName; 460 ${prev}context->task->next = C_$codeGearName;
456 ${prev}context->task->idgCount = $inputCount; 461 ${prev}context->task->idgCount = $inputCount;
457 ${prev}context->task->idg = context->task->dataNum; 462 ${prev}context->task->idg = context->task->dataNum;
458 ${prev}context->task->maxIdg = context->task->idg + $inputCount; 463 ${prev}context->task->maxIdg = context->task->idg + $inputCount;
459 ${prev}context->task->odg = context->task->maxIdg; 464 ${prev}context->task->odg = context->task->maxIdg;
460 ${prev}context->task->maxOdg = context->task->odg + $outputCount; 465 ${prev}context->task->maxOdg = context->task->odg + $outputCount;
461 EOFEOF 466 EOFEOF
462 print $fd $initTask; 467 print $fd $initTask;
463 if (@iterateCounts) { 468 if (@iterateCounts) {
464 print $fd "${prev}context->task->iterate = 0;\n"; 469 print $fd "${prev}context->task->iterate = 0;\n";
465 my $len = @iterateCounts; 470 my $len = @iterateCounts;
466 if ($len == 1) { 471 if ($len == 1) {
467 print $fd "${prev}context->task->iterator = createMultiDimIterator(context, $iterateCounts[0], 1, 1);\n"; 472 print $fd "${prev}context->task->iterator = createMultiDimIterator(context, $iterateCounts[0], 1, 1);\n";
468 } elsif ($len == 2) { 473 } elsif ($len == 2) {
469 print $fd "${prev}context->task->iterator = createMultiDimIterator(context, $iterateCounts[0], $iterateCounts[1], 1);\n"; 474 print $fd "${prev}context->task->iterator = createMultiDimIterator(context, $iterateCounts[0], $iterateCounts[1], 1);\n";
470 } elsif ($len == 3) { 475 } elsif ($len == 3) {
471 print $fd "${prev}context->task->iterator = createMultiDimIterator(context, $iterateCounts[0], $iterateCounts[1], $iterateCounts[2]);\n"; 476 print $fd "${prev}context->task->iterator = createMultiDimIterator(context, $iterateCounts[0], $iterateCounts[1], $iterateCounts[2]);\n";
472 } 477 }
473 } 478 }
474 for my $dataGear (@dataGears) { 479 for my $dataGear (@dataGears) {
475 print $fd "${prev}GET_META($dataGear)->wait = createSynchronizedQueue(context);\n"; 480 print $fd "${prev}GET_META($dataGear)->wait = createSynchronizedQueue(context);\n";
476 } 481 }
477 for my $i (0..$inputCount-1) { 482 for my $i (0..$inputCount-1) {
478 print $fd "${prev}context->task->data[context->task->idg+$i] = (union Data*)@dataGears[$i];\n"; 483 print $fd "${prev}context->task->data[context->task->idg+$i] = (union Data*)@dataGears[$i];\n";
479 } 484 }
480 485
481 for my $i (0..$outputCount-1) { 486 for my $i (0..$outputCount-1) {
482 print $fd "${prev}context->task->data[context->task->odg+$i] = (union Data*)@dataGears[$inputCount+$i];\n"; 487 print $fd "${prev}context->task->data[context->task->odg+$i] = (union Data*)@dataGears[$inputCount+$i];\n";
483 } 488 }
484 my $putTask = << "EOFEOF"; 489 my $putTask = << "EOFEOF";
485 ${prev}element = &ALLOCATE(context, Element)->Element; 490 ${prev}element = &ALLOCATE(context, Element)->Element;
486 ${prev}element->next = NULL; 491 ${prev}element->next = NULL;
487 ${prev}element->data = (union Data*)context->task; 492 ${prev}element->data = (union Data*)context->task;
488 ${prev}context->tasks->queue->SingleLinkedQueue.last->next = element; 493 ${prev}context->tasks->queue->SingleLinkedQueue.last->next = element;
489 ${prev}context->tasks->queue->SingleLinkedQueue.last = element; 494 ${prev}context->tasks->queue->SingleLinkedQueue.last = element;
490 EOFEOF 495 EOFEOF
491 print $fd $putTask; 496 print $fd $putTask;
492 next; 497 next;
493 } elsif (/^(.*)goto (\w+)\((.*)\);/) { 498 } elsif (/^(.*)goto (\w+)\((.*)\);/) {
494 # handling goto statement 499 # handling goto statement
495 # convert it to the meta call form with two arugments, that is context and enum Code 500 # convert it to the meta call form with two arugments, that is context and enum Code
496 my $prev = $1; 501 my $prev = $1;
497 my $next = $2; 502 my $next = $2;
498 my @args = split(/, /,$3); 503 my @args = split(/, /,$3);
499 my $v = 0; 504 my $v = 0;
500 for my $n ( @{$dataGearVar{$codeGearName}} ) { 505 for my $n ( @{$dataGearVar{$codeGearName}} ) {
501 # continuation arguments 506 # continuation arguments
502 $v = 1 if ( $n eq $next); 507 $v = 1 if ( $n eq $next);
503 } 508 }
504 if ($v || defined $code{$interface}->{$next}) { 509 if ($v || defined $code{$interface}->{$next}) {
505 # write continuation's arguments into the interface arguments 510 # write continuation's arguments into the interface arguments
506 # we may need a commit for a shared DataGear 511 # we may need a commit for a shared DataGear
507 for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) { 512 for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) {
508 my $v = shift(@args); 513 my $v = shift(@args);
509 print $fd "\t*O_$arg = $v;\n"; 514 print $fd "\t*O_$arg = $v;\n";
510 } 515 }
511 if ($inParGoto) { 516 if ($inParGoto) {
512 print $fd "${prev}taskManager->tasks = context->tasks;\n"; 517 print $fd "${prev}taskManager->tasks = context->tasks;\n";
513 print $fd "${prev}taskManager->next1 = C_$next;\n"; 518 print $fd "${prev}taskManager->next1 = C_$next;\n";
514 print $fd "${prev}goto meta(context, C_$next);\n"; 519 print $fd "${prev}goto meta(context, C_$next);\n";
515 } else { 520 } else {
516 print $fd "${prev}goto meta(context, $next);\n"; 521 print $fd "${prev}goto meta(context, $next);\n";
517 } 522 }
518 next; 523 next;
519 } 524 }
520 if ($inParGoto) { 525 if ($inParGoto) {
521 print $fd "${prev}taskManager->tasks = context->tasks;\n"; 526 print $fd "${prev}taskManager->tasks = context->tasks;\n";
522 print $fd "${prev}taskManager->next1 = C_$next;\n"; 527 print $fd "${prev}taskManager->next1 = C_$next;\n";
523 print $fd "${prev}goto meta(context, C_$next);\n"; 528 print $fd "${prev}goto meta(context, C_$next);\n";
524 next; 529 next;
525 } elsif ($next eq "meta") { 530 } elsif ($next eq "meta") {
526 print $fd $_; 531 print $fd $_;
527 next; 532 next;
528 } else { 533 } else {
529 print $fd "${prev}goto meta(context, C_$next);\n"; 534 print $fd "${prev}goto meta(context, C_$next);\n";
530 next; 535 next;
531 } 536 }
532 } elsif(/^.*(struct|union)?\s(\w+)\*\s(\w+)\s?[=;]/) { 537 } elsif(/^.*(struct|union)?\s(\w+)\*\s(\w+)\s?[=;]/) {
533 my $type = $2; 538 my $type = $2;
534 my $varName = $3; 539 my $varName = $3;
535 $localVarType{$varName} = $type; 540 $localVarType{$varName} = $type;
536 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new 541 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new
537 } 542 }
538 elsif(/^}/) { 543 elsif(/^}/) {
539 $inParGoto = 0; 544 $inParGoto = 0;
540 } else { 545 } else {
541 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new 546 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g; # replacing new
542 } 547 }
543 # gather type name and type 548 # gather type name and type
544 } elsif ($inMain) { 549 } elsif ($inMain) {
545 if (/^(.*)goto start_code\(main_context\);/) { 550 if (/^(.*)goto start_code\(main_context\);/) {
546 print $fd $_; 551 print $fd $_;
547 next; 552 next;
548 } elsif (/^(.*)goto (\w+)\((.*)\);/) { 553 } elsif (/^(.*)goto (\w+)\((.*)\);/) {
549 my $prev = $1; 554 my $prev = $1;
550 my $next = $2; 555 my $next = $2;
551 print $fd "${prev}struct Context* main_context = NEW(struct Context);\n"; 556 print $fd "${prev}struct Context* main_context = NEW(struct Context);\n";
552 print $fd "${prev}initContext(main_context);\n"; 557 print $fd "${prev}initContext(main_context);\n";
553 print $fd "${prev}main_context->next = C_$next;\n"; 558 print $fd "${prev}main_context->next = C_$next;\n";
554 print $fd "${prev}goto start_code(main_context);\n"; 559 print $fd "${prev}goto start_code(main_context);\n";
555 next; 560 next;
556 } 561 }
557 } 562 }
558 if (/^}/) { 563 if (/^}/) {
559 $inStub = 0; 564 $inStub = 0;
560 $inTypedef = 0; 565 $inTypedef = 0;
561 $inMain = 0; 566 $inMain = 0;
562 } 567 }
563 print $fd $_; 568 print $fd $_;
564 } 569 }
565 if (defined $prevCodeGearName) { 570 if (defined $prevCodeGearName) {
566 if (!defined $stub{$prevCodeGearName."_stub"}) { 571 if (!defined $stub{$prevCodeGearName."_stub"}) {
567 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName}); 572 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName});
568 } 573 }
569 } 574 }
570 } 575 }
571 576
572 # end 577 # end