OSDN Git Service

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