3 package x86unix; # GAS actually...
9 $align=($main'aout)?"4":"16";
10 $under=($main'aout or $main'coff)?"_":"";
11 $com_start="#" if ($main'aout or $main'coff);
13 sub main'asm_init_output { @out=(); }
14 sub main'asm_get_output { return(@out); }
15 sub main'get_labels { return(@labels); }
16 sub main'external_label { push(@labels,@_); }
46 %regs=( 'eax', '%eax',
87 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
93 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
99 local($addr,$reg1,$reg2,$idx)=@_;
102 $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
103 $reg1="$regs{$reg1}" if defined($regs{$reg1});
104 $reg2="$regs{$reg2}" if defined($regs{$reg2});
105 $ret.=$addr if ($addr ne "") && ($addr ne 0);
108 if($idx ne "" && $idx != 0)
109 { $ret.="($reg1,$reg2,$idx)"; }
111 { $ret.="($reg1,$reg2)"; }
120 return(&main'DWP(@_));
125 return(&main'DWP(@_));
140 # local($addr,$reg1,$reg2,$idx)=@_;
144 # $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
145 # $reg1="$regs{$reg1}" if defined($regs{$reg1});
146 # $reg2="$regs{$reg2}" if defined($regs{$reg2});
147 # $ret.=$addr if ($addr ne "") && ($addr ne 0);
149 # { $ret.="($reg1,$reg2,$idx)"; }
151 # { $ret.="($reg1)" }
155 sub main'mov { &out2("movl",@_); }
156 sub main'movb { &out2("movb",@_); }
157 sub main'and { &out2("andl",@_); }
158 sub main'or { &out2("orl",@_); }
159 sub main'shl { &out2("sall",@_); }
160 sub main'shr { &out2("shrl",@_); }
161 sub main'xor { &out2("xorl",@_); }
162 sub main'xorb { &out2("xorb",@_); }
163 sub main'add { &out2("addl",@_); }
164 sub main'adc { &out2("adcl",@_); }
165 sub main'sub { &out2("subl",@_); }
166 sub main'rotl { &out2("roll",@_); }
167 sub main'rotr { &out2("rorl",@_); }
168 sub main'exch { &out2("xchg",@_); }
169 sub main'cmp { &out2("cmpl",@_); }
170 sub main'lea { &out2("leal",@_); }
171 sub main'mul { &out1("mull",@_); }
172 sub main'div { &out1("divl",@_); }
173 sub main'jmp { &out1("jmp",@_); }
174 sub main'jmp_ptr { &out1p("jmp",@_); }
175 sub main'je { &out1("je",@_); }
176 sub main'jle { &out1("jle",@_); }
177 sub main'jne { &out1("jne",@_); }
178 sub main'jnz { &out1("jnz",@_); }
179 sub main'jz { &out1("jz",@_); }
180 sub main'jge { &out1("jge",@_); }
181 sub main'jl { &out1("jl",@_); }
182 sub main'ja { &out1("ja",@_); }
183 sub main'jae { &out1("jae",@_); }
184 sub main'jb { &out1("jb",@_); }
185 sub main'jbe { &out1("jbe",@_); }
186 sub main'jc { &out1("jc",@_); }
187 sub main'jnc { &out1("jnc",@_); }
188 sub main'jno { &out1("jno",@_); }
189 sub main'dec { &out1("decl",@_); }
190 sub main'inc { &out1("incl",@_); }
191 sub main'push { &out1("pushl",@_); $stack+=4; }
192 sub main'pop { &out1("popl",@_); $stack-=4; }
193 sub main'pushf { &out0("pushf"); $stack+=4; }
194 sub main'popf { &out0("popf"); $stack-=4; }
195 sub main'not { &out1("notl",@_); }
196 sub main'call { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); }
197 sub main'ret { &out0("ret"); }
198 sub main'nop { &out0("nop"); }
199 sub main'test { &out2("testl",@_); }
200 sub main'bt { &out2("btl",@_); }
201 sub main'leave { &out0("leave"); }
202 sub main'cpuid { &out0(".word\t0xa20f"); }
203 sub main'rdtsc { &out0(".word\t0x310f"); }
206 sub main'emms { &out0("emms"); }
207 sub main'movd { &out2("movd",@_); }
208 sub main'movq { &out2("movq",@_); }
209 sub main'movdqu { &out2("movdqu",@_); }
210 sub main'movdqa { &out2("movdqa",@_); }
211 sub main'movdq2q{ &out2("movdq2q",@_); }
212 sub main'movq2dq{ &out2("movq2dq",@_); }
213 sub main'paddq { &out2("paddq",@_); }
214 sub main'pmuludq{ &out2("pmuludq",@_); }
215 sub main'psrlq { &out2("psrlq",@_); }
216 sub main'psllq { &out2("psllq",@_); }
217 sub main'pxor { &out2("pxor",@_); }
218 sub main'por { &out2("por",@_); }
219 sub main'pand { &out2("pand",@_); }
221 # The bswapl instruction is new for the 486. Emulate if i386.
226 &main'comment("bswapl @_");
227 &main'exch(main'HB(@_),main'LB(@_));
229 &main'exch(main'HB(@_),main'LB(@_));
239 local($name,$p1,$p2)=@_;
241 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
242 "rcll",0xD1D0,"rcrl",0xD1D8,
243 "shll",0xD1E0,"shrl",0xD1E8,
246 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
248 $op=$special{$name}|$reg_val{$p1};
249 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
250 $tmp2=sprintf(".byte %d\t",$op &0xff);
256 &main'comment("$name $p2 $p1");
260 push(@out,"\t$name\t");
265 $tmp1=sprintf("\t" x $ll);
267 push(@out,&conv($p1)."\n");
274 local(%special)=("bswapl",0x0FC8);
276 if ((defined($special{$name})) && defined($regs{$p1}))
278 $op=$special{$name}|$reg_val{$p1};
279 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
280 $tmp2=sprintf(".byte %d\t",$op &0xff);
286 &main'comment("$name $p2 $p1");
290 push(@out,"\t$name\t".&conv($p1)."\n");
298 push(@out,"\t$name\t*".&conv($p1)."\n");
303 push(@out,"\t$_[0]\n");
310 # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
312 $p=$regs{$p} if (defined($regs{$p}));
314 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
315 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
329 sub main'function_begin
333 &main'external_label($func);
343 { $tmp=push(@out,"TYPE($func,\@function)\n"); }
345 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
348 else { $tmp=push(@out,".type\t$func,\@function\n"); }
349 push(@out,"$func:\n");
361 sub main'function_begin_B
363 local($func,$extra)=@_;
365 &main'external_label($func);
375 { push(@out,"TYPE($func,\@function)\n"); }
377 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
380 else { push(@out,".type $func,\@function\n"); }
381 push(@out,"$func:\n");
385 sub main'function_end
402 { push(@out,"SIZE($func,.L_${func}_end-$func)\n"); }
403 elsif ($main'coff or $main'aout)
404 { $tmp=push(@out,".align $align\n"); }
405 else { push(@out,".size\t$func,.L_${func}_end-$func\n"); }
406 push(@out,".ident \"$func\"\n");
411 sub main'function_end_A
425 sub main'function_end_B
431 push(@out,".L_${func}_end:\n");
433 { push(@out,"SIZE($func,.L_${func}_end-$func)\n"); }
434 elsif ($main'coff or $main'aout)
435 { push(@out,".align $align\n"); }
436 else { push(@out,".size\t$func,.L_${func}_end-$func\n"); }
437 push(@out,".ident \"$func\"\n");
446 return(&main'DWP($stack+$num*4,"esp","",0));
453 &main'sub("esp",$num*4);
460 &main'add("esp",$num*4);
465 return(&main'DWP($_[0]*4,"esp","",0));
468 # Should use swtmp, which is above esp. Linix can trash the stack above esp
473 # return(&main'DWP(-($num+1)*4,"esp","",0));
478 return if (!defined($com_start);
479 if ($main'elf) # GNU and SVR4 as'es use different comment delimiters,
480 { # so we just skip comments...
489 { push(@out,"\t$com_start $_ $com_end\n"); }
495 if (!defined($label{$_[0]}))
497 $label{$_[0]}=".${label}${_[0]}";
500 return($label{$_[0]});
505 if (!defined($label{$_[0]}))
507 $label{$_[0]}=".${label}${_[0]}";
510 push(@out,".align $align\n") if ($_[1] != 0);
511 push(@out,"$label{$_[0]}:\n");
516 # try to detect if SSE2 or MMX extensions were used on ELF platform...
517 if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
520 push (@out,"\n.comm\t".$under."OPENSSL_ia32cap,8,4\n");
522 push (@out,".section\t.init\n");
523 # One can argue that it's wasteful to craft every
524 # SSE/MMX module with this snippet... Well, it's 72
525 # bytes long and for the moment we have two modules.
526 # Let's argue when we have 7 modules or so...
528 # $1<<10 sets a reserved bit to signal that variable
529 # was initialized already...
530 &main'picmeup("edx","OPENSSL_ia32cap");
564 push(@out,".section .rodata\n");
572 push(@out,"\t.long\t".join(',',@_)."\n");
577 my $val=$_[0],$p2,$i;
579 for ($p2=0;$val!=0;$val>>=1) { $p2++; }
583 push(@out,".align $val\n");
586 # debug output functions: puts, putx, printf
591 &main'push('$Lstring' . ++$constl);
597 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
604 &main'push('$Lstring' . ++$constl);
605 &main'call('printf');
610 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
617 for ($i = @_ - 1; $i >= 0; $i--)
619 if ($i == 0) # change this to support %s format strings
621 &main'push('$Lstring' . ++$constl);
622 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
626 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
628 &main'push(($1 + $stack - $ostack) . '(%esp)');
636 &main'call('printf');
638 &main'add("esp",4*@_);
664 #if (defined(ELF) || defined(SOL)) && defined(PIC)
668 addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
669 movl $sym\@GOT($regs{$dst}),$regs{$dst}
671 leal $sym,$regs{$dst}
676 elsif ($main'pic && ($main'elf || $main'aout))
678 &main'call(&main'label("PIC_me_up"));
679 &main'set_label("PIC_me_up");
680 &main'blindpop($dst);
681 &main'add($dst,"\$$under"."_GLOBAL_OFFSET_TABLE_+[.-".
682 &main'label("PIC_me_up") . "]");
683 &main'mov($dst,&main'DWP($sym."\@GOT",$dst));
687 &main'lea($dst,&main'DWP($sym));
691 sub main'blindpop { &out1("popl",@_); }