| # 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 |
| 17 | package x86masm; |
| 18 |
| 19 | *out=\@::out; |
| 20 |
| 21 | $::lbdecor="\$L"; # local label decoration |
| 22 | $nmdecor="_"; # external name decoration |
| 23 |
| 24 | $initseg=""; |
| 25 | $segment=""; |
| 26 |
| 27 | sub ::generic |
| 28 | { my ($opcode,@arg)=@_; |
| 29 |
| 30 | # fix hexadecimal constants |
| 31 | for (@arg) { s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/oi; } |
| 32 |
| 33 | if ($opcode =~ /lea/ && @arg[1] =~ s/.*PTR\s+(\(.*\))$/OFFSET $1/) # no [] |
| 34 | { $opcode="mov"; } |
| 35 | elsif ($opcode !~ /mov[dq]$/) |
| 36 | { # fix xmm references |
| 37 | $arg[0] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[-1]=~/\bxmm[0-7]\b/i); |
| 38 | $arg[-1] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[0]=~/\bxmm[0-7]\b/i); |
| 39 | } |
| 40 |
| 41 | &::emit($opcode,@arg); |
| 42 | 1; |
| 43 | } |
| 44 | # |
| 45 | # opcodes not covered by ::generic above, mostly inconsistent namings... |
| 46 | # |
| 47 | sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); } |
| 48 | sub ::call_ptr { &::emit("call",@_); } |
| 49 | sub ::jmp_ptr { &::emit("jmp",@_); } |
| 50 | sub ::lock { &::data_byte(0xf0); } |
| 51 |
| 52 | sub get_mem |
| 53 | { my($size,$addr,$reg1,$reg2,$idx)=@_; |
| 54 | my($post,$ret); |
| 55 |
| 56 | if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; } |
| 57 |
| 58 | $ret .= "$size PTR " if ($size ne ""); |
| 59 |
| 60 | $addr =~ s/^\s+//; |
| 61 | # prepend global references with optional underscore |
| 62 | $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige; |
| 63 | # put address arithmetic expression in parenthesis |
| 64 | $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/); |
| 65 |
| 66 | if (($addr ne "") && ($addr ne 0)) |
| 67 | { if ($addr !~ /^-/) { $ret .= "$addr"; } |
| 68 | else { $post=$addr; } |
| 69 | } |
| 70 | $ret .= "["; |
| 71 |
| 72 | if ($reg2 ne "") |
| 73 | { $idx!=0 or $idx=1; |
| 74 | $ret .= "$reg2*$idx"; |
| 75 | $ret .= "+$reg1" if ($reg1 ne ""); |
| 76 | } |
| 77 | else |
| 78 | { $ret .= "$reg1"; } |
| 79 |
| 80 | $ret .= "$post]"; |
| 81 | $ret =~ s/\+\]/]/; # in case $addr was the only argument |
| 82 | $ret =~ s/\[\s*\]//; |
| 83 |
| 84 | $ret; |
| 85 | } |
| 86 | sub ::BP { &get_mem("BYTE",@_); } |
| 87 | sub ::WP { &get_mem("WORD",@_); } |
| 88 | sub ::DWP { &get_mem("DWORD",@_); } |
| 89 | sub ::QWP { &get_mem("QWORD",@_); } |
| 90 | sub ::BC { "@_"; } |
| 91 | sub ::DWC { "@_"; } |
| 92 |
| 93 | sub ::file |
| 94 | { my $tmp=<<___; |
| 95 | IF \@Version LT 800 |
| 96 | ECHO MASM version 8.00 or later is strongly recommended. |
| 97 | ENDIF |
| 98 | .686 |
| 99 | .MODEL FLAT |
| 100 | OPTION DOTNAME |
| 101 | IF \@Version LT 800 |
| 102 | .text\$ SEGMENT PAGE 'CODE' |
| 103 | ELSE |
| 104 | .text\$ SEGMENT ALIGN(64) 'CODE' |
| 105 | ENDIF |
| 106 | ___ |
| 107 | push(@out,$tmp); |
| 108 | $segment = ".text\$"; |
| 109 | } |
| 110 |
| 111 | sub ::function_begin_B |
| 112 | { my $func=shift; |
| 113 | my $global=($func !~ /^_/); |
| 114 | my $begin="${::lbdecor}_${func}_begin"; |
| 115 |
| 116 | &::LABEL($func,$global?"$begin":"$nmdecor$func"); |
| 117 | $func="ALIGN\t16\n".$nmdecor.$func."\tPROC"; |
| 118 |
| 119 | if ($global) { $func.=" PUBLIC\n${begin}::\n"; } |
| 120 | else { $func.=" PRIVATE\n"; } |
| 121 | push(@out,$func); |
| 122 | $::stack=4; |
| 123 | } |
| 124 | sub ::function_end_B |
| 125 | { my $func=shift; |
| 126 |
| 127 | push(@out,"$nmdecor$func ENDP\n"); |
| 128 | $::stack=0; |
| 129 | &::wipe_labels(); |
| 130 | } |
| 131 |
| 132 | sub ::file_end |
| 133 | { my $xmmheader=<<___; |
| 134 | .686 |
| 135 | .XMM |
| 136 | IF \@Version LT 800 |
| 137 | XMMWORD STRUCT 16 |
| 138 | DQ 2 dup (?) |
| 139 | XMMWORD ENDS |
| 140 | ENDIF |
| 141 | ___ |
| 142 | if (grep {/\b[x]?mm[0-7]\b/i} @out) { |
| 143 | grep {s/\.[3-7]86/$xmmheader/} @out; |
| 144 | } |
| 145 |
| 146 | push(@out,"$segment ENDS\n"); |
| 147 |
| 148 | if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) |
| 149 | { my $comm=<<___; |
| 150 | .bss SEGMENT 'BSS' |
| 151 | COMM ${nmdecor}OPENSSL_ia32cap_P:DWORD:4 |
| 152 | .bss ENDS |
| 153 | ___ |
| 154 | # comment out OPENSSL_ia32cap_P declarations |
| 155 | grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out; |
| 156 | push (@out,$comm); |
| 157 | } |
| 158 | push (@out,$initseg) if ($initseg); |
| 159 | push (@out,"END\n"); |
| 160 | } |
| 161 |
| 162 | sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } } |
| 163 |
| 164 | *::set_label_B = sub |
| 165 | { my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); }; |
| 166 |
| 167 | sub ::external_label |
| 168 | { foreach(@_) |
| 169 | { push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n"); } |
| 170 | } |
| 171 |
| 172 | sub ::public_label |
| 173 | { push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); } |
| 174 |
| 175 | sub ::data_byte |
| 176 | { push(@out,("DB\t").join(',',splice(@_,0,16))."\n") while(@_); } |
| 177 |
| 178 | sub ::data_short |
| 179 | { push(@out,("DW\t").join(',',splice(@_,0,8))."\n") while(@_); } |
| 180 |
| 181 | sub ::data_word |
| 182 | { push(@out,("DD\t").join(',',splice(@_,0,4))."\n") while(@_); } |
| 183 |
| 184 | sub ::align |
| 185 | { push(@out,"ALIGN\t$_[0]\n"); } |
| 186 |
| 187 | sub ::picmeup |
| 188 | { my($dst,$sym)=@_; |
| 189 | &::lea($dst,&::DWP($sym)); |
| 190 | } |
| 191 |
| 192 | sub ::initseg |
| 193 | { my $f=$nmdecor.shift; |
| 194 |
| 195 | $initseg.=<<___; |
| 196 | .CRT\$XCU SEGMENT DWORD PUBLIC 'DATA' |
| 197 | EXTERN $f:NEAR |
| 198 | DD $f |
| 199 | .CRT\$XCU ENDS |
| 200 | ___ |
| 201 | } |
| 202 |
| 203 | sub ::dataseg |
| 204 | { push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA"; } |
| 205 |
| 206 | sub ::safeseh |
| 207 | { my $nm=shift; |
| 208 | push(@out,"IF \@Version GE 710\n"); |
| 209 | push(@out,".SAFESEH ".&::LABEL($nm,$nmdecor.$nm)."\n"); |
| 210 | push(@out,"ENDIF\n"); |
| 211 | } |
| 212 |
| 213 | sub ::preprocessor_ifdef |
| 214 | { my($define)=@_; |
| 215 | push(@out,"%ifdef ${define}\n"); |
| 216 | } |
| 217 |
| 218 | sub ::preprocessor_endif |
| 219 | { push(@out,"%endif\n"); } |
| 220 |
| 221 | 1; |
| 222 |