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