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