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