]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - crypto/openssl/crypto/perlasm/x86unix.pl
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / crypto / openssl / crypto / perlasm / x86unix.pl
1 #!/usr/local/bin/perl
2
3 package 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
14 sub main'asm_init_output { @out=(); }
15 sub main'asm_get_output { return(@out); }
16 sub main'get_labels { return(@labels); }
17 sub main'external_label { push(@labels,@_); }
18
19 if ($main'cpp)
20         {
21         $align="ALIGN";
22         $under="";
23         $com_start='/*';
24         $com_end='*/';
25         }
26
27 %lb=(   'eax',  '%al',
28         'ebx',  '%bl',
29         'ecx',  '%cl',
30         'edx',  '%dl',
31         'ax',   '%al',
32         'bx',   '%bl',
33         'cx',   '%cl',
34         'dx',   '%dl',
35         );
36
37 %hb=(   'eax',  '%ah',
38         'ebx',  '%bh',
39         'ecx',  '%ch',
40         'edx',  '%dh',
41         'ax',   '%ah',
42         'bx',   '%bh',
43         'cx',   '%ch',
44         'dx',   '%dh',
45         );
46
47 %regs=( 'eax',  '%eax',
48         'ebx',  '%ebx',
49         'ecx',  '%ecx',
50         'edx',  '%edx',
51         'esi',  '%esi',
52         'edi',  '%edi',
53         'ebp',  '%ebp',
54         'esp',  '%esp',
55
56         'mm0',  '%mm0',
57         'mm1',  '%mm1',
58         'mm2',  '%mm2',
59         'mm3',  '%mm3',
60         'mm4',  '%mm4',
61         'mm5',  '%mm5',
62         'mm6',  '%mm6',
63         'mm7',  '%mm7',
64
65         'xmm0', '%xmm0',
66         'xmm1', '%xmm1',
67         'xmm2', '%xmm2',
68         'xmm3', '%xmm3',
69         'xmm4', '%xmm4',
70         'xmm5', '%xmm5',
71         'xmm6', '%xmm6',
72         'xmm7', '%xmm7',
73         );
74
75 %reg_val=(
76         'eax',  0x00,
77         'ebx',  0x03,
78         'ecx',  0x01,
79         'edx',  0x02,
80         'esi',  0x06,
81         'edi',  0x07,
82         'ebp',  0x05,
83         'esp',  0x04,
84         );
85
86 sub main'LB
87         {
88         (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
89         return($lb{$_[0]});
90         }
91
92 sub main'HB
93         {
94         (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
95         return($hb{$_[0]});
96         }
97
98 sub main'DWP
99         {
100         local($addr,$reg1,$reg2,$idx)=@_;
101
102         $ret="";
103         $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
104         $reg1="$regs{$reg1}" if defined($regs{$reg1});
105         $reg2="$regs{$reg2}" if defined($regs{$reg2});
106         $ret.=$addr if ($addr ne "") && ($addr ne 0);
107         if ($reg2 ne "")
108                 {
109                 if($idx ne "" && $idx != 0)
110                     { $ret.="($reg1,$reg2,$idx)"; }
111                 else
112                     { $ret.="($reg1,$reg2)"; }
113                 }
114         elsif ($reg1 ne "")
115                 { $ret.="($reg1)" }
116         return($ret);
117         }
118
119 sub main'QWP
120         {
121         return(&main'DWP(@_));
122         }
123
124 sub main'BP
125         {
126         return(&main'DWP(@_));
127         }
128
129 sub main'BC
130         {
131         return @_;
132         }
133
134 sub main'DWC
135         {
136         return @_;
137         }
138
139 #sub main'BP
140 #       {
141 #       local($addr,$reg1,$reg2,$idx)=@_;
142 #
143 #       $ret="";
144 #
145 #       $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
146 #       $reg1="$regs{$reg1}" if defined($regs{$reg1});
147 #       $reg2="$regs{$reg2}" if defined($regs{$reg2});
148 #       $ret.=$addr if ($addr ne "") && ($addr ne 0);
149 #       if ($reg2 ne "")
150 #               { $ret.="($reg1,$reg2,$idx)"; }
151 #       else
152 #               { $ret.="($reg1)" }
153 #       return($ret);
154 #       }
155
156 sub main'mov    { &out2("movl",@_); }
157 sub main'movb   { &out2("movb",@_); }
158 sub main'and    { &out2("andl",@_); }
159 sub main'or     { &out2("orl",@_); }
160 sub main'shl    { &out2("sall",@_); }
161 sub main'shr    { &out2("shrl",@_); }
162 sub main'xor    { &out2("xorl",@_); }
163 sub main'xorb   { &out2("xorb",@_); }
164 sub main'add    { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); }
165 sub main'adc    { &out2("adcl",@_); }
166 sub main'sub    { &out2("subl",@_); }
167 sub main'sbb    { &out2("sbbl",@_); }
168 sub main'rotl   { &out2("roll",@_); }
169 sub main'rotr   { &out2("rorl",@_); }
170 sub main'exch   { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); }
171 sub main'cmp    { &out2("cmpl",@_); }
172 sub main'lea    { &out2("leal",@_); }
173 sub main'mul    { &out1("mull",@_); }
174 sub main'imul   { &out2("imull",@_); }
175 sub main'div    { &out1("divl",@_); }
176 sub main'jmp    { &out1("jmp",@_); }
177 sub main'jmp_ptr { &out1p("jmp",@_); }
178 sub main'je     { &out1("je",@_); }
179 sub main'jle    { &out1("jle",@_); }
180 sub main'jne    { &out1("jne",@_); }
181 sub main'jnz    { &out1("jnz",@_); }
182 sub main'jz     { &out1("jz",@_); }
183 sub main'jge    { &out1("jge",@_); }
184 sub main'jl     { &out1("jl",@_); }
185 sub main'ja     { &out1("ja",@_); }
186 sub main'jae    { &out1("jae",@_); }
187 sub main'jb     { &out1("jb",@_); }
188 sub main'jbe    { &out1("jbe",@_); }
189 sub main'jc     { &out1("jc",@_); }
190 sub main'jnc    { &out1("jnc",@_); }
191 sub main'jno    { &out1("jno",@_); }
192 sub main'dec    { &out1("decl",@_); }
193 sub main'inc    { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
194 sub main'push   { &out1("pushl",@_); $stack+=4; }
195 sub main'pop    { &out1("popl",@_); $stack-=4; }
196 sub main'pushf  { &out0("pushfl"); $stack+=4; }
197 sub main'popf   { &out0("popfl"); $stack-=4; }
198 sub main'not    { &out1("notl",@_); }
199 sub main'call   {       my $pre=$under;
200                         foreach $i (%label)
201                         { if ($label{$i} eq $_[0]) { $pre=''; last; } }
202                         &out1("call",$pre.$_[0]);
203                 }
204 sub main'call_ptr { &out1p("call",@_); }
205 sub main'ret    { &out0("ret"); }
206 sub main'nop    { &out0("nop"); }
207 sub main'test   { &out2("testl",@_); }
208 sub main'bt     { &out2("btl",@_); }
209 sub main'leave  { &out0("leave"); }
210 sub main'cpuid  { &out0(".byte\t0x0f,0xa2"); }
211 sub main'rdtsc  { &out0(".byte\t0x0f,0x31"); }
212 sub main'halt   { &out0("hlt"); }
213 sub main'movz   { &out2("movzbl",@_); }
214 sub main'neg    { &out1("negl",@_); }
215 sub main'cld    { &out0("cld"); }
216
217 # SSE2
218 sub main'emms   { &out0("emms"); }
219 sub main'movd   { &out2("movd",@_); }
220 sub main'movdqu { &out2("movdqu",@_); }
221 sub main'movdqa { &out2("movdqa",@_); }
222 sub main'movdq2q{ &out2("movdq2q",@_); }
223 sub main'movq2dq{ &out2("movq2dq",@_); }
224 sub main'paddq  { &out2("paddq",@_); }
225 sub main'pmuludq{ &out2("pmuludq",@_); }
226 sub main'psrlq  { &out2("psrlq",@_); }
227 sub main'psllq  { &out2("psllq",@_); }
228 sub main'pxor   { &out2("pxor",@_); }
229 sub main'por    { &out2("por",@_); }
230 sub main'pand   { &out2("pand",@_); }
231 sub main'movq   {
232         local($p1,$p2,$optimize)=@_;
233         if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
234                 # movq between mmx registers can sink Intel CPUs
235                 {       push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n");      }
236         else    {       &out2("movq",@_);                               }
237         }
238
239 # The bswapl instruction is new for the 486. Emulate if i386.
240 sub main'bswap
241         {
242         if ($main'i386)
243                 {
244                 &main'comment("bswapl @_");
245                 &main'exch(main'HB(@_),main'LB(@_));
246                 &main'rotr(@_,16);
247                 &main'exch(main'HB(@_),main'LB(@_));
248                 }
249         else
250                 {
251                 &out1("bswapl",@_);
252                 }
253         }
254
255 sub out2
256         {
257         local($name,$p1,$p2)=@_;
258         local($l,$ll,$t);
259         local(%special)=(       "roll",0xD1C0,"rorl",0xD1C8,
260                                 "rcll",0xD1D0,"rcrl",0xD1D8,
261                                 "shll",0xD1E0,"shrl",0xD1E8,
262                                 "sarl",0xD1F8);
263         
264         if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
265                 {
266                 $op=$special{$name}|$reg_val{$p1};
267                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
268                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
269                 push(@out,$tmp1);
270                 push(@out,$tmp2);
271
272                 $p2=&conv($p2);
273                 $p1=&conv($p1);
274                 &main'comment("$name $p2 $p1");
275                 return;
276                 }
277
278         push(@out,"\t$name\t");
279         $t=&conv($p2).",";
280         $l=length($t);
281         push(@out,$t);
282         $ll=4-($l+9)/8;
283         $tmp1=sprintf("\t" x $ll);
284         push(@out,$tmp1);
285         push(@out,&conv($p1)."\n");
286         }
287
288 sub out1
289         {
290         local($name,$p1)=@_;
291         local($l,$t);
292         local(%special)=("bswapl",0x0FC8);
293
294         if ((defined($special{$name})) && defined($regs{$p1}))
295                 {
296                 $op=$special{$name}|$reg_val{$p1};
297                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
298                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
299                 push(@out,$tmp1);
300                 push(@out,$tmp2);
301
302                 $p2=&conv($p2);
303                 $p1=&conv($p1);
304                 &main'comment("$name $p2 $p1");
305                 return;
306                 }
307
308         push(@out,"\t$name\t".&conv($p1)."\n");
309         }
310
311 sub out1p
312         {
313         local($name,$p1)=@_;
314         local($l,$t);
315
316         push(@out,"\t$name\t*".&conv($p1)."\n");
317         }
318
319 sub out0
320         {
321         push(@out,"\t$_[0]\n");
322         }
323
324 sub conv
325         {
326         local($p)=@_;
327
328 #       $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
329
330         $p=$regs{$p} if (defined($regs{$p}));
331
332         $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
333         $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
334         return $p;
335         }
336
337 sub main'file
338         {
339         local($file)=@_;
340
341         local($tmp)=<<"EOF";
342         .file   "$file.s"
343 EOF
344         push(@out,$tmp);
345         }
346
347 sub main'function_begin
348         {
349         local($func)=@_;
350
351         &main'external_label($func);
352         $func=$under.$func;
353
354         local($tmp)=<<"EOF";
355 .text
356 .globl  $func
357 EOF
358         push(@out,$tmp);
359         if ($main'cpp)
360                 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
361         elsif ($main'coff)
362                 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
363         elsif ($main'aout and !$main'pic)
364                 { }
365         else    { $tmp=push(@out,".type\t$func,\@function\n"); }
366         push(@out,".align\t$align\n");
367         push(@out,"$func:\n");
368         $tmp=<<"EOF";
369         pushl   %ebp
370         pushl   %ebx
371         pushl   %esi
372         pushl   %edi
373
374 EOF
375         push(@out,$tmp);
376         $stack=20;
377         }
378
379 sub main'function_begin_B
380         {
381         local($func,$extra)=@_;
382
383         &main'external_label($func);
384         $func=$under.$func;
385
386         local($tmp)=<<"EOF";
387 .text
388 .globl  $func
389 EOF
390         push(@out,$tmp);
391         if ($main'cpp)
392                 { push(@out,"TYPE($func,\@function)\n"); }
393         elsif ($main'coff)
394                 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
395         elsif ($main'aout and !$main'pic)
396                 { }
397         else    { push(@out,".type      $func,\@function\n"); }
398         push(@out,".align\t$align\n");
399         push(@out,"$func:\n");
400         $stack=4;
401         }
402
403 sub main'function_end
404         {
405         local($func)=@_;
406
407         $func=$under.$func;
408
409         local($tmp)=<<"EOF";
410         popl    %edi
411         popl    %esi
412         popl    %ebx
413         popl    %ebp
414         ret
415 ${dot}L_${func}_end:
416 EOF
417         push(@out,$tmp);
418
419         if ($main'cpp)
420                 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
421         elsif ($main'coff or $main'aout)
422                 { }
423         else    { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
424         push(@out,".ident       \"$func\"\n");
425         $stack=0;
426         %label=();
427         }
428
429 sub main'function_end_A
430         {
431         local($func)=@_;
432
433         local($tmp)=<<"EOF";
434         popl    %edi
435         popl    %esi
436         popl    %ebx
437         popl    %ebp
438         ret
439 EOF
440         push(@out,$tmp);
441         }
442
443 sub main'function_end_B
444         {
445         local($func)=@_;
446
447         $func=$under.$func;
448
449         push(@out,"${dot}L_${func}_end:\n");
450         if ($main'cpp)
451                 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
452         elsif ($main'coff or $main'aout)
453                 { }
454         else    { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
455         push(@out,".ident       \"$func\"\n");
456         $stack=0;
457         %label=();
458         }
459
460 sub main'wparam
461         {
462         local($num)=@_;
463
464         return(&main'DWP($stack+$num*4,"esp","",0));
465         }
466
467 sub main'stack_push
468         {
469         local($num)=@_;
470         $stack+=$num*4;
471         &main'sub("esp",$num*4);
472         }
473
474 sub main'stack_pop
475         {
476         local($num)=@_;
477         $stack-=$num*4;
478         &main'add("esp",$num*4);
479         }
480
481 sub main'swtmp
482         {
483         return(&main'DWP($_[0]*4,"esp","",0));
484         }
485
486 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
487 #sub main'wtmp
488 #       {
489 #       local($num)=@_;
490 #
491 #       return(&main'DWP(-($num+1)*4,"esp","",0));
492 #       }
493
494 sub main'comment
495         {
496         if (!defined($com_start) or $main'elf)
497                 {       # Regarding $main'elf above...
498                         # GNU and SVR4 as'es use different comment delimiters,
499                 push(@out,"\n");        # so we just skip ELF comments...
500                 return;
501                 }
502         foreach (@_)
503                 {
504                 if (/^\s*$/)
505                         { push(@out,"\n"); }
506                 else
507                         { push(@out,"\t$com_start $_ $com_end\n"); }
508                 }
509         }
510
511 sub main'public_label
512         {
513         $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
514         push(@out,".globl\t$label{$_[0]}\n");
515         }
516
517 sub main'label
518         {
519         if (!defined($label{$_[0]}))
520                 {
521                 $label{$_[0]}="${dot}${label}${_[0]}";
522                 $label++;
523                 }
524         return($label{$_[0]});
525         }
526
527 sub main'set_label
528         {
529         if (!defined($label{$_[0]}))
530                 {
531                 $label{$_[0]}="${dot}${label}${_[0]}";
532                 $label++;
533                 }
534         if ($_[1]!=0)
535                 {
536                 if ($_[1]>1)    { main'align($_[1]);            }
537                 else            { push(@out,".align $align\n"); }
538                 }
539         push(@out,"$label{$_[0]}:\n");
540         }
541
542 sub main'file_end
543         {
544         # try to detect if SSE2 or MMX extensions were used on ELF platform...
545         if ($main'elf && grep {/\b%[x]*mm[0-7]\b|OPENSSL_ia32cap_P\b/i} @out) {
546                 local($tmp);
547
548                 push (@out,"\n.section\t.bss\n");
549                 push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
550
551                 return;
552         }
553
554         if ($const ne "")
555                 {
556                 push(@out,".section .rodata\n");
557                 push(@out,$const);
558                 $const="";
559                 }
560         }
561
562 sub main'data_byte
563         {
564         push(@out,"\t.byte\t".join(',',@_)."\n");
565         }
566
567 sub main'data_word
568         {
569         push(@out,"\t.long\t".join(',',@_)."\n");
570         }
571
572 sub main'align
573         {
574         my $val=$_[0],$p2,$i;
575         if ($main'aout) {
576                 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
577                 $val=$p2-1;
578                 $val.=",0x90";
579         }
580         push(@out,".align\t$val\n");
581         }
582
583 # debug output functions: puts, putx, printf
584
585 sub main'puts
586         {
587         &pushvars();
588         &main'push('$Lstring' . ++$constl);
589         &main'call('puts');
590         $stack-=4;
591         &main'add("esp",4);
592         &popvars();
593
594         $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
595         }
596
597 sub main'putx
598         {
599         &pushvars();
600         &main'push($_[0]);
601         &main'push('$Lstring' . ++$constl);
602         &main'call('printf');
603         &main'add("esp",8);
604         $stack-=8;
605         &popvars();
606
607         $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
608         }
609
610 sub main'printf
611         {
612         $ostack = $stack;
613         &pushvars();
614         for ($i = @_ - 1; $i >= 0; $i--)
615                 {
616                 if ($i == 0) # change this to support %s format strings
617                         {
618                         &main'push('$Lstring' . ++$constl);
619                         $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
620                         }
621                 else
622                         {
623                         if ($_[$i] =~ /([0-9]*)\(%esp\)/)
624                                 {
625                                 &main'push(($1 + $stack - $ostack) . '(%esp)');
626                                 }
627                         else
628                                 {
629                                 &main'push($_[$i]);
630                                 }
631                         }
632                 }
633         &main'call('printf');
634         $stack-=4*@_;
635         &main'add("esp",4*@_);
636         &popvars();
637         }
638
639 sub pushvars
640         {
641         &main'pushf();
642         &main'push("edx");
643         &main'push("ecx");
644         &main'push("eax");
645         }
646
647 sub popvars
648         {
649         &main'pop("eax");
650         &main'pop("ecx");
651         &main'pop("edx");
652         &main'popf();
653         }
654
655 sub main'picmeup
656         {
657         local($dst,$sym)=@_;
658         if ($main'cpp)
659                 {
660                 local($tmp)=<<___;
661 #if (defined(ELF) || defined(SOL)) && defined(PIC)
662         call    1f
663 1:      popl    $regs{$dst}
664         addl    \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
665         movl    $sym\@GOT($regs{$dst}),$regs{$dst}
666 #else
667         leal    $sym,$regs{$dst}
668 #endif
669 ___
670                 push(@out,$tmp);
671                 }
672         elsif ($main'pic && ($main'elf || $main'aout))
673                 {
674                 &main'call(&main'label("PIC_me_up"));
675                 &main'set_label("PIC_me_up");
676                 &main'blindpop($dst);
677                 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
678                                 &main'label("PIC_me_up") . "]");
679                 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
680                 }
681         else
682                 {
683                 &main'lea($dst,&main'DWP($sym));
684                 }
685         }
686
687 sub main'blindpop { &out1("popl",@_); }
688
689 sub main'initseg
690         {
691         local($f)=@_;
692         local($tmp);
693         if ($main'elf)
694                 {
695                 $tmp=<<___;
696 .section        .init
697         call    $under$f
698         jmp     .Linitalign
699 .align  $align
700 .Linitalign:
701 ___
702                 }
703         elsif ($main'coff)
704                 {
705                 $tmp=<<___;     # applies to both Cygwin and Mingw
706 .section        .ctors
707 .long   $under$f
708 ___
709                 }
710         elsif ($main'aout)
711                 {
712                 local($ctor)="${under}_GLOBAL_\$I\$$f";
713                 $tmp=".text\n";
714                 $tmp.=".type    $ctor,\@function\n" if ($main'pic);
715                 $tmp.=<<___;    # OpenBSD way...
716 .globl  $ctor
717 .align  2
718 $ctor:
719         jmp     $under$f
720 ___
721                 }
722         push(@out,$tmp) if ($tmp);
723         }
724
725 1;