ee446de5c1d036544a5a543a66367a03a2717413
[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 sub ::lock      { &::data_byte(0xf0);   }
35
36 sub get_mem
37 { my($size,$addr,$reg1,$reg2,$idx)=@_;
38   my($post,$ret);
39
40     $ret .= "$size PTR " if ($size ne "");
41
42     $addr =~ s/^\s+//;
43     # prepend global references with optional underscore
44     $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
45     # put address arithmetic expression in parenthesis
46     $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
47
48     if (($addr ne "") && ($addr ne 0))
49     {   if ($addr !~ /^-/)      { $ret .= "$addr";  }
50         else                    { $post=$addr;      }
51     }
52     $ret .= "[";
53
54     if ($reg2 ne "")
55     {   $idx!=0 or $idx=1;
56         $ret .= "$reg2*$idx";
57         $ret .= "+$reg1" if ($reg1 ne "");
58     }
59     else
60     {   $ret .= "$reg1";   }
61
62     $ret .= "$post]";
63     $ret =~ s/\+\]/]/; # in case $addr was the only argument
64     $ret =~ s/\[\s*\]//;
65
66   $ret;
67 }
68 sub ::BP        { &get_mem("BYTE",@_);  }
69 sub ::WP        { &get_mem("WORD",@_);  }
70 sub ::DWP       { &get_mem("DWORD",@_); }
71 sub ::QWP       { &get_mem("QWORD",@_); }
72 sub ::BC        { "@_";  }
73 sub ::DWC       { "@_"; }
74
75 sub ::file
76 { my $tmp=<<___;
77 TITLE   $_[0].asm
78 IF \@Version LT 800
79 ECHO MASM version 8.00 or later is strongly recommended.
80 ENDIF
81 .486
82 .MODEL  FLAT
83 OPTION  DOTNAME
84 IF \@Version LT 800
85 .text\$ SEGMENT PAGE 'CODE'
86 ELSE
87 .text\$ SEGMENT ALIGN(64) 'CODE'
88 ENDIF
89 ___
90     push(@out,$tmp);
91     $segment = ".text\$";
92 }
93
94 sub ::function_begin_B
95 { my $func=shift;
96   my $global=($func !~ /^_/);
97   my $begin="${::lbdecor}_${func}_begin";
98
99     &::LABEL($func,$global?"$begin":"$nmdecor$func");
100     $func="ALIGN\t16\n".$nmdecor.$func."\tPROC";
101
102     if ($global)    { $func.=" PUBLIC\n${begin}::\n"; }
103     else            { $func.=" PRIVATE\n";            }
104     push(@out,$func);
105     $::stack=4;
106 }
107 sub ::function_end_B
108 { my $func=shift;
109
110     push(@out,"$nmdecor$func ENDP\n");
111     $::stack=0;
112     &::wipe_labels();
113 }
114
115 sub ::file_end
116 { my $xmmheader=<<___;
117 .686
118 .XMM
119 IF \@Version LT 800
120 XMMWORD STRUCT 16
121 DQ      2 dup (?)
122 XMMWORD ENDS
123 ENDIF
124 ___
125     if (grep {/\b[x]?mm[0-7]\b/i} @out) {
126         grep {s/\.[3-7]86/$xmmheader/} @out;
127     }
128
129     push(@out,"$segment ENDS\n");
130
131     if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
132     {   my $comm=<<___;
133 .bss    SEGMENT 'BSS'
134 COMM    ${nmdecor}OPENSSL_ia32cap_P:QWORD
135 .bss    ENDS
136 ___
137         # comment out OPENSSL_ia32cap_P declarations
138         grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
139         push (@out,$comm);
140     }
141     push (@out,$initseg) if ($initseg);
142     push (@out,"END\n");
143 }
144
145 sub ::comment {   foreach (@_) { push(@out,"\t; $_\n"); }   }
146
147 *::set_label_B = sub
148 { my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); };
149
150 sub ::external_label
151 {   foreach(@_)
152     {   push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n");   }
153 }
154
155 sub ::public_label
156 {   push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n");   }
157
158 sub ::data_byte
159 {   push(@out,("DB\t").join(',',@_)."\n");      }
160
161 sub ::data_short
162 {   push(@out,("DW\t").join(',',@_)."\n");      }
163
164 sub ::data_word
165 {   push(@out,("DD\t").join(',',@_)."\n");      }
166
167 sub ::align
168 {   push(@out,"ALIGN\t$_[0]\n");        }
169
170 sub ::picmeup
171 { my($dst,$sym)=@_;
172     &::lea($dst,&::DWP($sym));
173 }
174
175 sub ::initseg
176 { my $f=$nmdecor.shift;
177
178     $initseg.=<<___;
179 .CRT\$XCU       SEGMENT DWORD PUBLIC 'DATA'
180 EXTERN  $f:NEAR
181 DD      $f
182 .CRT\$XCU       ENDS
183 ___
184 }
185
186 sub ::dataseg
187 {   push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA";   }
188
189 sub ::safeseh
190 { my $nm=shift;
191     push(@out,"IF \@Version GE 710\n");
192     push(@out,".SAFESEH ".&::LABEL($nm,$nmdecor.$nm)."\n");
193     push(@out,"ENDIF\n");
194 }
195
196 1;