fbb4afb9bda48c7110318cdaedd99efc628bdb67
[openssl.git] / crypto / perlasm / x86ms.pl
1 #!/usr/local/bin/perl
2
3 package x86ms;
4
5 $label="L000";
6
7 %lb=(   'eax',  'al',
8         'ebx',  'bl',
9         'ecx',  'cl',
10         'edx',  'dl',
11         'ax',   'al',
12         'bx',   'bl',
13         'cx',   'cl',
14         'dx',   'dl',
15         );
16
17 %hb=(   'eax',  'ah',
18         'ebx',  'bh',
19         'ecx',  'ch',
20         'edx',  'dh',
21         'ax',   'ah',
22         'bx',   'bh',
23         'cx',   'ch',
24         'dx',   'dh',
25         );
26
27 sub main'asm_init_output { @out=(); }
28 sub main'asm_get_output { return(@out); }
29 sub main'get_labels { return(@labels); }
30 sub main'external_label { push(@labels,@_); }
31
32 sub main'LB
33         {
34         (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
35         return($lb{$_[0]});
36         }
37
38 sub main'HB
39         {
40         (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
41         return($hb{$_[0]});
42         }
43
44 sub main'BP
45         {
46         &get_mem("BYTE",@_);
47         }
48
49 sub main'DWP
50         {
51         &get_mem("DWORD",@_);
52         }
53
54 sub main'BC
55         {
56         return @_;
57         }
58
59 sub main'DWC
60         {
61         return @_;
62         }
63
64 sub main'stack_push
65         {
66         local($num)=@_;
67         $stack+=$num*4;
68         &main'sub("esp",$num*4);
69         }
70
71 sub main'stack_pop
72         {
73         local($num)=@_;
74         $stack-=$num*4;
75         &main'add("esp",$num*4);
76         }
77
78 sub get_mem
79         {
80         local($size,$addr,$reg1,$reg2,$idx)=@_;
81         local($t,$post);
82         local($ret)="$size PTR ";
83
84         $addr =~ s/^\s+//;
85         if ($addr =~ /^(.+)\+(.+)$/)
86                 {
87                 $reg2=&conv($1);
88                 $addr="_$2";
89                 }
90         elsif ($addr =~ /^[_a-zA-Z]/)
91                 {
92                 $addr="_$addr";
93                 }
94
95         if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }
96
97         $reg1="$regs{$reg1}" if defined($regs{$reg1});
98         $reg2="$regs{$reg2}" if defined($regs{$reg2});
99         if (($addr ne "") && ($addr ne 0))
100                 {
101                 if ($addr !~ /^-/)
102                         { $ret.=$addr; }
103                 else    { $post=$addr; }
104                 }
105         if ($reg2 ne "")
106                 {
107                 $t="";
108                 $t="*$idx" if ($idx != 0);
109                 $reg1="+".$reg1 if ("$reg1$post" ne "");
110                 $ret.="[$reg2$t$reg1$post]";
111                 }
112         else
113                 {
114                 $ret.="[$reg1$post]"
115                 }
116         $ret =~ s/\[\]//;       # in case $addr was the only argument
117         return($ret);
118         }
119
120 sub main'mov    { &out2("mov",@_); }
121 sub main'movb   { &out2("mov",@_); }
122 sub main'and    { &out2("and",@_); }
123 sub main'or     { &out2("or",@_); }
124 sub main'shl    { &out2("shl",@_); }
125 sub main'shr    { &out2("shr",@_); }
126 sub main'xor    { &out2("xor",@_); }
127 sub main'xorb   { &out2("xor",@_); }
128 sub main'add    { &out2("add",@_); }
129 sub main'adc    { &out2("adc",@_); }
130 sub main'sub    { &out2("sub",@_); }
131 sub main'rotl   { &out2("rol",@_); }
132 sub main'rotr   { &out2("ror",@_); }
133 sub main'exch   { &out2("xchg",@_); }
134 sub main'cmp    { &out2("cmp",@_); }
135 sub main'lea    { &out2("lea",@_); }
136 sub main'mul    { &out1("mul",@_); }
137 sub main'div    { &out1("div",@_); }
138 sub main'dec    { &out1("dec",@_); }
139 sub main'inc    { &out1("inc",@_); }
140 sub main'jmp    { &out1("jmp",@_); }
141 sub main'jmp_ptr { &out1p("jmp",@_); }
142 sub main'je     { &out1("je",@_); }
143 sub main'jle    { &out1("jle",@_); }
144 sub main'jz     { &out1("jz",@_); }
145 sub main'jge    { &out1("jge",@_); }
146 sub main'jl     { &out1("jl",@_); }
147 sub main'ja     { &out1("ja",@_); }
148 sub main'jae    { &out1("jae",@_); }
149 sub main'jb     { &out1("jb",@_); }
150 sub main'jbe    { &out1("jbe",@_); }
151 sub main'jc     { &out1("jc",@_); }
152 sub main'jnc    { &out1("jnc",@_); }
153 sub main'jnz    { &out1("jnz",@_); }
154 sub main'jne    { &out1("jne",@_); }
155 sub main'jno    { &out1("jno",@_); }
156 sub main'push   { &out1("push",@_); $stack+=4; }
157 sub main'pop    { &out1("pop",@_); $stack-=4; }
158 sub main'bswap  { &out1("bswap",@_); &using486(); }
159 sub main'not    { &out1("not",@_); }
160 sub main'call   { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
161 sub main'ret    { &out0("ret"); }
162 sub main'nop    { &out0("nop"); }
163
164 sub out2
165         {
166         local($name,$p1,$p2)=@_;
167         local($l,$t);
168
169         push(@out,"\t$name\t");
170         $t=&conv($p1).",";
171         $l=length($t);
172         push(@out,$t);
173         $l=4-($l+9)/8;
174         push(@out,"\t" x $l);
175         push(@out,&conv($p2));
176         push(@out,"\n");
177         }
178
179 sub out0
180         {
181         local($name)=@_;
182
183         push(@out,"\t$name\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 conv
195         {
196         local($p)=@_;
197
198         $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
199         return $p;
200         }
201
202 sub using486
203         {
204         return if $using486;
205         $using486++;
206         grep(s/\.386/\.486/,@out);
207         }
208
209 sub main'file
210         {
211         local($file)=@_;
212
213         local($tmp)=<<"EOF";
214         TITLE   $file.asm
215         .386
216 .model FLAT
217 EOF
218         push(@out,$tmp);
219         }
220
221 sub main'function_begin
222         {
223         local($func,$extra)=@_;
224
225         push(@labels,$func);
226
227         local($tmp)=<<"EOF";
228 _TEXT   SEGMENT
229 PUBLIC  _$func
230 $extra
231 _$func PROC NEAR
232         push    ebp
233         push    ebx
234         push    esi
235         push    edi
236 EOF
237         push(@out,$tmp);
238         $stack=20;
239         }
240
241 sub main'function_begin_B
242         {
243         local($func,$extra)=@_;
244
245         local($tmp)=<<"EOF";
246 _TEXT   SEGMENT
247 PUBLIC  _$func
248 $extra
249 _$func PROC NEAR
250 EOF
251         push(@out,$tmp);
252         $stack=4;
253         }
254
255 sub main'function_end
256         {
257         local($func)=@_;
258
259         local($tmp)=<<"EOF";
260         pop     edi
261         pop     esi
262         pop     ebx
263         pop     ebp
264         ret
265 _$func ENDP
266 _TEXT   ENDS
267 EOF
268         push(@out,$tmp);
269         $stack=0;
270         %label=();
271         }
272
273 sub main'function_end_B
274         {
275         local($func)=@_;
276
277         local($tmp)=<<"EOF";
278 _$func ENDP
279 _TEXT   ENDS
280 EOF
281         push(@out,$tmp);
282         $stack=0;
283         %label=();
284         }
285
286 sub main'function_end_A
287         {
288         local($func)=@_;
289
290         local($tmp)=<<"EOF";
291         pop     edi
292         pop     esi
293         pop     ebx
294         pop     ebp
295         ret
296 EOF
297         push(@out,$tmp);
298         }
299
300 sub main'file_end
301         {
302         push(@out,"END\n");
303         }
304
305 sub main'wparam
306         {
307         local($num)=@_;
308
309         return(&main'DWP($stack+$num*4,"esp","",0));
310         }
311
312 sub main'swtmp
313         {
314         return(&main'DWP($_[0]*4,"esp","",0));
315         }
316
317 # Should use swtmp, which is above esp.  Linix can trash the stack above esp
318 #sub main'wtmp
319 #       {
320 #       local($num)=@_;
321 #
322 #       return(&main'DWP(-(($num+1)*4),"esp","",0));
323 #       }
324
325 sub main'comment
326         {
327         foreach (@_)
328                 {
329                 push(@out,"\t; $_\n");
330                 }
331         }
332
333 sub main'label
334         {
335         if (!defined($label{$_[0]}))
336                 {
337                 $label{$_[0]}="\$${label}${_[0]}";
338                 $label++;
339                 }
340         return($label{$_[0]});
341         }
342
343 sub main'set_label
344         {
345         if (!defined($label{$_[0]}))
346                 {
347                 $label{$_[0]}="\$${label}${_[0]}";
348                 $label++;
349                 }
350         if((defined $_[2]) && ($_[2] == 1))
351                 {
352                 push(@out,"$label{$_[0]}::\n");
353                 }
354         else
355                 {
356                 push(@out,"$label{$_[0]}:\n");
357                 }
358         }
359
360 sub main'data_word
361         {
362         push(@out,"\tDD\t$_[0]\n");
363         }
364
365 sub out1p
366         {
367         local($name,$p1)=@_;
368         local($l,$t);
369
370         push(@out,"\t$name\t ".&conv($p1)."\n");
371         }
372
373 sub main'picmeup
374         {
375         local($dst,$sym)=@_;
376         &main'lea($dst,&main'DWP($sym));
377         }
378
379 sub main'blindpop { &out1("pop",@_); }