comparison src/parallel_execution/generate_context.pl @ 277:9d671e63df74

generate extern
author mir3636
date Thu, 02 Feb 2017 18:29:50 +0900
parents 195518ab62fc
children 23767f714f4a
comparison
equal deleted inserted replaced
276:27bc962020de 277:9d671e63df74
61 exit; 61 exit;
62 } 62 }
63 63
64 my %codeGear; 64 my %codeGear;
65 my %dataGear; 65 my %dataGear;
66 my %constructor;
66 67
67 # gather module Information for code table initialization 68 # gather module Information for code table initialization
68 for (@ARGV) { 69 for (@ARGV) {
69 next if (/context.c/); 70 next if (/context.c/);
70 &getStubInfo($_); 71 &getStubInfo($_);
84 my ($filename) = @_; 85 my ($filename) = @_;
85 open my $fd,"<",$filename or die("can't open $filename $!"); 86 open my $fd,"<",$filename or die("can't open $filename $!");
86 while (<$fd>) { 87 while (<$fd>) {
87 if (/^__code (\w+)_stub\(struct Context\* context\)/ or /^\s__code (\w+)_stub\(struct Context\* context\)/) { 88 if (/^__code (\w+)_stub\(struct Context\* context\)/ or /^\s__code (\w+)_stub\(struct Context\* context\)/) {
88 $codeGear{$1} = $filename; 89 $codeGear{$1} = $filename;
90 } elsif (/^(\w+)(\*)+ create(\w+)\(([^]]*)\)/) {
91 my $interface = $1;
92 my $implementation = $3;
93 my $constructorArgs = $4;
94 $constructor{$implementation} = [$interface, $constructorArgs];
89 } 95 }
90 } 96 }
91 97
92 open my $cx,"<","context.h" or die("can't open context.h $!"); 98 open my $cx,"<","context.h" or die("can't open context.h $!");
93 my $inUnionData = 0; 99 my $inUnionData = 0;
108 sub generateContext { 114 sub generateContext {
109 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); 115 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!");
110 for my $code ( sort keys %codeGear ) { 116 for my $code ( sort keys %codeGear ) {
111 print $fd "extern __code ${code}_stub(struct Context*);\n"; 117 print $fd "extern __code ${code}_stub(struct Context*);\n";
112 } 118 }
119 for my $impl ( sort keys %constructor ) {
120 my ($interface, $constructorArgs) = @{$constructor{$impl}};
121 print $fd "extern $interface create${impl}($constructorArgs);\n";
122 }
113 print $fd "\n"; 123 print $fd "\n";
114 124
115 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!"); 125 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!");
116 print $fd "enum Code {\n"; 126 print $fd "enum Code {\n";
117 for my $code ( sort keys %codeGear ) { 127 for my $code ( sort keys %codeGear ) {
127 my $context_c = << "EOFEOF"; 137 my $context_c = << "EOFEOF";
128 #include <stdlib.h> 138 #include <stdlib.h>
129 139
130 #include "../context.h" 140 #include "../context.h"
131 #include "extern.h" 141 #include "extern.h"
142
143 extern __code start_code(struct Context* context);
144 extern __code exit_code(struct Context* context);
145 extern __code meta(struct Context* context, enum Code next);
146 extern void initContext(struct Context* context);
132 147
133 void initContext(struct Context* context) { 148 void initContext(struct Context* context) {
134 context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE; 149 context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE;
135 context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*); 150 context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*);
136 context->data = NEWN(ALLOCATE_SIZE, union Data*); 151 context->data = NEWN(ALLOCATE_SIZE, union Data*);
150 } 165 }
151 EOFEOF 166 EOFEOF
152 167
153 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!"); 168 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!");
154 print $fd $context_c; 169 print $fd $context_c;
155 170
171 my $meta_call = <<"EOFEOF";
172
173 __code meta(struct Context* context, enum Code next) {
174 // printf("meta %d\n",next);
175 goto (context->code[next])(context);
176 }
177
178 __code start_code(struct Context* context) {
179 goto meta(context, context->next);
180 }
181
182 __code start_code_stub(struct Context* context) {
183 goto start_code(context);
184 }
185
186 __code exit_code(struct Context* context) {
187 free(context->code);
188 free(context->data);
189 free(context->heapStart);
190 goto exit(0);
191 }
192
193 __code exit_code_stub(struct Context* context) {
194 goto exit_code(context);
195 }
196
197 // end $context_c
198 EOFEOF
156 199
157 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!"); 200 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!");
158 print $fd "enum DataType {\n"; 201 print $fd "enum DataType {\n";
159 print $fd " D_Code,\n"; 202 print $fd " D_Code,\n";
160 for my $data ( sort keys %dataGear ) { 203 for my $data ( sort keys %dataGear ) {