x86unix.pl 9.4 KB
Newer Older
1 2
#!/usr/local/bin/perl

3
package x86unix;
4 5

$label="L000";
6
$const="";
U
Ulf Möller 已提交
7
$constl=0;
8 9 10 11 12

$align=($main'aout)?"4":"16";
$under=($main'aout)?"_":"";
$com_start=($main'sol)?"/":"#";

13 14 15 16 17
sub main'asm_init_output { @out=(); }
sub main'asm_get_output { return(@out); }
sub main'get_labels { return(@labels); }
sub main'external_label { push(@labels,@_); }

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
if ($main'cpp)
	{
	$align="ALIGN";
	$under="";
	$com_start='/*';
	$com_end='*/';
	}

%lb=(	'eax',	'%al',
	'ebx',	'%bl',
	'ecx',	'%cl',
	'edx',	'%dl',
	'ax',	'%al',
	'bx',	'%bl',
	'cx',	'%cl',
	'dx',	'%dl',
	);

%hb=(	'eax',	'%ah',
	'ebx',	'%bh',
	'ecx',	'%ch',
	'edx',	'%dh',
	'ax',	'%ah',
	'bx',	'%bh',
	'cx',	'%ch',
	'dx',	'%dh',
	);

%regs=(	'eax',	'%eax',
	'ebx',	'%ebx',
	'ecx',	'%ecx',
	'edx',	'%edx',
	'esi',	'%esi',
	'edi',	'%edi',
	'ebp',	'%ebp',
	'esp',	'%esp',
	);

56 57 58 59 60 61 62 63 64 65 66
%reg_val=(
	'eax',	0x00,
	'ebx',	0x03,
	'ecx',	0x01,
	'edx',	0x02,
	'esi',	0x06,
	'edi',	0x07,
	'ebp',	0x05,
	'esp',	0x04,
	);

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
sub main'LB
	{
	(defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
	return($lb{$_[0]});
	}

sub main'HB
	{
	(defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
	return($hb{$_[0]});
	}

sub main'DWP
	{
	local($addr,$reg1,$reg2,$idx)=@_;

	$ret="";
84
	$addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
85 86 87 88
	$reg1="$regs{$reg1}" if defined($regs{$reg1});
	$reg2="$regs{$reg2}" if defined($regs{$reg2});
	$ret.=$addr if ($addr ne "") && ($addr ne 0);
	if ($reg2 ne "")
89 90 91 92 93 94
		{
		if($idx ne "")
		    { $ret.="($reg1,$reg2,$idx)"; }
		else
		    { $ret.="($reg1,$reg2)"; }
	        }
95
	else
96
		{ $ret.="($reg1)" }
97 98 99 100 101
	return($ret);
	}

sub main'BP
	{
102
	return(&main'DWP(@_));
103 104
	}

105 106 107 108 109 110 111 112 113 114
sub main'BC
	{
	return @_;
	}

sub main'DWC
	{
	return @_;
	}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#sub main'BP
#	{
#	local($addr,$reg1,$reg2,$idx)=@_;
#
#	$ret="";
#
#	$addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
#	$reg1="$regs{$reg1}" if defined($regs{$reg1});
#	$reg2="$regs{$reg2}" if defined($regs{$reg2});
#	$ret.=$addr if ($addr ne "") && ($addr ne 0);
#	if ($reg2 ne "")
#		{ $ret.="($reg1,$reg2,$idx)"; }
#	else
#		{ $ret.="($reg1)" }
#	return($ret);
#	}

132 133 134 135 136 137 138
sub main'mov	{ &out2("movl",@_); }
sub main'movb	{ &out2("movb",@_); }
sub main'and	{ &out2("andl",@_); }
sub main'or	{ &out2("orl",@_); }
sub main'shl	{ &out2("sall",@_); }
sub main'shr	{ &out2("shrl",@_); }
sub main'xor	{ &out2("xorl",@_); }
139
sub main'xorb	{ &out2("xorb",@_); }
140
sub main'add	{ &out2("addl",@_); }
141
sub main'adc	{ &out2("adcl",@_); }
142 143 144 145 146
sub main'sub	{ &out2("subl",@_); }
sub main'rotl	{ &out2("roll",@_); }
sub main'rotr	{ &out2("rorl",@_); }
sub main'exch	{ &out2("xchg",@_); }
sub main'cmp	{ &out2("cmpl",@_); }
147 148 149
sub main'lea	{ &out2("leal",@_); }
sub main'mul	{ &out1("mull",@_); }
sub main'div	{ &out1("divl",@_); }
150
sub main'jmp	{ &out1("jmp",@_); }
151
sub main'jmp_ptr { &out1p("jmp",@_); }
152
sub main'je	{ &out1("je",@_); }
153
sub main'jle	{ &out1("jle",@_); }
154 155 156
sub main'jne	{ &out1("jne",@_); }
sub main'jnz	{ &out1("jnz",@_); }
sub main'jz	{ &out1("jz",@_); }
157 158 159 160 161 162
sub main'jge	{ &out1("jge",@_); }
sub main'jl	{ &out1("jl",@_); }
sub main'jb	{ &out1("jb",@_); }
sub main'jc	{ &out1("jc",@_); }
sub main'jnc	{ &out1("jnc",@_); }
sub main'jno	{ &out1("jno",@_); }
163
sub main'dec	{ &out1("decl",@_); }
164 165 166
sub main'inc	{ &out1("incl",@_); }
sub main'push	{ &out1("pushl",@_); $stack+=4; }
sub main'pop	{ &out1("popl",@_); $stack-=4; }
U
Ulf Möller 已提交
167 168
sub main'pushf	{ &out0("pushf"); $stack+=4; }
sub main'popf	{ &out0("popf"); $stack-=4; }
169
sub main'not	{ &out1("notl",@_); }
170
sub main'call	{ &out1("call",$under.$_[0]); }
171 172
sub main'ret	{ &out0("ret"); }
sub main'nop	{ &out0("nop"); }
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
# The bswapl instruction is new for the 486. Emulate if i386.
sub main'bswap
	{
	if ($main'i386)
		{
		&main'comment("bswapl @_");
		&main'exch(main'HB(@_),main'LB(@_));
		&main'rotr(@_,16);
		&main'exch(main'HB(@_),main'LB(@_));
		}
	else
		{
		&out1("bswapl",@_);
		}
	}

190 191 192 193
sub out2
	{
	local($name,$p1,$p2)=@_;
	local($l,$ll,$t);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	local(%special)=(	"roll",0xD1C0,"rorl",0xD1C8,
				"rcll",0xD1D0,"rcrl",0xD1D8,
				"shll",0xD1E0,"shrl",0xD1E8,
				"sarl",0xD1F8);
	
	if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
		{
		$op=$special{$name}|$reg_val{$p1};
		$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
		$tmp2=sprintf(".byte %d\t",$op     &0xff);
		push(@out,$tmp1);
		push(@out,$tmp2);

		$p2=&conv($p2);
		$p1=&conv($p1);
		&main'comment("$name $p2 $p1");
		return;
		}
212

213
	push(@out,"\t$name\t");
214 215
	$t=&conv($p2).",";
	$l=length($t);
216
	push(@out,$t);
217
	$ll=4-($l+9)/8;
218 219 220
	$tmp1=sprintf("\t" x $ll);
	push(@out,$tmp1);
	push(@out,&conv($p1)."\n");
221 222 223 224 225 226
	}

sub out1
	{
	local($name,$p1)=@_;
	local($l,$t);
227
	local(%special)=("bswapl",0x0FC8);
228

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	if ((defined($special{$name})) && defined($regs{$p1}))
		{
		$op=$special{$name}|$reg_val{$p1};
		$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
		$tmp2=sprintf(".byte %d\t",$op     &0xff);
		push(@out,$tmp1);
		push(@out,$tmp2);

		$p2=&conv($p2);
		$p1=&conv($p1);
		&main'comment("$name $p2 $p1");
		return;
		}

	push(@out,"\t$name\t".&conv($p1)."\n");
	}

sub out1p
	{
	local($name,$p1)=@_;
	local($l,$t);

	push(@out,"\t$name\t*".&conv($p1)."\n");
	}

sub out0
	{
	push(@out,"\t$_[0]\n");
257 258 259 260 261 262 263 264 265 266
	}

sub conv
	{
	local($p)=@_;

#	$p =~ s/0x([0-9A-Fa-f]+)/0$1h/;

	$p=$regs{$p} if (defined($regs{$p}));

267
	$p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
268 269 270 271 272 273 274 275
	$p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
	return $p;
	}

sub main'file
	{
	local($file)=@_;

276
	local($tmp)=<<"EOF";
277 278 279 280
	.file	"$file.s"
	.version	"01.01"
gcc2_compiled.:
EOF
281
	push(@out,$tmp);
282 283 284 285
	}

sub main'function_begin
	{
286
	local($func)=@_;
287

288
	&main'external_label($func);
289 290
	$func=$under.$func;

291
	local($tmp)=<<"EOF";
292 293 294 295
.text
	.align $align
.globl $func
EOF
296
	push(@out,$tmp);
297
	if ($main'cpp)
298
		{ $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
U
Ulf Möller 已提交
299 300
	elsif ($main'gaswin)
		{ $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
301 302 303
	else	{ $tmp=push(@out,"\t.type\t$func,\@function\n"); }
	push(@out,"$func:\n");
	$tmp=<<"EOF";
304 305 306 307 308 309
	pushl	%ebp
	pushl	%ebx
	pushl	%esi
	pushl	%edi

EOF
310
	push(@out,$tmp);
311 312 313
	$stack=20;
	}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
sub main'function_begin_B
	{
	local($func,$extra)=@_;

	&main'external_label($func);
	$func=$under.$func;

	local($tmp)=<<"EOF";
.text
	.align $align
.globl $func
EOF
	push(@out,$tmp);
	if ($main'cpp)
		{ push(@out,"\tTYPE($func,\@function)\n"); }
U
Ulf Möller 已提交
329 330
	elsif ($main'gaswin)
		{ $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
331 332 333 334 335
	else	{ push(@out,"\t.type	$func,\@function\n"); }
	push(@out,"$func:\n");
	$stack=4;
	}

336 337 338 339 340 341
sub main'function_end
	{
	local($func)=@_;

	$func=$under.$func;

342
	local($tmp)=<<"EOF";
343 344 345 346 347
	popl	%edi
	popl	%esi
	popl	%ebx
	popl	%ebp
	ret
348
.${func}_end:
349
EOF
350
	push(@out,$tmp);
351

352
	if ($main'cpp)
353
		{ push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
U
Ulf Möller 已提交
354 355
	elsif ($main'gaswin)
                { $tmp=push(@out,"\t.align 4\n"); }
356 357
	else	{ push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
	push(@out,".ident	\"$func\"\n");
358 359 360 361 362 363 364 365
	$stack=0;
	%label=();
	}

sub main'function_end_A
	{
	local($func)=@_;

366
	local($tmp)=<<"EOF";
367 368 369 370 371 372
	popl	%edi
	popl	%esi
	popl	%ebx
	popl	%ebp
	ret
EOF
373
	push(@out,$tmp);
374 375 376 377 378 379 380 381
	}

sub main'function_end_B
	{
	local($func)=@_;

	$func=$under.$func;

382
	push(@out,".L_${func}_end:\n");
383
	if ($main'cpp)
384
		{ push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
U
Ulf Möller 已提交
385 386
        elsif ($main'gaswin)
                { push(@out,"\t.align 4\n"); }
387
	else	{ push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
388
	push(@out,".ident	\"desasm.pl\"\n");
389 390 391 392 393 394 395 396 397 398 399
	$stack=0;
	%label=();
	}

sub main'wparam
	{
	local($num)=@_;

	return(&main'DWP($stack+$num*4,"esp","",0));
	}

400
sub main'stack_push
401
	{
402 403 404
	local($num)=@_;
	$stack+=$num*4;
	&main'sub("esp",$num*4);
405 406
	}

407
sub main'stack_pop
408 409
	{
	local($num)=@_;
410 411 412
	$stack-=$num*4;
	&main'add("esp",$num*4);
	}
413

414 415 416
sub main'swtmp
	{
	return(&main'DWP($_[0]*4,"esp","",0));
417 418
	}

419 420 421 422 423 424 425 426
# Should use swtmp, which is above esp.  Linix can trash the stack above esp
#sub main'wtmp
#	{
#	local($num)=@_;
#
#	return(&main'DWP(-($num+1)*4,"esp","",0));
#	}

427 428 429 430 431
sub main'comment
	{
	foreach (@_)
		{
		if (/^\s*$/)
432
			{ push(@out,"\n"); }
433
		else
434
			{ push(@out,"\t$com_start $_ $com_end\n"); }
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		}
	}

sub main'label
	{
	if (!defined($label{$_[0]}))
		{
		$label{$_[0]}=".${label}${_[0]}";
		$label++;
		}
	return($label{$_[0]});
	}

sub main'set_label
	{
	if (!defined($label{$_[0]}))
		{
		$label{$_[0]}=".${label}${_[0]}";
		$label++;
		}
455 456
	push(@out,".align $align\n") if ($_[1] != 0);
	push(@out,"$label{$_[0]}:\n");
457 458 459 460
	}

sub main'file_end
	{
461 462 463 464 465 466
	if ($const ne "")
		{
		push(@out,".section .rodata\n");
		push(@out,$const);
		$const="";
		}
467
	}
468 469 470 471 472

sub main'data_word
	{
	push(@out,"\t.long $_[0]\n");
	}
473

474 475
# debug output functions: puts, putx, printf

476 477
sub main'puts
	{
478
	&pushvars();
479 480 481 482
	&main'push('$Lstring' . ++$constl);
	&main'call('puts');
	$stack-=4;
	&main'add("esp",4);
483
	&popvars();
484

485
	$const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
486 487 488 489
	}

sub main'putx
	{
490
	&pushvars();
491 492 493 494
	&main'push($_[0]);
	&main'push('$Lstring' . ++$constl);
	&main'call('printf');
	&main'add("esp",8);
U
Ulf Möller 已提交
495
	$stack-=8;
496
	&popvars();
497

498 499 500 501 502 503
	$const .= "Lstring$constl:\n\t.string \"\%X\"\n";
	}

sub main'printf
	{
	$ostack = $stack;
504
	&pushvars();
505 506 507 508
	for ($i = @_ - 1; $i >= 0; $i--)
		{
		if ($i == 0) # change this to support %s format strings
			{
U
Ulf Möller 已提交
509
			&main'push('$Lstring' . ++$constl);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
			$const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
			}
		else
			{
			if ($_[$i] =~ /([0-9]*)\(%esp\)/)
				{
				&main'push(($1 + $stack - $ostack) . '(%esp)');
				}
			else
				{
				&main'push($_[$i]);
				}
			}
		}
	&main'call('printf');
525
	$stack-=4*@_;
526
	&main'add("esp",4*@_);
U
Ulf Möller 已提交
527
	&popvars();
528
	}
529 530 531

sub pushvars
	{
U
Ulf Möller 已提交
532
	&main'pushf();
533 534 535 536 537 538 539 540 541 542
	&main'push("edx");
	&main'push("ecx");
	&main'push("eax");
	}

sub popvars
	{
	&main'pop("eax");
	&main'pop("ecx");
	&main'pop("edx");
U
Ulf Möller 已提交
543
	&main'popf();
544
	}