Mercurial > hg > Members > menikon > CbC_xv6
annotate src/gearsTools/generate_context.pl @ 111:239bd73abac6
add gen context.h at generate_context.pl
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 28 Nov 2019 12:17:05 +0900 |
parents | 435bfab09924 |
children | f9df567f7f2d |
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 |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
80 my @cbc_files; |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
81 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
|
82 my $gears = Gears::Context->new(compile_sources => \@cbc_files, find_root => "$FindBin::Bin/../", output => $output); |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
83 my $data_gears = $gears->extraction_dg_compile_sources(); |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
84 my $g = $gears->set_data_gear_header_path(); |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
85 |
239bd73abac6
add gen context.h at generate_context.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
53
diff
changeset
|
86 #use DDP {deparse =>1}; |
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 | |
135 $dataGear{$1} = 'primitive'; | |
136 } | |
137 $dataGear{"Context"} = "struct"; | |
138 } | |
139 } | |
140 | |
141 sub generateContext { | |
142 $codeGear{"start_code"} = "$ddir/$name-context.c"; | |
143 $codeGear{"exit_code"} = "$ddir/$name-context.c"; | |
144 $mCodeGear{"start_code"} = "$ddir/$name-context.c"; | |
145 $mCodeGear{"exit_code"} = "$ddir/$name-context.c"; | |
146 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); | |
147 for my $code ( sort keys %codeGear ) { | |
148 print $fd "extern __code ${code}_stub(struct Context*);\n"; | |
149 } | |
150 for my $impl ( sort keys %constructor ) { | |
151 my ($interface, $constructorArgs) = @{$constructor{$impl}}; | |
152 print $fd "extern ${interface}* create${impl}($constructorArgs);\n"; | |
153 } | |
154 print $fd "\n"; | |
155 | |
156 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!"); | |
157 print $fd "enum Code {\n"; | |
158 for my $code ( sort keys %codeGear ) { | |
159 print $fd " C_${code},\n"; | |
160 } | |
161 print $fd "};\n"; | |
162 | |
163 my $code_init = ''; | |
164 for my $code ( sort keys %mCodeGear ) { | |
52 | 165 $code_init .= " cbc_context->code[C_${code}] = ${code}_stub;\n"; |
44 | 166 } |
167 | |
168 my $data_num = keys(%dataGear); | |
169 $data_num++; | |
170 my $context_c = << "EOFEOF"; | |
47 | 171 #ifndef CBCXV6 |
44 | 172 #include <stdlib.h> |
47 | 173 #endif |
44 | 174 |
175 #include "../context.h" | |
176 | |
52 | 177 void initContext(struct Context* cbc_context) { |
178 cbc_context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE; | |
179 cbc_context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*); | |
180 cbc_context->data = NEWN(ALLOCATE_SIZE, union Data*); | |
181 cbc_context->heapStart = NEWN(cbc_context->heapLimit, char); | |
182 cbc_context->heap = cbc_context->heapStart; | |
183 // cbc_context->codeNum = Exit; | |
44 | 184 |
185 $code_init | |
186 | |
187 #include "dataGearInit.c" | |
52 | 188 cbc_context->dataNum = $data_num; |
44 | 189 } |
190 EOFEOF | |
191 | |
192 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!"); | |
193 print $fd $context_c; | |
194 | |
195 my $meta_call = <<"EOFEOF"; | |
52 | 196 __code meta(struct Context* cbc_context, enum Code next) { |
44 | 197 // printf("meta %d\\n",next); |
52 | 198 goto (cbc_context->code[next])(cbc_context); |
45 | 199 } |
200 | |
52 | 201 __code parGotoMeta(struct Context* cbc_context, enum Code next) { |
202 cbc_context->task = NULL; | |
203 cbc_context->taskList = NULL; | |
53 | 204 goto (cbc_context->code[Gearef(cbc_context, TaskManager)->taskManager->TaskManager.spawnTasks])(cbc_context); |
44 | 205 } |
206 | |
52 | 207 __code start_code(struct Context* cbc_context) { |
53 | 208 goto meta(cbc_context, cbc_context->next); |
44 | 209 } |
210 | |
52 | 211 __code start_code_stub(struct Context* cbc_context) { |
212 goto start_code(cbc_context); | |
44 | 213 } |
214 | |
52 | 215 __code exit_code(struct Context* cbc_context) { |
53 | 216 // free(cbc_context->code); |
217 // free(cbc_context->data); | |
218 // free(cbc_context->heapStart); | |
219 goto exit_code(cbc_context); | |
44 | 220 } |
221 | |
52 | 222 __code exit_code_stub(struct Context* cbc_context) { |
223 goto exit_code(cbc_context); | |
44 | 224 } |
225 | |
226 // end context_c | |
227 EOFEOF | |
228 | |
229 print $fd $meta_call; | |
230 | |
231 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!"); | |
232 print $fd "enum DataType {\n"; | |
233 print $fd " D_Code,\n"; | |
234 for my $data ( sort keys %dataGear ) { | |
235 print $fd " D_${data},\n"; | |
236 } | |
237 print $fd "};\n\n"; | |
238 | |
239 open my $fd,">","$ddir/typedefData.h" or die("can't open $ddir/typedefData.h $!"); | |
240 for my $data ( sort keys %dataGear ) { | |
241 if ($dataGear{$data} eq 'struct') { | |
242 print $fd "typedef struct ${data} ${data};\n"; | |
243 } | |
244 } | |
245 | |
246 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!"); | |
247 for my $data ( sort keys %dataGear ) { | |
52 | 248 print $fd " ALLOC_DATA(cbc_context, ${data});\n"; |
44 | 249 } |
250 } | |
251 | |
252 # end |