Mercurial > hg > CbC > CbC_xv6
annotate src/gearsTools/trans_impl.pl @ 130:3312d7ce91e6
fix output instance name
author | anatofuz |
---|---|
date | Tue, 03 Dec 2019 14:58:00 +0900 |
parents | 7ce72e373d9b |
children | 8e93917ce4be |
rev | line source |
---|---|
102 | 1 #!/usr/bin/env perl |
2 use strict; | |
3 use warnings; | |
4 | |
5 use FindBin; | |
6 use lib "$FindBin::Bin/lib"; | |
7 use Gears::Util; | |
8 | |
124 | 9 use Getopt::Std; |
122 | 10 use File::Spec; |
102 | 11 |
12 my %opt; | |
127
46d792f92156
impl output option at trans_impl.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
125
diff
changeset
|
13 getopts("wo:" => \%opt); |
102 | 14 |
15 my $impl_file = shift or die 'require impl file'; | |
122 | 16 my $impl_ir = Gears::Util->parse_with_rewrite(File::Spec->rel2abs($impl_file)); |
112 | 17 my $interface_file = Gears::Util->find_header($impl_ir->{isa},"$FindBin::Bin/.."); |
102 | 18 |
112 | 19 my $inter_ir = Gears::Util->parse_with_rewrite($interface_file); |
102 | 20 |
21 | |
22 my $output_file = $impl_file; | |
23 $output_file =~ s/\.h/.cbc/; | |
123 | 24 my $stdout = *STDOUT; |
102 | 25 |
123 | 26 if ($opt{w}) { |
27 open $stdout, '>', $output_file; | |
127
46d792f92156
impl output option at trans_impl.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
125
diff
changeset
|
28 } elsif ($opt{o}) { |
46d792f92156
impl output option at trans_impl.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
125
diff
changeset
|
29 open $stdout, '>', $opt{o}; |
102 | 30 } |
31 | |
127
46d792f92156
impl output option at trans_impl.pl
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
125
diff
changeset
|
32 |
102 | 33 emit_include_part($stdout, $inter_ir->{name}); |
34 emit_impl_header_in_comment($stdout, $impl_file); | |
35 emit_constracutor($stdout,$impl_ir,$inter_ir); | |
36 emit_code_gears($stdout,$impl_ir,$inter_ir); | |
123 | 37 close $stdout; |
102 | 38 |
39 sub emit_include_part { | |
40 my ($out, $interface) = @_; | |
41 print $out <<"EOF" | |
42 #include "../context.h"; | |
43 #interface "$interface.h"; | |
44 | |
45 EOF | |
46 } | |
47 | |
48 sub emit_impl_header_in_comment { | |
49 my ($out, $impl_file) = @_; | |
50 my $line = Gears::Util->slup($impl_file); | |
51 print $out "// ----\n"; | |
52 map { print $out "// $_\n" } split /\n/, $line; | |
53 print $out "// ----\n\n"; | |
54 } | |
55 | |
56 sub emit_constracutor { | |
57 my ($out, $impl_ir, $inter_ir) = @_; | |
58 | |
112 | 59 my @inter_data = @{$inter_ir->{data}}; |
60 my @impl_data = @{$impl_ir->{data}}; | |
102 | 61 my $instance_inter = shift @inter_data; |
112 | 62 |
102 | 63 if ($instance_inter =~ /union\s*Data\*\s*(\w+)/) { |
64 $instance_inter = $1; | |
65 } | |
112 | 66 |
67 my $instance_impl = lcfirst $impl_ir->{name}; | |
68 $instance_impl =~ s/([A-Z])/_\l$1/g; | |
102 | 69 |
70 print $out <<"EOF"; | |
71 $impl_ir->{isa}* create$impl_ir->{name}(struct Context* cbc_context) { | |
72 struct $impl_ir->{isa}* $instance_inter = new $impl_ir->{isa}(); | |
73 struct $impl_ir->{name}* $instance_impl = new $impl_ir->{name}(); | |
74 $instance_inter->$instance_inter = (union Data*)$instance_impl; | |
75 EOF | |
76 | |
109
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
77 for my $datum (@impl_data) { |
102 | 78 if ($datum =~ /\w+ \w+\* (\w+)/) { |
79 print $out " ${instance_impl}->$1 = NULL;\n"; | |
80 next; | |
81 } | |
82 if ($datum =~ /\w+ \w+ (\w+)/) { | |
83 print $out " ${instance_impl}->$1 = 0;\n"; | |
84 } | |
128 | 85 |
86 if ($datum =~ /\w+(\*)? (\w+)/) { | |
87 my $is_pointer = $1; | |
88 my $var_name = $2; | |
89 if ($1) { | |
90 print $out " ${instance_impl}->$var_name = NULL;\n"; | |
91 } else { | |
92 print $out " ${instance_impl}->$var_name = 0;\n"; | |
93 } | |
94 } | |
102 | 95 } |
96 | |
109
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
97 for my $datum (@inter_data) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
98 if ($datum =~ /\w+ \w+\* (\w+)/) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
99 print $out " ${instance_inter}->$1 = NULL;\n"; |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
100 next; |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
101 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
102 if ($datum =~ /\w+ \w+ (\w+)/) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
103 print $out " ${instance_inter}->$1 = 0;\n"; |
128 | 104 next; |
105 } | |
106 if ($datum =~ /\w+(\*)? (\w+)/) { | |
107 my $is_pointer = $1; | |
108 my $var_name = $2; | |
109 if ($1) { | |
130 | 110 print $out " ${instance_inter}->$var_name = NULL;\n"; |
128 | 111 } else { |
130 | 112 print $out " ${instance_inter}->$var_name = 0;\n"; |
128 | 113 } |
109
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
114 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
115 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
116 |
102 | 117 for my $code (@{$inter_ir->{codes}}) { |
112 | 118 my $code_gear = $code->{name}; |
102 | 119 print $out " ${instance_inter}->$code_gear = C_$code_gear$impl_ir->{name};\n" |
120 } | |
121 | |
122 print $out " return $instance_inter;\n"; | |
123 print $out "}\n"; | |
124 } | |
125 | |
126 | |
127 sub emit_code_gears { | |
128 my ($out, $impl_ir, $inter_ir) = @_; | |
129 my $impl = $impl_ir->{name}; | |
124 | 130 my $interface_name = $inter_ir->{name}; |
102 | 131 |
132 my @inter_data = @{$inter_ir->{data}}; | |
133 my $instance_inter = shift @inter_data; | |
134 if ($instance_inter =~ /union\s*Data\*\s*(\w+)/) { | |
135 $instance_inter = $1; | |
136 } | |
137 my $instance_impl = lcfirst $impl_ir->{name}; | |
105 | 138 $instance_impl =~ s/([A-Z])/_\l$1/g; |
102 | 139 my $data_gear_types = {}; |
140 | |
124 | 141 if (defined $impl_ir->{codes}) { |
142 for my $cg (@{$impl_ir->{codes}}) { | |
143 my $data_gears = $cg->{args}; | |
144 while ($data_gears =~ /Type\*\s*(\w+),/g) { | |
145 $data_gears =~ s/Type\*/struct $impl*/; | |
146 } | |
147 | |
148 while ($data_gears =~ /Isa\*\s*(\w+),/g) { | |
149 $data_gears =~ s/Isa\*/struct $interface_name*/; | |
150 } | |
151 print $out "__code $cg->{name}$impl("; | |
152 print $out "$data_gears) {\n\n"; | |
153 | |
154 #__code next(...), __code whenEmpty(...) | |
155 my @cg = (); | |
156 while ($data_gears =~ /__code ([\w(\.)\*\s,]+?\)),?/g) { | |
157 push(@cg, $1); | |
158 } | |
159 | |
160 if (@cg) { | |
161 if (@cg == 2) { | |
162 print $out " if (:TODO:) {\n"; | |
163 print $out " goto ",shift(@cg),";\n"; | |
164 print $out " }\n"; | |
165 print $out " goto ",shift(@cg),";\n"; | |
166 } else { | |
167 print $out " goto ",shift(@cg),";\n"; | |
168 } | |
169 } | |
170 print $out "}\n\n"; | |
171 } | |
172 } | |
173 | |
102 | 174 for my $code_ir (@{$inter_ir->{codes}}) { |
112 | 175 my $data_gears = $code_ir->{args}; |
176 $data_gears =~ s/Impl/struct $impl/g; | |
124 | 177 |
103 | 178 while ($data_gears =~ /Type\*\s*(\w+),/g) { |
124 | 179 $data_gears =~ s/Type\*/struct $interface_name*/; |
102 | 180 } |
181 | |
112 | 182 print $out "__code $code_ir->{name}$impl("; |
102 | 183 print $out "$data_gears) {\n\n"; |
184 | |
185 #__code next(...), __code whenEmpty(...) | |
186 my @cg = (); | |
187 while ($data_gears =~ /__code ([\w(\.)\*\s,]+?\)),?/g) { | |
188 push(@cg, $1); | |
189 } | |
190 | |
191 if (@cg) { | |
192 if (@cg == 2) { | |
193 print $out " if (:TODO:) {\n"; | |
194 print $out " goto ",shift(@cg),";\n"; | |
195 print $out " }\n"; | |
196 print $out " goto ",shift(@cg),";\n"; | |
197 } else { | |
198 print $out " goto ",shift(@cg),";\n"; | |
199 } | |
200 } | |
201 print $out "}\n\n"; | |
202 } | |
203 } |