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