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 ($#_==1 && $opcode =~ m/^(call|clflush|j|loop|set)/o)
49					{ &::emit($opcode,@arg);	}
50    else				{ &::emit($opcode.$suffix,@arg);}
51
52  1;
53}
54#
55# opcodes not covered by ::generic above, mostly inconsistent namings...
56#
57sub ::movzx	{ &::movzb(@_);			}
58sub ::pushfd	{ &::pushfl;			}
59sub ::popfd	{ &::popfl;			}
60sub ::cpuid	{ &::emit(".byte\t0x0f,0xa2");	}
61sub ::rdtsc	{ &::emit(".byte\t0x0f,0x31");	}
62
63sub ::call	{ &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
64sub ::call_ptr	{ &::generic("call","*$_[0]");	}
65sub ::jmp_ptr	{ &::generic("jmp","*$_[0]");	}
66
67*::bswap = sub	{ &::emit("bswap","%$_[0]");	} if (!$::i386);
68
69sub ::DWP
70{ my($addr,$reg1,$reg2,$idx)=@_;
71  my $ret="";
72
73    if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
74
75    $addr =~ s/^\s+//;
76    # prepend global references with optional underscore
77    $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
78
79    $reg1 = "%$reg1" if ($reg1);
80    $reg2 = "%$reg2" if ($reg2);
81
82    $ret .= $addr if (($addr ne "") && ($addr ne 0));
83
84    if ($reg2)
85    {	$idx!= 0 or $idx=1;
86	$ret .= "($reg1,$reg2,$idx)";
87    }
88    elsif ($reg1)
89    {	$ret .= "($reg1)";	}
90
91  $ret;
92}
93sub ::QWP	{ &::DWP(@_);	}
94sub ::BP	{ &::DWP(@_);	}
95sub ::WP	{ &::DWP(@_);	}
96sub ::BC	{ @_;		}
97sub ::DWC	{ @_;		}
98
99sub ::file
100{   push(@out,".file\t\"$_[0].S\"\n.text\n");	}
101
102sub ::function_begin_B
103{ my $func=shift;
104  my $global=($func !~ /^_/);
105  my $begin="${::lbdecor}_${func}_begin";
106
107    &::LABEL($func,$global?"$begin":"$nmdecor$func");
108    $func=$nmdecor.$func;
109
110    push(@out,".globl\t$func\n")	if ($global);
111    if ($::macosx) {
112      push(@out,".private_extern\t$func\n");
113    } else {
114      push(@out,".hidden\t$func\n");
115    }
116    if ($::coff)
117    {	push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
118    elsif (($::aout and !$::pic) or $::macosx)
119    { }
120    else
121    {	push(@out,".type	$func,\@function\n"); }
122    push(@out,".align\t$align\n");
123    push(@out,"$func:\n");
124    push(@out,"$begin:\n")		if ($global);
125    $::stack=4;
126}
127
128sub ::function_end_B
129{ my $func=shift;
130    push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
131    $::stack=0;
132    &::wipe_labels();
133}
134
135sub ::comment
136	{
137	if (!defined($com_start) or $::elf)
138		{	# Regarding $::elf above...
139			# GNU and SVR4 as'es use different comment delimiters,
140		push(@out,"\n");	# so we just skip ELF comments...
141		return;
142		}
143	foreach (@_)
144		{
145		if (/^\s*$/)
146			{ push(@out,"\n"); }
147		else
148			{ push(@out,"\t$com_start $_ $com_end\n"); }
149		}
150	}
151
152sub ::external_label
153{   foreach(@_) { &::LABEL($_,$nmdecor.$_); }   }
154
155sub ::public_label
156{   push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n");   }
157
158sub ::file_end
159{   if ($::macosx)
160    {	if (%non_lazy_ptr)
161    	{   push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
162	    foreach $i (keys %non_lazy_ptr)
163	    {	push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n");   }
164	}
165    }
166    if (0 && grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
167	my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,16";
168	if ($::macosx)	{ push (@out,"$tmp,2\n"); }
169	elsif ($::elf)	{ push (@out,"$tmp,4\n"); }
170	else		{ push (@out,"$tmp\n"); }
171    }
172    push(@out,$initseg) if ($initseg);
173}
174
175sub ::data_byte	{   push(@out,".byte\t".join(',',@_)."\n");   }
176sub ::data_short{   push(@out,".value\t".join(',',@_)."\n");  }
177sub ::data_word {   push(@out,".long\t".join(',',@_)."\n");   }
178
179sub ::align
180{ my $val=$_[0];
181    if ($::aout)
182    {	$val=int(log($val)/log(2));
183	$val.=",0x90";
184    }
185    push(@out,".align\t$val\n");
186}
187
188sub ::picmeup
189{ my($dst,$sym,$base,$reflabel)=@_;
190
191    if (($::pic && ($::elf || $::aout)) || $::macosx)
192    {	if (!defined($base))
193	{   &::call(&::label("PIC_me_up"));
194	    &::set_label("PIC_me_up");
195	    &::blindpop($dst);
196	    $base=$dst;
197	    $reflabel=&::label("PIC_me_up");
198	}
199	if ($::macosx)
200	{   my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
201	    &::mov($dst,&::DWP("$indirect-$reflabel",$base));
202	    $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
203	}
204	elsif ($sym eq "OPENSSL_ia32cap_P" && $::elf>0)
205	{   &::lea($dst,&::DWP("$sym-$reflabel",$base));   }
206	else
207	{   &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
208			    $base));
209	    &::mov($dst,&::DWP("$sym\@GOT",$dst));
210	}
211    }
212    else
213    {	&::lea($dst,&::DWP($sym));	}
214}
215
216sub ::initseg
217{ my $f=$nmdecor.shift;
218
219    if ($::android)
220    {	$initseg.=<<___;
221.section	.init_array
222.align	4
223.long	$f
224___
225    }
226    elsif ($::elf)
227    {	$initseg.=<<___;
228.section	.init
229	call	$f
230___
231    }
232    elsif ($::coff)
233    {   $initseg.=<<___;	# applies to both Cygwin and Mingw
234.section	.ctors
235.long	$f
236___
237    }
238    elsif ($::macosx)
239    {	$initseg.=<<___;
240.mod_init_func
241.align 2
242.long   $f
243___
244    }
245    elsif ($::aout)
246    {	my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
247	$initseg.=".text\n";
248	$initseg.=".type	$ctor,\@function\n" if ($::pic);
249	$initseg.=<<___;	# OpenBSD way...
250.globl	$ctor
251.align	2
252$ctor:
253	jmp	$f
254___
255    }
256}
257
258sub ::dataseg
259{   push(@out,".data\n");   }
260
261*::hidden = sub { push(@out,".hidden\t$nmdecor$_[0]\n"); } if ($::elf);
262
2631;
264