Mercurial > hg > CbC > CbC_xv6
annotate src/gearsTools/trans_impl.pl @ 122:f6558602f31e
tweak
author | anatofuz |
---|---|
date | Mon, 02 Dec 2019 19:21:20 +0900 |
parents | b9df8ea87b42 |
children | 004e825f37c7 53be0626c3fa |
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/; | |
26 open my $fh, '>', $output_file; | |
27 my $stdout = $fh; | |
28 | |
29 unless ($opt{w}) { | |
30 $stdout = *STDOUT; | |
31 } | |
32 | |
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); | |
37 close $fh; | |
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 } | |
85 } | |
86 | |
109
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
87 for my $datum (@inter_data) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
88 if ($datum =~ /\w+ \w+\* (\w+)/) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
89 print $out " ${instance_inter}->$1 = NULL;\n"; |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
90 next; |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
91 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
92 if ($datum =~ /\w+ \w+ (\w+)/) { |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
93 print $out " ${instance_inter}->$1 = 0;\n"; |
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 } |
4f9d95dc4efd
fix createInstance
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
105
diff
changeset
|
96 |
102 | 97 for my $code (@{$inter_ir->{codes}}) { |
112 | 98 my $code_gear = $code->{name}; |
102 | 99 print $out " ${instance_inter}->$code_gear = C_$code_gear$impl_ir->{name};\n" |
100 } | |
101 | |
102 print $out " return $instance_inter;\n"; | |
103 print $out "}\n"; | |
104 } | |
105 | |
106 | |
107 sub emit_code_gears { | |
108 my ($out, $impl_ir, $inter_ir) = @_; | |
109 my $impl = $impl_ir->{name}; | |
110 | |
111 my @inter_data = @{$inter_ir->{data}}; | |
112 my $instance_inter = shift @inter_data; | |
113 if ($instance_inter =~ /union\s*Data\*\s*(\w+)/) { | |
114 $instance_inter = $1; | |
115 } | |
116 my $instance_impl = lcfirst $impl_ir->{name}; | |
105 | 117 $instance_impl =~ s/([A-Z])/_\l$1/g; |
102 | 118 my $data_gear_types = {}; |
119 | |
120 for my $code_ir (@{$inter_ir->{codes}}) { | |
112 | 121 my $data_gears = $code_ir->{args}; |
122 $data_gears =~ s/Impl/struct $impl/g; | |
103 | 123 while ($data_gears =~ /Type\*\s*(\w+),/g) { |
102 | 124 my $target = $1; |
125 if (exists $data_gear_types->{$target}){ | |
126 $data_gears =~ s/Type\*/$data_gear_types->{$target}/; | |
127 } else { | |
128 my $td = ""; | |
129 map { $td = $_ if ($_ =~ /$target/) } @inter_data; | |
130 if ($td =~ /(\w+)\s*([\w\*]+)\s*(\w+)/) { | |
131 my $tmp = "$1 $2"; | |
132 $data_gears =~ s/Type\*/$tmp/; | |
133 $data_gear_types->{$target} = $tmp; | |
134 } | |
135 } | |
136 } | |
137 | |
112 | 138 print $out "__code $code_ir->{name}$impl("; |
102 | 139 print $out "$data_gears) {\n\n"; |
140 | |
141 #__code next(...), __code whenEmpty(...) | |
142 my @cg = (); | |
143 while ($data_gears =~ /__code ([\w(\.)\*\s,]+?\)),?/g) { | |
144 push(@cg, $1); | |
145 } | |
146 | |
147 if (@cg) { | |
148 if (@cg == 2) { | |
149 print $out " if (:TODO:) {\n"; | |
150 print $out " goto ",shift(@cg),";\n"; | |
151 print $out " }\n"; | |
152 print $out " goto ",shift(@cg),";\n"; | |
153 } else { | |
154 print $out " goto ",shift(@cg),";\n"; | |
155 } | |
156 } | |
157 print $out "}\n\n"; | |
158 } | |
159 } |