2 # Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
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
14 $::lbdecor="\$L"; # local label decoration
15 $nmdecor="_"; # external name decoration
21 { my ($opcode,@arg)=@_;
23 # fix hexadecimal constants
24 for (@arg) { s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/oi; }
26 if ($opcode =~ /lea/ && @arg[1] =~ s/.*PTR\s+(\(.*\))$/OFFSET $1/) # no []
28 elsif ($opcode !~ /mov[dq]$/)
29 { # fix xmm references
30 $arg[0] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[-1]=~/\bxmm[0-7]\b/i);
31 $arg[-1] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[0]=~/\bxmm[0-7]\b/i);
34 &::emit($opcode,@arg);
38 # opcodes not covered by ::generic above, mostly inconsistent namings...
40 sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
41 sub ::call_ptr { &::emit("call",@_); }
42 sub ::jmp_ptr { &::emit("jmp",@_); }
43 sub ::lock { &::data_byte(0xf0); }
46 { my($size,$addr,$reg1,$reg2,$idx)=@_;
49 if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
51 $ret .= "$size PTR " if ($size ne "");
54 # prepend global references with optional underscore
55 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
56 # put address arithmetic expression in parenthesis
57 $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
59 if (($addr ne "") && ($addr ne 0))
60 { if ($addr !~ /^-/) { $ret .= "$addr"; }
68 $ret .= "+$reg1" if ($reg1 ne "");
74 $ret =~ s/\+\]/]/; # in case $addr was the only argument
79 sub ::BP { &get_mem("BYTE",@_); }
80 sub ::WP { &get_mem("WORD",@_); }
81 sub ::DWP { &get_mem("DWORD",@_); }
82 sub ::QWP { &get_mem("QWORD",@_); }
90 ECHO MASM version 8.00 or later is strongly recommended.
96 .text\$ SEGMENT PAGE 'CODE'
98 .text\$ SEGMENT ALIGN(64) 'CODE'
102 $segment = ".text\$";
105 sub ::function_begin_B
107 my $global=($func !~ /^_/);
108 my $begin="${::lbdecor}_${func}_begin";
110 &::LABEL($func,$global?"$begin":"$nmdecor$func");
111 $func="ALIGN\t16\n".$nmdecor.$func."\tPROC";
113 if ($global) { $func.=" PUBLIC\n${begin}::\n"; }
114 else { $func.=" PRIVATE\n"; }
121 push(@out,"$nmdecor$func ENDP\n");
127 { my $xmmheader=<<___;
136 if (grep {/\b[x]?mm[0-7]\b/i} @out) {
137 grep {s/\.[3-7]86/$xmmheader/} @out;
140 push(@out,"$segment ENDS\n");
142 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
145 COMM ${nmdecor}OPENSSL_ia32cap_P:DWORD:4
148 # comment out OPENSSL_ia32cap_P declarations
149 grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
152 push (@out,$initseg) if ($initseg);
156 sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
159 { my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); };
163 { push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n"); }
167 { push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
170 { push(@out,("DB\t").join(',',splice(@_,0,16))."\n") while(@_); }
173 { push(@out,("DW\t").join(',',splice(@_,0,8))."\n") while(@_); }
176 { push(@out,("DD\t").join(',',splice(@_,0,4))."\n") while(@_); }
179 { push(@out,"ALIGN\t$_[0]\n"); }
183 &::lea($dst,&::DWP($sym));
187 { my $f=$nmdecor.shift;
190 .CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
198 { push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA"; }
202 push(@out,"IF \@Version GE 710\n");
203 push(@out,".SAFESEH ".&::LABEL($nm,$nmdecor.$nm)."\n");
204 push(@out,"ENDIF\n");