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