comparison src/gearsTools/lib/Gears/Context.pm @ 110:8c7c1ea49f21

impl auto gen context tools
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Wed, 27 Nov 2019 21:21:42 +0900
parents
children 239bd73abac6
comparison
equal deleted inserted replaced
109:4f9d95dc4efd 110:8c7c1ea49f21
1 package Gears::Context;
2
3 use strict;
4 use warnings;
5
6 use Gears::Util;
7 use Gears::Context::Template::XV6;
8 use Carp qw/croak/;
9
10 sub new {
11 my ($class, %args) = @_;
12 my $self = {
13 data_gears_with_count => {},
14 find_root => $args{find_root} // ".",
15 output => $args{output},
16 };
17
18 if ($args{compile_sources}) {
19 $self->{compile_sources} = $args{compile_sources};
20 map { Gears::Util->file_checking($_); } @{$self->{compile_sources}};
21 }
22
23 return bless $self, $class;
24 }
25
26
27 sub extraction_dg_compile_sources {
28 my $self = shift;
29 my %counter;
30 for my $cbc_file (@{$self->{compile_sources}}) {
31 open my $fh , '<', $cbc_file;
32 while (my $line = <$fh>) {
33 if ($line =~ /#interface\s*"(.*)\.h"/ || $line =~ /^\/\/\s*use\s*"(.*)\.h"/) {
34 $self->{data_gears_with_count}->{$1}->{caller}->{$cbc_file}++;
35 $counter{interfaces}->{$1}++;
36 next;
37 }
38
39 if ($line =~ /^(\w+)(\*)+ *create(\w+)\(([^]]*)\)/) {
40 my $interface = $1;
41 my $implementation = $3;
42 $self->{data_gears_with_count}->{$interface}->{caller}->{$cbc_file}++;
43 $self->{data_gears_with_count}->{$implementation}->{caller}->{$cbc_file}++;
44 $counter{interfaces}->{$interface}++;
45 $counter{impl}->{$implementation}++;
46 next;
47 }
48
49 if ($line =~ /Gearef\(context,\s*(\w+)\)/) {
50 my $implementation = $1;
51 $counter{impl}->{$implementation}++;
52 $self->{data_gears_with_count}->{$implementation}->{caller}->{$cbc_file}++;
53 next;
54 }
55
56 if ($line =~ /ALLOCATE_(?:PTR_)?ARRAY\(context,\s*(\w+),[\s\w]+\)/) {
57 my $implementation = $1;
58 $counter{impl}->{$implementation}++;
59 $self->{data_gears_with_count}->{$implementation}->{caller}->{$cbc_file}++;
60 next;
61 }
62
63 if ($line =~ /ALLOCATE_DATA_GEAR\((\w+),\s*(\w+)\)/) {
64 my $implementation = $2;
65 $counter{impl}->{$implementation}++;
66 $self->{data_gears_with_count}->{$implementation}->{caller}->{$cbc_file}++;
67 next;
68 }
69
70 #TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
71 if ($line =~ /\((\w+)\*\)GearImpl\(context,\s*(\w+),\s*(\w+)\)/) {
72 my $interface = $2;
73 my $implementation = $1;
74 $self->{data_gears_with_count}->{$interface}->{caller}->{$cbc_file}++;
75 $counter{interfaces}->{$interface}++;
76 $counter{impl}->{$implementation}++;
77 next;
78 }
79
80 if ($line =~ /^__code/) {
81 while ($line =~ /struct (\w+)\s*\*/g) {
82 next if $1 eq "Context";
83 $self->{data_gears_with_count}->{$1}->{caller}->{$cbc_file}++;
84 next if (exists $counter{interfaces}->{$1});
85 $counter{impl}->{$1}++;
86 }
87 }
88 }
89 close $fh;
90 }
91 $counter{interfaces}->{Meta}++;
92 $counter{interfaces}->{TaskManager}++;
93 $self->{data_gears_with_count}->{Meta}++;
94 $self->{data_gears_with_count}->{TaskManager}++;
95 return \%counter;
96 }
97
98 sub set_data_gear_header_path {
99 my $self = shift;
100 my @data_gears_name;
101 if (@_) {
102 @data_gears_name = @_;
103 } else {
104 map { push (@data_gears_name,$_) if $_ ne "Context" } keys %{$self->{data_gears_with_count}};
105 }
106 return _find_headers($self->{find_root},\@data_gears_name);
107 }
108
109 sub update_dg_each_header_path {
110 my ($self, $dgs, $dg2path) = @_;
111 my $new_dgs;
112 for my $kind (keys %$dgs) {
113 for my $dg_name (keys %{$dgs->{$kind}}) {
114 if ($dg2path->{$dg_name}) {
115 $new_dgs->{$kind}->{$dg_name} = $dg2path->{$dg_name};
116 } else {
117 croak "failed trans header $dg_name\n";
118 }
119 }
120 }
121 return $new_dgs;
122 }
123
124 sub tree2create_context_h {
125 my ($self, $dg2path) = @_;
126
127 my $data_struct_str = $dg2path ? $self->tree2data_struct_str($dg2path) : "\n";
128
129 my $output = $self->_select_output();
130 Gears::Context::Template::XV6->emit_top_header($output);
131 Gears::Context::Template::XV6->emit_data_gears($output,$data_struct_str);
132 Gears::Context::Template::XV6->emit_last_header($output);
133 close $output;
134 }
135
136 sub _select_output {
137 my $self = shift;
138 print "$self->{output}\n";
139 if ($self->{output} eq 'stdout') {
140 return *STDOUT;
141 }
142 open my $fh, '>', $self->{output};
143 return $fh;
144 }
145
146 sub tree2data_struct_str {
147 my ($self, $dg_str) = @_;
148 my $data_struct_str = "";
149 for my $interface (sort keys %$dg_str) {
150 $data_struct_str .= Gears::Util->h2context_str($dg_str->{$interface}->{elem});
151 next unless ($dg_str->{$interface}->{impl});
152 for my $impl (sort keys %{$dg_str->{$interface}->{impl}}) {
153 $data_struct_str .= Gears::Util->h2context_str($dg_str->{$interface}->{impl}->{$impl});
154 }
155 }
156 return $data_struct_str;
157 }
158
159 sub createImplTree_from_header {
160 my ($self, $dg2path) = @_;
161 my %dg_str = ();
162
163 my $inters = $dg2path->{interfaces};
164 my $impls = $dg2path->{impl};
165
166 use Data::Dumper;
167 use DDP {deparse => 1};
168 print Dumper $dg2path;
169 p $self;
170
171 map { $dg_str{$_}->{elem} = Gears::Util->parse_interface($inters->{$_}) } keys %$inters;
172
173 map {
174 my $res = Gears::Util->parse($impls->{$_});
175 if ($res->{isa}) {
176 $dg_str{$res->{isa}}->{impl}->{$_} = $res;
177 } else {
178 $dg_str{$_}->{elem} = $res;
179 }
180 } keys %$impls;
181 return \%dg_str;
182 }
183
184 sub _find_headers {
185 my ($search_bash_path, $targets) = @_;
186 my %res;
187 map { $res{$_}++ } @$targets;
188
189 my $header_paths = Gears::Util->find_headers_path($search_bash_path);
190 map {
191 /(\w+)\.h/;
192 my $header_tile = $1;
193 if (exists $res{$header_tile}){
194 $res{$header_tile} = $_;
195 }
196 } @$header_paths;
197 return \%res;
198 }
199
200 1;