comparison src/gearsTools/generate_context.pl @ 354:fde5f96c6ff1

use common perl script
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Fri, 12 Jun 2020 20:44:01 +0900
parents 36ed64fea8c1
children 045299ad7e97
comparison
equal deleted inserted replaced
353:c958c355f805 354:fde5f96c6ff1
1 #!/usr/bin/perl 1 #!/usr/bin/perl
2 2
3 use Getopt::Std; 3 use Getopt::Long;
4 use strict; 4 use strict;
5 5
6 # 6 #
7 # generrate Gears OS context heaader and initializer from CbC sources 7 # generrate Gears OS context heaader and initializer from CbC sources
8 # 8 #
9 # CodeGear 9 # CodeGear
10 # 10 #
11 # get stub information from # *.c 11 # get stub information from # *.c
12 # __code taskManager_stub(struct Context* cbc_context) { 12 # __code taskManager_stub(struct Context* context) {
13 # 13 #
14 # generate CodeGear indexn in context.h 14 # generate CodeGear indexn in context.h
15 # C_taskManager, 15 # C_taskManager,
16 # 16 #
17 # generate CodeGear stub reference in context.h 17 # generate CodeGear stub reference in context.h
18 # extern __code taskManager_stub(struct Context*); 18 # extern __code taskManager_stub(struct Context*);
19 # 19 #
20 # generate CodeGear stub reference in $name-context.h for each module 20 # generate CodeGear stub reference in $name-context.h for each module
21 # cbc_context->code[C_taskManager] = taskManager_stub; 21 # context->code[C_taskManager] = taskManager_stub;
22 # 22 #
23 # DataGear 23 # DataGear
24 # 24 #
25 # get DataGear information from context.h 25 # get DataGear information from context.h
26 # struct Worker { 26 # struct Worker {
41 # ALLOC_DATA(context, Worker); 41 # ALLOC_DATA(context, Worker);
42 # 42 #
43 43
44 my $ddir = "c"; 44 my $ddir = "c";
45 45
46 our($opt_o,$opt_d,$opt_h,$opt_w); 46 our($opt_o,$opt_d,$opt_h,$opt_w, $opt_project);
47 getopts('o:d:hw'); 47 GetOptions(
48 "o=s" => \$opt_o,
49 "d=s" => \$opt_d,
50 "h" => \$opt_h,
51 "w" => \$opt_w,
52 "project" => \$opt_project,
53 );
54
48 55
49 my $name = $opt_o?$opt_o:"gears"; 56 my $name = $opt_o?$opt_o:"gears";
50 57
51 if ($opt_d) { 58 if ($opt_d) {
52 $ddir = $opt_d; 59 $ddir = $opt_d;
59 if ($opt_h) { 66 if ($opt_h) {
60 print "$0 [-d distdir] [-h]\n"; 67 print "$0 [-d distdir] [-h]\n";
61 exit; 68 exit;
62 } 69 }
63 70
71 my %projects = (
72 gears => { name => "gears", cotnext => "context" , template => "Gears::Context::Template"},
73 xv6 => { name => "xv6" , context => "cbc_context" , template => "Gears::Context::Template::XV6"},
74 );
75
76
77 my $context_name = "context";
78
79 my $project = $projects{gears};
80
81 if ($opt_project && exists $projects{$opt_project}) {
82 $context_name = $projects{$opt_project}->{context};
83 }
84
85
86
64 my %codeGear; 87 my %codeGear;
65 my %dataGear; 88 my %dataGear;
66 my %constructor; 89 my %constructor;
67 90
68 { 91 {
78 my $output = $opt_w ? "context.h" : "stdout"; 101 my $output = $opt_w ? "context.h" : "stdout";
79 102
80 use Data::Dumper; 103 use Data::Dumper;
81 my @cbc_files; 104 my @cbc_files;
82 map { push(@cbc_files,File::Spec->rel2abs($_)); } @ARGV; 105 map { push(@cbc_files,File::Spec->rel2abs($_)); } @ARGV;
83 my $gears = Gears::Context->new(compile_sources => \@cbc_files, find_root => "$FindBin::Bin/../", output => $output); 106 my $gears = Gears::Context->new(compile_sources => \@cbc_files, find_root => "$FindBin::Bin/../", output => $output, template => $project->{template});
84 my $data_gears = $gears->extraction_dg_compile_sources($gears->{compile_sources}); 107 my $data_gears = $gears->extraction_dg_compile_sources($gears->{compile_sources});
85 my $g = $gears->set_data_gear_header_path(keys %{$data_gears->{impl}},keys %{$data_gears->{interfaces}}); 108 my $g = $gears->set_data_gear_header_path(keys %{$data_gears->{impl}},keys %{$data_gears->{interfaces}});
86 109
87 my $dg2path = $gears->update_dg_each_header_path($data_gears,$g); 110 my $dg2path = $gears->update_dg_each_header_path($data_gears,$g);
88 my $tree = $gears->createImplTree_from_header($dg2path); 111 my $tree = $gears->createImplTree_from_header($dg2path);
107 130
108 sub getStubInfo { 131 sub getStubInfo {
109 my ($filename) = @_; 132 my ($filename) = @_;
110 open my $fd,"<",$filename or die("can't open $filename $!"); 133 open my $fd,"<",$filename or die("can't open $filename $!");
111 while (<$fd>) { 134 while (<$fd>) {
112 if (/^__code (\w+)_stub\(struct *Context *\* *cbc_context\)/) { 135 if (/^__code (\w+)_stub\(struct *Context *\* *${context_name}\)/) {
113 $codeGear{$1} = $filename; 136 $codeGear{$1} = $filename;
114 } elsif (/^(\w+)(\*)+ *create(\w+)\(([^]]*)\)/) { 137 } elsif (/^(\w+)(\*)+ *create(\w+)\(([^]]*)\)/) {
115 my $interface = $1; 138 my $interface = $1;
116 my $implementation = $3; 139 my $implementation = $3;
117 my $constructorArgs = $4; 140 my $constructorArgs = $4;
165 } 188 }
166 print $fd "};\n"; 189 print $fd "};\n";
167 190
168 my $code_init = ''; 191 my $code_init = '';
169 for my $code ( sort keys %mCodeGear ) { 192 for my $code ( sort keys %mCodeGear ) {
170 $code_init .= " cbc_context->code[C_${code}] = ${code}_stub;\n"; 193 $code_init .= " context->code[C_${code}] = ${code}_stub;\n";
171 } 194 }
172 195
173 my $data_num = keys(%dataGear); 196 my $data_num = keys(%dataGear);
174 $data_num++; 197 $data_num++;
175 my $context_c = << "EOFEOF"; 198 my $context_c;
199 if ($project->{name} eq "xv6") {
200 $context_c .= << "EOFEOF";
176 #ifndef CBCXV6 201 #ifndef CBCXV6
177 #include <stdlib.h> 202 #include <stdlib.h>
178 #endif 203 #endif
204 EOFEOF
205 } else {
206 $context_c .= << "EOFEOF";
207 #include <stdlib.h>
208 EOFEOF
209 }
210
211 $context_c .= << "EOFEOF";
179 212
180 #include "../context.h" 213 #include "../context.h"
181 214
182 void initContext(struct Context* cbc_context) { 215 void initContext(struct Context* $context_name) {
183 cbc_context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE; 216 ${context_name}\->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
184 cbc_context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*); 217 ${context_name}\->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*);
185 cbc_context->data = NEWN(ALLOCATE_SIZE, union Data*); 218 ${context_name}\->data = NEWN(ALLOCATE_SIZE, union Data*);
186 cbc_context->heapStart = NEWN(cbc_context->heapLimit, char); 219 ${context_name}\->heapStart = NEWN(${context_name}\->heapLimit, char);
187 cbc_context->heap = cbc_context->heapStart; 220 ${context_name}\->heap = ${context_name}\->heapStart;
188 // cbc_context->codeNum = Exit; 221 // ${context_name}\->codeNum = Exit;
189 222
190 $code_init 223 $code_init
191 224
192 #include "dataGearInit.c" 225 #include "dataGearInit.c"
193 cbc_context->dataNum = $data_num; 226 ${context_name}\->dataNum = $data_num;
194 } 227 }
195 EOFEOF 228 EOFEOF
196 229
197 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!"); 230 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!");
198 print $fd $context_c; 231 print $fd $context_c;
199 232
200 my $meta_call = <<"EOFEOF"; 233 my $meta_call = <<"EOFEOF";
201 __code meta(struct Context* cbc_context, enum Code next) { 234 __code meta(struct Context* ${context_name}, enum Code next) {
202 // printf("meta %d\\n",next); 235 // printf("meta %d\\n",next);
203 goto (cbc_context->code[next])(cbc_context); 236 goto (${context_name}\->code[next])(${context_name});
204 } 237 }
205 238
206 __code parGotoMeta(struct Context* cbc_context, enum Code next) { 239 __code parGotoMeta(struct Context* ${context_name}, enum Code next) {
207 cbc_context->task = NULL; 240 ${context_name}->task = NULL;
208 cbc_context->taskList = NULL; 241 ${context_name}->taskList = NULL;
209 goto (cbc_context->code[Gearef(cbc_context, TaskManager)->taskManager->TaskManager.spawnTasks])(cbc_context); 242 goto (${context_name}\->code[Gearef(${context_name}, TaskManager)->taskManager->TaskManager.spawnTasks])(${context_name});
210 } 243 }
211 244
212 __code start_code(struct Context* cbc_context) { 245 __code start_code(struct Context* ${context_name}) {
213 goto meta(cbc_context, cbc_context->next); 246 goto meta(${context_name}, ${context_name}\->next);
214 } 247 }
215 248
216 __code start_code_stub(struct Context* cbc_context) { 249 __code start_code_stub(struct Context* ${context_name}) {
217 goto start_code(cbc_context); 250 goto start_code(${context_name});
218 } 251 }
219 252 EOFEOF
220 __code exit_code(struct Context* cbc_context) { 253
221 // free(cbc_context->code); 254 if ($project->{name} eq "gears") {
222 // free(cbc_context->data); 255 $mata_call .= <<"EOFEOF";
223 // free(cbc_context->heapStart); 256 __code exit_code(struct Context* ${context_name}) {
257 free(${context_name}->code);
258 free(${context_name}->data);
259 free(${context_name}->heapStart);
260 goto exit(0);
261 }
262 EOFEOF
263
264 } else {
265
266 $mata_call .= <<"EOFEOF";
267 __code exit_code(struct Context* ${context_name}) {
268 // free(${context_name}->code);
269 // free(${context_name}->data);
270 // free(${context_name}->heapStart);
224 goto exit_code(cbc_context); 271 goto exit_code(cbc_context);
225 } 272 }
226 273 EOFEOF
227 __code exit_code_stub(struct Context* cbc_context) { 274 }
228 goto exit_code(cbc_context); 275
276 $mata_call .= <<"EOFEOF";
277 __code exit_code_stub(struct Context* ${context_name}) {
278 goto exit_code(${context_name});
229 } 279 }
230 280
231 // end context_c 281 // end context_c
232 EOFEOF 282 EOFEOF
233 283
248 } 298 }
249 } 299 }
250 300
251 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!"); 301 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!");
252 for my $data ( sort keys %dataGear ) { 302 for my $data ( sort keys %dataGear ) {
253 print $fd " ALLOC_DATA(cbc_context, ${data});\n"; 303 print $fd " ALLOC_DATA(${context_name}, ${data});\n";
254 } 304 }
255 } 305 }
256 306
257 # end 307 # end