Initial support for MacOS is now available
[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         else    { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
296         push(@out,"$func:\n");
297         $tmp=<<"EOF";
298         pushl   %ebp
299         pushl   %ebx
300         pushl   %esi
301         pushl   %edi
302
303 EOF
304         push(@out,$tmp);
305         $stack=20;
306         }
307
308 sub main'function_begin_B
309         {
310         local($func,$extra)=@_;
311
312         &main'external_label($func);
313         $func=$under.$func;
314
315         local($tmp)=<<"EOF";
316 .text
317         .align $align
318 .globl $func
319 EOF
320         push(@out,$tmp);
321         if ($main'cpp)
322                 { push(@out,"\tTYPE($func,\@function)\n"); }
323         else    { push(@out,"\t.type    $func,\@function\n"); }
324         push(@out,"$func:\n");
325         $stack=4;
326         }
327
328 sub main'function_end
329         {
330         local($func)=@_;
331
332         $func=$under.$func;
333
334         local($tmp)=<<"EOF";
335         popl    %edi
336         popl    %esi
337         popl    %ebx
338         popl    %ebp
339         ret
340 .${func}_end:
341 EOF
342         push(@out,$tmp);
343         if ($main'cpp)
344                 { push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
345         else    { push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
346         push(@out,".ident       \"$func\"\n");
347         $stack=0;
348         %label=();
349         }
350
351 sub main'function_end_A
352         {
353         local($func)=@_;
354
355         local($tmp)=<<"EOF";
356         popl    %edi
357         popl    %esi
358         popl    %ebx
359         popl    %ebp
360         ret
361 EOF
362         push(@out,$tmp);
363         }
364
365 sub main'function_end_B
366         {
367         local($func)=@_;
368
369         $func=$under.$func;
370
371         push(@out,".L_${func}_end:\n");
372         if ($main'cpp)
373                 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
374         else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
375         push(@out,".ident       \"desasm.pl\"\n");
376         $stack=0;
377         %label=();
378         }
379
380 sub main'wparam
381         {
382         local($num)=@_;
383
384         return(&main'DWP($stack+$num*4,"esp","",0));
385         }
386
387 sub main'stack_push
388         {
389         local($num)=@_;
390         $stack+=$num*4;
391         &main'sub("esp",$num*4);
392         }
393
394 sub main'stack_pop
395         {
396         local($num)=@_;
397         $stack-=$num*4;
398         &main'add("esp",$num*4);
399         }
400
401 sub main'swtmp
402         {
403         return(&main'DWP($_[0]*4,"esp","",0));
404         }
405
406 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
407 #sub main'wtmp
408 #       {
409 #       local($num)=@_;
410 #
411 #       return(&main'DWP(-($num+1)*4,"esp","",0));
412 #       }
413
414 sub main'comment
415         {
416         foreach (@_)
417                 {
418                 if (/^\s*$/)
419                         { push(@out,"\n"); }
420                 else
421                         { push(@out,"\t$com_start $_ $com_end\n"); }
422                 }
423         }
424
425 sub main'label
426         {
427         if (!defined($label{$_[0]}))
428                 {
429                 $label{$_[0]}=".${label}${_[0]}";
430                 $label++;
431                 }
432         return($label{$_[0]});
433         }
434
435 sub main'set_label
436         {
437         if (!defined($label{$_[0]}))
438                 {
439                 $label{$_[0]}=".${label}${_[0]}";
440                 $label++;
441                 }
442         push(@out,".align $align\n") if ($_[1] != 0);
443         push(@out,"$label{$_[0]}:\n");
444         }
445
446 sub main'file_end
447         {
448         }
449
450 sub main'data_word
451         {
452         push(@out,"\t.long $_[0]\n");
453         }