c_type.pm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 output qw($output);
  21. sub _refresh($);
  22. sub new($)
  23. {
  24. my ($proto) = @_;
  25. my $class = ref($proto) || $proto;
  26. my $self = {};
  27. bless $self, $class;
  28. return $self;
  29. }
  30. #
  31. # Callback setters
  32. #
  33. sub set_find_align_callback($$)
  34. {
  35. my ($self, $find_align) = @_;
  36. $self->{FIND_ALIGN} = $find_align;
  37. }
  38. sub set_find_kind_callback($$)
  39. {
  40. my ($self, $find_kind) = @_;
  41. $self->{FIND_KIND} = $find_kind;
  42. }
  43. sub set_find_size_callback($$)
  44. {
  45. my ($self, $find_size) = @_;
  46. $self->{FIND_SIZE} = $find_size;
  47. }
  48. sub set_find_count_callback($$)
  49. {
  50. my ($self, $find_count) = @_;
  51. $self->{FIND_COUNT} = $find_count;
  52. }
  53. #
  54. # Property setter / getter functions (each does both)
  55. #
  56. sub kind($;$)
  57. {
  58. my ($self, $kind) = @_;
  59. if (defined $kind)
  60. {
  61. $self->{KIND} = $kind;
  62. $self->{DIRTY} = 1;
  63. }
  64. $self->_refresh() if (!defined $self->{KIND});
  65. return $self->{KIND};
  66. }
  67. sub _name($;$)
  68. {
  69. my ($self, $_name) = @_;
  70. if (defined $_name)
  71. {
  72. $self->{_NAME} = $_name;
  73. $self->{DIRTY} = 1;
  74. }
  75. return $self->{_NAME};
  76. }
  77. sub name($;$)
  78. {
  79. my ($self, $name) = @_;
  80. if (defined $name)
  81. {
  82. $self->{NAME} = $name;
  83. $self->{DIRTY} = 1;
  84. }
  85. return $self->{NAME} if ($self->{NAME});
  86. return "$self->{KIND} $self->{_NAME}";
  87. }
  88. sub pack($;$)
  89. {
  90. my ($self, $pack) = @_;
  91. if (defined $pack)
  92. {
  93. $self->{PACK} = $pack;
  94. $self->{DIRTY} = 1;
  95. }
  96. return $self->{PACK};
  97. }
  98. sub align($)
  99. {
  100. my ($self) = @_;
  101. $self->_refresh();
  102. return $self->{ALIGN};
  103. }
  104. sub fields($)
  105. {
  106. my ($self) = @_;
  107. my $count = $self->field_count;
  108. my @fields = ();
  109. for (my $n = 0; $n < $count; $n++) {
  110. my $field = 'c_type_field'->new($self, $n);
  111. push @fields, $field;
  112. }
  113. return @fields;
  114. }
  115. sub field_base_sizes($)
  116. {
  117. my ($self) = @_;
  118. $self->_refresh();
  119. return $self->{FIELD_BASE_SIZES};
  120. }
  121. sub field_aligns($)
  122. {
  123. my ($self) = @_;
  124. $self->_refresh();
  125. return $self->{FIELD_ALIGNS};
  126. }
  127. sub field_count($)
  128. {
  129. my ($self) = @_;
  130. return scalar @{$self->{FIELD_TYPE_NAMES}};
  131. }
  132. sub field_names($;$)
  133. {
  134. my ($self, $field_names) = @_;
  135. if (defined $field_names)
  136. {
  137. $self->{FIELD_NAMES} = $field_names;
  138. $self->{DIRTY} = 1;
  139. }
  140. return $self->{FIELD_NAMES};
  141. }
  142. sub field_offsets($)
  143. {
  144. my ($self) = @_;
  145. $self->_refresh();
  146. return $self->{FIELD_OFFSETS};
  147. }
  148. sub field_sizes($)
  149. {
  150. my ($self) = @_;
  151. $self->_refresh();
  152. return $self->{FIELD_SIZES};
  153. }
  154. sub field_type_names($;$)
  155. {
  156. my ($self, $field_type_names) = @_;
  157. if (defined $field_type_names)
  158. {
  159. $self->{FIELD_TYPE_NAMES} = $field_type_names;
  160. $self->{DIRTY} = 1;
  161. }
  162. return $self->{FIELD_TYPE_NAMES};
  163. }
  164. sub size($)
  165. {
  166. my ($self) = @_;
  167. $self->_refresh();
  168. return $self->{SIZE};
  169. }
  170. sub _refresh($)
  171. {
  172. my ($self) = @_;
  173. return if (!$self->{DIRTY});
  174. my $pack = $self->pack;
  175. $pack = 8 if !defined($pack);
  176. my $max_field_align = 0;
  177. my $offset = 0;
  178. my $bitfield_size = 0;
  179. my $bitfield_bits = 0;
  180. my $n = 0;
  181. foreach my $field ($self->fields())
  182. {
  183. my $type_name = $field->type_name;
  184. my $bits;
  185. my $count;
  186. if ($type_name =~ s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/)
  187. {
  188. $count = $2;
  189. $bits = $3;
  190. }
  191. my $declspec_align;
  192. if ($type_name =~ s/\s+DECLSPEC_ALIGN\((\d+)\)//)
  193. {
  194. $declspec_align=$1;
  195. }
  196. my $base_size = $self->{FIND_SIZE}($type_name);
  197. my $type_size=$base_size;
  198. if (defined $count)
  199. {
  200. $count=$self->{FIND_COUNT}($count) if ($count !~ /^\d+$/);
  201. if (!defined $count)
  202. {
  203. $type_size=undef;
  204. }
  205. else
  206. {
  207. print STDERR "$type_name -> type_size=undef, count=$count\n" if (!defined $type_size);
  208. $type_size *= int($count);
  209. }
  210. }
  211. if ($bitfield_size != 0)
  212. {
  213. if (($type_name eq "" and defined $bits and $bits == 0) or
  214. (defined $type_size and $bitfield_size != $type_size) or
  215. !defined $bits or
  216. $bitfield_bits + $bits > 8 * $bitfield_size)
  217. {
  218. # This marks the end of the previous bitfield
  219. $bitfield_size=0;
  220. $bitfield_bits=0;
  221. }
  222. else
  223. {
  224. $bitfield_bits+=$bits;
  225. $n++;
  226. next;
  227. }
  228. }
  229. $self->{ALIGN} = $self->{FIND_ALIGN}($type_name);
  230. $self->{ALIGN} = $declspec_align if (defined $declspec_align);
  231. if (defined $self->{ALIGN})
  232. {
  233. $self->{ALIGN} = $pack if ($self->{ALIGN} > $pack);
  234. $max_field_align = $self->{ALIGN} if ($self->{ALIGN}) > $max_field_align;
  235. if ($offset % $self->{ALIGN} != 0) {
  236. $offset = (int($offset / $self->{ALIGN}) + 1) * $self->{ALIGN};
  237. }
  238. }
  239. if ($self->{KIND} !~ /^(?:struct|union)$/)
  240. {
  241. $self->{KIND} = $self->{FIND_KIND}($type_name) || "";
  242. }
  243. if (!$type_size)
  244. {
  245. $self->{ALIGN} = undef;
  246. $self->{SIZE} = undef;
  247. return;
  248. }
  249. $self->{FIELD_ALIGNS}->[$n] = $self->{ALIGN};
  250. $self->{FIELD_BASE_SIZES}->[$n] = $base_size;
  251. $self->{FIELD_OFFSETS}->[$n] = $offset;
  252. $self->{FIELD_SIZES}->[$n] = $type_size;
  253. $offset += $type_size;
  254. if ($bits)
  255. {
  256. $bitfield_size=$type_size;
  257. $bitfield_bits=$bits;
  258. }
  259. $n++;
  260. }
  261. $self->{ALIGN} = $pack;
  262. $self->{ALIGN} = $max_field_align if ($max_field_align < $pack);
  263. $self->{SIZE} = $offset;
  264. if ($self->{KIND} =~ /^(?:struct|union)$/) {
  265. if ($self->{SIZE} % $self->{ALIGN} != 0) {
  266. $self->{SIZE} = (int($self->{SIZE} / $self->{ALIGN}) + 1) * $self->{ALIGN};
  267. }
  268. }
  269. $self->{DIRTY} = 0;
  270. }
  271. package c_type_field;
  272. sub new($$$)
  273. {
  274. my ($proto, $type, $number) = @_;
  275. my $class = ref($proto) || $proto;
  276. my $self = {TYPE=> $type,
  277. NUMBER => $number
  278. };
  279. bless $self, $class;
  280. return $self;
  281. }
  282. sub align($)
  283. {
  284. my ($self) = @_;
  285. return undef unless defined $self->{TYPE}->field_aligns();
  286. return $self->{TYPE}->field_aligns()->[$self->{NUMBER}];
  287. }
  288. sub base_size($)
  289. {
  290. my ($self) = @_;
  291. return undef unless defined $self->{TYPE}->field_base_sizes();
  292. return $self->{TYPE}->field_base_sizes()->[$self->{NUMBER}];
  293. }
  294. sub name($)
  295. {
  296. my ($self) = @_;
  297. return undef unless defined $self->{TYPE}->field_names();
  298. return $self->{TYPE}->field_names()->[$self->{NUMBER}];
  299. }
  300. sub offset($)
  301. {
  302. my ($self) = @_;
  303. return undef unless defined $self->{TYPE}->field_offsets();
  304. return $self->{TYPE}->field_offsets()->[$self->{NUMBER}];
  305. }
  306. sub size($)
  307. {
  308. my ($self) = @_;
  309. return undef unless defined $self->{TYPE}->field_sizes();
  310. return $self->{TYPE}->field_sizes()->[$self->{NUMBER}];
  311. }
  312. sub type_name($)
  313. {
  314. my ($self) = @_;
  315. return $self->{TYPE}->field_type_names()->[$self->{NUMBER}];
  316. }
  317. 1;