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 %type;
|
|
36 my %code;
|
|
37 my %dataGearVar;
|
|
38 my %dataGear;
|
|
39 my %dataGearName;
|
253
|
40 my $implementation;
|
|
41 my $interface;
|
250
|
42
|
194
|
43 sub getDataGear {
|
|
44 my ($filename) = @_;
|
253
|
45 my ($codeGearName, $name, $inTypedef);
|
194
|
46 open my $fd,"<",$filename or die("can't open $filename $!");
|
|
47 while (<$fd>) {
|
|
48 if (! $inTypedef) {
|
|
49 if (/^typedef struct (\w+) {/) {
|
|
50 $inTypedef = 1;
|
|
51 $name = $1;
|
|
52 $dataGear{$name} = $_;
|
253
|
53 $code{$name} = [];
|
249
|
54 } elsif (/^(\w+)\* create(\w+)\(/) {
|
|
55 if (defined $interface) {
|
|
56 die "duplicate interface $interface\n";
|
|
57 }
|
|
58 $interface = $1;
|
250
|
59 $implementation = $2;
|
|
60 if ( -f "$interface.cbc") {
|
|
61 &getDataGear("$interface.cbc");
|
|
62 }
|
226
|
63 }
|
194
|
64 next;
|
|
65 }
|
249
|
66 # gather type name and type
|
194
|
67 $dataGear{$name} .= $_;
|
196
|
68 if (/(\w+);$/ and !/^} (\w+)/) {
|
250
|
69 my $tmp = $1 . "\n";
|
200
|
70 if (/{/) {
|
|
71 $tmp = "{" . $';
|
201
|
72 $tmp =~ s/;$//;
|
200
|
73 }
|
|
74 $var{$name} .= $tmp;
|
196
|
75 $tmp = $`;
|
|
76 $tmp =~ s/^\s*//;
|
|
77 $type{$name} .= $tmp . "\n";
|
250
|
78 } elsif (/\_\_code (\w+)\(/) {
|
|
79 push $code{$name}, $1;
|
|
80 }
|
194
|
81 if (/^}/) {
|
|
82 $inTypedef = 0;
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86
|
250
|
87 sub generateStub {
|
251
|
88 my($fd,$prevCodeGearName,$dataGearName) = @_;
|
250
|
89 print $fd "__code ", $prevCodeGearName ,"_stub (struct Context* context) {\n";
|
251
|
90 print $fd $dataGearName;
|
250
|
91 print $fd "\n} \n\n";
|
251
|
92 return 1;
|
250
|
93 }
|
|
94
|
253
|
95 sub generateStubArgs {
|
|
96 my($codeGearName, $varName, $typeName, $typeField, $interface) = @_;
|
|
97 push @{$dataGearVar{$codeGearName}},$varName;
|
|
98 if ($typeField ne $varName) {
|
|
99 $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $typeName, $varName);\n";
|
|
100 # print STDOUT "$codeGearName \t$typeName* $varName = ($typeName*)GearImpl(context, $typeName, $varName);\n";
|
|
101 } else {
|
|
102 for my $ivar ($var{$interface}) {
|
|
103 if ($varName eq $ivar) {
|
|
104 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
|
|
105 # print STDOUT "$codeGearName \t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
|
|
106 return;
|
|
107 }
|
|
108 }
|
|
109 $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName)->$typeField;\n";
|
|
110 # print STDOUT "$codeGearName \t$typeName* $varName = Gearef(context, $typeName)->$typeField;\n";
|
|
111 }
|
|
112 }
|
|
113
|
194
|
114 sub generateDataGear {
|
249
|
115 my ($filename) = @_;
|
|
116 open my $in,"<",$filename or die("can't open $filename $!");
|
254
|
117
|
|
118 my $fn;
|
|
119 if ($opt_o) {
|
|
120 $fn = $opt_o;
|
|
121 } else {
|
|
122 my $fn1 = $filename;
|
|
123 $fn1 =~ s/\.cbc/.c/;
|
|
124 my $i = 1;
|
255
|
125 $fn = "$dir/$fn1";
|
254
|
126 while ( -f $fn) {
|
255
|
127 $fn = "$dir/$fn1.$i";
|
254
|
128 $i++;
|
|
129 }
|
250
|
130 }
|
249
|
131 open my $fd,">",$fn or die("can't write $fn $!");
|
254
|
132
|
249
|
133 my $prevCodeGearName;
|
250
|
134 my $inTypedef = 0;
|
|
135 my %stub;
|
251
|
136 my $codeGearName;
|
254
|
137
|
249
|
138 while (<$in>) {
|
|
139 if (! $inTypedef) {
|
|
140 if (/^typedef struct (\w+) {/) {
|
|
141 $inTypedef = 1;
|
253
|
142 } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
|
251
|
143 $codeGearName = $1;
|
253
|
144 my $args = $2;
|
|
145 my $tail = $3;
|
250
|
146 if ($codeGearName =~ /_stub$/) {
|
|
147 $stub{$codeGearName} = 1;
|
|
148 print $fd $_;
|
|
149 next;
|
|
150 }
|
249
|
151 if (defined $prevCodeGearName) {
|
250
|
152 if (defined $stub{$prevCodeGearName."_stub"}) {
|
|
153 undef $prevCodeGearName;
|
|
154 print $fd $_;
|
|
155 next;
|
|
156 }
|
253
|
157 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$prevCodeGearName});
|
249
|
158 }
|
253
|
159 $prevCodeGearName = $codeGearName;
|
|
160 $dataGearVar{$codeGearName} = [];
|
|
161 my $newArgs = "";
|
|
162 while($args) {
|
|
163 if ($args =~ s/(^\s*,\s*)//) {
|
|
164 $newArgs .= $1;
|
|
165 }
|
|
166 # replace __code next
|
|
167 if ($args =~ s/^\_\_code\s(\w+)\([^)]*\)//) {
|
|
168 my $next = $1;
|
|
169 my @args = split(/,/,$2);
|
|
170 $newArgs .= "enum Code $next";
|
|
171 for my $arg (@args) {
|
|
172 $arg =~ s/^\s*//;
|
|
173 $arg =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//;
|
|
174 my $structType = $1;
|
|
175 my $typeName = $2;
|
|
176 my $varName = $4;
|
|
177 my $typeField = lcfirst($typeName);
|
|
178 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface);
|
|
179 }
|
|
180 } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) {
|
|
181 my $structType = $1;
|
|
182 my $typeName = $2;
|
|
183 my $varName = $4;
|
|
184 my $typeField = lcfirst($typeName);
|
|
185 $newArgs .= $&;
|
|
186 &generateStubArgs($codeGearName, $varName, $typeName, $typeField, $interface);
|
255
|
187 } elsif ($args =~ s/(.*,)//) {
|
|
188 $newArgs .= $1;
|
|
189 } else {
|
|
190 $newArgs .= $args;
|
|
191 last;
|
253
|
192 }
|
|
193 }
|
|
194 $dataGearName{$codeGearName} .= "\tgoto $codeGearName(context";
|
|
195 for my $arg ( @{$dataGearVar{$codeGearName}}) {
|
|
196 $dataGearName{$codeGearName} .= ", $arg";
|
|
197 }
|
|
198 $dataGearName{$codeGearName} .= ");";
|
|
199 print $fd "__code $codeGearName($newArgs)$tail\n";
|
|
200 next;
|
250
|
201 } elsif (/^(.*)goto next\(\.\.\.(.*)\);/) {
|
|
202 my $prev = $1;
|
|
203 my $args = $2;
|
|
204 print $fd "${prev}goto meta(context, next);\n";
|
|
205 next;
|
249
|
206 }
|
250
|
207 print $fd $_;
|
249
|
208 next;
|
|
209 }
|
|
210 # gather type name and type
|
|
211 if (/^}/) {
|
|
212 $inTypedef = 0;
|
|
213 }
|
250
|
214 print $fd $_;
|
194
|
215 }
|
250
|
216 if (defined $prevCodeGearName) {
|
|
217 if (!defined $stub{$prevCodeGearName."_stub"}) {
|
251
|
218 $stub{$prevCodeGearName."_stub"} = &generateStub($fd,$prevCodeGearName,$dataGearName{$codeGearName});
|
250
|
219 }
|
228
|
220 }
|
194
|
221 }
|
|
222
|
|
223 # end
|