xref: /freebsd/crypto/openssl/crypto/perlasm/x86_64-xlate.pl (revision 10a428653ee7216475f1ddce3fb4cbf1200319f8)
1#! /usr/bin/env perl
2# Copyright 2005-2026 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://gitlab.com/x86-psABIs/x86-64-ABI.
667    my %DW_reg_idx = (
668	"%rax"=>0,  "%rdx"=>1,  "%rcx"=>2,  "%rbx"=>3,
669	"%rsi"=>4,  "%rdi"=>5,  "%rbp"=>6,  "%rsp"=>7,
670	"%r8" =>8,  "%r9" =>9,  "%r10"=>10, "%r11"=>11,
671	"%r12"=>12, "%r13"=>13, "%r14"=>14, "%r15"=>15
672	);
673
674    my ($cfa_reg, $cfa_rsp);
675    my @cfa_stack;
676
677    # [us]leb128 format is variable-length integer representation base
678    # 2^128, with most significant bit of each byte being 0 denoting
679    # *last* most significant digit. See "Variable Length Data" in the
680    # DWARF specification, numbered 7.6 at least in versions 3 and 4.
681    sub sleb128 {
682	use integer;	# get right shift extend sign
683
684	my $val = shift;
685	my $sign = ($val < 0) ? -1 : 0;
686	my @ret = ();
687
688	while(1) {
689	    push @ret, $val&0x7f;
690
691	    # see if remaining bits are same and equal to most
692	    # significant bit of the current digit, if so, it's
693	    # last digit...
694	    last if (($val>>6) == $sign);
695
696	    @ret[-1] |= 0x80;
697	    $val >>= 7;
698	}
699
700	return @ret;
701    }
702    sub uleb128 {
703	my $val = shift;
704	my @ret = ();
705
706	while(1) {
707	    push @ret, $val&0x7f;
708
709	    # see if it's last significant digit...
710	    last if (($val >>= 7) == 0);
711
712	    @ret[-1] |= 0x80;
713	}
714
715	return @ret;
716    }
717    sub const {
718	my $val = shift;
719
720	if ($val >= 0 && $val < 32) {
721            return ($DW_OP_complex{lit0}+$val);
722	}
723	return ($DW_OP_complex{consts}, sleb128($val));
724    }
725    sub reg {
726	my $val = shift;
727
728	return if ($val !~ m/^(%r\w+)(?:([\+\-])((?:0x)?[0-9a-f]+))?/);
729
730	my $reg = $DW_reg_idx{$1};
731	my $off = eval ("0 $2 $3");
732
733	return (($DW_OP_complex{breg0} + $reg), sleb128($off));
734	# Yes, we use DW_OP_bregX+0 to push register value and not
735	# DW_OP_regX, because latter would require even DW_OP_piece,
736	# which would be a waste under the circumstances. If you have
737	# to use DWP_OP_reg, use "regx:N"...
738    }
739    sub cfa_expression {
740	my $line = shift;
741	my @ret;
742
743	foreach my $token (split(/,\s*/,$line)) {
744	    if ($token =~ /^%r/) {
745		push @ret,reg($token);
746	    } elsif ($token =~ /((?:0x)?[0-9a-f]+)\((%r\w+)\)/) {
747		push @ret,reg("$2+$1");
748	    } elsif ($token =~ /(\w+):(\-?(?:0x)?[0-9a-f]+)(U?)/i) {
749		my $i = 1*eval($2);
750		push @ret,$DW_OP_complex{$1}, ($3 ? uleb128($i) : sleb128($i));
751	    } elsif (my $i = 1*eval($token) or $token eq "0") {
752		if ($token =~ /^\+/) {
753		    push @ret,$DW_OP_complex{plus_uconst},uleb128($i);
754		} else {
755		    push @ret,const($i);
756		}
757	    } else {
758		push @ret,$DW_OP_simple{$token};
759	    }
760	}
761
762	# Finally we return DW_CFA_def_cfa_expression, 15, followed by
763	# length of the expression and of course the expression itself.
764	return (15,scalar(@ret),@ret);
765    }
766    sub re {
767	my	($class, $line) = @_;
768	my	$self = {};
769	my	$ret;
770
771	if ($$line =~ s/^\s*\.cfi_(\w+)\s*//) {
772	    bless $self,$class;
773	    $ret = $self;
774	    undef $self->{value};
775	    my $dir = $1;
776
777	    SWITCH: for ($dir) {
778	    # What is $cfa_rsp? Effectively it's difference between %rsp
779	    # value and current CFA, Canonical Frame Address, which is
780	    # why it starts with -8. Recall that CFA is top of caller's
781	    # stack...
782	    /startproc/	&& do {	($cfa_reg, $cfa_rsp) = ("%rsp", -8); last; };
783	    /endproc/	&& do {	($cfa_reg, $cfa_rsp) = ("%rsp",  0);
784				# .cfi_remember_state directives that are not
785				# matched with .cfi_restore_state are
786				# unnecessary.
787				die "unpaired .cfi_remember_state" if (@cfa_stack);
788				last;
789			      };
790	    /def_cfa_register/
791			&& do {	$cfa_reg = $$line; last; };
792	    /def_cfa_offset/
793			&& do {	$cfa_rsp = -1*eval($$line) if ($cfa_reg eq "%rsp");
794				last;
795			      };
796	    /adjust_cfa_offset/
797			&& do {	$cfa_rsp -= 1*eval($$line) if ($cfa_reg eq "%rsp");
798				last;
799			      };
800	    /def_cfa/	&& do {	if ($$line =~ /(%r\w+)\s*,\s*(.+)/) {
801				    $cfa_reg = $1;
802				    $cfa_rsp = -1*eval($2) if ($cfa_reg eq "%rsp");
803				}
804				last;
805			      };
806	    /push/	&& do {	$dir = undef;
807				$cfa_rsp -= 8;
808				if ($cfa_reg eq "%rsp") {
809				    $self->{value} = ".cfi_adjust_cfa_offset\t8\n";
810				}
811				$self->{value} .= ".cfi_offset\t$$line,$cfa_rsp";
812				last;
813			      };
814	    /pop/	&& do {	$dir = undef;
815				$cfa_rsp += 8;
816				if ($cfa_reg eq "%rsp") {
817				    $self->{value} = ".cfi_adjust_cfa_offset\t-8\n";
818				}
819				$self->{value} .= ".cfi_restore\t$$line";
820				last;
821			      };
822	    /cfa_expression/
823			&& do {	$dir = undef;
824				$self->{value} = ".cfi_escape\t" .
825					join(",", map(sprintf("0x%02x", $_),
826						      cfa_expression($$line)));
827				last;
828			      };
829	    /remember_state/
830			&& do {	push @cfa_stack, [$cfa_reg, $cfa_rsp];
831				last;
832			      };
833	    /restore_state/
834			&& do {	($cfa_reg, $cfa_rsp) = @{pop @cfa_stack};
835				last;
836			      };
837	    }
838
839	    $self->{value} = ".cfi_$dir\t$$line" if ($dir);
840
841	    $$line = "";
842	}
843
844	return $ret;
845    }
846    sub out {
847	my $self = shift;
848	return ($elf ? $self->{value} : undef);
849    }
850}
851{ package directive;	# pick up directives, which start with .
852    sub re {
853	my	($class, $line) = @_;
854	my	$self = {};
855	my	$ret;
856	my	$dir;
857
858	# chain-call to cfi_directive
859	$ret = cfi_directive->re($line) and return $ret;
860
861	if ($$line =~ /^\s*(\.\w+)/) {
862	    bless $self,$class;
863	    $dir = $1;
864	    $ret = $self;
865	    undef $self->{value};
866	    $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
867
868	    SWITCH: for ($dir) {
869		/\.global|\.globl|\.extern/
870			    && do { $globals{$$line} = $prefix . $$line;
871				    $$line = $globals{$$line} if ($prefix);
872				    last;
873				  };
874		/\.type/    && do { my ($sym,$type,$narg) = split(',',$$line);
875				    if ($type eq "\@function") {
876					undef $current_function;
877					$current_function->{name} = $sym;
878					$current_function->{abi}  = "svr4";
879					$current_function->{narg} = $narg;
880					$current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
881				    } elsif ($type eq "\@abi-omnipotent") {
882					undef $current_function;
883					$current_function->{name} = $sym;
884					$current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
885				    }
886				    $$line =~ s/\@abi\-omnipotent/\@function/;
887				    $$line =~ s/\@function.*/\@function/;
888				    last;
889				  };
890		/\.asciz/   && do { if ($$line =~ /^"(.*)"$/) {
891					$dir  = ".byte";
892					$$line = join(",",unpack("C*",$1),0);
893				    }
894				    last;
895				  };
896		/\.rva|\.long|\.quad|\.byte/
897			    && do { $$line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
898				    $$line =~ s/\.L/$decor/g;
899				    last;
900				  };
901	    }
902
903	    if ($gas) {
904		$self->{value} = $dir . "\t" . $$line;
905
906		if ($dir =~ /\.extern/) {
907		    $self->{value} = ""; # swallow extern
908		} elsif (!$elf && $dir =~ /\.type/) {
909		    $self->{value} = "";
910		    $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
911				(defined($globals{$1})?".scl 2;":".scl 3;") .
912				"\t.type 32;\t.endef"
913				if ($win64 && $$line =~ /([^,]+),\@function/);
914		} elsif (!$elf && $dir =~ /\.size/) {
915		    $self->{value} = "";
916		    if (defined($current_function)) {
917			$self->{value} .= "${decor}SEH_end_$current_function->{name}:"
918				if ($win64 && $current_function->{abi} eq "svr4");
919			undef $current_function;
920		    }
921		} elsif (!$elf && $dir =~ /\.align/) {
922		    $self->{value} = ".p2align\t" . (log($$line)/log(2));
923		} elsif ($dir eq ".section") {
924		    #
925		    # get rid off align option, it's not supported/tolerated
926		    # by gcc. openssl project introduced the option as an aid
927		    # to deal with nasm/masm assembly.
928		    #
929		    $self->{value} =~ s/(.+)\s+align\s*=.*$/$1/;
930                    $current_segment = pop(@segment_stack);
931                    if (not $current_segment) {
932                        # if no previous section is defined, then assume .text
933                        # so code does not land in .data section by accident.
934                        # this deals with inconsistency of perl-assembly files.
935                        push(@segment_stack, ".text");
936                    }
937		    #
938		    # $$line may still contains align= option. We do care
939		    # about section type here.
940		    #
941		    $current_segment = $$line;
942		    $current_segment =~ s/([^\s]+).*$/$1/;
943                    push(@segment_stack, $current_segment);
944		    if (!$elf && $current_segment eq ".rodata") {
945			if	($flavour eq "macosx") { $self->{value} = ".section\t__DATA,__const"; }
946			elsif	($flavour eq "mingw64")	{ $self->{value} = ".section\t.rodata"; }
947		    }
948		    if (!$elf && $current_segment eq ".init") {
949			if	($flavour eq "macosx")	{ $self->{value} = ".mod_init_func"; }
950			elsif	($flavour eq "mingw64")	{ $self->{value} = ".section\t.ctors"; }
951		    }
952		} elsif ($dir =~ /\.(text|data)/) {
953                    $current_segment = pop(@segment_stack);
954                    if (not $current_segment) {
955                        # if no previous section is defined, then assume .text
956                        # so code does not land in .data section by accident.
957                        # this deals with inconsistency of perl-assembly files.
958                        push(@segment_stack, ".text");
959                    }
960		    $current_segment=".$1";
961		    push(@segment_stack, $current_segment);
962		} elsif ($dir =~ /\.hidden/) {
963		    if    ($flavour eq "macosx")  { $self->{value} = ".private_extern\t$prefix$$line"; }
964		    elsif ($flavour eq "mingw64") { $self->{value} = ""; }
965		} elsif ($dir =~ /\.comm/) {
966		    $self->{value} = "$dir\t$prefix$$line";
967		    $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
968		} elsif ($dir =~ /\.previous/) {
969                    pop(@segment_stack); #pop ourselves
970                    # just peek at the top of the stack here
971                    $current_segment = @segment_stack[0];
972                    if (not $current_segment) {
973                        # if no previous segment was defined assume .text so
974                        # the code does not accidentally land in .data section.
975                        $current_segment = ".text";
976                        push(@segment_stack, $current_segment);
977                    }
978                    if ($flavour eq "mingw64" || $flavour eq "macosx") {
979		        $self->{value} = $current_segment;
980                    }
981		}
982		$$line = "";
983		return $self;
984	    }
985
986	    # non-gas case or nasm/masm
987	    SWITCH: for ($dir) {
988		/\.text/    && do { my $v=undef;
989				    if ($nasm) {
990					$current_segment = pop(@segment_stack);
991					if (not $current_segment) {
992					    push(@segment_stack, ".text");
993				        }
994					$v="section	.text code align=64\n";
995					$current_segment = ".text";
996					push(@segment_stack, $current_segment);
997				    } else {
998					$current_segment = pop(@segment_stack);
999					if (not $current_segment) {
1000					    push(@segment_stack, ".text\$");
1001				        }
1002					$v="$current_segment\tENDS\n" if ($current_segment);
1003					$current_segment = ".text\$";
1004					push(@segment_stack, $current_segment);
1005					$v.="$current_segment\tSEGMENT ";
1006					$v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
1007					$v.=" 'CODE'";
1008				    }
1009				    $self->{value} = $v;
1010				    last;
1011				  };
1012		/\.data/    && do { my $v=undef;
1013				    if ($nasm) {
1014					$v="section	.data data align=8\n";
1015				    } else {
1016					$current_segment = pop(@segment_stack);
1017					$v="$current_segment\tENDS\n" if ($current_segment);
1018					$current_segment = "_DATA";
1019					push(@segment_stack, $current_segment);
1020					$v.="$current_segment\tSEGMENT";
1021				    }
1022				    $self->{value} = $v;
1023				    last;
1024				  };
1025		/\.section/ && do { my $v=undef;
1026				    my $align=undef;
1027				    #
1028				    # $$line may currently contain something like this
1029				    #	.rodata align = 64
1030				    # align part is optional
1031				    #
1032				    $align = $$line;
1033				    $align =~ s/(.*)(align\s*=\s*\d+$)/$2/;
1034				    $$line =~ s/(.*)(\s+align\s*=\s*\d+$)/$1/;
1035				    $$line =~ s/,.*//;
1036				    $$line = ".CRT\$XCU" if ($$line eq ".init");
1037				    $$line = ".rdata" if ($$line eq ".rodata");
1038				    if ($nasm) {
1039					$current_segment = pop(@segment_stack);
1040					if (not $current_segment) {
1041					    #
1042					    # This is a hack which deals with ecp_nistz256-x86_64.pl,
1043					    # The precomputed curve is stored in the first section
1044					    # in .asm file. Pushing extra .text section here
1045					    # allows our poor man's solution to stick to assumption
1046					    # .text section is always the first.
1047					    #
1048					    push(@segment_stack, ".text");
1049					}
1050					$v="section	$$line";
1051					if ($$line=~/\.([prx])data/) {
1052					    if ($align =~ /align\s*=\s*(\d+)/) {
1053						$v.= " rdata align=$1" ;
1054					    } else {
1055						$v.=" rdata align=";
1056						$v.=$1 eq "p"? 4 : 8;
1057					    }
1058					} elsif ($$line=~/\.CRT\$/i) {
1059					    $v.=" rdata align=8";
1060					}
1061				    } else {
1062					$current_segment = pop(@segment_stack);
1063					if (not $current_segment) {
1064					    #
1065					    # same hack for masm to keep ecp_nistz256-x86_64.pl
1066					    # happy.
1067					    #
1068					    push(@segment_stack, ".text\$");
1069				        }
1070					$v="$current_segment\tENDS\n" if ($current_segment);
1071					$v.="$$line\tSEGMENT";
1072					if ($$line=~/\.([prx])data/) {
1073					    $v.=" READONLY";
1074					    if ($align =~ /align\s*=\s*(\d+)$/) {
1075						$v.=" ALIGN($1)" if ($masm>=$masmref);
1076					    } else {
1077						$v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
1078					    }
1079					} elsif ($$line=~/\.CRT\$/i) {
1080					    $v.=" READONLY ";
1081					    $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
1082					}
1083				    }
1084				    $current_segment = $$line;
1085				    push(@segment_stack, $$line);
1086				    $self->{value} = $v;
1087				    last;
1088				  };
1089		/\.extern/  && do { $self->{value}  = "EXTERN\t".$$line;
1090				    $self->{value} .= ":NEAR" if ($masm);
1091				    last;
1092				  };
1093		/\.globl|.global/
1094			    && do { $self->{value}  = $masm?"PUBLIC":"global";
1095				    $self->{value} .= "\t".$$line;
1096				    last;
1097				  };
1098		/\.size/    && do { if (defined($current_function)) {
1099					undef $self->{value};
1100					if ($current_function->{abi} eq "svr4") {
1101					    $self->{value}="${decor}SEH_end_$current_function->{name}:";
1102					    $self->{value}.=":\n" if($masm);
1103					}
1104					$self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
1105					undef $current_function;
1106				    }
1107				    last;
1108				  };
1109		/\.align/   && do { my $max = ($masm && $masm>=$masmref) ? 256 : 4096;
1110				    $self->{value} = "ALIGN\t".($$line>$max?$max:$$line);
1111				    last;
1112				  };
1113		/\.(value|long|rva|quad)/
1114			    && do { my $sz  = substr($1,0,1);
1115				    my @arr = split(/,\s*/,$$line);
1116				    my $last = pop(@arr);
1117				    my $conv = sub  {	my $var=shift;
1118							$var=~s/^(0b[0-1]+)/oct($1)/eig;
1119							$var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
1120							if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
1121							{ $var=~s/^([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
1122							$var;
1123						    };
1124
1125				    $sz =~ tr/bvlrq/BWDDQ/;
1126				    $self->{value} = "\tD$sz\t";
1127				    for (@arr) { $self->{value} .= &$conv($_).","; }
1128				    $self->{value} .= &$conv($last);
1129				    last;
1130				  };
1131		/\.byte/    && do { my @str=split(/,\s*/,$$line);
1132				    map(s/(0b[0-1]+)/oct($1)/eig,@str);
1133				    map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
1134				    while ($#str>15) {
1135					$self->{value}.="DB\t"
1136						.join(",",@str[0..15])."\n";
1137					foreach (0..15) { shift @str; }
1138				    }
1139				    $self->{value}.="DB\t"
1140						.join(",",@str) if (@str);
1141				    last;
1142				  };
1143		/\.comm/    && do { my @str=split(/,\s*/,$$line);
1144				    my $v=undef;
1145				    if ($nasm) {
1146					$v.="common	$prefix@str[0] @str[1]";
1147				    } else {
1148					$current_segment = pop(@segment_stack);;
1149					$v="$current_segment\tENDS\n" if ($current_segment);
1150					$current_segment = "_DATA";
1151					push(@segment_stack, $current_segment);
1152					$v.="$current_segment\tSEGMENT\n";
1153					$v.="COMM	@str[0]:DWORD:".@str[1]/4;
1154				    }
1155				    $self->{value} = $v;
1156				    last;
1157				  };
1158		/^.previous/ && do {
1159				    my $v=undef;
1160				    if ($nasm) {
1161					pop(@segment_stack); # pop ourselves, we don't need to emit END directive
1162					# pop section so we can emit proper .section name.
1163					$current_segment = pop(@segment_stack);
1164					$v="section $current_segment";
1165					# Hack again:
1166					# push section/segment to stack. The .previous is currently paired
1167					# with .rodata only. We have to keep extra '.text' on stack for
1168					# situation where there is for example .pdata section 'terminated'
1169					# by new '.text' section.
1170					#
1171					push(@segment_stack, $current_segment);
1172				    } else {
1173					$current_segment = pop(@segment_stack);
1174					$v="$current_segment\tENDS\n" if ($current_segment);
1175					$current_segment = pop(@segment_stack);
1176					if ($current_segment =~ /\.text\$/) {
1177					    $v.="$current_segment\tSEGMENT ";
1178					    $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
1179					    $v.=" 'CODE'";
1180					    push(@segment_stack, $current_segment);
1181					}
1182				    }
1183				    $self->{value} = $v;
1184				    last;
1185				    };
1186	    }
1187	    $$line = "";
1188	}
1189
1190	$ret;
1191    }
1192    sub out {
1193	my $self = shift;
1194	$self->{value};
1195    }
1196}
1197
1198# Upon initial x86_64 introduction SSE>2 extensions were not introduced
1199# yet. In order not to be bothered by tracing exact assembler versions,
1200# but at the same time to provide a bare security minimum of AES-NI, we
1201# hard-code some instructions. Extensions past AES-NI on the other hand
1202# are traced by examining assembler version in individual perlasm
1203# modules...
1204
1205my %regrm = (	"%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
1206		"%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7	);
1207
1208sub rex {
1209 my $opcode=shift;
1210 my ($dst,$src,$rex)=@_;
1211
1212   $rex|=0x04 if($dst>=8);
1213   $rex|=0x01 if($src>=8);
1214   push @$opcode,($rex|0x40) if ($rex);
1215}
1216
1217my $movq = sub {	# elderly gas can't handle inter-register movq
1218  my $arg = shift;
1219  my @opcode=(0x66);
1220    if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
1221	my ($src,$dst)=($1,$2);
1222	if ($dst !~ /[0-9]+/)	{ $dst = $regrm{"%e$dst"}; }
1223	rex(\@opcode,$src,$dst,0x8);
1224	push @opcode,0x0f,0x7e;
1225	push @opcode,0xc0|(($src&7)<<3)|($dst&7);	# ModR/M
1226	@opcode;
1227    } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
1228	my ($src,$dst)=($2,$1);
1229	if ($dst !~ /[0-9]+/)	{ $dst = $regrm{"%e$dst"}; }
1230	rex(\@opcode,$src,$dst,0x8);
1231	push @opcode,0x0f,0x6e;
1232	push @opcode,0xc0|(($src&7)<<3)|($dst&7);	# ModR/M
1233	@opcode;
1234    } else {
1235	();
1236    }
1237};
1238
1239my $pextrd = sub {
1240    if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
1241      my @opcode=(0x66);
1242	my $imm=$1;
1243	my $src=$2;
1244	my $dst=$3;
1245	if ($dst =~ /%r([0-9]+)d/)	{ $dst = $1; }
1246	elsif ($dst =~ /%e/)		{ $dst = $regrm{$dst}; }
1247	rex(\@opcode,$src,$dst);
1248	push @opcode,0x0f,0x3a,0x16;
1249	push @opcode,0xc0|(($src&7)<<3)|($dst&7);	# ModR/M
1250	push @opcode,$imm;
1251	@opcode;
1252    } else {
1253	();
1254    }
1255};
1256
1257my $pinsrd = sub {
1258    if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
1259      my @opcode=(0x66);
1260	my $imm=$1;
1261	my $src=$2;
1262	my $dst=$3;
1263	if ($src =~ /%r([0-9]+)/)	{ $src = $1; }
1264	elsif ($src =~ /%e/)		{ $src = $regrm{$src}; }
1265	rex(\@opcode,$dst,$src);
1266	push @opcode,0x0f,0x3a,0x22;
1267	push @opcode,0xc0|(($dst&7)<<3)|($src&7);	# ModR/M
1268	push @opcode,$imm;
1269	@opcode;
1270    } else {
1271	();
1272    }
1273};
1274
1275my $pshufb = sub {
1276    if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1277      my @opcode=(0x66);
1278	rex(\@opcode,$2,$1);
1279	push @opcode,0x0f,0x38,0x00;
1280	push @opcode,0xc0|($1&7)|(($2&7)<<3);		# ModR/M
1281	@opcode;
1282    } else {
1283	();
1284    }
1285};
1286
1287my $palignr = sub {
1288    if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1289      my @opcode=(0x66);
1290	rex(\@opcode,$3,$2);
1291	push @opcode,0x0f,0x3a,0x0f;
1292	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
1293	push @opcode,$1;
1294	@opcode;
1295    } else {
1296	();
1297    }
1298};
1299
1300my $pclmulqdq = sub {
1301    if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1302      my @opcode=(0x66);
1303	rex(\@opcode,$3,$2);
1304	push @opcode,0x0f,0x3a,0x44;
1305	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
1306	my $c=$1;
1307	push @opcode,$c=~/^0/?oct($c):$c;
1308	@opcode;
1309    } else {
1310	();
1311    }
1312};
1313
1314my $rdrand = sub {
1315    if (shift =~ /%[er](\w+)/) {
1316      my @opcode=();
1317      my $dst=$1;
1318	if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
1319	rex(\@opcode,0,$dst,8);
1320	push @opcode,0x0f,0xc7,0xf0|($dst&7);
1321	@opcode;
1322    } else {
1323	();
1324    }
1325};
1326
1327my $rdseed = sub {
1328    if (shift =~ /%[er](\w+)/) {
1329      my @opcode=();
1330      my $dst=$1;
1331	if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
1332	rex(\@opcode,0,$dst,8);
1333	push @opcode,0x0f,0xc7,0xf8|($dst&7);
1334	@opcode;
1335    } else {
1336	();
1337    }
1338};
1339
1340# Not all AVX-capable assemblers recognize AMD XOP extension. Since we
1341# are using only two instructions hand-code them in order to be excused
1342# from chasing assembler versions...
1343
1344sub rxb {
1345 my $opcode=shift;
1346 my ($dst,$src1,$src2,$rxb)=@_;
1347
1348   $rxb|=0x7<<5;
1349   $rxb&=~(0x04<<5) if($dst>=8);
1350   $rxb&=~(0x01<<5) if($src1>=8);
1351   $rxb&=~(0x02<<5) if($src2>=8);
1352   push @$opcode,$rxb;
1353}
1354
1355my $vprotd = sub {
1356    if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1357      my @opcode=(0x8f);
1358	rxb(\@opcode,$3,$2,-1,0x08);
1359	push @opcode,0x78,0xc2;
1360	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
1361	my $c=$1;
1362	push @opcode,$c=~/^0/?oct($c):$c;
1363	@opcode;
1364    } else {
1365	();
1366    }
1367};
1368
1369my $vprotq = sub {
1370    if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1371      my @opcode=(0x8f);
1372	rxb(\@opcode,$3,$2,-1,0x08);
1373	push @opcode,0x78,0xc3;
1374	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
1375	my $c=$1;
1376	push @opcode,$c=~/^0/?oct($c):$c;
1377	@opcode;
1378    } else {
1379	();
1380    }
1381};
1382
1383# Intel Control-flow Enforcement Technology extension. All functions and
1384# indirect branch targets will have to start with this instruction...
1385
1386my $endbranch = sub {
1387    (0xf3,0x0f,0x1e,0xfa);
1388};
1389
1390########################################################################
1391
1392if ($nasm) {
1393    print <<___;
1394default	rel
1395%define XMMWORD
1396%define YMMWORD
1397%define ZMMWORD
1398___
1399} elsif ($masm) {
1400    print <<___;
1401OPTION	DOTNAME
1402___
1403}
1404while(defined(my $line=<>)) {
1405
1406    $line =~ s|\R$||;           # Better chomp
1407
1408    $line =~ s|[#!].*$||;	# get rid of asm-style comments...
1409    $line =~ s|/\*.*\*/||;	# ... and C-style comments...
1410    $line =~ s|^\s+||;		# ... and skip whitespaces in beginning
1411    $line =~ s|\s+$||;		# ... and at the end
1412
1413    if (my $label=label->re(\$line))	{ print $label->out(); }
1414
1415    if (my $directive=directive->re(\$line)) {
1416	printf "%s",$directive->out();
1417    } else {
1418	if (my $vex_prefix=vex_prefix->re(\$line)) {
1419	printf "%s",$vex_prefix->out();
1420	}
1421	if (my $opcode=opcode->re(\$line)) {
1422	my $asm = eval("\$".$opcode->mnemonic());
1423
1424	if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
1425	    print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
1426	    next;
1427	}
1428
1429	my @args;
1430	ARGUMENT: while (1) {
1431	    my $arg;
1432
1433	    ($arg=register->re(\$line, $opcode))||
1434	    ($arg=const->re(\$line))		||
1435	    ($arg=ea->re(\$line, $opcode))	||
1436	    ($arg=expr->re(\$line, $opcode))	||
1437	    last ARGUMENT;
1438
1439	    push @args,$arg;
1440
1441	    last ARGUMENT if ($line !~ /^,/);
1442
1443	    $line =~ s/^,\s*//;
1444	} # ARGUMENT:
1445
1446	if ($#args>=0) {
1447	    my $insn;
1448	    my $sz=$opcode->size();
1449
1450	    if ($gas) {
1451		$insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
1452		@args = map($_->out($sz),@args);
1453		printf "\t%s\t%s",$insn,join(",",@args);
1454	    } else {
1455		$insn = $opcode->out();
1456		foreach (@args) {
1457		    my $arg = $_->out();
1458		    # $insn.=$sz compensates for movq, pinsrw, ...
1459		    if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
1460		    if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
1461		    if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
1462		    if ($arg =~ /^mm[0-9]+$/)  { $insn.=$sz; $sz="q" if(!$sz); last; }
1463		}
1464		@args = reverse(@args);
1465		undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
1466		printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
1467	    }
1468	} else {
1469	    printf "\t%s",$opcode->out();
1470	}
1471	}
1472    }
1473
1474    print $line,"\n";
1475}
1476
1477print "$cet_property"			if ($cet_property);
1478print "\n$current_segment\tENDS\n"	if ($current_segment && $masm);
1479print "END\n"				if ($masm);
1480
1481close STDOUT or die "error closing STDOUT: $!;"
1482
1483#################################################
1484# Cross-reference x86_64 ABI "card"
1485#
1486# 		Unix		Win64
1487# %rax		*		*
1488# %rbx		-		-
1489# %rcx		#4		#1
1490# %rdx		#3		#2
1491# %rsi		#2		-
1492# %rdi		#1		-
1493# %rbp		-		-
1494# %rsp		-		-
1495# %r8		#5		#3
1496# %r9		#6		#4
1497# %r10		*		*
1498# %r11		*		*
1499# %r12		-		-
1500# %r13		-		-
1501# %r14		-		-
1502# %r15		-		-
1503#
1504# (*)	volatile register
1505# (-)	preserved by callee
1506# (#)	Nth argument, volatile
1507#
1508# In Unix terms top of stack is argument transfer area for arguments
1509# which could not be accommodated in registers. Or in other words 7th
1510# [integer] argument resides at 8(%rsp) upon function entry point.
1511# 128 bytes above %rsp constitute a "red zone" which is not touched
1512# by signal handlers and can be used as temporal storage without
1513# allocating a frame.
1514#
1515# In Win64 terms N*8 bytes on top of stack is argument transfer area,
1516# which belongs to/can be overwritten by callee. N is the number of
1517# arguments passed to callee, *but* not less than 4! This means that
1518# upon function entry point 5th argument resides at 40(%rsp), as well
1519# as that 32 bytes from 8(%rsp) can always be used as temporal
1520# storage [without allocating a frame]. One can actually argue that
1521# one can assume a "red zone" above stack pointer under Win64 as well.
1522# Point is that at apparently no occasion Windows kernel would alter
1523# the area above user stack pointer in true asynchronous manner...
1524#
1525# All the above means that if assembler programmer adheres to Unix
1526# register and stack layout, but disregards the "red zone" existence,
1527# it's possible to use following prologue and epilogue to "gear" from
1528# Unix to Win64 ABI in leaf functions with not more than 6 arguments.
1529#
1530# omnipotent_function:
1531# ifdef WIN64
1532#	movq	%rdi,8(%rsp)
1533#	movq	%rsi,16(%rsp)
1534#	movq	%rcx,%rdi	; if 1st argument is actually present
1535#	movq	%rdx,%rsi	; if 2nd argument is actually ...
1536#	movq	%r8,%rdx	; if 3rd argument is ...
1537#	movq	%r9,%rcx	; if 4th argument ...
1538#	movq	40(%rsp),%r8	; if 5th ...
1539#	movq	48(%rsp),%r9	; if 6th ...
1540# endif
1541#	...
1542# ifdef WIN64
1543#	movq	8(%rsp),%rdi
1544#	movq	16(%rsp),%rsi
1545# endif
1546#	ret
1547#
1548#################################################
1549# Win64 SEH, Structured Exception Handling.
1550#
1551# Unlike on Unix systems(*) lack of Win64 stack unwinding information
1552# has undesired side-effect at run-time: if an exception is raised in
1553# assembler subroutine such as those in question (basically we're
1554# referring to segmentation violations caused by malformed input
1555# parameters), the application is briskly terminated without invoking
1556# any exception handlers, most notably without generating memory dump
1557# or any user notification whatsoever. This poses a problem. It's
1558# possible to address it by registering custom language-specific
1559# handler that would restore processor context to the state at
1560# subroutine entry point and return "exception is not handled, keep
1561# unwinding" code. Writing such handler can be a challenge... But it's
1562# doable, though requires certain coding convention. Consider following
1563# snippet:
1564#
1565# .type	function,@function
1566# function:
1567#	movq	%rsp,%rax	# copy rsp to volatile register
1568#	pushq	%r15		# save non-volatile registers
1569#	pushq	%rbx
1570#	pushq	%rbp
1571#	movq	%rsp,%r11
1572#	subq	%rdi,%r11	# prepare [variable] stack frame
1573#	andq	$-64,%r11
1574#	movq	%rax,0(%r11)	# check for exceptions
1575#	movq	%r11,%rsp	# allocate [variable] stack frame
1576#	movq	%rax,0(%rsp)	# save original rsp value
1577# magic_point:
1578#	...
1579#	movq	0(%rsp),%rcx	# pull original rsp value
1580#	movq	-24(%rcx),%rbp	# restore non-volatile registers
1581#	movq	-16(%rcx),%rbx
1582#	movq	-8(%rcx),%r15
1583#	movq	%rcx,%rsp	# restore original rsp
1584# magic_epilogue:
1585#	ret
1586# .size function,.-function
1587#
1588# The key is that up to magic_point copy of original rsp value remains
1589# in chosen volatile register and no non-volatile register, except for
1590# rsp, is modified. While past magic_point rsp remains constant till
1591# the very end of the function. In this case custom language-specific
1592# exception handler would look like this:
1593#
1594# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1595#		CONTEXT *context,DISPATCHER_CONTEXT *disp)
1596# {	ULONG64 *rsp = (ULONG64 *)context->Rax;
1597#	ULONG64  rip = context->Rip;
1598#
1599#	if (rip >= magic_point)
1600#	{   rsp = (ULONG64 *)context->Rsp;
1601#	    if (rip < magic_epilogue)
1602#	    {	rsp = (ULONG64 *)rsp[0];
1603#		context->Rbp = rsp[-3];
1604#		context->Rbx = rsp[-2];
1605#		context->R15 = rsp[-1];
1606#	    }
1607#	}
1608#	context->Rsp = (ULONG64)rsp;
1609#	context->Rdi = rsp[1];
1610#	context->Rsi = rsp[2];
1611#
1612#	memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
1613#	RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
1614#		dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
1615#		&disp->HandlerData,&disp->EstablisherFrame,NULL);
1616#	return ExceptionContinueSearch;
1617# }
1618#
1619# It's appropriate to implement this handler in assembler, directly in
1620# function's module. In order to do that one has to know members'
1621# offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
1622# values. Here they are:
1623#
1624#	CONTEXT.Rax				120
1625#	CONTEXT.Rcx				128
1626#	CONTEXT.Rdx				136
1627#	CONTEXT.Rbx				144
1628#	CONTEXT.Rsp				152
1629#	CONTEXT.Rbp				160
1630#	CONTEXT.Rsi				168
1631#	CONTEXT.Rdi				176
1632#	CONTEXT.R8				184
1633#	CONTEXT.R9				192
1634#	CONTEXT.R10				200
1635#	CONTEXT.R11				208
1636#	CONTEXT.R12				216
1637#	CONTEXT.R13				224
1638#	CONTEXT.R14				232
1639#	CONTEXT.R15				240
1640#	CONTEXT.Rip				248
1641#	CONTEXT.Xmm6				512
1642#	sizeof(CONTEXT)				1232
1643#	DISPATCHER_CONTEXT.ControlPc		0
1644#	DISPATCHER_CONTEXT.ImageBase		8
1645#	DISPATCHER_CONTEXT.FunctionEntry	16
1646#	DISPATCHER_CONTEXT.EstablisherFrame	24
1647#	DISPATCHER_CONTEXT.TargetIp		32
1648#	DISPATCHER_CONTEXT.ContextRecord	40
1649#	DISPATCHER_CONTEXT.LanguageHandler	48
1650#	DISPATCHER_CONTEXT.HandlerData		56
1651#	UNW_FLAG_NHANDLER			0
1652#	ExceptionContinueSearch			1
1653#
1654# In order to tie the handler to the function one has to compose
1655# couple of structures: one for .xdata segment and one for .pdata.
1656#
1657# UNWIND_INFO structure for .xdata segment would be
1658#
1659# function_unwind_info:
1660#	.byte	9,0,0,0
1661#	.rva	handler
1662#
1663# This structure designates exception handler for a function with
1664# zero-length prologue, no stack frame or frame register.
1665#
1666# To facilitate composing of .pdata structures, auto-generated "gear"
1667# prologue copies rsp value to rax and denotes next instruction with
1668# .LSEH_begin_{function_name} label. This essentially defines the SEH
1669# styling rule mentioned in the beginning. Position of this label is
1670# chosen in such manner that possible exceptions raised in the "gear"
1671# prologue would be accounted to caller and unwound from latter's frame.
1672# End of function is marked with respective .LSEH_end_{function_name}
1673# label. To summarize, .pdata segment would contain
1674#
1675#	.rva	.LSEH_begin_function
1676#	.rva	.LSEH_end_function
1677#	.rva	function_unwind_info
1678#
1679# Reference to function_unwind_info from .xdata segment is the anchor.
1680# In case you wonder why references are 32-bit .rvas and not 64-bit
1681# .quads. References put into these two segments are required to be
1682# *relative* to the base address of the current binary module, a.k.a.
1683# image base. No Win64 module, be it .exe or .dll, can be larger than
1684# 2GB and thus such relative references can be and are accommodated in
1685# 32 bits.
1686#
1687# Having reviewed the example function code, one can argue that "movq
1688# %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
1689# rax would contain an undefined value. If this "offends" you, use
1690# another register and refrain from modifying rax till magic_point is
1691# reached, i.e. as if it was a non-volatile register. If more registers
1692# are required prior [variable] frame setup is completed, note that
1693# nobody says that you can have only one "magic point." You can
1694# "liberate" non-volatile registers by denoting last stack off-load
1695# instruction and reflecting it in finer grade unwind logic in handler.
1696# After all, isn't it why it's called *language-specific* handler...
1697#
1698# SE handlers are also involved in unwinding stack when executable is
1699# profiled or debugged. Profiling implies additional limitations that
1700# are too subtle to discuss here. For now it's sufficient to say that
1701# in order to simplify handlers one should either a) offload original
1702# %rsp to stack (like discussed above); or b) if you have a register to
1703# spare for frame pointer, choose volatile one.
1704#
1705# (*)	Note that we're talking about run-time, not debug-time. Lack of
1706#	unwind information makes debugging hard on both Windows and
1707#	Unix. "Unlike" refers to the fact that on Unix signal handler
1708#	will always be invoked, core dumped and appropriate exit code
1709#	returned to parent (for user notification).
1710