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