44
|
1 #!/usr/bin/perl
|
|
2
|
|
3 use strict;
|
|
4 use Getopt::Std;
|
|
5 use File::Path qw(make_path);
|
|
6
|
|
7 # interface.h
|
|
8 # typedef struct Worker {
|
|
9 # int id;
|
|
10 # struct Context* contexts;
|
|
11 # enum Code execute;
|
|
12 # enum Code taskSend;
|
|
13 # enum Code taskRecive;
|
|
14 # enum Code shutdown;
|
|
15 # struct Queue* tasks;
|
|
16 # } Worker;
|
|
17
|
|
18 our($opt_o,$opt_d,$opt_h);
|
|
19 getopts('o:d:h');
|
237
|
20 use Data::Dumper;
|
44
|
21
|
|
22 my $dir = ".";
|
|
23 if ($opt_d) {
|
|
24 $dir = $opt_d;
|
|
25 if (! -d $dir) {
|
|
26 make_path $dir;
|
|
27 }
|
|
28 }
|
|
29
|
|
30 for my $fn (@ARGV) {
|
|
31 next if ($fn !~ /\.cbc$/);
|
|
32 &getDataGear($fn);
|
|
33 &generateDataGear($fn);
|
|
34 }
|
|
35
|
|
36 my %var;
|
|
37 my %code;
|
|
38 my %dataGearVar;
|
|
39 my %outputVar; # output var initializer
|
|
40 my %outputArgs; # continuation's output variables
|
|
41 my %dataGear;
|
|
42 my %dataGearName;
|
|
43 my %generic;
|
|
44 my %dataGearVarType;
|
|
45 my %codeGear;
|
|
46 my $implementation;
|
|
47 my $interface;
|
|
48
|
|
49 # interface definision
|
|
50 #
|
|
51 # typedef struct Stack<Type, Impl>{
|
|
52 # Type* stack;
|
|
53 # Type* data;
|
|
54 # Type* data1;
|
|
55 # __code whenEmpty(...);
|
|
56 # __code clear(Impl* stack,__code next(...));
|
|
57 # __code push(Impl* stack,Type* data, __code next(...));
|
|
58 # __code pop(Impl* stack, __code next(Type*, ...));
|
|
59 # __code pop2(Impl* stack, Type** data, Type** data1, __code next(Type**, Type**, ...));
|
|
60 # __code isEmpty(Impl* stack, __code next(...), __code whenEmpty(...));
|
|
61 # __code get(Impl* stack, Type** data, __code next(...));
|
|
62 # __code get2(Impl* stack,..., __code next(...));
|
|
63 # __code next(...);
|
|
64 # } Stack;
|
|
65 #
|
|
66 # calling example
|
|
67 #
|
|
68 # goto nodeStack->push((union Data*)node, stackTest3);
|
|
69 #
|
|
70 # generated meta level code
|
|
71 #
|
52
|
72 # Gearef(cbc_context, Stack)->stack = (union Data*)nodeStack;
|
|
73 # Gearef(cbc_context, Stack)->data = (union Data*)node;
|
|
74 # Gearef(cbc_context, Stack)->next = C_stackTest3;
|
|
75 # goto meta(cbc_context, nodeStack->push);
|
44
|
76
|
|
77 sub getDataGear {
|
|
78 my ($filename) = @_;
|
|
79 my ($codeGearName, $name, $inTypedef);
|
|
80 open my $fd,"<",$filename or die("can't open $filename $!");
|
|
81 while (<$fd>) {
|
|
82 if (! $inTypedef) {
|
|
83 if (/^typedef struct (\w+)\s*<(.*)>/) {
|
|
84 $inTypedef = 1;
|
|
85 $name = $1;
|
|
86 $dataGear{$name} = $_;
|
|
87 $var{$name} = {};
|
|
88 $code{$name} = {};
|
|
89 $generic{$name} = \split(/,/,$2);
|
|
90 } elsif (/^typedef struct (\w+)/) {
|
|
91 $inTypedef = 1;
|
|
92 $name = $1;
|
|
93 $dataGear{$name} = $_;
|
|
94 $var{$name} = {};
|
|
95 $code{$name} = {};
|
|
96 $generic{$name} = [];
|
|
97 } elsif (/^(\w+)(\*)+ create(\w+)\(/) {
|
|
98 if (defined $interface) {
|
|
99 die "duplicate interface $interface\n";
|
|
100 }
|
|
101 $interface = $1;
|
|
102 $implementation = $3;
|
|
103 if ( -f "$interface.cbc") {
|
|
104 &getDataGear("$interface.cbc");
|
|
105 }
|
|
106 } elsif(/^(.*)par goto (\w+)\((.*)\)/) {
|
|
107 my $codeGearName = $2;
|
|
108 if ($filename =~ /^(.*)\/(.*)/) {
|
|
109 $codeGearName = "$1/$codeGearName";
|
|
110 }
|
|
111 if ( -f "$codeGearName.cbc") {
|
|
112 &getCodeGear("$codeGearName.cbc");
|
|
113 }
|
|
114 } elsif(/^#interface "(.*)"/) {
|
|
115 # use interface
|
|
116 my $interfaceHeader = $1;
|
|
117 next if ($interfaceHeader =~ /context.h/);
|
|
118 if (-f $interfaceHeader) {
|
|
119 &getDataGear("$interfaceHeader");
|
|
120 &getCodeGear("$interfaceHeader");
|
|
121 }
|
|
122 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
|
|
123 my $codeGearName = $1;
|
|
124 if ($filename =~ /^(.*)\/(.*)/) {
|
|
125 $codeGearName = "$1/$codeGearName";
|
|
126 }
|
|
127 if ( -f "$codeGearName.cbc") {
|
|
128 &getCodeGear("$codeGearName.cbc");
|
|
129 }
|
|
130 }
|
|
131 next;
|
|
132 }
|
|
133 # gather type name and type
|
|
134 $dataGear{$name} .= $_;
|
|
135 if (/^\s*(.*)\s+(\w+);$/ ) {
|
|
136 my $ttype = $1;
|
|
137 my $tname = $2;
|
237
|
138 if ($ttype =~ /^(union|struct|const)?\s*(\w+)/) {
|
|
139 if ($1 ne 'const') {
|
|
140 $ttype = $2;
|
|
141 } else {
|
|
142 $ttype = "const $2";
|
|
143 }
|
44
|
144 }
|
|
145 $var{$name}->{$tname} = $ttype;
|
|
146 }
|
|
147 if (/^}/) {
|
|
148 $inTypedef = 0;
|
|
149 }
|
|
150 }
|
|
151
|
|
152 }
|
|
153
|
|
154 sub getCodeGear {
|
|
155 my ($filename) = @_;
|
|
156 open my $fd,"<",$filename or die("can't open $filename $!");
|
|
157 my ($name,$impln);
|
|
158 while (<$fd>) {
|
|
159 if (/^(\w+)(\*)+ create(\w+)\(/) {
|
|
160 $name = $1;
|
|
161 $impln = $3;
|
|
162 } elsif(/^typedef struct (.*)<.*>\s*{/) {
|
|
163 $name = $1;
|
|
164 }
|
|
165 if (defined $name) {
|
|
166 if (/^\s*\_\_code (\w+)\((.*)\);/) {
|
|
167 my $args = $2;
|
|
168 my $method = $1;
|
|
169 $code{$name}->{$method} = [];
|
|
170 while($args) {
|
|
171 # replace comma
|
|
172 $args =~ s/(^\s*,\s*)//;
|
|
173 # continuation case
|
|
174 if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(([^)]*)\)//) {
|
|
175 my $next = $2;
|
|
176 my @args = split(/,/,$3);
|
|
177 push(@{$code{$name}->{$method}},"\_\_code $next");
|
237
|
178 } elsif ($args =~ s/^(struct|union|const)?\s*(\w+)(\**)\s+(\w+)//) {
|
44
|
179 my $structType = $1;
|
|
180 my $typeName = $2;
|
|
181 my $ptrType = $3;
|
|
182 my $varName = $4;
|
|
183 my $typeField = lcfirst($typeName);
|
237
|
184 if ($structType =~ /const/) {
|
|
185 $typeName = "$structType $typeName";
|
|
186 }
|
44
|
187 push(@{$code{$name}->{$method}},"$typeName$ptrType $varName");
|
|
188 } elsif ($args =~ s/(.*,)//) {
|
|
189 } else {
|
|
190 last;
|
|
191 }
|
|
192 }
|
|
193 }
|
|
194 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
|
|
195 my $codeGearName = $1;
|
|
196 my $args = $2;
|
|
197 my $inputCount = 0;
|
|
198 my $outputCount = 0;
|
|
199 my $inputIncFlag = 1;
|
|
200 while($args) {
|
|
201 if ($args =~ s/(^\s*,\s*)//) {
|
|
202 }
|
|
203 if ($args =~ s/^(\s)*\_\_code\s+(\w+)\((.*?)\)//) {
|
|
204 $codeGear{$codeGearName}->{"code"}->{$2} = "\_\_code";
|
|
205 $inputIncFlag = 0;
|
|
206 my @outputs = split(/,/,$3);
|
|
207 for my $output (@outputs) {
|
237
|
208 if ($output =~ /\s*(struct|union|const)?\s*(\w+)(\*)?+\s(\w+)/) {
|
|
209 my $structType = $1;
|
44
|
210 my $type = $2;
|
|
211 my $varName = $4;
|
237
|
212 if ($structType =~ /const/) {
|
|
213 $type = "$structType $type";
|
|
214 }
|
44
|
215 $codeGear{$codeGearName}->{"var"}->{$varName} = "$type $outputCount";
|
|
216 $outputCount++;
|
|
217 }
|
|
218 }
|
237
|
219 } elsif ($args =~ s/^(struct|union|const)?\s*(\w+)(\*)?+\s(\w+)// && $inputIncFlag) {
|
|
220 my $structType = $1;
|
44
|
221 my $type = $2;
|
|
222 my $varName = $4;
|
237
|
223 if ($structType =~ /const/) {
|
|
224 $type = "$structType $type";
|
|
225 }
|
44
|
226 $codeGear{$codeGearName}->{"var"}->{$varName} = "$type $inputCount";
|
|
227 $inputCount++;
|
|
228 } elsif ($args =~ s/(.*,)//) {
|
|
229 } else {
|
|
230 last;
|
|
231 }
|
|
232 }
|
|
233 $codeGear{$codeGearName}->{"input"} = $inputCount;
|
|
234 $codeGear{$codeGearName}->{"output"} = $outputCount;
|
|
235 }
|
|
236 }
|
|
237 }
|
|
238
|
|
239 sub generateStub {
|
|
240 my($fd,$prevCodeGearName,$dataGearName) = @_;
|
52
|
241 print $fd "__code ", $prevCodeGearName ,"_stub(struct Context* cbc_context) {\n";
|
44
|
242 print $fd $dataGearName;
|
|
243 print $fd "\n} \n\n";
|
|
244 return 1;
|
|
245 }
|
|
246
|
|
247 sub generateStubArgs {
|
|
248 my($codeGearName, $varName, $typeName, $ptrType, $typeField, $interface,$output) = @_;
|
|
249 my $varname1 = $output?"O_$varName":$varName;
|
|
250 for my $n ( @{$dataGearVar{$codeGearName}} ) {
|
|
251 # we already have it
|
|
252 return 0 if ( $n eq $varname1);
|
|
253 }
|
|
254 push @{$dataGearVar{$codeGearName}}, $varname1;
|
|
255 push @{$dataGearVarType{$codeGearName}}, $typeName;
|
|
256 if ($typeName eq $implementation) {
|
|
257 # get implementation
|
52
|
258 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(cbc_context, $interface, $varName);\n";
|
44
|
259 } else {
|
|
260 # interface var
|
237
|
261 print Dumper \%var;
|
44
|
262 for my $ivar (keys %{$var{$interface}}) {
|
|
263 # input data gear field
|
|
264 if ($varName eq $ivar) {
|
|
265 if ($typeName eq $var{$interface}->{$ivar}) {
|
|
266 if ($output) {
|
52
|
267 $dataGearName{$codeGearName} .= "\t$typeName$ptrType* O_$varName = &Gearef(cbc_context, $interface)->$varName;\n";
|
82
|
268 $outputVar{$codeGearName} .= "\t$typeName$ptrType $varName __attribute__((unused)) = *O_$varName;\n";
|
44
|
269 return 1;
|
|
270 }
|
52
|
271 $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = Gearef(cbc_context, $interface)->$varName;\n";
|
44
|
272 return 1;
|
|
273 }
|
|
274 }
|
|
275 }
|
237
|
276
|
44
|
277 # interface continuation
|
|
278 for my $cName (keys %{$code{$interface}}) {
|
|
279 if ($varName eq $cName) {
|
|
280 # continuation field
|
52
|
281 $dataGearName{$codeGearName} .= "\tenum Code $varName = Gearef(cbc_context, $interface)->$varName;\n";
|
44
|
282 return 1;
|
|
283 }
|
|
284 }
|
|
285 # par goto var
|
|
286 for my $var (keys %{$codeGear{$codeGearName}->{"var"}}) {
|
|
287 # input data gear field
|
|
288 if ($varName eq $var) {
|
|
289 my ($type, $count) = split(/\s/, $codeGear{$codeGearName}->{"var"}->{$var});
|
|
290 if ($typeName eq $type) {
|
|
291 if ($output) {
|
52
|
292 $dataGearName{$codeGearName} .= "\t$typeName$ptrType* O_$varName = ($typeName $ptrType*)&cbc_context->data[cbc_context->odg + $count];\n";
|
44
|
293 $outputVar{$codeGearName} .= "\t$typeName$ptrType $varName = *O_$varName;\n";
|
|
294 return 1;
|
|
295 }
|
52
|
296 $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = &cbc_context->data[cbc_context->idg + $count]->$typeName;\n";
|
44
|
297 return 1;
|
|
298 }
|
|
299 }
|
|
300 }
|
|
301
|
|
302 # par goto continuation
|
|
303 for my $cName (keys %{$codeGear{$codeGearName}->{"code"}}) {
|
|
304 if ($varName eq $cName) {
|
|
305 # continuation field
|
52
|
306 $dataGearName{$codeGearName} .= "\tenum Code $varName = cbc_context->next;\n";
|
44
|
307 return 1;
|
|
308 }
|
|
309 }
|
|
310
|
|
311 # par goto continuation
|
|
312 # global or local variable case
|
|
313 if ($typeName eq "Code") {
|
52
|
314 $dataGearName{$codeGearName} .= "\tenum $typeName$ptrType $varName = Gearef(cbc_context, $interface)->$varName;\n";
|
44
|
315 return 1;
|
|
316 }
|
52
|
317 $dataGearName{$codeGearName} .= "\t$typeName$ptrType $varName = Gearef(cbc_context, $typeName);\n";
|
44
|
318 return 1;
|
|
319 }
|
|
320 }
|
|
321
|
|
322 sub generateDataGear {
|
|
323 my ($filename) = @_;
|
|
324 open my $in,"<",$filename or die("can't open $filename $!");
|
|
325
|
|
326 my $fn;
|
|
327 if ($opt_o) {
|
|
328 $fn = $opt_o;
|
|
329 } else {
|
|
330 my $fn1 = $filename;
|
|
331 $fn1 =~ s/\.cbc/.c/;
|
|
332 my $i = 1;
|
|
333 $fn = "$dir/$fn1";
|
|
334 while ( -f $fn) {
|
|
335 $fn = "$dir/$fn1.$i";
|
|
336 $i++;
|
|
337 }
|
|
338 }
|
|
339 if ( $fn =~ m=(.*)/[^/]+$= ) {
|
|
340 if (! -d $1) {
|
|
341 make_path $1;
|
|
342 }
|
|
343 }
|
|
344 open my $fd,">",$fn or die("can't write $fn $!");
|
|
345
|
|
346 my $prevCodeGearName;
|
|
347 my $inTypedef = 0;
|
|
348 my $inStub = 0;
|
45
|
349 my $hasParGoto = 0;
|
44
|
350 my $inMain = 0 ;
|
52
|
351 my $inCode = 0 ;
|
44
|
352 my %stub;
|
|
353 my $codeGearName;
|
|
354 my %localVarType;
|
52
|
355 my %localCode;
|
44
|
356
|
|
357 while (<$in>) {
|
|
358 if (! $inTypedef && ! $inStub && ! $inMain) {
|
|
359 if (/^typedef struct (\w+) \{/) {
|
|
360 $inTypedef = 1;
|
|
361 } elsif (/^int main\((.*)\) \{/) {
|
|
362 $inMain = 1;
|
|
363 } elsif(/^#interface "(.*)"/) {
|
|
364 my $interfaceHeader = $1;
|
|
365 # #interface not write
|
|
366 next unless ($interfaceHeader =~ /context.h/);
|
52
|
367 } elsif (/^\s\s*_\_code (\w+)\((.*)\)(.*)/) {
|
|
368 $localCode{$1} = 1;
|
|
369 } elsif (/^\s\s*_\_code *\(\s*\*\s*(\w+)\)\((.*)\)(.*)/) {
|
|
370 $localCode{$1} = 1;
|
44
|
371 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
|
52
|
372 $inCode = 1;
|
|
373 %localCode = {};
|
44
|
374 %localVarType = {};
|
|
375 $codeGearName = $1;
|
|
376 my $args = $2;
|
|
377 my $tail = $3;
|
|
378 if ($codeGearName =~ /_stub$/) {
|
|
379 # don't touch already existing stub
|
|
380 $inStub = 1;
|
|
381 $stub{$codeGearName} = 1;
|
|
382 print $fd $_;
|
|
383 next;
|
|
384 }
|
|
385 if (defined $prevCodeGearName) {
|
|
386 # stub is generated just before next CodeGear
|
|
387 if (defined $stub{$prevCodeGearName."_stub"}) {
|
|
388 undef $prevCodeGearName;
|
|
389 } else {
|
|
390 &generateStub($fd,$prevCodeGearName,$dataGearName{$prevCodeGearName});
|
|
391 $stub{$prevCodeGearName."_stub"} = 1;
|
|
392 }
|
|
393 }
|
|
394 # analyzing CodeGear argument
|
|
395 # these arguments are extract from current context's arugment DataGear Interface
|
|
396 # and passed to the CodeGear
|
|
397 # struct Implementaion needs special handling
|
|
398 # __code next(...) ---> enum Code next
|
|
399 $prevCodeGearName = $codeGearName;
|
|
400 $dataGearVar{$codeGearName} = [];
|
|
401 $outputVar{$codeGearName} = "";
|
|
402 $outputArgs{$codeGearName} = {};
|
52
|
403 my $newArgs = "struct Context *cbc_context,";
|
|
404 if ($args=~/^struct Context\s*\*\s*cbc_context/) {
|
44
|
405 $newArgs = "";
|
|
406 }
|
|
407 if (!$args){
|
52
|
408 $newArgs = "struct Context *cbc_context";
|
44
|
409 }
|
|
410 while($args) {
|
|
411 if ($args =~ s/(^\s*,\s*)//) {
|
|
412 $newArgs .= $1;
|
|
413 }
|
|
414 # continuation case
|
|
415 if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(([^)]*)\)//) {
|
|
416 my $next = $2;
|
|
417 my @args = split(/,/,$3);
|
|
418 if (&generateStubArgs($codeGearName, $next, "Code", "", $next, $interface,0) ) {
|
|
419 $newArgs .= "enum Code $next";
|
|
420 }
|
|
421 # analyze continuation arguments
|
|
422 # output arguments are defined in the Interface take the pointer of these
|
|
423 # output arguments are put into the Interface DataGear just before the goto
|
|
424 for my $arg (@args) {
|
|
425 $arg =~ s/^\s*//;
|
|
426 last if ($arg =~ /\.\.\./);
|
237
|
427 $arg =~ s/^(struct|union|const)?\s*(\w+)(\**)\s(\w+)//;
|
44
|
428 my $structType = $1;
|
|
429 my $typeName = $2;
|
|
430 my $ptrType = $3;
|
|
431 my $varName = $4;
|
237
|
432 if ($structType =~ /const/) {
|
|
433 $typeName = "$structType $typeName";
|
|
434 }
|
44
|
435 my $typeField = lcfirst($typeName);
|
|
436 push(@{$outputArgs{$codeGearName}->{$next}}, $varName);
|
|
437 if (&generateStubArgs($codeGearName, $varName, $typeName, $ptrType, $typeField, $interface,1)) {
|
|
438 $newArgs .= ",$structType $typeName **O_$varName";
|
|
439 }
|
|
440 }
|
237
|
441 } elsif ($args =~ s/^(struct|union|const)?\s*(\w+)(\**)\s(\w+)//) {
|
44
|
442 my $structType = $1;
|
|
443 my $typeName = $2;
|
|
444 my $ptrType = $3;
|
|
445 my $varName = $4;
|
237
|
446 $newArgs .= $&; # assuming no duplicate
|
|
447 if ($structType =~ /const/) {
|
|
448 $typeName = "$structType $typeName";
|
|
449 }
|
44
|
450 my $typeField = lcfirst($typeName);
|
|
451 &generateStubArgs($codeGearName, $varName, $typeName, $ptrType, $typeField, $interface,0);
|
|
452 } elsif ($args =~ s/(.*,)//) {
|
|
453 $newArgs .= $1;
|
|
454 } else {
|
|
455 $newArgs .= $args;
|
|
456 last;
|
|
457 }
|
|
458 }
|
|
459 # generate goto statement from stub to the CodeGear in the buffer
|
52
|
460 $dataGearName{$codeGearName} .= "\tgoto $codeGearName(cbc_context";
|
44
|
461 for my $arg ( @{$dataGearVar{$codeGearName}}) {
|
|
462 $dataGearName{$codeGearName} .= ", $arg";
|
|
463 }
|
|
464 $dataGearName{$codeGearName} .= ");";
|
|
465 # generate CodeGear header with new arguments
|
|
466 print $fd "__code $codeGearName($newArgs)$tail\n";
|
|
467 if ($outputVar{$codeGearName} ne "") {
|
|
468 # output data var can be use before write
|
|
469 # it should be initialze by gearef
|
|
470 print $fd $outputVar{$codeGearName};
|
|
471 }
|
|
472 next;
|
52
|
473 } elsif (! $inCode) {
|
82
|
474 s/new\s+(\w+)\(\)/\&ALLOCATE(cbc_context, \1)->\1/g; # replacing new
|
52
|
475 print $fd $_;
|
|
476 next;
|
44
|
477 } elsif (/^(.*)goto (\w+)\-\>(\w+)\((.*)\);/) {
|
|
478 # handling goto statement
|
|
479 # convert it to the meta call form with two arugments, that is context and enum Code
|
|
480 my $prev = $1;
|
|
481 my $next = $2;
|
|
482 my $method = $3;
|
|
483 my $tmpArgs = $4;
|
82
|
484 #$tmpArgs =~ s/\(.*\)/\(\)/;
|
44
|
485 my @args = split(/,/,$tmpArgs);
|
52
|
486 if (! defined $dataGearVarType{$codeGearName}) {
|
|
487 print $fd $_ ;
|
|
488 next ;
|
|
489 }
|
44
|
490 my @types = @{$dataGearVarType{$codeGearName}};
|
|
491 my $ntype;
|
|
492 my $ftype;
|
|
493 for my $v (@{$dataGearVar{$codeGearName}}) {
|
|
494 my $t = shift @types;
|
|
495 if ($v eq $next || $v eq "O_$next") {
|
|
496 $ntype = $t;
|
|
497 $ftype = lcfirst($ntype);
|
|
498 }
|
|
499 }
|
|
500 if (!defined $ntype) {
|
|
501 $ntype = $localVarType{$next};
|
|
502 $ftype = lcfirst($ntype);
|
|
503 }
|
52
|
504 print $fd "\tGearef(cbc_context, $ntype)->$ftype = (union Data*) $next;\n";
|
44
|
505 # Put interface argument
|
|
506 my $prot = $code{$ntype}->{$method};
|
|
507 my $i = 1;
|
|
508 for my $arg (@args) {
|
|
509 my $pType;
|
|
510 my $pName;
|
|
511 my $p = @$prot[$i];
|
|
512 next if ($p eq $arg);
|
|
513 $p =~ s/^(.*)\s(\w+)//;
|
|
514 $pType = $1;
|
|
515 $pName = $2;
|
|
516 $arg =~ s/^(\s)*(\w+)/$2/;
|
|
517 if ($pType =~ s/\_\_code$//) {
|
|
518 if ($arg =~ /(\w+)\(.*\)/) {
|
52
|
519 print $fd "\tGearef(cbc_context, $ntype)->$pName = $1;\n";
|
44
|
520 } else {
|
52
|
521 print $fd "\tGearef(cbc_context, $ntype)->$pName = C_$arg;\n";
|
44
|
522 }
|
|
523 } elsif ($pType =~ /Data\**$/){
|
52
|
524 print $fd "\tGearef(cbc_context, $ntype)->$pName = (union $pType) $arg;\n";
|
44
|
525 } else {
|
52
|
526 print $fd "\tGearef(cbc_context, $ntype)->$pName = $arg;\n";
|
44
|
527 }
|
|
528 $i++;
|
|
529 }
|
52
|
530 # print $fd "${prev}cbc_context->before = C_$codeGearName;\n";
|
|
531 print $fd "${prev}goto meta(cbc_context, $next->$method);\n";
|
44
|
532 next;
|
|
533 } elsif(/^(.*)par goto (\w+)\((.*)\);/) {
|
|
534 # handling par goto statement
|
|
535 # convert it to the parallel
|
|
536 my $prev = $1;
|
|
537 my $codeGearName = $2;
|
|
538 my $args = $3;
|
|
539 my $inputCount = $codeGear{$codeGearName}->{'input'};
|
|
540 my $outputCount = $codeGear{$codeGearName}->{'output'};
|
|
541 my @iterateCounts;
|
|
542 # parse examples 'par goto(.., iterate(10), exit);'
|
|
543 if ($args =~ /iterate\((.*)?\),/) {
|
|
544 @iterateCounts = split(/,/,$1);;
|
|
545 $inputCount--;
|
|
546 }
|
|
547 # replace iterate keyword
|
|
548 $args =~ s/iterate\((.*)?\),//;
|
|
549 my @dataGears = split(/,\s*/, $args);
|
|
550 my $nextCodeGear = pop(@dataGears);
|
45
|
551 if (! $hasParGoto) {
|
|
552 $hasParGoto = 1;
|
44
|
553 print $fd "${prev}struct Element* element;\n";
|
|
554 }
|
|
555 my $initTask = << "EOFEOF";
|
52
|
556 ${prev}cbc_context->task = NEW(struct Context);
|
|
557 ${prev}initContext(cbc_context->task);
|
|
558 ${prev}cbc_context->task->next = C_$codeGearName;
|
|
559 ${prev}cbc_context->task->idgCount = $inputCount;
|
|
560 ${prev}cbc_context->task->idg = cbc_context->task->dataNum;
|
|
561 ${prev}cbc_context->task->maxIdg = cbc_context->task->idg + $inputCount;
|
|
562 ${prev}cbc_context->task->odg = cbc_context->task->maxIdg;
|
|
563 ${prev}cbc_context->task->maxOdg = cbc_context->task->odg + $outputCount;
|
44
|
564 EOFEOF
|
|
565 print $fd $initTask;
|
|
566 if (@iterateCounts) {
|
52
|
567 print $fd "${prev}cbc_context->task->iterate = 0;\n";
|
44
|
568 my $len = @iterateCounts;
|
|
569 if ($len == 1) {
|
52
|
570 print $fd "${prev}cbc_context->task->iterator = createMultiDimIterator(cbc_context, $iterateCounts[0], 1, 1);\n";
|
44
|
571 } elsif ($len == 2) {
|
52
|
572 print $fd "${prev}cbc_context->task->iterator = createMultiDimIterator(cbc_context, $iterateCounts[0], $iterateCounts[1], 1);\n";
|
44
|
573 } elsif ($len == 3) {
|
52
|
574 print $fd "${prev}cbc_context->task->iterator = createMultiDimIterator(cbc_context, $iterateCounts[0], $iterateCounts[1], $iterateCounts[2]);\n";
|
44
|
575 }
|
|
576 }
|
|
577 for my $dataGear (@dataGears) {
|
52
|
578 print $fd "${prev}GET_META($dataGear)->wait = createSynchronizedQueue(cbc_context);\n";
|
44
|
579 }
|
|
580 for my $i (0..$inputCount-1) {
|
52
|
581 print $fd "${prev}cbc_context->task->data[cbc_context->task->idg+$i] = (union Data*)@dataGears[$i];\n";
|
44
|
582 }
|
|
583 for my $i (0..$outputCount-1) {
|
52
|
584 print $fd "${prev}cbc_context->task->data[cbc_context->task->odg+$i] = (union Data*)@dataGears[$inputCount+$i];\n";
|
44
|
585 }
|
|
586 my $putTask = << "EOFEOF";
|
52
|
587 ${prev}element = &ALLOCATE(cbc_context, Element)->Element;
|
|
588 ${prev}element->data = (union Data*)cbc_context->task;
|
|
589 ${prev}element->next = cbc_context->taskList;
|
|
590 ${prev}cbc_context->taskList = element;
|
44
|
591 EOFEOF
|
|
592 print $fd $putTask;
|
|
593 next;
|
|
594 } elsif (/^(.*)goto (\w+)\((.*)\);/) {
|
|
595 # handling goto statement
|
|
596 # convert it to the meta call form with two arugments, that is context and enum Code
|
|
597 my $prev = $1;
|
|
598 my $next = $2;
|
|
599 my @args = split(/,/, $3);
|
|
600 my $v = 0;
|
52
|
601 if (defined $localCode{$next}) {
|
|
602 print $fd $_; next;
|
|
603 }
|
44
|
604 for my $n ( @{$dataGearVar{$codeGearName}} ) {
|
|
605 # continuation arguments
|
|
606 $v = 1 if ( $n eq $next);
|
|
607 }
|
|
608 if ($v || defined $code{$interface}->{$next}) {
|
|
609 # write continuation's arguments into the interface arguments
|
|
610 # we may need a commit for a shared DataGear
|
|
611 for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) {
|
|
612 my $v = shift(@args);
|
|
613 print $fd "\t*O_$arg = $v;\n";
|
|
614 }
|
45
|
615 if ($hasParGoto) {
|
52
|
616 print $fd "${prev}Gearef(cbc_context, TaskManager)->taskList = cbc_context->taskList;\n";
|
|
617 print $fd "${prev}Gearef(cbc_context, TaskManager)->next1 = C_$next;\n";
|
|
618 print $fd "${prev}goto meta(cbc_context, C_$next);\n";
|
44
|
619 } else {
|
52
|
620 # print $fd "${prev}cbc_context->before = C_$codeGearName;\n";
|
|
621 print $fd "${prev}goto meta(cbc_context, $next);\n";
|
44
|
622 }
|
|
623 next;
|
|
624 }
|
45
|
625 if ($hasParGoto) {
|
52
|
626 print $fd "${prev}Gearef(cbc_context, TaskManager)->taskList = cbc_context->taskList;\n";
|
|
627 print $fd "${prev}Gearef(cbc_context, TaskManager)->next1 = C_$next;\n";
|
|
628 print $fd "${prev}goto parGotoMeta(cbc_context, C_$next);\n";
|
44
|
629 next;
|
|
630 } elsif ($next eq "meta") {
|
|
631 print $fd $_;
|
|
632 next;
|
|
633 } else {
|
52
|
634 # print $fd "${prev}cbc_context->before = C_$codeGearName;\n";
|
|
635 print $fd "${prev}goto meta(cbc_context, C_$next);\n";
|
44
|
636 next;
|
|
637 }
|
|
638 } elsif(/^.*(struct|union)?\s(\w+)\*\s(\w+)\s?[=;]/) {
|
|
639 my $type = $2;
|
|
640 my $varName = $3;
|
|
641 $localVarType{$varName} = $type;
|
52
|
642 s/new\s+(\w+)\(\)/\&ALLOCATE(cbc_context, \1)->\1/g; # replacing new
|
44
|
643 } elsif(/^}/) {
|
45
|
644 $hasParGoto = 0;
|
44
|
645 } else {
|
52
|
646 s/new\s+(\w+)\(\)/\&ALLOCATE(cbc_context, \1)->\1/g; # replacing new
|
44
|
647 }
|
|
648 # gather type name and type
|
|
649 } elsif ($inMain) {
|
|
650 if (/^(.*)goto start_code\(main_context\);/) {
|
|
651 print $fd $_;
|
|
652 next;
|
|
653 } elsif (/^(.*)goto (\w+)\((.*)\);/) {
|
|
654 my $prev = $1;
|
|
655 my $next = $2;
|
|
656 print $fd "${prev}struct Context* main_context = NEW(struct Context);\n";
|
|
657 print $fd "${prev}initContext(main_context);\n";
|
52
|
658 print $fd "${prev}main_cbc_context->next = C_$next;\n";
|
44
|
659 print $fd "${prev}goto start_code(main_context);\n";
|
|
660 next;
|
|
661 }
|
|
662 }
|
|
663 if (/^}/) {
|
|
664 $inStub = 0;
|
|
665 $inTypedef = 0;
|
|
666 $inMain = 0;
|
52
|
667 $inCode = 0;
|
44
|
668 }
|
|
669 print $fd $_;
|
|
670 }
|
|
671 if (defined $prevCodeGearName) {
|
|
672 if (!defined $stub{$prevCodeGearName."_stub"}) {
|
|
673 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName});
|
|
674 }
|
|
675 }
|
|
676 }
|
|
677
|
|
678 # end
|