Import OpenSSL-0.9.8i.
[dragonfly.git] / crypto / openssl-0.9.7d / 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
56 %reg_val=(
57         'eax',  0x00,
58         'ebx',  0x03,
59         'ecx',  0x01,
60         'edx',  0x02,
61         'esi',  0x06,
62         'edi',  0x07,
63         'ebp',  0x05,
64         'esp',  0x04,
65         );
66
67 sub main'LB
68         {
69         (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
70         return($lb{$_[0]});
71         }
72
73 sub main'HB
74         {
75         (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
76         return($hb{$_[0]});
77         }
78
79 sub main'DWP
80         {
81         local($addr,$reg1,$reg2,$idx)=@_;
82
83         $ret="";
84         $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
85         $reg1="$regs{$reg1}" if defined($regs{$reg1});
86         $reg2="$regs{$reg2}" if defined($regs{$reg2});
87         $ret.=$addr if ($addr ne "") && ($addr ne 0);
88         if ($reg2 ne "")
89                 {
90                 if($idx ne "" && $idx != 0)
91                     { $ret.="($reg1,$reg2,$idx)"; }
92                 else
93                     { $ret.="($reg1,$reg2)"; }
94                 }
95         elsif ($reg1 ne "")
96                 { $ret.="($reg1)" }
97         return($ret);
98         }
99
100 sub main'BP
101         {
102         return(&main'DWP(@_));
103         }
104
105 sub main'BC
106         {
107         return @_;
108         }
109
110 sub main'DWC
111         {
112         return @_;
113         }
114
115 #sub main'BP
116 #       {
117 #       local($addr,$reg1,$reg2,$idx)=@_;
118 #
119 #       $ret="";
120 #
121 #       $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
122 #       $reg1="$regs{$reg1}" if defined($regs{$reg1});
123 #       $reg2="$regs{$reg2}" if defined($regs{$reg2});
124 #       $ret.=$addr if ($addr ne "") && ($addr ne 0);
125 #       if ($reg2 ne "")
126 #               { $ret.="($reg1,$reg2,$idx)"; }
127 #       else
128 #               { $ret.="($reg1)" }
129 #       return($ret);
130 #       }
131
132 sub main'mov    { &out2("movl",@_); }
133 sub main'movb   { &out2("movb",@_); }
134 sub main'and    { &out2("andl",@_); }
135 sub main'or     { &out2("orl",@_); }
136 sub main'shl    { &out2("sall",@_); }
137 sub main'shr    { &out2("shrl",@_); }
138 sub main'xor    { &out2("xorl",@_); }
139 sub main'xorb   { &out2("xorb",@_); }
140 sub main'add    { &out2("addl",@_); }
141 sub main'adc    { &out2("adcl",@_); }
142 sub main'sub    { &out2("subl",@_); }
143 sub main'rotl   { &out2("roll",@_); }
144 sub main'rotr   { &out2("rorl",@_); }
145 sub main'exch   { &out2("xchg",@_); }
146 sub main'cmp    { &out2("cmpl",@_); }
147 sub main'lea    { &out2("leal",@_); }
148 sub main'mul    { &out1("mull",@_); }
149 sub main'div    { &out1("divl",@_); }
150 sub main'jmp    { &out1("jmp",@_); }
151 sub main'jmp_ptr { &out1p("jmp",@_); }
152 sub main'je     { &out1("je",@_); }
153 sub main'jle    { &out1("jle",@_); }
154 sub main'jne    { &out1("jne",@_); }
155 sub main'jnz    { &out1("jnz",@_); }
156 sub main'jz     { &out1("jz",@_); }
157 sub main'jge    { &out1("jge",@_); }
158 sub main'jl     { &out1("jl",@_); }
159 sub main'ja     { &out1("ja",@_); }
160 sub main'jae    { &out1("jae",@_); }
161 sub main'jb     { &out1("jb",@_); }
162 sub main'jbe    { &out1("jbe",@_); }
163 sub main'jc     { &out1("jc",@_); }
164 sub main'jnc    { &out1("jnc",@_); }
165 sub main'jno    { &out1("jno",@_); }
166 sub main'dec    { &out1("decl",@_); }
167 sub main'inc    { &out1("incl",@_); }
168 sub main'push   { &out1("pushl",@_); $stack+=4; }
169 sub main'pop    { &out1("popl",@_); $stack-=4; }
170 sub main'pushf  { &out0("pushf"); $stack+=4; }
171 sub main'popf   { &out0("popf"); $stack-=4; }
172 sub main'not    { &out1("notl",@_); }
173 sub main'call   { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); }
174 sub main'ret    { &out0("ret"); }
175 sub main'nop    { &out0("nop"); }
176
177 # The bswapl instruction is new for the 486. Emulate if i386.
178 sub main'bswap
179         {
180         if ($main'i386)
181                 {
182                 &main'comment("bswapl @_");
183                 &main'exch(main'HB(@_),main'LB(@_));
184                 &main'rotr(@_,16);
185                 &main'exch(main'HB(@_),main'LB(@_));
186                 }
187         else
188                 {
189                 &out1("bswapl",@_);
190                 }
191         }
192
193 sub out2
194         {
195         local($name,$p1,$p2)=@_;
196         local($l,$ll,$t);
197         local(%special)=(       "roll",0xD1C0,"rorl",0xD1C8,
198                                 "rcll",0xD1D0,"rcrl",0xD1D8,
199                                 "shll",0xD1E0,"shrl",0xD1E8,
200                                 "sarl",0xD1F8);
201         
202         if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
203                 {
204                 $op=$special{$name}|$reg_val{$p1};
205                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
206                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
207                 push(@out,$tmp1);
208                 push(@out,$tmp2);
209
210                 $p2=&conv($p2);
211                 $p1=&conv($p1);
212                 &main'comment("$name $p2 $p1");
213                 return;
214                 }
215
216         push(@out,"\t$name\t");
217         $t=&conv($p2).",";
218         $l=length($t);
219         push(@out,$t);
220         $ll=4-($l+9)/8;
221         $tmp1=sprintf("\t" x $ll);
222         push(@out,$tmp1);
223         push(@out,&conv($p1)."\n");
224         }
225
226 sub out1
227         {
228         local($name,$p1)=@_;
229         local($l,$t);
230         local(%special)=("bswapl",0x0FC8);
231
232         if ((defined($special{$name})) && defined($regs{$p1}))
233                 {
234                 $op=$special{$name}|$reg_val{$p1};
235                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
236                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
237                 push(@out,$tmp1);
238                 push(@out,$tmp2);
239
240                 $p2=&conv($p2);
241                 $p1=&conv($p1);
242                 &main'comment("$name $p2 $p1");
243                 return;
244                 }
245
246         push(@out,"\t$name\t".&conv($p1)."\n");
247         }
248
249 sub out1p
250         {
251         local($name,$p1)=@_;
252         local($l,$t);
253
254         push(@out,"\t$name\t*".&conv($p1)."\n");
255         }
256
257 sub out0
258         {
259         push(@out,"\t$_[0]\n");
260         }
261
262 sub conv
263         {
264         local($p)=@_;
265
266 #       $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
267
268         $p=$regs{$p} if (defined($regs{$p}));
269
270         $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
271         $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
272         return $p;
273         }
274
275 sub main'file
276         {
277         local($file)=@_;
278
279         local($tmp)=<<"EOF";
280         .file   "$file.s"
281         .version        "01.01"
282 gcc2_compiled.:
283 EOF
284         push(@out,$tmp);
285         }
286
287 sub main'function_begin
288         {
289         local($func)=@_;
290
291         &main'external_label($func);
292         $func=$under.$func;
293
294         local($tmp)=<<"EOF";
295 .text
296         .align $align
297 .globl $func
298 EOF
299         push(@out,$tmp);
300         if ($main'cpp)
301                 { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
302         elsif ($main'gaswin)
303                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
304         else    { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
305         push(@out,"$func:\n");
306         $tmp=<<"EOF";
307         pushl   %ebp
308         pushl   %ebx
309         pushl   %esi
310         pushl   %edi
311
312 EOF
313         push(@out,$tmp);
314         $stack=20;
315         }
316
317 sub main'function_begin_B
318         {
319         local($func,$extra)=@_;
320
321         &main'external_label($func);
322         $func=$under.$func;
323
324         local($tmp)=<<"EOF";
325 .text
326         .align $align
327 .globl $func
328 EOF
329         push(@out,$tmp);
330         if ($main'cpp)
331                 { push(@out,"\tTYPE($func,\@function)\n"); }
332         elsif ($main'gaswin)
333                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
334         else    { push(@out,"\t.type    $func,\@function\n"); }
335         push(@out,"$func:\n");
336         $stack=4;
337         }
338
339 sub main'function_end
340         {
341         local($func)=@_;
342
343         $func=$under.$func;
344
345         local($tmp)=<<"EOF";
346         popl    %edi
347         popl    %esi
348         popl    %ebx
349         popl    %ebp
350         ret
351 .L_${func}_end:
352 EOF
353         push(@out,$tmp);
354
355         if ($main'cpp)
356                 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
357         elsif ($main'gaswin)
358                 { $tmp=push(@out,"\t.align 4\n"); }
359         else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
360         push(@out,".ident       \"$func\"\n");
361         $stack=0;
362         %label=();
363         }
364
365 sub main'function_end_A
366         {
367         local($func)=@_;
368
369         local($tmp)=<<"EOF";
370         popl    %edi
371         popl    %esi
372         popl    %ebx
373         popl    %ebp
374         ret
375 EOF
376         push(@out,$tmp);
377         }
378
379 sub main'function_end_B
380         {
381         local($func)=@_;
382
383         $func=$under.$func;
384
385         push(@out,".L_${func}_end:\n");
386         if ($main'cpp)
387                 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
388         elsif ($main'gaswin)
389                 { push(@out,"\t.align 4\n"); }
390         else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
391         push(@out,".ident       \"desasm.pl\"\n");
392         $stack=0;
393         %label=();
394         }
395
396 sub main'wparam
397         {
398         local($num)=@_;
399
400         return(&main'DWP($stack+$num*4,"esp","",0));
401         }
402
403 sub main'stack_push
404         {
405         local($num)=@_;
406         $stack+=$num*4;
407         &main'sub("esp",$num*4);
408         }
409
410 sub main'stack_pop
411         {
412         local($num)=@_;
413         $stack-=$num*4;
414         &main'add("esp",$num*4);
415         }
416
417 sub main'swtmp
418         {
419         return(&main'DWP($_[0]*4,"esp","",0));
420         }
421
422 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
423 #sub main'wtmp
424 #       {
425 #       local($num)=@_;
426 #
427 #       return(&main'DWP(-($num+1)*4,"esp","",0));
428 #       }
429
430 sub main'comment
431         {
432         if ($main'elf)  # GNU and SVR4 as'es use different comment delimiters,
433                 {       # so we just skip comments...
434                 push(@out,"\n");
435                 return;
436                 }
437         foreach (@_)
438                 {
439                 if (/^\s*$/)
440                         { push(@out,"\n"); }
441                 else
442                         { push(@out,"\t$com_start $_ $com_end\n"); }
443                 }
444         }
445
446 sub main'label
447         {
448         if (!defined($label{$_[0]}))
449                 {
450                 $label{$_[0]}=".${label}${_[0]}";
451                 $label++;
452                 }
453         return($label{$_[0]});
454         }
455
456 sub main'set_label
457         {
458         if (!defined($label{$_[0]}))
459                 {
460                 $label{$_[0]}=".${label}${_[0]}";
461                 $label++;
462                 }
463         push(@out,".align $align\n") if ($_[1] != 0);
464         push(@out,"$label{$_[0]}:\n");
465         }
466
467 sub main'file_end
468         {
469         if ($const ne "")
470                 {
471                 push(@out,".section .rodata\n");
472                 push(@out,$const);
473                 $const="";
474                 }
475         }
476
477 sub main'data_word
478         {
479         push(@out,"\t.long $_[0]\n");
480         }
481
482 # debug output functions: puts, putx, printf
483
484 sub main'puts
485         {
486         &pushvars();
487         &main'push('$Lstring' . ++$constl);
488         &main'call('puts');
489         $stack-=4;
490         &main'add("esp",4);
491         &popvars();
492
493         $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
494         }
495
496 sub main'putx
497         {
498         &pushvars();
499         &main'push($_[0]);
500         &main'push('$Lstring' . ++$constl);
501         &main'call('printf');
502         &main'add("esp",8);
503         $stack-=8;
504         &popvars();
505
506         $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
507         }
508
509 sub main'printf
510         {
511         $ostack = $stack;
512         &pushvars();
513         for ($i = @_ - 1; $i >= 0; $i--)
514                 {
515                 if ($i == 0) # change this to support %s format strings
516                         {
517                         &main'push('$Lstring' . ++$constl);
518                         $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
519                         }
520                 else
521                         {
522                         if ($_[$i] =~ /([0-9]*)\(%esp\)/)
523                                 {
524                                 &main'push(($1 + $stack - $ostack) . '(%esp)');
525                                 }
526                         else
527                                 {
528                                 &main'push($_[$i]);
529                                 }
530                         }
531                 }
532         &main'call('printf');
533         $stack-=4*@_;
534         &main'add("esp",4*@_);
535         &popvars();
536         }
537
538 sub pushvars
539         {
540         &main'pushf();
541         &main'push("edx");
542         &main'push("ecx");
543         &main'push("eax");
544         }
545
546 sub popvars
547         {
548         &main'pop("eax");
549         &main'pop("ecx");
550         &main'pop("edx");
551         &main'popf();
552         }
553
554 sub main'picmeup
555         {
556         local($dst,$sym)=@_;
557         if ($main'cpp)
558                 {
559                 local($tmp)=<<___;
560 #if (defined(ELF) || defined(SOL)) && defined(PIC)
561         .align  8
562         call    1f
563 1:      popl    $regs{$dst}
564         addl    \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
565         movl    $sym\@GOT($regs{$dst}),$regs{$dst}
566 #else
567         leal    $sym,$regs{$dst}
568 #endif
569 ___
570                 push(@out,$tmp);
571                 }
572         elsif ($main'pic && ($main'elf || $main'aout))
573                 {
574                 push(@out,"\t.align\t8\n");
575                 &main'call(&main'label("PIC_me_up"));
576                 &main'set_label("PIC_me_up");
577                 &main'blindpop($dst);
578                 &main'add($dst,"\$$under"."_GLOBAL_OFFSET_TABLE_+[.-".
579                                 &main'label("PIC_me_up") . "]");
580                 &main'mov($dst,&main'DWP($sym."\@GOT",$dst));
581                 }
582         else
583                 {
584                 &main'lea($dst,&main'DWP($sym));
585                 }
586         }
587
588 sub main'blindpop { &out1("popl",@_); }