blob: ce2bed9bb290dc4393dfa0b8724acfc58eba98b7 [file] [log] [blame]
Brian Carlstrom674ff292010-04-15 14:22:36 -07001#!/usr/bin/env perl
The Android Open Source Project656d9c72009-03-03 19:30:25 -08002
3package x86nasm;
4
Brian Carlstrom674ff292010-04-15 14:22:36 -07005*out=\@::out;
The Android Open Source Project656d9c72009-03-03 19:30:25 -08006
Brian Carlstrom674ff292010-04-15 14:22:36 -07007$::lbdecor="L\$"; # local label decoration
8$nmdecor=$::netware?"":"_"; # external name decoration
9$drdecor=$::mwerks?".":""; # directive decoration
The Android Open Source Project656d9c72009-03-03 19:30:25 -080010
Brian Carlstrom674ff292010-04-15 14:22:36 -070011$initseg="";
The Android Open Source Project656d9c72009-03-03 19:30:25 -080012
Brian Carlstrom674ff292010-04-15 14:22:36 -070013sub ::generic
14{ my $opcode=shift;
15 my $tmp;
The Android Open Source Project656d9c72009-03-03 19:30:25 -080016
Brian Carlstrom674ff292010-04-15 14:22:36 -070017 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;
The Android Open Source Project656d9c72009-03-03 19:30:25 -080025}
Brian Carlstrom674ff292010-04-15 14:22:36 -070026#
27# opcodes not covered by ::generic above, mostly inconsistent namings...
28#
29sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
30sub ::call_ptr { &::emit("call",@_); }
31sub ::jmp_ptr { &::emit("jmp",@_); }
The Android Open Source Project656d9c72009-03-03 19:30:25 -080032
33sub get_mem
Brian Carlstrom674ff292010-04-15 14:22:36 -070034{ my($size,$addr,$reg1,$reg2,$idx)=@_;
35 my($post,$ret);
The Android Open Source Project656d9c72009-03-03 19:30:25 -080036
Brian Carlstrom674ff292010-04-15 14:22:36 -070037 if ($size ne "")
38 { $ret .= "$size";
39 $ret .= " PTR" if ($::mwerks);
40 $ret .= " ";
41 }
42 $ret .= "[";
The Android Open Source Project656d9c72009-03-03 19:30:25 -080043
Brian Carlstrom674ff292010-04-15 14:22:36 -070044 $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 =~ /^.+[\-\+].+$/);
The Android Open Source Project656d9c72009-03-03 19:30:25 -080049
Brian Carlstrom674ff292010-04-15 14:22:36 -070050 if (($addr ne "") && ($addr ne 0))
51 { if ($addr !~ /^-/) { $ret .= "$addr+"; }
52 else { $post=$addr; }
53 }
The Android Open Source Project656d9c72009-03-03 19:30:25 -080054
Brian Carlstrom674ff292010-04-15 14:22:36 -070055 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"; }
The Android Open Source Project656d9c72009-03-03 19:30:25 -080062
Brian Carlstrom674ff292010-04-15 14:22:36 -070063 $ret .= "$post]";
64 $ret =~ s/\+\]/]/; # in case $addr was the only argument
The Android Open Source Project656d9c72009-03-03 19:30:25 -080065
Brian Carlstrom674ff292010-04-15 14:22:36 -070066 $ret;
67}
68sub ::BP { &get_mem("BYTE",@_); }
69sub ::DWP { &get_mem("DWORD",@_); }
70sub ::QWP { &get_mem("",@_); }
71sub ::BC { (($::mwerks)?"":"BYTE ")."@_"; }
72sub ::DWC { (($::mwerks)?"":"DWORD ")."@_"; }
The Android Open Source Project656d9c72009-03-03 19:30:25 -080073
Brian Carlstrom674ff292010-04-15 14:22:36 -070074sub ::file
75{ if ($::mwerks) { push(@out,".section\t.text,64\n"); }
76 else
77 { my $tmp=<<___;
78%ifidn __OUTPUT_FORMAT__,obj
79section code use32 class=code align=64
80%elifidn __OUTPUT_FORMAT__,win32
81\$\@feat.00 equ 1
82section .text code align=64
The Android Open Source Project656d9c72009-03-03 19:30:25 -080083%else
Brian Carlstrom674ff292010-04-15 14:22:36 -070084section .text code
The Android Open Source Project656d9c72009-03-03 19:30:25 -080085%endif
86___
The Android Open Source Project656d9c72009-03-03 19:30:25 -080087 push(@out,$tmp);
Brian Carlstrom674ff292010-04-15 14:22:36 -070088 }
89}
The Android Open Source Project656d9c72009-03-03 19:30:25 -080090
Brian Carlstrom674ff292010-04-15 14:22:36 -070091sub ::function_begin_B
92{ my $func=shift;
93 my $global=($func !~ /^_/);
94 my $begin="${::lbdecor}_${func}_begin";
The Android Open Source Project656d9c72009-03-03 19:30:25 -080095
Brian Carlstrom674ff292010-04-15 14:22:36 -070096 $begin =~ s/^\@/./ if ($::mwerks); # the torture never stops
The Android Open Source Project656d9c72009-03-03 19:30:25 -080097
Brian Carlstrom674ff292010-04-15 14:22:36 -070098 &::LABEL($func,$global?"$begin":"$nmdecor$func");
99 $func=$nmdecor.$func;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800100
Brian Carlstrom674ff292010-04-15 14:22:36 -0700101 push(@out,"${drdecor}global $func\n") if ($global);
102 push(@out,"${drdecor}align 16\n");
103 push(@out,"$func:\n");
104 push(@out,"$begin:\n") if ($global);
105 $::stack=4;
106}
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800107
Brian Carlstrom674ff292010-04-15 14:22:36 -0700108sub ::function_end_B
109{ $::stack=0;
110 &::wipe_labels();
111}
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800112
Brian Carlstrom674ff292010-04-15 14:22:36 -0700113sub ::file_end
114{ if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
115 { my $comm=<<___;
116${drdecor}segment .bss
117${drdecor}common ${nmdecor}OPENSSL_ia32cap_P 4
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800118___
Brian Carlstrom674ff292010-04-15 14:22:36 -0700119 # comment out OPENSSL_ia32cap_P declarations
120 grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
121 push (@out,$comm)
122 }
123 push (@out,$initseg) if ($initseg);
124}
125
126sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
127
128sub ::external_label
129{ foreach(@_)
130 { push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n"); }
131}
132
133sub ::public_label
134{ push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
135
136sub ::data_byte
137{ push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n"); }
138
139sub ::data_word
140{ push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n"); }
141
142sub ::align
143{ push(@out,"${drdecor}align\t$_[0]\n"); }
144
145sub ::picmeup
146{ my($dst,$sym)=@_;
147 &::lea($dst,&::DWP($sym));
148}
149
150sub ::initseg
151{ my $f=$nmdecor.shift;
152 if ($::win32)
153 { $initseg=<<___;
154segment .CRT\$XCU data align=4
155extern $f
156dd $f
157___
158 }
159}
160
161sub ::dataseg
162{ if ($mwerks) { push(@out,".section\t.data,4\n"); }
163 else { push(@out,"section\t.data align=4\n"); }
164}
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800165
1661;