Mercurial > hg > CbC > CbC_xv6
annotate src/gearsTools/trans_impl.pl @ 123:004e825f37c7
tweak trans_impl.pl
author | anatofuz |
---|---|
date | Mon, 02 Dec 2019 19:57:06 +0900 |
parents | f6558602f31e |
children | f103beea19f4 |
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 | |
122 | 9 use File::Spec; |
102 | 10 use Getopt::Std; |
11 | |
12 my %opt; | |
13 getopts("w" => \%opt); | |
14 | |
15 my $impl_file = shift or die 'require impl file'; | |
122 | 16 |
17 use Data::Dumper; | |
18 my $impl_ir = Gears::Util->parse_with_rewrite(File::Spec->rel2abs($impl_file)); | |
112 | 19 my $interface_file = Gears::Util->find_header($impl_ir->{isa},"$FindBin::Bin/.."); |
102 | 20 |
112 | 21 my $inter_ir = Gears::Util->parse_with_rewrite($interface_file); |
102 | 22 |
23 | |
24 my $output_file = $impl_file; | |
25 $output_file =~ s/\.h/.cbc/; | |
123 | 26 my $stdout = *STDOUT; |
102 | 27 |
123 | 28 if ($opt{w}) { |
29 open $stdout, '>', $output_file; | |
102 | 30 } |
31 | |
32 emit_include_part($stdout, $inter_ir->{name}); | |
33 emit_impl_header_in_comment($stdout, $impl_file); | |
34 emit_constracutor($stdout,$impl_ir,$inter_ir); | |
35 emit_code_gears($stdout,$impl_ir,$inter_ir); | |
123 | 36 close $stdout; |
102 | 37 |
38 sub emit_include_part { | |
39 my ($out, $interface) = @_; | |
40 print $out <<"EOF" | |
41 #include "../context.h"; | |
42 #interface "$interface.h"; | |
43 | |
44 EOF | |
45 } | |
46 | |
47 sub emit_impl_header_in_comment { | |
48 my ($out, $impl_file) = @_; | |
49 my $line = Gears::Util->slup($impl_file); | |
50 print $out "// ----\n"; | |
51 map { print $out "// $_\n" } split /\n/, $line; | |
52 print $out "// ----\n\n"; | |
53 } | |
54 | |
55 sub emit_constracutor { | |
56 my ($out, $impl_ir, $inter_ir) = @_; | |
57 | |
112 | 58 my @inter_data = @{$inter_ir->{data}}; |
59 my @impl_data = @{$impl_ir->{data}}; | |
102 | 60 my $instance_inter = shift @inter_data; |
112 | 61 |
102 | 62 if ($instance_inter =~ /union\s*Data\*\s*(\w+)/) { |
63 $instance_inter = $1; | |
64 } | |
112 | 65 |
66 my $instance_impl = lcfirst $impl_ir->{name}; | |
67 $instance_impl =~ s/([A-Z])/_\l$1/g; | |
102 | 68 |
69 print $out <<"EOF"; | |
70 $impl_ir->{isa}* create$impl_ir->{name}(struct Context* cbc_context) { | |
71 struct $impl_ir->{isa}* $instance_inter = new $impl_ir->{isa}(); | |
72 struct $impl_ir->{name}* $instance_impl = new $impl_ir->{name}(); | |
73 $instance_inter->$instance_inter = (union Data*)$instance_impl; | |
74 EOF | |
75 | |
109
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
76 for my $datum (@impl_data) { |
102 | 77 if ($datum =~ /\w+ \w+\* (\w+)/) { |
78 print $out " ${instance_impl}->$1 = NULL;\n"; | |
79 next; | |
80 } | |
81 if ($datum =~ /\w+ \w+ (\w+)/) { | |
82 print $out " ${instance_impl}->$1 = 0;\n"; | |
83 } | |
84 } | |
85 | |
109
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
86 for my $datum (@inter_data) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
87 if ($datum =~ /\w+ \w+\* (\w+)/) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
88 print $out " ${instance_inter}->$1 = NULL;\n"; |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
89 next; |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
90 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
91 if ($datum =~ /\w+ \w+ (\w+)/) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
92 print $out " ${instance_inter}->$1 = 0;\n"; |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
93 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
94 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
95 |
102 | 96 for my $code (@{$inter_ir->{codes}}) { |
112 | 97 my $code_gear = $code->{name}; |
102 | 98 print $out " ${instance_inter}->$code_gear = C_$code_gear$impl_ir->{name};\n" |
99 } | |
100 | |
101 print $out " return $instance_inter;\n"; | |
102 print $out "}\n"; | |
103 } | |
104 | |
105 | |
106 sub emit_code_gears { | |
107 my ($out, $impl_ir, $inter_ir) = @_; | |
108 my $impl = $impl_ir->{name}; | |
109 | |
110 my @inter_data = @{$inter_ir->{data}}; | |
111 my $instance_inter = shift @inter_data; | |
112 if ($instance_inter =~ /union\s*Data\*\s*(\w+)/) { | |
113 $instance_inter = $1; | |
114 } | |
115 my $instance_impl = lcfirst $impl_ir->{name}; | |
105 | 116 $instance_impl =~ s/([A-Z])/_\l$1/g; |
102 | 117 my $data_gear_types = {}; |
118 | |
119 for my $code_ir (@{$inter_ir->{codes}}) { | |
112 | 120 my $data_gears = $code_ir->{args}; |
121 $data_gears =~ s/Impl/struct $impl/g; | |
103 | 122 while ($data_gears =~ /Type\*\s*(\w+),/g) { |
102 | 123 my $target = $1; |
124 if (exists $data_gear_types->{$target}){ | |
125 $data_gears =~ s/Type\*/$data_gear_types->{$target}/; | |
126 } else { | |
127 my $td = ""; | |
128 map { $td = $_ if ($_ =~ /$target/) } @inter_data; | |
129 if ($td =~ /(\w+)\s*([\w\*]+)\s*(\w+)/) { | |
130 my $tmp = "$1 $2"; | |
131 $data_gears =~ s/Type\*/$tmp/; | |
132 $data_gear_types->{$target} = $tmp; | |
133 } | |
134 } | |
135 } | |
136 | |
112 | 137 print $out "__code $code_ir->{name}$impl("; |
102 | 138 print $out "$data_gears) {\n\n"; |
139 | |
140 #__code next(...), __code whenEmpty(...) | |
141 my @cg = (); | |
142 while ($data_gears =~ /__code ([\w(\.)\*\s,]+?\)),?/g) { | |
143 push(@cg, $1); | |
144 } | |
145 | |
146 if (@cg) { | |
147 if (@cg == 2) { | |
148 print $out " if (:TODO:) {\n"; | |
149 print $out " goto ",shift(@cg),";\n"; | |
150 print $out " }\n"; | |
151 print $out " goto ",shift(@cg),";\n"; | |
152 } else { | |
153 print $out " goto ",shift(@cg),";\n"; | |
154 } | |
155 } | |
156 print $out "}\n\n"; | |
157 } | |
158 } |