Add framework for yet another assembler module dubbed "cpuid." Idea
[oweals/openssl.git] / crypto / perlasm / x86unix.pl
1 #!/usr/local/bin/perl
2
3 package x86unix;
4
5 $label="L000";
6 $const="";
7 $constl=0;
8
9 $align=($main'aout)?"4":"16";
10 $under=($main'aout)?"_":"";
11 $com_start=($main'sol)?"/":"#";
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         .version        "01.01"
326 gcc2_compiled.:
327 EOF
328         push(@out,$tmp);
329         }
330
331 sub main'function_begin
332         {
333         local($func)=@_;
334
335         &main'external_label($func);
336         $func=$under.$func;
337
338         local($tmp)=<<"EOF";
339 .text
340         .align $align
341 .globl $func
342 EOF
343         push(@out,$tmp);
344         if ($main'cpp)
345                 { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
346         elsif ($main'gaswin)
347                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
348         else    { $tmp=push(@out,"\t.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,"\tTYPE($func,\@function)\n"); }
376         elsif ($main'gaswin)
377                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
378         else    { push(@out,"\t.type    $func,\@function\n"); }
379         push(@out,"$func:\n");
380         $stack=4;
381         }
382
383 sub main'function_end
384         {
385         local($func)=@_;
386
387         $func=$under.$func;
388
389         local($tmp)=<<"EOF";
390         popl    %edi
391         popl    %esi
392         popl    %ebx
393         popl    %ebp
394         ret
395 .L_${func}_end:
396 EOF
397         push(@out,$tmp);
398
399         if ($main'cpp)
400                 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
401         elsif ($main'gaswin)
402                 { $tmp=push(@out,"\t.align 4\n"); }
403         else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
404         push(@out,".ident       \"$func\"\n");
405         $stack=0;
406         %label=();
407         }
408
409 sub main'function_end_A
410         {
411         local($func)=@_;
412
413         local($tmp)=<<"EOF";
414         popl    %edi
415         popl    %esi
416         popl    %ebx
417         popl    %ebp
418         ret
419 EOF
420         push(@out,$tmp);
421         }
422
423 sub main'function_end_B
424         {
425         local($func)=@_;
426
427         $func=$under.$func;
428
429         push(@out,".L_${func}_end:\n");
430         if ($main'cpp)
431                 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
432         elsif ($main'gaswin)
433                 { push(@out,"\t.align 4\n"); }
434         else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
435         push(@out,".ident       \"$func\"\n");
436         $stack=0;
437         %label=();
438         }
439
440 sub main'wparam
441         {
442         local($num)=@_;
443
444         return(&main'DWP($stack+$num*4,"esp","",0));
445         }
446
447 sub main'stack_push
448         {
449         local($num)=@_;
450         $stack+=$num*4;
451         &main'sub("esp",$num*4);
452         }
453
454 sub main'stack_pop
455         {
456         local($num)=@_;
457         $stack-=$num*4;
458         &main'add("esp",$num*4);
459         }
460
461 sub main'swtmp
462         {
463         return(&main'DWP($_[0]*4,"esp","",0));
464         }
465
466 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
467 #sub main'wtmp
468 #       {
469 #       local($num)=@_;
470 #
471 #       return(&main'DWP(-($num+1)*4,"esp","",0));
472 #       }
473
474 sub main'comment
475         {
476         if ($main'elf)  # GNU and SVR4 as'es use different comment delimiters,
477                 {       # so we just skip comments...
478                 push(@out,"\n");
479                 return;
480                 }
481         foreach (@_)
482                 {
483                 if (/^\s*$/)
484                         { push(@out,"\n"); }
485                 else
486                         { push(@out,"\t$com_start $_ $com_end\n"); }
487                 }
488         }
489
490 sub main'label
491         {
492         if (!defined($label{$_[0]}))
493                 {
494                 $label{$_[0]}=".${label}${_[0]}";
495                 $label++;
496                 }
497         return($label{$_[0]});
498         }
499
500 sub main'set_label
501         {
502         if (!defined($label{$_[0]}))
503                 {
504                 $label{$_[0]}=".${label}${_[0]}";
505                 $label++;
506                 }
507         push(@out,".align $align\n") if ($_[1] != 0);
508         push(@out,"$label{$_[0]}:\n");
509         }
510
511 sub main'file_end
512         {
513         # try to detect if SSE2 or MMX extensions were used on ELF platform...
514         if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
515                 local($tmp);
516
517                 push (@out,"\n.comm\t".$under."OPENSSL_ia32cap,8,4\n");
518
519                 push (@out,".section\t.init\n");
520                 # One can argue that it's wasteful to craft every
521                 # SSE/MMX module with this snippet... Well, it's 72
522                 # bytes long and for the moment we have two modules.
523                 # Let's argue when we have 7 modules or so...
524                 #
525                 # $1<<10 sets a reserved bit to signal that variable
526                 # was initialized already...
527                 &main'picmeup("edx","OPENSSL_ia32cap");
528                 $tmp=<<___;
529                 cmpl    \$0,(%edx)
530                 jne     1f
531                 movl    \$1<<10,(%edx)
532                 pushf
533                 popl    %eax
534                 movl    %eax,%ecx
535                 xorl    \$1<<21,%eax
536                 pushl   %eax
537                 popf
538                 pushf
539                 popl    %eax
540                 xorl    %ecx,%eax
541                 bt      \$21,%eax
542                 jnc     1f
543                 pushl   %edi
544                 pushl   %ebx
545                 movl    %edx,%edi
546                 movl    \$1,%eax
547                 .word   0xa20f
548                 orl     \$1<<10,%edx
549                 movl    %edx,0(%edi)
550                 movl    %ecx,4(%edi)
551                 popl    %ebx
552                 popl    %edi
553         .align  4
554         1:
555 ___
556                 push (@out,$tmp);
557         }
558
559         if ($const ne "")
560                 {
561                 push(@out,".section .rodata\n");
562                 push(@out,$const);
563                 $const="";
564                 }
565         }
566
567 sub main'data_word
568         {
569         push(@out,"\t.long\t".join(',',@_)."\n");
570         }
571
572 sub main'align
573         {
574         push(@out,".align $_[0]\n");
575         }
576
577 # debug output functions: puts, putx, printf
578
579 sub main'puts
580         {
581         &pushvars();
582         &main'push('$Lstring' . ++$constl);
583         &main'call('puts');
584         $stack-=4;
585         &main'add("esp",4);
586         &popvars();
587
588         $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
589         }
590
591 sub main'putx
592         {
593         &pushvars();
594         &main'push($_[0]);
595         &main'push('$Lstring' . ++$constl);
596         &main'call('printf');
597         &main'add("esp",8);
598         $stack-=8;
599         &popvars();
600
601         $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
602         }
603
604 sub main'printf
605         {
606         $ostack = $stack;
607         &pushvars();
608         for ($i = @_ - 1; $i >= 0; $i--)
609                 {
610                 if ($i == 0) # change this to support %s format strings
611                         {
612                         &main'push('$Lstring' . ++$constl);
613                         $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
614                         }
615                 else
616                         {
617                         if ($_[$i] =~ /([0-9]*)\(%esp\)/)
618                                 {
619                                 &main'push(($1 + $stack - $ostack) . '(%esp)');
620                                 }
621                         else
622                                 {
623                                 &main'push($_[$i]);
624                                 }
625                         }
626                 }
627         &main'call('printf');
628         $stack-=4*@_;
629         &main'add("esp",4*@_);
630         &popvars();
631         }
632
633 sub pushvars
634         {
635         &main'pushf();
636         &main'push("edx");
637         &main'push("ecx");
638         &main'push("eax");
639         }
640
641 sub popvars
642         {
643         &main'pop("eax");
644         &main'pop("ecx");
645         &main'pop("edx");
646         &main'popf();
647         }
648
649 sub main'picmeup
650         {
651         local($dst,$sym)=@_;
652         if ($main'cpp)
653                 {
654                 local($tmp)=<<___;
655 #if (defined(ELF) || defined(SOL)) && defined(PIC)
656         .align  4
657         call    1f
658 1:      popl    $regs{$dst}
659         addl    \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
660         movl    $sym\@GOT($regs{$dst}),$regs{$dst}
661 #else
662         leal    $sym,$regs{$dst}
663 #endif
664 ___
665                 push(@out,$tmp);
666                 }
667         elsif ($main'pic && ($main'elf || $main'aout))
668                 {
669                 push(@out,"\t.align\t4\n");
670                 &main'call(&main'label("PIC_me_up"));
671                 &main'set_label("PIC_me_up");
672                 &main'blindpop($dst);
673                 &main'add($dst,"\$$under"."_GLOBAL_OFFSET_TABLE_+[.-".
674                                 &main'label("PIC_me_up") . "]");
675                 &main'mov($dst,&main'DWP($sym."\@GOT",$dst));
676                 }
677         else
678                 {
679                 &main'lea($dst,&main'DWP($sym));
680                 }
681         }
682
683 sub main'blindpop { &out1("popl",@_); }
684
685 sub main'initseg
686         {
687         local($f)=@_;
688         if ($main'elf)
689                 {
690                 local($tmp)=<<___;
691 .pushsection    .init
692         call    $under$f
693 .popsection
694 ___
695                 push(@out,$tmp);
696                 }
697         }