Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5::X64::Assembler - more push/pop
  • Loading branch information
fglock committed Mar 24, 2013
1 parent 0e13f14 commit 073e847
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
36 changes: 35 additions & 1 deletion src5/lib/Perlito5/X64/Assembler.pm
Expand Up @@ -82,10 +82,28 @@ sub to_hex {
map( $hex_char[int($_ / 16)] . $hex_char[$_ % 16], @buffer ) );
}

sub asm_reset {
@buffer = ();
}

sub emit {
push @buffer, $_[0];
}

sub emitw {
my ($v) = @_;
emit( $v & 0xFF );
emit( ( $v >> 8 ) & 0xFF );
}

sub emitl {
my ($v) = @_;
emit( $v & 0xFF );
emit( ( $v >> 8 ) & 0xFF );
emit( ( $v >> 16 ) & 0xFF );
emit( ( $v >> 24 ) & 0xFF );
}

sub is_register {
ref($_[0]) eq 'Perlito5::X64::Register'
}
Expand Down Expand Up @@ -124,21 +142,37 @@ sub _pop {
emit(0x58 | $dst->low_bits());
}
else {
die "push: don't know what to do with $dst";
die "pop: don't know what to do with $dst";
}
}

sub _popfq {
emit(0x9D);
}

sub _push {
my ( $src ) = @_;
if ( is_register($src) ) {
$src->emit_optional_rex_32();
emit(0x50 | $src->low_bits());
}
elsif (is_int8($src)) {
emit(0x6A);
emit($src); # Emit low byte of value.
}
elsif (!ref($src)) {
emit(0x68);
emitl($src); # int32
}
else {
die "push: don't know what to do with $src";
}
}

sub _pushfq {
emit(0x9C);
}

sub _ret {
my ( $imm16 ) = @_;
if ( !$imm16 ) {
Expand Down
11 changes: 10 additions & 1 deletion t5-x64/01_sanity.t
Expand Up @@ -25,6 +25,7 @@ say "1..4";
print "not " if $out;
say "ok # !is_register";

asm_reset();
_ret();
$out = to_hex();
print "not " if $out ne 'C3';
Expand All @@ -34,8 +35,16 @@ say "1..4";
print "not " if $out ne 'C3 C2 0A 00';
say "ok # $out";

asm_reset();
_push( rax );
_push( r14 );
say "# " . to_hex();
_push( 120 );
# TODO - test negative numbers
say "# push " . to_hex();

asm_reset();
_pop( rax );
_pop( r14 );
say "# pop " . to_hex();
}

0 comments on commit 073e847

Please sign in to comment.