OSDN Git Service

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