Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5::X64::Assembler - add cpuid, neg, leave, int3
  • Loading branch information
fglock committed Mar 24, 2013
1 parent 84ad906 commit d57161d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src5/lib/Perlito5/X64/Assembler.pm
Expand Up @@ -197,12 +197,18 @@ sub emit_rex_64 {
}
}

# Emit a ModR/M byte with registers coded in the reg and rm_reg fields.
sub emit_modrm {
my ($reg, $rm_reg) = @_;
if ( is_register($reg) && is_register($rm_reg) ) {
# Emit a ModR/M byte with registers coded in the reg and rm_reg fields.
emit(0xC0 | $reg->low_bits() << 3 | $rm_reg->low_bits());
}
elsif ( is_register($rm_reg) ) {
# Emit a ModR/M byte with an operation subcode in the reg field and
# a register in the rm_reg field.
my ($code, $rm_reg) = @_;
emit(0xC0 | $code << 3 | $rm_reg->low_bits());
}
else {
die "emit_modrm: don't know what to do with $reg, $rm_reg";
}
Expand Down Expand Up @@ -255,6 +261,24 @@ sub _bind {
$label->bind( scalar(@buffer) );
}

sub _cpuid {
emit(0x0F);
emit(0xA2);
}

sub _cqo {
emit_rex_64();
emit(0x99);
}

sub _hlt {
emit(0xF4);
}

sub _int3 {
emit(0xCC);
}

sub _jmp {
my ( $label, $distance ) = @_;

Expand All @@ -279,6 +303,10 @@ sub _jmp {
}
}

sub _leave {
emit(0xC9);
}

sub _movl {
my ( $dst, $src ) = @_;
if ( is_register($dst) && is_register($src) ) {
Expand Down Expand Up @@ -351,6 +379,27 @@ sub _repmovsq() {
emit(0xA5);
}

sub _mul {
my ($src) = @_;
emit_rex_64($src);
emit(0xF7);
emit_modrm( 0x4, $src );
}

sub _neg {
my ($dst) = @_;
emit_rex_64($dst);
emit(0xF7);
emit_modrm( 0x3, $dst );
}

sub _negl {
my ($dst) = @_;
emit_optional_rex_32($dst);
emit(0xF7);
emit_modrm( 0x3, $dst );
}

sub _nop {
emit(0x90);
}
Expand Down
1 change: 1 addition & 0 deletions t5-x64/01_sanity.t
Expand Up @@ -59,6 +59,7 @@ say "1..4";
my $here = label;
_xchg( rax, rcx );
_bind($here);
_neg( rbx );
_jmp($here);
say "# xchg " . to_hex();
say "# label pos=", $here->pos();
Expand Down

0 comments on commit d57161d

Please sign in to comment.