Mercurial > hg > GearsTemplate
annotate src/parallel_execution/generate_context.pl @ 278:23767f714f4a
fix generate_stub
author | mir3636 |
---|---|
date | Thu, 02 Feb 2017 20:27:56 +0900 |
parents | 9d671e63df74 |
children | 2c2e4e597eb0 |
rev | line source |
---|---|
186 | 1 #!/usr/bin/perl |
2 | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
3 use Getopt::Std; |
254 | 4 use strict; |
186 | 5 |
254 | 6 # |
7 # generrate Gears OS context heaader and initializer from CbC sources | |
8 # | |
186 | 9 # CodeGear |
10 # | |
254 | 11 # get stub information from # *.c |
12 # __code taskManager_stub(struct Context* context) { | |
13 # | |
14 # generate CodeGear indexn in context.h | |
15 # C_taskManager, | |
186 | 16 # |
254 | 17 # generate CodeGear stub reference in context.h |
18 # extern __code taskManager_stub(struct Context*); | |
186 | 19 # |
254 | 20 # generate CodeGear stub reference in $name-context.h for each module |
21 # context->code[C_taskManager] = taskManager_stub; | |
186 | 22 # |
23 # DataGear | |
24 # | |
254 | 25 # get DataGear information from context.h |
26 # struct Worker { | |
186 | 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 # | |
254 | 36 # generate typedefs and DataGear index in context.h |
37 # typedef struct Worker Worker; | |
38 # D_Worker, | |
186 | 39 # |
254 | 40 # generate DataGear allocator in context.h |
41 # ALLOC_DATA(context, Worker); | |
42 # | |
186 | 43 |
254 | 44 my $ddir = "c"; |
186 | 45 |
254 | 46 our($opt_o,$opt_d,$opt_h); |
47 getopts('o:d:h'); | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
48 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
49 my $name = $opt_o; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
50 |
254 | 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; | |
277 | 66 my %constructor; |
254 | 67 |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
68 # gather module Information for code table initialization |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
69 for (@ARGV) { |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
70 next if (/context.c/); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
71 &getStubInfo($_); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
72 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
73 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
74 my (%mCodeGear) = (%codeGear); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
75 |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
76 # anyway we gather all Gears Information |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
77 while (<*.c test/*.c>) { |
257 | 78 next if (/context.c/); |
79 &getStubInfo($_); | |
80 } | |
81 | |
186 | 82 &generateContext(); |
83 | |
84 sub getStubInfo { | |
85 my ($filename) = @_; | |
86 open my $fd,"<",$filename or die("can't open $filename $!"); | |
87 while (<$fd>) { | |
208 | 88 if (/^__code (\w+)_stub\(struct Context\* context\)/ or /^\s__code (\w+)_stub\(struct Context\* context\)/) { |
186 | 89 $codeGear{$1} = $filename; |
277 | 90 } elsif (/^(\w+)(\*)+ create(\w+)\(([^]]*)\)/) { |
91 my $interface = $1; | |
92 my $implementation = $3; | |
93 my $constructorArgs = $4; | |
94 $constructor{$implementation} = [$interface, $constructorArgs]; | |
186 | 95 } |
96 } | |
97 | |
98 open my $cx,"<","context.h" or die("can't open context.h $!"); | |
254 | 99 my $inUnionData = 0; |
186 | 100 while (<$cx>) { |
101 if (! $inUnionData) { | |
102 if ( /^union Data/) { | |
103 $inUnionData = 1; | |
104 } | |
105 next; | |
106 } | |
107 last if (/union Data end/); | |
108 if (/struct (\w+) \{/) { | |
109 $dataGear{$1} = $1; | |
110 } | |
111 } | |
112 } | |
113 | |
114 sub generateContext { | |
254 | 115 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); |
186 | 116 for my $code ( sort keys %codeGear ) { |
117 print $fd "extern __code ${code}_stub(struct Context*);\n"; | |
118 } | |
277 | 119 for my $impl ( sort keys %constructor ) { |
120 my ($interface, $constructorArgs) = @{$constructor{$impl}}; | |
278 | 121 print $fd "extern ${interface}* create${impl}($constructorArgs);\n"; |
277 | 122 } |
186 | 123 print $fd "\n"; |
124 | |
254 | 125 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!"); |
186 | 126 print $fd "enum Code {\n"; |
127 for my $code ( sort keys %codeGear ) { | |
128 print $fd " C_${code},\n"; | |
129 } | |
130 print $fd "};\n"; | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
131 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
132 my $code_init = ''; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
133 for my $code ( sort keys %mCodeGear ) { |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
134 $code_init .= " context->code[C_${code}] = ${code}_stub;\n"; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
135 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
136 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
137 my $context_c = << "EOFEOF"; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
138 #include <stdlib.h> |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
139 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
140 #include "../context.h" |
277 | 141 |
246
421ea91dd76c
Change initContext type from __code to void
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
210
diff
changeset
|
142 void initContext(struct Context* context) { |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
143 context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
144 context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
145 context->data = NEWN(ALLOCATE_SIZE, union Data*); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
146 context->heapStart = NEWN(context->heapLimit, char); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
147 context->heap = context->heapStart; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
148 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
149 // context->codeNum = Exit; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
150 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
151 $code_init |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
152 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
153 #include "dataGearInit.c" |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
154 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
155 // context->data[D_ActiveQueue] = createSynchronizedQueue(context); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
156 // context->data[D_WaitQueue] = createSynchronizedQueue(context); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
157 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
158 context->dataNum = D_Queue; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
159 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
160 EOFEOF |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
161 |
254 | 162 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!"); |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
163 print $fd $context_c; |
277 | 164 |
165 my $meta_call = <<"EOFEOF"; | |
166 | |
167 __code meta(struct Context* context, enum Code next) { | |
168 // printf("meta %d\n",next); | |
169 goto (context->code[next])(context); | |
170 } | |
171 | |
172 __code start_code(struct Context* context) { | |
173 goto meta(context, context->next); | |
174 } | |
175 | |
176 __code start_code_stub(struct Context* context) { | |
177 goto start_code(context); | |
178 } | |
179 | |
180 __code exit_code(struct Context* context) { | |
181 free(context->code); | |
182 free(context->data); | |
183 free(context->heapStart); | |
184 goto exit(0); | |
185 } | |
186 | |
187 __code exit_code_stub(struct Context* context) { | |
188 goto exit_code(context); | |
189 } | |
190 | |
191 // end $context_c | |
192 EOFEOF | |
186 | 193 |
278 | 194 print $fd $meta_call; |
195 | |
254 | 196 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!"); |
186 | 197 print $fd "enum DataType {\n"; |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
198 print $fd " D_Code,\n"; |
186 | 199 for my $data ( sort keys %dataGear ) { |
200 print $fd " D_${data},\n"; | |
201 } | |
202 print $fd "};\n\n"; | |
203 | |
254 | 204 open my $fd,">","$ddir/typedefData.h" or die("can't open $ddir/typedefData.h $!"); |
186 | 205 for my $data ( sort keys %dataGear ) { |
206 print $fd "typedef struct ${data} ${data};\n"; | |
207 } | |
208 | |
254 | 209 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!"); |
186 | 210 for my $data ( sort keys %dataGear ) { |
211 print $fd " ALLOC_DATA(context, ${data});\n"; | |
212 } | |
213 } | |
214 | |
215 # end |