c_type.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #
  2. # Copyright 2002 Patrik Stridvall
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. #
  18. package c_type;
  19. use strict;
  20. use warnings 'all';
  21. use output qw($output);
  22. sub _refresh($);
  23. sub new($)
  24. {
  25. my ($proto) = @_;
  26. my $class = ref($proto) || $proto;
  27. my $self = {};
  28. bless $self, $class;
  29. return $self;
  30. }
  31. #
  32. # Callback setters
  33. #
  34. sub set_find_align_callback($$)
  35. {
  36. my ($self, $find_align) = @_;
  37. $self->{FIND_ALIGN} = $find_align;
  38. }
  39. sub set_find_kind_callback($$)
  40. {
  41. my ($self, $find_kind) = @_;
  42. $self->{FIND_KIND} = $find_kind;
  43. }
  44. sub set_find_size_callback($$)
  45. {
  46. my ($self, $find_size) = @_;
  47. $self->{FIND_SIZE} = $find_size;
  48. }
  49. sub set_find_count_callback($$)
  50. {
  51. my ($self, $find_count) = @_;
  52. $self->{FIND_COUNT} = $find_count;
  53. }
  54. #
  55. # Property setter / getter functions (each does both)
  56. #
  57. sub kind($;$)
  58. {
  59. my ($self, $kind) = @_;
  60. if (defined $kind)
  61. {
  62. $self->{KIND} = $kind;
  63. $self->{DIRTY} = 1;
  64. }
  65. $self->_refresh() if (!defined $self->{KIND});
  66. return $self->{KIND};
  67. }
  68. sub _name($;$)
  69. {
  70. my ($self, $_name) = @_;
  71. if (defined $_name)
  72. {
  73. $self->{_NAME} = $_name;
  74. $self->{DIRTY} = 1;
  75. }
  76. return $self->{_NAME};
  77. }
  78. sub name($;$)
  79. {
  80. my ($self, $name) = @_;
  81. if (defined $name)
  82. {
  83. $self->{NAME} = $name;
  84. $self->{DIRTY} = 1;
  85. }
  86. return $self->{NAME} if ($self->{NAME});
  87. return "$self->{KIND} $self->{_NAME}";
  88. }
  89. sub pack($;$)
  90. {
  91. my ($self, $pack) = @_;
  92. if (defined $pack)
  93. {
  94. $self->{PACK} = $pack;
  95. $self->{DIRTY} = 1;
  96. }
  97. return $self->{PACK};
  98. }
  99. sub align($)
  100. {
  101. my ($self) = @_;
  102. $self->_refresh();
  103. return $self->{ALIGN};
  104. }
  105. sub fields($)
  106. {
  107. my ($self) = @_;
  108. my $count = $self->field_count;
  109. my @fields = ();
  110. for (my $n = 0; $n < $count; $n++) {
  111. my $field = 'c_type_field'->new($self, $n);
  112. push @fields, $field;
  113. }
  114. return @fields;
  115. }
  116. sub field_base_sizes($)
  117. {
  118. my ($self) = @_;
  119. $self->_refresh();
  120. return $self->{FIELD_BASE_SIZES};
  121. }
  122. sub field_aligns($)
  123. {
  124. my ($self) = @_;
  125. $self->_refresh();
  126. return $self->{FIELD_ALIGNS};
  127. }
  128. sub field_count($)
  129. {
  130. my ($self) = @_;
  131. return scalar @{$self->{FIELD_TYPE_NAMES}};
  132. }
  133. sub field_names($;$)
  134. {
  135. my ($self, $field_names) = @_;
  136. if (defined $field_names)
  137. {
  138. $self->{FIELD_NAMES} = $field_names;
  139. $self->{DIRTY} = 1;
  140. }
  141. return $self->{FIELD_NAMES};
  142. }
  143. sub field_offsets($)
  144. {
  145. my ($self) = @_;
  146. $self->_refresh();
  147. return $self->{FIELD_OFFSETS};
  148. }
  149. sub field_sizes($)
  150. {
  151. my ($self) = @_;
  152. $self->_refresh();
  153. return $self->{FIELD_SIZES};
  154. }
  155. sub field_type_names($;$)
  156. {
  157. my ($self, $field_type_names) = @_;
  158. if (defined $field_type_names)
  159. {
  160. $self->{FIELD_TYPE_NAMES} = $field_type_names;
  161. $self->{DIRTY} = 1;
  162. }
  163. return $self->{FIELD_TYPE_NAMES};
  164. }
  165. sub size($)
  166. {
  167. my ($self) = @_;
  168. $self->_refresh();
  169. return $self->{SIZE};
  170. }
  171. sub _refresh($)
  172. {
  173. my ($self) = @_;
  174. return if (!$self->{DIRTY});
  175. my $pack = $self->pack;
  176. $pack = 8 if !defined($pack);
  177. my $max_field_align = 0;
  178. my $offset = 0;
  179. my $bitfield_size = 0;
  180. my $bitfield_bits = 0;
  181. my $n = 0;
  182. foreach my $field ($self->fields())
  183. {
  184. my $type_name = $field->type_name;
  185. my $bits;
  186. my $count;
  187. if ($type_name =~ s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/)
  188. {
  189. $count = $2;
  190. $bits = $3;
  191. }
  192. my $declspec_align;
  193. if ($type_name =~ s/\s+DECLSPEC_ALIGN\((\d+)\)//)
  194. {
  195. $declspec_align=$1;
  196. }
  197. my $base_size = $self->{FIND_SIZE}($type_name);
  198. my $type_size=$base_size;
  199. if (defined $count)
  200. {
  201. $count=$self->{FIND_COUNT}($count) if ($count !~ /^\d+$/);
  202. if (!defined $count)
  203. {
  204. $type_size=undef;
  205. }
  206. else
  207. {
  208. if (!defined $type_size)
  209. {
  210. print STDERR "$type_name -> type_size=undef, count=$count\n";
  211. }
  212. else
  213. {
  214. $type_size *= int($count);
  215. }
  216. }
  217. }
  218. if ($bitfield_size != 0)
  219. {
  220. if (($type_name eq "" and defined $bits and $bits == 0) or
  221. (defined $type_size and $bitfield_size != $type_size) or
  222. !defined $bits or
  223. $bitfield_bits + $bits > 8 * $bitfield_size)
  224. {
  225. # This marks the end of the previous bitfield
  226. $bitfield_size=0;
  227. $bitfield_bits=0;
  228. }
  229. else
  230. {
  231. $bitfield_bits+=$bits;
  232. $n++;
  233. next;
  234. }
  235. }
  236. $self->{ALIGN} = $self->{FIND_ALIGN}($type_name);
  237. $self->{ALIGN} = $declspec_align if (defined $declspec_align);
  238. if (defined $self->{ALIGN})
  239. {
  240. $self->{ALIGN} = $pack if ($self->{ALIGN} > $pack);
  241. $max_field_align = $self->{ALIGN} if ($self->{ALIGN}) > $max_field_align;
  242. if ($offset % $self->{ALIGN} != 0) {
  243. $offset = (int($offset / $self->{ALIGN}) + 1) * $self->{ALIGN};
  244. }
  245. }
  246. if ($self->{KIND} !~ /^(?:struct|union)$/)
  247. {
  248. $self->{KIND} = $self->{FIND_KIND}($type_name) || "";
  249. }
  250. if (!$type_size)
  251. {
  252. $self->{ALIGN} = undef;
  253. $self->{SIZE} = undef;
  254. return;
  255. }
  256. $self->{FIELD_ALIGNS}->[$n] = $self->{ALIGN};
  257. $self->{FIELD_BASE_SIZES}->[$n] = $base_size;
  258. $self->{FIELD_OFFSETS}->[$n] = $offset;
  259. $self->{FIELD_SIZES}->[$n] = $type_size;
  260. $offset += $type_size;
  261. if ($bits)
  262. {
  263. $bitfield_size=$type_size;
  264. $bitfield_bits=$bits;
  265. }
  266. $n++;
  267. }
  268. $self->{ALIGN} = $pack;
  269. $self->{ALIGN} = $max_field_align if ($max_field_align < $pack);
  270. $self->{SIZE} = $offset;
  271. if ($self->{KIND} =~ /^(?:struct|union)$/) {
  272. if ($self->{SIZE} % $self->{ALIGN} != 0) {
  273. $self->{SIZE} = (int($self->{SIZE} / $self->{ALIGN}) + 1) * $self->{ALIGN};
  274. }
  275. }
  276. $self->{DIRTY} = 0;
  277. }
  278. package c_type_field;
  279. sub new($$$)
  280. {
  281. my ($proto, $type, $number) = @_;
  282. my $class = ref($proto) || $proto;
  283. my $self = {TYPE=> $type,
  284. NUMBER => $number
  285. };
  286. bless $self, $class;
  287. return $self;
  288. }
  289. sub align($)
  290. {
  291. my ($self) = @_;
  292. return undef unless defined $self->{TYPE}->field_aligns();
  293. return $self->{TYPE}->field_aligns()->[$self->{NUMBER}];
  294. }
  295. sub base_size($)
  296. {
  297. my ($self) = @_;
  298. return undef unless defined $self->{TYPE}->field_base_sizes();
  299. return $self->{TYPE}->field_base_sizes()->[$self->{NUMBER}];
  300. }
  301. sub name($)
  302. {
  303. my ($self) = @_;
  304. return undef unless defined $self->{TYPE}->field_names();
  305. return $self->{TYPE}->field_names()->[$self->{NUMBER}];
  306. }
  307. sub offset($)
  308. {
  309. my ($self) = @_;
  310. return undef unless defined $self->{TYPE}->field_offsets();
  311. return $self->{TYPE}->field_offsets()->[$self->{NUMBER}];
  312. }
  313. sub size($)
  314. {
  315. my ($self) = @_;
  316. return undef unless defined $self->{TYPE}->field_sizes();
  317. return $self->{TYPE}->field_sizes()->[$self->{NUMBER}];
  318. }
  319. sub type_name($)
  320. {
  321. my ($self) = @_;
  322. return $self->{TYPE}->field_type_names()->[$self->{NUMBER}];
  323. }
  324. 1;