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'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("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 0x0f; .byte 0xa2"); }
209 sub main'rdtsc { &out0(".byte 0x0f; .byte 0x31"); }
210 sub main'halt { &out0("hlt"); }
213 sub main'emms { &out0("emms"); }
214 sub main'movd { &out2("movd",@_); }
215 sub main'movq { &out2("movq",@_); }
216 sub main'movdqu { &out2("movdqu",@_); }
217 sub main'movdqa { &out2("movdqa",@_); }
218 sub main'movdq2q{ &out2("movdq2q",@_); }
219 sub main'movq2dq{ &out2("movq2dq",@_); }
220 sub main'paddq { &out2("paddq",@_); }
221 sub main'pmuludq{ &out2("pmuludq",@_); }
222 sub main'psrlq { &out2("psrlq",@_); }
223 sub main'psllq { &out2("psllq",@_); }
224 sub main'pxor { &out2("pxor",@_); }
225 sub main'por { &out2("por",@_); }
226 sub main'pand { &out2("pand",@_); }
228 # The bswapl instruction is new for the 486. Emulate if i386.
233 &main'comment("bswapl @_");
234 &main'exch(main'HB(@_),main'LB(@_));
236 &main'exch(main'HB(@_),main'LB(@_));
246 local($name,$p1,$p2)=@_;
248 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
249 "rcll",0xD1D0,"rcrl",0xD1D8,
250 "shll",0xD1E0,"shrl",0xD1E8,
253 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
255 $op=$special{$name}|$reg_val{$p1};
256 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
257 $tmp2=sprintf(".byte %d\t",$op &0xff);
263 &main'comment("$name $p2 $p1");
267 push(@out,"\t$name\t");
272 $tmp1=sprintf("\t" x $ll);
274 push(@out,&conv($p1)."\n");
281 local(%special)=("bswapl",0x0FC8);
283 if ((defined($special{$name})) && defined($regs{$p1}))
285 $op=$special{$name}|$reg_val{$p1};
286 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
287 $tmp2=sprintf(".byte %d\t",$op &0xff);
293 &main'comment("$name $p2 $p1");
297 push(@out,"\t$name\t".&conv($p1)."\n");
305 push(@out,"\t$name\t*".&conv($p1)."\n");
310 push(@out,"\t$_[0]\n");
317 # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
319 $p=$regs{$p} if (defined($regs{$p}));
321 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
322 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
336 sub main'function_begin
340 &main'external_label($func);
349 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
351 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
352 elsif ($main'aout and !$main'pic)
354 else { $tmp=push(@out,".type\t$func,\@function\n"); }
355 push(@out,".align\t$align\n");
356 push(@out,"$func:\n");
368 sub main'function_begin_B
370 local($func,$extra)=@_;
372 &main'external_label($func);
381 { push(@out,"TYPE($func,\@function)\n"); }
383 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
384 elsif ($main'aout and !$main'pic)
386 else { push(@out,".type $func,\@function\n"); }
387 push(@out,".align\t$align\n");
388 push(@out,"$func:\n");
392 sub main'function_end
409 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
410 elsif ($main'coff or $main'aout)
412 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
413 push(@out,".ident \"$func\"\n");
418 sub main'function_end_A
432 sub main'function_end_B
438 push(@out,"${dot}L_${func}_end:\n");
440 { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
441 elsif ($main'coff or $main'aout)
443 else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
444 push(@out,".ident \"$func\"\n");
453 return(&main'DWP($stack+$num*4,"esp","",0));
460 &main'sub("esp",$num*4);
467 &main'add("esp",$num*4);
472 return(&main'DWP($_[0]*4,"esp","",0));
475 # Should use swtmp, which is above esp. Linix can trash the stack above esp
480 # return(&main'DWP(-($num+1)*4,"esp","",0));
485 if (!defined($com_start) or $main'elf)
486 { # Regarding $main'elf above...
487 # GNU and SVR4 as'es use different comment delimiters,
488 push(@out,"\n"); # so we just skip ELF comments...
496 { push(@out,"\t$com_start $_ $com_end\n"); }
502 if (!defined($label{$_[0]}))
504 $label{$_[0]}="${dot}${label}${_[0]}";
507 return($label{$_[0]});
512 if (!defined($label{$_[0]}))
514 $label{$_[0]}="${dot}${label}${_[0]}";
519 if ($_[1]>1) { main'align($_[1]); }
520 else { push(@out,".align $align\n"); }
522 push(@out,"$label{$_[0]}:\n");
527 # try to detect if SSE2 or MMX extensions were used on ELF platform...
528 if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
531 push (@out,"\n.comm\t${under}OPENSSL_ia32cap_P,4,4\n");
533 push (@out,".section\t.init\n");
534 # One can argue that it's wasteful to craft every
535 # SSE/MMX module with this snippet... Well, it's 72
536 # bytes long and for the moment we have two modules.
537 # Let's argue when we have 7 modules or so...
539 # $1<<10 sets a reserved bit to signal that variable
540 # was initialized already...
541 &main'picmeup("edx","OPENSSL_ia32cap_P");
561 .byte 0x0f; .byte 0xa2
574 push(@out,".section .rodata\n");
582 push(@out,"\t.long\t".join(',',@_)."\n");
587 my $val=$_[0],$p2,$i;
589 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
593 push(@out,".align\t$val\n");
596 # debug output functions: puts, putx, printf
601 &main'push('$Lstring' . ++$constl);
607 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
614 &main'push('$Lstring' . ++$constl);
615 &main'call('printf');
620 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
627 for ($i = @_ - 1; $i >= 0; $i--)
629 if ($i == 0) # change this to support %s format strings
631 &main'push('$Lstring' . ++$constl);
632 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
636 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
638 &main'push(($1 + $stack - $ostack) . '(%esp)');
646 &main'call('printf');
648 &main'add("esp",4*@_);
674 #if (defined(ELF) || defined(SOL)) && defined(PIC)
677 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
678 movl $sym\@GOT($regs{$dst}),$regs{$dst}
680 leal $sym,$regs{$dst}
685 elsif ($main'pic && ($main'elf || $main'aout))
687 &main'call(&main'label("PIC_me_up"));
688 &main'set_label("PIC_me_up");
689 &main'blindpop($dst);
690 &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
691 &main'label("PIC_me_up") . "]");
692 &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
696 &main'lea($dst,&main'DWP($sym));
700 sub main'blindpop { &out1("popl",@_); }
716 $tmp=<<___; # applies to both Cygwin and Mingw
723 local($ctor)="${under}_GLOBAL_\$I\$$f";
725 $tmp.=".type $ctor,\@function\n" if ($main'pic);
726 $tmp.=<<___; # OpenBSD way...
733 push(@out,$tmp) if ($tmp);