insns-iflags.ph 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/usr/bin/perl
  2. ## --------------------------------------------------------------------------
  3. ##
  4. ## Copyright 1996-2018 The NASM Authors - All Rights Reserved
  5. ## See the file AUTHORS included with the NASM distribution for
  6. ## the specific copyright holders.
  7. ##
  8. ## Redistribution and use in source and binary forms, with or without
  9. ## modification, are permitted provided that the following
  10. ## conditions are met:
  11. ##
  12. ## * Redistributions of source code must retain the above copyright
  13. ## notice, this list of conditions and the following disclaimer.
  14. ## * Redistributions in binary form must reproduce the above
  15. ## copyright notice, this list of conditions and the following
  16. ## disclaimer in the documentation and/or other materials provided
  17. ## with the distribution.
  18. ##
  19. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  20. ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  21. ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  31. ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ##
  33. ## --------------------------------------------------------------------------
  34. #
  35. # Instruction template flags. These specify which processor
  36. # targets the instruction is eligible for, whether it is
  37. # privileged or undocumented, and also specify extra error
  38. # checking on the matching of the instruction.
  39. #
  40. # IF_SM stands for Size Match: any operand whose size is not
  41. # explicitly specified by the template is `really' intended to be
  42. # the same size as the first size-specified operand.
  43. # Non-specification is tolerated in the input instruction, but
  44. # _wrong_ specification is not.
  45. #
  46. # IF_SM2 invokes Size Match on only the first _two_ operands, for
  47. # three-operand instructions such as SHLD: it implies that the
  48. # first two operands must match in size, but that the third is
  49. # required to be _unspecified_.
  50. #
  51. # IF_SB invokes Size Byte: operands with unspecified size in the
  52. # template are really bytes, and so no non-byte specification in
  53. # the input instruction will be tolerated. IF_SW similarly invokes
  54. # Size Word, and IF_SD invokes Size Doubleword.
  55. #
  56. # (The default state if neither IF_SM nor IF_SM2 is specified is
  57. # that any operand with unspecified size in the template is
  58. # required to have unspecified size in the instruction too...)
  59. #
  60. # iflag_t is defined to store these flags.
  61. #
  62. # The order does matter here. We use some predefined masks to quick test
  63. # for a set of flags, so be careful moving bits (and
  64. # don't forget to update C code generation then).
  65. #
  66. sub dword_align($) {
  67. my($n) = @_;
  68. $$n = ($$n + 31) & ~31;
  69. return $n;
  70. }
  71. my $f = 0;
  72. my %insns_flag_bit = (
  73. #
  74. # dword bound, index 0 - specific flags
  75. #
  76. "SM" => [$f++, "Size match"],
  77. "SM2" => [$f++, "Size match first two operands"],
  78. "SB" => [$f++, "Unsized operands can't be non-byte"],
  79. "SW" => [$f++, "Unsized operands can't be non-word"],
  80. "SD" => [$f++, "Unsized operands can't be non-dword"],
  81. "SQ" => [$f++, "Unsized operands can't be non-qword"],
  82. "SO" => [$f++, "Unsized operands can't be non-oword"],
  83. "SY" => [$f++, "Unsized operands can't be non-yword"],
  84. "SZ" => [$f++, "Unsized operands can't be non-zword"],
  85. "SIZE" => [$f++, "Unsized operands must match the bitsize"],
  86. "SX" => [$f++, "Unsized operands not allowed"],
  87. "AR0" => [$f++, "SB, SW, SD applies to argument 0"],
  88. "AR1" => [$f++, "SB, SW, SD applies to argument 1"],
  89. "AR2" => [$f++, "SB, SW, SD applies to argument 2"],
  90. "AR3" => [$f++, "SB, SW, SD applies to argument 3"],
  91. "AR4" => [$f++, "SB, SW, SD applies to argument 4"],
  92. "OPT" => [$f++, "Optimizing assembly only"],
  93. #
  94. # dword bound - instruction filtering flags
  95. #
  96. "PRIV" => [${dword_align(\$f)}++, "Privileged instruction"],
  97. "SMM" => [$f++, "Only valid in SMM"],
  98. "PROT" => [$f++, "Protected mode only"],
  99. "LOCK" => [$f++, "Lockable if operand 0 is memory"],
  100. "NOLONG" => [$f++, "Not available in long mode"],
  101. "LONG" => [$f++, "Long mode"],
  102. "NOHLE" => [$f++, "HLE prefixes forbidden"],
  103. "MIB" => [$f++, "disassemble with split EA"],
  104. "BND" => [$f++, "BND (0xF2) prefix available"],
  105. "UNDOC" => [$f++, "Undocumented"],
  106. "HLE" => [$f++, "HLE prefixed"],
  107. "FPU" => [$f++, "FPU"],
  108. "MMX" => [$f++, "MMX"],
  109. "3DNOW" => [$f++, "3DNow!"],
  110. "SSE" => [$f++, "SSE (KNI, MMX2)"],
  111. "SSE2" => [$f++, "SSE2"],
  112. "SSE3" => [$f++, "SSE3 (PNI)"],
  113. "VMX" => [$f++, "VMX"],
  114. "SSSE3" => [$f++, "SSSE3"],
  115. "SSE4A" => [$f++, "AMD SSE4a"],
  116. "SSE41" => [$f++, "SSE4.1"],
  117. "SSE42" => [$f++, "SSE4.2"],
  118. "SSE5" => [$f++, "SSE5"],
  119. "AVX" => [$f++, "AVX (256-bit floating point)"],
  120. "AVX2" => [$f++, "AVX2 (256-bit integer)"],
  121. "FMA" => [$f++, ""],
  122. "BMI1" => [$f++, ""],
  123. "BMI2" => [$f++, ""],
  124. "TBM" => [$f++, ""],
  125. "RTM" => [$f++, ""],
  126. "INVPCID" => [$f++, ""],
  127. "AVX512" => [$f++, "AVX-512F (512-bit base architecture)"],
  128. "AVX512CD" => [$f++, "AVX-512 Conflict Detection"],
  129. "AVX512ER" => [$f++, "AVX-512 Exponential and Reciprocal"],
  130. "AVX512PF" => [$f++, "AVX-512 Prefetch"],
  131. "MPX" => [$f++, "MPX"],
  132. "SHA" => [$f++, "SHA"],
  133. "PREFETCHWT1" => [$f++, "PREFETCHWT1"],
  134. "AVX512VL" => [$f++, "AVX-512 Vector Length Orthogonality"],
  135. "AVX512DQ" => [$f++, "AVX-512 Dword and Qword"],
  136. "AVX512BW" => [$f++, "AVX-512 Byte and Word"],
  137. "AVX512IFMA" => [$f++, "AVX-512 IFMA instructions"],
  138. "AVX512VBMI" => [$f++, "AVX-512 VBMI instructions"],
  139. "AES" => [$f++, "AES instructions"],
  140. "VAES" => [$f++, "AES AVX instructions"],
  141. "VPCLMULQDQ" => [$f++, "AVX Carryless Multiplication"],
  142. "GFNI" => [$f++, "Galois Field instructions"],
  143. "AVX512VBMI2" => [$f++, "AVX-512 VBMI2 instructions"],
  144. "AVX512VNNI" => [$f++, "AVX-512 VNNI instructions"],
  145. "AVX512BITALG" => [$f++, "AVX-512 Bit Algorithm instructions"],
  146. "AVX512VPOPCNTDQ" => [$f++, "AVX-512 VPOPCNTD/VPOPCNTQ"],
  147. "AVX5124FMAPS" => [$f++, "AVX-512 4-iteration multiply-add"],
  148. "AVX5124VNNIW" => [$f++, "AVX-512 4-iteration dot product"],
  149. "SGX" => [$f++, "Intel Software Guard Extensions (SGX)"],
  150. # Put these last
  151. "OBSOLETE" => [$f++, "Instruction removed from architecture"],
  152. "VEX" => [$f++, "VEX or XOP encoded instruction"],
  153. "EVEX" => [$f++, "EVEX encoded instruction"],
  154. #
  155. # dword bound - cpu type flags
  156. #
  157. # The CYRIX and AMD flags should have the highest bit values; the
  158. # disassembler selection algorithm depends on it.
  159. #
  160. "8086" => [${dword_align(\$f)}++, "8086"],
  161. "186" => [$f++, "186+"],
  162. "286" => [$f++, "286+"],
  163. "386" => [$f++, "386+"],
  164. "486" => [$f++, "486+"],
  165. "PENT" => [$f++, "Pentium"],
  166. "P6" => [$f++, "P6"],
  167. "KATMAI" => [$f++, "Katmai"],
  168. "WILLAMETTE" => [$f++, "Willamette"],
  169. "PRESCOTT" => [$f++, "Prescott"],
  170. "X86_64" => [$f++, "x86-64 (long or legacy mode)"],
  171. "NEHALEM" => [$f++, "Nehalem"],
  172. "WESTMERE" => [$f++, "Westmere"],
  173. "SANDYBRIDGE" => [$f++, "Sandy Bridge"],
  174. "FUTURE" => [$f++, "Future processor (not yet disclosed)"],
  175. "IA64" => [$f++, "IA64 (in x86 mode)"],
  176. # Put these last
  177. "CYRIX" => [$f++, "Cyrix-specific"],
  178. "AMD" => [$f++, "AMD-specific"],
  179. );
  180. my %insns_flag_hash = ();
  181. my @insns_flag_values = ();
  182. my $iflag_words;
  183. sub get_flag_words() {
  184. my $max = -1;
  185. foreach my $vp (values(%insns_flag_bit)) {
  186. if ($vp->[0] > $max) {
  187. $max = $vp->[0];
  188. }
  189. }
  190. return int($max/32)+1;
  191. }
  192. sub insns_flag_index(@) {
  193. return undef if $_[0] eq "ignore";
  194. my @prekey = sort(@_);
  195. my $key = join("", @prekey);
  196. if (not defined($insns_flag_hash{$key})) {
  197. my @newkey = (0) x $iflag_words;
  198. for my $i (@prekey) {
  199. die "No key for $i\n" if not defined($insns_flag_bit{$i});
  200. $newkey[$insns_flag_bit{$i}[0]/32] |=
  201. (1 << ($insns_flag_bit{$i}[0] % 32));
  202. }
  203. my $str = join(',', map { sprintf("UINT32_C(0x%08x)",$_) } @newkey);
  204. push @insns_flag_values, $str;
  205. $insns_flag_hash{$key} = $#insns_flag_values;
  206. }
  207. return $insns_flag_hash{$key};
  208. }
  209. sub write_iflaggen_h() {
  210. print STDERR "Writing $oname...\n";
  211. open(N, '>', $oname) or die "$0: $!\n";
  212. print N "/* This file is auto-generated. Don't edit. */\n";
  213. print N "#ifndef NASM_IFLAGGEN_H\n";
  214. print N "#define NASM_IFLAGGEN_H 1\n\n";
  215. my @flagnames = keys(%insns_flag_bit);
  216. @flagnames = sort {
  217. $insns_flag_bit{$a}->[0] <=> $insns_flag_bit{$b}->[0]
  218. } @flagnames;
  219. my $next = 0;
  220. foreach my $key (@flagnames) {
  221. my $v = $insns_flag_bit{$key};
  222. if ($v->[0] > $next) {
  223. printf N "%-31s /* %-64s */\n", '',
  224. ($next != $v->[0]-1) ?
  225. sprintf("%d...%d unused", $next, $v->[0]-1) :
  226. sprintf("%d unused", $next);
  227. }
  228. print N sprintf("#define IF_%-16s %3d /* %-64s */\n",
  229. $key, $v->[0], $v->[1]);
  230. $next = $v->[0] + 1;
  231. }
  232. print N "\n";
  233. printf N "#define IF_FIELD_COUNT %d\n", $iflag_words;
  234. print N "typedef struct {\n";
  235. print N " uint32_t field[IF_FIELD_COUNT];\n";
  236. print N "} iflag_t;\n";
  237. print N "\n";
  238. printf N "extern const iflag_t insns_flags[%d];\n\n",
  239. $#insns_flag_values + 1;
  240. print N "#endif /* NASM_IFLAGGEN_H */\n";
  241. close N;
  242. }
  243. sub write_iflag_c() {
  244. print STDERR "Writing $oname...\n";
  245. open(N, '>', $oname) or die "$0: $!\n";
  246. print N "/* This file is auto-generated. Don't edit. */\n";
  247. print N "#include \"iflag.h\"\n\n";
  248. print N "/* Global flags referenced from instruction templates */\n";
  249. printf N "const iflag_t insns_flags[%d] = {\n",
  250. $#insns_flag_values + 1;
  251. foreach my $i (0 .. $#insns_flag_values) {
  252. print N sprintf(" /* %4d */ {{ %s }},\n", $i, $insns_flag_values[$i]);
  253. }
  254. print N "};\n\n";
  255. close N;
  256. }
  257. $iflag_words = get_flag_words();
  258. 1;