blob: 5c7ea3880e4d3965c6864c07ba5d940a39a4917d [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#! /usr/bin/env perl
2# Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the OpenSSL license (the "License"). You may not use
5# this file except in compliance with the License. You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9
10package x86gas;
11
12*out=\@::out;
13
14$::lbdecor=$::aout?"L":".L"; # local label decoration
15$nmdecor=($::aout or $::coff)?"_":""; # external name decoration
16
17$initseg="";
18
19$align=16;
20$align=log($align)/log(2) if ($::aout);
21$com_start="#" if ($::aout or $::coff);
22
23sub opsize()
24{ my $reg=shift;
25 if ($reg =~ m/^%e/o) { "l"; }
26 elsif ($reg =~ m/^%[a-d][hl]$/o) { "b"; }
27 elsif ($reg =~ m/^%[yxm]/o) { undef; }
28 else { "w"; }
29}
30
31# swap arguments;
32# expand opcode with size suffix;
33# prefix numeric constants with $;
34sub ::generic
35{ my($opcode,@arg)=@_;
36 my($suffix,$dst,$src);
37
38 @arg=reverse(@arg);
39
40 for (@arg)
41 { s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o; # gp registers
42 s/^([xy]?mm[0-7])$/%$1/o; # xmm/mmx registers
43 s/^(\-?[0-9]+)$/\$$1/o; # constants
44 s/^(\-?0x[0-9a-f]+)$/\$$1/o; # constants
45 }
46
47 $dst = $arg[$#arg] if ($#arg>=0);
48 $src = $arg[$#arg-1] if ($#arg>=1);
49 if ($dst =~ m/^%/o) { $suffix=&opsize($dst); }
50 elsif ($src =~ m/^%/o) { $suffix=&opsize($src); }
51 else { $suffix="l"; }
52 undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o);
53
54 if ($#_==0) { &::emit($opcode); }
55 elsif ($#_==1 && $opcode =~ m/^(call|clflush|j|loop|set)/o)
56 { &::emit($opcode,@arg); }
57 else { &::emit($opcode.$suffix,@arg);}
58
59 1;
60}
61#
62# opcodes not covered by ::generic above, mostly inconsistent namings...
63#
64sub ::movzx { &::movzb(@_); }
65sub ::pushfd { &::pushfl; }
66sub ::popfd { &::popfl; }
67sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); }
68sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); }
69
70sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
71sub ::call_ptr { &::generic("call","*$_[0]"); }
72sub ::jmp_ptr { &::generic("jmp","*$_[0]"); }
73
74*::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386);
75
76sub ::DWP
77{ my($addr,$reg1,$reg2,$idx)=@_;
78 my $ret="";
79
80 if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
81
82 $addr =~ s/^\s+//;
83 # prepend global references with optional underscore
84 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
85
86 $reg1 = "%$reg1" if ($reg1);
87 $reg2 = "%$reg2" if ($reg2);
88
89 $ret .= $addr if (($addr ne "") && ($addr ne 0));
90
91 if ($reg2)
92 { $idx!= 0 or $idx=1;
93 $ret .= "($reg1,$reg2,$idx)";
94 }
95 elsif ($reg1)
96 { $ret .= "($reg1)"; }
97
98 $ret;
99}
100sub ::QWP { &::DWP(@_); }
101sub ::BP { &::DWP(@_); }
102sub ::WP { &::DWP(@_); }
103sub ::BC { @_; }
104sub ::DWC { @_; }
105
106sub ::file
107{ push(@out,".text\n"); }
108
109sub ::function_begin_B
110{ my $func=shift;
111 my $global=($func !~ /^_/);
112 my $begin="${::lbdecor}_${func}_begin";
113
114 &::LABEL($func,$global?"$begin":"$nmdecor$func");
115 $func=$nmdecor.$func;
116
117 push(@out,".globl\t$func\n") if ($global);
118 if ($::coff)
119 { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
120 elsif (($::aout and !$::pic) or $::macosx)
121 { }
122 else
123 { push(@out,".type $func,\@function\n"); }
124 push(@out,".align\t$align\n");
125 push(@out,"$func:\n");
126 push(@out,"$begin:\n") if ($global);
127 $::stack=4;
128}
129
130sub ::function_end_B
131{ my $func=shift;
132 push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
133 $::stack=0;
134 &::wipe_labels();
135}
136
137sub ::comment
138 {
139 if (!defined($com_start) or $::elf)
140 { # Regarding $::elf above...
141 # GNU and SVR4 as'es use different comment delimiters,
142 push(@out,"\n"); # so we just skip ELF comments...
143 return;
144 }
145 foreach (@_)
146 {
147 if (/^\s*$/)
148 { push(@out,"\n"); }
149 else
150 { push(@out,"\t$com_start $_ $com_end\n"); }
151 }
152 }
153
154sub ::external_label
155{ foreach(@_) { &::LABEL($_,$nmdecor.$_); } }
156
157sub ::public_label
158{ push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
159
160sub ::file_end
161{ if ($::macosx)
162 { if (%non_lazy_ptr)
163 { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
164 foreach $i (keys %non_lazy_ptr)
165 { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); }
166 }
167 }
168 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
169 my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,16";
170 if ($::macosx) { push (@out,"$tmp,2\n"); }
171 elsif ($::elf) { push (@out,"$tmp,4\n"); }
172 else { push (@out,"$tmp\n"); }
173 }
174 push(@out,$initseg) if ($initseg);
175}
176
177sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); }
178sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); }
179sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); }
180
181sub ::align
182{ my $val=$_[0];
183 if ($::aout)
184 { $val=int(log($val)/log(2));
185 $val.=",0x90";
186 }
187 push(@out,".align\t$val\n");
188}
189
190sub ::picmeup
191{ my($dst,$sym,$base,$reflabel)=@_;
192
193 if (($::pic && ($::elf || $::aout)) || $::macosx)
194 { if (!defined($base))
195 { &::call(&::label("PIC_me_up"));
196 &::set_label("PIC_me_up");
197 &::blindpop($dst);
198 $base=$dst;
199 $reflabel=&::label("PIC_me_up");
200 }
201 if ($::macosx)
202 { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
203 &::mov($dst,&::DWP("$indirect-$reflabel",$base));
204 $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
205 }
206 elsif ($sym eq "OPENSSL_ia32cap_P" && $::elf>0)
207 { &::lea($dst,&::DWP("$sym-$reflabel",$base)); }
208 else
209 { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
210 $base));
211 &::mov($dst,&::DWP("$sym\@GOT",$dst));
212 }
213 }
214 else
215 { &::lea($dst,&::DWP($sym)); }
216}
217
218sub ::initseg
219{ my $f=$nmdecor.shift;
220
221 if ($::android)
222 { $initseg.=<<___;
223.section .init_array
224.align 4
225.long $f
226___
227 }
228 elsif ($::elf)
229 { $initseg.=<<___;
230.section .init
231 call $f
232___
233 }
234 elsif ($::coff)
235 { $initseg.=<<___; # applies to both Cygwin and Mingw
236.section .ctors
237.long $f
238___
239 }
240 elsif ($::macosx)
241 { $initseg.=<<___;
242.mod_init_func
243.align 2
244.long $f
245___
246 }
247 elsif ($::aout)
248 { my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
249 $initseg.=".text\n";
250 $initseg.=".type $ctor,\@function\n" if ($::pic);
251 $initseg.=<<___; # OpenBSD way...
252.globl $ctor
253.align 2
254$ctor:
255 jmp $f
256___
257 }
258}
259
260sub ::dataseg
261{ push(@out,".data\n"); }
262
263*::hidden = sub { push(@out,".hidden\t$nmdecor$_[0]\n"); } if ($::elf);
264
2651;