]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/perlasm/x86_64-xlate.pl
Also check for errors in x86_64-xlate.pl.
[thirdparty/openssl.git] / crypto / perlasm / x86_64-xlate.pl
CommitLineData
e0a65194 1#! /usr/bin/env perl
1212818e 2# Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
e0a65194 3#
a5d9549d 4# Licensed under the Apache License 2.0 (the "License"). You may not use
e0a65194
RS
5# this file except in compliance with the License. You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
1cfd258e 9
80aa9cc9 10# Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
1cfd258e
AP
11#
12# Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
13# format is way easier to parse. Because it's simpler to "gear" from
14# Unix ABI to Windows one [see cross-reference "card" at the end of
15# file]. Because Linux targets were available first...
16#
17# In addition the script also "distills" code suitable for GNU
18# assembler, so that it can be compiled with more rigid assemblers,
19# such as Solaris /usr/ccs/bin/as.
20#
21# This translator is not designed to convert *arbitrary* assembler
22# code from AT&T format to MASM one. It's designed to convert just
23# enough to provide for dual-ABI OpenSSL modules development...
24# There *are* limitations and you might have to modify your assembler
25# code or this script to achieve the desired result...
26#
27# Currently recognized limitations:
28#
29# - can't use multiple ops per line;
1cfd258e
AP
30#
31# Dual-ABI styling rules.
32#
80aa9cc9
AP
33# 1. Adhere to Unix register and stack layout [see cross-reference
34# ABI "card" at the end for explanation].
1cfd258e
AP
35# 2. Forget about "red zone," stick to more traditional blended
36# stack frame allocation. If volatile storage is actually required
37# that is. If not, just leave the stack as is.
38# 3. Functions tagged with ".type name,@function" get crafted with
4b450519 39# unified Win64 prologue and epilogue automatically. If you want
1cfd258e 40# to take care of ABI differences yourself, tag functions as
4b450519
AP
41# ".type name,@abi-omnipotent" instead.
42# 4. To optimize the Win64 prologue you can specify number of input
43# arguments as ".type name,@function,N." Keep in mind that if N is
44# larger than 6, then you *have to* write "abi-omnipotent" code,
45# because >6 cases can't be addressed with unified prologue.
46# 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
47# (sorry about latter).
48# 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
49# required to identify the spots, where to inject Win64 epilogue!
50# But on the pros, it's then prefixed with rep automatically:-)
85253772
AP
51# 7. Stick to explicit ip-relative addressing. If you have to use
52# GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
53# Both are recognized and translated to proper Win64 addressing
9d301cfe 54# modes.
80aa9cc9
AP
55#
56# 8. In order to provide for structured exception handling unified
57# Win64 prologue copies %rsp value to %rax. For further details
58# see SEH paragraph at the end.
85253772 59# 9. .init segment is allowed to contain calls to functions only.
f9a152bd
AP
60# a. If function accepts more than 4 arguments *and* >4th argument
61# is declared as non 64-bit value, do clear its upper part.
80aa9cc9 62\f
c25278db
DB
63
64use strict;
65
85253772
AP
66my $flavour = shift;
67my $output = shift;
68if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
932cc129 69
b2ae61ec
AP
70open STDOUT,">$output" || die "can't open $output: $!"
71 if (defined($output));
1cfd258e 72
85253772
AP
73my $gas=1; $gas=0 if ($output =~ /\.asm$/);
74my $elf=1; $elf=0 if (!$gas);
75my $win64=0;
76my $prefix="";
93c4ba07 77my $decor=".L";
a23e3dbe 78
8ab9025e 79my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
9b634c9b 80my $masm=0;
a23e3dbe
AP
81my $PTR=" PTR";
82
9b634c9b 83my $nasmref=2.03;
a23e3dbe
AP
84my $nasm=0;
85
f9a152bd
AP
86if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
87 $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
9ba96fbb 88 $prefix =~ s|\R$||; # Better chomp
f9a152bd 89 }
93c4ba07
AP
90elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
91elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
92elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
85253772 93elsif (!$gas)
9b634c9b
AP
94{ if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
95 { $nasm = $1 + $2*0.01; $PTR=""; }
a23e3dbe
AP
96 elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
97 { $masm = $1 + $2*2**-16 + $4*2**-32; }
e09b6216 98 die "no assembler found on %PATH%" if (!($nasm || $masm));
85253772
AP
99 $win64=1;
100 $elf=0;
93c4ba07 101 $decor="\$L\$";
a23e3dbe 102}
1cfd258e 103
51994e50
L
104my $cet_property;
105if ($flavour =~ /elf/) {
106 # Always generate .note.gnu.property section for ELF outputs to
107 # mark Intel CET support since all input files must be marked
108 # with Intel CET support in order for linker to mark output with
109 # Intel CET support.
110 my $p2align=3; $p2align=2 if ($flavour eq "elf32");
111 $cet_property = <<_____;
b2b43d1b 112 .section ".note.gnu.property", "a"
51994e50 113 .p2align $p2align
b2b43d1b
RL
114 .long 1f - 0f
115 .long 4f - 1f
116 .long 5
1170:
118 .asciz "GNU"
1191:
51994e50 120 .p2align $p2align
b2b43d1b
RL
121 .long 0xc0000002
122 .long 3f - 2f
1232:
124 .long 3
1253:
51994e50 126 .p2align $p2align
b2b43d1b
RL
1274:
128_____
51994e50
L
129}
130
1cfd258e
AP
131my $current_segment;
132my $current_function;
9b634c9b 133my %globals;
1cfd258e
AP
134
135{ package opcode; # pick up opcodes
136 sub re {
c25278db
DB
137 my ($class, $line) = @_;
138 my $self = {};
139 my $ret;
1cfd258e 140
c25278db
DB
141 if ($$line =~ /^([a-z][a-z0-9]*)/i) {
142 bless $self,$class;
1cfd258e
AP
143 $self->{op} = $1;
144 $ret = $self;
c25278db 145 $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
1cfd258e
AP
146
147 undef $self->{sz};
272ba870 148 if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain...
1cfd258e 149 $self->{op} = $1;
272ba870 150 $self->{sz} = $2;
9b634c9b 151 } elsif ($self->{op} =~ /call|jmp/) {
fead2539 152 $self->{sz} = "";
a9e790b9 153 } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
fead2539 154 $self->{sz} = "";
526ab896 155 } elsif ($self->{op} =~ /^[vk]/) { # VEX or k* such as kmov
0a9a692e 156 $self->{sz} = "";
c25278db 157 } elsif ($self->{op} =~ /mov[dq]/ && $$line =~ /%xmm/) {
c7b903e0 158 $self->{sz} = "";
a078befc 159 } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
1cfd258e
AP
160 $self->{op} = $1;
161 $self->{sz} = $2;
162 }
163 }
164 $ret;
165 }
166 sub size {
c25278db 167 my ($self, $sz) = @_;
1cfd258e
AP
168 $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
169 $self->{sz};
170 }
171 sub out {
172 my $self = shift;
85253772 173 if ($gas) {
932cc129 174 if ($self->{op} eq "movz") { # movz is pain...
1cfd258e 175 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
609b0852 176 } elsif ($self->{op} =~ /^set/) {
932cc129 177 "$self->{op}";
1cfd258e 178 } elsif ($self->{op} eq "ret") {
85253772
AP
179 my $epilogue = "";
180 if ($win64 && $current_function->{abi} eq "svr4") {
181 $epilogue = "movq 8(%rsp),%rdi\n\t" .
182 "movq 16(%rsp),%rsi\n\t";
183 }
184 $epilogue . ".byte 0xf3,0xc3";
185 } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
186 ".p2align\t3\n\t.quad";
1cfd258e
AP
187 } else {
188 "$self->{op}$self->{sz}";
189 }
190 } else {
a078befc 191 $self->{op} =~ s/^movz/movzx/;
1cfd258e
AP
192 if ($self->{op} eq "ret") {
193 $self->{op} = "";
85253772 194 if ($win64 && $current_function->{abi} eq "svr4") {
67b8bf4d
AP
195 $self->{op} = "mov rdi,QWORD$PTR\[8+rsp\]\t;WIN64 epilogue\n\t".
196 "mov rsi,QWORD$PTR\[16+rsp\]\n\t";
1cfd258e
AP
197 }
198 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
85253772 199 } elsif ($self->{op} =~ /^(pop|push)f/) {
80aa9cc9 200 $self->{op} .= $self->{sz};
85253772 201 } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
d6522548 202 $self->{op} = "\tDQ";
609b0852 203 }
1cfd258e
AP
204 $self->{op};
205 }
206 }
85253772 207 sub mnemonic {
c25278db 208 my ($self, $op) = @_;
85253772
AP
209 $self->{op}=$op if (defined($op));
210 $self->{op};
211 }
1cfd258e
AP
212}
213{ package const; # pick up constants, which start with $
214 sub re {
c25278db
DB
215 my ($class, $line) = @_;
216 my $self = {};
217 my $ret;
1cfd258e 218
c25278db
DB
219 if ($$line =~ /^\$([^,]+)/) {
220 bless $self, $class;
1cfd258e
AP
221 $self->{value} = $1;
222 $ret = $self;
c25278db 223 $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
1cfd258e
AP
224 }
225 $ret;
226 }
227 sub out {
228 my $self = shift;
5d0d60e2 229
6e42e3ff 230 $self->{value} =~ s/\b(0b[0-1]+)/oct($1)/eig;
85253772 231 if ($gas) {
e84b663a
AP
232 # Solaris /usr/ccs/bin/as can't handle multiplications
233 # in $self->{value}
fd7dc201 234 my $value = $self->{value};
67b8bf4d 235 no warnings; # oct might complain about overflow, ignore here...
fd7dc201
AP
236 $value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
237 if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) {
238 $self->{value} = $value;
239 }
5d0d60e2
AP
240 sprintf "\$%s",$self->{value};
241 } else {
c47aea8a
AP
242 my $value = $self->{value};
243 $value =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
244 sprintf "%s",$value;
5d0d60e2 245 }
1cfd258e
AP
246 }
247}
248{ package ea; # pick up effective addresses: expr(%reg,%reg,scale)
e09b6216
AP
249
250 my %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR",
251 l=>"DWORD$PTR", d=>"DWORD$PTR",
252 q=>"QWORD$PTR", o=>"OWORD$PTR",
253 x=>"XMMWORD$PTR", y=>"YMMWORD$PTR",
254 z=>"ZMMWORD$PTR" ) if (!$gas);
255
1cfd258e 256 sub re {
c25278db
DB
257 my ($class, $line, $opcode) = @_;
258 my $self = {};
259 my $ret;
1cfd258e 260
67b8bf4d 261 # optional * ----vvv--- appears in indirect jmp/call
526ab896 262 if ($$line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)((?:{[^}]+})*)/) {
c25278db 263 bless $self, $class;
9b634c9b
AP
264 $self->{asterisk} = $1;
265 $self->{label} = $2;
266 ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
1cfd258e 267 $self->{scale} = 1 if (!defined($self->{scale}));
e09b6216 268 $self->{opmask} = $4;
1cfd258e 269 $ret = $self;
c25278db 270 $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
1cfd258e 271
85253772 272 if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
c25278db
DB
273 die if ($opcode->mnemonic() ne "mov");
274 $opcode->mnemonic("lea");
85253772 275 }
1cfd258e
AP
276 $self->{base} =~ s/^%//;
277 $self->{index} =~ s/^%// if (defined($self->{index}));
c25278db 278 $self->{opcode} = $opcode;
1cfd258e
AP
279 }
280 $ret;
281 }
282 sub size {}
283 sub out {
c25278db 284 my ($self, $sz) = @_;
1cfd258e 285
85253772 286 $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
93c4ba07 287 $self->{label} =~ s/\.L/$decor/g;
85253772 288
afbe674e
AP
289 # Silently convert all EAs to 64-bit. This is required for
290 # elder GNU assembler and results in more compact code,
291 # *but* most importantly AES module depends on this feature!
292 $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
293 $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
294
6fa4c7c4 295 # Solaris /usr/ccs/bin/as can't handle multiplications
82e08930 296 # in $self->{label}...
6fa4c7c4
AP
297 use integer;
298 $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
667053a2 299 $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
82e08930
AP
300
301 # Some assemblers insist on signed presentation of 32-bit
302 # offsets, but sign extension is a tricky business in perl...
303 if ((1<<31)<<1) {
304 $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
305 } else {
306 $self->{label} =~ s/\b([0-9]+)\b/$1>>0/eg;
307 }
6fa4c7c4 308
e09b6216 309 # if base register is %rbp or %r13, see if it's possible to
fa3f8355 310 # flip base and index registers [for better performance]
22de0e65
AP
311 if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
312 $self->{base} =~ /(rbp|r13)/) {
313 $self->{base} = $self->{index}; $self->{index} = $1;
314 }
315
85253772 316 if ($gas) {
93c4ba07 317 $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64");
4b450519 318
1cfd258e 319 if (defined($self->{index})) {
526ab896
AP
320 sprintf "%s%s(%s,%%%s,%d)%s",
321 $self->{asterisk},$self->{label},
b5c6aab5 322 $self->{base}?"%$self->{base}":"",
526ab896 323 $self->{index},$self->{scale},
e09b6216 324 $self->{opmask};
5d0d60e2 325 } else {
526ab896 326 sprintf "%s%s(%%%s)%s", $self->{asterisk},$self->{label},
e09b6216 327 $self->{base},$self->{opmask};
1cfd258e
AP
328 }
329 } else {
70cf3095 330 $self->{label} =~ s/\./\$/g;
f9a152bd 331 $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
70cf3095 332 $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
1b0fe79f 333
c25278db
DB
334 my $mnemonic = $self->{opcode}->mnemonic();
335 ($self->{asterisk}) && ($sz="q") ||
336 ($mnemonic =~ /^v?mov([qd])$/) && ($sz=$1) ||
337 ($mnemonic =~ /^v?pinsr([qdwb])$/) && ($sz=$1) ||
338 ($mnemonic =~ /^vpbroadcast([qdwb])$/) && ($sz=$1) ||
339 ($mnemonic =~ /^v(?!perm)[a-z]+[fi]128$/) && ($sz="x");
70cf3095 340
e09b6216 341 $self->{opmask} =~ s/%(k[0-7])/$1/;
526ab896 342
1cfd258e 343 if (defined($self->{index})) {
526ab896 344 sprintf "%s[%s%s*%d%s]%s",$szmap{$sz},
a23e3dbe 345 $self->{label}?"$self->{label}+":"",
1cfd258e 346 $self->{index},$self->{scale},
526ab896 347 $self->{base}?"+$self->{base}":"",
e09b6216 348 $self->{opmask};
932cc129 349 } elsif ($self->{base} eq "rip") {
a23e3dbe 350 sprintf "%s[%s]",$szmap{$sz},$self->{label};
5d0d60e2 351 } else {
526ab896 352 sprintf "%s[%s%s]%s", $szmap{$sz},
a23e3dbe 353 $self->{label}?"$self->{label}+":"",
e09b6216 354 $self->{base},$self->{opmask};
1cfd258e
AP
355 }
356 }
357 }
358}
359{ package register; # pick up registers, which start with %.
360 sub re {
6a4ea002 361 my ($class, $line, $opcode) = @_;
1cfd258e 362 my $self = {};
c25278db 363 my $ret;
1cfd258e 364
6a4ea002 365 # optional * ----vvv--- appears in indirect jmp/call
526ab896 366 if ($$line =~ /^(\*?)%(\w+)((?:{[^}]+})*)/) {
1cfd258e 367 bless $self,$class;
9b634c9b
AP
368 $self->{asterisk} = $1;
369 $self->{value} = $2;
e09b6216 370 $self->{opmask} = $3;
6a4ea002 371 $opcode->size($self->size());
1cfd258e 372 $ret = $self;
c25278db 373 $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
1cfd258e
AP
374 }
375 $ret;
376 }
377 sub size {
378 my $self = shift;
c25278db 379 my $ret;
1cfd258e
AP
380
381 if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
382 elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
383 elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
384 elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
385 elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
386 elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
387 elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
388 elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
389
390 $ret;
391 }
392 sub out {
393 my $self = shift;
526ab896
AP
394 if ($gas) { sprintf "%s%%%s%s", $self->{asterisk},
395 $self->{value},
e09b6216
AP
396 $self->{opmask}; }
397 else { $self->{opmask} =~ s/%(k[0-7])/$1/;
398 $self->{value}.$self->{opmask}; }
1cfd258e
AP
399 }
400}
401{ package label; # pick up labels, which end with :
402 sub re {
c25278db
DB
403 my ($class, $line) = @_;
404 my $self = {};
405 my $ret;
1cfd258e 406
c25278db
DB
407 if ($$line =~ /(^[\.\w]+)\:/) {
408 bless $self,$class;
1cfd258e
AP
409 $self->{value} = $1;
410 $ret = $self;
c25278db 411 $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
1cfd258e 412
93c4ba07 413 $self->{value} =~ s/^\.L/$decor/;
1cfd258e
AP
414 }
415 $ret;
416 }
417 sub out {
418 my $self = shift;
419
85253772
AP
420 if ($gas) {
421 my $func = ($globals{$self->{value}} or $self->{value}) . ":";
e09b6216
AP
422 if ($win64 && $current_function->{name} eq $self->{value}
423 && $current_function->{abi} eq "svr4") {
85253772
AP
424 $func .= "\n";
425 $func .= " movq %rdi,8(%rsp)\n";
426 $func .= " movq %rsi,16(%rsp)\n";
427 $func .= " movq %rsp,%rax\n";
93c4ba07 428 $func .= "${decor}SEH_begin_$current_function->{name}:\n";
85253772
AP
429 my $narg = $current_function->{narg};
430 $narg=6 if (!defined($narg));
431 $func .= " movq %rcx,%rdi\n" if ($narg>0);
432 $func .= " movq %rdx,%rsi\n" if ($narg>1);
433 $func .= " movq %r8,%rdx\n" if ($narg>2);
434 $func .= " movq %r9,%rcx\n" if ($narg>3);
435 $func .= " movq 40(%rsp),%r8\n" if ($narg>4);
436 $func .= " movq 48(%rsp),%r9\n" if ($narg>5);
437 }
438 $func;
439 } elsif ($self->{value} ne "$current_function->{name}") {
c25278db
DB
440 # Make all labels in masm global.
441 $self->{value} .= ":" if ($masm);
85253772
AP
442 $self->{value} . ":";
443 } elsif ($win64 && $current_function->{abi} eq "svr4") {
9b634c9b
AP
444 my $func = "$current_function->{name}" .
445 ($nasm ? ":" : "\tPROC $current_function->{scope}") .
446 "\n";
67b8bf4d
AP
447 $func .= " mov QWORD$PTR\[8+rsp\],rdi\t;WIN64 prologue\n";
448 $func .= " mov QWORD$PTR\[16+rsp\],rsi\n";
80aa9cc9 449 $func .= " mov rax,rsp\n";
93c4ba07 450 $func .= "${decor}SEH_begin_$current_function->{name}:";
80aa9cc9
AP
451 $func .= ":" if ($masm);
452 $func .= "\n";
1cfd258e
AP
453 my $narg = $current_function->{narg};
454 $narg=6 if (!defined($narg));
455 $func .= " mov rdi,rcx\n" if ($narg>0);
456 $func .= " mov rsi,rdx\n" if ($narg>1);
457 $func .= " mov rdx,r8\n" if ($narg>2);
458 $func .= " mov rcx,r9\n" if ($narg>3);
67b8bf4d
AP
459 $func .= " mov r8,QWORD$PTR\[40+rsp\]\n" if ($narg>4);
460 $func .= " mov r9,QWORD$PTR\[48+rsp\]\n" if ($narg>5);
1cfd258e
AP
461 $func .= "\n";
462 } else {
9b634c9b
AP
463 "$current_function->{name}".
464 ($nasm ? ":" : "\tPROC $current_function->{scope}");
1cfd258e
AP
465 }
466 }
467}
7e12cdb5 468{ package expr; # pick up expressions
1cfd258e 469 sub re {
c25278db
DB
470 my ($class, $line, $opcode) = @_;
471 my $self = {};
472 my $ret;
1cfd258e 473
c25278db
DB
474 if ($$line =~ /(^[^,]+)/) {
475 bless $self,$class;
1cfd258e
AP
476 $self->{value} = $1;
477 $ret = $self;
c25278db 478 $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
1cfd258e 479
85253772
AP
480 $self->{value} =~ s/\@PLT// if (!$elf);
481 $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
93c4ba07 482 $self->{value} =~ s/\.L/$decor/g;
c25278db 483 $self->{opcode} = $opcode;
1cfd258e
AP
484 }
485 $ret;
486 }
487 sub out {
488 my $self = shift;
c25278db 489 if ($nasm && $self->{opcode}->mnemonic()=~m/^j(?![re]cxz)/) {
9b634c9b
AP
490 "NEAR ".$self->{value};
491 } else {
492 $self->{value};
493 }
1cfd258e
AP
494 }
495}
a3b5684f
AP
496{ package cfi_directive;
497 # CFI directives annotate instructions that are significant for
498 # stack unwinding procedure compliant with DWARF specification,
499 # see http://dwarfstd.org/. Besides naturally expected for this
500 # script platform-specific filtering function, this module adds
501 # three auxiliary synthetic directives not recognized by [GNU]
502 # assembler:
503 #
504 # - .cfi_push to annotate push instructions in prologue, which
505 # translates to .cfi_adjust_cfa_offset (if needed) and
506 # .cfi_offset;
507 # - .cfi_pop to annotate pop instructions in epilogue, which
508 # translates to .cfi_adjust_cfa_offset (if needed) and
509 # .cfi_restore;
510 # - [and most notably] .cfi_cfa_expression which encodes
511 # DW_CFA_def_cfa_expression and passes it to .cfi_escape as
512 # byte vector;
513 #
514 # CFA expressions were introduced in DWARF specification version
515 # 3 and describe how to deduce CFA, Canonical Frame Address. This
516 # becomes handy if your stack frame is variable and you can't
517 # spare register for [previous] frame pointer. Suggested directive
518 # syntax is made-up mix of DWARF operator suffixes [subset of]
519 # and references to registers with optional bias. Following example
520 # describes offloaded *original* stack pointer at specific offset
521 # from *current* stack pointer:
522 #
523 # .cfi_cfa_expression %rsp+40,deref,+8
524 #
525 # Final +8 has everything to do with the fact that CFA is defined
526 # as reference to top of caller's stack, and on x86_64 call to
527 # subroutine pushes 8-byte return address. In other words original
528 # stack pointer upon entry to a subroutine is 8 bytes off from CFA.
529
530 # Below constants are taken from "DWARF Expressions" section of the
531 # DWARF specification, section is numbered 7.7 in versions 3 and 4.
532 my %DW_OP_simple = ( # no-arg operators, mapped directly
533 deref => 0x06, dup => 0x12,
534 drop => 0x13, over => 0x14,
535 pick => 0x15, swap => 0x16,
536 rot => 0x17, xderef => 0x18,
537
538 abs => 0x19, and => 0x1a,
539 div => 0x1b, minus => 0x1c,
540 mod => 0x1d, mul => 0x1e,
541 neg => 0x1f, not => 0x20,
542 or => 0x21, plus => 0x22,
543 shl => 0x24, shr => 0x25,
544 shra => 0x26, xor => 0x27,
545 );
546
547 my %DW_OP_complex = ( # used in specific subroutines
548 constu => 0x10, # uleb128
549 consts => 0x11, # sleb128
550 plus_uconst => 0x23, # uleb128
551 lit0 => 0x30, # add 0-31 to opcode
552 reg0 => 0x50, # add 0-31 to opcode
553 breg0 => 0x70, # add 0-31 to opcole, sleb128
554 regx => 0x90, # uleb28
555 fbreg => 0x91, # sleb128
556 bregx => 0x92, # uleb128, sleb128
557 piece => 0x93, # uleb128
558 );
559
560 # Following constants are defined in x86_64 ABI supplement, for
46f4e1be 561 # example available at https://www.uclibc.org/docs/psABI-x86_64.pdf,
a3b5684f
AP
562 # see section 3.7 "Stack Unwind Algorithm".
563 my %DW_reg_idx = (
564 "%rax"=>0, "%rdx"=>1, "%rcx"=>2, "%rbx"=>3,
565 "%rsi"=>4, "%rdi"=>5, "%rbp"=>6, "%rsp"=>7,
566 "%r8" =>8, "%r9" =>9, "%r10"=>10, "%r11"=>11,
567 "%r12"=>12, "%r13"=>13, "%r14"=>14, "%r15"=>15
568 );
569
570 my ($cfa_reg, $cfa_rsp);
c0e8e500 571 my @cfa_stack;
a3b5684f
AP
572
573 # [us]leb128 format is variable-length integer representation base
574 # 2^128, with most significant bit of each byte being 0 denoting
46f4e1be 575 # *last* most significant digit. See "Variable Length Data" in the
a3b5684f
AP
576 # DWARF specification, numbered 7.6 at least in versions 3 and 4.
577 sub sleb128 {
578 use integer; # get right shift extend sign
579
580 my $val = shift;
581 my $sign = ($val < 0) ? -1 : 0;
582 my @ret = ();
583
584 while(1) {
585 push @ret, $val&0x7f;
586
587 # see if remaining bits are same and equal to most
588 # significant bit of the current digit, if so, it's
589 # last digit...
590 last if (($val>>6) == $sign);
591
592 @ret[-1] |= 0x80;
593 $val >>= 7;
594 }
595
596 return @ret;
597 }
598 sub uleb128 {
599 my $val = shift;
600 my @ret = ();
601
602 while(1) {
603 push @ret, $val&0x7f;
604
605 # see if it's last significant digit...
606 last if (($val >>= 7) == 0);
607
608 @ret[-1] |= 0x80;
609 }
610
611 return @ret;
612 }
613 sub const {
614 my $val = shift;
615
616 if ($val >= 0 && $val < 32) {
617 return ($DW_OP_complex{lit0}+$val);
618 }
619 return ($DW_OP_complex{consts}, sleb128($val));
620 }
621 sub reg {
622 my $val = shift;
623
624 return if ($val !~ m/^(%r\w+)(?:([\+\-])((?:0x)?[0-9a-f]+))?/);
625
626 my $reg = $DW_reg_idx{$1};
627 my $off = eval ("0 $2 $3");
628
629 return (($DW_OP_complex{breg0} + $reg), sleb128($off));
630 # Yes, we use DW_OP_bregX+0 to push register value and not
631 # DW_OP_regX, because latter would require even DW_OP_piece,
632 # which would be a waste under the circumstances. If you have
633 # to use DWP_OP_reg, use "regx:N"...
634 }
635 sub cfa_expression {
636 my $line = shift;
637 my @ret;
638
639 foreach my $token (split(/,\s*/,$line)) {
640 if ($token =~ /^%r/) {
641 push @ret,reg($token);
1cb35b47
AP
642 } elsif ($token =~ /((?:0x)?[0-9a-f]+)\((%r\w+)\)/) {
643 push @ret,reg("$2+$1");
a3b5684f
AP
644 } elsif ($token =~ /(\w+):(\-?(?:0x)?[0-9a-f]+)(U?)/i) {
645 my $i = 1*eval($2);
646 push @ret,$DW_OP_complex{$1}, ($3 ? uleb128($i) : sleb128($i));
647 } elsif (my $i = 1*eval($token) or $token eq "0") {
648 if ($token =~ /^\+/) {
649 push @ret,$DW_OP_complex{plus_uconst},uleb128($i);
650 } else {
651 push @ret,const($i);
652 }
653 } else {
654 push @ret,$DW_OP_simple{$token};
655 }
656 }
657
658 # Finally we return DW_CFA_def_cfa_expression, 15, followed by
659 # length of the expression and of course the expression itself.
660 return (15,scalar(@ret),@ret);
661 }
662 sub re {
663 my ($class, $line) = @_;
664 my $self = {};
665 my $ret;
666
88be429f 667 if ($$line =~ s/^\s*\.cfi_(\w+)\s*//) {
a3b5684f
AP
668 bless $self,$class;
669 $ret = $self;
670 undef $self->{value};
671 my $dir = $1;
672
673 SWITCH: for ($dir) {
674 # What is $cfa_rsp? Effectively it's difference between %rsp
675 # value and current CFA, Canonical Frame Address, which is
676 # why it starts with -8. Recall that CFA is top of caller's
677 # stack...
678 /startproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", -8); last; };
e0963310
DB
679 /endproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", 0);
680 # .cfi_remember_state directives that are not
681 # matched with .cfi_restore_state are
682 # unnecessary.
683 die "unpaired .cfi_remember_state" if (@cfa_stack);
684 last;
685 };
a3b5684f
AP
686 /def_cfa_register/
687 && do { $cfa_reg = $$line; last; };
688 /def_cfa_offset/
689 && do { $cfa_rsp = -1*eval($$line) if ($cfa_reg eq "%rsp");
690 last;
691 };
692 /adjust_cfa_offset/
693 && do { $cfa_rsp -= 1*eval($$line) if ($cfa_reg eq "%rsp");
694 last;
695 };
88be429f 696 /def_cfa/ && do { if ($$line =~ /(%r\w+)\s*,\s*(.+)/) {
a3b5684f
AP
697 $cfa_reg = $1;
698 $cfa_rsp = -1*eval($2) if ($cfa_reg eq "%rsp");
699 }
700 last;
701 };
702 /push/ && do { $dir = undef;
703 $cfa_rsp -= 8;
704 if ($cfa_reg eq "%rsp") {
705 $self->{value} = ".cfi_adjust_cfa_offset\t8\n";
706 }
707 $self->{value} .= ".cfi_offset\t$$line,$cfa_rsp";
708 last;
709 };
710 /pop/ && do { $dir = undef;
711 $cfa_rsp += 8;
712 if ($cfa_reg eq "%rsp") {
713 $self->{value} = ".cfi_adjust_cfa_offset\t-8\n";
714 }
715 $self->{value} .= ".cfi_restore\t$$line";
716 last;
717 };
718 /cfa_expression/
719 && do { $dir = undef;
720 $self->{value} = ".cfi_escape\t" .
721 join(",", map(sprintf("0x%02x", $_),
722 cfa_expression($$line)));
723 last;
724 };
c0e8e500
DB
725 /remember_state/
726 && do { push @cfa_stack, [$cfa_reg, $cfa_rsp];
727 last;
728 };
729 /restore_state/
730 && do { ($cfa_reg, $cfa_rsp) = @{pop @cfa_stack};
731 last;
732 };
a3b5684f
AP
733 }
734
735 $self->{value} = ".cfi_$dir\t$$line" if ($dir);
736
737 $$line = "";
738 }
739
740 return $ret;
741 }
742 sub out {
743 my $self = shift;
744 return ($elf ? $self->{value} : undef);
745 }
746}
1cfd258e
AP
747{ package directive; # pick up directives, which start with .
748 sub re {
c25278db
DB
749 my ($class, $line) = @_;
750 my $self = {};
751 my $ret;
1cfd258e
AP
752 my $dir;
753
a3b5684f
AP
754 # chain-call to cfi_directive
755 $ret = cfi_directive->re($line) and return $ret;
756
c25278db
DB
757 if ($$line =~ /^\s*(\.\w+)/) {
758 bless $self,$class;
85253772
AP
759 $dir = $1;
760 $ret = $self;
761 undef $self->{value};
c25278db 762 $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
85253772
AP
763
764 SWITCH: for ($dir) {
85253772 765 /\.global|\.globl|\.extern/
c25278db
DB
766 && do { $globals{$$line} = $prefix . $$line;
767 $$line = $globals{$$line} if ($prefix);
85253772
AP
768 last;
769 };
c25278db 770 /\.type/ && do { my ($sym,$type,$narg) = split(',',$$line);
85253772
AP
771 if ($type eq "\@function") {
772 undef $current_function;
773 $current_function->{name} = $sym;
774 $current_function->{abi} = "svr4";
775 $current_function->{narg} = $narg;
776 $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
777 } elsif ($type eq "\@abi-omnipotent") {
778 undef $current_function;
779 $current_function->{name} = $sym;
780 $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
781 }
c25278db
DB
782 $$line =~ s/\@abi\-omnipotent/\@function/;
783 $$line =~ s/\@function.*/\@function/;
85253772
AP
784 last;
785 };
c25278db 786 /\.asciz/ && do { if ($$line =~ /^"(.*)"$/) {
85253772 787 $dir = ".byte";
c25278db 788 $$line = join(",",unpack("C*",$1),0);
85253772
AP
789 }
790 last;
791 };
93c4ba07 792 /\.rva|\.long|\.quad/
c25278db
DB
793 && do { $$line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
794 $$line =~ s/\.L/$decor/g;
93c4ba07
AP
795 last;
796 };
85253772
AP
797 }
798
799 if ($gas) {
c25278db 800 $self->{value} = $dir . "\t" . $$line;
85253772
AP
801
802 if ($dir =~ /\.extern/) {
932cc129 803 $self->{value} = ""; # swallow extern
85253772
AP
804 } elsif (!$elf && $dir =~ /\.type/) {
805 $self->{value} = "";
806 $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
807 (defined($globals{$1})?".scl 2;":".scl 3;") .
808 "\t.type 32;\t.endef"
c25278db 809 if ($win64 && $$line =~ /([^,]+),\@function/);
85253772
AP
810 } elsif (!$elf && $dir =~ /\.size/) {
811 $self->{value} = "";
812 if (defined($current_function)) {
93c4ba07 813 $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
85253772
AP
814 if ($win64 && $current_function->{abi} eq "svr4");
815 undef $current_function;
816 }
817 } elsif (!$elf && $dir =~ /\.align/) {
c25278db 818 $self->{value} = ".p2align\t" . (log($$line)/log(2));
85253772 819 } elsif ($dir eq ".section") {
c25278db 820 $current_segment=$$line;
85253772
AP
821 if (!$elf && $current_segment eq ".init") {
822 if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
823 elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
824 }
825 } elsif ($dir =~ /\.(text|data)/) {
826 $current_segment=".$1";
ddc20d4d 827 } elsif ($dir =~ /\.hidden/) {
c25278db 828 if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$$line"; }
ddc20d4d
AP
829 elsif ($flavour eq "mingw64") { $self->{value} = ""; }
830 } elsif ($dir =~ /\.comm/) {
c25278db 831 $self->{value} = "$dir\t$prefix$$line";
ff6f9f96 832 $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
5d0d60e2 833 }
c25278db 834 $$line = "";
1cfd258e
AP
835 return $self;
836 }
837
85253772 838 # non-gas case or nasm/masm
1cfd258e 839 SWITCH: for ($dir) {
9b634c9b 840 /\.text/ && do { my $v=undef;
a23e3dbe 841 if ($nasm) {
9b634c9b 842 $v="section .text code align=64\n";
a23e3dbe
AP
843 } else {
844 $v="$current_segment\tENDS\n" if ($current_segment);
9b634c9b 845 $current_segment = ".text\$";
a23e3dbe 846 $v.="$current_segment\tSEGMENT ";
1b0fe79f 847 $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
a23e3dbe
AP
848 $v.=" 'CODE'";
849 }
1cfd258e
AP
850 $self->{value} = $v;
851 last;
852 };
9b634c9b
AP
853 /\.data/ && do { my $v=undef;
854 if ($nasm) {
855 $v="section .data data align=8\n";
856 } else {
857 $v="$current_segment\tENDS\n" if ($current_segment);
858 $current_segment = "_DATA";
859 $v.="$current_segment\tSEGMENT";
860 }
861 $self->{value} = $v;
862 last;
863 };
864 /\.section/ && do { my $v=undef;
c25278db
DB
865 $$line =~ s/([^,]*).*/$1/;
866 $$line = ".CRT\$XCU" if ($$line eq ".init");
9b634c9b 867 if ($nasm) {
c25278db
DB
868 $v="section $$line";
869 if ($$line=~/\.([px])data/) {
9b634c9b
AP
870 $v.=" rdata align=";
871 $v.=$1 eq "p"? 4 : 8;
c25278db 872 } elsif ($$line=~/\.CRT\$/i) {
d6522548 873 $v.=" rdata align=8";
9b634c9b
AP
874 }
875 } else {
876 $v="$current_segment\tENDS\n" if ($current_segment);
c25278db
DB
877 $v.="$$line\tSEGMENT";
878 if ($$line=~/\.([px])data/) {
9b634c9b
AP
879 $v.=" READONLY";
880 $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
c25278db 881 } elsif ($$line=~/\.CRT\$/i) {
e6903980
AP
882 $v.=" READONLY ";
883 $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
9b634c9b
AP
884 }
885 }
c25278db 886 $current_segment = $$line;
9b634c9b
AP
887 $self->{value} = $v;
888 last;
889 };
c25278db 890 /\.extern/ && do { $self->{value} = "EXTERN\t".$$line;
9b634c9b
AP
891 $self->{value} .= ":NEAR" if ($masm);
892 last;
893 };
80aa9cc9
AP
894 /\.globl|.global/
895 && do { $self->{value} = $masm?"PUBLIC":"global";
c25278db 896 $self->{value} .= "\t".$$line;
1cfd258e
AP
897 last;
898 };
5d0d60e2 899 /\.size/ && do { if (defined($current_function)) {
8fe8bae1
AP
900 undef $self->{value};
901 if ($current_function->{abi} eq "svr4") {
93c4ba07 902 $self->{value}="${decor}SEH_end_$current_function->{name}:";
8fe8bae1
AP
903 $self->{value}.=":\n" if($masm);
904 }
4010b341 905 $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
1cfd258e
AP
906 undef $current_function;
907 }
908 last;
909 };
d405aa2f 910 /\.align/ && do { my $max = ($masm && $masm>=$masmref) ? 256 : 4096;
c25278db 911 $self->{value} = "ALIGN\t".($$line>$max?$max:$$line);
d405aa2f
AP
912 last;
913 };
85253772 914 /\.(value|long|rva|quad)/
80aa9cc9 915 && do { my $sz = substr($1,0,1);
c25278db 916 my @arr = split(/,\s*/,$$line);
1cfd258e 917 my $last = pop(@arr);
a23e3dbe 918 my $conv = sub { my $var=shift;
065c5d63 919 $var=~s/^(0b[0-1]+)/oct($1)/eig;
fead2539 920 $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
85253772 921 if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
b068a9b9 922 { $var=~s/^([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
9b634c9b 923 $var;
609b0852 924 };
1cfd258e 925
85253772 926 $sz =~ tr/bvlrq/BWDDQ/;
1cfd258e 927 $self->{value} = "\tD$sz\t";
a23e3dbe
AP
928 for (@arr) { $self->{value} .= &$conv($_).","; }
929 $self->{value} .= &$conv($last);
1cfd258e
AP
930 last;
931 };
c25278db 932 /\.byte/ && do { my @str=split(/,\s*/,$$line);
e8169520 933 map(s/(0b[0-1]+)/oct($1)/eig,@str);
609b0852 934 map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
85253772 935 while ($#str>15) {
55eab3b7 936 $self->{value}.="DB\t"
85253772
AP
937 .join(",",@str[0..15])."\n";
938 foreach (0..15) { shift @str; }
d68ff710 939 }
85253772
AP
940 $self->{value}.="DB\t"
941 .join(",",@str) if (@str);
d68ff710
AP
942 last;
943 };
c25278db 944 /\.comm/ && do { my @str=split(/,\s*/,$$line);
ddc20d4d
AP
945 my $v=undef;
946 if ($nasm) {
301799b8 947 $v.="common $prefix@str[0] @str[1]";
ddc20d4d
AP
948 } else {
949 $v="$current_segment\tENDS\n" if ($current_segment);
94c64f9a 950 $current_segment = "_DATA";
ddc20d4d
AP
951 $v.="$current_segment\tSEGMENT\n";
952 $v.="COMM @str[0]:DWORD:".@str[1]/4;
953 }
954 $self->{value} = $v;
955 last;
956 };
1cfd258e 957 }
c25278db 958 $$line = "";
1cfd258e
AP
959 }
960
961 $ret;
962 }
963 sub out {
964 my $self = shift;
965 $self->{value};
966 }
967}
968
1eb12c43
AP
969# Upon initial x86_64 introduction SSE>2 extensions were not introduced
970# yet. In order not to be bothered by tracing exact assembler versions,
971# but at the same time to provide a bare security minimum of AES-NI, we
972# hard-code some instructions. Extensions past AES-NI on the other hand
973# are traced by examining assembler version in individual perlasm
974# modules...
975
fead2539
AP
976my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
977 "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 );
978
e09b6216
AP
979sub rex {
980 my $opcode=shift;
981 my ($dst,$src,$rex)=@_;
982
983 $rex|=0x04 if($dst>=8);
984 $rex|=0x01 if($src>=8);
985 push @$opcode,($rex|0x40) if ($rex);
986}
987
c7b903e0
AP
988my $movq = sub { # elderly gas can't handle inter-register movq
989 my $arg = shift;
990 my @opcode=(0x66);
a87ff751 991 if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
c7b903e0
AP
992 my ($src,$dst)=($1,$2);
993 if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
994 rex(\@opcode,$src,$dst,0x8);
995 push @opcode,0x0f,0x7e;
996 push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
997 @opcode;
a87ff751 998 } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
c7b903e0
AP
999 my ($src,$dst)=($2,$1);
1000 if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
1001 rex(\@opcode,$src,$dst,0x8);
1002 push @opcode,0x0f,0x6e;
1003 push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
1004 @opcode;
1005 } else {
1006 ();
1007 }
1008};
1009
fead2539 1010my $pextrd = sub {
a87ff751 1011 if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
fead2539 1012 my @opcode=(0x66);
c25278db
DB
1013 my $imm=$1;
1014 my $src=$2;
1015 my $dst=$3;
fead2539
AP
1016 if ($dst =~ /%r([0-9]+)d/) { $dst = $1; }
1017 elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; }
1018 rex(\@opcode,$src,$dst);
1019 push @opcode,0x0f,0x3a,0x16;
1020 push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
1021 push @opcode,$imm;
a9e790b9 1022 @opcode;
fead2539 1023 } else {
a9e790b9 1024 ();
fead2539 1025 }
a9e790b9 1026};
fead2539
AP
1027
1028my $pinsrd = sub {
a87ff751 1029 if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
fead2539 1030 my @opcode=(0x66);
c25278db
DB
1031 my $imm=$1;
1032 my $src=$2;
1033 my $dst=$3;
b5c6aab5 1034 if ($src =~ /%r([0-9]+)/) { $src = $1; }
fead2539
AP
1035 elsif ($src =~ /%e/) { $src = $regrm{$src}; }
1036 rex(\@opcode,$dst,$src);
1037 push @opcode,0x0f,0x3a,0x22;
1038 push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M
1039 push @opcode,$imm;
a9e790b9 1040 @opcode;
fead2539 1041 } else {
a9e790b9 1042 ();
fead2539 1043 }
a9e790b9 1044};
fead2539
AP
1045
1046my $pshufb = sub {
a87ff751 1047 if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
fead2539 1048 my @opcode=(0x66);
a9e790b9 1049 rex(\@opcode,$2,$1);
fead2539 1050 push @opcode,0x0f,0x38,0x00;
a9e790b9
AP
1051 push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
1052 @opcode;
fead2539 1053 } else {
a9e790b9 1054 ();
fead2539 1055 }
a9e790b9 1056};
fead2539 1057
b5c6aab5 1058my $palignr = sub {
a87ff751 1059 if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
b5c6aab5
AP
1060 my @opcode=(0x66);
1061 rex(\@opcode,$3,$2);
1062 push @opcode,0x0f,0x3a,0x0f;
1063 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1064 push @opcode,$1;
1065 @opcode;
1066 } else {
1067 ();
1068 }
1069};
1070
1071my $pclmulqdq = sub {
1072 if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1073 my @opcode=(0x66);
1074 rex(\@opcode,$3,$2);
1075 push @opcode,0x0f,0x3a,0x44;
1076 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1077 my $c=$1;
1078 push @opcode,$c=~/^0/?oct($c):$c;
1079 @opcode;
1080 } else {
1081 ();
1082 }
1083};
1084
301799b8
AP
1085my $rdrand = sub {
1086 if (shift =~ /%[er](\w+)/) {
1087 my @opcode=();
1088 my $dst=$1;
1089 if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
9c940446 1090 rex(\@opcode,0,$dst,8);
301799b8
AP
1091 push @opcode,0x0f,0xc7,0xf0|($dst&7);
1092 @opcode;
1093 } else {
1094 ();
1095 }
1096};
1097
f4d45640
AP
1098my $rdseed = sub {
1099 if (shift =~ /%[er](\w+)/) {
1100 my @opcode=();
1101 my $dst=$1;
1102 if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
9c940446 1103 rex(\@opcode,0,$dst,8);
f4d45640
AP
1104 push @opcode,0x0f,0xc7,0xf8|($dst&7);
1105 @opcode;
1106 } else {
1107 ();
1108 }
1109};
1110
e09b6216
AP
1111# Not all AVX-capable assemblers recognize AMD XOP extension. Since we
1112# are using only two instructions hand-code them in order to be excused
1113# from chasing assembler versions...
1114
f6ff1aa8 1115sub rxb {
c25278db 1116 my $opcode=shift;
f6ff1aa8
AP
1117 my ($dst,$src1,$src2,$rxb)=@_;
1118
1119 $rxb|=0x7<<5;
1120 $rxb&=~(0x04<<5) if($dst>=8);
1121 $rxb&=~(0x01<<5) if($src1>=8);
1122 $rxb&=~(0x02<<5) if($src2>=8);
c25278db 1123 push @$opcode,$rxb;
f6ff1aa8
AP
1124}
1125
1126my $vprotd = sub {
1127 if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1128 my @opcode=(0x8f);
1129 rxb(\@opcode,$3,$2,-1,0x08);
1130 push @opcode,0x78,0xc2;
1131 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1132 my $c=$1;
1133 push @opcode,$c=~/^0/?oct($c):$c;
1134 @opcode;
1135 } else {
1136 ();
1137 }
1138};
1139
1140my $vprotq = sub {
1141 if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1142 my @opcode=(0x8f);
1143 rxb(\@opcode,$3,$2,-1,0x08);
1144 push @opcode,0x78,0xc3;
1145 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1146 my $c=$1;
1147 push @opcode,$c=~/^0/?oct($c):$c;
1148 @opcode;
1149 } else {
1150 ();
1151 }
1152};
1153
e09b6216
AP
1154# Intel Control-flow Enforcement Technology extension. All functions and
1155# indirect branch targets will have to start with this instruction...
1156
4e3d2866
AP
1157my $endbranch = sub {
1158 (0xf3,0x0f,0x1e,0xfa);
1159};
1160
e09b6216
AP
1161########################################################################
1162
9b634c9b
AP
1163if ($nasm) {
1164 print <<___;
1165default rel
a9e790b9 1166%define XMMWORD
1b0fe79f
AP
1167%define YMMWORD
1168%define ZMMWORD
9b634c9b
AP
1169___
1170} elsif ($masm) {
1171 print <<___;
1172OPTION DOTNAME
1173___
1174}
c25278db 1175while(defined(my $line=<>)) {
1cfd258e 1176
9ba96fbb 1177 $line =~ s|\R$||; # Better chomp
1cfd258e 1178
4b450519
AP
1179 $line =~ s|[#!].*$||; # get rid of asm-style comments...
1180 $line =~ s|/\*.*\*/||; # ... and C-style comments...
1181 $line =~ s|^\s+||; # ... and skip white spaces in beginning
41965a84 1182 $line =~ s|\s+$||; # ... and at the end
1cfd258e 1183
c25278db 1184 if (my $label=label->re(\$line)) { print $label->out(); }
1cfd258e 1185
c25278db
DB
1186 if (my $directive=directive->re(\$line)) {
1187 printf "%s",$directive->out();
1188 } elsif (my $opcode=opcode->re(\$line)) {
a9e790b9 1189 my $asm = eval("\$".$opcode->mnemonic());
609b0852 1190
c25278db 1191 if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
a9e790b9
AP
1192 print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
1193 next;
1194 }
1195
c25278db 1196 my @args;
a9e790b9 1197 ARGUMENT: while (1) {
6a4ea002 1198 my $arg;
1cfd258e 1199
6a4ea002
AP
1200 ($arg=register->re(\$line, $opcode))||
1201 ($arg=const->re(\$line)) ||
1202 ($arg=ea->re(\$line, $opcode)) ||
1203 ($arg=expr->re(\$line, $opcode)) ||
1204 last ARGUMENT;
1cfd258e 1205
6a4ea002 1206 push @args,$arg;
1cfd258e 1207
6a4ea002 1208 last ARGUMENT if ($line !~ /^,/);
1cfd258e 1209
6a4ea002 1210 $line =~ s/^,\s*//;
1cfd258e
AP
1211 } # ARGUMENT:
1212
4db48824
AP
1213 if ($#args>=0) {
1214 my $insn;
c25278db 1215 my $sz=$opcode->size();
a9e790b9 1216
85253772 1217 if ($gas) {
4db48824 1218 $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
fead2539 1219 @args = map($_->out($sz),@args);
a9e790b9 1220 printf "\t%s\t%s",$insn,join(",",@args);
5d0d60e2 1221 } else {
4db48824 1222 $insn = $opcode->out();
046ea308
AP
1223 foreach (@args) {
1224 my $arg = $_->out();
a9e790b9
AP
1225 # $insn.=$sz compensates for movq, pinsrw, ...
1226 if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
a9d14832 1227 if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
1b0fe79f 1228 if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
a9e790b9 1229 if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; }
046ea308 1230 }
4db48824 1231 @args = reverse(@args);
a23e3dbe 1232 undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
fead2539 1233 printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
1cfd258e 1234 }
1cfd258e
AP
1235 } else {
1236 printf "\t%s",$opcode->out();
1237 }
1238 }
1239
1240 print $line,"\n";
1241}
1242
51994e50 1243print "$cet_property" if ($cet_property);
85253772
AP
1244print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
1245print "END\n" if ($masm);
1cfd258e 1246
a21314db 1247close STDOUT or die "error closing STDOUT: $!;"
1cfd258e 1248
80aa9cc9 1249\f#################################################
1cfd258e
AP
1250# Cross-reference x86_64 ABI "card"
1251#
1252# Unix Win64
1253# %rax * *
1254# %rbx - -
1255# %rcx #4 #1
1256# %rdx #3 #2
1257# %rsi #2 -
1258# %rdi #1 -
1259# %rbp - -
1260# %rsp - -
1261# %r8 #5 #3
1262# %r9 #6 #4
1263# %r10 * *
1264# %r11 * *
1265# %r12 - -
1266# %r13 - -
1267# %r14 - -
1268# %r15 - -
609b0852 1269#
1cfd258e
AP
1270# (*) volatile register
1271# (-) preserved by callee
1272# (#) Nth argument, volatile
1273#
1274# In Unix terms top of stack is argument transfer area for arguments
0d4fb843 1275# which could not be accommodated in registers. Or in other words 7th
1cfd258e
AP
1276# [integer] argument resides at 8(%rsp) upon function entry point.
1277# 128 bytes above %rsp constitute a "red zone" which is not touched
1278# by signal handlers and can be used as temporal storage without
1279# allocating a frame.
1280#
1281# In Win64 terms N*8 bytes on top of stack is argument transfer area,
1282# which belongs to/can be overwritten by callee. N is the number of
1283# arguments passed to callee, *but* not less than 4! This means that
1284# upon function entry point 5th argument resides at 40(%rsp), as well
1285# as that 32 bytes from 8(%rsp) can always be used as temporal
d256b957
AP
1286# storage [without allocating a frame]. One can actually argue that
1287# one can assume a "red zone" above stack pointer under Win64 as well.
6a3a7f30
AP
1288# Point is that at apparently no occasion Windows kernel would alter
1289# the area above user stack pointer in true asynchronous manner...
1cfd258e
AP
1290#
1291# All the above means that if assembler programmer adheres to Unix
7e12cdb5 1292# register and stack layout, but disregards the "red zone" existence,
1cfd258e
AP
1293# it's possible to use following prologue and epilogue to "gear" from
1294# Unix to Win64 ABI in leaf functions with not more than 6 arguments.
1295#
1296# omnipotent_function:
1297# ifdef WIN64
1298# movq %rdi,8(%rsp)
1299# movq %rsi,16(%rsp)
1300# movq %rcx,%rdi ; if 1st argument is actually present
1301# movq %rdx,%rsi ; if 2nd argument is actually ...
1302# movq %r8,%rdx ; if 3rd argument is ...
1303# movq %r9,%rcx ; if 4th argument ...
1304# movq 40(%rsp),%r8 ; if 5th ...
1305# movq 48(%rsp),%r9 ; if 6th ...
1306# endif
1307# ...
1308# ifdef WIN64
1309# movq 8(%rsp),%rdi
1310# movq 16(%rsp),%rsi
1311# endif
1312# ret
4f469342 1313#
80aa9cc9
AP
1314\f#################################################
1315# Win64 SEH, Structured Exception Handling.
1316#
4f469342
AP
1317# Unlike on Unix systems(*) lack of Win64 stack unwinding information
1318# has undesired side-effect at run-time: if an exception is raised in
1319# assembler subroutine such as those in question (basically we're
1320# referring to segmentation violations caused by malformed input
1321# parameters), the application is briskly terminated without invoking
1322# any exception handlers, most notably without generating memory dump
1323# or any user notification whatsoever. This poses a problem. It's
1324# possible to address it by registering custom language-specific
1325# handler that would restore processor context to the state at
1326# subroutine entry point and return "exception is not handled, keep
1327# unwinding" code. Writing such handler can be a challenge... But it's
1328# doable, though requires certain coding convention. Consider following
1329# snippet:
1330#
80aa9cc9 1331# .type function,@function
4f469342
AP
1332# function:
1333# movq %rsp,%rax # copy rsp to volatile register
1334# pushq %r15 # save non-volatile registers
1335# pushq %rbx
1336# pushq %rbp
1337# movq %rsp,%r11
1338# subq %rdi,%r11 # prepare [variable] stack frame
1339# andq $-64,%r11
1340# movq %rax,0(%r11) # check for exceptions
1341# movq %r11,%rsp # allocate [variable] stack frame
1342# movq %rax,0(%rsp) # save original rsp value
1343# magic_point:
1344# ...
1345# movq 0(%rsp),%rcx # pull original rsp value
1346# movq -24(%rcx),%rbp # restore non-volatile registers
1347# movq -16(%rcx),%rbx
1348# movq -8(%rcx),%r15
1349# movq %rcx,%rsp # restore original rsp
e1dbf7f4 1350# magic_epilogue:
4f469342 1351# ret
80aa9cc9 1352# .size function,.-function
4f469342
AP
1353#
1354# The key is that up to magic_point copy of original rsp value remains
1355# in chosen volatile register and no non-volatile register, except for
1356# rsp, is modified. While past magic_point rsp remains constant till
1357# the very end of the function. In this case custom language-specific
1358# exception handler would look like this:
1359#
1360# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1361# CONTEXT *context,DISPATCHER_CONTEXT *disp)
80aa9cc9 1362# { ULONG64 *rsp = (ULONG64 *)context->Rax;
e1dbf7f4
AP
1363# ULONG64 rip = context->Rip;
1364#
1365# if (rip >= magic_point)
1366# { rsp = (ULONG64 *)context->Rsp;
1367# if (rip < magic_epilogue)
1368# { rsp = (ULONG64 *)rsp[0];
1369# context->Rbp = rsp[-3];
1370# context->Rbx = rsp[-2];
1371# context->R15 = rsp[-1];
1372# }
4f469342
AP
1373# }
1374# context->Rsp = (ULONG64)rsp;
1375# context->Rdi = rsp[1];
1376# context->Rsi = rsp[2];
1377#
1378# memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
1379# RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
1380# dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
1381# &disp->HandlerData,&disp->EstablisherFrame,NULL);
1382# return ExceptionContinueSearch;
1383# }
1384#
1385# It's appropriate to implement this handler in assembler, directly in
1386# function's module. In order to do that one has to know members'
1387# offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
1388# values. Here they are:
1389#
1390# CONTEXT.Rax 120
1391# CONTEXT.Rcx 128
1392# CONTEXT.Rdx 136
1393# CONTEXT.Rbx 144
1394# CONTEXT.Rsp 152
1395# CONTEXT.Rbp 160
1396# CONTEXT.Rsi 168
1397# CONTEXT.Rdi 176
1398# CONTEXT.R8 184
1399# CONTEXT.R9 192
1400# CONTEXT.R10 200
1401# CONTEXT.R11 208
1402# CONTEXT.R12 216
1403# CONTEXT.R13 224
1404# CONTEXT.R14 232
1405# CONTEXT.R15 240
1406# CONTEXT.Rip 248
6dd9066e 1407# CONTEXT.Xmm6 512
4f469342
AP
1408# sizeof(CONTEXT) 1232
1409# DISPATCHER_CONTEXT.ControlPc 0
1410# DISPATCHER_CONTEXT.ImageBase 8
1411# DISPATCHER_CONTEXT.FunctionEntry 16
1412# DISPATCHER_CONTEXT.EstablisherFrame 24
1413# DISPATCHER_CONTEXT.TargetIp 32
1414# DISPATCHER_CONTEXT.ContextRecord 40
1415# DISPATCHER_CONTEXT.LanguageHandler 48
1416# DISPATCHER_CONTEXT.HandlerData 56
1417# UNW_FLAG_NHANDLER 0
1418# ExceptionContinueSearch 1
1419#
80aa9cc9
AP
1420# In order to tie the handler to the function one has to compose
1421# couple of structures: one for .xdata segment and one for .pdata.
1422#
4f469342 1423# UNWIND_INFO structure for .xdata segment would be
80aa9cc9
AP
1424#
1425# function_unwind_info:
1426# .byte 9,0,0,0
85253772 1427# .rva handler
80aa9cc9
AP
1428#
1429# This structure designates exception handler for a function with
1430# zero-length prologue, no stack frame or frame register.
1431#
1432# To facilitate composing of .pdata structures, auto-generated "gear"
1433# prologue copies rsp value to rax and denotes next instruction with
85253772 1434# .LSEH_begin_{function_name} label. This essentially defines the SEH
80aa9cc9
AP
1435# styling rule mentioned in the beginning. Position of this label is
1436# chosen in such manner that possible exceptions raised in the "gear"
1437# prologue would be accounted to caller and unwound from latter's frame.
85253772 1438# End of function is marked with respective .LSEH_end_{function_name}
80aa9cc9
AP
1439# label. To summarize, .pdata segment would contain
1440#
85253772
AP
1441# .rva .LSEH_begin_function
1442# .rva .LSEH_end_function
1443# .rva function_unwind_info
80aa9cc9 1444#
478b50cf 1445# Reference to function_unwind_info from .xdata segment is the anchor.
85253772 1446# In case you wonder why references are 32-bit .rvas and not 64-bit
80aa9cc9
AP
1447# .quads. References put into these two segments are required to be
1448# *relative* to the base address of the current binary module, a.k.a.
1449# image base. No Win64 module, be it .exe or .dll, can be larger than
1450# 2GB and thus such relative references can be and are accommodated in
1451# 32 bits.
1452#
1453# Having reviewed the example function code, one can argue that "movq
1454# %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
1455# rax would contain an undefined value. If this "offends" you, use
1456# another register and refrain from modifying rax till magic_point is
1457# reached, i.e. as if it was a non-volatile register. If more registers
1458# are required prior [variable] frame setup is completed, note that
1459# nobody says that you can have only one "magic point." You can
1460# "liberate" non-volatile registers by denoting last stack off-load
1461# instruction and reflecting it in finer grade unwind logic in handler.
1462# After all, isn't it why it's called *language-specific* handler...
1463#
e1dbf7f4
AP
1464# SE handlers are also involved in unwinding stack when executable is
1465# profiled or debugged. Profiling implies additional limitations that
1466# are too subtle to discuss here. For now it's sufficient to say that
1467# in order to simplify handlers one should either a) offload original
1468# %rsp to stack (like discussed above); or b) if you have a register to
1469# spare for frame pointer, choose volatile one.
4f469342
AP
1470#
1471# (*) Note that we're talking about run-time, not debug-time. Lack of
1472# unwind information makes debugging hard on both Windows and
46f4e1be 1473# Unix. "Unlike" refers to the fact that on Unix signal handler
4f469342
AP
1474# will always be invoked, core dumped and appropriate exit code
1475# returned to parent (for user notification).