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