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("xchg",@_); }
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("movzb",@_); }
214 sub main'emms { &out0("emms"); }
215 sub main'movd { &out2("movd",@_); }
216 sub main'movq { &out2("movq",@_); }
217 sub main'movdqu { &out2("movdqu",@_); }
218 sub main'movdqa { &out2("movdqa",@_); }
219 sub main'movdq2q{ &out2("movdq2q",@_); }
220 sub main'movq2dq{ &out2("movq2dq",@_); }
221 sub main'paddq { &out2("paddq",@_); }
222 sub main'pmuludq{ &out2("pmuludq",@_); }
223 sub main'psrlq { &out2("psrlq",@_); }
224 sub main'psllq { &out2("psllq",@_); }
225 sub main'pxor { &out2("pxor",@_); }
226 sub main'por { &out2("por",@_); }
227 sub main'pand { &out2("pand",@_); }
229 # The bswapl instruction is new for the 486. Emulate if i386.
234 &main'comment("bswapl @_");
235 &main'exch(main'HB(@_),main'LB(@_));
237 &main'exch(main'HB(@_),main'LB(@_));
247 local($name,$p1,$p2)=@_;
249 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
250 "rcll",0xD1D0,"rcrl",0xD1D8,
251 "shll",0xD1E0,"shrl",0xD1E8,
254 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
256 $op=$special{$name}|$reg_val{$p1};
257 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
258 $tmp2=sprintf(".byte %d\t",$op &0xff);
264 &main'comment("$name $p2 $p1");
268 push(@out,"\t$name\t");
273 $tmp1=sprintf("\t" x $ll);
275 push(@out,&conv($p1)."\n");
282 local(%special)=("bswapl",0x0FC8);
284 if ((defined($special{$name})) && defined($regs{$p1}))
286 $op=$special{$name}|$reg_val{$p1};
287 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
288 $tmp2=sprintf(".byte %d\t",$op &0xff);
294 &main'comment("$name $p2 $p1");
298 push(@out,"\t$name\t".&conv($p1)."\n");
306 push(@out,"\t$name\t*".&conv($p1)."\n");
311 push(@out,"\t$_[0]\n");
318 # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
320 $p=$regs{$p} if (defined($regs{$p}));
322 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
323 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
337 sub main'function_begin
341 &main'external_label($func);
350 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
352 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
353 elsif ($main'aout and !$main'pic)
355 else { $tmp=push(@out,".type\t$func,\@function\n"); }
356 push(@out,".align\t$align\n");
357 push(@out,"$func:\n");
369 sub main'function_begin_B
371 local($func,$extra)=@_;
373 &main'external_label($func);
382 { push(@out,"TYPE($func,\@function)\n"); }
384 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
385 elsif ($main'aout and !$main'pic)
387 else { push(@out,".type $func,\@function\n"); }
388 push(@out,".align\t$align\n");
389 push(@out,"$func:\n");
393 sub main'function_end
410 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
411 elsif ($main'coff or $main'aout)
413 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
414 push(@out,".ident \"$func\"\n");
419 sub main'function_end_A
433 sub main'function_end_B
439 push(@out,"${dot}L_${func}_end:\n");
441 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
442 elsif ($main'coff or $main'aout)
444 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
445 push(@out,".ident \"$func\"\n");
454 return(&main'DWP($stack+$num*4,"esp","",0));
461 &main'sub("esp",$num*4);
468 &main'add("esp",$num*4);
473 return(&main'DWP($_[0]*4,"esp","",0));
476 # Should use swtmp, which is above esp. Linix can trash the stack above esp
481 # return(&main'DWP(-($num+1)*4,"esp","",0));
486 if (!defined($com_start) or $main'elf)
487 { # Regarding $main'elf above...
488 # GNU and SVR4 as'es use different comment delimiters,
489 push(@out,"\n"); # so we just skip ELF comments...
497 { push(@out,"\t$com_start $_ $com_end\n"); }
503 if (!defined($label{$_[0]}))
505 $label{$_[0]}="${dot}${label}${_[0]}";
508 return($label{$_[0]});
513 if (!defined($label{$_[0]}))
515 $label{$_[0]}="${dot}${label}${_[0]}";
520 if ($_[1]>1) { main'align($_[1]); }
521 else { push(@out,".align $align\n"); }
523 push(@out,"$label{$_[0]}:\n");
528 # try to detect if SSE2 or MMX extensions were used on ELF platform...
529 if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
532 push (@out,"\n.comm\t${under}OPENSSL_ia32cap_P,4,4\n");
534 push (@out,".section\t.init\n");
535 # One can argue that it's wasteful to craft every
536 # SSE/MMX module with this snippet... Well, it's 72
537 # bytes long and for the moment we have two modules.
538 # Let's argue when we have 7 modules or so...
540 # $1<<10 sets a reserved bit to signal that variable
541 # was initialized already...
542 &main'picmeup("edx","OPENSSL_ia32cap_P");
575 push(@out,".section .rodata\n");
583 push(@out,"\t.long\t".join(',',@_)."\n");
588 my $val=$_[0],$p2,$i;
590 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
594 push(@out,".align\t$val\n");
597 # debug output functions: puts, putx, printf
602 &main'push('$Lstring' . ++$constl);
608 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
615 &main'push('$Lstring' . ++$constl);
616 &main'call('printf');
621 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
628 for ($i = @_ - 1; $i >= 0; $i--)
630 if ($i == 0) # change this to support %s format strings
632 &main'push('$Lstring' . ++$constl);
633 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
637 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
639 &main'push(($1 + $stack - $ostack) . '(%esp)');
647 &main'call('printf');
649 &main'add("esp",4*@_);
675 #if (defined(ELF) || defined(SOL)) && defined(PIC)
678 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
679 movl $sym\@GOT($regs{$dst}),$regs{$dst}
681 leal $sym,$regs{$dst}
686 elsif ($main'pic && ($main'elf || $main'aout))
688 &main'call(&main'label("PIC_me_up"));
689 &main'set_label("PIC_me_up");
690 &main'blindpop($dst);
691 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
692 &main'label("PIC_me_up") . "]");
693 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
697 &main'lea($dst,&main'DWP($sym));
701 sub main'blindpop { &out1("popl",@_); }
717 $tmp=<<___; # applies to both Cygwin and Mingw
724 local($ctor)="${under}_GLOBAL_\$I\$$f";
726 $tmp.=".type $ctor,\@function\n" if ($main'pic);
727 $tmp.=<<___; # OpenBSD way...
734 push(@out,$tmp) if ($tmp);