38c7a6df4260637f03452dd3297cecf2dd43f296
[openssl.git] / 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'div    { &out1("divl",@_); }
175 sub main'jmp    { &out1("jmp",@_); }
176 sub main'jmp_ptr { &out1p("jmp",@_); }
177 sub main'je     { &out1("je",@_); }
178 sub main'jle    { &out1("jle",@_); }
179 sub main'jne    { &out1("jne",@_); }
180 sub main'jnz    { &out1("jnz",@_); }
181 sub main'jz     { &out1("jz",@_); }
182 sub main'jge    { &out1("jge",@_); }
183 sub main'jl     { &out1("jl",@_); }
184 sub main'ja     { &out1("ja",@_); }
185 sub main'jae    { &out1("jae",@_); }
186 sub main'jb     { &out1("jb",@_); }
187 sub main'jbe    { &out1("jbe",@_); }
188 sub main'jc     { &out1("jc",@_); }
189 sub main'jnc    { &out1("jnc",@_); }
190 sub main'jno    { &out1("jno",@_); }
191 sub main'dec    { &out1("decl",@_); }
192 sub main'inc    { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
193 sub main'push   { &out1("pushl",@_); $stack+=4; }
194 sub main'pop    { &out1("popl",@_); $stack-=4; }
195 sub main'pushf  { &out0("pushfl"); $stack+=4; }
196 sub main'popf   { &out0("popfl"); $stack-=4; }
197 sub main'not    { &out1("notl",@_); }
198 sub main'call   {       my $pre=$under;
199                         foreach $i (%label)
200                         { if ($label{$i} eq $_[0]) { $pre=''; last; } }
201                         &out1("call",$pre.$_[0]);
202                 }
203 sub main'ret    { &out0("ret"); }
204 sub main'nop    { &out0("nop"); }
205 sub main'test   { &out2("testl",@_); }
206 sub main'bt     { &out2("btl",@_); }
207 sub main'leave  { &out0("leave"); }
208 sub main'cpuid  { &out0(".byte\t0x0f,0xa2"); }
209 sub main'rdtsc  { &out0(".byte\t0x0f,0x31"); }
210 sub main'halt   { &out0("hlt"); }
211 sub main'movz   { &out2("movzbl",@_); }
212 sub main'neg    { &out1("negl",@_); }
213 sub main'cld    { &out0("cld"); }
214
215 # SSE2
216 sub main'emms   { &out0("emms"); }
217 sub main'movd   { &out2("movd",@_); }
218 sub main'movdqu { &out2("movdqu",@_); }
219 sub main'movdqa { &out2("movdqa",@_); }
220 sub main'movdq2q{ &out2("movdq2q",@_); }
221 sub main'movq2dq{ &out2("movq2dq",@_); }
222 sub main'paddq  { &out2("paddq",@_); }
223 sub main'pmuludq{ &out2("pmuludq",@_); }
224 sub main'psrlq  { &out2("psrlq",@_); }
225 sub main'psllq  { &out2("psllq",@_); }
226 sub main'pxor   { &out2("pxor",@_); }
227 sub main'por    { &out2("por",@_); }
228 sub main'pand   { &out2("pand",@_); }
229 sub main'movq   {
230         local($p1,$p2)=@_;
231         if ($p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
232                 # movq between mmx registers sinks Intel CPUs
233                 {       push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n");      }
234         else    {       &out2("movq",@_);                               }
235         }
236
237 # The bswapl instruction is new for the 486. Emulate if i386.
238 sub main'bswap
239         {
240         if ($main'i386)
241                 {
242                 &main'comment("bswapl @_");
243                 &main'exch(main'HB(@_),main'LB(@_));
244                 &main'rotr(@_,16);
245                 &main'exch(main'HB(@_),main'LB(@_));
246                 }
247         else
248                 {
249                 &out1("bswapl",@_);
250                 }
251         }
252
253 sub out2
254         {
255         local($name,$p1,$p2)=@_;
256         local($l,$ll,$t);
257         local(%special)=(       "roll",0xD1C0,"rorl",0xD1C8,
258                                 "rcll",0xD1D0,"rcrl",0xD1D8,
259                                 "shll",0xD1E0,"shrl",0xD1E8,
260                                 "sarl",0xD1F8);
261         
262         if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
263                 {
264                 $op=$special{$name}|$reg_val{$p1};
265                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
266                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
267                 push(@out,$tmp1);
268                 push(@out,$tmp2);
269
270                 $p2=&conv($p2);
271                 $p1=&conv($p1);
272                 &main'comment("$name $p2 $p1");
273                 return;
274                 }
275
276         push(@out,"\t$name\t");
277         $t=&conv($p2).",";
278         $l=length($t);
279         push(@out,$t);
280         $ll=4-($l+9)/8;
281         $tmp1=sprintf("\t" x $ll);
282         push(@out,$tmp1);
283         push(@out,&conv($p1)."\n");
284         }
285
286 sub out1
287         {
288         local($name,$p1)=@_;
289         local($l,$t);
290         local(%special)=("bswapl",0x0FC8);
291
292         if ((defined($special{$name})) && defined($regs{$p1}))
293                 {
294                 $op=$special{$name}|$reg_val{$p1};
295                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
296                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
297                 push(@out,$tmp1);
298                 push(@out,$tmp2);
299
300                 $p2=&conv($p2);
301                 $p1=&conv($p1);
302                 &main'comment("$name $p2 $p1");
303                 return;
304                 }
305
306         push(@out,"\t$name\t".&conv($p1)."\n");
307         }
308
309 sub out1p
310         {
311         local($name,$p1)=@_;
312         local($l,$t);
313
314         push(@out,"\t$name\t*".&conv($p1)."\n");
315         }
316
317 sub out0
318         {
319         push(@out,"\t$_[0]\n");
320         }
321
322 sub conv
323         {
324         local($p)=@_;
325
326 #       $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
327
328         $p=$regs{$p} if (defined($regs{$p}));
329
330         $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
331         $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
332         return $p;
333         }
334
335 sub main'file
336         {
337         local($file)=@_;
338
339         local($tmp)=<<"EOF";
340         .file   "$file.s"
341 EOF
342         push(@out,$tmp);
343         }
344
345 sub main'function_begin
346         {
347         local($func)=@_;
348
349         &main'external_label($func);
350         $func=$under.$func;
351
352         local($tmp)=<<"EOF";
353 .text
354 .globl  $func
355 EOF
356         push(@out,$tmp);
357         if ($main'cpp)
358                 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
359         elsif ($main'coff)
360                 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
361         elsif ($main'aout and !$main'pic)
362                 { }
363         else    { $tmp=push(@out,".type\t$func,\@function\n"); }
364         push(@out,".align\t$align\n");
365         push(@out,"$func:\n");
366         $tmp=<<"EOF";
367         pushl   %ebp
368         pushl   %ebx
369         pushl   %esi
370         pushl   %edi
371
372 EOF
373         push(@out,$tmp);
374         $stack=20;
375         }
376
377 sub main'function_begin_B
378         {
379         local($func,$extra)=@_;
380
381         &main'external_label($func);
382         $func=$under.$func;
383
384         local($tmp)=<<"EOF";
385 .text
386 .globl  $func
387 EOF
388         push(@out,$tmp);
389         if ($main'cpp)
390                 { push(@out,"TYPE($func,\@function)\n"); }
391         elsif ($main'coff)
392                 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
393         elsif ($main'aout and !$main'pic)
394                 { }
395         else    { push(@out,".type      $func,\@function\n"); }
396         push(@out,".align\t$align\n");
397         push(@out,"$func:\n");
398         $stack=4;
399         }
400
401 sub main'function_end
402         {
403         local($func)=@_;
404
405         $func=$under.$func;
406
407         local($tmp)=<<"EOF";
408         popl    %edi
409         popl    %esi
410         popl    %ebx
411         popl    %ebp
412         ret
413 ${dot}L_${func}_end:
414 EOF
415         push(@out,$tmp);
416
417         if ($main'cpp)
418                 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
419         elsif ($main'coff or $main'aout)
420                 { }
421         else    { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
422         push(@out,".ident       \"$func\"\n");
423         $stack=0;
424         %label=();
425         }
426
427 sub main'function_end_A
428         {
429         local($func)=@_;
430
431         local($tmp)=<<"EOF";
432         popl    %edi
433         popl    %esi
434         popl    %ebx
435         popl    %ebp
436         ret
437 EOF
438         push(@out,$tmp);
439         }
440
441 sub main'function_end_B
442         {
443         local($func)=@_;
444
445         $func=$under.$func;
446
447         push(@out,"${dot}L_${func}_end:\n");
448         if ($main'cpp)
449                 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
450         elsif ($main'coff or $main'aout)
451                 { }
452         else    { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
453         push(@out,".ident       \"$func\"\n");
454         $stack=0;
455         %label=();
456         }
457
458 sub main'wparam
459         {
460         local($num)=@_;
461
462         return(&main'DWP($stack+$num*4,"esp","",0));
463         }
464
465 sub main'stack_push
466         {
467         local($num)=@_;
468         $stack+=$num*4;
469         &main'sub("esp",$num*4);
470         }
471
472 sub main'stack_pop
473         {
474         local($num)=@_;
475         $stack-=$num*4;
476         &main'add("esp",$num*4);
477         }
478
479 sub main'swtmp
480         {
481         return(&main'DWP($_[0]*4,"esp","",0));
482         }
483
484 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
485 #sub main'wtmp
486 #       {
487 #       local($num)=@_;
488 #
489 #       return(&main'DWP(-($num+1)*4,"esp","",0));
490 #       }
491
492 sub main'comment
493         {
494         if (!defined($com_start) or $main'elf)
495                 {       # Regarding $main'elf above...
496                         # GNU and SVR4 as'es use different comment delimiters,
497                 push(@out,"\n");        # so we just skip ELF comments...
498                 return;
499                 }
500         foreach (@_)
501                 {
502                 if (/^\s*$/)
503                         { push(@out,"\n"); }
504                 else
505                         { push(@out,"\t$com_start $_ $com_end\n"); }
506                 }
507         }
508
509 sub main'public_label
510         {
511         $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
512         push(@out,".globl\t$label{$_[0]}\n");
513         }
514
515 sub main'label
516         {
517         if (!defined($label{$_[0]}))
518                 {
519                 $label{$_[0]}="${dot}${label}${_[0]}";
520                 $label++;
521                 }
522         return($label{$_[0]});
523         }
524
525 sub main'set_label
526         {
527         if (!defined($label{$_[0]}))
528                 {
529                 $label{$_[0]}="${dot}${label}${_[0]}";
530                 $label++;
531                 }
532         if ($_[1]!=0)
533                 {
534                 if ($_[1]>1)    { main'align($_[1]);            }
535                 else            { push(@out,".align $align\n"); }
536                 }
537         push(@out,"$label{$_[0]}:\n");
538         }
539
540 sub main'file_end
541         {
542         # try to detect if SSE2 or MMX extensions were used on ELF platform...
543         if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
544                 local($tmp);
545
546                 push (@out,"\n.section\t.bss\n");
547                 push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
548
549                 push (@out,".section\t.init\n");
550                 # One can argue that it's wasteful to craft every
551                 # SSE/MMX module with this snippet... Well, it's 72
552                 # bytes long and for the moment we have two modules.
553                 # Let's argue when we have 7 modules or so...
554                 #
555                 # $1<<10 sets a reserved bit to signal that variable
556                 # was initialized already...
557                 &main'picmeup("edx","OPENSSL_ia32cap_P");
558                 $tmp=<<___;
559                 cmpl    \$0,(%edx)
560                 jne     1f
561                 movl    \$1<<10,(%edx)
562                 pushf
563                 popl    %eax
564                 movl    %eax,%ecx
565                 xorl    \$1<<21,%eax
566                 pushl   %eax
567                 popf
568                 pushf
569                 popl    %eax
570                 xorl    %ecx,%eax
571                 btl     \$21,%eax
572                 jnc     1f
573                 pushl   %edi
574                 pushl   %ebx
575                 movl    %edx,%edi
576                 movl    \$1,%eax
577                 .byte   0x0f,0xa2
578                 orl     \$1<<10,%edx
579                 movl    %edx,0(%edi)
580                 popl    %ebx
581                 popl    %edi
582                 jmp     1f
583         .align  $align
584         1:
585 ___
586                 push (@out,$tmp);
587         }
588
589         if ($const ne "")
590                 {
591                 push(@out,".section .rodata\n");
592                 push(@out,$const);
593                 $const="";
594                 }
595         }
596
597 sub main'data_word
598         {
599         push(@out,"\t.long\t".join(',',@_)."\n");
600         }
601
602 sub main'align
603         {
604         my $val=$_[0],$p2,$i;
605         if ($main'aout) {
606                 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
607                 $val=$p2-1;
608                 $val.=",0x90";
609         }
610         push(@out,".align\t$val\n");
611         }
612
613 # debug output functions: puts, putx, printf
614
615 sub main'puts
616         {
617         &pushvars();
618         &main'push('$Lstring' . ++$constl);
619         &main'call('puts');
620         $stack-=4;
621         &main'add("esp",4);
622         &popvars();
623
624         $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
625         }
626
627 sub main'putx
628         {
629         &pushvars();
630         &main'push($_[0]);
631         &main'push('$Lstring' . ++$constl);
632         &main'call('printf');
633         &main'add("esp",8);
634         $stack-=8;
635         &popvars();
636
637         $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
638         }
639
640 sub main'printf
641         {
642         $ostack = $stack;
643         &pushvars();
644         for ($i = @_ - 1; $i >= 0; $i--)
645                 {
646                 if ($i == 0) # change this to support %s format strings
647                         {
648                         &main'push('$Lstring' . ++$constl);
649                         $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
650                         }
651                 else
652                         {
653                         if ($_[$i] =~ /([0-9]*)\(%esp\)/)
654                                 {
655                                 &main'push(($1 + $stack - $ostack) . '(%esp)');
656                                 }
657                         else
658                                 {
659                                 &main'push($_[$i]);
660                                 }
661                         }
662                 }
663         &main'call('printf');
664         $stack-=4*@_;
665         &main'add("esp",4*@_);
666         &popvars();
667         }
668
669 sub pushvars
670         {
671         &main'pushf();
672         &main'push("edx");
673         &main'push("ecx");
674         &main'push("eax");
675         }
676
677 sub popvars
678         {
679         &main'pop("eax");
680         &main'pop("ecx");
681         &main'pop("edx");
682         &main'popf();
683         }
684
685 sub main'picmeup
686         {
687         local($dst,$sym)=@_;
688         if ($main'cpp)
689                 {
690                 local($tmp)=<<___;
691 #if (defined(ELF) || defined(SOL)) && defined(PIC)
692         call    1f
693 1:      popl    $regs{$dst}
694         addl    \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
695         movl    $sym\@GOT($regs{$dst}),$regs{$dst}
696 #else
697         leal    $sym,$regs{$dst}
698 #endif
699 ___
700                 push(@out,$tmp);
701                 }
702         elsif ($main'pic && ($main'elf || $main'aout))
703                 {
704                 &main'call(&main'label("PIC_me_up"));
705                 &main'set_label("PIC_me_up");
706                 &main'blindpop($dst);
707                 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
708                                 &main'label("PIC_me_up") . "]");
709                 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
710                 }
711         else
712                 {
713                 &main'lea($dst,&main'DWP($sym));
714                 }
715         }
716
717 sub main'blindpop { &out1("popl",@_); }
718
719 sub main'initseg
720         {
721         local($f)=@_;
722         local($tmp);
723         if ($main'elf)
724                 {
725                 $tmp=<<___;
726 .section        .init
727         call    $under$f
728         jmp     .Linitalign
729 .align  $align
730 .Linitalign:
731 ___
732                 }
733         elsif ($main'coff)
734                 {
735                 $tmp=<<___;     # applies to both Cygwin and Mingw
736 .section        .ctors
737 .long   $under$f
738 ___
739                 }
740         elsif ($main'aout)
741                 {
742                 local($ctor)="${under}_GLOBAL_\$I\$$f";
743                 $tmp=".text\n";
744                 $tmp.=".type    $ctor,\@function\n" if ($main'pic);
745                 $tmp.=<<___;    # OpenBSD way...
746 .globl  $ctor
747 .align  2
748 $ctor:
749         jmp     $under$f
750 ___
751                 }
752         push(@out,$tmp) if ($tmp);
753         }
754
755 1;