aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/perlasm/x86unix.pl
blob: e3f24f860bc4f9c36e57146bc0824dc182ae8d95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env perl

package x86unix;	# GAS actually...

*out=\@::out;

$label="L000";
$const="";
$constl=0;

$align=($::aout)?"4":"16";
$under=($::aout or $::coff)?"_":"";
$dot=($::aout)?"":".";
$com_start="#" if ($::aout or $::coff);

sub opsize()
{ my $reg=shift;
    if    ($reg =~ m/^%e/o)		{ "l"; }
    elsif ($reg =~ m/^%[a-d][hl]$/o)	{ "b"; }
    elsif ($reg =~ m/^%[xm]/o)		{ undef; }
    else				{ "w"; }
}

# swap arguments;
# expand opcode with size suffix;
# prefix numeric constants with $;
sub ::generic
{ my($opcode,$dst,$src)=@_;
  my($tmp,$suffix,@arg);

    if (defined($src))
    {	$src =~ s/^(e?[a-dsixphl]{2})$/%$1/o;
	$src =~ s/^(x?mm[0-7])$/%$1/o;
	$src =~ s/^(\-?[0-9]+)$/\$$1/o;
	$src =~ s/^(\-?0x[0-9a-f]+)$/\$$1/o;
	push(@arg,$src);
    }
    if (defined($dst))
    {	$dst =~ s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o;
	$dst =~ s/^(x?mm[0-7])$/%$1/o;
	$dst =~ s/^(\-?[0-9]+)$/\$$1/o		if(!defined($src));
	$dst =~ s/^(\-?0x[0-9a-f]+)$/\$$1/o	if(!defined($src));
	push(@arg,$dst);
    }

    if    ($dst =~ m/^%/o)	{ $suffix=&opsize($dst); }
    elsif ($src =~ m/^%/o)	{ $suffix=&opsize($src); }
    else			{ $suffix="l";           }
    undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o);

    if ($#_==0)				{ &::emit($opcode);		}
    elsif ($opcode =~ m/^j/o && $#_==1)	{ &::emit($opcode,@arg);	}
    elsif ($opcode eq "call" && $#_==1)	{ &::emit($opcode,@arg);	}
    else				{ &::emit($opcode.$suffix,@arg);}

  1;
}
#
# opcodes not covered by ::generic above, mostly inconsistent namings...
#
sub ::movz	{ &::movzb(@_);			}
sub ::pushf	{ &::pushfl;			}
sub ::popf	{ &::popfl;			}
sub ::cpuid	{ &::emit(".byte\t0x0f,0xa2");	}
sub ::rdtsc	{ &::emit(".byte\t0x0f,0x31");	}

sub ::call	{ &::emit("call",(&islabel($_[0]) or "$under$_[0]")); }
sub ::call_ptr	{ &::generic("call","*$_[0]");	}
sub ::jmp_ptr	{ &::generic("jmp","*$_[0]");	}

*::bswap = sub	{ &::emit("bswap","%$_[0]");	} if (!$::i386);

# chosen SSE instructions
sub ::movq
{ my($p1,$p2,$optimize)=@_;
    if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
    # movq between mmx registers can sink Intel CPUs
    {	&::pshufw($p1,$p2,0xe4);	}
    else
    {	&::generic("movq",@_);	}
}
sub ::pshufw
{ my($dst,$src,$magic)=@_;
    &::emit("pshufw","\$$magic","%$src","%$dst");
}

sub ::DWP
{ my($addr,$reg1,$reg2,$idx)=@_;
  my $ret="";

    $addr =~ s/^\s+//;
    # prepend global references with optional underscore
    $addr =~ s/^([^\+\-0-9][^\+\-]*)/islabel($1) or "$under$1"/ige;

    $reg1 = "%$reg1" if ($reg1);
    $reg2 = "%$reg2" if ($reg2);

    $ret .= $addr if (($addr ne "") && ($addr ne 0));

    if ($reg2)
    {	$idx!= 0 or $idx=1;
	$ret .= "($reg1,$reg2,$idx)";
    }
    elsif ($reg1)
    {	$ret .= "($reg1)";	}

  $ret;
}
sub ::QWP	{ &::DWP(@_);	}
sub ::BP	{ &::DWP(@_);	}
sub ::BC	{ @_;		}
sub ::DWC	{ @_;		}

sub ::file
{   push(@out,".file\t\"$_[0].s\"\n");	}

sub ::function_begin_B
{ my($func,$extra)=@_;
  my $tmp;

    &::external_label($func);
    $func=$under.$func;

    push(@out,".text\n.globl\t$func\n");
    if ($::coff)
    {	push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
    elsif ($::aout and !$::pic)
    { }
    else
    {	push(@out,".type	$func,\@function\n"); }
    push(@out,".align\t$align\n");
    push(@out,"$func:\n");
    $::stack=4;
}

sub ::function_end_B
{ my($func)=@_;

    $func=$under.$func;
    push(@out,"${dot}L_${func}_end:\n");
    if ($::elf)
    {	push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
    $::stack=0;
    %label=();
}

sub ::comment
	{
	if (!defined($com_start) or $::elf)
		{	# Regarding $::elf above...
			# GNU and SVR4 as'es use different comment delimiters,
		push(@out,"\n");	# so we just skip ELF comments...
		return;
		}
	foreach (@_)
		{
		if (/^\s*$/)
			{ push(@out,"\n"); }
		else
			{ push(@out,"\t$com_start $_ $com_end\n"); }
		}
	}

sub islabel	# see is argument is a known label
{ my $i;
    foreach $i (%label) { return $label{$i} if ($label{$i} eq $_[0]); }
  undef;
}

sub ::external_label { push(@labels,@_); }

sub ::public_label
{   $label{$_[0]}="${under}${_[0]}"	if (!defined($label{$_[0]}));
    push(@out,".globl\t$label{$_[0]}\n");
}

sub ::label
{   if (!defined($label{$_[0]}))
    {	$label{$_[0]}="${dot}${label}${_[0]}"; $label++;   }
  $label{$_[0]};
}

sub ::set_label
{ my $label=&::label($_[0]);
    &::align($_[1]) if ($_[1]>1);
    push(@out,"$label:\n");
}

sub ::file_end
{   # try to detect if SSE2 or MMX extensions were used on ELF platform...
    if ($::elf && grep {/%[x]?mm[0-7]/i} @out){
	my $tmp;

	push (@out,"\n.section\t.bss\n");
	push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");

	push (@out,".section\t.init\n");
	# One can argue that it's wasteful to craft every
	# SSE/MMX module with this snippet... Well, it's 72
	# bytes long and for the moment we have two modules.
	# Let's argue when we have 7 modules or so...
	#
	# $1<<10 sets a reserved bit to signal that variable
	# was initialized already...
	&::picmeup("edx","OPENSSL_ia32cap_P");
	$tmp=<<___;
	cmpl	\$0,(%edx)
	jne	1f
	movl	\$1<<10,(%edx)
	pushf
	popl	%eax
	movl	%eax,%ecx
	xorl	\$1<<21,%eax
	pushl	%eax
	popf
	pushf
	popl	%eax
	xorl	%ecx,%eax
	btl	\$21,%eax
	jnc	1f
	pushl	%edi
	pushl	%ebx
	movl	%edx,%edi
	movl	\$1,%eax
	.byte	0x0f,0xa2
	orl	\$1<<10,%edx
	movl	%edx,0(%edi)
	popl	%ebx
	popl	%edi
	jmp	1f
	.align	$align
	1:
___
	push (@out,$tmp);
    }

    if ($const ne "")
    {	push(@out,".section .rodata\n");
	push(@out,$const);
	$const="";
    }
}

sub ::data_byte	{   push(@out,".byte\t".join(',',@_)."\n");   }
sub ::data_word {   push(@out,".long\t".join(',',@_)."\n");   }

sub ::align
{ my $val=$_[0],$p2,$i;
    if ($::aout)
    {	for ($p2=0;$val!=0;$val>>=1) { $p2++; }
	$val=$p2-1;
	$val.=",0x90";
    }
    push(@out,".align\t$val\n");
}

sub ::picmeup
{ my($dst,$sym,$base,$reflabel)=@_;

    if ($::pic && ($::elf || $::aout))
    {	if (!defined($base))
	{   &::call(&::label("PIC_me_up"));
	    &::set_label("PIC_me_up");
	    &::blindpop($dst);
	    &::add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
			    &::label("PIC_me_up") . "]");
	}
	else
	{   &::lea($dst,&::DWP("${under}_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
			    $base));
	}
	&::mov($dst,&::DWP($under.$sym."\@GOT",$dst));
    }
    else
    {	&::lea($dst,&::DWP($sym));	}
}

sub ::initseg
{ my($f)=@_;
  my($tmp,$ctor);

    if ($::elf)
    {	$tmp=<<___;
.section	.init
	call	$under$f
	jmp	.Linitalign
.align	$align
.Linitalign:
___
    }
    elsif ($::coff)
    {   $tmp=<<___;	# applies to both Cygwin and Mingw
.section	.ctors
.long	$under$f
___
    }
    elsif ($::aout)
    {	$ctor="${under}_GLOBAL_\$I\$$f";
	$tmp=".text\n";
	$tmp.=".type	$ctor,\@function\n" if ($::pic);
	$tmp.=<<___;	# OpenBSD way...
.globl	$ctor
.align	2
$ctor:
	jmp	$under$f
___
    }
    push(@out,$tmp) if ($tmp);
}

1;