blob: 2dcd3f79f6ed23fcd2f52fd0f987f0f4fa2a7c96 [file] [log] [blame]
Rich Salze0a65192016-04-19 22:10:43 -04001#! /usr/bin/env perl
2# Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
3#
Richard Levittea5d95492018-12-06 13:48:38 +01004# Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salze0a65192016-04-19 22:10:43 -04005# 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
Andy Polyakov43d8f272007-12-18 09:18:49 +00009
10package x86masm;
11
12*out=\@::out;
13
14$::lbdecor="\$L"; # local label decoration
15$nmdecor="_"; # external name decoration
16
17$initseg="";
Andy Polyakovb94551e2008-07-22 08:44:31 +000018$segment="";
Andy Polyakov43d8f272007-12-18 09:18:49 +000019
20sub ::generic
21{ my ($opcode,@arg)=@_;
22
23 # fix hexadecimal constants
Andy Polyakovd08eae12010-06-04 13:13:18 +000024 for (@arg) { s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/oi; }
Andy Polyakov43d8f272007-12-18 09:18:49 +000025
Andy Polyakovee743dc2012-03-29 18:09:36 +000026 if ($opcode =~ /lea/ && @arg[1] =~ s/.*PTR\s+(\(.*\))$/OFFSET $1/) # no []
Andy Polyakov0e1467a2011-12-15 22:20:05 +000027 { $opcode="mov"; }
Andy Polyakov2f8d82d2015-02-22 19:23:25 +010028 elsif ($opcode !~ /mov[dq]$/)
Andy Polyakov6c8b9252009-04-26 17:58:58 +000029 { # fix xmm references
Andy Polyakov2f8d82d2015-02-22 19:23:25 +010030 $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);
Andy Polyakov6c8b9252009-04-26 17:58:58 +000032 }
Andy Polyakov3a877562008-01-03 16:21:06 +000033
Andy Polyakov43d8f272007-12-18 09:18:49 +000034 &::emit($opcode,@arg);
35 1;
36}
37#
38# opcodes not covered by ::generic above, mostly inconsistent namings...
39#
40sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
41sub ::call_ptr { &::emit("call",@_); }
42sub ::jmp_ptr { &::emit("jmp",@_); }
Andy Polyakov70d01a72011-04-18 20:18:03 +000043sub ::lock { &::data_byte(0xf0); }
Andy Polyakov43d8f272007-12-18 09:18:49 +000044
45sub get_mem
46{ my($size,$addr,$reg1,$reg2,$idx)=@_;
47 my($post,$ret);
48
Andy Polyakovc5cd28b2012-11-17 19:04:15 +000049 if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
50
Andy Polyakov43d8f272007-12-18 09:18:49 +000051 $ret .= "$size PTR " if ($size ne "");
52
53 $addr =~ s/^\s+//;
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 =~ /^.+[\-\+].+$/);
58
59 if (($addr ne "") && ($addr ne 0))
60 { if ($addr !~ /^-/) { $ret .= "$addr"; }
61 else { $post=$addr; }
62 }
63 $ret .= "[";
64
65 if ($reg2 ne "")
66 { $idx!=0 or $idx=1;
67 $ret .= "$reg2*$idx";
68 $ret .= "+$reg1" if ($reg1 ne "");
69 }
70 else
71 { $ret .= "$reg1"; }
72
73 $ret .= "$post]";
74 $ret =~ s/\+\]/]/; # in case $addr was the only argument
75 $ret =~ s/\[\s*\]//;
76
77 $ret;
78}
79sub ::BP { &get_mem("BYTE",@_); }
Andy Polyakovd08eae12010-06-04 13:13:18 +000080sub ::WP { &get_mem("WORD",@_); }
Andy Polyakov43d8f272007-12-18 09:18:49 +000081sub ::DWP { &get_mem("DWORD",@_); }
82sub ::QWP { &get_mem("QWORD",@_); }
83sub ::BC { "@_"; }
84sub ::DWC { "@_"; }
85
86sub ::file
87{ my $tmp=<<___;
Andy Polyakov9960bdc2008-07-15 13:16:42 +000088IF \@Version LT 800
89ECHO MASM version 8.00 or later is strongly recommended.
90ENDIF
Gergely Nagy1bb73102016-08-16 14:46:13 +020091.686
Andy Polyakov43d8f272007-12-18 09:18:49 +000092.MODEL FLAT
93OPTION DOTNAME
Andy Polyakov9960bdc2008-07-15 13:16:42 +000094IF \@Version LT 800
Andy Polyakove4662fd2008-07-17 09:46:09 +000095.text\$ SEGMENT PAGE 'CODE'
Andy Polyakov9960bdc2008-07-15 13:16:42 +000096ELSE
97.text\$ SEGMENT ALIGN(64) 'CODE'
98ENDIF
Andy Polyakov43d8f272007-12-18 09:18:49 +000099___
100 push(@out,$tmp);
Andy Polyakovb94551e2008-07-22 08:44:31 +0000101 $segment = ".text\$";
Andy Polyakov43d8f272007-12-18 09:18:49 +0000102}
103
104sub ::function_begin_B
105{ my $func=shift;
106 my $global=($func !~ /^_/);
107 my $begin="${::lbdecor}_${func}_begin";
108
109 &::LABEL($func,$global?"$begin":"$nmdecor$func");
Andy Polyakove4662fd2008-07-17 09:46:09 +0000110 $func="ALIGN\t16\n".$nmdecor.$func."\tPROC";
Andy Polyakov43d8f272007-12-18 09:18:49 +0000111
112 if ($global) { $func.=" PUBLIC\n${begin}::\n"; }
113 else { $func.=" PRIVATE\n"; }
114 push(@out,$func);
115 $::stack=4;
116}
117sub ::function_end_B
118{ my $func=shift;
119
120 push(@out,"$nmdecor$func ENDP\n");
121 $::stack=0;
122 &::wipe_labels();
123}
124
125sub ::file_end
126{ my $xmmheader=<<___;
127.686
128.XMM
129IF \@Version LT 800
130XMMWORD STRUCT 16
131DQ 2 dup (?)
132XMMWORD ENDS
133ENDIF
134___
135 if (grep {/\b[x]?mm[0-7]\b/i} @out) {
136 grep {s/\.[3-7]86/$xmmheader/} @out;
137 }
138
Andy Polyakovb94551e2008-07-22 08:44:31 +0000139 push(@out,"$segment ENDS\n");
Andy Polyakov43d8f272007-12-18 09:18:49 +0000140
141 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
142 { my $comm=<<___;
Andy Polyakov55ff3af2009-11-15 17:06:44 +0000143.bss SEGMENT 'BSS'
Andy Polyakovc5cd28b2012-11-17 19:04:15 +0000144COMM ${nmdecor}OPENSSL_ia32cap_P:DWORD:4
Andy Polyakov9960bdc2008-07-15 13:16:42 +0000145.bss ENDS
Andy Polyakov43d8f272007-12-18 09:18:49 +0000146___
147 # comment out OPENSSL_ia32cap_P declarations
148 grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
149 push (@out,$comm);
150 }
151 push (@out,$initseg) if ($initseg);
152 push (@out,"END\n");
153}
154
155sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
156
157*::set_label_B = sub
158{ my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); };
159
160sub ::external_label
Andy Polyakov3a877562008-01-03 16:21:06 +0000161{ foreach(@_)
162 { push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n"); }
163}
Andy Polyakov43d8f272007-12-18 09:18:49 +0000164
165sub ::public_label
166{ push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
167
168sub ::data_byte
Andy Polyakov2f8d82d2015-02-22 19:23:25 +0100169{ push(@out,("DB\t").join(',',splice(@_,0,16))."\n") while(@_); }
Andy Polyakov43d8f272007-12-18 09:18:49 +0000170
Andy Polyakovd08eae12010-06-04 13:13:18 +0000171sub ::data_short
Andy Polyakov2f8d82d2015-02-22 19:23:25 +0100172{ push(@out,("DW\t").join(',',splice(@_,0,8))."\n") while(@_); }
Andy Polyakovd08eae12010-06-04 13:13:18 +0000173
Andy Polyakov43d8f272007-12-18 09:18:49 +0000174sub ::data_word
Andy Polyakov2f8d82d2015-02-22 19:23:25 +0100175{ push(@out,("DD\t").join(',',splice(@_,0,4))."\n") while(@_); }
Andy Polyakov43d8f272007-12-18 09:18:49 +0000176
177sub ::align
178{ push(@out,"ALIGN\t$_[0]\n"); }
179
180sub ::picmeup
181{ my($dst,$sym)=@_;
182 &::lea($dst,&::DWP($sym));
183}
184
185sub ::initseg
186{ my $f=$nmdecor.shift;
187
188 $initseg.=<<___;
Andy Polyakov3a877562008-01-03 16:21:06 +0000189.CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
Andy Polyakov43d8f272007-12-18 09:18:49 +0000190EXTERN $f:NEAR
191DD $f
192.CRT\$XCU ENDS
193___
194}
195
Andy Polyakovb94551e2008-07-22 08:44:31 +0000196sub ::dataseg
197{ push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA"; }
198
Andy Polyakoved28aef2011-09-06 20:45:36 +0000199sub ::safeseh
200{ my $nm=shift;
201 push(@out,"IF \@Version GE 710\n");
202 push(@out,".SAFESEH ".&::LABEL($nm,$nmdecor.$nm)."\n");
203 push(@out,"ENDIF\n");
204}
205
Andy Polyakov43d8f272007-12-18 09:18:49 +00002061;