Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/perlasm/x86nasm.pl

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