Deprecate cpp and gaswin targets. New coff fills in for gaswin, but cpp is
[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 $com_start="#" if ($main'aout or $main'coff);
12
13 sub main'asm_init_output { @out=(); }
14 sub main'asm_get_output { return(@out); }
15 sub main'get_labels { return(@labels); }
16 sub main'external_label { push(@labels,@_); }
17
18 if ($main'cpp)
19         {
20         $align="ALIGN";
21         $under="";
22         $com_start='/*';
23         $com_end='*/';
24         }
25
26 %lb=(   'eax',  '%al',
27         'ebx',  '%bl',
28         'ecx',  '%cl',
29         'edx',  '%dl',
30         'ax',   '%al',
31         'bx',   '%bl',
32         'cx',   '%cl',
33         'dx',   '%dl',
34         );
35
36 %hb=(   'eax',  '%ah',
37         'ebx',  '%bh',
38         'ecx',  '%ch',
39         'edx',  '%dh',
40         'ax',   '%ah',
41         'bx',   '%bh',
42         'cx',   '%ch',
43         'dx',   '%dh',
44         );
45
46 %regs=( 'eax',  '%eax',
47         'ebx',  '%ebx',
48         'ecx',  '%ecx',
49         'edx',  '%edx',
50         'esi',  '%esi',
51         'edi',  '%edi',
52         'ebp',  '%ebp',
53         'esp',  '%esp',
54
55         'mm0',  '%mm0',
56         'mm1',  '%mm1',
57         'mm2',  '%mm2',
58         'mm3',  '%mm3',
59         'mm4',  '%mm4',
60         'mm5',  '%mm5',
61         'mm6',  '%mm6',
62         'mm7',  '%mm7',
63
64         'xmm0', '%xmm0',
65         'xmm1', '%xmm1',
66         'xmm2', '%xmm2',
67         'xmm3', '%xmm3',
68         'xmm4', '%xmm4',
69         'xmm5', '%xmm5',
70         'xmm6', '%xmm6',
71         'xmm7', '%xmm7',
72         );
73
74 %reg_val=(
75         'eax',  0x00,
76         'ebx',  0x03,
77         'ecx',  0x01,
78         'edx',  0x02,
79         'esi',  0x06,
80         'edi',  0x07,
81         'ebp',  0x05,
82         'esp',  0x04,
83         );
84
85 sub main'LB
86         {
87         (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
88         return($lb{$_[0]});
89         }
90
91 sub main'HB
92         {
93         (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
94         return($hb{$_[0]});
95         }
96
97 sub main'DWP
98         {
99         local($addr,$reg1,$reg2,$idx)=@_;
100
101         $ret="";
102         $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
103         $reg1="$regs{$reg1}" if defined($regs{$reg1});
104         $reg2="$regs{$reg2}" if defined($regs{$reg2});
105         $ret.=$addr if ($addr ne "") && ($addr ne 0);
106         if ($reg2 ne "")
107                 {
108                 if($idx ne "" && $idx != 0)
109                     { $ret.="($reg1,$reg2,$idx)"; }
110                 else
111                     { $ret.="($reg1,$reg2)"; }
112                 }
113         elsif ($reg1 ne "")
114                 { $ret.="($reg1)" }
115         return($ret);
116         }
117
118 sub main'QWP
119         {
120         return(&main'DWP(@_));
121         }
122
123 sub main'BP
124         {
125         return(&main'DWP(@_));
126         }
127
128 sub main'BC
129         {
130         return @_;
131         }
132
133 sub main'DWC
134         {
135         return @_;
136         }
137
138 #sub main'BP
139 #       {
140 #       local($addr,$reg1,$reg2,$idx)=@_;
141 #
142 #       $ret="";
143 #
144 #       $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
145 #       $reg1="$regs{$reg1}" if defined($regs{$reg1});
146 #       $reg2="$regs{$reg2}" if defined($regs{$reg2});
147 #       $ret.=$addr if ($addr ne "") && ($addr ne 0);
148 #       if ($reg2 ne "")
149 #               { $ret.="($reg1,$reg2,$idx)"; }
150 #       else
151 #               { $ret.="($reg1)" }
152 #       return($ret);
153 #       }
154
155 sub main'mov    { &out2("movl",@_); }
156 sub main'movb   { &out2("movb",@_); }
157 sub main'and    { &out2("andl",@_); }
158 sub main'or     { &out2("orl",@_); }
159 sub main'shl    { &out2("sall",@_); }
160 sub main'shr    { &out2("shrl",@_); }
161 sub main'xor    { &out2("xorl",@_); }
162 sub main'xorb   { &out2("xorb",@_); }
163 sub main'add    { &out2("addl",@_); }
164 sub main'adc    { &out2("adcl",@_); }
165 sub main'sub    { &out2("subl",@_); }
166 sub main'rotl   { &out2("roll",@_); }
167 sub main'rotr   { &out2("rorl",@_); }
168 sub main'exch   { &out2("xchg",@_); }
169 sub main'cmp    { &out2("cmpl",@_); }
170 sub main'lea    { &out2("leal",@_); }
171 sub main'mul    { &out1("mull",@_); }
172 sub main'div    { &out1("divl",@_); }
173 sub main'jmp    { &out1("jmp",@_); }
174 sub main'jmp_ptr { &out1p("jmp",@_); }
175 sub main'je     { &out1("je",@_); }
176 sub main'jle    { &out1("jle",@_); }
177 sub main'jne    { &out1("jne",@_); }
178 sub main'jnz    { &out1("jnz",@_); }
179 sub main'jz     { &out1("jz",@_); }
180 sub main'jge    { &out1("jge",@_); }
181 sub main'jl     { &out1("jl",@_); }
182 sub main'ja     { &out1("ja",@_); }
183 sub main'jae    { &out1("jae",@_); }
184 sub main'jb     { &out1("jb",@_); }
185 sub main'jbe    { &out1("jbe",@_); }
186 sub main'jc     { &out1("jc",@_); }
187 sub main'jnc    { &out1("jnc",@_); }
188 sub main'jno    { &out1("jno",@_); }
189 sub main'dec    { &out1("decl",@_); }
190 sub main'inc    { &out1("incl",@_); }
191 sub main'push   { &out1("pushl",@_); $stack+=4; }
192 sub main'pop    { &out1("popl",@_); $stack-=4; }
193 sub main'pushf  { &out0("pushf"); $stack+=4; }
194 sub main'popf   { &out0("popf"); $stack-=4; }
195 sub main'not    { &out1("notl",@_); }
196 sub main'call   { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); }
197 sub main'ret    { &out0("ret"); }
198 sub main'nop    { &out0("nop"); }
199 sub main'test   { &out2("testl",@_); }
200 sub main'bt     { &out2("btl",@_); }
201 sub main'leave  { &out0("leave"); }
202 sub main'cpuid  { &out0(".word\t0xa20f"); }
203 sub main'rdtsc  { &out0(".word\t0x310f"); }
204
205 # SSE2
206 sub main'emms   { &out0("emms"); }
207 sub main'movd   { &out2("movd",@_); }
208 sub main'movq   { &out2("movq",@_); }
209 sub main'movdqu { &out2("movdqu",@_); }
210 sub main'movdqa { &out2("movdqa",@_); }
211 sub main'movdq2q{ &out2("movdq2q",@_); }
212 sub main'movq2dq{ &out2("movq2dq",@_); }
213 sub main'paddq  { &out2("paddq",@_); }
214 sub main'pmuludq{ &out2("pmuludq",@_); }
215 sub main'psrlq  { &out2("psrlq",@_); }
216 sub main'psllq  { &out2("psllq",@_); }
217 sub main'pxor   { &out2("pxor",@_); }
218 sub main'por    { &out2("por",@_); }
219 sub main'pand   { &out2("pand",@_); }
220
221 # The bswapl instruction is new for the 486. Emulate if i386.
222 sub main'bswap
223         {
224         if ($main'i386)
225                 {
226                 &main'comment("bswapl @_");
227                 &main'exch(main'HB(@_),main'LB(@_));
228                 &main'rotr(@_,16);
229                 &main'exch(main'HB(@_),main'LB(@_));
230                 }
231         else
232                 {
233                 &out1("bswapl",@_);
234                 }
235         }
236
237 sub out2
238         {
239         local($name,$p1,$p2)=@_;
240         local($l,$ll,$t);
241         local(%special)=(       "roll",0xD1C0,"rorl",0xD1C8,
242                                 "rcll",0xD1D0,"rcrl",0xD1D8,
243                                 "shll",0xD1E0,"shrl",0xD1E8,
244                                 "sarl",0xD1F8);
245         
246         if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
247                 {
248                 $op=$special{$name}|$reg_val{$p1};
249                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
250                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
251                 push(@out,$tmp1);
252                 push(@out,$tmp2);
253
254                 $p2=&conv($p2);
255                 $p1=&conv($p1);
256                 &main'comment("$name $p2 $p1");
257                 return;
258                 }
259
260         push(@out,"\t$name\t");
261         $t=&conv($p2).",";
262         $l=length($t);
263         push(@out,$t);
264         $ll=4-($l+9)/8;
265         $tmp1=sprintf("\t" x $ll);
266         push(@out,$tmp1);
267         push(@out,&conv($p1)."\n");
268         }
269
270 sub out1
271         {
272         local($name,$p1)=@_;
273         local($l,$t);
274         local(%special)=("bswapl",0x0FC8);
275
276         if ((defined($special{$name})) && defined($regs{$p1}))
277                 {
278                 $op=$special{$name}|$reg_val{$p1};
279                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
280                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
281                 push(@out,$tmp1);
282                 push(@out,$tmp2);
283
284                 $p2=&conv($p2);
285                 $p1=&conv($p1);
286                 &main'comment("$name $p2 $p1");
287                 return;
288                 }
289
290         push(@out,"\t$name\t".&conv($p1)."\n");
291         }
292
293 sub out1p
294         {
295         local($name,$p1)=@_;
296         local($l,$t);
297
298         push(@out,"\t$name\t*".&conv($p1)."\n");
299         }
300
301 sub out0
302         {
303         push(@out,"\t$_[0]\n");
304         }
305
306 sub conv
307         {
308         local($p)=@_;
309
310 #       $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
311
312         $p=$regs{$p} if (defined($regs{$p}));
313
314         $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
315         $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
316         return $p;
317         }
318
319 sub main'file
320         {
321         local($file)=@_;
322
323         local($tmp)=<<"EOF";
324         .file   "$file.s"
325 EOF
326         push(@out,$tmp);
327         }
328
329 sub main'function_begin
330         {
331         local($func)=@_;
332
333         &main'external_label($func);
334         $func=$under.$func;
335
336         local($tmp)=<<"EOF";
337 .text
338 .align $align
339 .globl $func
340 EOF
341         push(@out,$tmp);
342         if ($main'cpp)
343                 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
344         elsif ($main'coff)
345                 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
346         elsif ($main'aout)
347                 { }
348         else    { $tmp=push(@out,".type\t$func,\@function\n"); }
349         push(@out,"$func:\n");
350         $tmp=<<"EOF";
351         pushl   %ebp
352         pushl   %ebx
353         pushl   %esi
354         pushl   %edi
355
356 EOF
357         push(@out,$tmp);
358         $stack=20;
359         }
360
361 sub main'function_begin_B
362         {
363         local($func,$extra)=@_;
364
365         &main'external_label($func);
366         $func=$under.$func;
367
368         local($tmp)=<<"EOF";
369 .text
370 .align $align
371 .globl $func
372 EOF
373         push(@out,$tmp);
374         if ($main'cpp)
375                 { push(@out,"TYPE($func,\@function)\n"); }
376         elsif ($main'coff)
377                 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
378         elsif ($main'aout)
379                 { }
380         else    { push(@out,".type      $func,\@function\n"); }
381         push(@out,"$func:\n");
382         $stack=4;
383         }
384
385 sub main'function_end
386         {
387         local($func)=@_;
388
389         $func=$under.$func;
390
391         local($tmp)=<<"EOF";
392         popl    %edi
393         popl    %esi
394         popl    %ebx
395         popl    %ebp
396         ret
397 .L_${func}_end:
398 EOF
399         push(@out,$tmp);
400
401         if ($main'cpp)
402                 { push(@out,"SIZE($func,.L_${func}_end-$func)\n"); }
403         elsif ($main'coff or $main'aout)
404                 { $tmp=push(@out,".align $align\n"); }
405         else    { push(@out,".size\t$func,.L_${func}_end-$func\n"); }
406         push(@out,".ident       \"$func\"\n");
407         $stack=0;
408         %label=();
409         }
410
411 sub main'function_end_A
412         {
413         local($func)=@_;
414
415         local($tmp)=<<"EOF";
416         popl    %edi
417         popl    %esi
418         popl    %ebx
419         popl    %ebp
420         ret
421 EOF
422         push(@out,$tmp);
423         }
424
425 sub main'function_end_B
426         {
427         local($func)=@_;
428
429         $func=$under.$func;
430
431         push(@out,".L_${func}_end:\n");
432         if ($main'cpp)
433                 { push(@out,"SIZE($func,.L_${func}_end-$func)\n"); }
434         elsif ($main'coff or $main'aout)
435                 { push(@out,".align $align\n"); }
436         else    { push(@out,".size\t$func,.L_${func}_end-$func\n"); }
437         push(@out,".ident       \"$func\"\n");
438         $stack=0;
439         %label=();
440         }
441
442 sub main'wparam
443         {
444         local($num)=@_;
445
446         return(&main'DWP($stack+$num*4,"esp","",0));
447         }
448
449 sub main'stack_push
450         {
451         local($num)=@_;
452         $stack+=$num*4;
453         &main'sub("esp",$num*4);
454         }
455
456 sub main'stack_pop
457         {
458         local($num)=@_;
459         $stack-=$num*4;
460         &main'add("esp",$num*4);
461         }
462
463 sub main'swtmp
464         {
465         return(&main'DWP($_[0]*4,"esp","",0));
466         }
467
468 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
469 #sub main'wtmp
470 #       {
471 #       local($num)=@_;
472 #
473 #       return(&main'DWP(-($num+1)*4,"esp","",0));
474 #       }
475
476 sub main'comment
477         {
478         return if (!defined($com_start);
479         if ($main'elf)  # GNU and SVR4 as'es use different comment delimiters,
480                 {       # so we just skip comments...
481                 push(@out,"\n");
482                 return;
483                 }
484         foreach (@_)
485                 {
486                 if (/^\s*$/)
487                         { push(@out,"\n"); }
488                 else
489                         { push(@out,"\t$com_start $_ $com_end\n"); }
490                 }
491         }
492
493 sub main'label
494         {
495         if (!defined($label{$_[0]}))
496                 {
497                 $label{$_[0]}=".${label}${_[0]}";
498                 $label++;
499                 }
500         return($label{$_[0]});
501         }
502
503 sub main'set_label
504         {
505         if (!defined($label{$_[0]}))
506                 {
507                 $label{$_[0]}=".${label}${_[0]}";
508                 $label++;
509                 }
510         push(@out,".align $align\n") if ($_[1] != 0);
511         push(@out,"$label{$_[0]}:\n");
512         }
513
514 sub main'file_end
515         {
516         # try to detect if SSE2 or MMX extensions were used on ELF platform...
517         if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
518                 local($tmp);
519
520                 push (@out,"\n.comm\t".$under."OPENSSL_ia32cap,8,4\n");
521
522                 push (@out,".section\t.init\n");
523                 # One can argue that it's wasteful to craft every
524                 # SSE/MMX module with this snippet... Well, it's 72
525                 # bytes long and for the moment we have two modules.
526                 # Let's argue when we have 7 modules or so...
527                 #
528                 # $1<<10 sets a reserved bit to signal that variable
529                 # was initialized already...
530                 &main'picmeup("edx","OPENSSL_ia32cap");
531                 $tmp=<<___;
532                 cmpl    \$0,(%edx)
533                 jne     1f
534                 movl    \$1<<10,(%edx)
535                 pushf
536                 popl    %eax
537                 movl    %eax,%ecx
538                 xorl    \$1<<21,%eax
539                 pushl   %eax
540                 popf
541                 pushf
542                 popl    %eax
543                 xorl    %ecx,%eax
544                 bt      \$21,%eax
545                 jnc     1f
546                 pushl   %edi
547                 pushl   %ebx
548                 movl    %edx,%edi
549                 movl    \$1,%eax
550                 .word   0xa20f
551                 orl     \$1<<10,%edx
552                 movl    %edx,0(%edi)
553                 movl    %ecx,4(%edi)
554                 popl    %ebx
555                 popl    %edi
556         .align  4
557         1:
558 ___
559                 push (@out,$tmp);
560         }
561
562         if ($const ne "")
563                 {
564                 push(@out,".section .rodata\n");
565                 push(@out,$const);
566                 $const="";
567                 }
568         }
569
570 sub main'data_word
571         {
572         push(@out,"\t.long\t".join(',',@_)."\n");
573         }
574
575 sub main'align
576         {
577         my $val=$_[0],$p2,$i;
578         if ($main'aout) {
579                 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
580                 $val=$p2-1;
581                 $val.=",0x90";
582         }
583         push(@out,".align $val\n");
584         }
585
586 # debug output functions: puts, putx, printf
587
588 sub main'puts
589         {
590         &pushvars();
591         &main'push('$Lstring' . ++$constl);
592         &main'call('puts');
593         $stack-=4;
594         &main'add("esp",4);
595         &popvars();
596
597         $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
598         }
599
600 sub main'putx
601         {
602         &pushvars();
603         &main'push($_[0]);
604         &main'push('$Lstring' . ++$constl);
605         &main'call('printf');
606         &main'add("esp",8);
607         $stack-=8;
608         &popvars();
609
610         $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
611         }
612
613 sub main'printf
614         {
615         $ostack = $stack;
616         &pushvars();
617         for ($i = @_ - 1; $i >= 0; $i--)
618                 {
619                 if ($i == 0) # change this to support %s format strings
620                         {
621                         &main'push('$Lstring' . ++$constl);
622                         $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
623                         }
624                 else
625                         {
626                         if ($_[$i] =~ /([0-9]*)\(%esp\)/)
627                                 {
628                                 &main'push(($1 + $stack - $ostack) . '(%esp)');
629                                 }
630                         else
631                                 {
632                                 &main'push($_[$i]);
633                                 }
634                         }
635                 }
636         &main'call('printf');
637         $stack-=4*@_;
638         &main'add("esp",4*@_);
639         &popvars();
640         }
641
642 sub pushvars
643         {
644         &main'pushf();
645         &main'push("edx");
646         &main'push("ecx");
647         &main'push("eax");
648         }
649
650 sub popvars
651         {
652         &main'pop("eax");
653         &main'pop("ecx");
654         &main'pop("edx");
655         &main'popf();
656         }
657
658 sub main'picmeup
659         {
660         local($dst,$sym)=@_;
661         if ($main'cpp)
662                 {
663                 local($tmp)=<<___;
664 #if (defined(ELF) || defined(SOL)) && defined(PIC)
665         .align  4
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($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         }