Skip to content

Commit 54135fa

Browse files
committed
Perlito5::X64::Assembler - add some mov variants
1 parent 27f3c4b commit 54135fa

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src5/lib/Perlito5/X64/Assembler.pm

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,27 @@ sub emitl {
113113
emit( ( $v >> 24 ) & 0xFF );
114114
}
115115

116+
sub emit_rex_64 {
117+
my ($reg, $rm_reg) = @_;
118+
if ( is_register($reg) && is_register($rm_reg) ) {
119+
emit(0x48 | $reg->high_bit() << 2 | $rm_reg->high_bit());
120+
}
121+
else {
122+
die "emit_rex_64: don't know what to do with $reg, $rm_reg";
123+
}
124+
}
125+
126+
# Emit a ModR/M byte with registers coded in the reg and rm_reg fields.
127+
sub emit_modrm {
128+
my ($reg, $rm_reg) = @_;
129+
if ( is_register($reg) && is_register($rm_reg) ) {
130+
emit(0xC0 | $reg->low_bits() << 3 | $rm_reg->low_bits());
131+
}
132+
else {
133+
die "emit_modrm: don't know what to do with $reg, $rm_reg";
134+
}
135+
}
136+
116137
sub is_register {
117138
ref($_[0]) eq 'Perlito5::X64::Register'
118139
}
@@ -140,6 +161,56 @@ sub is_uint16 {
140161

141162
#--- instructions
142163

164+
sub _movl {
165+
my ( $dst, $src ) = @_;
166+
if ( is_register($dst) && is_register($src) ) {
167+
if ($src->low_bits() == 4) {
168+
emit_optional_rex_32($src, $dst);
169+
emit(0x89);
170+
emit_modrm($src, $dst);
171+
}
172+
else {
173+
emit_optional_rex_32($dst, $src);
174+
emit(0x8B);
175+
emit_modrm($dst, $src);
176+
}
177+
}
178+
else {
179+
die "movl: don't know what to do with $dst, $src";
180+
}
181+
}
182+
183+
sub _movq {
184+
my ( $dst, $src ) = @_;
185+
if ( is_register($dst) && is_register($src) ) {
186+
if ($src->low_bits() == 4) {
187+
emit_rex_64($src, $dst);
188+
emit(0x89);
189+
emit_modrm($src, $dst);
190+
}
191+
else {
192+
emit_rex_64($dst, $src);
193+
emit(0x8B);
194+
emit_modrm($dst, $src);
195+
}
196+
}
197+
else {
198+
die "movq: don't know what to do with $dst, $src";
199+
}
200+
}
201+
202+
sub _movsxlq {
203+
my ( $dst, $src ) = @_;
204+
if ( is_register($dst) && is_register($src) ) {
205+
emit_rex_64($src, $dst);
206+
emit(0x63);
207+
emit_modrm($src, $dst);
208+
}
209+
else {
210+
die "movsxlq: don't know what to do with $dst, $src";
211+
}
212+
}
213+
143214
sub _nop {
144215
emit(0x90);
145216
}

t5-x64/01_sanity.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@ say "1..4";
4646
_pop( rax );
4747
_pop( r14 );
4848
say "# pop " . to_hex();
49+
50+
asm_reset();
51+
_movq( rax, rcx );
52+
say "# movq " . to_hex();
4953
}
5054

0 commit comments

Comments
 (0)