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