• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/openssl-0.9.8e/crypto/perlasm/
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'div	{ &out1("divl",@_); }
175sub main'jmp	{ &out1("jmp",@_); }
176sub main'jmp_ptr { &out1p("jmp",@_); }
177sub main'je	{ &out1("je",@_); }
178sub main'jle	{ &out1("jle",@_); }
179sub main'jne	{ &out1("jne",@_); }
180sub main'jnz	{ &out1("jnz",@_); }
181sub main'jz	{ &out1("jz",@_); }
182sub main'jge	{ &out1("jge",@_); }
183sub main'jl	{ &out1("jl",@_); }
184sub main'ja	{ &out1("ja",@_); }
185sub main'jae	{ &out1("jae",@_); }
186sub main'jb	{ &out1("jb",@_); }
187sub main'jbe	{ &out1("jbe",@_); }
188sub main'jc	{ &out1("jc",@_); }
189sub main'jnc	{ &out1("jnc",@_); }
190sub main'jno	{ &out1("jno",@_); }
191sub main'dec	{ &out1("decl",@_); }
192sub main'inc	{ &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
193sub main'push	{ &out1("pushl",@_); $stack+=4; }
194sub main'pop	{ &out1("popl",@_); $stack-=4; }
195sub main'pushf	{ &out0("pushfl"); $stack+=4; }
196sub main'popf	{ &out0("popfl"); $stack-=4; }
197sub main'not	{ &out1("notl",@_); }
198sub main'call	{	my $pre=$under;
199			foreach $i (%label)
200			{ if ($label{$i} eq $_[0]) { $pre=''; last; } }
201			&out1("call",$pre.$_[0]);
202		}
203sub main'call_ptr { &out1p("call",@_); }
204sub main'ret	{ &out0("ret"); }
205sub main'nop	{ &out0("nop"); }
206sub main'test	{ &out2("testl",@_); }
207sub main'bt	{ &out2("btl",@_); }
208sub main'leave	{ &out0("leave"); }
209sub main'cpuid	{ &out0(".byte\t0x0f,0xa2"); }
210sub main'rdtsc	{ &out0(".byte\t0x0f,0x31"); }
211sub main'halt	{ &out0("hlt"); }
212sub main'movz	{ &out2("movzbl",@_); }
213sub main'neg	{ &out1("negl",@_); }
214sub main'cld	{ &out0("cld"); }
215
216# SSE2
217sub main'emms	{ &out0("emms"); }
218sub main'movd	{ &out2("movd",@_); }
219sub main'movdqu	{ &out2("movdqu",@_); }
220sub main'movdqa	{ &out2("movdqa",@_); }
221sub main'movdq2q{ &out2("movdq2q",@_); }
222sub main'movq2dq{ &out2("movq2dq",@_); }
223sub main'paddq	{ &out2("paddq",@_); }
224sub main'pmuludq{ &out2("pmuludq",@_); }
225sub main'psrlq	{ &out2("psrlq",@_); }
226sub main'psllq	{ &out2("psllq",@_); }
227sub main'pxor	{ &out2("pxor",@_); }
228sub main'por	{ &out2("por",@_); }
229sub main'pand	{ &out2("pand",@_); }
230sub main'movq	{
231	local($p1,$p2,$optimize)=@_;
232	if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
233		# movq between mmx registers can sink Intel CPUs
234		{	push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n");	}
235	else	{	&out2("movq",@_);				}
236	}
237
238# The bswapl instruction is new for the 486. Emulate if i386.
239sub main'bswap
240	{
241	if ($main'i386)
242		{
243		&main'comment("bswapl @_");
244		&main'exch(main'HB(@_),main'LB(@_));
245		&main'rotr(@_,16);
246		&main'exch(main'HB(@_),main'LB(@_));
247		}
248	else
249		{
250		&out1("bswapl",@_);
251		}
252	}
253
254sub out2
255	{
256	local($name,$p1,$p2)=@_;
257	local($l,$ll,$t);
258	local(%special)=(	"roll",0xD1C0,"rorl",0xD1C8,
259				"rcll",0xD1D0,"rcrl",0xD1D8,
260				"shll",0xD1E0,"shrl",0xD1E8,
261				"sarl",0xD1F8);
262
263	if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
264		{
265		$op=$special{$name}|$reg_val{$p1};
266		$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
267		$tmp2=sprintf(".byte %d\t",$op     &0xff);
268		push(@out,$tmp1);
269		push(@out,$tmp2);
270
271		$p2=&conv($p2);
272		$p1=&conv($p1);
273		&main'comment("$name $p2 $p1");
274		return;
275		}
276
277	push(@out,"\t$name\t");
278	$t=&conv($p2).",";
279	$l=length($t);
280	push(@out,$t);
281	$ll=4-($l+9)/8;
282	$tmp1=sprintf("\t" x $ll);
283	push(@out,$tmp1);
284	push(@out,&conv($p1)."\n");
285	}
286
287sub out1
288	{
289	local($name,$p1)=@_;
290	local($l,$t);
291	local(%special)=("bswapl",0x0FC8);
292
293	if ((defined($special{$name})) && defined($regs{$p1}))
294		{
295		$op=$special{$name}|$reg_val{$p1};
296		$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
297		$tmp2=sprintf(".byte %d\t",$op     &0xff);
298		push(@out,$tmp1);
299		push(@out,$tmp2);
300
301		$p2=&conv($p2);
302		$p1=&conv($p1);
303		&main'comment("$name $p2 $p1");
304		return;
305		}
306
307	push(@out,"\t$name\t".&conv($p1)."\n");
308	}
309
310sub out1p
311	{
312	local($name,$p1)=@_;
313	local($l,$t);
314
315	push(@out,"\t$name\t*".&conv($p1)."\n");
316	}
317
318sub out0
319	{
320	push(@out,"\t$_[0]\n");
321	}
322
323sub conv
324	{
325	local($p)=@_;
326
327#	$p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
328
329	$p=$regs{$p} if (defined($regs{$p}));
330
331	$p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
332	$p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
333	return $p;
334	}
335
336sub main'file
337	{
338	local($file)=@_;
339
340	local($tmp)=<<"EOF";
341	.file	"$file.s"
342EOF
343	push(@out,$tmp);
344	}
345
346sub main'function_begin
347	{
348	local($func)=@_;
349
350	&main'external_label($func);
351	$func=$under.$func;
352
353	local($tmp)=<<"EOF";
354.text
355.globl	$func
356EOF
357	push(@out,$tmp);
358	if ($main'cpp)
359		{ $tmp=push(@out,"TYPE($func,\@function)\n"); }
360	elsif ($main'coff)
361		{ $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
362	elsif ($main'aout and !$main'pic)
363		{ }
364	else	{ $tmp=push(@out,".type\t$func,\@function\n"); }
365	push(@out,".align\t$align\n");
366	push(@out,"$func:\n");
367	$tmp=<<"EOF";
368	pushl	%ebp
369	pushl	%ebx
370	pushl	%esi
371	pushl	%edi
372
373EOF
374	push(@out,$tmp);
375	$stack=20;
376	}
377
378sub main'function_begin_B
379	{
380	local($func,$extra)=@_;
381
382	&main'external_label($func);
383	$func=$under.$func;
384
385	local($tmp)=<<"EOF";
386.text
387.globl	$func
388EOF
389	push(@out,$tmp);
390	if ($main'cpp)
391		{ push(@out,"TYPE($func,\@function)\n"); }
392	elsif ($main'coff)
393		{ $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
394	elsif ($main'aout and !$main'pic)
395		{ }
396	else	{ push(@out,".type	$func,\@function\n"); }
397	push(@out,".align\t$align\n");
398	push(@out,"$func:\n");
399	$stack=4;
400	}
401
402sub main'function_end
403	{
404	local($func)=@_;
405
406	$func=$under.$func;
407
408	local($tmp)=<<"EOF";
409	popl	%edi
410	popl	%esi
411	popl	%ebx
412	popl	%ebp
413	ret
414${dot}L_${func}_end:
415EOF
416	push(@out,$tmp);
417
418	if ($main'cpp)
419		{ push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
420	elsif ($main'coff or $main'aout)
421                { }
422	else	{ push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
423	push(@out,".ident	\"$func\"\n");
424	$stack=0;
425	%label=();
426	}
427
428sub main'function_end_A
429	{
430	local($func)=@_;
431
432	local($tmp)=<<"EOF";
433	popl	%edi
434	popl	%esi
435	popl	%ebx
436	popl	%ebp
437	ret
438EOF
439	push(@out,$tmp);
440	}
441
442sub main'function_end_B
443	{
444	local($func)=@_;
445
446	$func=$under.$func;
447
448	push(@out,"${dot}L_${func}_end:\n");
449	if ($main'cpp)
450		{ push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
451        elsif ($main'coff or $main'aout)
452                { }
453	else	{ push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
454	push(@out,".ident	\"$func\"\n");
455	$stack=0;
456	%label=();
457	}
458
459sub main'wparam
460	{
461	local($num)=@_;
462
463	return(&main'DWP($stack+$num*4,"esp","",0));
464	}
465
466sub main'stack_push
467	{
468	local($num)=@_;
469	$stack+=$num*4;
470	&main'sub("esp",$num*4);
471	}
472
473sub main'stack_pop
474	{
475	local($num)=@_;
476	$stack-=$num*4;
477	&main'add("esp",$num*4);
478	}
479
480sub main'swtmp
481	{
482	return(&main'DWP($_[0]*4,"esp","",0));
483	}
484
485# Should use swtmp, which is above esp.  Linix can trash the stack above esp
486#sub main'wtmp
487#	{
488#	local($num)=@_;
489#
490#	return(&main'DWP(-($num+1)*4,"esp","",0));
491#	}
492
493sub main'comment
494	{
495	if (!defined($com_start) or $main'elf)
496		{	# Regarding $main'elf above...
497			# GNU and SVR4 as'es use different comment delimiters,
498		push(@out,"\n");	# so we just skip ELF comments...
499		return;
500		}
501	foreach (@_)
502		{
503		if (/^\s*$/)
504			{ push(@out,"\n"); }
505		else
506			{ push(@out,"\t$com_start $_ $com_end\n"); }
507		}
508	}
509
510sub main'public_label
511	{
512	$label{$_[0]}="${under}${_[0]}"	if (!defined($label{$_[0]}));
513	push(@out,".globl\t$label{$_[0]}\n");
514	}
515
516sub main'label
517	{
518	if (!defined($label{$_[0]}))
519		{
520		$label{$_[0]}="${dot}${label}${_[0]}";
521		$label++;
522		}
523	return($label{$_[0]});
524	}
525
526sub main'set_label
527	{
528	if (!defined($label{$_[0]}))
529		{
530		$label{$_[0]}="${dot}${label}${_[0]}";
531		$label++;
532		}
533	if ($_[1]!=0)
534		{
535		if ($_[1]>1)	{ main'align($_[1]);		}
536		else		{ push(@out,".align $align\n");	}
537		}
538	push(@out,"$label{$_[0]}:\n");
539	}
540
541sub main'file_end
542	{
543	# try to detect if SSE2 or MMX extensions were used on ELF platform...
544	if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
545		local($tmp);
546
547		push (@out,"\n.section\t.bss\n");
548		push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
549
550		push (@out,".section\t.init\n");
551		# One can argue that it's wasteful to craft every
552		# SSE/MMX module with this snippet... Well, it's 72
553		# bytes long and for the moment we have two modules.
554		# Let's argue when we have 7 modules or so...
555		#
556		# $1<<10 sets a reserved bit to signal that variable
557		# was initialized already...
558		&main'picmeup("edx","OPENSSL_ia32cap_P");
559		$tmp=<<___;
560		cmpl	\$0,(%edx)
561		jne	1f
562		movl	\$1<<10,(%edx)
563		pushf
564		popl	%eax
565		movl	%eax,%ecx
566		xorl	\$1<<21,%eax
567		pushl	%eax
568		popf
569		pushf
570		popl	%eax
571		xorl	%ecx,%eax
572		btl	\$21,%eax
573		jnc	1f
574		pushl	%edi
575		pushl	%ebx
576		movl	%edx,%edi
577		movl	\$1,%eax
578		.byte	0x0f,0xa2
579		orl	\$1<<10,%edx
580		movl	%edx,0(%edi)
581		popl	%ebx
582		popl	%edi
583		jmp	1f
584	.align	$align
585	1:
586___
587		push (@out,$tmp);
588	}
589
590	if ($const ne "")
591		{
592		push(@out,".section .rodata\n");
593		push(@out,$const);
594		$const="";
595		}
596	}
597
598sub main'data_byte
599	{
600	push(@out,"\t.byte\t".join(',',@_)."\n");
601	}
602
603sub main'data_word
604	{
605	push(@out,"\t.long\t".join(',',@_)."\n");
606	}
607
608sub main'align
609	{
610	my $val=$_[0],$p2,$i;
611	if ($main'aout) {
612		for ($p2=0;$val!=0;$val>>=1) { $p2++; }
613		$val=$p2-1;
614		$val.=",0x90";
615	}
616	push(@out,".align\t$val\n");
617	}
618
619# debug output functions: puts, putx, printf
620
621sub main'puts
622	{
623	&pushvars();
624	&main'push('$Lstring' . ++$constl);
625	&main'call('puts');
626	$stack-=4;
627	&main'add("esp",4);
628	&popvars();
629
630	$const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
631	}
632
633sub main'putx
634	{
635	&pushvars();
636	&main'push($_[0]);
637	&main'push('$Lstring' . ++$constl);
638	&main'call('printf');
639	&main'add("esp",8);
640	$stack-=8;
641	&popvars();
642
643	$const .= "Lstring$constl:\n\t.string \"\%X\"\n";
644	}
645
646sub main'printf
647	{
648	$ostack = $stack;
649	&pushvars();
650	for ($i = @_ - 1; $i >= 0; $i--)
651		{
652		if ($i == 0) # change this to support %s format strings
653			{
654			&main'push('$Lstring' . ++$constl);
655			$const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
656			}
657		else
658			{
659			if ($_[$i] =~ /([0-9]*)\(%esp\)/)
660				{
661				&main'push(($1 + $stack - $ostack) . '(%esp)');
662				}
663			else
664				{
665				&main'push($_[$i]);
666				}
667			}
668		}
669	&main'call('printf');
670	$stack-=4*@_;
671	&main'add("esp",4*@_);
672	&popvars();
673	}
674
675sub pushvars
676	{
677	&main'pushf();
678	&main'push("edx");
679	&main'push("ecx");
680	&main'push("eax");
681	}
682
683sub popvars
684	{
685	&main'pop("eax");
686	&main'pop("ecx");
687	&main'pop("edx");
688	&main'popf();
689	}
690
691sub main'picmeup
692	{
693	local($dst,$sym)=@_;
694	if ($main'cpp)
695		{
696		local($tmp)=<<___;
697#if (defined(ELF) || defined(SOL)) && defined(PIC)
698	call	1f
6991:	popl	$regs{$dst}
700	addl	\$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
701	movl	$sym\@GOT($regs{$dst}),$regs{$dst}
702#else
703	leal	$sym,$regs{$dst}
704#endif
705___
706		push(@out,$tmp);
707		}
708	elsif ($main'pic && ($main'elf || $main'aout))
709		{
710		&main'call(&main'label("PIC_me_up"));
711		&main'set_label("PIC_me_up");
712		&main'blindpop($dst);
713		&main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
714				&main'label("PIC_me_up") . "]");
715		&main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
716		}
717	else
718		{
719		&main'lea($dst,&main'DWP($sym));
720		}
721	}
722
723sub main'blindpop { &out1("popl",@_); }
724
725sub main'initseg
726	{
727	local($f)=@_;
728	local($tmp);
729	if ($main'elf)
730		{
731		$tmp=<<___;
732.section	.init
733	call	$under$f
734	jmp	.Linitalign
735.align	$align
736.Linitalign:
737___
738		}
739	elsif ($main'coff)
740		{
741		$tmp=<<___;	# applies to both Cygwin and Mingw
742.section	.ctors
743.long	$under$f
744___
745		}
746	elsif ($main'aout)
747		{
748		local($ctor)="${under}_GLOBAL_\$I\$$f";
749		$tmp=".text\n";
750		$tmp.=".type	$ctor,\@function\n" if ($main'pic);
751		$tmp.=<<___;	# OpenBSD way...
752.globl	$ctor
753.align	2
754$ctor:
755	jmp	$under$f
756___
757		}
758	push(@out,$tmp) if ($tmp);
759	}
760
7611;
762