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