f89d473b7e2fd891db17e734df0a59a8355e7e83
[openssl.git] / crypto / perlasm / x86unix.pl
1 #!/usr/local/bin/perl
2
3 package x86unix;
4
5 $label="L000";
6 $const="";
7
8 $align=($main'aout)?"4":"16";
9 $under=($main'aout)?"_":"";
10 $com_start=($main'sol)?"/":"#";
11
12 sub main'asm_init_output { @out=(); }
13 sub main'asm_get_output { return(@out); }
14 sub main'get_labels { return(@labels); }
15 sub main'external_label { push(@labels,@_); }
16
17 if ($main'cpp)
18         {
19         $align="ALIGN";
20         $under="";
21         $com_start='/*';
22         $com_end='*/';
23         }
24
25 %lb=(   'eax',  '%al',
26         'ebx',  '%bl',
27         'ecx',  '%cl',
28         'edx',  '%dl',
29         'ax',   '%al',
30         'bx',   '%bl',
31         'cx',   '%cl',
32         'dx',   '%dl',
33         );
34
35 %hb=(   'eax',  '%ah',
36         'ebx',  '%bh',
37         'ecx',  '%ch',
38         'edx',  '%dh',
39         'ax',   '%ah',
40         'bx',   '%bh',
41         'cx',   '%ch',
42         'dx',   '%dh',
43         );
44
45 %regs=( 'eax',  '%eax',
46         'ebx',  '%ebx',
47         'ecx',  '%ecx',
48         'edx',  '%edx',
49         'esi',  '%esi',
50         'edi',  '%edi',
51         'ebp',  '%ebp',
52         'esp',  '%esp',
53         );
54
55 %reg_val=(
56         'eax',  0x00,
57         'ebx',  0x03,
58         'ecx',  0x01,
59         'edx',  0x02,
60         'esi',  0x06,
61         'edi',  0x07,
62         'ebp',  0x05,
63         'esp',  0x04,
64         );
65
66 sub main'LB
67         {
68         (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
69         return($lb{$_[0]});
70         }
71
72 sub main'HB
73         {
74         (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
75         return($hb{$_[0]});
76         }
77
78 sub main'DWP
79         {
80         local($addr,$reg1,$reg2,$idx)=@_;
81
82         $ret="";
83         $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
84         $reg1="$regs{$reg1}" if defined($regs{$reg1});
85         $reg2="$regs{$reg2}" if defined($regs{$reg2});
86         $ret.=$addr if ($addr ne "") && ($addr ne 0);
87         if ($reg2 ne "")
88                 {
89                 if($idx ne "")
90                     { $ret.="($reg1,$reg2,$idx)"; }
91                 else
92                     { $ret.="($reg1,$reg2)"; }
93                 }
94         else
95                 { $ret.="($reg1)" }
96         return($ret);
97         }
98
99 sub main'BP
100         {
101         return(&main'DWP(@_));
102         }
103
104 sub main'BC
105         {
106         return @_;
107         }
108
109 sub main'DWC
110         {
111         return @_;
112         }
113
114 #sub main'BP
115 #       {
116 #       local($addr,$reg1,$reg2,$idx)=@_;
117 #
118 #       $ret="";
119 #
120 #       $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
121 #       $reg1="$regs{$reg1}" if defined($regs{$reg1});
122 #       $reg2="$regs{$reg2}" if defined($regs{$reg2});
123 #       $ret.=$addr if ($addr ne "") && ($addr ne 0);
124 #       if ($reg2 ne "")
125 #               { $ret.="($reg1,$reg2,$idx)"; }
126 #       else
127 #               { $ret.="($reg1)" }
128 #       return($ret);
129 #       }
130
131 sub main'mov    { &out2("movl",@_); }
132 sub main'movb   { &out2("movb",@_); }
133 sub main'and    { &out2("andl",@_); }
134 sub main'or     { &out2("orl",@_); }
135 sub main'shl    { &out2("sall",@_); }
136 sub main'shr    { &out2("shrl",@_); }
137 sub main'xor    { &out2("xorl",@_); }
138 sub main'xorb   { &out2("xorb",@_); }
139 sub main'add    { &out2("addl",@_); }
140 sub main'adc    { &out2("adcl",@_); }
141 sub main'sub    { &out2("subl",@_); }
142 sub main'rotl   { &out2("roll",@_); }
143 sub main'rotr   { &out2("rorl",@_); }
144 sub main'exch   { &out2("xchg",@_); }
145 sub main'cmp    { &out2("cmpl",@_); }
146 sub main'lea    { &out2("leal",@_); }
147 sub main'mul    { &out1("mull",@_); }
148 sub main'div    { &out1("divl",@_); }
149 sub main'jmp    { &out1("jmp",@_); }
150 sub main'jmp_ptr { &out1p("jmp",@_); }
151 sub main'je     { &out1("je",@_); }
152 sub main'jle    { &out1("jle",@_); }
153 sub main'jne    { &out1("jne",@_); }
154 sub main'jnz    { &out1("jnz",@_); }
155 sub main'jz     { &out1("jz",@_); }
156 sub main'jge    { &out1("jge",@_); }
157 sub main'jl     { &out1("jl",@_); }
158 sub main'jb     { &out1("jb",@_); }
159 sub main'jc     { &out1("jc",@_); }
160 sub main'jnc    { &out1("jnc",@_); }
161 sub main'jno    { &out1("jno",@_); }
162 sub main'dec    { &out1("decl",@_); }
163 sub main'inc    { &out1("incl",@_); }
164 sub main'push   { &out1("pushl",@_); $stack+=4; }
165 sub main'pop    { &out1("popl",@_); $stack-=4; }
166 sub main'not    { &out1("notl",@_); }
167 sub main'call   { &out1("call",$under.$_[0]); }
168 sub main'ret    { &out0("ret"); }
169 sub main'nop    { &out0("nop"); }
170
171 # The bswapl instruction is new for the 486. Emulate if i386.
172 sub main'bswap
173         {
174         if ($main'i386)
175                 {
176                 &main'comment("bswapl @_");
177                 &main'exch(main'HB(@_),main'LB(@_));
178                 &main'rotr(@_,16);
179                 &main'exch(main'HB(@_),main'LB(@_));
180                 }
181         else
182                 {
183                 &out1("bswapl",@_);
184                 }
185         }
186
187 sub out2
188         {
189         local($name,$p1,$p2)=@_;
190         local($l,$ll,$t);
191         local(%special)=(       "roll",0xD1C0,"rorl",0xD1C8,
192                                 "rcll",0xD1D0,"rcrl",0xD1D8,
193                                 "shll",0xD1E0,"shrl",0xD1E8,
194                                 "sarl",0xD1F8);
195         
196         if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
197                 {
198                 $op=$special{$name}|$reg_val{$p1};
199                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
200                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
201                 push(@out,$tmp1);
202                 push(@out,$tmp2);
203
204                 $p2=&conv($p2);
205                 $p1=&conv($p1);
206                 &main'comment("$name $p2 $p1");
207                 return;
208                 }
209
210         push(@out,"\t$name\t");
211         $t=&conv($p2).",";
212         $l=length($t);
213         push(@out,$t);
214         $ll=4-($l+9)/8;
215         $tmp1=sprintf("\t" x $ll);
216         push(@out,$tmp1);
217         push(@out,&conv($p1)."\n");
218         }
219
220 sub out1
221         {
222         local($name,$p1)=@_;
223         local($l,$t);
224         local(%special)=("bswapl",0x0FC8);
225
226         if ((defined($special{$name})) && defined($regs{$p1}))
227                 {
228                 $op=$special{$name}|$reg_val{$p1};
229                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
230                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
231                 push(@out,$tmp1);
232                 push(@out,$tmp2);
233
234                 $p2=&conv($p2);
235                 $p1=&conv($p1);
236                 &main'comment("$name $p2 $p1");
237                 return;
238                 }
239
240         push(@out,"\t$name\t".&conv($p1)."\n");
241         }
242
243 sub out1p
244         {
245         local($name,$p1)=@_;
246         local($l,$t);
247
248         push(@out,"\t$name\t*".&conv($p1)."\n");
249         }
250
251 sub out0
252         {
253         push(@out,"\t$_[0]\n");
254         }
255
256 sub conv
257         {
258         local($p)=@_;
259
260 #       $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
261
262         $p=$regs{$p} if (defined($regs{$p}));
263
264         $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
265         $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
266         return $p;
267         }
268
269 sub main'file
270         {
271         local($file)=@_;
272
273         local($tmp)=<<"EOF";
274         .file   "$file.s"
275         .version        "01.01"
276 gcc2_compiled.:
277 EOF
278         push(@out,$tmp);
279         }
280
281 sub main'function_begin
282         {
283         local($func)=@_;
284
285         &main'external_label($func);
286         $func=$under.$func;
287
288         local($tmp)=<<"EOF";
289 .text
290         .align $align
291 .globl $func
292 EOF
293         push(@out,$tmp);
294         if ($main'cpp)
295                 { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
296         elsif ($main'gaswin)
297                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
298         else    { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
299         push(@out,"$func:\n");
300         $tmp=<<"EOF";
301         pushl   %ebp
302         pushl   %ebx
303         pushl   %esi
304         pushl   %edi
305
306 EOF
307         push(@out,$tmp);
308         $stack=20;
309         }
310
311 sub main'function_begin_B
312         {
313         local($func,$extra)=@_;
314
315         &main'external_label($func);
316         $func=$under.$func;
317
318         local($tmp)=<<"EOF";
319 .text
320         .align $align
321 .globl $func
322 EOF
323         push(@out,$tmp);
324         if ($main'cpp)
325                 { push(@out,"\tTYPE($func,\@function)\n"); }
326         elsif ($main'gaswin)
327                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
328         else    { push(@out,"\t.type    $func,\@function\n"); }
329         push(@out,"$func:\n");
330         $stack=4;
331         }
332
333 sub main'function_end
334         {
335         local($func)=@_;
336
337         $func=$under.$func;
338
339         local($tmp)=<<"EOF";
340         popl    %edi
341         popl    %esi
342         popl    %ebx
343         popl    %ebp
344         ret
345 $const
346 .${func}_end:
347 EOF
348         push(@out,$tmp);
349         $const="";
350
351         if ($main'cpp)
352                 { push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
353         elsif ($main'gaswin)
354                 { $tmp=push(@out,"\t.align 4\n"); }
355         else    { push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
356         push(@out,".ident       \"$func\"\n");
357         $stack=0;
358         %label=();
359         }
360
361 sub main'function_end_A
362         {
363         local($func)=@_;
364
365         local($tmp)=<<"EOF";
366         popl    %edi
367         popl    %esi
368         popl    %ebx
369         popl    %ebp
370         ret
371 EOF
372         push(@out,$tmp);
373         }
374
375 sub main'function_end_B
376         {
377         local($func)=@_;
378
379         $func=$under.$func;
380
381         push(@out,".L_${func}_end:\n");
382         if ($main'cpp)
383                 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
384         elsif ($main'gaswin)
385                 { push(@out,"\t.align 4\n"); }
386         else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
387         push(@out,".ident       \"desasm.pl\"\n");
388         $stack=0;
389         %label=();
390         }
391
392 sub main'wparam
393         {
394         local($num)=@_;
395
396         return(&main'DWP($stack+$num*4,"esp","",0));
397         }
398
399 sub main'stack_push
400         {
401         local($num)=@_;
402         $stack+=$num*4;
403         &main'sub("esp",$num*4);
404         }
405
406 sub main'stack_pop
407         {
408         local($num)=@_;
409         $stack-=$num*4;
410         &main'add("esp",$num*4);
411         }
412
413 sub main'swtmp
414         {
415         return(&main'DWP($_[0]*4,"esp","",0));
416         }
417
418 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
419 #sub main'wtmp
420 #       {
421 #       local($num)=@_;
422 #
423 #       return(&main'DWP(-($num+1)*4,"esp","",0));
424 #       }
425
426 sub main'comment
427         {
428         foreach (@_)
429                 {
430                 if (/^\s*$/)
431                         { push(@out,"\n"); }
432                 else
433                         { push(@out,"\t$com_start $_ $com_end\n"); }
434                 }
435         }
436
437 sub main'label
438         {
439         if (!defined($label{$_[0]}))
440                 {
441                 $label{$_[0]}=".${label}${_[0]}";
442                 $label++;
443                 }
444         return($label{$_[0]});
445         }
446
447 sub main'set_label
448         {
449         if (!defined($label{$_[0]}))
450                 {
451                 $label{$_[0]}=".${label}${_[0]}";
452                 $label++;
453                 }
454         push(@out,".align $align\n") if ($_[1] != 0);
455         push(@out,"$label{$_[0]}:\n");
456         }
457
458 sub main'file_end
459         {
460         }
461
462 sub main'data_word
463         {
464         push(@out,"\t.long $_[0]\n");
465         }
466
467 sub main'puts
468         {
469             $constl++;
470             &main'push('$Lstring' . $constl);
471             &main'call('puts');
472             &main'add("esp",4);
473
474             $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
475         }
476
477 sub main'putx
478         {
479             $constl++;
480             &main'push($_[0]);
481             &main'push('$Lstring' . $constl);
482             &main'call('printf');
483             &main'add("esp",8);
484
485             $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
486         }