194
|
1 #!/usr/bin/perl
|
|
2
|
250
|
3 use strict;
|
|
4
|
194
|
5 # interface.cbc
|
|
6 # typedef struct Worker {
|
|
7 # int id;
|
|
8 # struct Context* contexts;
|
|
9 # enum Code execute;
|
|
10 # enum Code taskSend;
|
|
11 # enum Code taskRecive;
|
|
12 # enum Code shutdown;
|
|
13 # struct Queue* tasks;
|
|
14 # } Worker;
|
|
15
|
252
|
16 #while (<*.cbc>) {
|
|
17 while (<SingleLinkedStack.cbc>) {
|
249
|
18 my $fn = $_;
|
|
19 &getDataGear($fn);
|
|
20 &generateDataGear($fn);
|
194
|
21 }
|
|
22
|
250
|
23 my %var;
|
|
24 my %type;
|
|
25 my %code;
|
|
26 my %dataGearVar;
|
|
27 my %dataGear;
|
|
28 my %dataGearName;
|
253
|
29 my $implementation;
|
|
30 my $interface;
|
250
|
31
|
194
|
32 sub getDataGear {
|
|
33 my ($filename) = @_;
|
253
|
34 my ($codeGearName, $name, $inTypedef);
|
194
|
35 open my $fd,"<",$filename or die("can't open $filename $!");
|
|
36 while (<$fd>) {
|
|
37 if (! $inTypedef) {
|
|
38 if (/^typedef struct (\w+) {/) {
|
|
39 $inTypedef = 1;
|
|
40 $name = $1;
|
|
41 $dataGear{$name} = $_;
|
253
|
42 $code{$name} = [];
|
249
|
43 } elsif (/^(\w+)\* create(\w+)\(/) {
|
|
44 if (defined $interface) {
|
|
45 die "duplicate interface $interface\n";
|
|
46 }
|
|
47 $interface = $1;
|
250
|
48 $implementation = $2;
|
|
49 if ( -f "$interface.cbc") {
|
|
50 &getDataGear("$interface.cbc");
|
|
51 }
|
226
|
52 }
|
194
|
53 next;
|
|
54 }
|
249
|
55 # gather type name and type
|
194
|
56 $dataGear{$name} .= $_;
|
196
|
57 if (/(\w+);$/ and !/^} (\w+)/) {
|
250
|
58 my $tmp = $1 . "\n";
|
200
|
59 if (/{/) {
|
|
60 $tmp = "{" . $';
|
201
|
61 $tmp =~ s/;$//;
|
200
|
62 }
|
|
63 $var{$name} .= $tmp;
|
196
|
64 $tmp = $`;
|
|
65 $tmp =~ s/^\s*//;
|
|
66 $type{$name} .= $tmp . "\n";
|
250
|
67 } elsif (/\_\_code (\w+)\(/) {
|
|
68 push $code{$name}, $1;
|
|
69 }
|
194
|
70 if (/^}/) {
|
|
71 $inTypedef = 0;
|
|
72 }
|
|
73 }
|
|
74 }
|
|
75
|
250
|
76 sub generateStub {
|
251
|
77 my($fd,$prevCodeGearName,$dataGearName) = @_;
|
250
|
78 print $fd "__code ", $prevCodeGearName ,"_stub (struct Context* context) {\n";
|
251
|
79 print $fd $dataGearName;
|
250
|
80 print $fd "\n} \n\n";
|
251
|
81 return 1;
|
250
|
82 }
|
|
83
|
253
|
84 sub generateStubArgs {
|
|
85 my($codeGearName, $varName, $typeName, $typeField, $interface) = @_;
|
|
86 push @{$dataGearVar{$codeGearName}},$varName;
|
|
87 if ($typeField ne $varName) {
|
|
88 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $typeName, $varName);\n";
|
|
89 # print STDOUT "$codeGearName \t$typeName* $varName = ($typeName*)GearImpl(context, $typeName, $varName);\n";
|
|
90 } else {
|
|
91 for my $ivar ($var{$interface}) {
|
|
92 if ($varName eq $ivar) {
|
|
93 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
|
|
94 # print STDOUT "$codeGearName \t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
|
|
95 return;
|
|
96 }
|
|
97 }
|
|
98 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName)->$typeField;\n";
|
|
99 # print STDOUT "$codeGearName \t$typeName* $varName = Gearef(context, $typeName)->$typeField;\n";
|
|
100 }
|
|
101 }
|
|
102
|
194
|
103 sub generateDataGear {
|
249
|
104 my ($filename) = @_;
|
250
|
105 my $fn1 = $filename;
|
|
106 $fn1 =~ s/\.cbc/.c/;
|
249
|
107 open my $in,"<",$filename or die("can't open $filename $!");
|
250
|
108 my $i = 1;
|
|
109 my $fn = $fn1;
|
|
110 while ( -f $fn) {
|
|
111 $fn = "$fn1.$i";
|
|
112 $i++;
|
|
113 }
|
249
|
114 open my $fd,">",$fn or die("can't write $fn $!");
|
|
115 my $prevCodeGearName;
|
250
|
116 my $inTypedef = 0;
|
|
117 my %stub;
|
251
|
118 my $codeGearName;
|
249
|
119 while (<$in>) {
|
|
120 if (! $inTypedef) {
|
|
121 if (/^typedef struct (\w+) {/) {
|
|
122 $inTypedef = 1;
|
253
|
123 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
|
251
|
124 $codeGearName = $1;
|
253
|
125 my $args = $2;
|
|
126 my $tail = $3;
|
250
|
127 if ($codeGearName =~ /_stub$/) {
|
|
128 $stub{$codeGearName} = 1;
|
|
129 print $fd $_;
|
|
130 next;
|
|
131 }
|
249
|
132 if (defined $prevCodeGearName) {
|
250
|
133 if (defined $stub{$prevCodeGearName."_stub"}) {
|
|
134 undef $prevCodeGearName;
|
|
135 print $fd $_;
|
|
136 next;
|
|
137 }
|
253
|
138 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$prevCodeGearName});
|
249
|
139 }
|
253
|
140 $prevCodeGearName = $codeGearName;
|
|
141 $dataGearVar{$codeGearName} = [];
|
|
142 my $newArgs = "";
|
|
143 while($args) {
|
|
144 if ($args =~ s/(^\s*,\s*)//) {
|
|
145 $newArgs .= $1;
|
|
146 }
|
|
147 # replace __code next
|
|
148 if ($args =~ s/^\_\_code\s(\w+)\([^)]*\)//) {
|
|
149 my $next = $1;
|
|
150 my @args = split(/,/,$2);
|
|
151 $newArgs .= "enum Code $next";
|
|
152 for my $arg (@args) {
|
|
153 $arg =~ s/^\s*//;
|
|
154 $arg =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//;
|
|
155 my $structType = $1;
|
|
156 my $typeName = $2;
|
|
157 my $varName = $4;
|
|
158 my $typeField = lcfirst($typeName);
|
|
159 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface);
|
|
160 }
|
|
161 } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) {
|
|
162 my $structType = $1;
|
|
163 my $typeName = $2;
|
|
164 my $varName = $4;
|
|
165 my $typeField = lcfirst($typeName);
|
|
166 $newArgs .= $&;
|
|
167 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface);
|
|
168 }
|
|
169 }
|
|
170 $dataGearName{$codeGearName} .= "\tgoto $codeGearName(context";
|
|
171 for my $arg ( @{$dataGearVar{$codeGearName}}) {
|
|
172 $dataGearName{$codeGearName} .= ", $arg";
|
|
173 }
|
|
174 $dataGearName{$codeGearName} .= ");";
|
|
175 print $fd "__code $codeGearName($newArgs)$tail\n";
|
|
176 next;
|
250
|
177 } elsif (/^(.*)goto next\(\.\.\.(.*)\);/) {
|
|
178 my $prev = $1;
|
|
179 my $args = $2;
|
|
180 print $fd "${prev}goto meta(context, next);\n";
|
|
181 next;
|
249
|
182 }
|
250
|
183 print $fd $_;
|
249
|
184 next;
|
|
185 }
|
|
186 # gather type name and type
|
|
187 if (/^}/) {
|
|
188 $inTypedef = 0;
|
|
189 }
|
250
|
190 print $fd $_;
|
194
|
191 }
|
250
|
192 if (defined $prevCodeGearName) {
|
|
193 if (!defined $stub{$prevCodeGearName."_stub"}) {
|
251
|
194 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName});
|
250
|
195 }
|
228
|
196 }
|
194
|
197 }
|
|
198
|
|
199 # end
|