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'div { &out1("divl",@_); }
175 sub main'jmp { &out1("jmp",@_); }
176 sub main'jmp_ptr { &out1p("jmp",@_); }
177 sub main'je { &out1("je",@_); }
178 sub main'jle { &out1("jle",@_); }
179 sub main'jne { &out1("jne",@_); }
180 sub main'jnz { &out1("jnz",@_); }
181 sub main'jz { &out1("jz",@_); }
182 sub main'jge { &out1("jge",@_); }
183 sub main'jl { &out1("jl",@_); }
184 sub main'ja { &out1("ja",@_); }
185 sub main'jae { &out1("jae",@_); }
186 sub main'jb { &out1("jb",@_); }
187 sub main'jbe { &out1("jbe",@_); }
188 sub main'jc { &out1("jc",@_); }
189 sub main'jnc { &out1("jnc",@_); }
190 sub main'jno { &out1("jno",@_); }
191 sub main'dec { &out1("decl",@_); }
192 sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
193 sub main'push { &out1("pushl",@_); $stack+=4; }
194 sub main'pop { &out1("popl",@_); $stack-=4; }
195 sub main'pushf { &out0("pushfl"); $stack+=4; }
196 sub main'popf { &out0("popfl"); $stack-=4; }
197 sub main'not { &out1("notl",@_); }
198 sub main'call { my $pre=$under;
200 { if ($label{$i} eq $_[0]) { $pre=''; last; } }
201 &out1("call",$pre.$_[0]);
203 sub main'ret { &out0("ret"); }
204 sub main'nop { &out0("nop"); }
205 sub main'test { &out2("testl",@_); }
206 sub main'bt { &out2("btl",@_); }
207 sub main'leave { &out0("leave"); }
208 sub main'cpuid { &out0(".byte\t0x0f,0xa2"); }
209 sub main'rdtsc { &out0(".byte\t0x0f,0x31"); }
210 sub main'halt { &out0("hlt"); }
211 sub main'movz { &out2("movzbl",@_); }
212 sub main'neg { &out1("negl",@_); }
213 sub main'cld { &out0("cld"); }
216 sub main'emms { &out0("emms"); }
217 sub main'movd { &out2("movd",@_); }
218 sub main'movdqu { &out2("movdqu",@_); }
219 sub main'movdqa { &out2("movdqa",@_); }
220 sub main'movdq2q{ &out2("movdq2q",@_); }
221 sub main'movq2dq{ &out2("movq2dq",@_); }
222 sub main'paddq { &out2("paddq",@_); }
223 sub main'pmuludq{ &out2("pmuludq",@_); }
224 sub main'psrlq { &out2("psrlq",@_); }
225 sub main'psllq { &out2("psllq",@_); }
226 sub main'pxor { &out2("pxor",@_); }
227 sub main'por { &out2("por",@_); }
228 sub main'pand { &out2("pand",@_); }
230 local($p1,$p2,$optimize)=@_;
231 if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
232 # movq between mmx registers can sink Intel CPUs
233 { push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n"); }
234 else { &out2("movq",@_); }
237 # The bswapl instruction is new for the 486. Emulate if i386.
242 &main'comment("bswapl @_");
243 &main'exch(main'HB(@_),main'LB(@_));
245 &main'exch(main'HB(@_),main'LB(@_));
255 local($name,$p1,$p2)=@_;
257 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
258 "rcll",0xD1D0,"rcrl",0xD1D8,
259 "shll",0xD1E0,"shrl",0xD1E8,
262 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
264 $op=$special{$name}|$reg_val{$p1};
265 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
266 $tmp2=sprintf(".byte %d\t",$op &0xff);
272 &main'comment("$name $p2 $p1");
276 push(@out,"\t$name\t");
281 $tmp1=sprintf("\t" x $ll);
283 push(@out,&conv($p1)."\n");
290 local(%special)=("bswapl",0x0FC8);
292 if ((defined($special{$name})) && defined($regs{$p1}))
294 $op=$special{$name}|$reg_val{$p1};
295 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
296 $tmp2=sprintf(".byte %d\t",$op &0xff);
302 &main'comment("$name $p2 $p1");
306 push(@out,"\t$name\t".&conv($p1)."\n");
314 push(@out,"\t$name\t*".&conv($p1)."\n");
319 push(@out,"\t$_[0]\n");
326 # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
328 $p=$regs{$p} if (defined($regs{$p}));
330 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
331 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
345 sub main'function_begin
349 &main'external_label($func);
358 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
360 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
361 elsif ($main'aout and !$main'pic)
363 else { $tmp=push(@out,".type\t$func,\@function\n"); }
364 push(@out,".align\t$align\n");
365 push(@out,"$func:\n");
377 sub main'function_begin_B
379 local($func,$extra)=@_;
381 &main'external_label($func);
390 { push(@out,"TYPE($func,\@function)\n"); }
392 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
393 elsif ($main'aout and !$main'pic)
395 else { push(@out,".type $func,\@function\n"); }
396 push(@out,".align\t$align\n");
397 push(@out,"$func:\n");
401 sub main'function_end
418 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
419 elsif ($main'coff or $main'aout)
421 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
422 push(@out,".ident \"$func\"\n");
427 sub main'function_end_A
441 sub main'function_end_B
447 push(@out,"${dot}L_${func}_end:\n");
449 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
450 elsif ($main'coff or $main'aout)
452 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
453 push(@out,".ident \"$func\"\n");
462 return(&main'DWP($stack+$num*4,"esp","",0));
469 &main'sub("esp",$num*4);
476 &main'add("esp",$num*4);
481 return(&main'DWP($_[0]*4,"esp","",0));
484 # Should use swtmp, which is above esp. Linix can trash the stack above esp
489 # return(&main'DWP(-($num+1)*4,"esp","",0));
494 if (!defined($com_start) or $main'elf)
495 { # Regarding $main'elf above...
496 # GNU and SVR4 as'es use different comment delimiters,
497 push(@out,"\n"); # so we just skip ELF comments...
505 { push(@out,"\t$com_start $_ $com_end\n"); }
509 sub main'public_label
511 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
512 push(@out,".globl\t$label{$_[0]}\n");
517 if (!defined($label{$_[0]}))
519 $label{$_[0]}="${dot}${label}${_[0]}";
522 return($label{$_[0]});
527 if (!defined($label{$_[0]}))
529 $label{$_[0]}="${dot}${label}${_[0]}";
534 if ($_[1]>1) { main'align($_[1]); }
535 else { push(@out,".align $align\n"); }
537 push(@out,"$label{$_[0]}:\n");
542 # try to detect if SSE2 or MMX extensions were used on ELF platform...
543 if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
546 push (@out,"\n.section\t.bss\n");
547 push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
549 push (@out,".section\t.init\n");
550 # One can argue that it's wasteful to craft every
551 # SSE/MMX module with this snippet... Well, it's 72
552 # bytes long and for the moment we have two modules.
553 # Let's argue when we have 7 modules or so...
555 # $1<<10 sets a reserved bit to signal that variable
556 # was initialized already...
557 &main'picmeup("edx","OPENSSL_ia32cap_P");
591 push(@out,".section .rodata\n");
599 push(@out,"\t.byte\t".join(',',@_)."\n");
604 push(@out,"\t.long\t".join(',',@_)."\n");
609 my $val=$_[0],$p2,$i;
611 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
615 push(@out,".align\t$val\n");
618 # debug output functions: puts, putx, printf
623 &main'push('$Lstring' . ++$constl);
629 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
636 &main'push('$Lstring' . ++$constl);
637 &main'call('printf');
642 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
649 for ($i = @_ - 1; $i >= 0; $i--)
651 if ($i == 0) # change this to support %s format strings
653 &main'push('$Lstring' . ++$constl);
654 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
658 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
660 &main'push(($1 + $stack - $ostack) . '(%esp)');
668 &main'call('printf');
670 &main'add("esp",4*@_);
696 #if (defined(ELF) || defined(SOL)) && defined(PIC)
699 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
700 movl $sym\@GOT($regs{$dst}),$regs{$dst}
702 leal $sym,$regs{$dst}
707 elsif ($main'pic && ($main'elf || $main'aout))
709 &main'call(&main'label("PIC_me_up"));
710 &main'set_label("PIC_me_up");
711 &main'blindpop($dst);
712 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
713 &main'label("PIC_me_up") . "]");
714 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
718 &main'lea($dst,&main'DWP($sym));
722 sub main'blindpop { &out1("popl",@_); }
740 $tmp=<<___; # applies to both Cygwin and Mingw
747 local($ctor)="${under}_GLOBAL_\$I\$$f";
749 $tmp.=".type $ctor,\@function\n" if ($main'pic);
750 $tmp.=<<___; # OpenBSD way...
757 push(@out,$tmp) if ($tmp);