Apply mingw patches as supplied by Roumen Petrov an Alon Bar-Lev
[openssl.git] / crypto / perlasm / x86nasm.pl
1 #!/usr/bin/env perl
2
3 package x86nasm;
4
5 *out=\@::out;
6
7 $::lbdecor="\@L";               # local label decoration
8 $nmdecor=$::netware?"":"_";     # external name decoration
9 $drdecor=$::mwerks?".":"";      # directive decoration
10
11 $initseg="";
12
13 sub ::generic
14 { my $opcode=shift;
15   my $tmp;
16
17     if (!$::mwerks)
18     {   if    ($opcode =~ m/^j/o && $#_==0) # optimize jumps
19         {   $_[0] = "NEAR $_[0]";       }
20         elsif ($opcode eq "lea" && $#_==1)  # wipe storage qualifier from lea
21         {   $_[1] =~ s/^[^\[]*\[/\[/o;  }
22     }
23     &::emit($opcode,@_);
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     if ($size ne "")
38     {   $ret .= "$size";
39         $ret .= " PTR" if ($::mwerks);
40         $ret .= " ";
41     }
42     $ret .= "[";
43
44     $addr =~ s/^\s+//;
45     # prepend global references with optional underscore
46     $addr =~ s/^([^\+\-0-9][^\+\-]*)/::islabel($1) or "$nmdecor$1"/ige;
47     # put address arithmetic expression in parenthesis
48     $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
49
50     if (($addr ne "") && ($addr ne 0))
51     {   if ($addr !~ /^-/)      { $ret .= "$addr+"; }
52         else                    { $post=$addr;      }
53     }
54
55     if ($reg2 ne "")
56     {   $idx!=0 or $idx=1;
57         $ret .= "$reg2*$idx";
58         $ret .= "+$reg1" if ($reg1 ne "");
59     }
60     else
61     {   $ret .= "$reg1";   }
62
63     $ret .= "$post]";
64     $ret =~ s/\+\]/]/; # in case $addr was the only argument
65
66   $ret;
67 }
68 sub ::BP        { &get_mem("BYTE",@_);  }
69 sub ::DWP       { &get_mem("DWORD",@_); }
70 sub ::QWP       { &get_mem("",@_);      }
71 sub ::BC        { (($::mwerks)?"":"BYTE ")."@_";  }
72 sub ::DWC       { (($::mwerks)?"":"DWORD ")."@_"; }
73
74 sub ::file
75 {   if ($::mwerks)      { push(@out,".section\t.text,64\n"); }
76     else
77     { my $tmp=<<___;
78 %ifdef __omf__
79 section code    use32 class=code align=64
80 %elifdef __coff__
81 section .text   code
82 %else
83 section .text   code align=64
84 %endif
85 ___
86         push(@out,$tmp);
87     }
88 }
89
90 sub ::function_begin_B
91 { my $func=shift;
92   my $global=($func !~ /^_/);
93   my $begin="${::lbdecor}_${func}_begin";
94
95     $begin =~ s/^\@/./ if ($::mwerks);  # the torture never stops
96
97     &::LABEL($func,$global?"$begin":"$nmdecor$func");
98     $func=$nmdecor.$func;
99
100     push(@out,"${drdecor}global $func\n")       if ($global);
101     push(@out,"${drdecor}align  16\n");
102     push(@out,"$func:\n");
103     push(@out,"$begin:\n")                      if ($global);
104     $::stack=4;
105 }
106
107 sub ::function_end_B
108 {   $::stack=0;
109     &::wipe_labels();
110 }
111
112 sub ::file_end
113 {   if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
114     {   my $comm=<<___;
115 ${drdecor}segment       .bss
116 ${drdecor}common        ${nmdecor}OPENSSL_ia32cap_P 4
117 ___
118         # comment out OPENSSL_ia32cap_P declarations
119         grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
120         push (@out,$comm)
121     }
122     push (@out,$initseg) if ($initseg);         
123 }
124
125 sub ::comment {   foreach (@_) { push(@out,"\t; $_\n"); }   }
126
127 sub ::external_label
128 {   foreach(@_)
129     {   push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n");   }
130 }
131
132 sub ::public_label
133 {   push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n");  }
134
135 sub ::data_byte
136 {   push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n");        }
137
138 sub ::data_word
139 {   push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n");        }
140
141 sub ::align
142 {   push(@out,"${drdecor}align\t$_[0]\n");      }
143
144 sub ::picmeup
145 { my($dst,$sym)=@_;
146     &::lea($dst,&::DWP($sym));
147 }
148
149 sub ::initseg
150 { my $f=$nmdecor.shift;
151     if ($::win32)
152     {   $initseg=<<___;
153 segment .CRT\$XCU data align=4
154 extern  $f
155 dd      $f
156 ___
157     }
158 }
159
160 1;