summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/perlasm
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/perlasm')
-rw-r--r--src/lib/libcrypto/perlasm/alpha.pl434
-rw-r--r--src/lib/libcrypto/perlasm/cbc.pl351
-rwxr-xr-xsrc/lib/libcrypto/perlasm/ppc-xlate.pl152
-rw-r--r--src/lib/libcrypto/perlasm/readme124
-rwxr-xr-xsrc/lib/libcrypto/perlasm/x86_64-xlate.pl916
-rw-r--r--src/lib/libcrypto/perlasm/x86asm.pl221
-rw-r--r--src/lib/libcrypto/perlasm/x86gas.pl267
-rw-r--r--src/lib/libcrypto/perlasm/x86masm.pl184
-rw-r--r--src/lib/libcrypto/perlasm/x86ms.pl472
-rw-r--r--src/lib/libcrypto/perlasm/x86unix.pl725
10 files changed, 2215 insertions, 1631 deletions
diff --git a/src/lib/libcrypto/perlasm/alpha.pl b/src/lib/libcrypto/perlasm/alpha.pl
deleted file mode 100644
index 3dac571743..0000000000
--- a/src/lib/libcrypto/perlasm/alpha.pl
+++ /dev/null
@@ -1,434 +0,0 @@
1#!/usr/local/bin/perl
2
3package alpha;
4use Carp qw(croak cluck);
5
6$label="100";
7
8$n_debug=0;
9$smear_regs=1;
10$reg_alloc=1;
11
12$align="3";
13$com_start="#";
14
15sub main'asm_init_output { @out=(); }
16sub main'asm_get_output { return(@out); }
17sub main'get_labels { return(@labels); }
18sub main'external_label { push(@labels,@_); }
19
20# General registers
21
22%regs=( 'r0', '$0',
23 'r1', '$1',
24 'r2', '$2',
25 'r3', '$3',
26 'r4', '$4',
27 'r5', '$5',
28 'r6', '$6',
29 'r7', '$7',
30 'r8', '$8',
31 'r9', '$22',
32 'r10', '$23',
33 'r11', '$24',
34 'r12', '$25',
35 'r13', '$27',
36 'r14', '$28',
37 'r15', '$21', # argc == 5
38 'r16', '$20', # argc == 4
39 'r17', '$19', # argc == 3
40 'r18', '$18', # argc == 2
41 'r19', '$17', # argc == 1
42 'r20', '$16', # argc == 0
43 'r21', '$9', # save 0
44 'r22', '$10', # save 1
45 'r23', '$11', # save 2
46 'r24', '$12', # save 3
47 'r25', '$13', # save 4
48 'r26', '$14', # save 5
49
50 'a0', '$16',
51 'a1', '$17',
52 'a2', '$18',
53 'a3', '$19',
54 'a4', '$20',
55 'a5', '$21',
56
57 's0', '$9',
58 's1', '$10',
59 's2', '$11',
60 's3', '$12',
61 's4', '$13',
62 's5', '$14',
63 'zero', '$31',
64 'sp', '$30',
65 );
66
67$main'reg_s0="r21";
68$main'reg_s1="r22";
69$main'reg_s2="r23";
70$main'reg_s3="r24";
71$main'reg_s4="r25";
72$main'reg_s5="r26";
73
74@reg=( '$0', '$1' ,'$2' ,'$3' ,'$4' ,'$5' ,'$6' ,'$7' ,'$8',
75 '$22','$23','$24','$25','$20','$21','$27','$28');
76
77
78sub main'sub { &out3("subq",@_); }
79sub main'add { &out3("addq",@_); }
80sub main'mov { &out3("bis",$_[0],$_[0],$_[1]); }
81sub main'or { &out3("bis",@_); }
82sub main'bis { &out3("bis",@_); }
83sub main'br { &out1("br",@_); }
84sub main'ld { &out2("ldq",@_); }
85sub main'st { &out2("stq",@_); }
86sub main'cmpult { &out3("cmpult",@_); }
87sub main'cmplt { &out3("cmplt",@_); }
88sub main'bgt { &out2("bgt",@_); }
89sub main'ble { &out2("ble",@_); }
90sub main'blt { &out2("blt",@_); }
91sub main'mul { &out3("mulq",@_); }
92sub main'muh { &out3("umulh",@_); }
93
94$main'QWS=8;
95
96sub main'asm_add
97 {
98 push(@out,@_);
99 }
100
101sub main'asm_finish
102 {
103 &main'file_end();
104 print &main'asm_get_output();
105 }
106
107sub main'asm_init
108 {
109 ($type,$fn)=@_;
110 $filename=$fn;
111
112 &main'asm_init_output();
113 &main'comment("Don't even think of reading this code");
114 &main'comment("It was automatically generated by $filename");
115 &main'comment("Which is a perl program used to generate the alpha assember.");
116 &main'comment("eric <eay\@cryptsoft.com>");
117 &main'comment("");
118
119 $filename =~ s/\.pl$//;
120 &main'file($filename);
121 }
122
123sub conv
124 {
125 local($r)=@_;
126 local($v);
127
128 return($regs{$r}) if defined($regs{$r});
129 return($r);
130 }
131
132sub main'QWPw
133 {
134 local($off,$reg)=@_;
135
136 return(&main'QWP($off*8,$reg));
137 }
138
139sub main'QWP
140 {
141 local($off,$reg)=@_;
142
143 $ret="$off(".&conv($reg).")";
144 return($ret);
145 }
146
147sub out3
148 {
149 local($name,$p1,$p2,$p3)=@_;
150
151 $p1=&conv($p1);
152 $p2=&conv($p2);
153 $p3=&conv($p3);
154 push(@out,"\t$name\t");
155 $l=length($p1)+1;
156 push(@out,$p1.",");
157 $ll=3-($l+9)/8;
158 $tmp1=sprintf("\t" x $ll);
159 push(@out,$tmp1);
160
161 $l=length($p2)+1;
162 push(@out,$p2.",");
163 $ll=3-($l+9)/8;
164 $tmp1=sprintf("\t" x $ll);
165 push(@out,$tmp1);
166
167 push(@out,&conv($p3)."\n");
168 }
169
170sub out2
171 {
172 local($name,$p1,$p2,$p3)=@_;
173
174 $p1=&conv($p1);
175 $p2=&conv($p2);
176 push(@out,"\t$name\t");
177 $l=length($p1)+1;
178 push(@out,$p1.",");
179 $ll=3-($l+9)/8;
180 $tmp1=sprintf("\t" x $ll);
181 push(@out,$tmp1);
182
183 push(@out,&conv($p2)."\n");
184 }
185
186sub out1
187 {
188 local($name,$p1)=@_;
189
190 $p1=&conv($p1);
191 push(@out,"\t$name\t".$p1."\n");
192 }
193
194sub out0
195 {
196 push(@out,"\t$_[0]\n");
197 }
198
199sub main'file
200 {
201 local($file)=@_;
202
203 local($tmp)=<<"EOF";
204 # DEC Alpha assember
205 # Generated from perl scripts contains in SSLeay
206 .file 1 "$file.s"
207 .set noat
208EOF
209 push(@out,$tmp);
210 }
211
212sub main'function_begin
213 {
214 local($func)=@_;
215
216print STDERR "$func\n";
217 local($tmp)=<<"EOF";
218 .text
219 .align $align
220 .globl $func
221 .ent $func
222${func}:
223${func}..ng:
224 .frame \$30,0,\$26,0
225 .prologue 0
226EOF
227 push(@out,$tmp);
228 $stack=0;
229 }
230
231sub main'function_end
232 {
233 local($func)=@_;
234
235 local($tmp)=<<"EOF";
236 ret \$31,(\$26),1
237 .end $func
238EOF
239 push(@out,$tmp);
240 $stack=0;
241 %label=();
242 }
243
244sub main'function_end_A
245 {
246 local($func)=@_;
247
248 local($tmp)=<<"EOF";
249 ret \$31,(\$26),1
250EOF
251 push(@out,$tmp);
252 }
253
254sub main'function_end_B
255 {
256 local($func)=@_;
257
258 $func=$under.$func;
259
260 push(@out,"\t.end $func\n");
261 $stack=0;
262 %label=();
263 }
264
265sub main'wparam
266 {
267 local($num)=@_;
268
269 if ($num < 6)
270 {
271 $num=20-$num;
272 return("r$num");
273 }
274 else
275 { return(&main'QWP($stack+$num*8,"sp")); }
276 }
277
278sub main'stack_push
279 {
280 local($num)=@_;
281 $stack+=$num*8;
282 &main'sub("sp",$num*8,"sp");
283 }
284
285sub main'stack_pop
286 {
287 local($num)=@_;
288 $stack-=$num*8;
289 &main'add("sp",$num*8,"sp");
290 }
291
292sub main'swtmp
293 {
294 return(&main'QWP(($_[0])*8,"sp"));
295 }
296
297# Should use swtmp, which is above sp. Linix can trash the stack above esp
298#sub main'wtmp
299# {
300# local($num)=@_;
301#
302# return(&main'QWP(-($num+1)*4,"esp","",0));
303# }
304
305sub main'comment
306 {
307 foreach (@_)
308 {
309 if (/^\s*$/)
310 { push(@out,"\n"); }
311 else
312 { push(@out,"\t$com_start $_ $com_end\n"); }
313 }
314 }
315
316sub main'label
317 {
318 if (!defined($label{$_[0]}))
319 {
320 $label{$_[0]}=$label;
321 $label++;
322 }
323 return('$'.$label{$_[0]});
324 }
325
326sub main'set_label
327 {
328 if (!defined($label{$_[0]}))
329 {
330 $label{$_[0]}=$label;
331 $label++;
332 }
333# push(@out,".align $align\n") if ($_[1] != 0);
334 push(@out,'$'."$label{$_[0]}:\n");
335 }
336
337sub main'file_end
338 {
339 }
340
341sub main'data_word
342 {
343 push(@out,"\t.long $_[0]\n");
344 }
345
346@pool_free=();
347@pool_taken=();
348$curr_num=0;
349$max=0;
350
351sub main'init_pool
352 {
353 local($args)=@_;
354 local($i);
355
356 @pool_free=();
357 for ($i=(14+(6-$args)); $i >= 0; $i--)
358 {
359 push(@pool_free,"r$i");
360 }
361 print STDERR "START :register pool:@pool_free\n";
362 $curr_num=$max=0;
363 }
364
365sub main'fin_pool
366 {
367 printf STDERR "END %2d:register pool:@pool_free\n",$max;
368 }
369
370sub main'GR
371 {
372 local($r)=@_;
373 local($i,@n,$_);
374
375 foreach (@pool_free)
376 {
377 if ($r ne $_)
378 { push(@n,$_); }
379 else
380 {
381 $curr_num++;
382 $max=$curr_num if ($curr_num > $max);
383 }
384 }
385 @pool_free=@n;
386print STDERR "GR:@pool_free\n" if $reg_alloc;
387 return(@_);
388 }
389
390sub main'NR
391 {
392 local($num)=@_;
393 local(@ret);
394
395 $num=1 if $num == 0;
396 ($#pool_free >= ($num-1)) || croak "out of registers: want $num, have @pool_free";
397 while ($num > 0)
398 {
399 push(@ret,pop @pool_free);
400 $curr_num++;
401 $max=$curr_num if ($curr_num > $max);
402 $num--
403 }
404 print STDERR "nr @ret\n" if $n_debug;
405print STDERR "NR:@pool_free\n" if $reg_alloc;
406 return(@ret);
407
408 }
409
410sub main'FR
411 {
412 local(@r)=@_;
413 local(@a,$v,$w);
414
415 print STDERR "fr @r\n" if $n_debug;
416# cluck "fr @r";
417 for $w (@pool_free)
418 {
419 foreach $v (@r)
420 {
421 croak "double register free of $v (@pool_free)" if $w eq $v;
422 }
423 }
424 foreach $v (@r)
425 {
426 croak "bad argument to FR" if ($v !~ /^r\d+$/);
427 if ($smear_regs)
428 { unshift(@pool_free,$v); }
429 else { push(@pool_free,$v); }
430 $curr_num--;
431 }
432print STDERR "FR:@pool_free\n" if $reg_alloc;
433 }
4341;
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl
new file mode 100644
index 0000000000..e43dc9ae15
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/cbc.pl
@@ -0,0 +1,351 @@
1#!/usr/local/bin/perl
2
3# void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
4# des_cblock (*input);
5# des_cblock (*output);
6# long length;
7# des_key_schedule schedule;
8# des_cblock (*ivec);
9# int enc;
10#
11# calls
12# des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
13#
14
15#&cbc("des_ncbc_encrypt","des_encrypt",0);
16#&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",
17# 1,4,5,3,5,-1);
18#&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",
19# 0,4,5,3,5,-1);
20#&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",
21# 0,6,7,3,4,5);
22#
23# When doing a cipher that needs bigendian order,
24# for encrypt, the iv is kept in bigendian form,
25# while for decrypt, it is kept in little endian.
26sub cbc
27 {
28 local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_;
29 # name is the function name
30 # enc_func and dec_func and the functions to call for encrypt/decrypt
31 # swap is true if byte order needs to be reversed
32 # iv_off is parameter number for the iv
33 # enc_off is parameter number for the encrypt/decrypt flag
34 # p1,p2,p3 are the offsets for parameters to be passed to the
35 # underlying calls.
36
37 &function_begin_B($name,"");
38 &comment("");
39
40 $in="esi";
41 $out="edi";
42 $count="ebp";
43
44 &push("ebp");
45 &push("ebx");
46 &push("esi");
47 &push("edi");
48
49 $data_off=4;
50 $data_off+=4 if ($p1 > 0);
51 $data_off+=4 if ($p2 > 0);
52 $data_off+=4 if ($p3 > 0);
53
54 &mov($count, &wparam(2)); # length
55
56 &comment("getting iv ptr from parameter $iv_off");
57 &mov("ebx", &wparam($iv_off)); # Get iv ptr
58
59 &mov($in, &DWP(0,"ebx","",0));# iv[0]
60 &mov($out, &DWP(4,"ebx","",0));# iv[1]
61
62 &push($out);
63 &push($in);
64 &push($out); # used in decrypt for iv[1]
65 &push($in); # used in decrypt for iv[0]
66
67 &mov("ebx", "esp"); # This is the address of tin[2]
68
69 &mov($in, &wparam(0)); # in
70 &mov($out, &wparam(1)); # out
71
72 # We have loaded them all, how lets push things
73 &comment("getting encrypt flag from parameter $enc_off");
74 &mov("ecx", &wparam($enc_off)); # Get enc flag
75 if ($p3 > 0)
76 {
77 &comment("get and push parameter $p3");
78 if ($enc_off != $p3)
79 { &mov("eax", &wparam($p3)); &push("eax"); }
80 else { &push("ecx"); }
81 }
82 if ($p2 > 0)
83 {
84 &comment("get and push parameter $p2");
85 if ($enc_off != $p2)
86 { &mov("eax", &wparam($p2)); &push("eax"); }
87 else { &push("ecx"); }
88 }
89 if ($p1 > 0)
90 {
91 &comment("get and push parameter $p1");
92 if ($enc_off != $p1)
93 { &mov("eax", &wparam($p1)); &push("eax"); }
94 else { &push("ecx"); }
95 }
96 &push("ebx"); # push data/iv
97
98 &cmp("ecx",0);
99 &jz(&label("decrypt"));
100
101 &and($count,0xfffffff8);
102 &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0]
103 &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1]
104
105 &jz(&label("encrypt_finish"));
106
107 #############################################################
108
109 &set_label("encrypt_loop");
110 # encrypt start
111 # "eax" and "ebx" hold iv (or the last cipher text)
112
113 &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes
114 &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes
115
116 &xor("eax", "ecx");
117 &xor("ebx", "edx");
118
119 &bswap("eax") if $swap;
120 &bswap("ebx") if $swap;
121
122 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
123 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
124
125 &call($enc_func);
126
127 &mov("eax", &DWP($data_off,"esp","",0));
128 &mov("ebx", &DWP($data_off+4,"esp","",0));
129
130 &bswap("eax") if $swap;
131 &bswap("ebx") if $swap;
132
133 &mov(&DWP(0,$out,"",0),"eax");
134 &mov(&DWP(4,$out,"",0),"ebx");
135
136 # eax and ebx are the next iv.
137
138 &add($in, 8);
139 &add($out, 8);
140
141 &sub($count, 8);
142 &jnz(&label("encrypt_loop"));
143
144###################################################################3
145 &set_label("encrypt_finish");
146 &mov($count, &wparam(2)); # length
147 &and($count, 7);
148 &jz(&label("finish"));
149 &call(&label("PIC_point"));
150&set_label("PIC_point");
151 &blindpop("edx");
152 &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx"));
153 &mov($count,&DWP(0,"ecx",$count,4))
154 &add($count,"edx");
155 &xor("ecx","ecx");
156 &xor("edx","edx");
157 #&mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4));
158 &jmp_ptr($count);
159
160&set_label("ej7");
161 &xor("edx", "edx") if $ppro; # ppro friendly
162 &movb(&HB("edx"), &BP(6,$in,"",0));
163 &shl("edx",8);
164&set_label("ej6");
165 &movb(&HB("edx"), &BP(5,$in,"",0));
166&set_label("ej5");
167 &movb(&LB("edx"), &BP(4,$in,"",0));
168&set_label("ej4");
169 &mov("ecx", &DWP(0,$in,"",0));
170 &jmp(&label("ejend"));
171&set_label("ej3");
172 &movb(&HB("ecx"), &BP(2,$in,"",0));
173 &xor("ecx", "ecx") if $ppro; # ppro friendly
174 &shl("ecx",8);
175&set_label("ej2");
176 &movb(&HB("ecx"), &BP(1,$in,"",0));
177&set_label("ej1");
178 &movb(&LB("ecx"), &BP(0,$in,"",0));
179&set_label("ejend");
180
181 &xor("eax", "ecx");
182 &xor("ebx", "edx");
183
184 &bswap("eax") if $swap;
185 &bswap("ebx") if $swap;
186
187 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
188 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
189
190 &call($enc_func);
191
192 &mov("eax", &DWP($data_off,"esp","",0));
193 &mov("ebx", &DWP($data_off+4,"esp","",0));
194
195 &bswap("eax") if $swap;
196 &bswap("ebx") if $swap;
197
198 &mov(&DWP(0,$out,"",0),"eax");
199 &mov(&DWP(4,$out,"",0),"ebx");
200
201 &jmp(&label("finish"));
202
203 #############################################################
204 #############################################################
205 &set_label("decrypt",1);
206 # decrypt start
207 &and($count,0xfffffff8);
208 # The next 2 instructions are only for if the jz is taken
209 &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
210 &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
211 &jz(&label("decrypt_finish"));
212
213 &set_label("decrypt_loop");
214 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
215 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
216
217 &bswap("eax") if $swap;
218 &bswap("ebx") if $swap;
219
220 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
221 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
222
223 &call($dec_func);
224
225 &mov("eax", &DWP($data_off,"esp","",0)); # get return
226 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
227
228 &bswap("eax") if $swap;
229 &bswap("ebx") if $swap;
230
231 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
232 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
233
234 &xor("ecx", "eax");
235 &xor("edx", "ebx");
236
237 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
238 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
239
240 &mov(&DWP(0,$out,"",0),"ecx");
241 &mov(&DWP(4,$out,"",0),"edx");
242
243 &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
244 &mov(&DWP($data_off+12,"esp","",0), "ebx"); #
245
246 &add($in, 8);
247 &add($out, 8);
248
249 &sub($count, 8);
250 &jnz(&label("decrypt_loop"));
251############################ ENDIT #######################3
252 &set_label("decrypt_finish");
253 &mov($count, &wparam(2)); # length
254 &and($count, 7);
255 &jz(&label("finish"));
256
257 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
258 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
259
260 &bswap("eax") if $swap;
261 &bswap("ebx") if $swap;
262
263 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
264 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
265
266 &call($dec_func);
267
268 &mov("eax", &DWP($data_off,"esp","",0)); # get return
269 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
270
271 &bswap("eax") if $swap;
272 &bswap("ebx") if $swap;
273
274 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
275 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
276
277 &xor("ecx", "eax");
278 &xor("edx", "ebx");
279
280 # this is for when we exit
281 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
282 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
283
284&set_label("dj7");
285 &rotr("edx", 16);
286 &movb(&BP(6,$out,"",0), &LB("edx"));
287 &shr("edx",16);
288&set_label("dj6");
289 &movb(&BP(5,$out,"",0), &HB("edx"));
290&set_label("dj5");
291 &movb(&BP(4,$out,"",0), &LB("edx"));
292&set_label("dj4");
293 &mov(&DWP(0,$out,"",0), "ecx");
294 &jmp(&label("djend"));
295&set_label("dj3");
296 &rotr("ecx", 16);
297 &movb(&BP(2,$out,"",0), &LB("ecx"));
298 &shl("ecx",16);
299&set_label("dj2");
300 &movb(&BP(1,$in,"",0), &HB("ecx"));
301&set_label("dj1");
302 &movb(&BP(0,$in,"",0), &LB("ecx"));
303&set_label("djend");
304
305 # final iv is still in eax:ebx
306 &jmp(&label("finish"));
307
308
309############################ FINISH #######################3
310 &set_label("finish",1);
311 &mov("ecx", &wparam($iv_off)); # Get iv ptr
312
313 #################################################
314 $total=16+4;
315 $total+=4 if ($p1 > 0);
316 $total+=4 if ($p2 > 0);
317 $total+=4 if ($p3 > 0);
318 &add("esp",$total);
319
320 &mov(&DWP(0,"ecx","",0), "eax"); # save iv
321 &mov(&DWP(4,"ecx","",0), "ebx"); # save iv
322
323 &function_end_A($name);
324
325 &align(64);
326 &set_label("cbc_enc_jmp_table");
327 &data_word("0");
328 &data_word(&label("ej1")."-".&label("PIC_point"));
329 &data_word(&label("ej2")."-".&label("PIC_point"));
330 &data_word(&label("ej3")."-".&label("PIC_point"));
331 &data_word(&label("ej4")."-".&label("PIC_point"));
332 &data_word(&label("ej5")."-".&label("PIC_point"));
333 &data_word(&label("ej6")."-".&label("PIC_point"));
334 &data_word(&label("ej7")."-".&label("PIC_point"));
335 # not used
336 #&set_label("cbc_dec_jmp_table",1);
337 #&data_word("0");
338 #&data_word(&label("dj1")."-".&label("PIC_point"));
339 #&data_word(&label("dj2")."-".&label("PIC_point"));
340 #&data_word(&label("dj3")."-".&label("PIC_point"));
341 #&data_word(&label("dj4")."-".&label("PIC_point"));
342 #&data_word(&label("dj5")."-".&label("PIC_point"));
343 #&data_word(&label("dj6")."-".&label("PIC_point"));
344 #&data_word(&label("dj7")."-".&label("PIC_point"));
345 &align(64);
346
347 &function_end_B($name);
348
349 }
350
3511;
diff --git a/src/lib/libcrypto/perlasm/ppc-xlate.pl b/src/lib/libcrypto/perlasm/ppc-xlate.pl
new file mode 100755
index 0000000000..4579671c97
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/ppc-xlate.pl
@@ -0,0 +1,152 @@
1#!/usr/bin/env perl
2
3# PowerPC assembler distiller by <appro>.
4
5my $flavour = shift;
6my $output = shift;
7open STDOUT,">$output" || die "can't open $output: $!";
8
9my %GLOBALS;
10my $dotinlocallabels=($flavour=~/linux/)?1:0;
11
12################################################################
13# directives which need special treatment on different platforms
14################################################################
15my $globl = sub {
16 my $junk = shift;
17 my $name = shift;
18 my $global = \$GLOBALS{$name};
19 my $ret;
20
21 $name =~ s|^[\.\_]||;
22
23 SWITCH: for ($flavour) {
24 /aix/ && do { $name = ".$name";
25 last;
26 };
27 /osx/ && do { $name = "_$name";
28 last;
29 };
30 /linux.*32/ && do { $ret .= ".globl $name\n";
31 $ret .= ".type $name,\@function";
32 last;
33 };
34 /linux.*64/ && do { $ret .= ".globl .$name\n";
35 $ret .= ".type .$name,\@function\n";
36 $ret .= ".section \".opd\",\"aw\"\n";
37 $ret .= ".globl $name\n";
38 $ret .= ".align 3\n";
39 $ret .= "$name:\n";
40 $ret .= ".quad .$name,.TOC.\@tocbase,0\n";
41 $ret .= ".size $name,24\n";
42 $ret .= ".previous\n";
43
44 $name = ".$name";
45 last;
46 };
47 }
48
49 $ret = ".globl $name" if (!$ret);
50 $$global = $name;
51 $ret;
52};
53my $text = sub {
54 ($flavour =~ /aix/) ? ".csect" : ".text";
55};
56my $machine = sub {
57 my $junk = shift;
58 my $arch = shift;
59 if ($flavour =~ /osx/)
60 { $arch =~ s/\"//g;
61 $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
62 }
63 ".machine $arch";
64};
65my $asciz = sub {
66 shift;
67 my $line = join(",",@_);
68 if ($line =~ /^"(.*)"$/)
69 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
70 else
71 { ""; }
72};
73
74################################################################
75# simplified mnemonics not handled by at least one assembler
76################################################################
77my $cmplw = sub {
78 my $f = shift;
79 my $cr = 0; $cr = shift if ($#_>1);
80 # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
81 ($flavour =~ /linux.*32/) ?
82 " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
83 " cmplw ".join(',',$cr,@_);
84};
85my $bdnz = sub {
86 my $f = shift;
87 my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
88 " bc $bo,0,".shift;
89} if ($flavour!~/linux/);
90my $bltlr = sub {
91 my $f = shift;
92 my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
93 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
94 " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
95 " bclr $bo,0";
96};
97my $bnelr = sub {
98 my $f = shift;
99 my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
100 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
101 " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
102 " bclr $bo,2";
103};
104my $beqlr = sub {
105 my $f = shift;
106 my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
107 ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
108 " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
109 " bclr $bo,2";
110};
111# GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
112# arguments is 64, with "operand out of range" error.
113my $extrdi = sub {
114 my ($f,$ra,$rs,$n,$b) = @_;
115 $b = ($b+$n)&63; $n = 64-$n;
116 " rldicl $ra,$rs,$b,$n";
117};
118
119while($line=<>) {
120
121 $line =~ s|[#!;].*$||; # get rid of asm-style comments...
122 $line =~ s|/\*.*\*/||; # ... and C-style comments...
123 $line =~ s|^\s+||; # ... and skip white spaces in beginning...
124 $line =~ s|\s+$||; # ... and at the end
125
126 {
127 $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel
128 $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
129 }
130
131 {
132 $line =~ s|(^[\.\w]+)\:\s*||;
133 my $label = $1;
134 printf "%s:",($GLOBALS{$label} or $label) if ($label);
135 }
136
137 {
138 $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
139 my $c = $1; $c = "\t" if ($c eq "");
140 my $mnemonic = $2;
141 my $f = $3;
142 my $opcode = eval("\$$mnemonic");
143 $line =~ s|\bc?[rf]([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/);
144 if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
145 elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
146 }
147
148 print $line if ($line);
149 print "\n";
150}
151
152close STDOUT;
diff --git a/src/lib/libcrypto/perlasm/readme b/src/lib/libcrypto/perlasm/readme
new file mode 100644
index 0000000000..f02bbee75a
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/readme
@@ -0,0 +1,124 @@
1The perl scripts in this directory are my 'hack' to generate
2multiple different assembler formats via the one origional script.
3
4The way to use this library is to start with adding the path to this directory
5and then include it.
6
7push(@INC,"perlasm","../../perlasm");
8require "x86asm.pl";
9
10The first thing we do is setup the file and type of assember
11
12&asm_init($ARGV[0],$0);
13
14The first argument is the 'type'. Currently
15'cpp', 'sol', 'a.out', 'elf' or 'win32'.
16Argument 2 is the file name.
17
18The reciprocal function is
19&asm_finish() which should be called at the end.
20
21There are 2 main 'packages'. x86ms.pl, which is the microsoft assembler,
22and x86unix.pl which is the unix (gas) version.
23
24Functions of interest are:
25&external_label("des_SPtrans"); declare and external variable
26&LB(reg); Low byte for a register
27&HB(reg); High byte for a register
28&BP(off,base,index,scale) Byte pointer addressing
29&DWP(off,base,index,scale) Word pointer addressing
30&stack_push(num) Basically a 'sub esp, num*4' with extra
31&stack_pop(num) inverse of stack_push
32&function_begin(name,extra) Start a function with pushing of
33 edi, esi, ebx and ebp. extra is extra win32
34 external info that may be required.
35&function_begin_B(name,extra) Same as norma function_begin but no pushing.
36&function_end(name) Call at end of function.
37&function_end_A(name) Standard pop and ret, for use inside functions
38&function_end_B(name) Call at end but with poping or 'ret'.
39&swtmp(num) Address on stack temp word.
40&wparam(num) Parameter number num, that was push
41 in C convention. This all works over pushes
42 and pops.
43&comment("hello there") Put in a comment.
44&label("loop") Refer to a label, normally a jmp target.
45&set_label("loop") Set a label at this point.
46&data_word(word) Put in a word of data.
47
48So how does this all hold together? Given
49
50int calc(int len, int *data)
51 {
52 int i,j=0;
53
54 for (i=0; i<len; i++)
55 {
56 j+=other(data[i]);
57 }
58 }
59
60So a very simple version of this function could be coded as
61
62 push(@INC,"perlasm","../../perlasm");
63 require "x86asm.pl";
64
65 &asm_init($ARGV[0],"cacl.pl");
66
67 &external_label("other");
68
69 $tmp1= "eax";
70 $j= "edi";
71 $data= "esi";
72 $i= "ebp";
73
74 &comment("a simple function");
75 &function_begin("calc");
76 &mov( $data, &wparam(1)); # data
77 &xor( $j, $j);
78 &xor( $i, $i);
79
80 &set_label("loop");
81 &cmp( $i, &wparam(0));
82 &jge( &label("end"));
83
84 &mov( $tmp1, &DWP(0,$data,$i,4));
85 &push( $tmp1);
86 &call( "other");
87 &add( $j, "eax");
88 &pop( $tmp1);
89 &inc( $i);
90 &jmp( &label("loop"));
91
92 &set_label("end");
93 &mov( "eax", $j);
94
95 &function_end("calc");
96
97 &asm_finish();
98
99The above example is very very unoptimised but gives an idea of how
100things work.
101
102There is also a cbc mode function generator in cbc.pl
103
104&cbc( $name,
105 $encrypt_function_name,
106 $decrypt_function_name,
107 $true_if_byte_swap_needed,
108 $parameter_number_for_iv,
109 $parameter_number_for_encrypt_flag,
110 $first_parameter_to_pass,
111 $second_parameter_to_pass,
112 $third_parameter_to_pass);
113
114So for example, given
115void BF_encrypt(BF_LONG *data,BF_KEY *key);
116void BF_decrypt(BF_LONG *data,BF_KEY *key);
117void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
118 BF_KEY *ks, unsigned char *iv, int enc);
119
120&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1);
121
122&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
123&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
124
diff --git a/src/lib/libcrypto/perlasm/x86_64-xlate.pl b/src/lib/libcrypto/perlasm/x86_64-xlate.pl
new file mode 100755
index 0000000000..d66ad24095
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86_64-xlate.pl
@@ -0,0 +1,916 @@
1#!/usr/bin/env perl
2
3# Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
4#
5# Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
6# format is way easier to parse. Because it's simpler to "gear" from
7# Unix ABI to Windows one [see cross-reference "card" at the end of
8# file]. Because Linux targets were available first...
9#
10# In addition the script also "distills" code suitable for GNU
11# assembler, so that it can be compiled with more rigid assemblers,
12# such as Solaris /usr/ccs/bin/as.
13#
14# This translator is not designed to convert *arbitrary* assembler
15# code from AT&T format to MASM one. It's designed to convert just
16# enough to provide for dual-ABI OpenSSL modules development...
17# There *are* limitations and you might have to modify your assembler
18# code or this script to achieve the desired result...
19#
20# Currently recognized limitations:
21#
22# - can't use multiple ops per line;
23#
24# Dual-ABI styling rules.
25#
26# 1. Adhere to Unix register and stack layout [see cross-reference
27# ABI "card" at the end for explanation].
28# 2. Forget about "red zone," stick to more traditional blended
29# stack frame allocation. If volatile storage is actually required
30# that is. If not, just leave the stack as is.
31# 3. Functions tagged with ".type name,@function" get crafted with
32# unified Win64 prologue and epilogue automatically. If you want
33# to take care of ABI differences yourself, tag functions as
34# ".type name,@abi-omnipotent" instead.
35# 4. To optimize the Win64 prologue you can specify number of input
36# arguments as ".type name,@function,N." Keep in mind that if N is
37# larger than 6, then you *have to* write "abi-omnipotent" code,
38# because >6 cases can't be addressed with unified prologue.
39# 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
40# (sorry about latter).
41# 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
42# required to identify the spots, where to inject Win64 epilogue!
43# But on the pros, it's then prefixed with rep automatically:-)
44# 7. Stick to explicit ip-relative addressing. If you have to use
45# GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
46# Both are recognized and translated to proper Win64 addressing
47# modes. To support legacy code a synthetic directive, .picmeup,
48# is implemented. It puts address of the *next* instruction into
49# target register, e.g.:
50#
51# .picmeup %rax
52# lea .Label-.(%rax),%rax
53#
54# 8. In order to provide for structured exception handling unified
55# Win64 prologue copies %rsp value to %rax. For further details
56# see SEH paragraph at the end.
57# 9. .init segment is allowed to contain calls to functions only.
58# a. If function accepts more than 4 arguments *and* >4th argument
59# is declared as non 64-bit value, do clear its upper part.
60
61my $flavour = shift;
62my $output = shift;
63if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
64
65{ my ($stddev,$stdino,@junk)=stat(STDOUT);
66 my ($outdev,$outino,@junk)=stat($output);
67
68 open STDOUT,">$output" || die "can't open $output: $!"
69 if (1 || $stddev!=$outdev || $stdino!=$outino);
70}
71
72my $gas=1; $gas=0 if ($output =~ /\.asm$/);
73my $elf=1; $elf=0 if (!$gas);
74my $win64=0;
75my $prefix="";
76my $decor=".L";
77
78my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
79my $masm=0;
80my $PTR=" PTR";
81
82my $nasmref=2.03;
83my $nasm=0;
84
85if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
86 $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
87 chomp($prefix);
88 }
89elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
90elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
91elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
92elsif (!$gas)
93{ if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
94 { $nasm = $1 + $2*0.01; $PTR=""; }
95 elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
96 { $masm = $1 + $2*2**-16 + $4*2**-32; }
97 die "no assembler found on %PATH" if (!($nasm || $masm));
98 $win64=1;
99 $elf=0;
100 $decor="\$L\$";
101}
102
103my $current_segment;
104my $current_function;
105my %globals;
106
107{ package opcode; # pick up opcodes
108 sub re {
109 my $self = shift; # single instance in enough...
110 local *line = shift;
111 undef $ret;
112
113 if ($line =~ /^([a-z][a-z0-9]*)/i) {
114 $self->{op} = $1;
115 $ret = $self;
116 $line = substr($line,@+[0]); $line =~ s/^\s+//;
117
118 undef $self->{sz};
119 if ($self->{op} =~ /^(movz)b.*/) { # movz is pain...
120 $self->{op} = $1;
121 $self->{sz} = "b";
122 } elsif ($self->{op} =~ /call|jmp/) {
123 $self->{sz} = "";
124 } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op)/) { # SSEn
125 $self->{sz} = "";
126 } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
127 $self->{op} = $1;
128 $self->{sz} = $2;
129 }
130 }
131 $ret;
132 }
133 sub size {
134 my $self = shift;
135 my $sz = shift;
136 $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
137 $self->{sz};
138 }
139 sub out {
140 my $self = shift;
141 if ($gas) {
142 if ($self->{op} eq "movz") { # movz is pain...
143 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
144 } elsif ($self->{op} =~ /^set/) {
145 "$self->{op}";
146 } elsif ($self->{op} eq "ret") {
147 my $epilogue = "";
148 if ($win64 && $current_function->{abi} eq "svr4") {
149 $epilogue = "movq 8(%rsp),%rdi\n\t" .
150 "movq 16(%rsp),%rsi\n\t";
151 }
152 $epilogue . ".byte 0xf3,0xc3";
153 } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
154 ".p2align\t3\n\t.quad";
155 } else {
156 "$self->{op}$self->{sz}";
157 }
158 } else {
159 $self->{op} =~ s/^movz/movzx/;
160 if ($self->{op} eq "ret") {
161 $self->{op} = "";
162 if ($win64 && $current_function->{abi} eq "svr4") {
163 $self->{op} = "mov rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t".
164 "mov rsi,QWORD${PTR}[16+rsp]\n\t";
165 }
166 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
167 } elsif ($self->{op} =~ /^(pop|push)f/) {
168 $self->{op} .= $self->{sz};
169 } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
170 $self->{op} = "ALIGN\t8\n\tDQ";
171 }
172 $self->{op};
173 }
174 }
175 sub mnemonic {
176 my $self=shift;
177 my $op=shift;
178 $self->{op}=$op if (defined($op));
179 $self->{op};
180 }
181}
182{ package const; # pick up constants, which start with $
183 sub re {
184 my $self = shift; # single instance in enough...
185 local *line = shift;
186 undef $ret;
187
188 if ($line =~ /^\$([^,]+)/) {
189 $self->{value} = $1;
190 $ret = $self;
191 $line = substr($line,@+[0]); $line =~ s/^\s+//;
192 }
193 $ret;
194 }
195 sub out {
196 my $self = shift;
197
198 if ($gas) {
199 # Solaris /usr/ccs/bin/as can't handle multiplications
200 # in $self->{value}
201 $self->{value} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
202 $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
203 sprintf "\$%s",$self->{value};
204 } else {
205 $self->{value} =~ s/(0b[0-1]+)/oct($1)/eig;
206 $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
207 sprintf "%s",$self->{value};
208 }
209 }
210}
211{ package ea; # pick up effective addresses: expr(%reg,%reg,scale)
212 sub re {
213 my $self = shift; # single instance in enough...
214 local *line = shift;
215 undef $ret;
216
217 # optional * ---vvv--- appears in indirect jmp/call
218 if ($line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)/ &&
219 !($line =~ /^PIC_(GOT|PLT)/)) {
220 $self->{asterisk} = $1;
221 $self->{label} = $2;
222 ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
223 $self->{scale} = 1 if (!defined($self->{scale}));
224 $ret = $self;
225 $line = substr($line,@+[0]); $line =~ s/^\s+//;
226
227 if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
228 die if (opcode->mnemonic() ne "mov");
229 opcode->mnemonic("lea");
230 }
231 $self->{base} =~ s/^%//;
232 $self->{index} =~ s/^%// if (defined($self->{index}));
233 }
234 $ret;
235 }
236 sub size {}
237 sub out {
238 my $self = shift;
239 my $sz = shift;
240
241 $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
242 $self->{label} =~ s/\.L/$decor/g;
243
244 # Silently convert all EAs to 64-bit. This is required for
245 # elder GNU assembler and results in more compact code,
246 # *but* most importantly AES module depends on this feature!
247 $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
248 $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
249
250 if ($gas) {
251 # Solaris /usr/ccs/bin/as can't handle multiplications
252 # in $self->{label}, new gas requires sign extension...
253 use integer;
254 $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
255 $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
256 $self->{label} =~ s/([0-9]+)/$1<<32>>32/eg;
257 $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64");
258
259 if (defined($self->{index})) {
260 sprintf "%s%s(%%%s,%%%s,%d)",$self->{asterisk},
261 $self->{label},$self->{base},
262 $self->{index},$self->{scale};
263 } else {
264 sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base};
265 }
266 } else {
267 %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR", q=>"QWORD$PTR" );
268
269 $self->{label} =~ s/\./\$/g;
270 $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
271 $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
272 $sz="q" if ($self->{asterisk});
273
274 if (defined($self->{index})) {
275 sprintf "%s[%s%s*%d+%s]",$szmap{$sz},
276 $self->{label}?"$self->{label}+":"",
277 $self->{index},$self->{scale},
278 $self->{base};
279 } elsif ($self->{base} eq "rip") {
280 sprintf "%s[%s]",$szmap{$sz},$self->{label};
281 } else {
282 sprintf "%s[%s%s]",$szmap{$sz},
283 $self->{label}?"$self->{label}+":"",
284 $self->{base};
285 }
286 }
287 }
288}
289{ package register; # pick up registers, which start with %.
290 sub re {
291 my $class = shift; # muliple instances...
292 my $self = {};
293 local *line = shift;
294 undef $ret;
295
296 # optional * ---vvv--- appears in indirect jmp/call
297 if ($line =~ /^(\*?)%(\w+)/) {
298 bless $self,$class;
299 $self->{asterisk} = $1;
300 $self->{value} = $2;
301 $ret = $self;
302 $line = substr($line,@+[0]); $line =~ s/^\s+//;
303 }
304 $ret;
305 }
306 sub size {
307 my $self = shift;
308 undef $ret;
309
310 if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
311 elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
312 elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
313 elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
314 elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
315 elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
316 elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
317 elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
318
319 $ret;
320 }
321 sub out {
322 my $self = shift;
323 if ($gas) { sprintf "%s%%%s",$self->{asterisk},$self->{value}; }
324 else { $self->{value}; }
325 }
326}
327{ package label; # pick up labels, which end with :
328 sub re {
329 my $self = shift; # single instance is enough...
330 local *line = shift;
331 undef $ret;
332
333 if ($line =~ /(^[\.\w]+)\:/) {
334 $self->{value} = $1;
335 $ret = $self;
336 $line = substr($line,@+[0]); $line =~ s/^\s+//;
337
338 $self->{value} =~ s/^\.L/$decor/;
339 }
340 $ret;
341 }
342 sub out {
343 my $self = shift;
344
345 if ($gas) {
346 my $func = ($globals{$self->{value}} or $self->{value}) . ":";
347 if ($win64 &&
348 $current_function->{name} eq $self->{value} &&
349 $current_function->{abi} eq "svr4") {
350 $func .= "\n";
351 $func .= " movq %rdi,8(%rsp)\n";
352 $func .= " movq %rsi,16(%rsp)\n";
353 $func .= " movq %rsp,%rax\n";
354 $func .= "${decor}SEH_begin_$current_function->{name}:\n";
355 my $narg = $current_function->{narg};
356 $narg=6 if (!defined($narg));
357 $func .= " movq %rcx,%rdi\n" if ($narg>0);
358 $func .= " movq %rdx,%rsi\n" if ($narg>1);
359 $func .= " movq %r8,%rdx\n" if ($narg>2);
360 $func .= " movq %r9,%rcx\n" if ($narg>3);
361 $func .= " movq 40(%rsp),%r8\n" if ($narg>4);
362 $func .= " movq 48(%rsp),%r9\n" if ($narg>5);
363 }
364 $func;
365 } elsif ($self->{value} ne "$current_function->{name}") {
366 $self->{value} .= ":" if ($masm && $ret!~m/^\$/);
367 $self->{value} . ":";
368 } elsif ($win64 && $current_function->{abi} eq "svr4") {
369 my $func = "$current_function->{name}" .
370 ($nasm ? ":" : "\tPROC $current_function->{scope}") .
371 "\n";
372 $func .= " mov QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n";
373 $func .= " mov QWORD${PTR}[16+rsp],rsi\n";
374 $func .= " mov rax,rsp\n";
375 $func .= "${decor}SEH_begin_$current_function->{name}:";
376 $func .= ":" if ($masm);
377 $func .= "\n";
378 my $narg = $current_function->{narg};
379 $narg=6 if (!defined($narg));
380 $func .= " mov rdi,rcx\n" if ($narg>0);
381 $func .= " mov rsi,rdx\n" if ($narg>1);
382 $func .= " mov rdx,r8\n" if ($narg>2);
383 $func .= " mov rcx,r9\n" if ($narg>3);
384 $func .= " mov r8,QWORD${PTR}[40+rsp]\n" if ($narg>4);
385 $func .= " mov r9,QWORD${PTR}[48+rsp]\n" if ($narg>5);
386 $func .= "\n";
387 } else {
388 "$current_function->{name}".
389 ($nasm ? ":" : "\tPROC $current_function->{scope}");
390 }
391 }
392}
393{ package expr; # pick up expressioins
394 sub re {
395 my $self = shift; # single instance is enough...
396 local *line = shift;
397 undef $ret;
398
399 if ($line =~ /(^[^,]+)/) {
400 $self->{value} = $1;
401 $ret = $self;
402 $line = substr($line,@+[0]); $line =~ s/^\s+//;
403
404 $self->{value} =~ s/\@PLT// if (!$elf);
405 $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
406 $self->{value} =~ s/\.L/$decor/g;
407 }
408 $ret;
409 }
410 sub out {
411 my $self = shift;
412 if ($nasm && opcode->mnemonic()=~m/^j/) {
413 "NEAR ".$self->{value};
414 } else {
415 $self->{value};
416 }
417 }
418}
419{ package directive; # pick up directives, which start with .
420 sub re {
421 my $self = shift; # single instance is enough...
422 local *line = shift;
423 undef $ret;
424 my $dir;
425 my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
426 ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
427 "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
428 "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
429 "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
430 "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
431 "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
432 "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
433 "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c );
434
435 if ($line =~ /^\s*(\.\w+)/) {
436 $dir = $1;
437 $ret = $self;
438 undef $self->{value};
439 $line = substr($line,@+[0]); $line =~ s/^\s+//;
440
441 SWITCH: for ($dir) {
442 /\.picmeup/ && do { if ($line =~ /(%r[\w]+)/i) {
443 $dir="\t.long";
444 $line=sprintf "0x%x,0x90000000",$opcode{$1};
445 }
446 last;
447 };
448 /\.global|\.globl|\.extern/
449 && do { $globals{$line} = $prefix . $line;
450 $line = $globals{$line} if ($prefix);
451 last;
452 };
453 /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
454 if ($type eq "\@function") {
455 undef $current_function;
456 $current_function->{name} = $sym;
457 $current_function->{abi} = "svr4";
458 $current_function->{narg} = $narg;
459 $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
460 } elsif ($type eq "\@abi-omnipotent") {
461 undef $current_function;
462 $current_function->{name} = $sym;
463 $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
464 }
465 $line =~ s/\@abi\-omnipotent/\@function/;
466 $line =~ s/\@function.*/\@function/;
467 last;
468 };
469 /\.asciz/ && do { if ($line =~ /^"(.*)"$/) {
470 $dir = ".byte";
471 $line = join(",",unpack("C*",$1),0);
472 }
473 last;
474 };
475 /\.rva|\.long|\.quad/
476 && do { $line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
477 $line =~ s/\.L/$decor/g;
478 last;
479 };
480 }
481
482 if ($gas) {
483 $self->{value} = $dir . "\t" . $line;
484
485 if ($dir =~ /\.extern/) {
486 $self->{value} = ""; # swallow extern
487 } elsif (!$elf && $dir =~ /\.type/) {
488 $self->{value} = "";
489 $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
490 (defined($globals{$1})?".scl 2;":".scl 3;") .
491 "\t.type 32;\t.endef"
492 if ($win64 && $line =~ /([^,]+),\@function/);
493 } elsif (!$elf && $dir =~ /\.size/) {
494 $self->{value} = "";
495 if (defined($current_function)) {
496 $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
497 if ($win64 && $current_function->{abi} eq "svr4");
498 undef $current_function;
499 }
500 } elsif (!$elf && $dir =~ /\.align/) {
501 $self->{value} = ".p2align\t" . (log($line)/log(2));
502 } elsif ($dir eq ".section") {
503 $current_segment=$line;
504 if (!$elf && $current_segment eq ".init") {
505 if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
506 elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
507 }
508 } elsif ($dir =~ /\.(text|data)/) {
509 $current_segment=".$1";
510 }
511 $line = "";
512 return $self;
513 }
514
515 # non-gas case or nasm/masm
516 SWITCH: for ($dir) {
517 /\.text/ && do { my $v=undef;
518 if ($nasm) {
519 $v="section .text code align=64\n";
520 } else {
521 $v="$current_segment\tENDS\n" if ($current_segment);
522 $current_segment = ".text\$";
523 $v.="$current_segment\tSEGMENT ";
524 $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE";
525 $v.=" 'CODE'";
526 }
527 $self->{value} = $v;
528 last;
529 };
530 /\.data/ && do { my $v=undef;
531 if ($nasm) {
532 $v="section .data data align=8\n";
533 } else {
534 $v="$current_segment\tENDS\n" if ($current_segment);
535 $current_segment = "_DATA";
536 $v.="$current_segment\tSEGMENT";
537 }
538 $self->{value} = $v;
539 last;
540 };
541 /\.section/ && do { my $v=undef;
542 $line =~ s/([^,]*).*/$1/;
543 $line = ".CRT\$XCU" if ($line eq ".init");
544 if ($nasm) {
545 $v="section $line";
546 if ($line=~/\.([px])data/) {
547 $v.=" rdata align=";
548 $v.=$1 eq "p"? 4 : 8;
549 }
550 } else {
551 $v="$current_segment\tENDS\n" if ($current_segment);
552 $v.="$line\tSEGMENT";
553 if ($line=~/\.([px])data/) {
554 $v.=" READONLY";
555 $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
556 }
557 }
558 $current_segment = $line;
559 $self->{value} = $v;
560 last;
561 };
562 /\.extern/ && do { $self->{value} = "EXTERN\t".$line;
563 $self->{value} .= ":NEAR" if ($masm);
564 last;
565 };
566 /\.globl|.global/
567 && do { $self->{value} = $masm?"PUBLIC":"global";
568 $self->{value} .= "\t".$line;
569 last;
570 };
571 /\.size/ && do { if (defined($current_function)) {
572 undef $self->{value};
573 if ($current_function->{abi} eq "svr4") {
574 $self->{value}="${decor}SEH_end_$current_function->{name}:";
575 $self->{value}.=":\n" if($masm);
576 }
577 $self->{value}.="$current_function->{name}\tENDP" if($masm);
578 undef $current_function;
579 }
580 last;
581 };
582 /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
583 /\.(value|long|rva|quad)/
584 && do { my $sz = substr($1,0,1);
585 my @arr = split(/,\s*/,$line);
586 my $last = pop(@arr);
587 my $conv = sub { my $var=shift;
588 $var=~s/^(0b[0-1]+)/oct($1)/eig;
589 $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
590 if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
591 { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
592 $var;
593 };
594
595 $sz =~ tr/bvlrq/BWDDQ/;
596 $self->{value} = "\tD$sz\t";
597 for (@arr) { $self->{value} .= &$conv($_).","; }
598 $self->{value} .= &$conv($last);
599 last;
600 };
601 /\.byte/ && do { my @str=split(/,\s*/,$line);
602 map(s/(0b[0-1]+)/oct($1)/eig,@str);
603 map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
604 while ($#str>15) {
605 $self->{value}.="DB\t"
606 .join(",",@str[0..15])."\n";
607 foreach (0..15) { shift @str; }
608 }
609 $self->{value}.="DB\t"
610 .join(",",@str) if (@str);
611 last;
612 };
613 }
614 $line = "";
615 }
616
617 $ret;
618 }
619 sub out {
620 my $self = shift;
621 $self->{value};
622 }
623}
624
625print "#include <machine/asm.h>\n";
626
627if ($nasm) {
628 print <<___;
629default rel
630___
631} elsif ($masm) {
632 print <<___;
633OPTION DOTNAME
634___
635}
636while($line=<>) {
637
638 chomp($line);
639
640 $line =~ s|[#!].*$||; # get rid of asm-style comments...
641 $line =~ s|/\*.*\*/||; # ... and C-style comments...
642 $line =~ s|^\s+||; # ... and skip white spaces in beginning
643
644 undef $label;
645 undef $opcode;
646 undef $sz;
647 undef @args;
648
649 if ($label=label->re(\$line)) { print $label->out(); }
650
651 if (directive->re(\$line)) {
652 printf "%s",directive->out();
653 } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: while (1) {
654 my $arg;
655
656 if ($arg=register->re(\$line)) { opcode->size($arg->size()); }
657 elsif ($arg=const->re(\$line)) { }
658 elsif ($arg=ea->re(\$line)) { }
659 elsif ($arg=expr->re(\$line)) { }
660 else { last ARGUMENT; }
661
662 push @args,$arg;
663
664 last ARGUMENT if ($line !~ /^,/);
665
666 $line =~ s/^,\s*//;
667 } # ARGUMENT:
668
669 $sz=opcode->size();
670
671 if ($#args>=0) {
672 my $insn;
673 if ($gas) {
674 $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
675 } else {
676 $insn = $opcode->out();
677 $insn .= $sz if (map($_->out() =~ /x?mm/,@args));
678 @args = reverse(@args);
679 undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
680 }
681 printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
682 } else {
683 printf "\t%s",$opcode->out();
684 }
685 }
686
687 print $line,"\n";
688}
689
690print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
691print "END\n" if ($masm);
692
693close STDOUT;
694
695 #################################################
696# Cross-reference x86_64 ABI "card"
697#
698# Unix Win64
699# %rax * *
700# %rbx - -
701# %rcx #4 #1
702# %rdx #3 #2
703# %rsi #2 -
704# %rdi #1 -
705# %rbp - -
706# %rsp - -
707# %r8 #5 #3
708# %r9 #6 #4
709# %r10 * *
710# %r11 * *
711# %r12 - -
712# %r13 - -
713# %r14 - -
714# %r15 - -
715#
716# (*) volatile register
717# (-) preserved by callee
718# (#) Nth argument, volatile
719#
720# In Unix terms top of stack is argument transfer area for arguments
721# which could not be accomodated in registers. Or in other words 7th
722# [integer] argument resides at 8(%rsp) upon function entry point.
723# 128 bytes above %rsp constitute a "red zone" which is not touched
724# by signal handlers and can be used as temporal storage without
725# allocating a frame.
726#
727# In Win64 terms N*8 bytes on top of stack is argument transfer area,
728# which belongs to/can be overwritten by callee. N is the number of
729# arguments passed to callee, *but* not less than 4! This means that
730# upon function entry point 5th argument resides at 40(%rsp), as well
731# as that 32 bytes from 8(%rsp) can always be used as temporal
732# storage [without allocating a frame]. One can actually argue that
733# one can assume a "red zone" above stack pointer under Win64 as well.
734# Point is that at apparently no occasion Windows kernel would alter
735# the area above user stack pointer in true asynchronous manner...
736#
737# All the above means that if assembler programmer adheres to Unix
738# register and stack layout, but disregards the "red zone" existense,
739# it's possible to use following prologue and epilogue to "gear" from
740# Unix to Win64 ABI in leaf functions with not more than 6 arguments.
741#
742# omnipotent_function:
743# ifdef WIN64
744# movq %rdi,8(%rsp)
745# movq %rsi,16(%rsp)
746# movq %rcx,%rdi ; if 1st argument is actually present
747# movq %rdx,%rsi ; if 2nd argument is actually ...
748# movq %r8,%rdx ; if 3rd argument is ...
749# movq %r9,%rcx ; if 4th argument ...
750# movq 40(%rsp),%r8 ; if 5th ...
751# movq 48(%rsp),%r9 ; if 6th ...
752# endif
753# ...
754# ifdef WIN64
755# movq 8(%rsp),%rdi
756# movq 16(%rsp),%rsi
757# endif
758# ret
759#
760 #################################################
761# Win64 SEH, Structured Exception Handling.
762#
763# Unlike on Unix systems(*) lack of Win64 stack unwinding information
764# has undesired side-effect at run-time: if an exception is raised in
765# assembler subroutine such as those in question (basically we're
766# referring to segmentation violations caused by malformed input
767# parameters), the application is briskly terminated without invoking
768# any exception handlers, most notably without generating memory dump
769# or any user notification whatsoever. This poses a problem. It's
770# possible to address it by registering custom language-specific
771# handler that would restore processor context to the state at
772# subroutine entry point and return "exception is not handled, keep
773# unwinding" code. Writing such handler can be a challenge... But it's
774# doable, though requires certain coding convention. Consider following
775# snippet:
776#
777# .type function,@function
778# function:
779# movq %rsp,%rax # copy rsp to volatile register
780# pushq %r15 # save non-volatile registers
781# pushq %rbx
782# pushq %rbp
783# movq %rsp,%r11
784# subq %rdi,%r11 # prepare [variable] stack frame
785# andq $-64,%r11
786# movq %rax,0(%r11) # check for exceptions
787# movq %r11,%rsp # allocate [variable] stack frame
788# movq %rax,0(%rsp) # save original rsp value
789# magic_point:
790# ...
791# movq 0(%rsp),%rcx # pull original rsp value
792# movq -24(%rcx),%rbp # restore non-volatile registers
793# movq -16(%rcx),%rbx
794# movq -8(%rcx),%r15
795# movq %rcx,%rsp # restore original rsp
796# ret
797# .size function,.-function
798#
799# The key is that up to magic_point copy of original rsp value remains
800# in chosen volatile register and no non-volatile register, except for
801# rsp, is modified. While past magic_point rsp remains constant till
802# the very end of the function. In this case custom language-specific
803# exception handler would look like this:
804#
805# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
806# CONTEXT *context,DISPATCHER_CONTEXT *disp)
807# { ULONG64 *rsp = (ULONG64 *)context->Rax;
808# if (context->Rip >= magic_point)
809# { rsp = ((ULONG64 **)context->Rsp)[0];
810# context->Rbp = rsp[-3];
811# context->Rbx = rsp[-2];
812# context->R15 = rsp[-1];
813# }
814# context->Rsp = (ULONG64)rsp;
815# context->Rdi = rsp[1];
816# context->Rsi = rsp[2];
817#
818# memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
819# RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
820# dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
821# &disp->HandlerData,&disp->EstablisherFrame,NULL);
822# return ExceptionContinueSearch;
823# }
824#
825# It's appropriate to implement this handler in assembler, directly in
826# function's module. In order to do that one has to know members'
827# offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
828# values. Here they are:
829#
830# CONTEXT.Rax 120
831# CONTEXT.Rcx 128
832# CONTEXT.Rdx 136
833# CONTEXT.Rbx 144
834# CONTEXT.Rsp 152
835# CONTEXT.Rbp 160
836# CONTEXT.Rsi 168
837# CONTEXT.Rdi 176
838# CONTEXT.R8 184
839# CONTEXT.R9 192
840# CONTEXT.R10 200
841# CONTEXT.R11 208
842# CONTEXT.R12 216
843# CONTEXT.R13 224
844# CONTEXT.R14 232
845# CONTEXT.R15 240
846# CONTEXT.Rip 248
847# CONTEXT.Xmm6 512
848# sizeof(CONTEXT) 1232
849# DISPATCHER_CONTEXT.ControlPc 0
850# DISPATCHER_CONTEXT.ImageBase 8
851# DISPATCHER_CONTEXT.FunctionEntry 16
852# DISPATCHER_CONTEXT.EstablisherFrame 24
853# DISPATCHER_CONTEXT.TargetIp 32
854# DISPATCHER_CONTEXT.ContextRecord 40
855# DISPATCHER_CONTEXT.LanguageHandler 48
856# DISPATCHER_CONTEXT.HandlerData 56
857# UNW_FLAG_NHANDLER 0
858# ExceptionContinueSearch 1
859#
860# In order to tie the handler to the function one has to compose
861# couple of structures: one for .xdata segment and one for .pdata.
862#
863# UNWIND_INFO structure for .xdata segment would be
864#
865# function_unwind_info:
866# .byte 9,0,0,0
867# .rva handler
868#
869# This structure designates exception handler for a function with
870# zero-length prologue, no stack frame or frame register.
871#
872# To facilitate composing of .pdata structures, auto-generated "gear"
873# prologue copies rsp value to rax and denotes next instruction with
874# .LSEH_begin_{function_name} label. This essentially defines the SEH
875# styling rule mentioned in the beginning. Position of this label is
876# chosen in such manner that possible exceptions raised in the "gear"
877# prologue would be accounted to caller and unwound from latter's frame.
878# End of function is marked with respective .LSEH_end_{function_name}
879# label. To summarize, .pdata segment would contain
880#
881# .rva .LSEH_begin_function
882# .rva .LSEH_end_function
883# .rva function_unwind_info
884#
885# Reference to functon_unwind_info from .xdata segment is the anchor.
886# In case you wonder why references are 32-bit .rvas and not 64-bit
887# .quads. References put into these two segments are required to be
888# *relative* to the base address of the current binary module, a.k.a.
889# image base. No Win64 module, be it .exe or .dll, can be larger than
890# 2GB and thus such relative references can be and are accommodated in
891# 32 bits.
892#
893# Having reviewed the example function code, one can argue that "movq
894# %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
895# rax would contain an undefined value. If this "offends" you, use
896# another register and refrain from modifying rax till magic_point is
897# reached, i.e. as if it was a non-volatile register. If more registers
898# are required prior [variable] frame setup is completed, note that
899# nobody says that you can have only one "magic point." You can
900# "liberate" non-volatile registers by denoting last stack off-load
901# instruction and reflecting it in finer grade unwind logic in handler.
902# After all, isn't it why it's called *language-specific* handler...
903#
904# Attentive reader can notice that exceptions would be mishandled in
905# auto-generated "gear" epilogue. Well, exception effectively can't
906# occur there, because if memory area used by it was subject to
907# segmentation violation, then it would be raised upon call to the
908# function (and as already mentioned be accounted to caller, which is
909# not a problem). If you're still not comfortable, then define tail
910# "magic point" just prior ret instruction and have handler treat it...
911#
912# (*) Note that we're talking about run-time, not debug-time. Lack of
913# unwind information makes debugging hard on both Windows and
914# Unix. "Unlike" referes to the fact that on Unix signal handler
915# will always be invoked, core dumped and appropriate exit code
916# returned to parent (for user notification).
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl
new file mode 100644
index 0000000000..4756a28e59
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86asm.pl
@@ -0,0 +1,221 @@
1#!/usr/bin/env perl
2
3# require 'x86asm.pl';
4# &asm_init(<flavor>,"des-586.pl"[,$i386only]);
5# &function_begin("foo");
6# ...
7# &function_end("foo");
8# &asm_finish
9
10$out=();
11$i386=0;
12
13# AUTOLOAD is this context has quite unpleasant side effect, namely
14# that typos in function calls effectively go to assembler output,
15# but on the pros side we don't have to implement one subroutine per
16# each opcode...
17sub ::AUTOLOAD
18{ my $opcode = $AUTOLOAD;
19
20 die "more than 4 arguments passed to $opcode" if ($#_>3);
21
22 $opcode =~ s/.*:://;
23 if ($opcode =~ /^push/) { $stack+=4; }
24 elsif ($opcode =~ /^pop/) { $stack-=4; }
25
26 &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
27}
28
29sub ::emit
30{ my $opcode=shift;
31
32 if ($#_==-1) { push(@out,"\t$opcode\n"); }
33 else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
34}
35
36sub ::emitraw
37{ my $opcode=shift;
38
39 if ($#_==-1) { push(@out,"$opcode\n"); }
40 else { push(@out,"$opcode\t".join(',',@_)."\n"); }
41}
42
43sub ::LB
44{ $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
45 $1."l";
46}
47sub ::HB
48{ $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
49 $1."h";
50}
51sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); }
52sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); }
53sub ::blindpop { &pop($_[0]); $stack+=4; }
54sub ::wparam { &DWP($stack+4*$_[0],"esp"); }
55sub ::swtmp { &DWP(4*$_[0],"esp"); }
56
57sub ::bswap
58{ if ($i386) # emulate bswap for i386
59 { &comment("bswap @_");
60 &xchg(&HB(@_),&LB(@_));
61 &ror (@_,16);
62 &xchg(&HB(@_),&LB(@_));
63 }
64 else
65 { &generic("bswap",@_); }
66}
67# These are made-up opcodes introduced over the years essentially
68# by ignorance, just alias them to real ones...
69sub ::movb { &mov(@_); }
70sub ::xorb { &xor(@_); }
71sub ::rotl { &rol(@_); }
72sub ::rotr { &ror(@_); }
73sub ::exch { &xchg(@_); }
74sub ::halt { &hlt; }
75sub ::movz { &movzx(@_); }
76sub ::pushf { &pushfd; }
77sub ::popf { &popfd; }
78
79# 3 argument instructions
80sub ::movq
81{ my($p1,$p2,$optimize)=@_;
82
83 if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
84 # movq between mmx registers can sink Intel CPUs
85 { &::pshufw($p1,$p2,0xe4); }
86 else
87 { &::generic("movq",@_); }
88}
89
90# label management
91$lbdecor="L"; # local label decoration, set by package
92$label="000";
93
94sub ::islabel # see is argument is a known label
95{ my $i;
96 foreach $i (values %label) { return $i if ($i eq $_[0]); }
97 $label{$_[0]}; # can be undef
98}
99
100sub ::label # instantiate a function-scope label
101{ if (!defined($label{$_[0]}))
102 { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
103 $label{$_[0]};
104}
105
106sub ::LABEL # instantiate a file-scope label
107{ $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
108 $label{$_[0]};
109}
110
111sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
112
113sub ::set_label_B { push(@out,"@_:\n"); }
114sub ::set_label
115{ my $label=&::label($_[0]);
116 &::align($_[1]) if ($_[1]>1);
117 &::set_label_B($label);
118 $label;
119}
120
121sub ::wipe_labels # wipes function-scope labels
122{ foreach $i (keys %label)
123 { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
124}
125
126# subroutine management
127sub ::function_begin
128{ &function_begin_B(@_);
129 $stack=4;
130 &push("ebp");
131 &push("ebx");
132 &push("esi");
133 &push("edi");
134}
135
136sub ::function_end
137{ &pop("edi");
138 &pop("esi");
139 &pop("ebx");
140 &pop("ebp");
141 &ret();
142 &function_end_B(@_);
143 $stack=0;
144 &wipe_labels();
145}
146
147sub ::function_end_A
148{ &pop("edi");
149 &pop("esi");
150 &pop("ebx");
151 &pop("ebp");
152 &ret();
153 $stack+=16; # readjust esp as if we didn't pop anything
154}
155
156sub ::asciz
157{ my @str=unpack("C*",shift);
158 push @str,0;
159 while ($#str>15) {
160 &data_byte(@str[0..15]);
161 foreach (0..15) { shift @str; }
162 }
163 &data_byte(@str) if (@str);
164}
165
166sub ::asm_finish
167{ &file_end();
168 print @out;
169}
170
171sub ::asm_init
172{ my ($type,$fn,$cpu)=@_;
173
174 $filename=$fn;
175 $i386=$cpu;
176
177 $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$openbsd=0;
178 if (($type eq "elf"))
179 { $elf=1; require "x86gas.pl"; }
180 elsif (($type eq "a\.out"))
181 { $aout=1; require "x86gas.pl"; }
182 elsif (($type eq "coff" or $type eq "gaswin"))
183 { $coff=1; require "x86gas.pl"; }
184 elsif (($type eq "win32n"))
185 { $win32=1; require "x86nasm.pl"; }
186 elsif (($type eq "nw-nasm"))
187 { $netware=1; require "x86nasm.pl"; }
188 #elsif (($type eq "nw-mwasm"))
189 #{ $netware=1; $mwerks=1; require "x86nasm.pl"; }
190 elsif (($type eq "win32"))
191 { $win32=1; require "x86masm.pl"; }
192 elsif (($type eq "macosx"))
193 { $aout=1; $macosx=1; require "x86gas.pl"; }
194 elsif (($type eq "openbsd-elf"))
195 { $openbsd=$elf=1; require "x86gas.pl"; }
196 elsif (($type eq "openbsd-a.out"))
197 { $openbsd=1; require "x86gas.pl"; }
198 else
199 { print STDERR <<"EOF";
200Pick one target type from
201 elf - Linux, FreeBSD, Solaris x86, etc.
202 a.out - DJGPP, elder OpenBSD, etc.
203 coff - GAS/COFF such as Win32 targets
204 win32n - Windows 95/Windows NT NASM format
205 openbsd-elf - OpenBSD elf
206 openbsd-a.out - OpenBSD a.out
207 nw-nasm - NetWare NASM format
208 macosx - Mac OS X
209EOF
210 exit(1);
211 }
212
213 $pic=0;
214 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
215
216 ::emitraw("#include <machine/asm.h>\n") if $openbsd;
217 $filename =~ s/\.pl$//;
218 &file($filename);
219}
220
2211;
diff --git a/src/lib/libcrypto/perlasm/x86gas.pl b/src/lib/libcrypto/perlasm/x86gas.pl
new file mode 100644
index 0000000000..15e17f25d0
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86gas.pl
@@ -0,0 +1,267 @@
1#!/usr/bin/env perl
2
3package x86gas;
4
5*out=\@::out;
6
7$::lbdecor=$::aout?"L":".L"; # local label decoration
8$nmdecor=($::aout or $::coff)?"_":""; # external name decoration
9
10$initseg="";
11
12$align=16;
13$align=log($align)/log(2) if ($::aout);
14$com_start="#" if ($::aout or $::coff);
15
16sub opsize()
17{ my $reg=shift;
18 if ($reg =~ m/^%e/o) { "l"; }
19 elsif ($reg =~ m/^%[a-d][hl]$/o) { "b"; }
20 elsif ($reg =~ m/^%[xm]/o) { undef; }
21 else { "w"; }
22}
23
24# swap arguments;
25# expand opcode with size suffix;
26# prefix numeric constants with $;
27sub ::generic
28{ my($opcode,@arg)=@_;
29 my($suffix,$dst,$src);
30
31 @arg=reverse(@arg);
32
33 for (@arg)
34 { s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o; # gp registers
35 s/^([xy]?mm[0-7])$/%$1/o; # xmm/mmx registers
36 s/^(\-?[0-9]+)$/\$$1/o; # constants
37 s/^(\-?0x[0-9a-f]+)$/\$$1/o; # constants
38 }
39
40 $dst = $arg[$#arg] if ($#arg>=0);
41 $src = $arg[$#arg-1] if ($#arg>=1);
42 if ($dst =~ m/^%/o) { $suffix=&opsize($dst); }
43 elsif ($src =~ m/^%/o) { $suffix=&opsize($src); }
44 else { $suffix="l"; }
45 undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o);
46
47 if ($#_==0) { &::emit($opcode); }
48 elsif ($opcode =~ m/^j/o && $#_==1) { &::emit($opcode,@arg); }
49 elsif ($opcode eq "call" && $#_==1) { &::emit($opcode,@arg); }
50 elsif ($opcode =~ m/^set/&& $#_==1) { &::emit($opcode,@arg); }
51 else { &::emit($opcode.$suffix,@arg);}
52
53 1;
54}
55#
56# opcodes not covered by ::generic above, mostly inconsistent namings...
57#
58sub ::movzx { &::movzb(@_); }
59sub ::pushfd { &::pushfl; }
60sub ::popfd { &::popfl; }
61sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); }
62sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); }
63
64sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
65sub ::call_ptr { &::generic("call","*$_[0]"); }
66sub ::jmp_ptr { &::generic("jmp","*$_[0]"); }
67
68*::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386);
69
70sub ::DWP
71{ my($addr,$reg1,$reg2,$idx)=@_;
72 my $ret="";
73
74 $addr =~ s/^\s+//;
75 # prepend global references with optional underscore
76 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
77
78 $reg1 = "%$reg1" if ($reg1);
79 $reg2 = "%$reg2" if ($reg2);
80
81 $ret .= $addr if (($addr ne "") && ($addr ne 0));
82
83 if ($reg2)
84 { $idx!= 0 or $idx=1;
85 $ret .= "($reg1,$reg2,$idx)";
86 }
87 elsif ($reg1)
88 { $ret .= "($reg1)"; }
89
90 $ret;
91}
92sub ::QWP { &::DWP(@_); }
93sub ::BP { &::DWP(@_); }
94sub ::BC { @_; }
95sub ::DWC { @_; }
96
97sub ::file
98{ push(@out,".file\t\"$_[0].s\"\n.text\n"); }
99
100sub ::function_begin_B
101{ my $func=shift;
102 my $global=($func !~ /^_/);
103 my $begin="${::lbdecor}_${func}_begin";
104
105 &::LABEL($func,$global?"$begin":"$nmdecor$func");
106 $func=$nmdecor.$func;
107
108 push(@out,".globl\t$func\n") if ($global);
109 if ($::coff)
110 { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
111 elsif (($::aout and !$::pic) or $::macosx)
112 { }
113 else
114 { push(@out,".type $func,\@function\n"); }
115 push(@out,".align\t$align\n");
116 push(@out,"$func:\n");
117 push(@out,"$begin:\n") if ($global);
118 $::stack=4;
119}
120
121sub ::function_end_B
122{ my $func=shift;
123 push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
124 $::stack=0;
125 &::wipe_labels();
126}
127
128sub ::comment
129 {
130 if (!defined($com_start) or $::elf)
131 { # Regarding $::elf above...
132 # GNU and SVR4 as'es use different comment delimiters,
133 push(@out,"\n"); # so we just skip ELF comments...
134 return;
135 }
136 foreach (@_)
137 {
138 if (/^\s*$/)
139 { push(@out,"\n"); }
140 else
141 { push(@out,"\t$com_start $_ $com_end\n"); }
142 }
143 }
144
145sub ::external_label
146{ foreach(@_) { &::LABEL($_,$nmdecor.$_); } }
147
148sub ::public_label
149{ push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
150
151sub ::file_end
152{ if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
153 my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,4";
154 if ($::elf) { push (@out,"$tmp,4\n"); }
155 else { push (@out,"$tmp\n"); }
156 }
157 if ($::macosx)
158 { if (%non_lazy_ptr)
159 { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
160 foreach $i (keys %non_lazy_ptr)
161 { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); }
162 }
163 }
164 push(@out,$initseg) if ($initseg);
165}
166
167sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); }
168sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); }
169
170sub ::align
171{ my $val=$_[0],$p2,$i;
172 if ($::aout)
173 { for ($p2=0;$val!=0;$val>>=1) { $p2++; }
174 $val=$p2-1;
175 $val.=",0x90";
176 }
177 push(@out,".align\t$val\n");
178}
179
180sub ::picmeup
181{ my($dst,$sym,$base,$reflabel)=@_;
182
183 if ($::openbsd)
184 { &::emitraw("#ifdef PIC");
185 &::emitraw("PIC_PROLOGUE");
186 &::mov($dst, &::DWP("PIC_GOT($sym)"));
187 &::emitraw("PIC_EPILOGUE");
188 &::emitraw("#else /* PIC */");
189 &::lea($dst,&::DWP($sym));
190 &::emitraw("#endif /* PIC */");
191 }
192 elsif ($::pic && ($::elf || $::aout))
193 { if (!defined($base))
194 { &::call(&::label("PIC_me_up"));
195 &::set_label("PIC_me_up");
196 &::blindpop($dst);
197 $base=$dst;
198 $reflabel=&::label("PIC_me_up");
199 }
200 if ($::macosx)
201 { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
202 &::mov($dst,&::DWP("$indirect-$reflabel",$base));
203 $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
204 }
205 else
206 { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
207 $base));
208 &::mov($dst,&::DWP("$sym\@GOT",$dst));
209 }
210 }
211 else
212 { &::lea($dst,&::DWP($sym)); }
213}
214
215sub ::initseg
216{ my $f=$nmdecor.shift;
217
218 if ($::openbsd)
219 { $initseg.=<<___;
220.section .init
221PIC_PROLOGUE
222 call PIC_PLT($f)
223PIC_EPILOGUE
224 jmp .Linitalign
225.align $align
226.Linitalign:
227___
228 }
229 elsif ($::elf)
230 { $initseg.=<<___;
231.section .init
232 call $f
233 jmp .Linitalign
234.align $align
235.Linitalign:
236___
237 }
238 elsif ($::coff)
239 { $initseg.=<<___; # applies to both Cygwin and Mingw
240.section .ctors
241.long $f
242___
243 }
244 elsif ($::macosx)
245 { $initseg.=<<___;
246.mod_init_func
247.align 2
248.long $f
249___
250 }
251 elsif ($::aout)
252 { my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
253 $initseg.=".text\n";
254 $initseg.=".type $ctor,\@function\n" if ($::pic);
255 $initseg.=<<___; # OpenBSD way...
256.globl $ctor
257.align 2
258$ctor:
259 jmp $f
260___
261 }
262}
263
264sub ::dataseg
265{ push(@out,".data\n"); }
266
2671;
diff --git a/src/lib/libcrypto/perlasm/x86masm.pl b/src/lib/libcrypto/perlasm/x86masm.pl
new file mode 100644
index 0000000000..3d50e4a786
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86masm.pl
@@ -0,0 +1,184 @@
1#!/usr/bin/env perl
2
3package x86masm;
4
5*out=\@::out;
6
7$::lbdecor="\$L"; # local label decoration
8$nmdecor="_"; # external name decoration
9
10$initseg="";
11$segment="";
12
13sub ::generic
14{ my ($opcode,@arg)=@_;
15
16 # fix hexadecimal constants
17 for (@arg) { s/0x([0-9a-f]+)/0$1h/oi; }
18
19 if ($opcode !~ /movq/)
20 { # fix xmm references
21 $arg[0] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[1]=~/\bxmm[0-7]\b/i);
22 $arg[1] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[0]=~/\bxmm[0-7]\b/i);
23 }
24
25 &::emit($opcode,@arg);
26 1;
27}
28#
29# opcodes not covered by ::generic above, mostly inconsistent namings...
30#
31sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
32sub ::call_ptr { &::emit("call",@_); }
33sub ::jmp_ptr { &::emit("jmp",@_); }
34
35sub get_mem
36{ my($size,$addr,$reg1,$reg2,$idx)=@_;
37 my($post,$ret);
38
39 $ret .= "$size PTR " if ($size ne "");
40
41 $addr =~ s/^\s+//;
42 # prepend global references with optional underscore
43 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
44 # put address arithmetic expression in parenthesis
45 $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
46
47 if (($addr ne "") && ($addr ne 0))
48 { if ($addr !~ /^-/) { $ret .= "$addr"; }
49 else { $post=$addr; }
50 }
51 $ret .= "[";
52
53 if ($reg2 ne "")
54 { $idx!=0 or $idx=1;
55 $ret .= "$reg2*$idx";
56 $ret .= "+$reg1" if ($reg1 ne "");
57 }
58 else
59 { $ret .= "$reg1"; }
60
61 $ret .= "$post]";
62 $ret =~ s/\+\]/]/; # in case $addr was the only argument
63 $ret =~ s/\[\s*\]//;
64
65 $ret;
66}
67sub ::BP { &get_mem("BYTE",@_); }
68sub ::DWP { &get_mem("DWORD",@_); }
69sub ::QWP { &get_mem("QWORD",@_); }
70sub ::BC { "@_"; }
71sub ::DWC { "@_"; }
72
73sub ::file
74{ my $tmp=<<___;
75TITLE $_[0].asm
76IF \@Version LT 800
77ECHO MASM version 8.00 or later is strongly recommended.
78ENDIF
79.486
80.MODEL FLAT
81OPTION DOTNAME
82IF \@Version LT 800
83.text\$ SEGMENT PAGE 'CODE'
84ELSE
85.text\$ SEGMENT ALIGN(64) 'CODE'
86ENDIF
87___
88 push(@out,$tmp);
89 $segment = ".text\$";
90}
91
92sub ::function_begin_B
93{ my $func=shift;
94 my $global=($func !~ /^_/);
95 my $begin="${::lbdecor}_${func}_begin";
96
97 &::LABEL($func,$global?"$begin":"$nmdecor$func");
98 $func="ALIGN\t16\n".$nmdecor.$func."\tPROC";
99
100 if ($global) { $func.=" PUBLIC\n${begin}::\n"; }
101 else { $func.=" PRIVATE\n"; }
102 push(@out,$func);
103 $::stack=4;
104}
105sub ::function_end_B
106{ my $func=shift;
107
108 push(@out,"$nmdecor$func ENDP\n");
109 $::stack=0;
110 &::wipe_labels();
111}
112
113sub ::file_end
114{ my $xmmheader=<<___;
115.686
116.XMM
117IF \@Version LT 800
118XMMWORD STRUCT 16
119DQ 2 dup (?)
120XMMWORD ENDS
121ENDIF
122___
123 if (grep {/\b[x]?mm[0-7]\b/i} @out) {
124 grep {s/\.[3-7]86/$xmmheader/} @out;
125 }
126
127 push(@out,"$segment ENDS\n");
128
129 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
130 { my $comm=<<___;
131.bss SEGMENT 'BSS'
132COMM ${nmdecor}OPENSSL_ia32cap_P:DWORD
133.bss ENDS
134___
135 # comment out OPENSSL_ia32cap_P declarations
136 grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
137 push (@out,$comm);
138 }
139 push (@out,$initseg) if ($initseg);
140 push (@out,"END\n");
141}
142
143sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
144
145*::set_label_B = sub
146{ my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); };
147
148sub ::external_label
149{ foreach(@_)
150 { push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n"); }
151}
152
153sub ::public_label
154{ push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
155
156sub ::data_byte
157{ push(@out,("DB\t").join(',',@_)."\n"); }
158
159sub ::data_word
160{ push(@out,("DD\t").join(',',@_)."\n"); }
161
162sub ::align
163{ push(@out,"ALIGN\t$_[0]\n"); }
164
165sub ::picmeup
166{ my($dst,$sym)=@_;
167 &::lea($dst,&::DWP($sym));
168}
169
170sub ::initseg
171{ my $f=$nmdecor.shift;
172
173 $initseg.=<<___;
174.CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
175EXTERN $f:NEAR
176DD $f
177.CRT\$XCU ENDS
178___
179}
180
181sub ::dataseg
182{ push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA"; }
183
1841;
diff --git a/src/lib/libcrypto/perlasm/x86ms.pl b/src/lib/libcrypto/perlasm/x86ms.pl
deleted file mode 100644
index a0be2934c2..0000000000
--- a/src/lib/libcrypto/perlasm/x86ms.pl
+++ /dev/null
@@ -1,472 +0,0 @@
1#!/usr/local/bin/perl
2
3package 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
27sub main'asm_init_output { @out=(); }
28sub main'asm_get_output { return(@out); }
29sub main'get_labels { return(@labels); }
30sub main'external_label
31{
32 push(@labels,@_);
33 foreach (@_) {
34 push(@out, "EXTRN\t_$_:DWORD\n");
35 }
36}
37
38sub main'LB
39 {
40 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
41 return($lb{$_[0]});
42 }
43
44sub main'HB
45 {
46 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
47 return($hb{$_[0]});
48 }
49
50sub main'BP
51 {
52 &get_mem("BYTE",@_);
53 }
54
55sub main'DWP
56 {
57 &get_mem("DWORD",@_);
58 }
59
60sub main'QWP
61 {
62 &get_mem("QWORD",@_);
63 }
64
65sub main'BC
66 {
67 return @_;
68 }
69
70sub main'DWC
71 {
72 return @_;
73 }
74
75sub main'stack_push
76 {
77 local($num)=@_;
78 $stack+=$num*4;
79 &main'sub("esp",$num*4);
80 }
81
82sub main'stack_pop
83 {
84 local($num)=@_;
85 $stack-=$num*4;
86 &main'add("esp",$num*4);
87 }
88
89sub get_mem
90 {
91 local($size,$addr,$reg1,$reg2,$idx)=@_;
92 local($t,$post);
93 local($ret)="$size PTR ";
94
95 $addr =~ s/^\s+//;
96 if ($addr =~ /^(.+)\+(.+)$/)
97 {
98 $reg2=&conv($1);
99 $addr="_$2";
100 }
101 elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i)
102 {
103 $addr="_$addr";
104 }
105
106 if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }
107
108 $reg1="$regs{$reg1}" if defined($regs{$reg1});
109 $reg2="$regs{$reg2}" if defined($regs{$reg2});
110 if (($addr ne "") && ($addr ne 0))
111 {
112 if ($addr !~ /^-/)
113 { $ret.=$addr; }
114 else { $post=$addr; }
115 }
116 if ($reg2 ne "")
117 {
118 $t="";
119 $t="*$idx" if ($idx != 0);
120 $reg1="+".$reg1 if ("$reg1$post" ne "");
121 $ret.="[$reg2$t$reg1$post]";
122 }
123 else
124 {
125 $ret.="[$reg1$post]"
126 }
127 $ret =~ s/\[\]//; # in case $addr was the only argument
128 return($ret);
129 }
130
131sub main'mov { &out2("mov",@_); }
132sub main'movb { &out2("mov",@_); }
133sub main'and { &out2("and",@_); }
134sub main'or { &out2("or",@_); }
135sub main'shl { &out2("shl",@_); }
136sub main'shr { &out2("shr",@_); }
137sub main'xor { &out2("xor",@_); }
138sub main'xorb { &out2("xor",@_); }
139sub main'add { &out2("add",@_); }
140sub main'adc { &out2("adc",@_); }
141sub main'sub { &out2("sub",@_); }
142sub main'sbb { &out2("sbb",@_); }
143sub main'rotl { &out2("rol",@_); }
144sub main'rotr { &out2("ror",@_); }
145sub main'exch { &out2("xchg",@_); }
146sub main'cmp { &out2("cmp",@_); }
147sub main'lea { &out2("lea",@_); }
148sub main'mul { &out1("mul",@_); }
149sub main'imul { &out2("imul",@_); }
150sub main'div { &out1("div",@_); }
151sub main'dec { &out1("dec",@_); }
152sub main'inc { &out1("inc",@_); }
153sub main'jmp { &out1("jmp",@_); }
154sub main'jmp_ptr { &out1p("jmp",@_); }
155sub main'je { &out1("je",@_); }
156sub main'jle { &out1("jle",@_); }
157sub main'jz { &out1("jz",@_); }
158sub main'jge { &out1("jge",@_); }
159sub main'jl { &out1("jl",@_); }
160sub main'ja { &out1("ja",@_); }
161sub main'jae { &out1("jae",@_); }
162sub main'jb { &out1("jb",@_); }
163sub main'jbe { &out1("jbe",@_); }
164sub main'jc { &out1("jc",@_); }
165sub main'jnc { &out1("jnc",@_); }
166sub main'jnz { &out1("jnz",@_); }
167sub main'jne { &out1("jne",@_); }
168sub main'jno { &out1("jno",@_); }
169sub main'push { &out1("push",@_); $stack+=4; }
170sub main'pop { &out1("pop",@_); $stack-=4; }
171sub main'pushf { &out0("pushfd"); $stack+=4; }
172sub main'popf { &out0("popfd"); $stack-=4; }
173sub main'bswap { &out1("bswap",@_); &using486(); }
174sub main'not { &out1("not",@_); }
175sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
176sub main'call_ptr { &out1p("call",@_); }
177sub main'ret { &out0("ret"); }
178sub main'nop { &out0("nop"); }
179sub main'test { &out2("test",@_); }
180sub main'bt { &out2("bt",@_); }
181sub main'leave { &out0("leave"); }
182sub main'cpuid { &out0("DW\t0A20Fh"); }
183sub main'rdtsc { &out0("DW\t0310Fh"); }
184sub main'halt { &out0("hlt"); }
185sub main'movz { &out2("movzx",@_); }
186sub main'neg { &out1("neg",@_); }
187sub main'cld { &out0("cld"); }
188
189# SSE2
190sub main'emms { &out0("emms"); }
191sub main'movd { &out2("movd",@_); }
192sub main'movq { &out2("movq",@_); }
193sub main'movdqu { &out2("movdqu",@_); }
194sub main'movdqa { &out2("movdqa",@_); }
195sub main'movdq2q{ &out2("movdq2q",@_); }
196sub main'movq2dq{ &out2("movq2dq",@_); }
197sub main'paddq { &out2("paddq",@_); }
198sub main'pmuludq{ &out2("pmuludq",@_); }
199sub main'psrlq { &out2("psrlq",@_); }
200sub main'psllq { &out2("psllq",@_); }
201sub main'pxor { &out2("pxor",@_); }
202sub main'por { &out2("por",@_); }
203sub main'pand { &out2("pand",@_); }
204
205sub out2
206 {
207 local($name,$p1,$p2)=@_;
208 local($l,$t,$line);
209
210 $line="\t$name\t";
211 $t=&conv($p1).",";
212 $l=length($t);
213 $line.="$t";
214 $l=4-($l+9)/8;
215 $line.="\t" x $l;
216 $line.=&conv($p2);
217 if ($line=~/\bxmm[0-7]\b/i) { $line=~s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i; }
218 push(@out,$line."\n");
219 }
220
221sub out0
222 {
223 local($name)=@_;
224
225 push(@out,"\t$name\n");
226 }
227
228sub out1
229 {
230 local($name,$p1)=@_;
231 local($l,$t);
232
233 push(@out,"\t$name\t".&conv($p1)."\n");
234 }
235
236sub conv
237 {
238 local($p)=@_;
239
240 $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
241 return $p;
242 }
243
244sub using486
245 {
246 return if $using486;
247 $using486++;
248 grep(s/\.386/\.486/,@out);
249 }
250
251sub main'file
252 {
253 local($file)=@_;
254
255 local($tmp)=<<"EOF";
256 TITLE $file.asm
257 .386
258.model FLAT
259_TEXT\$ SEGMENT PAGE 'CODE'
260
261EOF
262 push(@out,$tmp);
263 }
264
265sub main'function_begin
266 {
267 local($func,$extra)=@_;
268
269 push(@labels,$func);
270
271 local($tmp)=<<"EOF";
272PUBLIC _$func
273$extra
274_$func PROC NEAR
275 push ebp
276 push ebx
277 push esi
278 push edi
279EOF
280 push(@out,$tmp);
281 $stack=20;
282 }
283
284sub main'function_begin_B
285 {
286 local($func,$extra)=@_;
287
288 local($tmp)=<<"EOF";
289PUBLIC _$func
290$extra
291_$func PROC NEAR
292EOF
293 push(@out,$tmp);
294 $stack=4;
295 }
296
297sub main'function_end
298 {
299 local($func)=@_;
300
301 local($tmp)=<<"EOF";
302 pop edi
303 pop esi
304 pop ebx
305 pop ebp
306 ret
307_$func ENDP
308EOF
309 push(@out,$tmp);
310 $stack=0;
311 %label=();
312 }
313
314sub main'function_end_B
315 {
316 local($func)=@_;
317
318 local($tmp)=<<"EOF";
319_$func ENDP
320EOF
321 push(@out,$tmp);
322 $stack=0;
323 %label=();
324 }
325
326sub main'function_end_A
327 {
328 local($func)=@_;
329
330 local($tmp)=<<"EOF";
331 pop edi
332 pop esi
333 pop ebx
334 pop ebp
335 ret
336EOF
337 push(@out,$tmp);
338 }
339
340sub main'file_end
341 {
342 # try to detect if SSE2 or MMX extensions were used...
343 my $xmmheader=<<___;
344.686
345.XMM
346IF \@Version LT 800
347XMMWORD STRUCT 16
348 DQ 2 dup (?)
349XMMWORD ENDS
350ENDIF
351___
352 if (grep {/\b[x]?mm[0-7]\b/i} @out) {
353 grep {s/\.[3-7]86/$xmmheader/} @out;
354 }
355 push(@out,"_TEXT\$ ENDS\n");
356 push(@out,"END\n");
357 }
358
359sub main'wparam
360 {
361 local($num)=@_;
362
363 return(&main'DWP($stack+$num*4,"esp","",0));
364 }
365
366sub main'swtmp
367 {
368 return(&main'DWP($_[0]*4,"esp","",0));
369 }
370
371# Should use swtmp, which is above esp. Linix can trash the stack above esp
372#sub main'wtmp
373# {
374# local($num)=@_;
375#
376# return(&main'DWP(-(($num+1)*4),"esp","",0));
377# }
378
379sub main'comment
380 {
381 foreach (@_)
382 {
383 push(@out,"\t; $_\n");
384 }
385 }
386
387sub main'public_label
388 {
389 $label{$_[0]}="_$_[0]" if (!defined($label{$_[0]}));
390 push(@out,"PUBLIC\t$label{$_[0]}\n");
391 }
392
393sub main'label
394 {
395 if (!defined($label{$_[0]}))
396 {
397 $label{$_[0]}="\$${label}${_[0]}";
398 $label++;
399 }
400 return($label{$_[0]});
401 }
402
403sub main'set_label
404 {
405 if (!defined($label{$_[0]}))
406 {
407 $label{$_[0]}="\$${label}${_[0]}";
408 $label++;
409 }
410 if ($_[1]!=0 && $_[1]>1)
411 {
412 main'align($_[1]);
413 }
414 if((defined $_[2]) && ($_[2] == 1))
415 {
416 push(@out,"$label{$_[0]}::\n");
417 }
418 elsif ($label{$_[0]} !~ /^\$/)
419 {
420 push(@out,"$label{$_[0]}\tLABEL PTR\n");
421 }
422 else
423 {
424 push(@out,"$label{$_[0]}:\n");
425 }
426 }
427
428sub main'data_byte
429 {
430 push(@out,"\tDB\t".join(',',@_)."\n");
431 }
432
433sub main'data_word
434 {
435 push(@out,"\tDD\t".join(',',@_)."\n");
436 }
437
438sub main'align
439 {
440 push(@out,"\tALIGN\t$_[0]\n");
441 }
442
443sub out1p
444 {
445 local($name,$p1)=@_;
446 local($l,$t);
447
448 push(@out,"\t$name\t".&conv($p1)."\n");
449 }
450
451sub main'picmeup
452 {
453 local($dst,$sym)=@_;
454 &main'lea($dst,&main'DWP($sym));
455 }
456
457sub main'blindpop { &out1("pop",@_); }
458
459sub main'initseg
460 {
461 local($f)=@_;
462 local($tmp)=<<___;
463OPTION DOTNAME
464.CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
465EXTRN _$f:NEAR
466DD _$f
467.CRT\$XCU ENDS
468___
469 push(@out,$tmp);
470 }
471
4721;
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl
deleted file mode 100644
index a4c947165e..0000000000
--- a/src/lib/libcrypto/perlasm/x86unix.pl
+++ /dev/null
@@ -1,725 +0,0 @@
1#!/usr/local/bin/perl
2
3package x86unix; # GAS actually...
4
5$label="L000";
6$const="";
7$constl=0;
8
9$align=($main'aout)?"4":"16";
10$under=($main'aout or $main'coff)?"_":"";
11$dot=($main'aout)?"":".";
12$com_start="#" if ($main'aout or $main'coff);
13
14sub main'asm_init_output { @out=(); }
15sub main'asm_get_output { return(@out); }
16sub main'get_labels { return(@labels); }
17sub main'external_label { push(@labels,@_); }
18
19if ($main'cpp)
20 {
21 $align="ALIGN";
22 $under="";
23 $com_start='/*';
24 $com_end='*/';
25 }
26
27%lb=( 'eax', '%al',
28 'ebx', '%bl',
29 'ecx', '%cl',
30 'edx', '%dl',
31 'ax', '%al',
32 'bx', '%bl',
33 'cx', '%cl',
34 'dx', '%dl',
35 );
36
37%hb=( 'eax', '%ah',
38 'ebx', '%bh',
39 'ecx', '%ch',
40 'edx', '%dh',
41 'ax', '%ah',
42 'bx', '%bh',
43 'cx', '%ch',
44 'dx', '%dh',
45 );
46
47%regs=( 'eax', '%eax',
48 'ebx', '%ebx',
49 'ecx', '%ecx',
50 'edx', '%edx',
51 'esi', '%esi',
52 'edi', '%edi',
53 'ebp', '%ebp',
54 'esp', '%esp',
55
56 'mm0', '%mm0',
57 'mm1', '%mm1',
58 'mm2', '%mm2',
59 'mm3', '%mm3',
60 'mm4', '%mm4',
61 'mm5', '%mm5',
62 'mm6', '%mm6',
63 'mm7', '%mm7',
64
65 'xmm0', '%xmm0',
66 'xmm1', '%xmm1',
67 'xmm2', '%xmm2',
68 'xmm3', '%xmm3',
69 'xmm4', '%xmm4',
70 'xmm5', '%xmm5',
71 'xmm6', '%xmm6',
72 'xmm7', '%xmm7',
73 );
74
75%reg_val=(
76 'eax', 0x00,
77 'ebx', 0x03,
78 'ecx', 0x01,
79 'edx', 0x02,
80 'esi', 0x06,
81 'edi', 0x07,
82 'ebp', 0x05,
83 'esp', 0x04,
84 );
85
86sub main'LB
87 {
88 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
89 return($lb{$_[0]});
90 }
91
92sub main'HB
93 {
94 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
95 return($hb{$_[0]});
96 }
97
98sub main'DWP
99 {
100 local($addr,$reg1,$reg2,$idx)=@_;
101
102 $ret="";
103 $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
104 $reg1="$regs{$reg1}" if defined($regs{$reg1});
105 $reg2="$regs{$reg2}" if defined($regs{$reg2});
106 $ret.=$addr if ($addr ne "") && ($addr ne 0);
107 if ($reg2 ne "")
108 {
109 if($idx ne "" && $idx != 0)
110 { $ret.="($reg1,$reg2,$idx)"; }
111 else
112 { $ret.="($reg1,$reg2)"; }
113 }
114 elsif ($reg1 ne "")
115 { $ret.="($reg1)" }
116 return($ret);
117 }
118
119sub main'QWP
120 {
121 return(&main'DWP(@_));
122 }
123
124sub main'BP
125 {
126 return(&main'DWP(@_));
127 }
128
129sub main'BC
130 {
131 return @_;
132 }
133
134sub main'DWC
135 {
136 return @_;
137 }
138
139#sub main'BP
140# {
141# local($addr,$reg1,$reg2,$idx)=@_;
142#
143# $ret="";
144#
145# $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
146# $reg1="$regs{$reg1}" if defined($regs{$reg1});
147# $reg2="$regs{$reg2}" if defined($regs{$reg2});
148# $ret.=$addr if ($addr ne "") && ($addr ne 0);
149# if ($reg2 ne "")
150# { $ret.="($reg1,$reg2,$idx)"; }
151# else
152# { $ret.="($reg1)" }
153# return($ret);
154# }
155
156sub main'mov { &out2("movl",@_); }
157sub main'movb { &out2("movb",@_); }
158sub main'and { &out2("andl",@_); }
159sub main'or { &out2("orl",@_); }
160sub main'shl { &out2("sall",@_); }
161sub main'shr { &out2("shrl",@_); }
162sub main'xor { &out2("xorl",@_); }
163sub main'xorb { &out2("xorb",@_); }
164sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); }
165sub main'adc { &out2("adcl",@_); }
166sub main'sub { &out2("subl",@_); }
167sub main'sbb { &out2("sbbl",@_); }
168sub main'rotl { &out2("roll",@_); }
169sub main'rotr { &out2("rorl",@_); }
170sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); }
171sub main'cmp { &out2("cmpl",@_); }
172sub main'lea { &out2("leal",@_); }
173sub main'mul { &out1("mull",@_); }
174sub main'imul { &out2("imull",@_); }
175sub main'div { &out1("divl",@_); }
176sub main'jmp { &out1("jmp",@_); }
177sub main'jmp_ptr { &out1p("jmp",@_); }
178sub main'je { &out1("je",@_); }
179sub main'jle { &out1("jle",@_); }
180sub main'jne { &out1("jne",@_); }
181sub main'jnz { &out1("jnz",@_); }
182sub main'jz { &out1("jz",@_); }
183sub main'jge { &out1("jge",@_); }
184sub main'jl { &out1("jl",@_); }
185sub main'ja { &out1("ja",@_); }
186sub main'jae { &out1("jae",@_); }
187sub main'jb { &out1("jb",@_); }
188sub main'jbe { &out1("jbe",@_); }
189sub main'jc { &out1("jc",@_); }
190sub main'jnc { &out1("jnc",@_); }
191sub main'jno { &out1("jno",@_); }
192sub main'dec { &out1("decl",@_); }
193sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
194sub main'push { &out1("pushl",@_); $stack+=4; }
195sub main'pop { &out1("popl",@_); $stack-=4; }
196sub main'pushf { &out0("pushfl"); $stack+=4; }
197sub main'popf { &out0("popfl"); $stack-=4; }
198sub main'not { &out1("notl",@_); }
199sub main'call { my $pre=$under;
200 foreach $i (%label)
201 { if ($label{$i} eq $_[0]) { $pre=''; last; } }
202 &out1("call",$pre.$_[0]);
203 }
204sub main'call_ptr { &out1p("call",@_); }
205sub main'ret { &out0("ret"); }
206sub main'nop { &out0("nop"); }
207sub main'test { &out2("testl",@_); }
208sub main'bt { &out2("btl",@_); }
209sub main'leave { &out0("leave"); }
210sub main'cpuid { &out0(".byte\t0x0f,0xa2"); }
211sub main'rdtsc { &out0(".byte\t0x0f,0x31"); }
212sub main'halt { &out0("hlt"); }
213sub main'movz { &out2("movzbl",@_); }
214sub main'neg { &out1("negl",@_); }
215sub main'cld { &out0("cld"); }
216
217# SSE2
218sub main'emms { &out0("emms"); }
219sub main'movd { &out2("movd",@_); }
220sub main'movdqu { &out2("movdqu",@_); }
221sub main'movdqa { &out2("movdqa",@_); }
222sub main'movdq2q{ &out2("movdq2q",@_); }
223sub main'movq2dq{ &out2("movq2dq",@_); }
224sub main'paddq { &out2("paddq",@_); }
225sub main'pmuludq{ &out2("pmuludq",@_); }
226sub main'psrlq { &out2("psrlq",@_); }
227sub main'psllq { &out2("psllq",@_); }
228sub main'pxor { &out2("pxor",@_); }
229sub main'por { &out2("por",@_); }
230sub main'pand { &out2("pand",@_); }
231sub main'movq {
232 local($p1,$p2,$optimize)=@_;
233 if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
234 # movq between mmx registers can sink Intel CPUs
235 { push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n"); }
236 else { &out2("movq",@_); }
237 }
238
239# The bswapl instruction is new for the 486. Emulate if i386.
240sub main'bswap
241 {
242 if ($main'i386)
243 {
244 &main'comment("bswapl @_");
245 &main'exch(main'HB(@_),main'LB(@_));
246 &main'rotr(@_,16);
247 &main'exch(main'HB(@_),main'LB(@_));
248 }
249 else
250 {
251 &out1("bswapl",@_);
252 }
253 }
254
255sub out2
256 {
257 local($name,$p1,$p2)=@_;
258 local($l,$ll,$t);
259 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
260 "rcll",0xD1D0,"rcrl",0xD1D8,
261 "shll",0xD1E0,"shrl",0xD1E8,
262 "sarl",0xD1F8);
263
264 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
265 {
266 $op=$special{$name}|$reg_val{$p1};
267 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
268 $tmp2=sprintf(".byte %d\t",$op &0xff);
269 push(@out,$tmp1);
270 push(@out,$tmp2);
271
272 $p2=&conv($p2);
273 $p1=&conv($p1);
274 &main'comment("$name $p2 $p1");
275 return;
276 }
277
278 push(@out,"\t$name\t");
279 $t=&conv($p2).",";
280 $l=length($t);
281 push(@out,$t);
282 $ll=4-($l+9)/8;
283 $tmp1=sprintf("\t" x $ll);
284 push(@out,$tmp1);
285 push(@out,&conv($p1)."\n");
286 }
287
288sub out1
289 {
290 local($name,$p1)=@_;
291 local($l,$t);
292 local(%special)=("bswapl",0x0FC8);
293
294 if ((defined($special{$name})) && defined($regs{$p1}))
295 {
296 $op=$special{$name}|$reg_val{$p1};
297 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
298 $tmp2=sprintf(".byte %d\t",$op &0xff);
299 push(@out,$tmp1);
300 push(@out,$tmp2);
301
302 $p2=&conv($p2);
303 $p1=&conv($p1);
304 &main'comment("$name $p2 $p1");
305 return;
306 }
307
308 push(@out,"\t$name\t".&conv($p1)."\n");
309 }
310
311sub out1p
312 {
313 local($name,$p1)=@_;
314 local($l,$t);
315
316 push(@out,"\t$name\t*".&conv($p1)."\n");
317 }
318
319sub out0
320 {
321 push(@out,"\t$_[0]\n");
322 }
323
324sub conv
325 {
326 local($p)=@_;
327
328# $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
329
330 $p=$regs{$p} if (defined($regs{$p}));
331
332 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
333 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
334 return $p;
335 }
336
337sub main'file
338 {
339 local($file)=@_;
340
341 local($tmp)=<<"EOF";
342 .file "$file.s"
343EOF
344 push(@out,$tmp);
345 }
346
347sub main'function_begin
348 {
349 local($func)=@_;
350
351 &main'external_label($func);
352 $func=$under.$func;
353
354 local($tmp)=<<"EOF";
355.text
356.globl $func
357EOF
358 push(@out,$tmp);
359 if ($main'cpp)
360 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
361 elsif ($main'coff)
362 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
363 elsif ($main'aout and !$main'pic)
364 { }
365 else { $tmp=push(@out,".type\t$func,\@function\n"); }
366 push(@out,".align\t$align\n");
367 push(@out,"$func:\n");
368 $tmp=<<"EOF";
369 pushl %ebp
370 pushl %ebx
371 pushl %esi
372 pushl %edi
373
374EOF
375 push(@out,$tmp);
376 $stack=20;
377 }
378
379sub main'function_begin_B
380 {
381 local($func,$extra)=@_;
382
383 &main'external_label($func);
384 $func=$under.$func;
385
386 local($tmp)=<<"EOF";
387.text
388.globl $func
389EOF
390 push(@out,$tmp);
391 if ($main'cpp)
392 { push(@out,"TYPE($func,\@function)\n"); }
393 elsif ($main'coff)
394 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
395 elsif ($main'aout and !$main'pic)
396 { }
397 else { push(@out,".type $func,\@function\n"); }
398 push(@out,".align\t$align\n");
399 push(@out,"$func:\n");
400 $stack=4;
401 }
402
403sub main'function_end
404 {
405 local($func)=@_;
406
407 $func=$under.$func;
408
409 local($tmp)=<<"EOF";
410 popl %edi
411 popl %esi
412 popl %ebx
413 popl %ebp
414 ret
415${dot}L_${func}_end:
416EOF
417 push(@out,$tmp);
418
419 if ($main'cpp)
420 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
421 elsif ($main'coff or $main'aout)
422 { }
423 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
424 push(@out,".ident \"$func\"\n");
425 $stack=0;
426 %label=();
427 }
428
429sub main'function_end_A
430 {
431 local($func)=@_;
432
433 local($tmp)=<<"EOF";
434 popl %edi
435 popl %esi
436 popl %ebx
437 popl %ebp
438 ret
439EOF
440 push(@out,$tmp);
441 }
442
443sub main'function_end_B
444 {
445 local($func)=@_;
446
447 $func=$under.$func;
448
449 push(@out,"${dot}L_${func}_end:\n");
450 if ($main'cpp)
451 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
452 elsif ($main'coff or $main'aout)
453 { }
454 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
455 push(@out,".ident \"$func\"\n");
456 $stack=0;
457 %label=();
458 }
459
460sub main'wparam
461 {
462 local($num)=@_;
463
464 return(&main'DWP($stack+$num*4,"esp","",0));
465 }
466
467sub main'stack_push
468 {
469 local($num)=@_;
470 $stack+=$num*4;
471 &main'sub("esp",$num*4);
472 }
473
474sub main'stack_pop
475 {
476 local($num)=@_;
477 $stack-=$num*4;
478 &main'add("esp",$num*4);
479 }
480
481sub main'swtmp
482 {
483 return(&main'DWP($_[0]*4,"esp","",0));
484 }
485
486# Should use swtmp, which is above esp. Linix can trash the stack above esp
487#sub main'wtmp
488# {
489# local($num)=@_;
490#
491# return(&main'DWP(-($num+1)*4,"esp","",0));
492# }
493
494sub main'comment
495 {
496 if (!defined($com_start) or $main'elf)
497 { # Regarding $main'elf above...
498 # GNU and SVR4 as'es use different comment delimiters,
499 push(@out,"\n"); # so we just skip ELF comments...
500 return;
501 }
502 foreach (@_)
503 {
504 if (/^\s*$/)
505 { push(@out,"\n"); }
506 else
507 { push(@out,"\t$com_start $_ $com_end\n"); }
508 }
509 }
510
511sub main'public_label
512 {
513 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
514 push(@out,".globl\t$label{$_[0]}\n");
515 }
516
517sub main'label
518 {
519 if (!defined($label{$_[0]}))
520 {
521 $label{$_[0]}="${dot}${label}${_[0]}";
522 $label++;
523 }
524 return($label{$_[0]});
525 }
526
527sub main'set_label
528 {
529 if (!defined($label{$_[0]}))
530 {
531 $label{$_[0]}="${dot}${label}${_[0]}";
532 $label++;
533 }
534 if ($_[1]!=0)
535 {
536 if ($_[1]>1) { main'align($_[1]); }
537 else { push(@out,".align $align\n"); }
538 }
539 push(@out,"$label{$_[0]}:\n");
540 }
541
542sub main'file_end
543 {
544 # try to detect if SSE2 or MMX extensions were used on ELF platform...
545 if ($main'elf && grep {/\b%[x]*mm[0-7]\b|OPENSSL_ia32cap_P\b/i} @out) {
546 local($tmp);
547
548 push (@out,"\n.section\t.bss\n");
549 push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
550
551 return;
552 }
553
554 if ($const ne "")
555 {
556 push(@out,".section .rodata\n");
557 push(@out,$const);
558 $const="";
559 }
560 }
561
562sub main'data_byte
563 {
564 push(@out,"\t.byte\t".join(',',@_)."\n");
565 }
566
567sub main'data_word
568 {
569 push(@out,"\t.long\t".join(',',@_)."\n");
570 }
571
572sub main'align
573 {
574 my $val=$_[0],$p2,$i;
575 if ($main'aout) {
576 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
577 $val=$p2-1;
578 $val.=",0x90";
579 }
580 push(@out,".align\t$val\n");
581 }
582
583# debug output functions: puts, putx, printf
584
585sub main'puts
586 {
587 &pushvars();
588 &main'push('$Lstring' . ++$constl);
589 &main'call('puts');
590 $stack-=4;
591 &main'add("esp",4);
592 &popvars();
593
594 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
595 }
596
597sub main'putx
598 {
599 &pushvars();
600 &main'push($_[0]);
601 &main'push('$Lstring' . ++$constl);
602 &main'call('printf');
603 &main'add("esp",8);
604 $stack-=8;
605 &popvars();
606
607 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
608 }
609
610sub main'printf
611 {
612 $ostack = $stack;
613 &pushvars();
614 for ($i = @_ - 1; $i >= 0; $i--)
615 {
616 if ($i == 0) # change this to support %s format strings
617 {
618 &main'push('$Lstring' . ++$constl);
619 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
620 }
621 else
622 {
623 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
624 {
625 &main'push(($1 + $stack - $ostack) . '(%esp)');
626 }
627 else
628 {
629 &main'push($_[$i]);
630 }
631 }
632 }
633 &main'call('printf');
634 $stack-=4*@_;
635 &main'add("esp",4*@_);
636 &popvars();
637 }
638
639sub pushvars
640 {
641 &main'pushf();
642 &main'push("edx");
643 &main'push("ecx");
644 &main'push("eax");
645 }
646
647sub popvars
648 {
649 &main'pop("eax");
650 &main'pop("ecx");
651 &main'pop("edx");
652 &main'popf();
653 }
654
655sub main'picmeup
656 {
657 local($dst,$sym)=@_;
658 if ($main'cpp)
659 {
660 local($tmp)=<<___;
661#if (defined(ELF) || defined(SOL)) && defined(PIC)
662 call 1f
6631: popl $regs{$dst}
664 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
665 movl $sym\@GOT($regs{$dst}),$regs{$dst}
666#else
667 leal $sym,$regs{$dst}
668#endif
669___
670 push(@out,$tmp);
671 }
672 elsif ($main'pic && ($main'elf || $main'aout))
673 {
674 &main'call(&main'label("PIC_me_up"));
675 &main'set_label("PIC_me_up");
676 &main'blindpop($dst);
677 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
678 &main'label("PIC_me_up") . "]");
679 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
680 }
681 else
682 {
683 &main'lea($dst,&main'DWP($sym));
684 }
685 }
686
687sub main'blindpop { &out1("popl",@_); }
688
689sub main'initseg
690 {
691 local($f)=@_;
692 local($tmp);
693 if ($main'elf)
694 {
695 $tmp=<<___;
696.section .init
697 call $under$f
698 jmp .Linitalign
699.align $align
700.Linitalign:
701___
702 }
703 elsif ($main'coff)
704 {
705 $tmp=<<___; # applies to both Cygwin and Mingw
706.section .ctors
707.long $under$f
708___
709 }
710 elsif ($main'aout)
711 {
712 local($ctor)="${under}_GLOBAL_\$I\$$f";
713 $tmp=".text\n";
714 $tmp.=".type $ctor,\@function\n" if ($main'pic);
715 $tmp.=<<___; # OpenBSD way...
716.globl $ctor
717.align 2
718$ctor:
719 jmp $under$f
720___
721 }
722 push(@out,$tmp) if ($tmp);
723 }
724
7251;