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/cbc.pl349
-rw-r--r--src/lib/libcrypto/perlasm/readme124
-rw-r--r--src/lib/libcrypto/perlasm/x86asm.pl135
-rw-r--r--src/lib/libcrypto/perlasm/x86ms.pl122
-rw-r--r--src/lib/libcrypto/perlasm/x86nasm.pl153
-rw-r--r--src/lib/libcrypto/perlasm/x86unix.pl251
6 files changed, 734 insertions, 400 deletions
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl
new file mode 100644
index 0000000000..22149c680e
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/cbc.pl
@@ -0,0 +1,349 @@
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 &set_label("cbc_enc_jmp_table",1);
326 &data_word("0");
327 &data_word(&label("ej1")."-".&label("PIC_point"));
328 &data_word(&label("ej2")."-".&label("PIC_point"));
329 &data_word(&label("ej3")."-".&label("PIC_point"));
330 &data_word(&label("ej4")."-".&label("PIC_point"));
331 &data_word(&label("ej5")."-".&label("PIC_point"));
332 &data_word(&label("ej6")."-".&label("PIC_point"));
333 &data_word(&label("ej7")."-".&label("PIC_point"));
334 # not used
335 #&set_label("cbc_dec_jmp_table",1);
336 #&data_word("0");
337 #&data_word(&label("dj1")."-".&label("PIC_point"));
338 #&data_word(&label("dj2")."-".&label("PIC_point"));
339 #&data_word(&label("dj3")."-".&label("PIC_point"));
340 #&data_word(&label("dj4")."-".&label("PIC_point"));
341 #&data_word(&label("dj5")."-".&label("PIC_point"));
342 #&data_word(&label("dj6")."-".&label("PIC_point"));
343 #&data_word(&label("dj7")."-".&label("PIC_point"));
344
345 &function_end_B($name);
346
347 }
348
3491;
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/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl
new file mode 100644
index 0000000000..c3de90c65d
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86asm.pl
@@ -0,0 +1,135 @@
1#!/usr/local/bin/perl
2
3# require 'x86asm.pl';
4# &asm_init("cpp","des-586.pl");
5# XXX
6# XXX
7# main'asm_finish
8
9sub main'asm_finish
10 {
11 &file_end();
12 &asm_finish_cpp() if $cpp;
13 print &asm_get_output();
14 }
15
16sub main'asm_init
17 {
18 ($type,$fn,$i386)=@_;
19 $filename=$fn;
20
21 $elf=$cpp=$sol=$aout=$win32=$gaswin=$openbsd=0;
22 if ( ($type eq "elf"))
23 { $elf=1; require "x86unix.pl"; }
24 elsif ( ($type eq "openbsd-elf"))
25 { $openbsd=$elf=1; require "x86unix.pl"; }
26 elsif ( ($type eq "openbsd-a.out"))
27 { $openbsd=1; require "x86unix.pl"; }
28 elsif ( ($type eq "a.out"))
29 { $aout=1; require "x86unix.pl"; }
30 elsif ( ($type eq "gaswin"))
31 { $gaswin=1; $aout=1; require "x86unix.pl"; }
32 elsif ( ($type eq "sol"))
33 { $sol=1; require "x86unix.pl"; }
34 elsif ( ($type eq "cpp"))
35 { $cpp=1; require "x86unix.pl"; }
36 elsif ( ($type eq "win32"))
37 { $win32=1; require "x86ms.pl"; }
38 elsif ( ($type eq "win32n"))
39 { $win32=1; require "x86nasm.pl"; }
40 else
41 {
42 print STDERR <<"EOF";
43Pick one target type from
44 elf - linux, FreeBSD etc
45 a.out - old linux
46 sol - x86 solaris
47 cpp - format so x86unix.cpp can be used
48 win32 - Windows 95/Windows NT
49 win32n - Windows 95/Windows NT NASM format
50 openbsd-elf - OpenBSD elf
51 openbsd-a.out - OpenBSD a.out
52EOF
53 exit(1);
54 }
55
56 $pic=0;
57 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
58
59 &asm_init_output();
60
61&comment("Don't even think of reading this code");
62&comment("It was automatically generated by $filename");
63&comment("Which is a perl program used to generate the x86 assember for");
64&comment("any of elf, a.out, BSDI, Win32, gaswin (for GNU as on Win32) or Solaris");
65&comment("eric <eay\@cryptsoft.com>");
66&comment("");
67
68 $filename =~ s/\.pl$//;
69 &file($filename);
70 }
71
72sub asm_finish_cpp
73 {
74 return unless $cpp;
75
76 local($tmp,$i);
77 foreach $i (&get_labels())
78 {
79 $tmp.="#define $i _$i\n";
80 }
81 print <<"EOF";
82/* Run the C pre-processor over this file with one of the following defined
83 * ELF - elf object files,
84 * OUT - a.out object files,
85 * BSDI - BSDI style a.out object files
86 * SOL - Solaris style elf
87 */
88
89#define TYPE(a,b) .type a,b
90#define SIZE(a,b) .size a,b
91
92#if defined(OUT) || (defined(BSDI) && !defined(ELF))
93$tmp
94#endif
95
96#ifdef OUT
97#define OK 1
98#define ALIGN 4
99#if defined(__CYGWIN__) || defined(__DJGPP__) || defined(__MINGW32__)
100#undef SIZE
101#undef TYPE
102#define SIZE(a,b)
103#define TYPE(a,b) .def a; .scl 2; .type 32; .endef
104#endif /* __CYGWIN || __DJGPP */
105#endif
106
107#if defined(BSDI) && !defined(ELF)
108#define OK 1
109#define ALIGN 4
110#undef SIZE
111#undef TYPE
112#define SIZE(a,b)
113#define TYPE(a,b)
114#endif
115
116#if defined(ELF) || defined(SOL)
117#define OK 1
118#define ALIGN 16
119#endif
120
121#ifndef OK
122You need to define one of
123ELF - elf systems - linux-elf, NetBSD and DG-UX
124OUT - a.out systems - linux-a.out and FreeBSD
125SOL - solaris systems, which are elf with strange comment lines
126BSDI - a.out with a very primative version of as.
127#endif
128
129/* Let the Assembler begin :-) */
130EOF
131 }
132
133sub main'align() {} # swallow align statements in 0.9.7 context
134
1351;
diff --git a/src/lib/libcrypto/perlasm/x86ms.pl b/src/lib/libcrypto/perlasm/x86ms.pl
index a0be2934c2..b6bd744057 100644
--- a/src/lib/libcrypto/perlasm/x86ms.pl
+++ b/src/lib/libcrypto/perlasm/x86ms.pl
@@ -27,13 +27,7 @@ $label="L000";
27sub main'asm_init_output { @out=(); } 27sub main'asm_init_output { @out=(); }
28sub main'asm_get_output { return(@out); } 28sub main'asm_get_output { return(@out); }
29sub main'get_labels { return(@labels); } 29sub main'get_labels { return(@labels); }
30sub main'external_label 30sub main'external_label { push(@labels,@_); }
31{
32 push(@labels,@_);
33 foreach (@_) {
34 push(@out, "EXTRN\t_$_:DWORD\n");
35 }
36}
37 31
38sub main'LB 32sub main'LB
39 { 33 {
@@ -57,11 +51,6 @@ sub main'DWP
57 &get_mem("DWORD",@_); 51 &get_mem("DWORD",@_);
58 } 52 }
59 53
60sub main'QWP
61 {
62 &get_mem("QWORD",@_);
63 }
64
65sub main'BC 54sub main'BC
66 { 55 {
67 return @_; 56 return @_;
@@ -98,7 +87,7 @@ sub get_mem
98 $reg2=&conv($1); 87 $reg2=&conv($1);
99 $addr="_$2"; 88 $addr="_$2";
100 } 89 }
101 elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i) 90 elsif ($addr =~ /^[_a-zA-Z]/)
102 { 91 {
103 $addr="_$addr"; 92 $addr="_$addr";
104 } 93 }
@@ -139,14 +128,12 @@ sub main'xorb { &out2("xor",@_); }
139sub main'add { &out2("add",@_); } 128sub main'add { &out2("add",@_); }
140sub main'adc { &out2("adc",@_); } 129sub main'adc { &out2("adc",@_); }
141sub main'sub { &out2("sub",@_); } 130sub main'sub { &out2("sub",@_); }
142sub main'sbb { &out2("sbb",@_); }
143sub main'rotl { &out2("rol",@_); } 131sub main'rotl { &out2("rol",@_); }
144sub main'rotr { &out2("ror",@_); } 132sub main'rotr { &out2("ror",@_); }
145sub main'exch { &out2("xchg",@_); } 133sub main'exch { &out2("xchg",@_); }
146sub main'cmp { &out2("cmp",@_); } 134sub main'cmp { &out2("cmp",@_); }
147sub main'lea { &out2("lea",@_); } 135sub main'lea { &out2("lea",@_); }
148sub main'mul { &out1("mul",@_); } 136sub main'mul { &out1("mul",@_); }
149sub main'imul { &out2("imul",@_); }
150sub main'div { &out1("div",@_); } 137sub main'div { &out1("div",@_); }
151sub main'dec { &out1("dec",@_); } 138sub main'dec { &out1("dec",@_); }
152sub main'inc { &out1("inc",@_); } 139sub main'inc { &out1("inc",@_); }
@@ -168,54 +155,26 @@ sub main'jne { &out1("jne",@_); }
168sub main'jno { &out1("jno",@_); } 155sub main'jno { &out1("jno",@_); }
169sub main'push { &out1("push",@_); $stack+=4; } 156sub main'push { &out1("push",@_); $stack+=4; }
170sub main'pop { &out1("pop",@_); $stack-=4; } 157sub 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(); } 158sub main'bswap { &out1("bswap",@_); &using486(); }
174sub main'not { &out1("not",@_); } 159sub main'not { &out1("not",@_); }
175sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); } 160sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
176sub main'call_ptr { &out1p("call",@_); }
177sub main'ret { &out0("ret"); } 161sub main'ret { &out0("ret"); }
178sub main'nop { &out0("nop"); } 162sub 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",@_); } 163sub 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 164
205sub out2 165sub out2
206 { 166 {
207 local($name,$p1,$p2)=@_; 167 local($name,$p1,$p2)=@_;
208 local($l,$t,$line); 168 local($l,$t);
209 169
210 $line="\t$name\t"; 170 push(@out,"\t$name\t");
211 $t=&conv($p1).","; 171 $t=&conv($p1).",";
212 $l=length($t); 172 $l=length($t);
213 $line.="$t"; 173 push(@out,$t);
214 $l=4-($l+9)/8; 174 $l=4-($l+9)/8;
215 $line.="\t" x $l; 175 push(@out,"\t" x $l);
216 $line.=&conv($p2); 176 push(@out,&conv($p2));
217 if ($line=~/\bxmm[0-7]\b/i) { $line=~s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i; } 177 push(@out,"\n");
218 push(@out,$line."\n");
219 } 178 }
220 179
221sub out0 180sub out0
@@ -255,9 +214,7 @@ sub main'file
255 local($tmp)=<<"EOF"; 214 local($tmp)=<<"EOF";
256 TITLE $file.asm 215 TITLE $file.asm
257 .386 216 .386
258.model FLAT 217.model FLAT
259_TEXT\$ SEGMENT PAGE 'CODE'
260
261EOF 218EOF
262 push(@out,$tmp); 219 push(@out,$tmp);
263 } 220 }
@@ -269,6 +226,7 @@ sub main'function_begin
269 push(@labels,$func); 226 push(@labels,$func);
270 227
271 local($tmp)=<<"EOF"; 228 local($tmp)=<<"EOF";
229_TEXT SEGMENT
272PUBLIC _$func 230PUBLIC _$func
273$extra 231$extra
274_$func PROC NEAR 232_$func PROC NEAR
@@ -286,6 +244,7 @@ sub main'function_begin_B
286 local($func,$extra)=@_; 244 local($func,$extra)=@_;
287 245
288 local($tmp)=<<"EOF"; 246 local($tmp)=<<"EOF";
247_TEXT SEGMENT
289PUBLIC _$func 248PUBLIC _$func
290$extra 249$extra
291_$func PROC NEAR 250_$func PROC NEAR
@@ -305,6 +264,7 @@ sub main'function_end
305 pop ebp 264 pop ebp
306 ret 265 ret
307_$func ENDP 266_$func ENDP
267_TEXT ENDS
308EOF 268EOF
309 push(@out,$tmp); 269 push(@out,$tmp);
310 $stack=0; 270 $stack=0;
@@ -317,6 +277,7 @@ sub main'function_end_B
317 277
318 local($tmp)=<<"EOF"; 278 local($tmp)=<<"EOF";
319_$func ENDP 279_$func ENDP
280_TEXT ENDS
320EOF 281EOF
321 push(@out,$tmp); 282 push(@out,$tmp);
322 $stack=0; 283 $stack=0;
@@ -339,20 +300,6 @@ EOF
339 300
340sub main'file_end 301sub main'file_end
341 { 302 {
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"); 303 push(@out,"END\n");
357 } 304 }
358 305
@@ -384,12 +331,6 @@ sub main'comment
384 } 331 }
385 } 332 }
386 333
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 334sub main'label
394 { 335 {
395 if (!defined($label{$_[0]})) 336 if (!defined($label{$_[0]}))
@@ -407,37 +348,19 @@ sub main'set_label
407 $label{$_[0]}="\$${label}${_[0]}"; 348 $label{$_[0]}="\$${label}${_[0]}";
408 $label++; 349 $label++;
409 } 350 }
410 if ($_[1]!=0 && $_[1]>1)
411 {
412 main'align($_[1]);
413 }
414 if((defined $_[2]) && ($_[2] == 1)) 351 if((defined $_[2]) && ($_[2] == 1))
415 { 352 {
416 push(@out,"$label{$_[0]}::\n"); 353 push(@out,"$label{$_[0]}::\n");
417 } 354 }
418 elsif ($label{$_[0]} !~ /^\$/)
419 {
420 push(@out,"$label{$_[0]}\tLABEL PTR\n");
421 }
422 else 355 else
423 { 356 {
424 push(@out,"$label{$_[0]}:\n"); 357 push(@out,"$label{$_[0]}:\n");
425 } 358 }
426 } 359 }
427 360
428sub main'data_byte
429 {
430 push(@out,"\tDB\t".join(',',@_)."\n");
431 }
432
433sub main'data_word 361sub main'data_word
434 { 362 {
435 push(@out,"\tDD\t".join(',',@_)."\n"); 363 push(@out,"\tDD\t$_[0]\n");
436 }
437
438sub main'align
439 {
440 push(@out,"\tALIGN\t$_[0]\n");
441 } 364 }
442 365
443sub out1p 366sub out1p
@@ -445,7 +368,7 @@ sub out1p
445 local($name,$p1)=@_; 368 local($name,$p1)=@_;
446 local($l,$t); 369 local($l,$t);
447 370
448 push(@out,"\t$name\t".&conv($p1)."\n"); 371 push(@out,"\t$name\t ".&conv($p1)."\n");
449 } 372 }
450 373
451sub main'picmeup 374sub main'picmeup
@@ -455,18 +378,3 @@ sub main'picmeup
455 } 378 }
456 379
457sub main'blindpop { &out1("pop",@_); } 380sub 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/x86nasm.pl b/src/lib/libcrypto/perlasm/x86nasm.pl
index fa38f89c09..4bdb3fe180 100644
--- a/src/lib/libcrypto/perlasm/x86nasm.pl
+++ b/src/lib/libcrypto/perlasm/x86nasm.pl
@@ -3,7 +3,6 @@
3package x86nasm; 3package x86nasm;
4 4
5$label="L000"; 5$label="L000";
6$under=($main'netware)?'':'_';
7 6
8%lb=( 'eax', 'al', 7%lb=( 'eax', 'al',
9 'ebx', 'bl', 8 'ebx', 'bl',
@@ -33,8 +32,7 @@ sub main'external_label
33{ 32{
34 push(@labels,@_); 33 push(@labels,@_);
35 foreach (@_) { 34 foreach (@_) {
36 push(@out,".") if ($main'mwerks); 35 push(@out, "extern\t_$_\n");
37 push(@out, "extern\t${under}$_\n");
38 } 36 }
39} 37}
40 38
@@ -60,19 +58,14 @@ sub main'DWP
60 &get_mem("DWORD",@_); 58 &get_mem("DWORD",@_);
61 } 59 }
62 60
63sub main'QWP
64 {
65 &get_mem("",@_);
66 }
67
68sub main'BC 61sub main'BC
69 { 62 {
70 return (($main'mwerks)?"":"BYTE ")."@_"; 63 return "BYTE @_";
71 } 64 }
72 65
73sub main'DWC 66sub main'DWC
74 { 67 {
75 return (($main'mwerks)?"":"DWORD ")."@_"; 68 return "DWORD @_";
76 } 69 }
77 70
78sub main'stack_push 71sub main'stack_push
@@ -93,22 +86,16 @@ sub get_mem
93 { 86 {
94 my($size,$addr,$reg1,$reg2,$idx)=@_; 87 my($size,$addr,$reg1,$reg2,$idx)=@_;
95 my($t,$post); 88 my($t,$post);
96 my($ret)=$size; 89 my($ret)="$size [";
97 if ($ret ne "")
98 {
99 $ret .= " PTR" if ($main'mwerks);
100 $ret .= " ";
101 }
102 $ret .= "[";
103 $addr =~ s/^\s+//; 90 $addr =~ s/^\s+//;
104 if ($addr =~ /^(.+)\+(.+)$/) 91 if ($addr =~ /^(.+)\+(.+)$/)
105 { 92 {
106 $reg2=&conv($1); 93 $reg2=&conv($1);
107 $addr="$under$2"; 94 $addr="_$2";
108 } 95 }
109 elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i) 96 elsif ($addr =~ /^[_a-zA-Z]/)
110 { 97 {
111 $addr="$under$addr"; 98 $addr="_$addr";
112 } 99 }
113 100
114 if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; } 101 if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }
@@ -147,14 +134,12 @@ sub main'xorb { &out2("xor",@_); }
147sub main'add { &out2("add",@_); } 134sub main'add { &out2("add",@_); }
148sub main'adc { &out2("adc",@_); } 135sub main'adc { &out2("adc",@_); }
149sub main'sub { &out2("sub",@_); } 136sub main'sub { &out2("sub",@_); }
150sub main'sbb { &out2("sbb",@_); }
151sub main'rotl { &out2("rol",@_); } 137sub main'rotl { &out2("rol",@_); }
152sub main'rotr { &out2("ror",@_); } 138sub main'rotr { &out2("ror",@_); }
153sub main'exch { &out2("xchg",@_); } 139sub main'exch { &out2("xchg",@_); }
154sub main'cmp { &out2("cmp",@_); } 140sub main'cmp { &out2("cmp",@_); }
155sub main'lea { &out2("lea",@_); } 141sub main'lea { &out2("lea",@_); }
156sub main'mul { &out1("mul",@_); } 142sub main'mul { &out1("mul",@_); }
157sub main'imul { &out2("imul",@_); }
158sub main'div { &out1("div",@_); } 143sub main'div { &out1("div",@_); }
159sub main'dec { &out1("dec",@_); } 144sub main'dec { &out1("dec",@_); }
160sub main'inc { &out1("inc",@_); } 145sub main'inc { &out1("inc",@_); }
@@ -162,57 +147,29 @@ sub main'jmp { &out1("jmp",@_); }
162sub main'jmp_ptr { &out1p("jmp",@_); } 147sub main'jmp_ptr { &out1p("jmp",@_); }
163 148
164# This is a bit of a kludge: declare all branches as NEAR. 149# This is a bit of a kludge: declare all branches as NEAR.
165$near=($main'mwerks)?'':'NEAR'; 150sub main'je { &out1("je NEAR",@_); }
166sub main'je { &out1("je $near",@_); } 151sub main'jle { &out1("jle NEAR",@_); }
167sub main'jle { &out1("jle $near",@_); } 152sub main'jz { &out1("jz NEAR",@_); }
168sub main'jz { &out1("jz $near",@_); } 153sub main'jge { &out1("jge NEAR",@_); }
169sub main'jge { &out1("jge $near",@_); } 154sub main'jl { &out1("jl NEAR",@_); }
170sub main'jl { &out1("jl $near",@_); } 155sub main'ja { &out1("ja NEAR",@_); }
171sub main'ja { &out1("ja $near",@_); } 156sub main'jae { &out1("jae NEAR",@_); }
172sub main'jae { &out1("jae $near",@_); } 157sub main'jb { &out1("jb NEAR",@_); }
173sub main'jb { &out1("jb $near",@_); } 158sub main'jbe { &out1("jbe NEAR",@_); }
174sub main'jbe { &out1("jbe $near",@_); } 159sub main'jc { &out1("jc NEAR",@_); }
175sub main'jc { &out1("jc $near",@_); } 160sub main'jnc { &out1("jnc NEAR",@_); }
176sub main'jnc { &out1("jnc $near",@_); } 161sub main'jnz { &out1("jnz NEAR",@_); }
177sub main'jnz { &out1("jnz $near",@_); } 162sub main'jne { &out1("jne NEAR",@_); }
178sub main'jne { &out1("jne $near",@_); } 163sub main'jno { &out1("jno NEAR",@_); }
179sub main'jno { &out1("jno $near",@_); }
180 164
181sub main'push { &out1("push",@_); $stack+=4; } 165sub main'push { &out1("push",@_); $stack+=4; }
182sub main'pop { &out1("pop",@_); $stack-=4; } 166sub main'pop { &out1("pop",@_); $stack-=4; }
183sub main'pushf { &out0("pushfd"); $stack+=4; }
184sub main'popf { &out0("popfd"); $stack-=4; }
185sub main'bswap { &out1("bswap",@_); &using486(); } 167sub main'bswap { &out1("bswap",@_); &using486(); }
186sub main'not { &out1("not",@_); } 168sub main'not { &out1("not",@_); }
187sub main'call { &out1("call",($_[0]=~/^\@L/?'':$under).$_[0]); } 169sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
188sub main'call_ptr { &out1p("call",@_); }
189sub main'ret { &out0("ret"); } 170sub main'ret { &out0("ret"); }
190sub main'nop { &out0("nop"); } 171sub main'nop { &out0("nop"); }
191sub main'test { &out2("test",@_); }
192sub main'bt { &out2("bt",@_); }
193sub main'leave { &out0("leave"); }
194sub main'cpuid { &out0("cpuid"); }
195sub main'rdtsc { &out0("rdtsc"); }
196sub main'halt { &out0("hlt"); }
197sub main'movz { &out2("movzx",@_); } 172sub main'movz { &out2("movzx",@_); }
198sub main'neg { &out1("neg",@_); }
199sub main'cld { &out0("cld"); }
200
201# SSE2
202sub main'emms { &out0("emms"); }
203sub main'movd { &out2("movd",@_); }
204sub main'movq { &out2("movq",@_); }
205sub main'movdqu { &out2("movdqu",@_); }
206sub main'movdqa { &out2("movdqa",@_); }
207sub main'movdq2q{ &out2("movdq2q",@_); }
208sub main'movq2dq{ &out2("movq2dq",@_); }
209sub main'paddq { &out2("paddq",@_); }
210sub main'pmuludq{ &out2("pmuludq",@_); }
211sub main'psrlq { &out2("psrlq",@_); }
212sub main'psllq { &out2("psllq",@_); }
213sub main'pxor { &out2("pxor",@_); }
214sub main'por { &out2("por",@_); }
215sub main'pand { &out2("pand",@_); }
216 173
217sub out2 174sub out2
218 { 175 {
@@ -220,7 +177,7 @@ sub out2
220 my($l,$t); 177 my($l,$t);
221 178
222 push(@out,"\t$name\t"); 179 push(@out,"\t$name\t");
223 if (!$main'mwerks and $name eq "lea") 180 if ($name eq "lea")
224 { 181 {
225 $p1 =~ s/^[^\[]*\[/\[/; 182 $p1 =~ s/^[^\[]*\[/\[/;
226 $p2 =~ s/^[^\[]*\[/\[/; 183 $p2 =~ s/^[^\[]*\[/\[/;
@@ -264,17 +221,15 @@ sub using486
264 221
265sub main'file 222sub main'file
266 { 223 {
267 if ($main'mwerks) { push(@out,".section\t.text\n"); } 224 local $tmp;
268 else { 225 $tmp=<<___;
269 local $tmp=<<___;
270%ifdef __omf__ 226%ifdef __omf__
271section code use32 class=code 227section code use32 class=code
272%else 228%else
273section .text 229section .text
274%endif 230%endif
275___ 231___
276 push(@out,$tmp); 232 push(@out,$tmp);
277 }
278 } 233 }
279 234
280sub main'function_begin 235sub main'function_begin
@@ -282,10 +237,9 @@ sub main'function_begin
282 my($func,$extra)=@_; 237 my($func,$extra)=@_;
283 238
284 push(@labels,$func); 239 push(@labels,$func);
285 push(@out,".") if ($main'mwerks);
286 my($tmp)=<<"EOF"; 240 my($tmp)=<<"EOF";
287global $under$func 241global _$func
288$under$func: 242_$func:
289 push ebp 243 push ebp
290 push ebx 244 push ebx
291 push esi 245 push esi
@@ -298,10 +252,9 @@ EOF
298sub main'function_begin_B 252sub main'function_begin_B
299 { 253 {
300 my($func,$extra)=@_; 254 my($func,$extra)=@_;
301 push(@out,".") if ($main'mwerks);
302 my($tmp)=<<"EOF"; 255 my($tmp)=<<"EOF";
303global $under$func 256global _$func
304$under$func: 257_$func:
305EOF 258EOF
306 push(@out,$tmp); 259 push(@out,$tmp);
307 $stack=4; 260 $stack=4;
@@ -375,18 +328,11 @@ sub main'comment
375 } 328 }
376 } 329 }
377 330
378sub main'public_label
379 {
380 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
381 push(@out,".") if ($main'mwerks);
382 push(@out,"global\t$label{$_[0]}\n");
383 }
384
385sub main'label 331sub main'label
386 { 332 {
387 if (!defined($label{$_[0]})) 333 if (!defined($label{$_[0]}))
388 { 334 {
389 $label{$_[0]}="\@${label}${_[0]}"; 335 $label{$_[0]}="\$${label}${_[0]}";
390 $label++; 336 $label++;
391 } 337 }
392 return($label{$_[0]}); 338 return($label{$_[0]});
@@ -396,30 +342,15 @@ sub main'set_label
396 { 342 {
397 if (!defined($label{$_[0]})) 343 if (!defined($label{$_[0]}))
398 { 344 {
399 $label{$_[0]}="\@${label}${_[0]}"; 345 $label{$_[0]}="\$${label}${_[0]}";
400 $label++; 346 $label++;
401 } 347 }
402 if ($_[1]!=0 && $_[1]>1)
403 {
404 main'align($_[1]);
405 }
406 push(@out,"$label{$_[0]}:\n"); 348 push(@out,"$label{$_[0]}:\n");
407 } 349 }
408 350
409sub main'data_byte
410 {
411 push(@out,(($main'mwerks)?".byte\t":"DB\t").join(',',@_)."\n");
412 }
413
414sub main'data_word 351sub main'data_word
415 { 352 {
416 push(@out,(($main'mwerks)?".long\t":"DD\t").join(',',@_)."\n"); 353 push(@out,"\tDD\t$_[0]\n");
417 }
418
419sub main'align
420 {
421 push(@out,".") if ($main'mwerks);
422 push(@out,"align\t$_[0]\n");
423 } 354 }
424 355
425sub out1p 356sub out1p
@@ -427,7 +358,7 @@ sub out1p
427 my($name,$p1)=@_; 358 my($name,$p1)=@_;
428 my($l,$t); 359 my($l,$t);
429 360
430 push(@out,"\t$name\t".&conv($p1)."\n"); 361 push(@out,"\t$name\t ".&conv($p1)."\n");
431 } 362 }
432 363
433sub main'picmeup 364sub main'picmeup
@@ -437,19 +368,3 @@ sub main'picmeup
437 } 368 }
438 369
439sub main'blindpop { &out1("pop",@_); } 370sub main'blindpop { &out1("pop",@_); }
440
441sub main'initseg
442 {
443 local($f)=@_;
444 if ($main'win32)
445 {
446 local($tmp)=<<___;
447segment .CRT\$XCU data
448extern $under$f
449DD $under$f
450___
451 push(@out,$tmp);
452 }
453 }
454
4551;
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl
index a4c947165e..b61425e951 100644
--- a/src/lib/libcrypto/perlasm/x86unix.pl
+++ b/src/lib/libcrypto/perlasm/x86unix.pl
@@ -1,21 +1,26 @@
1#!/usr/local/bin/perl 1#!/usr/local/bin/perl
2 2
3package x86unix; # GAS actually... 3package x86unix;
4 4
5$label="L000"; 5$label="L000";
6$const=""; 6$const="";
7$constl=0; 7$constl=0;
8 8
9$align=($main'aout)?"4":"16"; 9$align=($main'aout)?"4":"16";
10$under=($main'aout or $main'coff)?"_":""; 10$under=($main'aout)?"_":"";
11$dot=($main'aout)?"":"."; 11$com_start=($main'sol)?"/":"#";
12$com_start="#" if ($main'aout or $main'coff);
13 12
14sub main'asm_init_output { @out=(); } 13sub main'asm_init_output { @out=(); }
15sub main'asm_get_output { return(@out); } 14sub main'asm_get_output { return(@out); }
16sub main'get_labels { return(@labels); } 15sub main'get_labels { return(@labels); }
17sub main'external_label { push(@labels,@_); } 16sub main'external_label { push(@labels,@_); }
18 17
18if ($main'openbsd)
19 {
20 $com_start='/*';
21 $com_end='*/';
22 }
23
19if ($main'cpp) 24if ($main'cpp)
20 { 25 {
21 $align="ALIGN"; 26 $align="ALIGN";
@@ -52,24 +57,6 @@ if ($main'cpp)
52 'edi', '%edi', 57 'edi', '%edi',
53 'ebp', '%ebp', 58 'ebp', '%ebp',
54 'esp', '%esp', 59 '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 ); 60 );
74 61
75%reg_val=( 62%reg_val=(
@@ -116,11 +103,6 @@ sub main'DWP
116 return($ret); 103 return($ret);
117 } 104 }
118 105
119sub main'QWP
120 {
121 return(&main'DWP(@_));
122 }
123
124sub main'BP 106sub main'BP
125 { 107 {
126 return(&main'DWP(@_)); 108 return(&main'DWP(@_));
@@ -164,14 +146,12 @@ sub main'xorb { &out2("xorb",@_); }
164sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); } 146sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); }
165sub main'adc { &out2("adcl",@_); } 147sub main'adc { &out2("adcl",@_); }
166sub main'sub { &out2("subl",@_); } 148sub main'sub { &out2("subl",@_); }
167sub main'sbb { &out2("sbbl",@_); }
168sub main'rotl { &out2("roll",@_); } 149sub main'rotl { &out2("roll",@_); }
169sub main'rotr { &out2("rorl",@_); } 150sub main'rotr { &out2("rorl",@_); }
170sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); } 151sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); }
171sub main'cmp { &out2("cmpl",@_); } 152sub main'cmp { &out2("cmpl",@_); }
172sub main'lea { &out2("leal",@_); } 153sub main'lea { &out2("leal",@_); }
173sub main'mul { &out1("mull",@_); } 154sub main'mul { &out1("mull",@_); }
174sub main'imul { &out2("imull",@_); }
175sub main'div { &out1("divl",@_); } 155sub main'div { &out1("divl",@_); }
176sub main'jmp { &out1("jmp",@_); } 156sub main'jmp { &out1("jmp",@_); }
177sub main'jmp_ptr { &out1p("jmp",@_); } 157sub main'jmp_ptr { &out1p("jmp",@_); }
@@ -193,48 +173,15 @@ sub main'dec { &out1("decl",@_); }
193sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); } 173sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
194sub main'push { &out1("pushl",@_); $stack+=4; } 174sub main'push { &out1("pushl",@_); $stack+=4; }
195sub main'pop { &out1("popl",@_); $stack-=4; } 175sub main'pop { &out1("popl",@_); $stack-=4; }
196sub main'pushf { &out0("pushfl"); $stack+=4; } 176sub main'pushf { &out0("pushf"); $stack+=4; }
197sub main'popf { &out0("popfl"); $stack-=4; } 177sub main'popf { &out0("popf"); $stack-=4; }
198sub main'not { &out1("notl",@_); } 178sub main'not { &out1("notl",@_); }
199sub main'call { my $pre=$under; 179sub main'call { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); }
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"); } 180sub main'ret { &out0("ret"); }
206sub main'nop { &out0("nop"); } 181sub main'nop { &out0("nop"); }
207sub main'test { &out2("testl",@_); } 182sub 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",@_); } 183sub main'movz { &out2("movzbl",@_); }
214sub main'neg { &out1("negl",@_); } 184sub 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 185
239# The bswapl instruction is new for the 486. Emulate if i386. 186# The bswapl instruction is new for the 486. Emulate if i386.
240sub main'bswap 187sub main'bswap
@@ -338,8 +285,13 @@ sub main'file
338 { 285 {
339 local($file)=@_; 286 local($file)=@_;
340 287
288 if ($main'openbsd)
289 { push(@out,"#include <machine/asm.h>\n"); return; }
290
341 local($tmp)=<<"EOF"; 291 local($tmp)=<<"EOF";
342 .file "$file.s" 292 .file "$file.s"
293 .version "01.01"
294gcc2_compiled.:
343EOF 295EOF
344 push(@out,$tmp); 296 push(@out,$tmp);
345 } 297 }
@@ -351,20 +303,22 @@ sub main'function_begin
351 &main'external_label($func); 303 &main'external_label($func);
352 $func=$under.$func; 304 $func=$under.$func;
353 305
306 if ($main'openbsd)
307 { push (@out, "\nENTRY($func)\n"); goto skip; }
308
354 local($tmp)=<<"EOF"; 309 local($tmp)=<<"EOF";
355.text 310.text
356.globl $func 311 .align $align
312.globl $func
357EOF 313EOF
358 push(@out,$tmp); 314 push(@out,$tmp);
359 if ($main'cpp) 315 if ($main'cpp)
360 { $tmp=push(@out,"TYPE($func,\@function)\n"); } 316 { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
361 elsif ($main'coff) 317 elsif ($main'gaswin)
362 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } 318 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
363 elsif ($main'aout and !$main'pic) 319 else { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
364 { }
365 else { $tmp=push(@out,".type\t$func,\@function\n"); }
366 push(@out,".align\t$align\n");
367 push(@out,"$func:\n"); 320 push(@out,"$func:\n");
321skip:
368 $tmp=<<"EOF"; 322 $tmp=<<"EOF";
369 pushl %ebp 323 pushl %ebp
370 pushl %ebx 324 pushl %ebx
@@ -383,20 +337,22 @@ sub main'function_begin_B
383 &main'external_label($func); 337 &main'external_label($func);
384 $func=$under.$func; 338 $func=$under.$func;
385 339
340 if ($main'openbsd)
341 { push(@out, "\nENTRY($func)\n"); goto skip; }
342
386 local($tmp)=<<"EOF"; 343 local($tmp)=<<"EOF";
387.text 344.text
388.globl $func 345 .align $align
346.globl $func
389EOF 347EOF
390 push(@out,$tmp); 348 push(@out,$tmp);
391 if ($main'cpp) 349 if ($main'cpp)
392 { push(@out,"TYPE($func,\@function)\n"); } 350 { push(@out,"\tTYPE($func,\@function)\n"); }
393 elsif ($main'coff) 351 elsif ($main'gaswin)
394 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } 352 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
395 elsif ($main'aout and !$main'pic) 353 else { push(@out,"\t.type $func,\@function\n"); }
396 { }
397 else { push(@out,".type $func,\@function\n"); }
398 push(@out,".align\t$align\n");
399 push(@out,"$func:\n"); 354 push(@out,"$func:\n");
355skip:
400 $stack=4; 356 $stack=4;
401 } 357 }
402 358
@@ -412,15 +368,15 @@ sub main'function_end
412 popl %ebx 368 popl %ebx
413 popl %ebp 369 popl %ebp
414 ret 370 ret
415${dot}L_${func}_end: 371.L_${func}_end:
416EOF 372EOF
417 push(@out,$tmp); 373 push(@out,$tmp);
418 374
419 if ($main'cpp) 375 if ($main'cpp)
420 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); } 376 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
421 elsif ($main'coff or $main'aout) 377 elsif ($main'gaswin)
422 { } 378 { $tmp=push(@out,"\t.align 4\n"); }
423 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); } 379 else { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
424 push(@out,".ident \"$func\"\n"); 380 push(@out,".ident \"$func\"\n");
425 $stack=0; 381 $stack=0;
426 %label=(); 382 %label=();
@@ -446,13 +402,13 @@ sub main'function_end_B
446 402
447 $func=$under.$func; 403 $func=$under.$func;
448 404
449 push(@out,"${dot}L_${func}_end:\n"); 405 push(@out,".L_${func}_end:\n");
450 if ($main'cpp) 406 if ($main'cpp)
451 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); } 407 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
452 elsif ($main'coff or $main'aout) 408 elsif ($main'gaswin)
453 { } 409 { push(@out,"\t.align 4\n"); }
454 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); } 410 else { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
455 push(@out,".ident \"$func\"\n"); 411 push(@out,".ident \"desasm.pl\"\n");
456 $stack=0; 412 $stack=0;
457 %label=(); 413 %label=();
458 } 414 }
@@ -493,10 +449,10 @@ sub main'swtmp
493 449
494sub main'comment 450sub main'comment
495 { 451 {
496 if (!defined($com_start) or $main'elf) 452 if (!$main'openbsd && $main'elf)
497 { # Regarding $main'elf above...
498 # GNU and SVR4 as'es use different comment delimiters, 453 # GNU and SVR4 as'es use different comment delimiters,
499 push(@out,"\n"); # so we just skip ELF comments... 454 { # so we just skip comments...
455 push(@out,"\n");
500 return; 456 return;
501 } 457 }
502 foreach (@_) 458 foreach (@_)
@@ -509,16 +465,16 @@ sub main'comment
509 } 465 }
510 466
511sub main'public_label 467sub main'public_label
512 { 468 {
513 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]})); 469 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
514 push(@out,".globl\t$label{$_[0]}\n"); 470 push(@out,".globl\t$label{$_[0]}\n");
515 } 471 }
516 472
517sub main'label 473sub main'label
518 { 474 {
519 if (!defined($label{$_[0]})) 475 if (!defined($label{$_[0]}))
520 { 476 {
521 $label{$_[0]}="${dot}${label}${_[0]}"; 477 $label{$_[0]}=".${label}${_[0]}";
522 $label++; 478 $label++;
523 } 479 }
524 return($label{$_[0]}); 480 return($label{$_[0]});
@@ -528,29 +484,18 @@ sub main'set_label
528 { 484 {
529 if (!defined($label{$_[0]})) 485 if (!defined($label{$_[0]}))
530 { 486 {
531 $label{$_[0]}="${dot}${label}${_[0]}"; 487 $label{$_[0]}=".${label}${_[0]}";
532 $label++; 488 $label++;
533 } 489 }
534 if ($_[1]!=0) 490 if ($main'openbsd)
535 { 491 { push(@out,"_ALIGN_TEXT\n") if ($_[1] != 0); }
536 if ($_[1]>1) { main'align($_[1]); } 492 else
537 else { push(@out,".align $align\n"); } 493 { push(@out,".align $align\n") if ($_[1] != 0); }
538 }
539 push(@out,"$label{$_[0]}:\n"); 494 push(@out,"$label{$_[0]}:\n");
540 } 495 }
541 496
542sub main'file_end 497sub main'file_end
543 { 498 {
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 "") 499 if ($const ne "")
555 { 500 {
556 push(@out,".section .rodata\n"); 501 push(@out,".section .rodata\n");
@@ -559,27 +504,11 @@ sub main'file_end
559 } 504 }
560 } 505 }
561 506
562sub main'data_byte
563 {
564 push(@out,"\t.byte\t".join(',',@_)."\n");
565 }
566
567sub main'data_word 507sub main'data_word
568 { 508 {
569 push(@out,"\t.long\t".join(',',@_)."\n"); 509 push(@out,"\t.long\t".join(',',@_)."\n");
570 } 510 }
571 511
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 512# debug output functions: puts, putx, printf
584 513
585sub main'puts 514sub main'puts
@@ -659,6 +588,7 @@ sub main'picmeup
659 { 588 {
660 local($tmp)=<<___; 589 local($tmp)=<<___;
661#if (defined(ELF) || defined(SOL)) && defined(PIC) 590#if (defined(ELF) || defined(SOL)) && defined(PIC)
591 .align 8
662 call 1f 592 call 1f
6631: popl $regs{$dst} 5931: popl $regs{$dst}
664 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst} 594 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
@@ -669,14 +599,25 @@ sub main'picmeup
669___ 599___
670 push(@out,$tmp); 600 push(@out,$tmp);
671 } 601 }
602 elsif ($main'openbsd)
603 {
604 push(@out, "#ifdef PIC\n");
605 push(@out, "\tPIC_PROLOGUE\n");
606 &main'mov($dst,"PIC_GOT($sym)");
607 push(@out, "\tPIC_EPILOGUE\n");
608 push(@out, "#else\n");
609 &main'lea($dst,&main'DWP($sym));
610 push(@out, "#endif\n");
611 }
672 elsif ($main'pic && ($main'elf || $main'aout)) 612 elsif ($main'pic && ($main'elf || $main'aout))
673 { 613 {
614 push(@out,"\t.align\t8\n");
674 &main'call(&main'label("PIC_me_up")); 615 &main'call(&main'label("PIC_me_up"));
675 &main'set_label("PIC_me_up"); 616 &main'set_label("PIC_me_up");
676 &main'blindpop($dst); 617 &main'blindpop($dst);
677 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-". 618 &main'add($dst,"\$$under"."_GLOBAL_OFFSET_TABLE_+[.-".
678 &main'label("PIC_me_up") . "]"); 619 &main'label("PIC_me_up") . "]");
679 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst)); 620 &main'mov($dst,&main'DWP($sym."\@GOT",$dst));
680 } 621 }
681 else 622 else
682 { 623 {
@@ -685,41 +626,3 @@ ___
685 } 626 }
686 627
687sub main'blindpop { &out1("popl",@_); } 628sub 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;