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("addl",@_); }
165 sub main'adc { &out2("adcl",@_); }
166 sub main'sub { &out2("subl",@_); }
167 sub main'rotl { &out2("roll",@_); }
168 sub main'rotr { &out2("rorl",@_); }
169 sub main'exch { &out2("xchg",@_); }
170 sub main'cmp { &out2("cmpl",@_); }
171 sub main'lea { &out2("leal",@_); }
172 sub main'mul { &out1("mull",@_); }
173 sub main'div { &out1("divl",@_); }
174 sub main'jmp { &out1("jmp",@_); }
175 sub main'jmp_ptr { &out1p("jmp",@_); }
176 sub main'je { &out1("je",@_); }
177 sub main'jle { &out1("jle",@_); }
178 sub main'jne { &out1("jne",@_); }
179 sub main'jnz { &out1("jnz",@_); }
180 sub main'jz { &out1("jz",@_); }
181 sub main'jge { &out1("jge",@_); }
182 sub main'jl { &out1("jl",@_); }
183 sub main'ja { &out1("ja",@_); }
184 sub main'jae { &out1("jae",@_); }
185 sub main'jb { &out1("jb",@_); }
186 sub main'jbe { &out1("jbe",@_); }
187 sub main'jc { &out1("jc",@_); }
188 sub main'jnc { &out1("jnc",@_); }
189 sub main'jno { &out1("jno",@_); }
190 sub main'dec { &out1("decl",@_); }
191 sub main'inc { &out1("incl",@_); }
192 sub main'push { &out1("pushl",@_); $stack+=4; }
193 sub main'pop { &out1("popl",@_); $stack-=4; }
194 sub main'pushf { &out0("pushf"); $stack+=4; }
195 sub main'popf { &out0("popf"); $stack-=4; }
196 sub main'not { &out1("notl",@_); }
197 sub main'call { my $pre=$under;
199 { if ($label{$i} eq $_[0]) { $pre=''; last; } }
200 &out1("call",$pre.$_[0]);
202 sub main'ret { &out0("ret"); }
203 sub main'nop { &out0("nop"); }
204 sub main'test { &out2("testl",@_); }
205 sub main'bt { &out2("btl",@_); }
206 sub main'leave { &out0("leave"); }
207 sub main'cpuid { &out0(".word\t0xa20f"); }
208 sub main'rdtsc { &out0(".word\t0x310f"); }
211 sub main'emms { &out0("emms"); }
212 sub main'movd { &out2("movd",@_); }
213 sub main'movq { &out2("movq",@_); }
214 sub main'movdqu { &out2("movdqu",@_); }
215 sub main'movdqa { &out2("movdqa",@_); }
216 sub main'movdq2q{ &out2("movdq2q",@_); }
217 sub main'movq2dq{ &out2("movq2dq",@_); }
218 sub main'paddq { &out2("paddq",@_); }
219 sub main'pmuludq{ &out2("pmuludq",@_); }
220 sub main'psrlq { &out2("psrlq",@_); }
221 sub main'psllq { &out2("psllq",@_); }
222 sub main'pxor { &out2("pxor",@_); }
223 sub main'por { &out2("por",@_); }
224 sub main'pand { &out2("pand",@_); }
226 # The bswapl instruction is new for the 486. Emulate if i386.
231 &main'comment("bswapl @_");
232 &main'exch(main'HB(@_),main'LB(@_));
234 &main'exch(main'HB(@_),main'LB(@_));
244 local($name,$p1,$p2)=@_;
246 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
247 "rcll",0xD1D0,"rcrl",0xD1D8,
248 "shll",0xD1E0,"shrl",0xD1E8,
251 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
253 $op=$special{$name}|$reg_val{$p1};
254 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
255 $tmp2=sprintf(".byte %d\t",$op &0xff);
261 &main'comment("$name $p2 $p1");
265 push(@out,"\t$name\t");
270 $tmp1=sprintf("\t" x $ll);
272 push(@out,&conv($p1)."\n");
279 local(%special)=("bswapl",0x0FC8);
281 if ((defined($special{$name})) && defined($regs{$p1}))
283 $op=$special{$name}|$reg_val{$p1};
284 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
285 $tmp2=sprintf(".byte %d\t",$op &0xff);
291 &main'comment("$name $p2 $p1");
295 push(@out,"\t$name\t".&conv($p1)."\n");
303 push(@out,"\t$name\t*".&conv($p1)."\n");
308 push(@out,"\t$_[0]\n");
315 # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
317 $p=$regs{$p} if (defined($regs{$p}));
319 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
320 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
334 sub main'function_begin
338 &main'external_label($func);
347 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
349 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
352 else { $tmp=push(@out,".type\t$func,\@function\n"); }
353 push(@out,".align\t$align\n");
354 push(@out,"$func:\n");
366 sub main'function_begin_B
368 local($func,$extra)=@_;
370 &main'external_label($func);
379 { push(@out,"TYPE($func,\@function)\n"); }
381 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
384 else { push(@out,".type $func,\@function\n"); }
385 push(@out,".align\t$align\n");
386 push(@out,"$func:\n");
390 sub main'function_end
407 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
408 elsif ($main'coff or $main'aout)
410 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
411 push(@out,".ident \"$func\"\n");
416 sub main'function_end_A
430 sub main'function_end_B
436 push(@out,"${dot}L_${func}_end:\n");
438 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
439 elsif ($main'coff or $main'aout)
441 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
442 push(@out,".ident \"$func\"\n");
451 return(&main'DWP($stack+$num*4,"esp","",0));
458 &main'sub("esp",$num*4);
465 &main'add("esp",$num*4);
470 return(&main'DWP($_[0]*4,"esp","",0));
473 # Should use swtmp, which is above esp. Linix can trash the stack above esp
478 # return(&main'DWP(-($num+1)*4,"esp","",0));
483 if (!defined($com_start) or $main'elf)
484 { # Regarding $main'elf above...
485 # GNU and SVR4 as'es use different comment delimiters,
486 push(@out,"\n"); # so we just skip ELF comments...
494 { push(@out,"\t$com_start $_ $com_end\n"); }
500 if (!defined($label{$_[0]}))
502 $label{$_[0]}="${dot}${label}${_[0]}";
505 return($label{$_[0]});
510 if (!defined($label{$_[0]}))
512 $label{$_[0]}="${dot}${label}${_[0]}";
517 if ($_[1]>1) { main'align($_[1]); }
518 else { push(@out,".align $align\n"); }
520 push(@out,"$label{$_[0]}:\n");
525 # try to detect if SSE2 or MMX extensions were used on ELF platform...
526 if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
529 push (@out,"\n.comm\t${under}OPENSSL_ia32cap_P,4,4\n");
531 push (@out,".section\t.init\n");
532 # One can argue that it's wasteful to craft every
533 # SSE/MMX module with this snippet... Well, it's 72
534 # bytes long and for the moment we have two modules.
535 # Let's argue when we have 7 modules or so...
537 # $1<<10 sets a reserved bit to signal that variable
538 # was initialized already...
539 &main'picmeup("edx","OPENSSL_ia32cap_P");
572 push(@out,".section .rodata\n");
580 push(@out,"\t.long\t".join(',',@_)."\n");
585 my $val=$_[0],$p2,$i;
587 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
591 push(@out,".align\t$val\n");
594 # debug output functions: puts, putx, printf
599 &main'push('$Lstring' . ++$constl);
605 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
612 &main'push('$Lstring' . ++$constl);
613 &main'call('printf');
618 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
625 for ($i = @_ - 1; $i >= 0; $i--)
627 if ($i == 0) # change this to support %s format strings
629 &main'push('$Lstring' . ++$constl);
630 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
634 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
636 &main'push(($1 + $stack - $ostack) . '(%esp)');
644 &main'call('printf');
646 &main'add("esp",4*@_);
672 #if (defined(ELF) || defined(SOL)) && defined(PIC)
675 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
676 movl $sym\@GOT($regs{$dst}),$regs{$dst}
678 leal $sym,$regs{$dst}
683 elsif ($main'pic && ($main'elf || $main'aout))
685 &main'call(&main'label("PIC_me_up"));
686 &main'set_label("PIC_me_up");
687 &main'blindpop($dst);
688 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
689 &main'label("PIC_me_up") . "]");
690 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
694 &main'lea($dst,&main'DWP($sym));
698 sub main'blindpop { &out1("popl",@_); }
713 $tmp=<<___; # applies to both Cygwin and Mingw
720 $tmp=<<___; # OpenBSD way...
722 .globl ${under}_GLOBAL_\$I\$$f
724 ${under}_GLOBAL_\$I\$$f
728 push(@out,$tmp) if ($tmp);