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