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