1#! /usr/bin/env perl
2# Copyright 2006-2021 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
9my $flavour = shift;
10my $output = shift;
11open STDOUT,">$output" || die "can't open $output: $!";
12
13my %GLOBALS;
14my %TYPES;
15my $dotinlocallabels=($flavour=~/linux/)?1:0;
16
17################################################################
18# directives which need special treatment on different platforms
19################################################################
20my $type = sub {
21    my ($dir,$name,$type) = @_;
22
23    $TYPES{$name} = $type;
24    if ($flavour =~ /linux/) {
25	$name =~ s|^\.||;
26	".type	$name,$type";
27    } else {
28	"";
29    }
30};
31my $globl = sub {
32    my $junk = shift;
33    my $name = shift;
34    my $global = \$GLOBALS{$name};
35    my $type = \$TYPES{$name};
36    my $ret;
37
38    $name =~ s|^\.||;
39
40    SWITCH: for ($flavour) {
41	/aix/		&& do { if (!$$type) {
42				    $$type = "\@function";
43				}
44				if ($$type =~ /function/) {
45				    $name = ".$name";
46				}
47				last;
48			      };
49	/osx/		&& do { $name = "_$name";
50				last;
51			      };
52	/linux.*(32|64(le|v2))/
53			&& do {	$ret .= ".globl	$name";
54				if (!$$type) {
55				    $ret .= "\n.type	$name,\@function";
56				    $$type = "\@function";
57				}
58				last;
59			      };
60	/linux.*64/	&& do {	$ret .= ".globl	$name";
61				if (!$$type) {
62				    $ret .= "\n.type	$name,\@function";
63				    $$type = "\@function";
64				}
65				if ($$type =~ /function/) {
66				    $ret .= "\n.section	\".opd\",\"aw\"";
67				    $ret .= "\n.align	3";
68				    $ret .= "\n$name:";
69				    $ret .= "\n.quad	.$name,.TOC.\@tocbase,0";
70				    $ret .= "\n.previous";
71				    $name = ".$name";
72				}
73				last;
74			      };
75    }
76
77    $ret = ".globl	$name" if (!$ret);
78    $$global = $name;
79    $ret;
80};
81my $text = sub {
82    my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
83    $ret = ".abiversion	2\n".$ret	if ($flavour =~ /linux.*64(le|v2)/);
84    $ret;
85};
86my $p2align = sub {
87    my $ret = ($flavour =~ /aix64-as/) ? "" : ".p2align $line";
88    $ret;
89};
90my $machine = sub {
91    my $junk = shift;
92    my $arch = shift;
93    if ($flavour =~ /osx/)
94    {	$arch =~ s/\"//g;
95	$arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
96    }
97    ".machine	$arch";
98};
99my $size = sub {
100    if ($flavour =~ /linux/)
101    {	shift;
102	my $name = shift;
103	my $real = $GLOBALS{$name} ? \$GLOBALS{$name} : \$name;
104	my $ret  = ".size	$$real,.-$$real";
105	$name =~ s|^\.||;
106	if ($$real ne $name) {
107	    $ret .= "\n.size	$name,.-$$real";
108	}
109	$ret;
110    }
111    else
112    {	"";	}
113};
114my $asciz = sub {
115    shift;
116    my $line = join(",",@_);
117    if ($line =~ /^"(.*)"$/)
118    {	".byte	" . join(",",unpack("C*",$1),0) . "\n.align	2";	}
119    else
120    {	"";	}
121};
122my $quad = sub {
123    shift;
124    my @ret;
125    my ($hi,$lo);
126    for (@_) {
127	if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
128	{  $hi=$1?"0x$1":"0"; $lo="0x$2";  }
129	elsif (/^([0-9]+)$/o)
130	{  $hi=$1>>32; $lo=$1&0xffffffff;  } # error-prone with 32-bit perl
131	else
132	{  $hi=undef; $lo=$_; }
133
134	if (defined($hi))
135	{  push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo");  }
136	else
137	{  push(@ret,".quad	$lo");  }
138    }
139    join("\n",@ret);
140};
141
142################################################################
143# vector register number hacking
144################################################################
145
146# It is convenient to be able to set a variable like:
147#   my $foo = "v33";
148# and use this in different contexts where:
149# * a VSR (Vector-Scaler Register) number (i.e. "v33") is required
150# * a VR (Vector Register) number (i.e. "v1") is required
151# Map VSR numbering to VR number for certain vector instructions.
152
153# vs<N> -> v<N-32> if N > 32
154sub vsr2vr1 {
155    my $in = shift;
156    my ($prefix, $reg) = ($in =~ m/(\D*)(\d+)/);
157
158    my $n = int($reg);
159    if ($n >= 32) {
160	    $n -= 32;
161    }
162
163    return "${prefix}${n}";
164}
165# As above for first $num register args, returns list
166sub _vsr2vr {
167    my $num = shift;
168    my @rest = @_;
169    my @subst = splice(@rest, 0, $num);
170
171    @subst = map { vsr2vr1($_); } @subst;
172
173    return (@subst, @rest);
174}
175# As above but 1st arg ($f) is extracted and reinserted after
176# processing so that it can be ignored by a code generation function
177# that consumes the result
178sub vsr2vr_args {
179    my $num = shift;
180    my $f = shift;
181
182    my @out = _vsr2vr($num, @_);
183
184    return ($f, @out);
185}
186# As above but 1st arg is mnemonic, return formatted instruction
187sub vsr2vr {
188    my $mnemonic = shift;
189    my $num = shift;
190    my $f = shift;
191
192    my @out = _vsr2vr($num, @_);
193
194    "	${mnemonic}${f}	" . join(",", @out);
195}
196
197# ISA 2.03
198my $vsel	= sub { vsr2vr("vsel",		4, @_); };
199my $vsl		= sub { vsr2vr("vsl",		3, @_); };
200my $vspltisb	= sub { vsr2vr("vspltisb",	1, @_); };
201my $vspltisw	= sub { vsr2vr("vspltisw",	1, @_); };
202my $vsr		= sub { vsr2vr("vsr",		3, @_); };
203my $vsro	= sub { vsr2vr("vsro",		3, @_); };
204
205# ISA 3.0
206my $lxsd	= sub { vsr2vr("lxsd",		1, @_); };
207
208################################################################
209# simplified mnemonics not handled by at least one assembler
210################################################################
211my $cmplw = sub {
212    my $f = shift;
213    my $cr = 0; $cr = shift if ($#_>1);
214    # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
215    ($flavour =~ /linux.*32/) ?
216	"	.long	".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
217	"	cmplw	".join(',',$cr,@_);
218};
219my $bdnz = sub {
220    my $f = shift;
221    my $bo = $f=~/[\+\-]/ ? 16+9 : 16;	# optional "to be taken" hint
222    "	bc	$bo,0,".shift;
223} if ($flavour!~/linux/);
224my $bltlr = sub {
225    my $f = shift;
226    my $bo = $f=~/\-/ ? 12+2 : 12;	# optional "not to be taken" hint
227    ($flavour =~ /linux/) ?		# GNU as doesn't allow most recent hints
228	"	.long	".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
229	"	bclr	$bo,0";
230};
231my $bnelr = sub {
232    my $f = shift;
233    my $bo = $f=~/\-/ ? 4+2 : 4;	# optional "not to be taken" hint
234    ($flavour =~ /linux/) ?		# GNU as doesn't allow most recent hints
235	"	.long	".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
236	"	bclr	$bo,2";
237};
238my $beqlr = sub {
239    my $f = shift;
240    my $bo = $f=~/-/ ? 12+2 : 12;	# optional "not to be taken" hint
241    ($flavour =~ /linux/) ?		# GNU as doesn't allow most recent hints
242	"	.long	".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
243	"	bclr	$bo,2";
244};
245# GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
246# arguments is 64, with "operand out of range" error.
247my $extrdi = sub {
248    my ($f,$ra,$rs,$n,$b) = @_;
249    $b = ($b+$n)&63; $n = 64-$n;
250    "	rldicl	$ra,$rs,$b,$n";
251};
252my $vmr = sub {
253    my ($f,$vx,$vy) = @_;
254    "	vor	$vx,$vy,$vy";
255};
256
257# Some ABIs specify vrsave, special-purpose register #256, as reserved
258# for system use.
259my $no_vrsave = ($flavour =~ /aix|linux64(le|v2)/);
260my $mtspr = sub {
261    my ($f,$idx,$ra) = @_;
262    if ($idx == 256 && $no_vrsave) {
263	"	or	$ra,$ra,$ra";
264    } else {
265	"	mtspr	$idx,$ra";
266    }
267};
268my $mfspr = sub {
269    my ($f,$rd,$idx) = @_;
270    if ($idx == 256 && $no_vrsave) {
271	"	li	$rd,-1";
272    } else {
273	"	mfspr	$rd,$idx";
274    }
275};
276
277# PowerISA 2.06 stuff
278sub vsxmem_op {
279    my ($f, $vrt, $ra, $rb, $op) = @_;
280    "	.long	".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
281}
282# made-up unaligned memory reference AltiVec/VMX instructions
283my $lvx_u	= sub {	vsxmem_op(@_, 844); };	# lxvd2x
284my $stvx_u	= sub {	vsxmem_op(@_, 972); };	# stxvd2x
285my $lvdx_u	= sub {	vsxmem_op(@_, 588); };	# lxsdx
286my $stvdx_u	= sub {	vsxmem_op(@_, 716); };	# stxsdx
287my $lvx_4w	= sub { vsxmem_op(@_, 780); };	# lxvw4x
288my $stvx_4w	= sub { vsxmem_op(@_, 908); };	# stxvw4x
289my $lvx_splt	= sub { vsxmem_op(@_, 332); };	# lxvdsx
290# VSX instruction[s] masqueraded as made-up AltiVec/VMX
291my $vpermdi	= sub {				# xxpermdi
292    my ($f, $vrt, $vra, $vrb, $dm) = @_;
293    $dm = oct($dm) if ($dm =~ /^0/);
294    "	.long	".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($dm<<8)|(10<<3)|7;
295};
296
297# PowerISA 2.07 stuff
298sub vcrypto_op {
299    my ($f, $vrt, $vra, $vrb, $op) = vsr2vr_args(3, @_);
300    "	.long	".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
301}
302sub vfour {
303    my ($f, $vrt, $vra, $vrb, $vrc, $op) = @_;
304    "	.long	".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($vrc<<6)|$op;
305};
306sub vfour_vsr {
307    my ($f, $vrt, $vra, $vrb, $vrc, $op) = vsr2vr_args(4, @_);
308    "	.long	".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($vrc<<6)|$op;
309};
310
311my $vcipher	= sub { vcrypto_op(@_, 1288); };
312my $vcipherlast	= sub { vcrypto_op(@_, 1289); };
313my $vncipher	= sub { vcrypto_op(@_, 1352); };
314my $vncipherlast= sub { vcrypto_op(@_, 1353); };
315my $vsbox	= sub { vcrypto_op(@_, 0, 1480); };
316my $vshasigmad	= sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
317my $vshasigmaw	= sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
318my $vpmsumb	= sub { vcrypto_op(@_, 1032); };
319my $vpmsumd	= sub { vcrypto_op(@_, 1224); };
320my $vpmsubh	= sub { vcrypto_op(@_, 1096); };
321my $vpmsumw	= sub { vcrypto_op(@_, 1160); };
322# These are not really crypto, but vcrypto_op template works
323my $vaddudm	= sub { vcrypto_op(@_, 192);  };
324my $vadduqm	= sub { vcrypto_op(@_, 256);  };
325my $vmuleuw	= sub { vcrypto_op(@_, 648);  };
326my $vmulouw	= sub { vcrypto_op(@_, 136);  };
327my $vrld	= sub { vcrypto_op(@_, 196);  };
328my $vsld	= sub { vcrypto_op(@_, 1476); };
329my $vsrd	= sub { vcrypto_op(@_, 1732); };
330my $vsubudm	= sub { vcrypto_op(@_, 1216); };
331my $vaddcuq	= sub { vcrypto_op(@_, 320);  };
332my $vaddeuqm	= sub { vfour_vsr(@_,60); };
333my $vaddecuq	= sub { vfour_vsr(@_,61); };
334my $vmrgew	= sub { vfour_vsr(@_,0,1932); };
335my $vmrgow	= sub { vfour_vsr(@_,0,1676); };
336
337my $mtsle	= sub {
338    my ($f, $arg) = @_;
339    "	.long	".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
340};
341
342# VSX instructions masqueraded as AltiVec/VMX
343my $mtvrd	= sub {
344    my ($f, $vrt, $ra) = @_;
345    "	.long	".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(179<<1)|1;
346};
347my $mtvrwz	= sub {
348    my ($f, $vrt, $ra) = @_;
349    "	.long	".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(243<<1)|1;
350};
351my $lvwzx_u	= sub { vsxmem_op(@_, 12); };	# lxsiwzx
352my $stvwx_u	= sub { vsxmem_op(@_, 140); };	# stxsiwx
353
354# PowerISA 3.0 stuff
355my $maddhdu	= sub { vfour(@_,49); };
356my $maddld	= sub { vfour(@_,51); };
357my $darn = sub {
358    my ($f, $rt, $l) = @_;
359    "	.long	".sprintf "0x%X",(31<<26)|($rt<<21)|($l<<16)|(755<<1);
360};
361my $iseleq = sub {
362    my ($f, $rt, $ra, $rb) = @_;
363    "	.long	".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|(2<<6)|30;
364};
365# VSX instruction[s] masqueraded as made-up AltiVec/VMX
366my $vspltib	= sub {				# xxspltib
367    my ($f, $vrt, $imm8) = @_;
368    $imm8 = oct($imm8) if ($imm8 =~ /^0/);
369    $imm8 &= 0xff;
370    "	.long	".sprintf "0x%X",(60<<26)|($vrt<<21)|($imm8<<11)|(360<<1)|1;
371};
372
373# PowerISA 3.0B stuff
374my $addex = sub {
375    my ($f, $rt, $ra, $rb, $cy) = @_;	# only cy==0 is specified in 3.0B
376    "	.long	".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|($cy<<9)|(170<<1);
377};
378my $vmsumudm	= sub { vfour_vsr(@_, 35); };
379
380while($line=<>) {
381
382    $line =~ s|[#!;].*$||;	# get rid of asm-style comments...
383    $line =~ s|/\*.*\*/||;	# ... and C-style comments...
384    $line =~ s|^\s+||;		# ... and skip whitespaces in beginning...
385    $line =~ s|\s+$||;		# ... and at the end
386
387    {
388	$line =~ s|\.L(\w+)|L$1|g;	# common denominator for Locallabel
389	$line =~ s|\bL(\w+)|\.L$1|g	if ($dotinlocallabels);
390    }
391
392    {
393	$line =~ s|(^[\.\w]+)\:\s*||;
394	my $label = $1;
395	if ($label) {
396	    my $xlated = ($GLOBALS{$label} or $label);
397	    print "$xlated:";
398	    if ($flavour =~ /linux.*64(le|v2)/) {
399		if ($TYPES{$label} =~ /function/) {
400		    printf "\n.localentry	%s,0\n",$xlated;
401		}
402	    }
403	}
404    }
405
406    {
407	$line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
408	my $c = $1; $c = "\t" if ($c eq "");
409	my $mnemonic = $2;
410	my $f = $3;
411	my $opcode = eval("\$$mnemonic");
412	$line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
413	if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(/,\s*/,$line)); }
414	elsif ($mnemonic)           { $line = $c.$mnemonic.$f."\t".$line; }
415    }
416
417    print $line if ($line);
418    print "\n";
419}
420
421close STDOUT or die "error closing STDOUT: $!";
422