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