summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/perlasm
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/perlasm')
-rw-r--r--src/lib/libcrypto/perlasm/alpha.pl434
-rwxr-xr-xsrc/lib/libcrypto/perlasm/x86_64-xlate.pl5
-rw-r--r--src/lib/libcrypto/perlasm/x86asm.pl8
-rw-r--r--src/lib/libcrypto/perlasm/x86ms.pl472
-rw-r--r--src/lib/libcrypto/perlasm/x86nasm.pl455
-rw-r--r--src/lib/libcrypto/perlasm/x86unix.pl808
6 files changed, 2180 insertions, 2 deletions
diff --git a/src/lib/libcrypto/perlasm/alpha.pl b/src/lib/libcrypto/perlasm/alpha.pl
new file mode 100644
index 0000000000..3dac571743
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/alpha.pl
@@ -0,0 +1,434 @@
1#!/usr/local/bin/perl
2
3package alpha;
4use Carp qw(croak cluck);
5
6$label="100";
7
8$n_debug=0;
9$smear_regs=1;
10$reg_alloc=1;
11
12$align="3";
13$com_start="#";
14
15sub main'asm_init_output { @out=(); }
16sub main'asm_get_output { return(@out); }
17sub main'get_labels { return(@labels); }
18sub main'external_label { push(@labels,@_); }
19
20# General registers
21
22%regs=( 'r0', '$0',
23 'r1', '$1',
24 'r2', '$2',
25 'r3', '$3',
26 'r4', '$4',
27 'r5', '$5',
28 'r6', '$6',
29 'r7', '$7',
30 'r8', '$8',
31 'r9', '$22',
32 'r10', '$23',
33 'r11', '$24',
34 'r12', '$25',
35 'r13', '$27',
36 'r14', '$28',
37 'r15', '$21', # argc == 5
38 'r16', '$20', # argc == 4
39 'r17', '$19', # argc == 3
40 'r18', '$18', # argc == 2
41 'r19', '$17', # argc == 1
42 'r20', '$16', # argc == 0
43 'r21', '$9', # save 0
44 'r22', '$10', # save 1
45 'r23', '$11', # save 2
46 'r24', '$12', # save 3
47 'r25', '$13', # save 4
48 'r26', '$14', # save 5
49
50 'a0', '$16',
51 'a1', '$17',
52 'a2', '$18',
53 'a3', '$19',
54 'a4', '$20',
55 'a5', '$21',
56
57 's0', '$9',
58 's1', '$10',
59 's2', '$11',
60 's3', '$12',
61 's4', '$13',
62 's5', '$14',
63 'zero', '$31',
64 'sp', '$30',
65 );
66
67$main'reg_s0="r21";
68$main'reg_s1="r22";
69$main'reg_s2="r23";
70$main'reg_s3="r24";
71$main'reg_s4="r25";
72$main'reg_s5="r26";
73
74@reg=( '$0', '$1' ,'$2' ,'$3' ,'$4' ,'$5' ,'$6' ,'$7' ,'$8',
75 '$22','$23','$24','$25','$20','$21','$27','$28');
76
77
78sub main'sub { &out3("subq",@_); }
79sub main'add { &out3("addq",@_); }
80sub main'mov { &out3("bis",$_[0],$_[0],$_[1]); }
81sub main'or { &out3("bis",@_); }
82sub main'bis { &out3("bis",@_); }
83sub main'br { &out1("br",@_); }
84sub main'ld { &out2("ldq",@_); }
85sub main'st { &out2("stq",@_); }
86sub main'cmpult { &out3("cmpult",@_); }
87sub main'cmplt { &out3("cmplt",@_); }
88sub main'bgt { &out2("bgt",@_); }
89sub main'ble { &out2("ble",@_); }
90sub main'blt { &out2("blt",@_); }
91sub main'mul { &out3("mulq",@_); }
92sub main'muh { &out3("umulh",@_); }
93
94$main'QWS=8;
95
96sub main'asm_add
97 {
98 push(@out,@_);
99 }
100
101sub main'asm_finish
102 {
103 &main'file_end();
104 print &main'asm_get_output();
105 }
106
107sub main'asm_init
108 {
109 ($type,$fn)=@_;
110 $filename=$fn;
111
112 &main'asm_init_output();
113 &main'comment("Don't even think of reading this code");
114 &main'comment("It was automatically generated by $filename");
115 &main'comment("Which is a perl program used to generate the alpha assember.");
116 &main'comment("eric <eay\@cryptsoft.com>");
117 &main'comment("");
118
119 $filename =~ s/\.pl$//;
120 &main'file($filename);
121 }
122
123sub conv
124 {
125 local($r)=@_;
126 local($v);
127
128 return($regs{$r}) if defined($regs{$r});
129 return($r);
130 }
131
132sub main'QWPw
133 {
134 local($off,$reg)=@_;
135
136 return(&main'QWP($off*8,$reg));
137 }
138
139sub main'QWP
140 {
141 local($off,$reg)=@_;
142
143 $ret="$off(".&conv($reg).")";
144 return($ret);
145 }
146
147sub out3
148 {
149 local($name,$p1,$p2,$p3)=@_;
150
151 $p1=&conv($p1);
152 $p2=&conv($p2);
153 $p3=&conv($p3);
154 push(@out,"\t$name\t");
155 $l=length($p1)+1;
156 push(@out,$p1.",");
157 $ll=3-($l+9)/8;
158 $tmp1=sprintf("\t" x $ll);
159 push(@out,$tmp1);
160
161 $l=length($p2)+1;
162 push(@out,$p2.",");
163 $ll=3-($l+9)/8;
164 $tmp1=sprintf("\t" x $ll);
165 push(@out,$tmp1);
166
167 push(@out,&conv($p3)."\n");
168 }
169
170sub out2
171 {
172 local($name,$p1,$p2,$p3)=@_;
173
174 $p1=&conv($p1);
175 $p2=&conv($p2);
176 push(@out,"\t$name\t");
177 $l=length($p1)+1;
178 push(@out,$p1.",");
179 $ll=3-($l+9)/8;
180 $tmp1=sprintf("\t" x $ll);
181 push(@out,$tmp1);
182
183 push(@out,&conv($p2)."\n");
184 }
185
186sub out1
187 {
188 local($name,$p1)=@_;
189
190 $p1=&conv($p1);
191 push(@out,"\t$name\t".$p1."\n");
192 }
193
194sub out0
195 {
196 push(@out,"\t$_[0]\n");
197 }
198
199sub main'file
200 {
201 local($file)=@_;
202
203 local($tmp)=<<"EOF";
204 # DEC Alpha assember
205 # Generated from perl scripts contains in SSLeay
206 .file 1 "$file.s"
207 .set noat
208EOF
209 push(@out,$tmp);
210 }
211
212sub main'function_begin
213 {
214 local($func)=@_;
215
216print STDERR "$func\n";
217 local($tmp)=<<"EOF";
218 .text
219 .align $align
220 .globl $func
221 .ent $func
222${func}:
223${func}..ng:
224 .frame \$30,0,\$26,0
225 .prologue 0
226EOF
227 push(@out,$tmp);
228 $stack=0;
229 }
230
231sub main'function_end
232 {
233 local($func)=@_;
234
235 local($tmp)=<<"EOF";
236 ret \$31,(\$26),1
237 .end $func
238EOF
239 push(@out,$tmp);
240 $stack=0;
241 %label=();
242 }
243
244sub main'function_end_A
245 {
246 local($func)=@_;
247
248 local($tmp)=<<"EOF";
249 ret \$31,(\$26),1
250EOF
251 push(@out,$tmp);
252 }
253
254sub main'function_end_B
255 {
256 local($func)=@_;
257
258 $func=$under.$func;
259
260 push(@out,"\t.end $func\n");
261 $stack=0;
262 %label=();
263 }
264
265sub main'wparam
266 {
267 local($num)=@_;
268
269 if ($num < 6)
270 {
271 $num=20-$num;
272 return("r$num");
273 }
274 else
275 { return(&main'QWP($stack+$num*8,"sp")); }
276 }
277
278sub main'stack_push
279 {
280 local($num)=@_;
281 $stack+=$num*8;
282 &main'sub("sp",$num*8,"sp");
283 }
284
285sub main'stack_pop
286 {
287 local($num)=@_;
288 $stack-=$num*8;
289 &main'add("sp",$num*8,"sp");
290 }
291
292sub main'swtmp
293 {
294 return(&main'QWP(($_[0])*8,"sp"));
295 }
296
297# Should use swtmp, which is above sp. Linix can trash the stack above esp
298#sub main'wtmp
299# {
300# local($num)=@_;
301#
302# return(&main'QWP(-($num+1)*4,"esp","",0));
303# }
304
305sub main'comment
306 {
307 foreach (@_)
308 {
309 if (/^\s*$/)
310 { push(@out,"\n"); }
311 else
312 { push(@out,"\t$com_start $_ $com_end\n"); }
313 }
314 }
315
316sub main'label
317 {
318 if (!defined($label{$_[0]}))
319 {
320 $label{$_[0]}=$label;
321 $label++;
322 }
323 return('$'.$label{$_[0]});
324 }
325
326sub main'set_label
327 {
328 if (!defined($label{$_[0]}))
329 {
330 $label{$_[0]}=$label;
331 $label++;
332 }
333# push(@out,".align $align\n") if ($_[1] != 0);
334 push(@out,'$'."$label{$_[0]}:\n");
335 }
336
337sub main'file_end
338 {
339 }
340
341sub main'data_word
342 {
343 push(@out,"\t.long $_[0]\n");
344 }
345
346@pool_free=();
347@pool_taken=();
348$curr_num=0;
349$max=0;
350
351sub main'init_pool
352 {
353 local($args)=@_;
354 local($i);
355
356 @pool_free=();
357 for ($i=(14+(6-$args)); $i >= 0; $i--)
358 {
359 push(@pool_free,"r$i");
360 }
361 print STDERR "START :register pool:@pool_free\n";
362 $curr_num=$max=0;
363 }
364
365sub main'fin_pool
366 {
367 printf STDERR "END %2d:register pool:@pool_free\n",$max;
368 }
369
370sub main'GR
371 {
372 local($r)=@_;
373 local($i,@n,$_);
374
375 foreach (@pool_free)
376 {
377 if ($r ne $_)
378 { push(@n,$_); }
379 else
380 {
381 $curr_num++;
382 $max=$curr_num if ($curr_num > $max);
383 }
384 }
385 @pool_free=@n;
386print STDERR "GR:@pool_free\n" if $reg_alloc;
387 return(@_);
388 }
389
390sub main'NR
391 {
392 local($num)=@_;
393 local(@ret);
394
395 $num=1 if $num == 0;
396 ($#pool_free >= ($num-1)) || croak "out of registers: want $num, have @pool_free";
397 while ($num > 0)
398 {
399 push(@ret,pop @pool_free);
400 $curr_num++;
401 $max=$curr_num if ($curr_num > $max);
402 $num--
403 }
404 print STDERR "nr @ret\n" if $n_debug;
405print STDERR "NR:@pool_free\n" if $reg_alloc;
406 return(@ret);
407
408 }
409
410sub main'FR
411 {
412 local(@r)=@_;
413 local(@a,$v,$w);
414
415 print STDERR "fr @r\n" if $n_debug;
416# cluck "fr @r";
417 for $w (@pool_free)
418 {
419 foreach $v (@r)
420 {
421 croak "double register free of $v (@pool_free)" if $w eq $v;
422 }
423 }
424 foreach $v (@r)
425 {
426 croak "bad argument to FR" if ($v !~ /^r\d+$/);
427 if ($smear_regs)
428 { unshift(@pool_free,$v); }
429 else { push(@pool_free,$v); }
430 $curr_num--;
431 }
432print STDERR "FR:@pool_free\n" if $reg_alloc;
433 }
4341;
diff --git a/src/lib/libcrypto/perlasm/x86_64-xlate.pl b/src/lib/libcrypto/perlasm/x86_64-xlate.pl
index a4af769b4a..74153b017d 100755
--- a/src/lib/libcrypto/perlasm/x86_64-xlate.pl
+++ b/src/lib/libcrypto/perlasm/x86_64-xlate.pl
@@ -163,7 +163,8 @@ my $current_function;
163 local *line = shift; 163 local *line = shift;
164 undef $ret; 164 undef $ret;
165 165
166 if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/) { 166 if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/ &&
167 !($line =~ /^PIC_(GOT|PLT)/)) {
167 $self->{label} = $1; 168 $self->{label} = $1;
168 ($self->{base},$self->{index},$self->{scale})=split(/,/,$2); 169 ($self->{base},$self->{index},$self->{scale})=split(/,/,$2);
169 $self->{scale} = 1 if (!defined($self->{scale})); 170 $self->{scale} = 1 if (!defined($self->{scale}));
@@ -429,6 +430,8 @@ my $current_function;
429 } 430 }
430} 431}
431 432
433print "#include <machine/asm.h>\n";
434
432while($line=<>) { 435while($line=<>) {
433 436
434 chomp($line); 437 chomp($line);
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl
index 5979122158..f535c9c7fa 100644
--- a/src/lib/libcrypto/perlasm/x86asm.pl
+++ b/src/lib/libcrypto/perlasm/x86asm.pl
@@ -18,9 +18,13 @@ sub main'asm_init
18 ($type,$fn,$i386)=@_; 18 ($type,$fn,$i386)=@_;
19 $filename=$fn; 19 $filename=$fn;
20 20
21 $elf=$cpp=$coff=$aout=$win32=$netware=$mwerks=0; 21 $elf=$cpp=$coff=$aout=$win32=$netware=$mwerks=$openbsd=0;
22 if ( ($type eq "elf")) 22 if ( ($type eq "elf"))
23 { $elf=1; require "x86unix.pl"; } 23 { $elf=1; require "x86unix.pl"; }
24 elsif ( ($type eq "openbsd-elf"))
25 { $openbsd=$elf=1; require "x86unix.pl"; }
26 elsif ( ($type eq "openbsd-a.out"))
27 { $openbsd=1; require "x86unix.pl"; }
24 elsif ( ($type eq "a.out")) 28 elsif ( ($type eq "a.out"))
25 { $aout=1; require "x86unix.pl"; } 29 { $aout=1; require "x86unix.pl"; }
26 elsif ( ($type eq "coff" or $type eq "gaswin")) 30 elsif ( ($type eq "coff" or $type eq "gaswin"))
@@ -44,6 +48,8 @@ Pick one target type from
44 coff - GAS/COFF such as Win32 targets 48 coff - GAS/COFF such as Win32 targets
45 win32 - Windows 95/Windows NT 49 win32 - Windows 95/Windows NT
46 win32n - Windows 95/Windows NT NASM format 50 win32n - Windows 95/Windows NT NASM format
51 openbsd-elf - OpenBSD elf
52 openbsd-a.out - OpenBSD a.out
47 nw-nasm - NetWare NASM format 53 nw-nasm - NetWare NASM format
48 nw-mwasm- NetWare Metrowerks Assembler 54 nw-mwasm- NetWare Metrowerks Assembler
49EOF 55EOF
diff --git a/src/lib/libcrypto/perlasm/x86ms.pl b/src/lib/libcrypto/perlasm/x86ms.pl
new file mode 100644
index 0000000000..a0be2934c2
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86ms.pl
@@ -0,0 +1,472 @@
1#!/usr/local/bin/perl
2
3package x86ms;
4
5$label="L000";
6
7%lb=( 'eax', 'al',
8 'ebx', 'bl',
9 'ecx', 'cl',
10 'edx', 'dl',
11 'ax', 'al',
12 'bx', 'bl',
13 'cx', 'cl',
14 'dx', 'dl',
15 );
16
17%hb=( 'eax', 'ah',
18 'ebx', 'bh',
19 'ecx', 'ch',
20 'edx', 'dh',
21 'ax', 'ah',
22 'bx', 'bh',
23 'cx', 'ch',
24 'dx', 'dh',
25 );
26
27sub main'asm_init_output { @out=(); }
28sub main'asm_get_output { return(@out); }
29sub main'get_labels { return(@labels); }
30sub main'external_label
31{
32 push(@labels,@_);
33 foreach (@_) {
34 push(@out, "EXTRN\t_$_:DWORD\n");
35 }
36}
37
38sub main'LB
39 {
40 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
41 return($lb{$_[0]});
42 }
43
44sub main'HB
45 {
46 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
47 return($hb{$_[0]});
48 }
49
50sub main'BP
51 {
52 &get_mem("BYTE",@_);
53 }
54
55sub main'DWP
56 {
57 &get_mem("DWORD",@_);
58 }
59
60sub main'QWP
61 {
62 &get_mem("QWORD",@_);
63 }
64
65sub main'BC
66 {
67 return @_;
68 }
69
70sub main'DWC
71 {
72 return @_;
73 }
74
75sub main'stack_push
76 {
77 local($num)=@_;
78 $stack+=$num*4;
79 &main'sub("esp",$num*4);
80 }
81
82sub main'stack_pop
83 {
84 local($num)=@_;
85 $stack-=$num*4;
86 &main'add("esp",$num*4);
87 }
88
89sub get_mem
90 {
91 local($size,$addr,$reg1,$reg2,$idx)=@_;
92 local($t,$post);
93 local($ret)="$size PTR ";
94
95 $addr =~ s/^\s+//;
96 if ($addr =~ /^(.+)\+(.+)$/)
97 {
98 $reg2=&conv($1);
99 $addr="_$2";
100 }
101 elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i)
102 {
103 $addr="_$addr";
104 }
105
106 if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }
107
108 $reg1="$regs{$reg1}" if defined($regs{$reg1});
109 $reg2="$regs{$reg2}" if defined($regs{$reg2});
110 if (($addr ne "") && ($addr ne 0))
111 {
112 if ($addr !~ /^-/)
113 { $ret.=$addr; }
114 else { $post=$addr; }
115 }
116 if ($reg2 ne "")
117 {
118 $t="";
119 $t="*$idx" if ($idx != 0);
120 $reg1="+".$reg1 if ("$reg1$post" ne "");
121 $ret.="[$reg2$t$reg1$post]";
122 }
123 else
124 {
125 $ret.="[$reg1$post]"
126 }
127 $ret =~ s/\[\]//; # in case $addr was the only argument
128 return($ret);
129 }
130
131sub main'mov { &out2("mov",@_); }
132sub main'movb { &out2("mov",@_); }
133sub main'and { &out2("and",@_); }
134sub main'or { &out2("or",@_); }
135sub main'shl { &out2("shl",@_); }
136sub main'shr { &out2("shr",@_); }
137sub main'xor { &out2("xor",@_); }
138sub main'xorb { &out2("xor",@_); }
139sub main'add { &out2("add",@_); }
140sub main'adc { &out2("adc",@_); }
141sub main'sub { &out2("sub",@_); }
142sub main'sbb { &out2("sbb",@_); }
143sub main'rotl { &out2("rol",@_); }
144sub main'rotr { &out2("ror",@_); }
145sub main'exch { &out2("xchg",@_); }
146sub main'cmp { &out2("cmp",@_); }
147sub main'lea { &out2("lea",@_); }
148sub main'mul { &out1("mul",@_); }
149sub main'imul { &out2("imul",@_); }
150sub main'div { &out1("div",@_); }
151sub main'dec { &out1("dec",@_); }
152sub main'inc { &out1("inc",@_); }
153sub main'jmp { &out1("jmp",@_); }
154sub main'jmp_ptr { &out1p("jmp",@_); }
155sub main'je { &out1("je",@_); }
156sub main'jle { &out1("jle",@_); }
157sub main'jz { &out1("jz",@_); }
158sub main'jge { &out1("jge",@_); }
159sub main'jl { &out1("jl",@_); }
160sub main'ja { &out1("ja",@_); }
161sub main'jae { &out1("jae",@_); }
162sub main'jb { &out1("jb",@_); }
163sub main'jbe { &out1("jbe",@_); }
164sub main'jc { &out1("jc",@_); }
165sub main'jnc { &out1("jnc",@_); }
166sub main'jnz { &out1("jnz",@_); }
167sub main'jne { &out1("jne",@_); }
168sub main'jno { &out1("jno",@_); }
169sub main'push { &out1("push",@_); $stack+=4; }
170sub main'pop { &out1("pop",@_); $stack-=4; }
171sub main'pushf { &out0("pushfd"); $stack+=4; }
172sub main'popf { &out0("popfd"); $stack-=4; }
173sub main'bswap { &out1("bswap",@_); &using486(); }
174sub main'not { &out1("not",@_); }
175sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
176sub main'call_ptr { &out1p("call",@_); }
177sub main'ret { &out0("ret"); }
178sub main'nop { &out0("nop"); }
179sub main'test { &out2("test",@_); }
180sub main'bt { &out2("bt",@_); }
181sub main'leave { &out0("leave"); }
182sub main'cpuid { &out0("DW\t0A20Fh"); }
183sub main'rdtsc { &out0("DW\t0310Fh"); }
184sub main'halt { &out0("hlt"); }
185sub main'movz { &out2("movzx",@_); }
186sub main'neg { &out1("neg",@_); }
187sub main'cld { &out0("cld"); }
188
189# SSE2
190sub main'emms { &out0("emms"); }
191sub main'movd { &out2("movd",@_); }
192sub main'movq { &out2("movq",@_); }
193sub main'movdqu { &out2("movdqu",@_); }
194sub main'movdqa { &out2("movdqa",@_); }
195sub main'movdq2q{ &out2("movdq2q",@_); }
196sub main'movq2dq{ &out2("movq2dq",@_); }
197sub main'paddq { &out2("paddq",@_); }
198sub main'pmuludq{ &out2("pmuludq",@_); }
199sub main'psrlq { &out2("psrlq",@_); }
200sub main'psllq { &out2("psllq",@_); }
201sub main'pxor { &out2("pxor",@_); }
202sub main'por { &out2("por",@_); }
203sub main'pand { &out2("pand",@_); }
204
205sub out2
206 {
207 local($name,$p1,$p2)=@_;
208 local($l,$t,$line);
209
210 $line="\t$name\t";
211 $t=&conv($p1).",";
212 $l=length($t);
213 $line.="$t";
214 $l=4-($l+9)/8;
215 $line.="\t" x $l;
216 $line.=&conv($p2);
217 if ($line=~/\bxmm[0-7]\b/i) { $line=~s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i; }
218 push(@out,$line."\n");
219 }
220
221sub out0
222 {
223 local($name)=@_;
224
225 push(@out,"\t$name\n");
226 }
227
228sub out1
229 {
230 local($name,$p1)=@_;
231 local($l,$t);
232
233 push(@out,"\t$name\t".&conv($p1)."\n");
234 }
235
236sub conv
237 {
238 local($p)=@_;
239
240 $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
241 return $p;
242 }
243
244sub using486
245 {
246 return if $using486;
247 $using486++;
248 grep(s/\.386/\.486/,@out);
249 }
250
251sub main'file
252 {
253 local($file)=@_;
254
255 local($tmp)=<<"EOF";
256 TITLE $file.asm
257 .386
258.model FLAT
259_TEXT\$ SEGMENT PAGE 'CODE'
260
261EOF
262 push(@out,$tmp);
263 }
264
265sub main'function_begin
266 {
267 local($func,$extra)=@_;
268
269 push(@labels,$func);
270
271 local($tmp)=<<"EOF";
272PUBLIC _$func
273$extra
274_$func PROC NEAR
275 push ebp
276 push ebx
277 push esi
278 push edi
279EOF
280 push(@out,$tmp);
281 $stack=20;
282 }
283
284sub main'function_begin_B
285 {
286 local($func,$extra)=@_;
287
288 local($tmp)=<<"EOF";
289PUBLIC _$func
290$extra
291_$func PROC NEAR
292EOF
293 push(@out,$tmp);
294 $stack=4;
295 }
296
297sub main'function_end
298 {
299 local($func)=@_;
300
301 local($tmp)=<<"EOF";
302 pop edi
303 pop esi
304 pop ebx
305 pop ebp
306 ret
307_$func ENDP
308EOF
309 push(@out,$tmp);
310 $stack=0;
311 %label=();
312 }
313
314sub main'function_end_B
315 {
316 local($func)=@_;
317
318 local($tmp)=<<"EOF";
319_$func ENDP
320EOF
321 push(@out,$tmp);
322 $stack=0;
323 %label=();
324 }
325
326sub main'function_end_A
327 {
328 local($func)=@_;
329
330 local($tmp)=<<"EOF";
331 pop edi
332 pop esi
333 pop ebx
334 pop ebp
335 ret
336EOF
337 push(@out,$tmp);
338 }
339
340sub main'file_end
341 {
342 # try to detect if SSE2 or MMX extensions were used...
343 my $xmmheader=<<___;
344.686
345.XMM
346IF \@Version LT 800
347XMMWORD STRUCT 16
348 DQ 2 dup (?)
349XMMWORD ENDS
350ENDIF
351___
352 if (grep {/\b[x]?mm[0-7]\b/i} @out) {
353 grep {s/\.[3-7]86/$xmmheader/} @out;
354 }
355 push(@out,"_TEXT\$ ENDS\n");
356 push(@out,"END\n");
357 }
358
359sub main'wparam
360 {
361 local($num)=@_;
362
363 return(&main'DWP($stack+$num*4,"esp","",0));
364 }
365
366sub main'swtmp
367 {
368 return(&main'DWP($_[0]*4,"esp","",0));
369 }
370
371# Should use swtmp, which is above esp. Linix can trash the stack above esp
372#sub main'wtmp
373# {
374# local($num)=@_;
375#
376# return(&main'DWP(-(($num+1)*4),"esp","",0));
377# }
378
379sub main'comment
380 {
381 foreach (@_)
382 {
383 push(@out,"\t; $_\n");
384 }
385 }
386
387sub main'public_label
388 {
389 $label{$_[0]}="_$_[0]" if (!defined($label{$_[0]}));
390 push(@out,"PUBLIC\t$label{$_[0]}\n");
391 }
392
393sub main'label
394 {
395 if (!defined($label{$_[0]}))
396 {
397 $label{$_[0]}="\$${label}${_[0]}";
398 $label++;
399 }
400 return($label{$_[0]});
401 }
402
403sub main'set_label
404 {
405 if (!defined($label{$_[0]}))
406 {
407 $label{$_[0]}="\$${label}${_[0]}";
408 $label++;
409 }
410 if ($_[1]!=0 && $_[1]>1)
411 {
412 main'align($_[1]);
413 }
414 if((defined $_[2]) && ($_[2] == 1))
415 {
416 push(@out,"$label{$_[0]}::\n");
417 }
418 elsif ($label{$_[0]} !~ /^\$/)
419 {
420 push(@out,"$label{$_[0]}\tLABEL PTR\n");
421 }
422 else
423 {
424 push(@out,"$label{$_[0]}:\n");
425 }
426 }
427
428sub main'data_byte
429 {
430 push(@out,"\tDB\t".join(',',@_)."\n");
431 }
432
433sub main'data_word
434 {
435 push(@out,"\tDD\t".join(',',@_)."\n");
436 }
437
438sub main'align
439 {
440 push(@out,"\tALIGN\t$_[0]\n");
441 }
442
443sub out1p
444 {
445 local($name,$p1)=@_;
446 local($l,$t);
447
448 push(@out,"\t$name\t".&conv($p1)."\n");
449 }
450
451sub main'picmeup
452 {
453 local($dst,$sym)=@_;
454 &main'lea($dst,&main'DWP($sym));
455 }
456
457sub main'blindpop { &out1("pop",@_); }
458
459sub main'initseg
460 {
461 local($f)=@_;
462 local($tmp)=<<___;
463OPTION DOTNAME
464.CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
465EXTRN _$f:NEAR
466DD _$f
467.CRT\$XCU ENDS
468___
469 push(@out,$tmp);
470 }
471
4721;
diff --git a/src/lib/libcrypto/perlasm/x86nasm.pl b/src/lib/libcrypto/perlasm/x86nasm.pl
new file mode 100644
index 0000000000..fa38f89c09
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86nasm.pl
@@ -0,0 +1,455 @@
1#!/usr/local/bin/perl
2
3package x86nasm;
4
5$label="L000";
6$under=($main'netware)?'':'_';
7
8%lb=( 'eax', 'al',
9 'ebx', 'bl',
10 'ecx', 'cl',
11 'edx', 'dl',
12 'ax', 'al',
13 'bx', 'bl',
14 'cx', 'cl',
15 'dx', 'dl',
16 );
17
18%hb=( 'eax', 'ah',
19 'ebx', 'bh',
20 'ecx', 'ch',
21 'edx', 'dh',
22 'ax', 'ah',
23 'bx', 'bh',
24 'cx', 'ch',
25 'dx', 'dh',
26 );
27
28sub main'asm_init_output { @out=(); }
29sub main'asm_get_output { return(@out); }
30sub main'get_labels { return(@labels); }
31
32sub main'external_label
33{
34 push(@labels,@_);
35 foreach (@_) {
36 push(@out,".") if ($main'mwerks);
37 push(@out, "extern\t${under}$_\n");
38 }
39}
40
41sub main'LB
42 {
43 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
44 return($lb{$_[0]});
45 }
46
47sub main'HB
48 {
49 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
50 return($hb{$_[0]});
51 }
52
53sub main'BP
54 {
55 &get_mem("BYTE",@_);
56 }
57
58sub main'DWP
59 {
60 &get_mem("DWORD",@_);
61 }
62
63sub main'QWP
64 {
65 &get_mem("",@_);
66 }
67
68sub main'BC
69 {
70 return (($main'mwerks)?"":"BYTE ")."@_";
71 }
72
73sub main'DWC
74 {
75 return (($main'mwerks)?"":"DWORD ")."@_";
76 }
77
78sub main'stack_push
79 {
80 my($num)=@_;
81 $stack+=$num*4;
82 &main'sub("esp",$num*4);
83 }
84
85sub main'stack_pop
86 {
87 my($num)=@_;
88 $stack-=$num*4;
89 &main'add("esp",$num*4);
90 }
91
92sub get_mem
93 {
94 my($size,$addr,$reg1,$reg2,$idx)=@_;
95 my($t,$post);
96 my($ret)=$size;
97 if ($ret ne "")
98 {
99 $ret .= " PTR" if ($main'mwerks);
100 $ret .= " ";
101 }
102 $ret .= "[";
103 $addr =~ s/^\s+//;
104 if ($addr =~ /^(.+)\+(.+)$/)
105 {
106 $reg2=&conv($1);
107 $addr="$under$2";
108 }
109 elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i)
110 {
111 $addr="$under$addr";
112 }
113
114 if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }
115
116 $reg1="$regs{$reg1}" if defined($regs{$reg1});
117 $reg2="$regs{$reg2}" if defined($regs{$reg2});
118 if (($addr ne "") && ($addr ne 0))
119 {
120 if ($addr !~ /^-/)
121 { $ret.="${addr}+"; }
122 else { $post=$addr; }
123 }
124 if ($reg2 ne "")
125 {
126 $t="";
127 $t="*$idx" if ($idx != 0);
128 $reg1="+".$reg1 if ("$reg1$post" ne "");
129 $ret.="$reg2$t$reg1$post]";
130 }
131 else
132 {
133 $ret.="$reg1$post]"
134 }
135 $ret =~ s/\+\]/]/; # in case $addr was the only argument
136 return($ret);
137 }
138
139sub main'mov { &out2("mov",@_); }
140sub main'movb { &out2("mov",@_); }
141sub main'and { &out2("and",@_); }
142sub main'or { &out2("or",@_); }
143sub main'shl { &out2("shl",@_); }
144sub main'shr { &out2("shr",@_); }
145sub main'xor { &out2("xor",@_); }
146sub main'xorb { &out2("xor",@_); }
147sub main'add { &out2("add",@_); }
148sub main'adc { &out2("adc",@_); }
149sub main'sub { &out2("sub",@_); }
150sub main'sbb { &out2("sbb",@_); }
151sub main'rotl { &out2("rol",@_); }
152sub main'rotr { &out2("ror",@_); }
153sub main'exch { &out2("xchg",@_); }
154sub main'cmp { &out2("cmp",@_); }
155sub main'lea { &out2("lea",@_); }
156sub main'mul { &out1("mul",@_); }
157sub main'imul { &out2("imul",@_); }
158sub main'div { &out1("div",@_); }
159sub main'dec { &out1("dec",@_); }
160sub main'inc { &out1("inc",@_); }
161sub main'jmp { &out1("jmp",@_); }
162sub main'jmp_ptr { &out1p("jmp",@_); }
163
164# This is a bit of a kludge: declare all branches as NEAR.
165$near=($main'mwerks)?'':'NEAR';
166sub main'je { &out1("je $near",@_); }
167sub main'jle { &out1("jle $near",@_); }
168sub main'jz { &out1("jz $near",@_); }
169sub main'jge { &out1("jge $near",@_); }
170sub main'jl { &out1("jl $near",@_); }
171sub main'ja { &out1("ja $near",@_); }
172sub main'jae { &out1("jae $near",@_); }
173sub main'jb { &out1("jb $near",@_); }
174sub main'jbe { &out1("jbe $near",@_); }
175sub main'jc { &out1("jc $near",@_); }
176sub main'jnc { &out1("jnc $near",@_); }
177sub main'jnz { &out1("jnz $near",@_); }
178sub main'jne { &out1("jne $near",@_); }
179sub main'jno { &out1("jno $near",@_); }
180
181sub main'push { &out1("push",@_); $stack+=4; }
182sub main'pop { &out1("pop",@_); $stack-=4; }
183sub main'pushf { &out0("pushfd"); $stack+=4; }
184sub main'popf { &out0("popfd"); $stack-=4; }
185sub main'bswap { &out1("bswap",@_); &using486(); }
186sub main'not { &out1("not",@_); }
187sub main'call { &out1("call",($_[0]=~/^\@L/?'':$under).$_[0]); }
188sub main'call_ptr { &out1p("call",@_); }
189sub main'ret { &out0("ret"); }
190sub main'nop { &out0("nop"); }
191sub main'test { &out2("test",@_); }
192sub main'bt { &out2("bt",@_); }
193sub main'leave { &out0("leave"); }
194sub main'cpuid { &out0("cpuid"); }
195sub main'rdtsc { &out0("rdtsc"); }
196sub main'halt { &out0("hlt"); }
197sub main'movz { &out2("movzx",@_); }
198sub main'neg { &out1("neg",@_); }
199sub main'cld { &out0("cld"); }
200
201# SSE2
202sub main'emms { &out0("emms"); }
203sub main'movd { &out2("movd",@_); }
204sub main'movq { &out2("movq",@_); }
205sub main'movdqu { &out2("movdqu",@_); }
206sub main'movdqa { &out2("movdqa",@_); }
207sub main'movdq2q{ &out2("movdq2q",@_); }
208sub main'movq2dq{ &out2("movq2dq",@_); }
209sub main'paddq { &out2("paddq",@_); }
210sub main'pmuludq{ &out2("pmuludq",@_); }
211sub main'psrlq { &out2("psrlq",@_); }
212sub main'psllq { &out2("psllq",@_); }
213sub main'pxor { &out2("pxor",@_); }
214sub main'por { &out2("por",@_); }
215sub main'pand { &out2("pand",@_); }
216
217sub out2
218 {
219 my($name,$p1,$p2)=@_;
220 my($l,$t);
221
222 push(@out,"\t$name\t");
223 if (!$main'mwerks and $name eq "lea")
224 {
225 $p1 =~ s/^[^\[]*\[/\[/;
226 $p2 =~ s/^[^\[]*\[/\[/;
227 }
228 $t=&conv($p1).",";
229 $l=length($t);
230 push(@out,$t);
231 $l=4-($l+9)/8;
232 push(@out,"\t" x $l);
233 push(@out,&conv($p2));
234 push(@out,"\n");
235 }
236
237sub out0
238 {
239 my($name)=@_;
240
241 push(@out,"\t$name\n");
242 }
243
244sub out1
245 {
246 my($name,$p1)=@_;
247 my($l,$t);
248 push(@out,"\t$name\t".&conv($p1)."\n");
249 }
250
251sub conv
252 {
253 my($p)=@_;
254 $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
255 return $p;
256 }
257
258sub using486
259 {
260 return if $using486;
261 $using486++;
262 grep(s/\.386/\.486/,@out);
263 }
264
265sub main'file
266 {
267 if ($main'mwerks) { push(@out,".section\t.text\n"); }
268 else {
269 local $tmp=<<___;
270%ifdef __omf__
271section code use32 class=code
272%else
273section .text
274%endif
275___
276 push(@out,$tmp);
277 }
278 }
279
280sub main'function_begin
281 {
282 my($func,$extra)=@_;
283
284 push(@labels,$func);
285 push(@out,".") if ($main'mwerks);
286 my($tmp)=<<"EOF";
287global $under$func
288$under$func:
289 push ebp
290 push ebx
291 push esi
292 push edi
293EOF
294 push(@out,$tmp);
295 $stack=20;
296 }
297
298sub main'function_begin_B
299 {
300 my($func,$extra)=@_;
301 push(@out,".") if ($main'mwerks);
302 my($tmp)=<<"EOF";
303global $under$func
304$under$func:
305EOF
306 push(@out,$tmp);
307 $stack=4;
308 }
309
310sub main'function_end
311 {
312 my($func)=@_;
313
314 my($tmp)=<<"EOF";
315 pop edi
316 pop esi
317 pop ebx
318 pop ebp
319 ret
320EOF
321 push(@out,$tmp);
322 $stack=0;
323 %label=();
324 }
325
326sub main'function_end_B
327 {
328 $stack=0;
329 %label=();
330 }
331
332sub main'function_end_A
333 {
334 my($func)=@_;
335
336 my($tmp)=<<"EOF";
337 pop edi
338 pop esi
339 pop ebx
340 pop ebp
341 ret
342EOF
343 push(@out,$tmp);
344 }
345
346sub main'file_end
347 {
348 }
349
350sub main'wparam
351 {
352 my($num)=@_;
353
354 return(&main'DWP($stack+$num*4,"esp","",0));
355 }
356
357sub main'swtmp
358 {
359 return(&main'DWP($_[0]*4,"esp","",0));
360 }
361
362# Should use swtmp, which is above esp. Linix can trash the stack above esp
363#sub main'wtmp
364# {
365# my($num)=@_;
366#
367# return(&main'DWP(-(($num+1)*4),"esp","",0));
368# }
369
370sub main'comment
371 {
372 foreach (@_)
373 {
374 push(@out,"\t; $_\n");
375 }
376 }
377
378sub main'public_label
379 {
380 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
381 push(@out,".") if ($main'mwerks);
382 push(@out,"global\t$label{$_[0]}\n");
383 }
384
385sub main'label
386 {
387 if (!defined($label{$_[0]}))
388 {
389 $label{$_[0]}="\@${label}${_[0]}";
390 $label++;
391 }
392 return($label{$_[0]});
393 }
394
395sub main'set_label
396 {
397 if (!defined($label{$_[0]}))
398 {
399 $label{$_[0]}="\@${label}${_[0]}";
400 $label++;
401 }
402 if ($_[1]!=0 && $_[1]>1)
403 {
404 main'align($_[1]);
405 }
406 push(@out,"$label{$_[0]}:\n");
407 }
408
409sub main'data_byte
410 {
411 push(@out,(($main'mwerks)?".byte\t":"DB\t").join(',',@_)."\n");
412 }
413
414sub main'data_word
415 {
416 push(@out,(($main'mwerks)?".long\t":"DD\t").join(',',@_)."\n");
417 }
418
419sub main'align
420 {
421 push(@out,".") if ($main'mwerks);
422 push(@out,"align\t$_[0]\n");
423 }
424
425sub out1p
426 {
427 my($name,$p1)=@_;
428 my($l,$t);
429
430 push(@out,"\t$name\t".&conv($p1)."\n");
431 }
432
433sub main'picmeup
434 {
435 local($dst,$sym)=@_;
436 &main'lea($dst,&main'DWP($sym));
437 }
438
439sub main'blindpop { &out1("pop",@_); }
440
441sub main'initseg
442 {
443 local($f)=@_;
444 if ($main'win32)
445 {
446 local($tmp)=<<___;
447segment .CRT\$XCU data
448extern $under$f
449DD $under$f
450___
451 push(@out,$tmp);
452 }
453 }
454
4551;
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl
new file mode 100644
index 0000000000..ae8f0964dc
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86unix.pl
@@ -0,0 +1,808 @@
1#!/usr/local/bin/perl
2
3package x86unix; # GAS actually...
4
5$label="L000";
6$const="";
7$constl=0;
8
9$align=($main'aout)?"4":"16";
10$under=($main'aout or $main'coff)?"_":"";
11$dot=($main'aout)?"":".";
12$com_start="#" if ($main'aout or $main'coff);
13
14sub main'asm_init_output { @out=(); }
15sub main'asm_get_output { return(@out); }
16sub main'get_labels { return(@labels); }
17sub main'external_label { push(@labels,@_); }
18
19if ($main'openbsd)
20 {
21 $com_start='/*';
22 $com_end='*/';
23 }
24
25if ($main'cpp)
26 {
27 $align="ALIGN";
28 $under="";
29 $com_start='/*';
30 $com_end='*/';
31 }
32
33%lb=( 'eax', '%al',
34 'ebx', '%bl',
35 'ecx', '%cl',
36 'edx', '%dl',
37 'ax', '%al',
38 'bx', '%bl',
39 'cx', '%cl',
40 'dx', '%dl',
41 );
42
43%hb=( 'eax', '%ah',
44 'ebx', '%bh',
45 'ecx', '%ch',
46 'edx', '%dh',
47 'ax', '%ah',
48 'bx', '%bh',
49 'cx', '%ch',
50 'dx', '%dh',
51 );
52
53%regs=( 'eax', '%eax',
54 'ebx', '%ebx',
55 'ecx', '%ecx',
56 'edx', '%edx',
57 'esi', '%esi',
58 'edi', '%edi',
59 'ebp', '%ebp',
60 'esp', '%esp',
61
62 'mm0', '%mm0',
63 'mm1', '%mm1',
64 'mm2', '%mm2',
65 'mm3', '%mm3',
66 'mm4', '%mm4',
67 'mm5', '%mm5',
68 'mm6', '%mm6',
69 'mm7', '%mm7',
70
71 'xmm0', '%xmm0',
72 'xmm1', '%xmm1',
73 'xmm2', '%xmm2',
74 'xmm3', '%xmm3',
75 'xmm4', '%xmm4',
76 'xmm5', '%xmm5',
77 'xmm6', '%xmm6',
78 'xmm7', '%xmm7',
79 );
80
81%reg_val=(
82 'eax', 0x00,
83 'ebx', 0x03,
84 'ecx', 0x01,
85 'edx', 0x02,
86 'esi', 0x06,
87 'edi', 0x07,
88 'ebp', 0x05,
89 'esp', 0x04,
90 );
91
92sub main'LB
93 {
94 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
95 return($lb{$_[0]});
96 }
97
98sub main'HB
99 {
100 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
101 return($hb{$_[0]});
102 }
103
104sub main'DWP
105 {
106 local($addr,$reg1,$reg2,$idx)=@_;
107
108 $ret="";
109 $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
110 $reg1="$regs{$reg1}" if defined($regs{$reg1});
111 $reg2="$regs{$reg2}" if defined($regs{$reg2});
112 $ret.=$addr if ($addr ne "") && ($addr ne 0);
113 if ($reg2 ne "")
114 {
115 if($idx ne "" && $idx != 0)
116 { $ret.="($reg1,$reg2,$idx)"; }
117 else
118 { $ret.="($reg1,$reg2)"; }
119 }
120 elsif ($reg1 ne "")
121 { $ret.="($reg1)" }
122 return($ret);
123 }
124
125sub main'QWP
126 {
127 return(&main'DWP(@_));
128 }
129
130sub main'BP
131 {
132 return(&main'DWP(@_));
133 }
134
135sub main'BC
136 {
137 return @_;
138 }
139
140sub main'DWC
141 {
142 return @_;
143 }
144
145#sub main'BP
146# {
147# local($addr,$reg1,$reg2,$idx)=@_;
148#
149# $ret="";
150#
151# $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
152# $reg1="$regs{$reg1}" if defined($regs{$reg1});
153# $reg2="$regs{$reg2}" if defined($regs{$reg2});
154# $ret.=$addr if ($addr ne "") && ($addr ne 0);
155# if ($reg2 ne "")
156# { $ret.="($reg1,$reg2,$idx)"; }
157# else
158# { $ret.="($reg1)" }
159# return($ret);
160# }
161
162sub main'mov { &out2("movl",@_); }
163sub main'movb { &out2("movb",@_); }
164sub main'and { &out2("andl",@_); }
165sub main'or { &out2("orl",@_); }
166sub main'shl { &out2("sall",@_); }
167sub main'shr { &out2("shrl",@_); }
168sub main'xor { &out2("xorl",@_); }
169sub main'xorb { &out2("xorb",@_); }
170sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); }
171sub main'adc { &out2("adcl",@_); }
172sub main'sub { &out2("subl",@_); }
173sub main'sbb { &out2("sbbl",@_); }
174sub main'rotl { &out2("roll",@_); }
175sub main'rotr { &out2("rorl",@_); }
176sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); }
177sub main'cmp { &out2("cmpl",@_); }
178sub main'lea { &out2("leal",@_); }
179sub main'mul { &out1("mull",@_); }
180sub main'imul { &out2("imull",@_); }
181sub main'div { &out1("divl",@_); }
182sub main'jmp { &out1("jmp",@_); }
183sub main'jmp_ptr { &out1p("jmp",@_); }
184sub main'je { &out1("je",@_); }
185sub main'jle { &out1("jle",@_); }
186sub main'jne { &out1("jne",@_); }
187sub main'jnz { &out1("jnz",@_); }
188sub main'jz { &out1("jz",@_); }
189sub main'jge { &out1("jge",@_); }
190sub main'jl { &out1("jl",@_); }
191sub main'ja { &out1("ja",@_); }
192sub main'jae { &out1("jae",@_); }
193sub main'jb { &out1("jb",@_); }
194sub main'jbe { &out1("jbe",@_); }
195sub main'jc { &out1("jc",@_); }
196sub main'jnc { &out1("jnc",@_); }
197sub main'jno { &out1("jno",@_); }
198sub main'dec { &out1("decl",@_); }
199sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
200sub main'push { &out1("pushl",@_); $stack+=4; }
201sub main'pop { &out1("popl",@_); $stack-=4; }
202sub main'pushf { &out0("pushfl"); $stack+=4; }
203sub main'popf { &out0("popfl"); $stack-=4; }
204sub main'not { &out1("notl",@_); }
205sub main'call { my $pre=$under;
206 foreach $i (%label)
207 { if ($label{$i} eq $_[0]) { $pre=''; last; } }
208 &out1("call",$pre.$_[0]);
209 }
210sub main'call_ptr { &out1p("call",@_); }
211sub main'ret { &out0("ret"); }
212sub main'nop { &out0("nop"); }
213sub main'test { &out2("testl",@_); }
214sub main'bt { &out2("btl",@_); }
215sub main'leave { &out0("leave"); }
216sub main'cpuid { &out0(".byte\t0x0f,0xa2"); }
217sub main'rdtsc { &out0(".byte\t0x0f,0x31"); }
218sub main'halt { &out0("hlt"); }
219sub main'movz { &out2("movzbl",@_); }
220sub main'neg { &out1("negl",@_); }
221sub main'cld { &out0("cld"); }
222
223# SSE2
224sub main'emms { &out0("emms"); }
225sub main'movd { &out2("movd",@_); }
226sub main'movdqu { &out2("movdqu",@_); }
227sub main'movdqa { &out2("movdqa",@_); }
228sub main'movdq2q{ &out2("movdq2q",@_); }
229sub main'movq2dq{ &out2("movq2dq",@_); }
230sub main'paddq { &out2("paddq",@_); }
231sub main'pmuludq{ &out2("pmuludq",@_); }
232sub main'psrlq { &out2("psrlq",@_); }
233sub main'psllq { &out2("psllq",@_); }
234sub main'pxor { &out2("pxor",@_); }
235sub main'por { &out2("por",@_); }
236sub main'pand { &out2("pand",@_); }
237sub main'movq {
238 local($p1,$p2,$optimize)=@_;
239 if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
240 # movq between mmx registers can sink Intel CPUs
241 { push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n"); }
242 else { &out2("movq",@_); }
243 }
244
245# The bswapl instruction is new for the 486. Emulate if i386.
246sub main'bswap
247 {
248 if ($main'i386)
249 {
250 &main'comment("bswapl @_");
251 &main'exch(main'HB(@_),main'LB(@_));
252 &main'rotr(@_,16);
253 &main'exch(main'HB(@_),main'LB(@_));
254 }
255 else
256 {
257 &out1("bswapl",@_);
258 }
259 }
260
261sub out2
262 {
263 local($name,$p1,$p2)=@_;
264 local($l,$ll,$t);
265 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
266 "rcll",0xD1D0,"rcrl",0xD1D8,
267 "shll",0xD1E0,"shrl",0xD1E8,
268 "sarl",0xD1F8);
269
270 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
271 {
272 $op=$special{$name}|$reg_val{$p1};
273 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
274 $tmp2=sprintf(".byte %d\t",$op &0xff);
275 push(@out,$tmp1);
276 push(@out,$tmp2);
277
278 $p2=&conv($p2);
279 $p1=&conv($p1);
280 &main'comment("$name $p2 $p1");
281 return;
282 }
283
284 push(@out,"\t$name\t");
285 $t=&conv($p2).",";
286 $l=length($t);
287 push(@out,$t);
288 $ll=4-($l+9)/8;
289 $tmp1=sprintf("\t" x $ll);
290 push(@out,$tmp1);
291 push(@out,&conv($p1)."\n");
292 }
293
294sub out1
295 {
296 local($name,$p1)=@_;
297 local($l,$t);
298 local(%special)=("bswapl",0x0FC8);
299
300 if ((defined($special{$name})) && defined($regs{$p1}))
301 {
302 $op=$special{$name}|$reg_val{$p1};
303 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
304 $tmp2=sprintf(".byte %d\t",$op &0xff);
305 push(@out,$tmp1);
306 push(@out,$tmp2);
307
308 $p2=&conv($p2);
309 $p1=&conv($p1);
310 &main'comment("$name $p2 $p1");
311 return;
312 }
313
314 push(@out,"\t$name\t".&conv($p1)."\n");
315 }
316
317sub out1p
318 {
319 local($name,$p1)=@_;
320 local($l,$t);
321
322 push(@out,"\t$name\t*".&conv($p1)."\n");
323 }
324
325sub out0
326 {
327 push(@out,"\t$_[0]\n");
328 }
329
330sub conv
331 {
332 local($p)=@_;
333
334# $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
335
336 $p=$regs{$p} if (defined($regs{$p}));
337
338 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
339 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
340 return $p;
341 }
342
343sub main'file
344 {
345 local($file)=@_;
346
347 if ($main'openbsd)
348 { push(@out,"#include <machine/asm.h>\n"); }
349
350 local($tmp)=<<"EOF";
351 .file "$file.s"
352EOF
353 push(@out,$tmp);
354 }
355
356sub main'function_begin
357 {
358 local($func,$junk,$llabel)=@_;
359
360 &main'external_label($func);
361 $func=$under.$func;
362
363 if ($main'openbsd)
364 {
365 push (@out, "\nENTRY($func)\n");
366 push (@out, "$llabel:\n") if $llabel;
367 goto skip;
368 }
369
370 local($tmp)=<<"EOF";
371.text
372.globl $func
373EOF
374 push(@out,$tmp);
375 if ($main'cpp)
376 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
377 elsif ($main'coff)
378 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
379 elsif ($main'aout and !$main'pic)
380 { }
381 else { $tmp=push(@out,".type\t$func,\@function\n"); }
382 push(@out,".align\t$align\n");
383 push(@out,"$func:\n");
384skip:
385 $tmp=<<"EOF";
386 pushl %ebp
387 pushl %ebx
388 pushl %esi
389 pushl %edi
390
391EOF
392 push(@out,$tmp);
393 $stack=20;
394 }
395
396sub main'function_begin_B
397 {
398 local($func,$extra)=@_;
399
400 &main'external_label($func);
401 $func=$under.$func;
402
403 if ($main'openbsd)
404 { push(@out, "\nENTRY($func)\n"); goto skip; }
405
406 local($tmp)=<<"EOF";
407.text
408.globl $func
409EOF
410 push(@out,$tmp);
411 if ($main'cpp)
412 { push(@out,"TYPE($func,\@function)\n"); }
413 elsif ($main'coff)
414 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
415 elsif ($main'aout and !$main'pic)
416 { }
417 else { push(@out,".type $func,\@function\n"); }
418 push(@out,".align\t$align\n");
419 push(@out,"$func:\n");
420skip:
421 $stack=4;
422 }
423
424# Like function_begin_B but with static linkage
425sub main'function_begin_C
426 {
427 local($func,$extra)=@_;
428
429 &main'external_label($func);
430 $func=$under.$func;
431
432 if ($main'openbsd)
433 {
434 local($tmp)=<<"EOF";
435.text
436_ALIGN_TEXT
437.type $func,\@function
438$func:
439EOF
440 push(@out, $tmp);
441 goto skip;
442 }
443
444 local($tmp)=<<"EOF";
445.text
446.globl $func
447EOF
448 push(@out,$tmp);
449 if ($main'cpp)
450 { push(@out,"TYPE($func,\@function)\n"); }
451 elsif ($main'coff)
452 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
453 elsif ($main'aout and !$main'pic)
454 { }
455 else { push(@out,".type $func,\@function\n"); }
456 push(@out,".align\t$align\n");
457 push(@out,"$func:\n");
458skip:
459 $stack=4;
460 }
461
462sub main'function_end
463 {
464 local($func)=@_;
465
466 $func=$under.$func;
467
468 local($tmp)=<<"EOF";
469 popl %edi
470 popl %esi
471 popl %ebx
472 popl %ebp
473 ret
474${dot}L_${func}_end:
475EOF
476 push(@out,$tmp);
477
478 if ($main'cpp)
479 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
480 elsif ($main'coff or $main'aout)
481 { }
482 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
483 push(@out,".ident \"$func\"\n");
484 $stack=0;
485 %label=();
486 }
487
488sub main'function_end_A
489 {
490 local($func)=@_;
491
492 local($tmp)=<<"EOF";
493 popl %edi
494 popl %esi
495 popl %ebx
496 popl %ebp
497 ret
498EOF
499 push(@out,$tmp);
500 }
501
502sub main'function_end_B
503 {
504 local($func)=@_;
505
506 $func=$under.$func;
507
508 push(@out,"${dot}L_${func}_end:\n");
509 if ($main'cpp)
510 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
511 elsif ($main'coff or $main'aout)
512 { }
513 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
514 push(@out,".ident \"$func\"\n");
515 $stack=0;
516 %label=();
517 }
518
519sub main'function_end_C { function_end_B(@_); }
520
521sub main'wparam
522 {
523 local($num)=@_;
524
525 return(&main'DWP($stack+$num*4,"esp","",0));
526 }
527
528sub main'stack_push
529 {
530 local($num)=@_;
531 $stack+=$num*4;
532 &main'sub("esp",$num*4);
533 }
534
535sub main'stack_pop
536 {
537 local($num)=@_;
538 $stack-=$num*4;
539 &main'add("esp",$num*4);
540 }
541
542sub main'swtmp
543 {
544 return(&main'DWP($_[0]*4,"esp","",0));
545 }
546
547# Should use swtmp, which is above esp. Linix can trash the stack above esp
548#sub main'wtmp
549# {
550# local($num)=@_;
551#
552# return(&main'DWP(-($num+1)*4,"esp","",0));
553# }
554
555sub main'comment
556 {
557 if (!defined($com_start) or (!$main'openbsd && $main'elf))
558 { # Regarding $main'elf above...
559 # GNU and SVR4 as'es use different comment delimiters,
560 push(@out,"\n"); # so we just skip ELF comments...
561 return;
562 }
563 foreach (@_)
564 {
565 if (/^\s*$/)
566 { push(@out,"\n"); }
567 else
568 { push(@out,"\t$com_start $_ $com_end\n"); }
569 }
570 }
571
572sub main'public_label
573 {
574 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
575 push(@out,".globl\t$label{$_[0]}\n");
576 }
577
578sub main'label
579 {
580 if (!defined($label{$_[0]}))
581 {
582 $label{$_[0]}="${dot}${label}${_[0]}";
583 $label++;
584 }
585 return($label{$_[0]});
586 }
587
588sub main'set_label
589 {
590 if (!defined($label{$_[0]}))
591 {
592 $label{$_[0]}="${dot}${label}${_[0]}";
593 $label++;
594 }
595 if ($_[1]!=0)
596 {
597 if ($_[1]>1) { main'align($_[1]); }
598 else
599 {
600 if ($main'openbsd)
601 { push(@out,"_ALIGN_TEXT\n"); }
602 else
603 { push(@out,".align $align\n"); }
604 }
605 }
606 push(@out,"$label{$_[0]}:\n");
607 }
608
609sub main'file_end
610 {
611 # try to detect if SSE2 or MMX extensions were used on ELF platform...
612 if ($main'elf && grep {/\b%[x]*mm[0-7]\b|OPENSSL_ia32cap_P\b/i} @out) {
613 local($tmp);
614
615 push (@out,"\n.section\t.bss\n");
616 push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
617
618 return;
619 }
620
621 if ($const ne "")
622 {
623 push(@out,".section .rodata\n");
624 push(@out,$const);
625 $const="";
626 }
627 }
628
629sub main'data_byte
630 {
631 push(@out,"\t.byte\t".join(',',@_)."\n");
632 }
633
634sub main'data_word
635 {
636 push(@out,"\t.long\t".join(',',@_)."\n");
637 }
638
639sub main'align
640 {
641 my $val=$_[0],$p2,$i;
642 if ($main'aout) {
643 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
644 $val=$p2-1;
645 $val.=",0x90";
646 }
647 push(@out,".align\t$val\n");
648 if ($main'openbsd)
649 { push(@out,"_ALIGN_TEXT\n"); }
650 else
651 { push(@out,".align $tval\n"); }
652 }
653
654# debug output functions: puts, putx, printf
655
656sub main'puts
657 {
658 &pushvars();
659 &main'push('$Lstring' . ++$constl);
660 &main'call('puts');
661 $stack-=4;
662 &main'add("esp",4);
663 &popvars();
664
665 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
666 }
667
668sub main'putx
669 {
670 &pushvars();
671 &main'push($_[0]);
672 &main'push('$Lstring' . ++$constl);
673 &main'call('printf');
674 &main'add("esp",8);
675 $stack-=8;
676 &popvars();
677
678 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
679 }
680
681sub main'printf
682 {
683 $ostack = $stack;
684 &pushvars();
685 for ($i = @_ - 1; $i >= 0; $i--)
686 {
687 if ($i == 0) # change this to support %s format strings
688 {
689 &main'push('$Lstring' . ++$constl);
690 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
691 }
692 else
693 {
694 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
695 {
696 &main'push(($1 + $stack - $ostack) . '(%esp)');
697 }
698 else
699 {
700 &main'push($_[$i]);
701 }
702 }
703 }
704 &main'call('printf');
705 $stack-=4*@_;
706 &main'add("esp",4*@_);
707 &popvars();
708 }
709
710sub pushvars
711 {
712 &main'pushf();
713 &main'push("edx");
714 &main'push("ecx");
715 &main'push("eax");
716 }
717
718sub popvars
719 {
720 &main'pop("eax");
721 &main'pop("ecx");
722 &main'pop("edx");
723 &main'popf();
724 }
725
726sub main'picmeup
727 {
728 local($dst,$sym)=@_;
729 if ($main'cpp)
730 {
731 local($tmp)=<<___;
732#if (defined(ELF) || defined(SOL)) && defined(PIC)
733 call 1f
7341: popl $regs{$dst}
735 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
736 movl $sym\@GOT($regs{$dst}),$regs{$dst}
737#else
738 leal $sym,$regs{$dst}
739#endif
740___
741 push(@out,$tmp);
742 }
743 elsif ($main'openbsd)
744 {
745 push(@out, "#ifdef PIC\n");
746 push(@out, "\tPIC_PROLOGUE\n");
747 &main'mov($dst,"PIC_GOT($sym)");
748 push(@out, "\tPIC_EPILOGUE\n");
749 push(@out, "#else\n");
750 &main'lea($dst,&main'DWP($sym));
751 push(@out, "#endif\n");
752 }
753 elsif ($main'pic && ($main'elf || $main'aout))
754 {
755 &main'call(&main'label("PIC_me_up"));
756 &main'set_label("PIC_me_up");
757 &main'blindpop($dst);
758 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
759 &main'label("PIC_me_up") . "]");
760 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
761 }
762 else
763 {
764 &main'lea($dst,&main'DWP($sym));
765 }
766 }
767
768sub main'blindpop { &out1("popl",@_); }
769
770sub main'initseg
771 {
772 local($f)=@_;
773 local($tmp);
774 if ($main'elf)
775 {
776 $tmp=<<___;
777.section .init
778 PIC_PROLOGUE
779 call PIC_PLT($under$f)
780 PIC_EPILOGUE
781 jmp .Linitalign
782.align $align
783.Linitalign:
784___
785 }
786 elsif ($main'coff)
787 {
788 $tmp=<<___; # applies to both Cygwin and Mingw
789.section .ctors
790.long $under$f
791___
792 }
793 elsif ($main'aout)
794 {
795 local($ctor)="${under}_GLOBAL_\$I\$$f";
796 $tmp=".text\n";
797 $tmp.=".type $ctor,\@function\n" if ($main'pic);
798 $tmp.=<<___; # OpenBSD way...
799.globl $ctor
800.align 2
801$ctor:
802 jmp $under$f
803___
804 }
805 push(@out,$tmp) if ($tmp);
806 }
807
8081;