8 $align=($main'aout)?"4":"16";
9 $under=($main'aout)?"_":"";
10 $com_start=($main'sol)?"/":"#";
12 sub main'asm_init_output { @out=(); }
13 sub main'asm_get_output { return(@out); }
14 sub main'get_labels { return(@labels); }
15 sub main'external_label { push(@labels,@_); }
45 %regs=( 'eax', '%eax',
68 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
74 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
80 local($addr,$reg1,$reg2,$idx)=@_;
83 $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
84 $reg1="$regs{$reg1}" if defined($regs{$reg1});
85 $reg2="$regs{$reg2}" if defined($regs{$reg2});
86 $ret.=$addr if ($addr ne "") && ($addr ne 0);
90 { $ret.="($reg1,$reg2,$idx)"; }
92 { $ret.="($reg1,$reg2)"; }
101 return(&main'DWP(@_));
116 # local($addr,$reg1,$reg2,$idx)=@_;
120 # $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
121 # $reg1="$regs{$reg1}" if defined($regs{$reg1});
122 # $reg2="$regs{$reg2}" if defined($regs{$reg2});
123 # $ret.=$addr if ($addr ne "") && ($addr ne 0);
125 # { $ret.="($reg1,$reg2,$idx)"; }
127 # { $ret.="($reg1)" }
131 sub main'mov { &out2("movl",@_); }
132 sub main'movb { &out2("movb",@_); }
133 sub main'and { &out2("andl",@_); }
134 sub main'or { &out2("orl",@_); }
135 sub main'shl { &out2("sall",@_); }
136 sub main'shr { &out2("shrl",@_); }
137 sub main'xor { &out2("xorl",@_); }
138 sub main'xorb { &out2("xorb",@_); }
139 sub main'add { &out2("addl",@_); }
140 sub main'adc { &out2("adcl",@_); }
141 sub main'sub { &out2("subl",@_); }
142 sub main'rotl { &out2("roll",@_); }
143 sub main'rotr { &out2("rorl",@_); }
144 sub main'exch { &out2("xchg",@_); }
145 sub main'cmp { &out2("cmpl",@_); }
146 sub main'lea { &out2("leal",@_); }
147 sub main'mul { &out1("mull",@_); }
148 sub main'div { &out1("divl",@_); }
149 sub main'jmp { &out1("jmp",@_); }
150 sub main'jmp_ptr { &out1p("jmp",@_); }
151 sub main'je { &out1("je",@_); }
152 sub main'jle { &out1("jle",@_); }
153 sub main'jne { &out1("jne",@_); }
154 sub main'jnz { &out1("jnz",@_); }
155 sub main'jz { &out1("jz",@_); }
156 sub main'jge { &out1("jge",@_); }
157 sub main'jl { &out1("jl",@_); }
158 sub main'jb { &out1("jb",@_); }
159 sub main'jc { &out1("jc",@_); }
160 sub main'jnc { &out1("jnc",@_); }
161 sub main'jno { &out1("jno",@_); }
162 sub main'dec { &out1("decl",@_); }
163 sub main'inc { &out1("incl",@_); }
164 sub main'push { &out1("pushl",@_); $stack+=4; }
165 sub main'pop { &out1("popl",@_); $stack-=4; }
166 sub main'not { &out1("notl",@_); }
167 sub main'call { &out1("call",$under.$_[0]); }
168 sub main'ret { &out0("ret"); }
169 sub main'nop { &out0("nop"); }
171 # The bswapl instruction is new for the 486. Emulate if i386.
176 &main'comment("bswapl @_");
177 &main'exch(main'HB(@_),main'LB(@_));
179 &main'exch(main'HB(@_),main'LB(@_));
189 local($name,$p1,$p2)=@_;
191 local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
192 "rcll",0xD1D0,"rcrl",0xD1D8,
193 "shll",0xD1E0,"shrl",0xD1E8,
196 if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
198 $op=$special{$name}|$reg_val{$p1};
199 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
200 $tmp2=sprintf(".byte %d\t",$op &0xff);
206 &main'comment("$name $p2 $p1");
210 push(@out,"\t$name\t");
215 $tmp1=sprintf("\t" x $ll);
217 push(@out,&conv($p1)."\n");
224 local(%special)=("bswapl",0x0FC8);
226 if ((defined($special{$name})) && defined($regs{$p1}))
228 $op=$special{$name}|$reg_val{$p1};
229 $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
230 $tmp2=sprintf(".byte %d\t",$op &0xff);
236 &main'comment("$name $p2 $p1");
240 push(@out,"\t$name\t".&conv($p1)."\n");
248 push(@out,"\t$name\t*".&conv($p1)."\n");
253 push(@out,"\t$_[0]\n");
260 # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
262 $p=$regs{$p} if (defined($regs{$p}));
264 $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
265 $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
281 sub main'function_begin
285 &main'external_label($func);
295 { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
297 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
298 else { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
299 push(@out,"$func:\n");
311 sub main'function_begin_B
313 local($func,$extra)=@_;
315 &main'external_label($func);
325 { push(@out,"\tTYPE($func,\@function)\n"); }
327 { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
328 else { push(@out,"\t.type $func,\@function\n"); }
329 push(@out,"$func:\n");
333 sub main'function_end
352 { push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
354 { $tmp=push(@out,"\t.align 4\n"); }
355 else { push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
356 push(@out,".ident \"$func\"\n");
361 sub main'function_end_A
375 sub main'function_end_B
381 push(@out,".L_${func}_end:\n");
383 { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
385 { push(@out,"\t.align 4\n"); }
386 else { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
387 push(@out,".ident \"desasm.pl\"\n");
396 return(&main'DWP($stack+$num*4,"esp","",0));
403 &main'sub("esp",$num*4);
410 &main'add("esp",$num*4);
415 return(&main'DWP($_[0]*4,"esp","",0));
418 # Should use swtmp, which is above esp. Linix can trash the stack above esp
423 # return(&main'DWP(-($num+1)*4,"esp","",0));
433 { push(@out,"\t$com_start $_ $com_end\n"); }
439 if (!defined($label{$_[0]}))
441 $label{$_[0]}=".${label}${_[0]}";
444 return($label{$_[0]});
449 if (!defined($label{$_[0]}))
451 $label{$_[0]}=".${label}${_[0]}";
454 push(@out,".align $align\n") if ($_[1] != 0);
455 push(@out,"$label{$_[0]}:\n");
464 push(@out,"\t.long $_[0]\n");
467 # debug output functions: puts, putx, printf
472 &main'push('$Lstring' . ++$constl);
478 $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
485 &main'push('$Lstring' . ++$constl);
486 &main'call('printf');
491 $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
498 for ($i = @_ - 1; $i >= 0; $i--)
501 if ($i == 0) # change this to support %s format strings
503 &main'push('$Lstring' . $constl);
504 $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
508 if ($_[$i] =~ /([0-9]*)\(%esp\)/)
510 &main'push(($1 + $stack - $ostack) . '(%esp)');
518 &main'call('printf');
521 &main'add("esp",4*@_);