1#! /usr/bin/env perl
2# Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17package x86nasm;
18
19*out=\@::out;
20
21$::lbdecor="L\$";		# local label decoration
22$nmdecor="_";			# external name decoration
23$drdecor=$::mwerks?".":"";	# directive decoration
24
25$initseg="";
26
27sub ::generic
28{ my $opcode=shift;
29  my $tmp;
30
31    if (!$::mwerks)
32    {   if    ($opcode =~ m/^j/o && $#_==0) # optimize jumps
33	{   $_[0] = "NEAR $_[0]";   	}
34	elsif ($opcode eq "lea" && $#_==1)  # wipe storage qualifier from lea
35	{   $_[1] =~ s/^[^\[]*\[/\[/o;	}
36	elsif ($opcode eq "clflush" && $#_==0)
37	{   $_[0] =~ s/^[^\[]*\[/\[/o;	}
38    }
39    &::emit($opcode,@_);
40  1;
41}
42#
43# opcodes not covered by ::generic above, mostly inconsistent namings...
44#
45sub ::call	{ &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
46sub ::call_ptr	{ &::emit("call",@_);	}
47sub ::jmp_ptr	{ &::emit("jmp",@_);	}
48
49sub get_mem
50{ my($size,$addr,$reg1,$reg2,$idx)=@_;
51  my($post,$ret);
52
53    if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
54
55    if ($size ne "")
56    {	$ret .= "$size";
57	$ret .= " PTR" if ($::mwerks);
58	$ret .= " ";
59    }
60    $ret .= "[";
61
62    $addr =~ s/^\s+//;
63    # prepend global references with optional underscore
64    $addr =~ s/^([^\+\-0-9][^\+\-]*)/::islabel($1) or "$nmdecor$1"/ige;
65    # put address arithmetic expression in parenthesis
66    $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
67
68    if (($addr ne "") && ($addr ne 0))
69    {	if ($addr !~ /^-/)	{ $ret .= "$addr+"; }
70	else			{ $post=$addr;      }
71    }
72
73    if ($reg2 ne "")
74    {	$idx!=0 or $idx=1;
75	$ret .= "$reg2*$idx";
76	$ret .= "+$reg1" if ($reg1 ne "");
77    }
78    else
79    {	$ret .= "$reg1";   }
80
81    $ret .= "$post]";
82    $ret =~ s/\+\]/]/; # in case $addr was the only argument
83
84  $ret;
85}
86sub ::BP	{ &get_mem("BYTE",@_);  }
87sub ::DWP	{ &get_mem("DWORD",@_); }
88sub ::WP	{ &get_mem("WORD",@_);	}
89sub ::QWP	{ &get_mem("",@_);      }
90sub ::BC	{ (($::mwerks)?"":"BYTE ")."@_";  }
91sub ::DWC	{ (($::mwerks)?"":"DWORD ")."@_"; }
92
93sub ::file
94{   if ($::mwerks)	{ push(@out,".section\t.text,64\n"); }
95    else
96    { my $tmp=<<___;
97%ifidn __OUTPUT_FORMAT__,obj
98section	code	use32 class=code align=64
99%elifidn __OUTPUT_FORMAT__,win32
100\$\@feat.00 equ 1
101section	.text	code align=64
102%else
103section	.text	code
104%endif
105___
106	push(@out,$tmp);
107    }
108}
109
110sub ::function_begin_B
111{ my $func=shift;
112  my $global=($func !~ /^_/);
113  my $begin="${::lbdecor}_${func}_begin";
114
115    $begin =~ s/^\@/./ if ($::mwerks);	# the torture never stops
116
117    &::LABEL($func,$global?"$begin":"$nmdecor$func");
118    $func=$nmdecor.$func;
119
120    push(@out,"${drdecor}global	$func\n")	if ($global);
121    push(@out,"${drdecor}align	16\n");
122    push(@out,"$func:\n");
123    push(@out,"$begin:\n")			if ($global);
124    $::stack=4;
125}
126
127sub ::function_end_B
128{   $::stack=0;
129    &::wipe_labels();
130}
131
132sub ::file_end
133{   if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
134    {	my $comm=<<___;
135${drdecor}segment	.bss
136${drdecor}common	${nmdecor}OPENSSL_ia32cap_P 16
137___
138	# comment out OPENSSL_ia32cap_P declarations
139	grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
140	push (@out,$comm)
141    }
142    push (@out,$initseg) if ($initseg);
143}
144
145sub ::comment {   foreach (@_) { push(@out,"\t; $_\n"); }   }
146
147sub ::external_label
148{   foreach(@_)
149    {	push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n");   }
150}
151
152sub ::public_label
153{   push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n");  }
154
155sub ::data_byte
156{   push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n");	}
157sub ::data_short
158{   push(@out,(($::mwerks)?".word\t":"dw\t").join(',',@_)."\n");	}
159sub ::data_word
160{   push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n");	}
161
162sub ::align
163{   push(@out,"${drdecor}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    if ($::win32)
173    {	$initseg=<<___;
174segment	.CRT\$XCU data align=4
175extern	$f
176dd	$f
177___
178    }
179}
180
181sub ::dataseg
182{   if ($mwerks)	{ push(@out,".section\t.data,4\n");   }
183    else		{ push(@out,"section\t.data align=4\n"); }
184}
185
186sub ::safeseh
187{ my $nm=shift;
188    push(@out,"%if	__NASM_VERSION_ID__ >= 0x02030000\n");
189    push(@out,"safeseh	".&::LABEL($nm,$nmdecor.$nm)."\n");
190    push(@out,"%endif\n");
191}
192
193sub ::preprocessor_ifdef
194{ my($define)=@_;
195    push(@out,"%ifdef ${define}\n");
196}
197
198sub ::preprocessor_endif
199{ push(@out,"%endif\n");    }
200
2011;
202