309060ea001856b9eb8616b0edadffeb9b8660c4
[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'BC
104         {
105         return @_;
106         }
107
108 sub main'DWC
109         {
110         return @_;
111         }
112
113 #sub main'BP
114 #       {
115 #       local($addr,$reg1,$reg2,$idx)=@_;
116 #
117 #       $ret="";
118 #
119 #       $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
120 #       $reg1="$regs{$reg1}" if defined($regs{$reg1});
121 #       $reg2="$regs{$reg2}" if defined($regs{$reg2});
122 #       $ret.=$addr if ($addr ne "") && ($addr ne 0);
123 #       if ($reg2 ne "")
124 #               { $ret.="($reg1,$reg2,$idx)"; }
125 #       else
126 #               { $ret.="($reg1)" }
127 #       return($ret);
128 #       }
129
130 sub main'mov    { &out2("movl",@_); }
131 sub main'movb   { &out2("movb",@_); }
132 sub main'and    { &out2("andl",@_); }
133 sub main'or     { &out2("orl",@_); }
134 sub main'shl    { &out2("sall",@_); }
135 sub main'shr    { &out2("shrl",@_); }
136 sub main'xor    { &out2("xorl",@_); }
137 sub main'xorb   { &out2("xorb",@_); }
138 sub main'add    { &out2("addl",@_); }
139 sub main'adc    { &out2("adcl",@_); }
140 sub main'sub    { &out2("subl",@_); }
141 sub main'rotl   { &out2("roll",@_); }
142 sub main'rotr   { &out2("rorl",@_); }
143 sub main'exch   { &out2("xchg",@_); }
144 sub main'cmp    { &out2("cmpl",@_); }
145 sub main'lea    { &out2("leal",@_); }
146 sub main'mul    { &out1("mull",@_); }
147 sub main'div    { &out1("divl",@_); }
148 sub main'jmp    { &out1("jmp",@_); }
149 sub main'jmp_ptr { &out1p("jmp",@_); }
150 sub main'je     { &out1("je",@_); }
151 sub main'jle    { &out1("jle",@_); }
152 sub main'jne    { &out1("jne",@_); }
153 sub main'jnz    { &out1("jnz",@_); }
154 sub main'jz     { &out1("jz",@_); }
155 sub main'jge    { &out1("jge",@_); }
156 sub main'jl     { &out1("jl",@_); }
157 sub main'jb     { &out1("jb",@_); }
158 sub main'jc     { &out1("jc",@_); }
159 sub main'jnc    { &out1("jnc",@_); }
160 sub main'jno    { &out1("jno",@_); }
161 sub main'dec    { &out1("decl",@_); }
162 sub main'inc    { &out1("incl",@_); }
163 sub main'push   { &out1("pushl",@_); $stack+=4; }
164 sub main'pop    { &out1("popl",@_); $stack-=4; }
165 sub main'not    { &out1("notl",@_); }
166 sub main'call   { &out1("call",$under.$_[0]); }
167 sub main'ret    { &out0("ret"); }
168 sub main'nop    { &out0("nop"); }
169
170 # The bswapl instruction is new for the 486. Emulate if i386.
171 sub main'bswap
172         {
173         if ($main'i386)
174                 {
175                 &main'comment("bswapl @_");
176                 &main'exch(main'HB(@_),main'LB(@_));
177                 &main'rotr(@_,16);
178                 &main'exch(main'HB(@_),main'LB(@_));
179                 }
180         else
181                 {
182                 &out1("bswapl",@_);
183                 }
184         }
185
186 sub out2
187         {
188         local($name,$p1,$p2)=@_;
189         local($l,$ll,$t);
190         local(%special)=(       "roll",0xD1C0,"rorl",0xD1C8,
191                                 "rcll",0xD1D0,"rcrl",0xD1D8,
192                                 "shll",0xD1E0,"shrl",0xD1E8,
193                                 "sarl",0xD1F8);
194         
195         if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
196                 {
197                 $op=$special{$name}|$reg_val{$p1};
198                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
199                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
200                 push(@out,$tmp1);
201                 push(@out,$tmp2);
202
203                 $p2=&conv($p2);
204                 $p1=&conv($p1);
205                 &main'comment("$name $p2 $p1");
206                 return;
207                 }
208
209         push(@out,"\t$name\t");
210         $t=&conv($p2).",";
211         $l=length($t);
212         push(@out,$t);
213         $ll=4-($l+9)/8;
214         $tmp1=sprintf("\t" x $ll);
215         push(@out,$tmp1);
216         push(@out,&conv($p1)."\n");
217         }
218
219 sub out1
220         {
221         local($name,$p1)=@_;
222         local($l,$t);
223         local(%special)=("bswapl",0x0FC8);
224
225         if ((defined($special{$name})) && defined($regs{$p1}))
226                 {
227                 $op=$special{$name}|$reg_val{$p1};
228                 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
229                 $tmp2=sprintf(".byte %d\t",$op     &0xff);
230                 push(@out,$tmp1);
231                 push(@out,$tmp2);
232
233                 $p2=&conv($p2);
234                 $p1=&conv($p1);
235                 &main'comment("$name $p2 $p1");
236                 return;
237                 }
238
239         push(@out,"\t$name\t".&conv($p1)."\n");
240         }
241
242 sub out1p
243         {
244         local($name,$p1)=@_;
245         local($l,$t);
246
247         push(@out,"\t$name\t*".&conv($p1)."\n");
248         }
249
250 sub out0
251         {
252         push(@out,"\t$_[0]\n");
253         }
254
255 sub conv
256         {
257         local($p)=@_;
258
259 #       $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
260
261         $p=$regs{$p} if (defined($regs{$p}));
262
263         $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
264         $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
265         return $p;
266         }
267
268 sub main'file
269         {
270         local($file)=@_;
271
272         local($tmp)=<<"EOF";
273         .file   "$file.s"
274         .version        "01.01"
275 gcc2_compiled.:
276 EOF
277         push(@out,$tmp);
278         }
279
280 sub main'function_begin
281         {
282         local($func)=@_;
283
284         &main'external_label($func);
285         $func=$under.$func;
286
287         local($tmp)=<<"EOF";
288 .text
289         .align $align
290 .globl $func
291 EOF
292         push(@out,$tmp);
293         if ($main'cpp)
294                 { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
295         elsif ($main'gaswin)
296                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
297         else    { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
298         push(@out,"$func:\n");
299         $tmp=<<"EOF";
300         pushl   %ebp
301         pushl   %ebx
302         pushl   %esi
303         pushl   %edi
304
305 EOF
306         push(@out,$tmp);
307         $stack=20;
308         }
309
310 sub main'function_begin_B
311         {
312         local($func,$extra)=@_;
313
314         &main'external_label($func);
315         $func=$under.$func;
316
317         local($tmp)=<<"EOF";
318 .text
319         .align $align
320 .globl $func
321 EOF
322         push(@out,$tmp);
323         if ($main'cpp)
324                 { push(@out,"\tTYPE($func,\@function)\n"); }
325         elsif ($main'gaswin)
326                 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
327         else    { push(@out,"\t.type    $func,\@function\n"); }
328         push(@out,"$func:\n");
329         $stack=4;
330         }
331
332 sub main'function_end
333         {
334         local($func)=@_;
335
336         $func=$under.$func;
337
338         local($tmp)=<<"EOF";
339         popl    %edi
340         popl    %esi
341         popl    %ebx
342         popl    %ebp
343         ret
344 .${func}_end:
345 EOF
346         push(@out,$tmp);
347         if ($main'cpp)
348                 { push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
349         elsif ($main'gaswin)
350                 { $tmp=push(@out,"\t.align 4\n"); }
351         else    { push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
352         push(@out,".ident       \"$func\"\n");
353         $stack=0;
354         %label=();
355         }
356
357 sub main'function_end_A
358         {
359         local($func)=@_;
360
361         local($tmp)=<<"EOF";
362         popl    %edi
363         popl    %esi
364         popl    %ebx
365         popl    %ebp
366         ret
367 EOF
368         push(@out,$tmp);
369         }
370
371 sub main'function_end_B
372         {
373         local($func)=@_;
374
375         $func=$under.$func;
376
377         push(@out,".L_${func}_end:\n");
378         if ($main'cpp)
379                 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
380         elsif ($main'gaswin)
381                 { push(@out,"\t.align 4\n"); }
382         else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
383         push(@out,".ident       \"desasm.pl\"\n");
384         $stack=0;
385         %label=();
386         }
387
388 sub main'wparam
389         {
390         local($num)=@_;
391
392         return(&main'DWP($stack+$num*4,"esp","",0));
393         }
394
395 sub main'stack_push
396         {
397         local($num)=@_;
398         $stack+=$num*4;
399         &main'sub("esp",$num*4);
400         }
401
402 sub main'stack_pop
403         {
404         local($num)=@_;
405         $stack-=$num*4;
406         &main'add("esp",$num*4);
407         }
408
409 sub main'swtmp
410         {
411         return(&main'DWP($_[0]*4,"esp","",0));
412         }
413
414 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
415 #sub main'wtmp
416 #       {
417 #       local($num)=@_;
418 #
419 #       return(&main'DWP(-($num+1)*4,"esp","",0));
420 #       }
421
422 sub main'comment
423         {
424         foreach (@_)
425                 {
426                 if (/^\s*$/)
427                         { push(@out,"\n"); }
428                 else
429                         { push(@out,"\t$com_start $_ $com_end\n"); }
430                 }
431         }
432
433 sub main'label
434         {
435         if (!defined($label{$_[0]}))
436                 {
437                 $label{$_[0]}=".${label}${_[0]}";
438                 $label++;
439                 }
440         return($label{$_[0]});
441         }
442
443 sub main'set_label
444         {
445         if (!defined($label{$_[0]}))
446                 {
447                 $label{$_[0]}=".${label}${_[0]}";
448                 $label++;
449                 }
450         push(@out,".align $align\n") if ($_[1] != 0);
451         push(@out,"$label{$_[0]}:\n");
452         }
453
454 sub main'file_end
455         {
456         }
457
458 sub main'data_word
459         {
460         push(@out,"\t.long $_[0]\n");
461         }