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