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