Mercurial > hg > CbC > CbC_xv6
annotate src/gearsTools/generate_context.pl @ 346:36ed64fea8c1
emit context.h
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 02 Mar 2020 14:19:49 +0900 |
parents | 49a70efcbd3a |
children | fde5f96c6ff1 |
rev | line source |
---|---|
44 | 1 #!/usr/bin/perl |
2 | |
3 use Getopt::Std; | |
4 use strict; | |
5 | |
6 # | |
7 # generrate Gears OS context heaader and initializer from CbC sources | |
8 # | |
9 # CodeGear | |
10 # | |
11 # get stub information from # *.c | |
52 | 12 # __code taskManager_stub(struct Context* cbc_context) { |
44 | 13 # |
14 # generate CodeGear indexn in context.h | |
15 # C_taskManager, | |
16 # | |
17 # generate CodeGear stub reference in context.h | |
18 # extern __code taskManager_stub(struct Context*); | |
19 # | |
20 # generate CodeGear stub reference in $name-context.h for each module | |
52 | 21 # cbc_context->code[C_taskManager] = taskManager_stub; |
44 | 22 # |
23 # DataGear | |
24 # | |
25 # get DataGear information from context.h | |
26 # struct Worker { | |
27 # int id; | |
28 # struct Context* contexts; | |
29 # enum Code execute; | |
30 # enum Code taskSend; | |
31 # enum Code taskRecive; | |
32 # enum Code shutdown; | |
33 # struct Queue* tasks; | |
34 # } Worker; | |
35 # | |
36 # generate typedefs and DataGear index in context.h | |
37 # typedef struct Worker Worker; | |
38 # D_Worker, | |
39 # | |
40 # generate DataGear allocator in context.h | |
41 # ALLOC_DATA(context, Worker); | |
42 # | |
43 | |
44 my $ddir = "c"; | |
45 | |
111
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
46 our($opt_o,$opt_d,$opt_h,$opt_w); |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
47 getopts('o:d:hw'); |
44 | 48 |
49 my $name = $opt_o?$opt_o:"gears"; | |
50 | |
51 if ($opt_d) { | |
52 $ddir = $opt_d; | |
53 } | |
54 | |
55 if ( ! -d $ddir) { | |
56 mkdir $ddir; | |
57 } | |
58 | |
59 if ($opt_h) { | |
60 print "$0 [-d distdir] [-h]\n"; | |
61 exit; | |
62 } | |
63 | |
64 my %codeGear; | |
65 my %dataGear; | |
66 my %constructor; | |
67 | |
111
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
68 { |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
69 use FindBin; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
70 use lib "$FindBin::Bin/lib"; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
71 |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
72 use File::Spec; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
73 use Cwd 'getcwd'; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
74 |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
75 use Gears::Context; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
76 use Getopt::Std; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
77 |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
78 my $output = $opt_w ? "context.h" : "stdout"; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
79 |
180 | 80 use Data::Dumper; |
111
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
81 my @cbc_files; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
82 map { push(@cbc_files,File::Spec->rel2abs($_)); } @ARGV; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
83 my $gears = Gears::Context->new(compile_sources => \@cbc_files, find_root => "$FindBin::Bin/../", output => $output); |
233 | 84 my $data_gears = $gears->extraction_dg_compile_sources($gears->{compile_sources}); |
180 | 85 my $g = $gears->set_data_gear_header_path(keys %{$data_gears->{impl}},keys %{$data_gears->{interfaces}}); |
111
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
86 |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
87 my $dg2path = $gears->update_dg_each_header_path($data_gears,$g); |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
88 my $tree = $gears->createImplTree_from_header($dg2path); |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
89 $gears->tree2create_context_h($tree); |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
90 } |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
91 |
44 | 92 # gather module Information for code table initialization |
93 for (@ARGV) { | |
94 next if (/context.c/); | |
95 &getStubInfo($_); | |
96 } | |
97 | |
98 my (%mCodeGear) = (%codeGear); | |
99 | |
100 # anyway we gather all Gears Information | |
101 while (<*.c test/*.c>) { | |
102 next if (/context.c/); | |
103 &getStubInfo($_); | |
104 } | |
105 | |
106 &generateContext(); | |
107 | |
108 sub getStubInfo { | |
109 my ($filename) = @_; | |
110 open my $fd,"<",$filename or die("can't open $filename $!"); | |
111 while (<$fd>) { | |
52 | 112 if (/^__code (\w+)_stub\(struct *Context *\* *cbc_context\)/) { |
44 | 113 $codeGear{$1} = $filename; |
114 } elsif (/^(\w+)(\*)+ *create(\w+)\(([^]]*)\)/) { | |
115 my $interface = $1; | |
116 my $implementation = $3; | |
117 my $constructorArgs = $4; | |
118 $constructor{$implementation} = [$interface, $constructorArgs]; | |
119 } | |
120 } | |
121 | |
122 open my $cx,"<","context.h" or die("can't open context.h $!"); | |
123 my $inUnionData = 0; | |
124 while (<$cx>) { | |
125 if (! $inUnionData) { | |
126 if ( /^union Data/) { | |
127 $inUnionData = 1; | |
128 } | |
129 next; | |
130 } | |
131 last if (/union Data end/); | |
132 if (/struct (\w+) \{/) { | |
133 $dataGear{$1} = 'struct'; | |
134 } elsif (/^\s{4}(\w+) (\w+);/) { # primitive type | |
346 | 135 my $vtype = $1; |
136 my $vname = $2; | |
137 if (exists $dataGear{$vname}) { | |
138 next; | |
139 } | |
140 $dataGear{$vtype} = 'primitive'; | |
44 | 141 } |
142 $dataGear{"Context"} = "struct"; | |
143 } | |
144 } | |
145 | |
146 sub generateContext { | |
147 $codeGear{"start_code"} = "$ddir/$name-context.c"; | |
148 $codeGear{"exit_code"} = "$ddir/$name-context.c"; | |
149 $mCodeGear{"start_code"} = "$ddir/$name-context.c"; | |
150 $mCodeGear{"exit_code"} = "$ddir/$name-context.c"; | |
151 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); | |
152 for my $code ( sort keys %codeGear ) { | |
153 print $fd "extern __code ${code}_stub(struct Context*);\n"; | |
154 } | |
155 for my $impl ( sort keys %constructor ) { | |
156 my ($interface, $constructorArgs) = @{$constructor{$impl}}; | |
157 print $fd "extern ${interface}* create${impl}($constructorArgs);\n"; | |
158 } | |
159 print $fd "\n"; | |
160 | |
161 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!"); | |
162 print $fd "enum Code {\n"; | |
163 for my $code ( sort keys %codeGear ) { | |
164 print $fd " C_${code},\n"; | |
165 } | |
166 print $fd "};\n"; | |
167 | |
168 my $code_init = ''; | |
169 for my $code ( sort keys %mCodeGear ) { | |
52 | 170 $code_init .= " cbc_context->code[C_${code}] = ${code}_stub;\n"; |
44 | 171 } |
172 | |
173 my $data_num = keys(%dataGear); | |
174 $data_num++; | |
175 my $context_c = << "EOFEOF"; | |
47 | 176 #ifndef CBCXV6 |
44 | 177 #include <stdlib.h> |
47 | 178 #endif |
44 | 179 |
180 #include "../context.h" | |
181 | |
52 | 182 void initContext(struct Context* cbc_context) { |
183 cbc_context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE; | |
184 cbc_context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*); | |
185 cbc_context->data = NEWN(ALLOCATE_SIZE, union Data*); | |
186 cbc_context->heapStart = NEWN(cbc_context->heapLimit, char); | |
187 cbc_context->heap = cbc_context->heapStart; | |
188 // cbc_context->codeNum = Exit; | |
44 | 189 |
190 $code_init | |
191 | |
192 #include "dataGearInit.c" | |
52 | 193 cbc_context->dataNum = $data_num; |
44 | 194 } |
195 EOFEOF | |
196 | |
197 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!"); | |
198 print $fd $context_c; | |
199 | |
200 my $meta_call = <<"EOFEOF"; | |
52 | 201 __code meta(struct Context* cbc_context, enum Code next) { |
44 | 202 // printf("meta %d\\n",next); |
52 | 203 goto (cbc_context->code[next])(cbc_context); |
45 | 204 } |
205 | |
52 | 206 __code parGotoMeta(struct Context* cbc_context, enum Code next) { |
207 cbc_context->task = NULL; | |
208 cbc_context->taskList = NULL; | |
53 | 209 goto (cbc_context->code[Gearef(cbc_context, TaskManager)->taskManager->TaskManager.spawnTasks])(cbc_context); |
44 | 210 } |
211 | |
52 | 212 __code start_code(struct Context* cbc_context) { |
53 | 213 goto meta(cbc_context, cbc_context->next); |
44 | 214 } |
215 | |
52 | 216 __code start_code_stub(struct Context* cbc_context) { |
217 goto start_code(cbc_context); | |
44 | 218 } |
219 | |
52 | 220 __code exit_code(struct Context* cbc_context) { |
53 | 221 // free(cbc_context->code); |
222 // free(cbc_context->data); | |
223 // free(cbc_context->heapStart); | |
224 goto exit_code(cbc_context); | |
44 | 225 } |
226 | |
52 | 227 __code exit_code_stub(struct Context* cbc_context) { |
228 goto exit_code(cbc_context); | |
44 | 229 } |
230 | |
231 // end context_c | |
232 EOFEOF | |
233 | |
234 print $fd $meta_call; | |
235 | |
236 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!"); | |
237 print $fd "enum DataType {\n"; | |
238 print $fd " D_Code,\n"; | |
239 for my $data ( sort keys %dataGear ) { | |
240 print $fd " D_${data},\n"; | |
241 } | |
242 print $fd "};\n\n"; | |
243 | |
244 open my $fd,">","$ddir/typedefData.h" or die("can't open $ddir/typedefData.h $!"); | |
245 for my $data ( sort keys %dataGear ) { | |
246 if ($dataGear{$data} eq 'struct') { | |
247 print $fd "typedef struct ${data} ${data};\n"; | |
248 } | |
249 } | |
250 | |
251 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!"); | |
252 for my $data ( sort keys %dataGear ) { | |
52 | 253 print $fd " ALLOC_DATA(cbc_context, ${data});\n"; |
44 | 254 } |
255 } | |
256 | |
257 # end |