| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Preprocess the instruction pattern file. |
| 4 |
# |
| 5 |
# Generate some common repeated patterns here. |
| 6 |
# |
| 7 |
# Tag instructions which set the flags, and tag instructions |
| 8 |
# which already do upper-zeroing. |
| 9 |
# |
| 10 |
# This can also be used to create other implied flags. |
| 11 |
# |
| 12 |
|
| 13 |
use integer; |
| 14 |
use strict; |
| 15 |
|
| 16 |
require 'x86/insns-iflags.ph'; |
| 17 |
|
| 18 |
our %macros; |
| 19 |
our($macro, $outfile, $infile, $line); # Public for error messages |
| 20 |
|
| 21 |
# Common pattern for the basic 7 arithmetric functions |
| 22 |
$macros{'arith'} = { |
| 23 |
'def' => *def_eightfold, |
| 24 |
'txt' => <<'EOL' |
| 25 |
$$bwdq $op rm#,reg# [mr: $hle o# $00# /r ] 8086,FL,SM,$lock |
| 26 |
$$bwdq $op reg#,rm# [rm: o# $02# /r ] 8086,FL,SM |
| 27 |
$$wdq $op rm#,sbyte# [mi: $hle o# 83 /$n ib,s ] 8086,FL,SM,$lock |
| 28 |
$$bwdq $op ax#,imm# [-i: o# $04# i# ] 8086,FL,SM |
| 29 |
$$bwdq $op rm#,imm# [mi: $hle o# 80# /$n i# ] 8086,FL,SM,$lock |
| 30 |
$$bwdq $op reg#?,reg#,rm# [vrm: evex.ndx.$nf.l0.m4.o# $02# /r ] $evex,FL,APX,SM |
| 31 |
$$bwdq $op reg#?,rm#,reg# [vmr: evex.ndx.$nf.l0.m4.o# $00# /r ] $evex,FL,APX,SM |
| 32 |
$$wdq $op reg#?,rm#,sbyte# [vmi: evex.ndx.$nf.l0.m4.o# 83 /$n ib,s ] $evex,FL,APX,SM |
| 33 |
$$bwdq $op reg#?,rm#,imm# [vmi: evex.ndx.$nf.l0.m4.o# 80# /$n i# ] $evex,FL,APX,SM |
| 34 |
EOL |
| 35 |
}; |
| 36 |
|
| 37 |
# Common pattern for the basic shift and rotate instructions |
| 38 |
# Separate legacy and EVEX versions because additional patterns are |
| 39 |
# needed to handle the -X VEX versions |
| 40 |
$macros{'shift'} = { |
| 41 |
'def' => *def_eightfold, |
| 42 |
'txt' => <<'EOL' |
| 43 |
$$bwdq $op rm#,unity [m-: o# d0# /$n] ] 8086,FL |
| 44 |
$$bwdq $op rm#,reg_cl [m-: o# d2# /$n] ] 8086,FL |
| 45 |
$$bwdq $op rm#,reg_cx [m-: o# d2# /$n] ] 8086,FL,ND |
| 46 |
$$bwdq $op rm#,reg_ecx [m-: o# d2# /$n] ] 8086,FL,ND |
| 47 |
$$bwdq $op rm#,reg_rcx [m-: o# d2# /$n] ] 8086,FL,ND |
| 48 |
$$bwdq $op rm#,imm8 [mi: o# c0# /$n ib,u] ] 186,FL |
| 49 |
EOL |
| 50 |
}; |
| 51 |
|
| 52 |
# APX EVEX versions |
| 53 |
$macros{'eshift'} = { |
| 54 |
'def' => *def_eightfold, |
| 55 |
'txt' => <<'EOL' |
| 56 |
$$bwdq $op reg#?,rm#,unity [vm-: evex.ndx.nf.l0.m4.o# d0# /$n ] $apx,FL,SM0-1 |
| 57 |
$$bwdq $op reg#?,rm#,reg_cl [vm-: evex.ndx.nf.l0.m4.o# d2# /$n ] $apx,FL,SM0-1 |
| 58 |
$$bwdq $op reg#?,rm#,reg_cx [vm-: evex.ndx.nf.l0.m4.o# d2# /$n ] $apx,FL,SM0-1,ND |
| 59 |
$$bwdq $op reg#?,rm#,reg_ecx [vm-: evex.ndx.nf.l0.m4.o# d2# /$n ] $apx,FL,SM0-1,ND |
| 60 |
$$bwdq $op reg#?,rm#,reg_rcx [vm-: evex.ndx.nf.l0.m4.o# d2# /$n ] $apx,FL,SM0-1,ND |
| 61 |
$$bwdq $op reg#?,rm#,imm8 [vmi: evex.ndx.nf.l0.m4.o# c0# /$n ib,u ] $apx,FL,SM0-1 |
| 62 |
EOL |
| 63 |
}; |
| 64 |
|
| 65 |
# -X shifts |
| 66 |
$macros{'xshift'} = { |
| 67 |
'func' => sub { |
| 68 |
my($mac, $args, $rawargs) = @_; |
| 69 |
my @ol; |
| 70 |
my $vex = 'vex'; |
| 71 |
my $vfl = ''; |
| 72 |
if (grep { /^evex=1$/ } @$rawargs) { |
| 73 |
$vex = 'evex'; |
| 74 |
$vfl = 'APX'; |
| 75 |
} |
| 76 |
foreach my $xf (['X',"$vfl"], ['', "$vfl,ND,NF!,OPT"]) { |
| 77 |
my($x,$fl) = @$xf; |
| 78 |
foreach my $os (32, 64) { |
| 79 |
my $w = ($os eq 32) ? 'w0' : 'w1'; |
| 80 |
my $ixor = sprintf('%02x', $os-1); |
| 81 |
push(@ol, "ROR$x reg$os,rm$os,imm8 [rmi: $vex.lz.f2.0f3a.$w f0 /r ib] BMI2,SM0-1,!FL,$fl"); |
| 82 |
push(@ol, "ROL$x reg$os,rm$os,imm_known8 [rmi: $vex.lz.f2.0f3a.$w f0 /r ib^$ixor] BMI2,SM0-1,!FL,$fl"); |
| 83 |
foreach my $ss (8, 16, 32, 64) { |
| 84 |
foreach my $opp (['SHL','66'], ['SAL','66'], ['SAR','f3'], ['SHR','f2']) { |
| 85 |
my($op,$pp) = @$opp; |
| 86 |
my $ndss = ',ND' unless ($ss == $os && $op ne 'SAL'); |
| 87 |
push(@ol, "$op$x reg$os,rm${os}*,reg$ss [rmv: $vex.lz.$pp.0f38.$w f7 /r] BMI2,SM0-1,!FL,$fl,$ndss"); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
return @ol; |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
# |
| 97 |
# Common pattern for multiple 32/64, 16/32/64, or 8/16/32/64 instructions. |
| 98 |
# 'z' is used for a null-prefixed default-sized instruction (osm/osd) |
| 99 |
# |
| 100 |
my @sizename = ('z', 'b', 'w', 'd', 'q'); |
| 101 |
|
| 102 |
for (my $i = 1; $i <= 31; $i++) { |
| 103 |
my $n; |
| 104 |
for (my $j = 0; $j < scalar @sizename; $j++) { |
| 105 |
$n .= $sizename[$j] if ($i & (1 << $j)); |
| 106 |
} |
| 107 |
$macros{$n} = { 'func' => *func_multisize, 'mask' => $i }; |
| 108 |
} |
| 109 |
|
| 110 |
sub func_multisize($$$) { |
| 111 |
my($mac, $args, $rawargs) = @_; |
| 112 |
my @sbyte = ('imm8', 'imm8', 'sbyteword16', 'sbytedword32', 'sbytedword64'); |
| 113 |
my $long = 0; # 1 for LONG, 2 for NOLONG, 3 for invalid |
| 114 |
my $wwflag = 0; |
| 115 |
|
| 116 |
my @ol; |
| 117 |
my $mask = $mac->{'mask'}; |
| 118 |
|
| 119 |
for (my $i = 0; $i < scalar(@sizename); $i++) { |
| 120 |
next unless ($mask & (1 << $i)); |
| 121 |
my $s = ($i > 0) ? 4 << $i : ''; |
| 122 |
my $sn = $sizename[$i]; |
| 123 |
my $sz = $s || 'sm'; |
| 124 |
my $o; |
| 125 |
my $ins = join(' ', @$rawargs); |
| 126 |
my $nd = 0; |
| 127 |
|
| 128 |
# Conditional pattern inclusions |
| 129 |
# Syntax: (which1:text1/which2:text2/text3) |
| 130 |
# ... where "which" is a combination of sizename letters. |
| 131 |
# No colon means unconditional ("else" clause) |
| 132 |
while ($ins =~ /^(.*?)\(([^\)]+)\)(.*)$/) { |
| 133 |
$o .= $1; |
| 134 |
my @cpp = split(/\//, $2); |
| 135 |
$ins = $3; |
| 136 |
|
| 137 |
my $found; |
| 138 |
foreach my $cp (@cpp) { |
| 139 |
if ($cp =~ /^([a-z]+)\:(.*)$/) { |
| 140 |
$cp = $2; |
| 141 |
$found = $1 =~ /$sn/; |
| 142 |
} else { |
| 143 |
$found = 1; |
| 144 |
} |
| 145 |
if ($found) { |
| 146 |
$o .= $cp; |
| 147 |
last; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$ins = $o.$ins; |
| 153 |
$o = ''; |
| 154 |
|
| 155 |
while ($ins =~ /^(.*?)((?:\b[0-9a-f]{2}(?:\+r)?|\bsbyte|\bimm|\bsel|\bopt\w?|\b[ioa]d?|\b(?:reg_)?[abcd]x|\bk?reg|\bk?rm|\bw|\bS\b)?\#{1,2}|\b(?:reg|rm)64\b|\b(?:o64)?nw\b|\b(?:NO)?LONG\w+\b|\%{1,2}|[ABCD]X\#)(.*)$/) { |
| 156 |
$o .= $1; |
| 157 |
my $mw = $2; |
| 158 |
$ins = $3; |
| 159 |
if ($mw eq '%') { |
| 160 |
$o .= uc($sn) if ($i); |
| 161 |
} elsif ($mw eq '%%') { |
| 162 |
if ($i < 2) { |
| 163 |
die "$0:$infile:$line: $mw cannot be used with z|b\n"; |
| 164 |
} |
| 165 |
$o .= uc($sizename[$i-1]) . uc($sn); |
| 166 |
} elsif ($mw =~ /^([0-9a-f]{2})(\+r)?(\#)?\#$/) { |
| 167 |
my $n; |
| 168 |
$n = ($3 ne '') ? $s > 16 : $s > 8; |
| 169 |
$n <<= 3 if ($2 ne ''); |
| 170 |
$o .= sprintf('%02x%s', hex($1) | $n, $2); |
| 171 |
} elsif ($mw eq 'sbyte#') { |
| 172 |
$o .= $sbyte[$i]; |
| 173 |
} elsif ($mw =~ /^imm\#(\#?)$/) { |
| 174 |
$o .= (($1 eq '' && $s >= 64) ? 'sdword' : 'imm').$s; |
| 175 |
} elsif ($mw =~ '^([ao])(d?)\#$') { |
| 176 |
$o .= $1.("$2$sz" eq 'dsm' ? 'df' : $sz); |
| 177 |
} elsif ($mw eq 'i#') { |
| 178 |
$o .= !$i ? 'iwd' : ($s >= 64) ? 'id,s' : "i$sn"; |
| 179 |
} elsif ($mw eq 'i##') { |
| 180 |
$o .= !$i ? 'iwdq' : "i$sn"; |
| 181 |
} elsif ($mw =~ /^(?:reg_)?([abcd])x\#$/i) { |
| 182 |
my $rl = $1; |
| 183 |
my $upr = ($rl =~ /^[A-Z]/); |
| 184 |
if ($i == 1) { |
| 185 |
$o .= $upr ? "${rl}L" : "reg_${rl}l"; |
| 186 |
} elsif ($i == 2) { |
| 187 |
$o .= $upr ? "${rl}X" : "reg_${rl}x"; |
| 188 |
} elsif ($i == 3) { |
| 189 |
$o .= $upr ? "E${rl}X" : "reg_e${rl}x"; |
| 190 |
} elsif ($i == 4) { |
| 191 |
$o .= $upr ? "R${rl}X" : "reg_r${rl}x"; |
| 192 |
$long |= 1; |
| 193 |
} else { |
| 194 |
die "$0:$infile:$line: register cannot be used with z\n"; |
| 195 |
} |
| 196 |
} elsif ($mw eq 'sel#') { |
| 197 |
# z is (ab)used to mean the memory form of a segment selector |
| 198 |
if ($i == 0) { |
| 199 |
$o .= 'mem16'; |
| 200 |
} elsif ($i == 1) { |
| 201 |
die "$0:$infile:$line: $mw cannot be used with b\n"; |
| 202 |
} else { |
| 203 |
$o .= "reg$s"; |
| 204 |
} |
| 205 |
} elsif ($mw =~ /^opt(\w?)\#$/) { |
| 206 |
if ($i >= 2) { |
| 207 |
my $opt .= $1 eq '' ? "opt$sn" : "opt$1"; |
| 208 |
if (!($opt eq 'optd' && $s == 16) && $opt ne 'optq') { |
| 209 |
$o .= $opt.' '; |
| 210 |
} |
| 211 |
} |
| 212 |
$o .= "o$s" if ($s > 0); |
| 213 |
} elsif ($mw =~ /^(o64)?nw$/) { |
| 214 |
$long |= 2 if ($i == 32); # nw = 32 bits not encodable in long mode |
| 215 |
$o .= $mw; |
| 216 |
} elsif ($mw =~ /^((k?)(?:reg|rm))(\#{1,2}|[0-9]+)$/) { |
| 217 |
# (Possible) GPR reference |
| 218 |
$o .= $1; |
| 219 |
my $isk = $2; |
| 220 |
my $n = $3; |
| 221 |
if ($n eq '#') { |
| 222 |
$n = $s; |
| 223 |
} elsif ($n eq '##') { |
| 224 |
$n = $s >> 1; |
| 225 |
} |
| 226 |
$o .= $n; |
| 227 |
# 64-bit K registers OK in 32-bit mode |
| 228 |
$long |= 1 if ($n >= 64 && !$isk); |
| 229 |
} elsif ($mw =~ /^(NO)?LONG(\w+)$/) { |
| 230 |
my $longflag = $1 ? 2 : 1; |
| 231 |
$long |= $longflag if ($2 =~ /$sn/i); |
| 232 |
# Drop |
| 233 |
} elsif ($mw eq 'w#') { |
| 234 |
if (!$i) { |
| 235 |
$o .= 'ww'; |
| 236 |
$wwflag = 1; |
| 237 |
} else { |
| 238 |
$o .= ($s >= 64) ? 'w1' : 'w0'; |
| 239 |
} |
| 240 |
} elsif ($mw eq 'w##') { |
| 241 |
$o .= 'w'.(($i-1) & 1); |
| 242 |
} elsif ($mw eq 'S#') { |
| 243 |
$o .= 'S' |
| 244 |
} elsif ($mw eq '#') { |
| 245 |
$o .= $s; |
| 246 |
} else { |
| 247 |
die "$0:$infile:$line: unknown or invalid sequence \"$mw\"\n"; |
| 248 |
} |
| 249 |
} |
| 250 |
$o .= $ins; |
| 251 |
|
| 252 |
$o .= ',WW' if ($wwflag); |
| 253 |
$o .= ',LONG' if ($long & 1); |
| 254 |
$o .= ',NOLONG' if ($long & 2); |
| 255 |
$o .= ',ND' if ($nd); |
| 256 |
|
| 257 |
if ($s >= 32 && $o !~ /\B\!386\b/ && $o =~ /\b(8086|[12]86)\b/) { |
| 258 |
$o .= ',386'; |
| 259 |
} |
| 260 |
|
| 261 |
push(@ol, $o); |
| 262 |
} |
| 263 |
|
| 264 |
return @ol; |
| 265 |
} |
| 266 |
|
| 267 |
# Near branch operand size patterns |
| 268 |
# This allows the "normal" size patterns to be used for |
| 269 |
# address size features, as used by JCXZ and LOOP. |
| 270 |
# This also allows the syntax "jmp dword foo" in 64-bit |
| 271 |
# mode, even though it is really bogus. |
| 272 |
$macros{'br'} = { |
| 273 |
'func' => |
| 274 |
sub { |
| 275 |
my($mac, $args, $rawargs) = @_; |
| 276 |
my @ol; |
| 277 |
my $ins = join(' ', @$rawargs); |
| 278 |
|
| 279 |
foreach my $wx ([16,16], [32,32], [64,64], [64,32]) { |
| 280 |
my($w,$iw,$sz) = @$wx; |
| 281 |
my $i = $ins; |
| 282 |
my $argn; |
| 283 |
if ($i =~ /^(.*)\b(near|short)\b/) { |
| 284 |
my $what = $2; |
| 285 |
next if ($what eq 'short' && $iw != $w); |
| 286 |
(my $argn = $1) =~ s/[^,:]+//g; |
| 287 |
$argn = 'AR'.length($argn); |
| 288 |
} |
| 289 |
$i =~ s/\b(near|short)\b/imm$iw|$1/; |
| 290 |
$i =~ s/\bos\b/nw o$w/; |
| 291 |
$i .= ",$argn"; |
| 292 |
$i .= ($iw != $w) ? ',SX,ND' : ',OSIZE'; |
| 293 |
$i .= ($w == 64) ? ',LONG' : ',NOLONG'; |
| 294 |
push(@ol, $i); |
| 295 |
} |
| 296 |
return(@ol); |
| 297 |
} |
| 298 |
}; |
| 299 |
|
| 300 |
# Common pattern for K-register instructions |
| 301 |
$macros{'k'} = { |
| 302 |
'func' => |
| 303 |
sub { |
| 304 |
my($mac, $args, $rawargs) = @_; |
| 305 |
my @ol; |
| 306 |
my $ins = join(' ', @$rawargs); |
| 307 |
my $xins; |
| 308 |
my $n; |
| 309 |
|
| 310 |
$ins .= ',ZU'; |
| 311 |
($xins = $ins) =~ s/\bSM[0-9-]*,?//; |
| 312 |
push(@ol, $xins); |
| 313 |
($xins = $ins) =~ s/\%//; |
| 314 |
push(@ol, $xins); |
| 315 |
if ($xins =~ s/\%//) { |
| 316 |
push(@ol, $xins); |
| 317 |
} |
| 318 |
|
| 319 |
# Allow instruction without K, as long as they aren't "TEST" |
| 320 |
if ($ins !~ /^\bK\w*TEST/) { |
| 321 |
my @on; |
| 322 |
foreach my $oi (@ol) { |
| 323 |
# Remove first capital K |
| 324 |
($xins = $oi) =~ s/\bK//; |
| 325 |
push(@on, $xins); |
| 326 |
} |
| 327 |
push(@ol, @on); |
| 328 |
} |
| 329 |
|
| 330 |
# Allow SHIFT -> SH |
| 331 |
if ($ins =~ /SHIFT/) { |
| 332 |
my @on; |
| 333 |
foreach my $oi (@ol) { |
| 334 |
# Remove first capital K |
| 335 |
($xins = $oi) =~ s/SHIFT/SH/; |
| 336 |
push(@on, $xins); |
| 337 |
} |
| 338 |
push(@ol, @on); |
| 339 |
} |
| 340 |
|
| 341 |
# All instruction patterns except the first are ND |
| 342 |
for (my $i = 1; $i < scalar(@ol); $i++) { |
| 343 |
$ol[$i] .= ',ND'; |
| 344 |
} |
| 345 |
return(@ol); |
| 346 |
} |
| 347 |
}; |
| 348 |
|
| 349 |
# Common pattern for the HINT_NOPx pseudo-instructions |
| 350 |
$macros{'hint_nops'} = { |
| 351 |
'func' => |
| 352 |
sub { |
| 353 |
my($mac, $args, $rawargs) = @_; |
| 354 |
my @ol; |
| 355 |
|
| 356 |
for (my $i = 0; $i < 64; $i++) { |
| 357 |
push(@ol, |
| 358 |
sprintf("\$wdq HINT_NOP%d\trm#\t[m: o# 0f %02x /%d]\tP6,UNDOC,ND", |
| 359 |
$i, 0x18+($i >> 3), $i & 7)); |
| 360 |
} |
| 361 |
return @ol; |
| 362 |
} |
| 363 |
}; |
| 364 |
|
| 365 |
# |
| 366 |
# Macro helper functions for common constructs |
| 367 |
# |
| 368 |
|
| 369 |
# Parse arguments handling variable setting |
| 370 |
sub parse_args($@) { |
| 371 |
my $uvars = shift(@_); |
| 372 |
my %initvars = defined($uvars) ? %$uvars : (); |
| 373 |
my @oa; |
| 374 |
my $n = 0; # Argument counter |
| 375 |
|
| 376 |
foreach my $ops (@_) { |
| 377 |
my %vars = %initvars; |
| 378 |
$vars{'n'} = $n; |
| 379 |
$vars{'nd'} = 0; |
| 380 |
my @oaa; |
| 381 |
my $is_op; |
| 382 |
foreach my $op ($ops =~ /(?:[^\,\[\]\"]+|\[.*?\]|\".*?\")+/g) { |
| 383 |
$op =~ s/\"//g; |
| 384 |
|
| 385 |
$vars{'nd'} = 'nd' if ($op =~ s/^\@//); |
| 386 |
if ($op =~ /^(\w+)\=(.*)$/) { |
| 387 |
$vars{$1} = $2; |
| 388 |
next; |
| 389 |
} elsif ($op =~ /^([\!\+\-])(\w+)$/) { |
| 390 |
# The commas around KILL guarantees that it is a separate token |
| 391 |
$vars{$2} = |
| 392 |
($1 eq '+') ? $2 : |
| 393 |
($1 eq '!') ? ',KILL,' : |
| 394 |
''; |
| 395 |
next; |
| 396 |
} elsif ($op =~ /^\-?$/) { |
| 397 |
# Null (placeholder) operand |
| 398 |
$is_op = 1; |
| 399 |
next; |
| 400 |
} |
| 401 |
|
| 402 |
$vars{'op'} = $op; |
| 403 |
push(@oaa, {%vars}); |
| 404 |
$vars{'nd'} = 'nd'; |
| 405 |
$is_op = 1; |
| 406 |
} |
| 407 |
if ($is_op) { |
| 408 |
push(@oa, [@oaa]); |
| 409 |
$n++; |
| 410 |
} else { |
| 411 |
# Global variable setting |
| 412 |
%initvars = %vars; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
return @oa; |
| 417 |
} |
| 418 |
|
| 419 |
# "8-fold" or similar sequential instruction patterns |
| 420 |
sub def_eightfold($$$) { |
| 421 |
my($var, $arg, $mac) = @_; |
| 422 |
|
| 423 |
my $shift = $arg->{'shift'}; |
| 424 |
$shift = 3 unless (defined($shift)); |
| 425 |
|
| 426 |
if ($var =~ /^[0-9a-f]{1,2}$/) { |
| 427 |
return sprintf('%02x', hex($var) + ($arg->{'n'} << $shift)); |
| 428 |
} else { |
| 429 |
return $var; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
# |
| 434 |
# Substitute variables in a pattern |
| 435 |
# |
| 436 |
sub substitute($$;$) { |
| 437 |
my($pat, $vars, $defs) = @_; |
| 438 |
my $o = ''; |
| 439 |
my $def; |
| 440 |
my @defargs; |
| 441 |
|
| 442 |
if (defined($defs)) { |
| 443 |
@defargs = @$defs; |
| 444 |
$def = shift(@defargs); |
| 445 |
} |
| 446 |
|
| 447 |
while ($pat =~ /^(.*?)\$(?:(\w+\b|\$+)|\{(\w+|\$+)\})(.*)$/s) { |
| 448 |
$o .= $1; |
| 449 |
$pat = $4; |
| 450 |
my $vn = $2.$3; |
| 451 |
my $vv; |
| 452 |
if ($vn =~ /^\$/) { |
| 453 |
$vv = $vn; # Reduce by one $ |
| 454 |
} else { |
| 455 |
$vv = $vars->{$vn}; |
| 456 |
if (!defined($vv)) { |
| 457 |
if (defined($def)) { |
| 458 |
$vv = $def->($vn, @defargs); |
| 459 |
} |
| 460 |
$vv = $vn unless(defined($vv)); |
| 461 |
} |
| 462 |
} |
| 463 |
$vv =~ s/\s+$// if ($pat =~ /^\s/); |
| 464 |
$o .= $vv; |
| 465 |
} |
| 466 |
$o .= $pat; |
| 467 |
|
| 468 |
return $o; |
| 469 |
} |
| 470 |
|
| 471 |
# |
| 472 |
# Build output by substituting the variables for each argument, |
| 473 |
# |
| 474 |
sub subst_list($$;$$) { |
| 475 |
my($pat, $args, $def, $mac) = @_; |
| 476 |
my @o = (); |
| 477 |
|
| 478 |
foreach my $a0 (@$args) { |
| 479 |
foreach my $arg (@$a0) { |
| 480 |
push(@o, substitute($pat, $arg, [$def, $arg, $mac])); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
return @o; |
| 485 |
} |
| 486 |
|
| 487 |
# |
| 488 |
# Actually invoke a macro |
| 489 |
# |
| 490 |
sub process_macro(@) { |
| 491 |
$macro = shift(@_); |
| 492 |
my $mac = $macros{$macro}; |
| 493 |
|
| 494 |
if (!defined($mac)) { |
| 495 |
die "$0:$infile:$line: no macro named \$$macro\n"; |
| 496 |
} |
| 497 |
|
| 498 |
my @args = parse_args($mac->{'vars'}, @_); |
| 499 |
my $func = $mac->{'func'}; |
| 500 |
my @o; |
| 501 |
if (defined($func)) { |
| 502 |
@o = $func->($mac, \@args, \@_); |
| 503 |
} else { |
| 504 |
@o = subst_list($mac->{'txt'}, \@args, $mac->{'def'}, $mac); |
| 505 |
} |
| 506 |
return map { split(/\n/, $_) } @o; |
| 507 |
} |
| 508 |
|
| 509 |
# |
| 510 |
# Main program |
| 511 |
# |
| 512 |
($infile, $outfile) = @ARGV; |
| 513 |
$line = 0; |
| 514 |
|
| 515 |
## XXX: fix special case: XCHG |
| 516 |
## XXX: check: CMPSS, CMPSD |
| 517 |
## XXX: check VEX encoded instructions that do not write |
| 518 |
|
| 519 |
# Instructions which (possibly) change the flags without annotations |
| 520 |
# The FL or !FL flags will override this |
| 521 |
my $flaggy = '^(aa[adms]|ad[dc]|ad[co]x|aes\w*kl|and|andn|arpl|bextr|bl[sc]ic?|bl[sc]msk|bl[sc]r|\ |
| 522 |
bs[rf]|bt|bt[crs]|bzhi|clac|clc|cld|cli|clrssbsy|cmc|cmp|cmpxchg.*|da[as]|dec|div|\ |
| 523 |
encodekey.*|enqcmd.*|fu?comip?|idiv|imul|inc|iret.*|kortest.*|ktest.*|lar|loadiwkey|\ |
| 524 |
lsl|[lt]zcnt|mul|neg|or|pconfig|popcnt|popf.*|r[co][lr]|rdrand|rdseed|sahf|s[ah][lr]|\ |
| 525 |
sbb|scas.*|sh[lr]d|stac|stc|std|sti|sub|test|testui|tpause|v?u?comis[sdh]|uiret|\ |
| 526 |
umwait|ver[rw]|vtestp[ps]|xadd|xor|xtest|getsec|rsm|sbb|cmps[bwdq]|hint_.*)$'; |
| 527 |
|
| 528 |
# Instructions which don't write their leftmost operand are inherently not {zu} |
| 529 |
my $nozero = '^(jmp|call|bt|test|cmp|ud[012].*|ptwrite|tpause|u?monitor.*|u?mwait.*|incssp.*|\ |
| 530 |
enqcmds?|senduipi|hint_.*|jmpe|nop|inv.*|push2?p?|vmwrite|clzero|clflush|clwb|lkgs)$'; |
| 531 |
|
| 532 |
sub add_flag($@) { |
| 533 |
my $flags = shift(@_); |
| 534 |
|
| 535 |
foreach my $fl (@_) { |
| 536 |
$flags->{$fl}++ unless ($fl =~ /^\s*$/); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
sub has_flag($@) { |
| 541 |
my $flags = shift(@_); |
| 542 |
foreach my $fl (@_) { |
| 543 |
return $flags->{$fl} if ($flags->{$fl}); |
| 544 |
} |
| 545 |
return undef; |
| 546 |
} |
| 547 |
|
| 548 |
sub adjust_fl_zu(@) { |
| 549 |
my($opcode, $operands, $encoding, $flags) = @_; |
| 550 |
|
| 551 |
# Flag-changing instructions |
| 552 |
if ($encoding =~ /\bnf\b/) { |
| 553 |
add_flag($flags, 'NF'); |
| 554 |
} |
| 555 |
|
| 556 |
if (!has_flag($flags, '!FL', 'NF', 'NF!')) { |
| 557 |
add_flag($flags, 'FL') if ($opcode =~ /$flaggy/io); |
| 558 |
} |
| 559 |
|
| 560 |
## XXX: fix special case: XCHG |
| 561 |
## XXX: check: CMPSS, CMPSD |
| 562 |
## XXX: check VEX encoded instructions that do not write |
| 563 |
|
| 564 |
# Zero-upper. This can also be used to select the AVX forms |
| 565 |
# to clear the upper part of a vector register. |
| 566 |
if (!$flags->{'!ZU'} && |
| 567 |
(($encoding =~ /\be?vex\b/ && $operands =~ /^(xyz)mm(reg|rm)/) || |
| 568 |
$operands =~ /^(reg_[re]([abcd]x|[sb]p|[sd]i))\b/) && |
| 569 |
$opcode !~ /$nozero/io) { |
| 570 |
add_flag($flags, 'ZU'); |
| 571 |
} |
| 572 |
|
| 573 |
return $flags; |
| 574 |
} |
| 575 |
|
| 576 |
sub adjust_instruction_flags(@) { |
| 577 |
my @i = @_; |
| 578 |
|
| 579 |
$i[3] = adjust_fl_zu(@i); |
| 580 |
|
| 581 |
return undef unless (defined($i[3])); |
| 582 |
|
| 583 |
if ($i[0] =~ /\bKILL\b/ || $i[1] =~ /\bKILL\b/ || |
| 584 |
$i[2] =~ /\bKILL\b/ || has_flag($i[3], 'KILL')) { |
| 585 |
return undef; |
| 586 |
} |
| 587 |
|
| 588 |
if ($i[2] =~ /\b(a16|rex\.l)\b/) { |
| 589 |
add_flag($i[3], 'NOLONG'); |
| 590 |
} elsif ($i[2] =~ /\b(o64(nw)?\b|rex2?|a64\b)/) { |
| 591 |
add_flag($i[3], 'LONG'); |
| 592 |
} |
| 593 |
if (has_flag($i[3], 'NOLONG') && has_flag($i[3], 'LONG')) { |
| 594 |
# This is obviously not very useful... |
| 595 |
return undef; |
| 596 |
} |
| 597 |
|
| 598 |
if (has_flag($i[3], 'LONG')) { |
| 599 |
add_flag($i[3], 'X86_64'); |
| 600 |
} |
| 601 |
|
| 602 |
return $i[3]; |
| 603 |
} |
| 604 |
|
| 605 |
sub process_insn($$) { |
| 606 |
my($out, $l) = @_; |
| 607 |
|
| 608 |
if ($l !~ /^\s*([^\s\;]+)\s+([^\s\;]+)\s+([^\s\;]+|\[[^\;]*?\])\s+([^\s\;]+)\s*(\;.*?)?\s*$/) { |
| 609 |
print $out $l, "\n"; |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
print $out $5, "\n" unless ($5 eq ''); # Comment |
| 614 |
|
| 615 |
my $opcode = $1; |
| 616 |
my $operands = $2; |
| 617 |
my $encoding = $3; |
| 618 |
my $flagstr = $4; |
| 619 |
|
| 620 |
my $nopr = ($operands =~ /^(void|ignore)$/) ? 0 : scalar(split(/[\,\:]/, $operands)); |
| 621 |
|
| 622 |
# Modify the instruction flags |
| 623 |
my $flags = {split_flags($flagstr)}; |
| 624 |
set_implied_flags($flags, $nopr); |
| 625 |
|
| 626 |
$flags = adjust_instruction_flags($opcode, $operands, $encoding, $flags); |
| 627 |
return unless (defined($flags)); |
| 628 |
$flagstr = merge_flags($flags, 1); |
| 629 |
|
| 630 |
# Tidy up the encoding for readability |
| 631 |
$encoding =~ s/\s+/ /g; |
| 632 |
if ($encoding =~ /^\s*\[\s*(.*?\:)?\s*([^\:]*?)\s*\]\s*$/) { |
| 633 |
$encoding = sprintf('[%-10s%-34s]', $1, $2); |
| 634 |
} |
| 635 |
|
| 636 |
print $out sprintf("%-23s %-39s %-47s %s\n", $opcode, $operands, $encoding, $flagstr); |
| 637 |
} |
| 638 |
|
| 639 |
open(my $in, '<', $infile) or die "$0:$infile: $!\n"; |
| 640 |
open(my $out, '>', $outfile) or die "$0:$outfile: $!\n"; |
| 641 |
|
| 642 |
|
| 643 |
while (defined(my $l = <$in>)) { |
| 644 |
$line++; |
| 645 |
chomp $l; |
| 646 |
my @insi = ($l); |
| 647 |
|
| 648 |
while (defined(my $li = shift(@insi))) { |
| 649 |
if ($li =~ /^\s*\$(\w+[^\;]*?)\s*(\;.*)?$/) { |
| 650 |
push(@insi, "$2\n") unless ($2 eq ''); # Retain comment |
| 651 |
my @args = ($1 =~ /(?:\[[^\]]*\]|\"[^\"]*\"|[^\[\]\"\s])+/g); |
| 652 |
unshift(@insi, process_macro(@args)); |
| 653 |
} else { |
| 654 |
process_insn($out, $li); |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
close($in); |
| 660 |
close($out); |