3 package x86unix; # GAS actually...
9 $align=($main'aout)?"4":"16";
10 $under=($main'aout or $main'coff)?"_":"";
11 $dot=($main'aout)?"":".";
12 $com_start="#" if ($main'aout or $main'coff);
14 sub main'asm_init_output { @out=(); }
15 sub main'asm_get_output { return(@out); }
16 sub main'get_labels { return(@labels); }
17 sub main'external_label { push(@labels,@_); }
47 %regs=( 'eax', '%eax',
88 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
94 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
100 local($addr,$reg1,$reg2,$idx)=@_;
103 $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
104 $reg1="$regs{$reg1}" if defined($regs{$reg1});
105 $reg2="$regs{$reg2}" if defined($regs{$reg2});
106 $ret.=$addr if ($addr ne "") && ($addr ne 0);
109 if($idx ne "" && $idx != 0)
110 { $ret.="($reg1,$reg2,$idx)"; }
112 { $ret.="($reg1,$reg2)"; }
121 return(&main'DWP(@_));
126 return(&main'DWP(@_));
141 # local($addr,$reg1,$reg2,$idx)=@_;
145 # $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
146 # $reg1="$regs{$reg1}" if defined($regs{$reg1});
147 # $reg2="$regs{$reg2}" if defined($regs{$reg2});
148 # $ret.=$addr if ($addr ne "") && ($addr ne 0);
150 # { $ret.="($reg1,$reg2,$idx)"; }
152 # { $ret.="($reg1)" }
156 sub main'mov { &out2("movl",@_); }
157 sub main'movb { &out2("movb",@_); }
158 sub main'and { &out2("andl",@_); }
159 sub main'or { &out2("orl",@_); }
160 sub main'shl { &out2("sall",@_); }
161 sub main'shr { &out2("shrl",@_); }
162 sub main'xor { &out2("xorl",@_); }
163 sub main'xorb { &out2("xorb",@_); }
164 sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); }
165 sub main'adc { &out2("adcl",@_); }
166 sub main'sub { &out2("subl",@_); }
167 sub main'sbb { &out2("sbbl",@_); }
168 sub main'rotl { &out2("roll",@_); }
169 sub main'rotr { &out2("rorl",@_); }
170 sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); }
171 sub main'cmp { &out2("cmpl",@_); }
172 sub main'lea { &out2("leal",@_); }
173 sub main'mul { &out1("mull",@_); }
174 sub main'imul { &out2("imull",@_); }
175 sub main'div { &out1("divl",@_); }
176 sub main'jmp { &out1("jmp",@_); }
177 sub main'jmp_ptr { &out1p("jmp",@_); }
178 sub main'je { &out1("je",@_); }
179 sub main'jle { &out1("jle",@_); }
180 sub main'jne { &out1("jne",@_); }
181 sub main'jnz { &out1("jnz",@_); }
182 sub main'jz { &out1("jz",@_); }
183 sub main'jge { &out1("jge",@_); }
184 sub main'jl { &out1("jl",@_); }
185 sub main'ja { &out1("ja",@_); }
186 sub main'jae { &out1("jae",@_); }
187 sub main'jb { &out1("jb",@_); }
188 sub main'jbe { &out1("jbe",@_); }
189 sub main'jc { &out1("jc",@_); }
190 sub main'jnc { &out1("jnc",@_); }
191 sub main'jno { &out1("jno",@_); }
192 sub main'dec { &out1("decl",@_); }
193 sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
194 sub main'push { &out1("pushl",@_); $stack+=4; }
195 sub main'pop { &out1("popl",@_); $stack-=4; }
196 sub main'pushf { &out0("pushfl"); $stack+=4; }
197 sub main'popf { &out0("popfl"); $stack-=4; }
198 sub main'not { &out1("notl",@_); }
199 sub main'call { my $pre=$under;
201 { if ($label{$i} eq $_[0]) { $pre=''; last; } }
202 &out1("call",$pre.$_[0]);
204 sub main'call_ptr { &out1p("call",@_); }
205 sub main'ret { &out0("ret"); }
206 sub main'nop { &out0("nop"); }
207 sub main'test { &out2("testl",@_); }
208 sub main'bt { &out2("btl",@_); }
209 sub main'leave { &out0("leave"); }
210 sub main'cpuid { &out0(".byte\t0x0f,0xa2"); }
211 sub main'rdtsc { &out0(".byte\t0x0f,0x31"); }
212 sub main'halt { &out0("hlt"); }
213 sub main'movz { &out2("movzbl",@_); }
214 sub main'neg { &out1("negl",@_); }
215 sub main'cld { &out0("cld"); }
218 sub main'emms { &out0("emms"); }
219 sub main'movd { &out2("movd",@_); }
220 sub main'movdqu { &out2("movdqu",@_); }
221 sub main'movdqa { &out2("movdqa",@_); }
222 sub main'movdq2q{ &out2("movdq2q",@_); }
223 sub main'movq2dq{ &out2("movq2dq",@_); }
224 sub main'paddq { &out2("paddq",@_); }
225 sub main'pmuludq{ &out2("pmuludq",@_); }
226 sub main'psrlq { &out2("psrlq",@_); }
227 sub main'psllq { &out2("psllq",@_); }
228 sub main'pxor { &out2("pxor",@_); }
229 sub main'por { &out2("por",@_); }
230 sub main'pand { &out2("pand",@_); }
232 local($p1,$p2,$optimize)=@_;
233 if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
234 # movq between mmx registers can sink Intel CPUs
235 { push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n"); }
236 else { &out2("movq",@_); }
239 # The bswapl instruction is new for the 486. Emulate if i386.
244 &main'comment("bswapl @_");
245 &main'exch(main'HB(@_),main'LB(@_));
247 &main'exch(main'HB(@_),main'LB(@_));
257 local($name,$p1,$p2)=@_;
259 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
260 "rcll",0xD1D0,"rcrl",0xD1D8,
261 "shll",0xD1E0,"shrl",0xD1E8,
264 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
266 $op=$special{$name}|$reg_val{$p1};
267 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
268 $tmp2=sprintf(".byte %d\t",$op &0xff);
274 &main'comment("$name $p2 $p1");
278 push(@out,"\t$name\t");
283 $tmp1=sprintf("\t" x $ll);
285 push(@out,&conv($p1)."\n");
292 local(%special)=("bswapl",0x0FC8);
294 if ((defined($special{$name})) && defined($regs{$p1}))
296 $op=$special{$name}|$reg_val{$p1};
297 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
298 $tmp2=sprintf(".byte %d\t",$op &0xff);
304 &main'comment("$name $p2 $p1");
308 push(@out,"\t$name\t".&conv($p1)."\n");
316 push(@out,"\t$name\t*".&conv($p1)."\n");
321 push(@out,"\t$_[0]\n");
328 # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
330 $p=$regs{$p} if (defined($regs{$p}));
332 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
333 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
347 sub main'function_begin
351 &main'external_label($func);
360 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
362 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
363 elsif ($main'aout and !$main'pic)
365 else { $tmp=push(@out,".type\t$func,\@function\n"); }
366 push(@out,".align\t$align\n");
367 push(@out,"$func:\n");
379 sub main'function_begin_B
381 local($func,$extra)=@_;
383 &main'external_label($func);
392 { push(@out,"TYPE($func,\@function)\n"); }
394 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
395 elsif ($main'aout and !$main'pic)
397 else { push(@out,".type $func,\@function\n"); }
398 push(@out,".align\t$align\n");
399 push(@out,"$func:\n");
403 sub main'function_end
420 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
421 elsif ($main'coff or $main'aout)
423 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
424 push(@out,".ident \"$func\"\n");
429 sub main'function_end_A
443 sub main'function_end_B
449 push(@out,"${dot}L_${func}_end:\n");
451 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
452 elsif ($main'coff or $main'aout)
454 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
455 push(@out,".ident \"$func\"\n");
464 return(&main'DWP($stack+$num*4,"esp","",0));
471 &main'sub("esp",$num*4);
478 &main'add("esp",$num*4);
483 return(&main'DWP($_[0]*4,"esp","",0));
486 # Should use swtmp, which is above esp. Linix can trash the stack above esp
491 # return(&main'DWP(-($num+1)*4,"esp","",0));
496 if (!defined($com_start) or $main'elf)
497 { # Regarding $main'elf above...
498 # GNU and SVR4 as'es use different comment delimiters,
499 push(@out,"\n"); # so we just skip ELF comments...
507 { push(@out,"\t$com_start $_ $com_end\n"); }
511 sub main'public_label
513 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
514 push(@out,".globl\t$label{$_[0]}\n");
519 if (!defined($label{$_[0]}))
521 $label{$_[0]}="${dot}${label}${_[0]}";
524 return($label{$_[0]});
529 if (!defined($label{$_[0]}))
531 $label{$_[0]}="${dot}${label}${_[0]}";
536 if ($_[1]>1) { main'align($_[1]); }
537 else { push(@out,".align $align\n"); }
539 push(@out,"$label{$_[0]}:\n");
544 # try to detect if SSE2 or MMX extensions were used on ELF platform...
545 if ($main'elf && grep {/\b%[x]*mm[0-7]\b|OPENSSL_ia32cap_P\b/i} @out) {
548 push (@out,"\n.section\t.bss\n");
549 push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
556 push(@out,".section .rodata\n");
564 push(@out,"\t.byte\t".join(',',@_)."\n");
569 push(@out,"\t.long\t".join(',',@_)."\n");
574 my $val=$_[0],$p2,$i;
576 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
580 push(@out,".align\t$val\n");
583 # debug output functions: puts, putx, printf
588 &main'push('$Lstring' . ++$constl);
594 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
601 &main'push('$Lstring' . ++$constl);
602 &main'call('printf');
607 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
614 for ($i = @_ - 1; $i >= 0; $i--)
616 if ($i == 0) # change this to support %s format strings
618 &main'push('$Lstring' . ++$constl);
619 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
623 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
625 &main'push(($1 + $stack - $ostack) . '(%esp)');
633 &main'call('printf');
635 &main'add("esp",4*@_);
661 #if (defined(ELF) || defined(SOL)) && defined(PIC)
664 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
665 movl $sym\@GOT($regs{$dst}),$regs{$dst}
667 leal $sym,$regs{$dst}
672 elsif ($main'pic && ($main'elf || $main'aout))
674 &main'call(&main'label("PIC_me_up"));
675 &main'set_label("PIC_me_up");
676 &main'blindpop($dst);
677 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
678 &main'label("PIC_me_up") . "]");
679 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
683 &main'lea($dst,&main'DWP($sym));
687 sub main'blindpop { &out1("popl",@_); }
705 $tmp=<<___; # applies to both Cygwin and Mingw
712 local($ctor)="${under}_GLOBAL_\$I\$$f";
714 $tmp.=".type $ctor,\@function\n" if ($main'pic);
715 $tmp.=<<___; # OpenBSD way...
722 push(@out,$tmp) if ($tmp);