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