Perlito5/X64/Assembler.pm untested

filter: Statementstmt 0.0% 0 / 330 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 93 MC/DCmcdc 0.0% 0 / 57 Subroutinesub 0.0% 0 / 115 Podpod 0.0% 0 / 113 totaltotal 0.0% 0 / 810 CC 52 SCAR 24.2
CC
52
Cov
0%
Operand
56.0
_j
56.0
_jmp
30.0
CRAP 11.2
1 use strict;
2 use warnings;
3  
4  
5 package Perlito5::X64::Label;
6  
7 sub new {
8 my $class = shift;
9 bless {@_}, $class;
10 }
11  
12 sub pos {
13 return $_[0]->{pos}
14 }
15  
16 sub bind {
17 $_[0]->{pos} = $_[1];
18 }
19  
20 sub is_bound {
21 return defined( $_[0]->{pos} );
22 }
23  
24  
25 package Perlito5::X64::Register;
26  
27 sub new {
28 my $class = shift;
29 bless {@_}, $class;
30 }
31  
32 sub code {
33 return $_[0]->{code}
34 }
35  
36 sub is {
37 return $_[0]->{code} == $_[1]->{code};
38 }
39  
40 # Return the high bit of the register code as a 0 or 1. Used often when constructing the REX prefix byte.
41 sub high_bit {
42 my ($reg) = @_;
43 return $reg->{code} >> 3;
44 }
45  
46 # Return the 3 low bits of the register code. Used when encoding registers in modR/M, SIB, and opcode bytes.
47 sub low_bits {
48 my ($reg) = @_;
49 return $reg->{code} & 0x7;
50 }
51  
52  
53 package Perlito5::X64::Immediate;
54  
55 sub new {
56 my $class = shift;
57 bless {@_}, $class;
58 }
59  
60  
61 package Perlito5::X64::Operand;
62  
63 sub new {
64 # Register base,
65 # Register index,
66 # ScaleFactor scale,
67 # int32_t disp
68  
69 # byte rex_;
70 # byte buf_[6];
71 # byte len_ # The number of bytes of buf_ in use.
72  
73 my $class = shift;
74 bless {@_}, $class;
75 }
76  
77 sub rsp () { Perlito5::X64::Assembler::rsp }
78 sub r12 () { Perlito5::X64::Assembler::r12 }
79  
80 sub set_modrm {
81 my ($op, $mod, $rm_reg) = @_;
82 die unless (is_uint2($mod));
83 $op->{buf_}[0] = $mod << 6 | $rm_reg->low_bits();
84 # Set REX.B to the high bit of rm.code().
85 $op->{rex_} |= $rm_reg->high_bit();
86 }
87
88
89 sub set_sib {
90 my ($op, $scale, $index, $base) = @_;
91 die unless ($op->{len_} == 1);
92 die unless (is_uint2($scale));
93 # Use SIB with no index register only for base rsp or r12. Otherwise we
94 # would skip the SIB byte entirely.
95 die unless (!$index->is(rsp) || $base->is(rsp) || $base->is(r12));
96 $op->{buf_}[1] = ($scale << 6) | ($index->low_bits() << 3) | $base->low_bits();
97 $op->{rex_} |= $index->high_bit() << 1 | $base->high_bit();
98 $op->{len_} = 2;
99 }
100
101 sub set_disp8 {
102 my ($op, $disp) = @_;
103 die unless (is_int8($disp));
104 die unless ($op->{len_} == 1 || $op->{len_} == 2);
105 $op->{buf_}[$op->{len_}] = $disp;
106 $op->{len_}++;
107 }
108
109 sub set_disp32 {
110 my ($op, $disp) = @_;
111 die unless ($op->{len_} == 1 || $op->{len_} == 2);
112 $op->{buf_}[$op->{len_} + 0] = ( $disp & 0xFF );
113 $op->{buf_}[$op->{len_} + 1] = ( ( $disp >> 8 ) & 0xFF );
114 $op->{buf_}[$op->{len_} + 2] = ( ( $disp >> 16 ) & 0xFF );
115 $op->{buf_}[$op->{len_} + 3] = ( ( $disp >> 24 ) & 0xFF );
116 $op->{len_} += 4;
117 }
118  
119  
120 package Perlito5::X64::Assembler;
121  
122 my @buffer;
123 my @hex_char = qw( 0 1 2 3 4 5 6 7 8 9 A B C D E F );
124 my $predictable_code_size = 1;
125  
126 #--- registers
127  
128 my $r_rax = Perlito5::X64::Register->new( code => 0 );
129 my $r_rcx = Perlito5::X64::Register->new( code => 1 );
130 my $r_rdx = Perlito5::X64::Register->new( code => 2 );
131 my $r_rbx = Perlito5::X64::Register->new( code => 3 );
132 my $r_rsp = Perlito5::X64::Register->new( code => 4 );
133 my $r_rbp = Perlito5::X64::Register->new( code => 5 );
134 my $r_rsi = Perlito5::X64::Register->new( code => 6 );
135 my $r_rdi = Perlito5::X64::Register->new( code => 7 );
136 my $r_r8 = Perlito5::X64::Register->new( code => 8 );
137 my $r_r9 = Perlito5::X64::Register->new( code => 9 );
138 my $r_r10 = Perlito5::X64::Register->new( code => 10 );
139 my $r_r11 = Perlito5::X64::Register->new( code => 11 );
140 my $r_r12 = Perlito5::X64::Register->new( code => 12 );
141 my $r_r13 = Perlito5::X64::Register->new( code => 13 );
142 my $r_r14 = Perlito5::X64::Register->new( code => 14 );
143 my $r_r15 = Perlito5::X64::Register->new( code => 15 );
144  
145 sub rax () { $r_rax }
146 sub rcx () { $r_rcx }
147 sub rdx () { $r_rdx }
148 sub rbx () { $r_rbx }
149 sub rsp () { $r_rsp }
150 sub rbp () { $r_rbp }
151 sub rsi () { $r_rsi }
152 sub rdi () { $r_rdi }
153 sub r8 () { $r_r8 }
154 sub r9 () { $r_r9 }
155 sub r10 () { $r_r10 }
156 sub r11 () { $r_r11 }
157 sub r12 () { $r_r12 }
158 sub r13 () { $r_r13 }
159 sub r14 () { $r_r14 }
160 sub r15 () { $r_r15 }
161  
162 #--- scale factors for operands
163  
164 sub times_1 () { 0 }
165 sub times_2 () { 1 }
166 sub times_4 () { 2 }
167 sub times_8 () { 3 }
168 sub times_int_size () { times_4 }
169 sub times_pointer_size () { times_8 }
170  
171 #--- conditions
172  
173 # # any value < 0 is considered no_condition
174 # no_condition = -1,
175  
176 sub overflow () { 0 }
177 sub no_overflow () { 1 }
178 sub below () { 2 }
179 sub above_equal () { 3 }
180 sub equal () { 4 }
181 sub not_equal () { 5 }
182 sub below_equal () { 6 }
183 sub above () { 7 }
184 sub negative () { 8 }
185 sub positive () { 9 }
186 sub parity_even () { 10 }
187 sub parity_odd () { 11 }
188 sub less () { 12 }
189 sub greater_equal () { 13 }
190 sub less_equal () { 14 }
191 sub greater () { 15 }
192  
193 # Fake conditions that are handled by the
194 # opcodes using them.
195 sub always () { 16 }
196 sub never () { 17 }
197  
198 # # aliases
199 # carry = below,
200 # not_carry = above_equal,
201 # zero = equal,
202 # not_zero = not_equal,
203 # sign = negative,
204 # not_sign = positive,
205 # last_condition = greater
206  
207 #--- general
208  
209 sub new {
210 my $class = shift;
211 @buffer = ();
212 bless {@_}, $class;
213 }
214  
215 sub to_hex {
216 return join(' ',
217 map( $hex_char[int($_ / 16)] . $hex_char[$_ % 16], @buffer ) );
218 }
219  
220 sub asm_reset {
221 @buffer = ();
222 }
223  
224 sub emit {
225 if ( ref($_[0]) eq 'Perlito5::X64::Immediate' ) {
226 emitl($_[0]->{value});
227 }
228 else {
229 push @buffer, $_[0];
230 }
231 }
232  
233 sub emitw {
234 my ($v) = @_;
235 emit( $v & 0xFF );
236 emit( ( $v >> 8 ) & 0xFF );
237 }
238  
239 sub emitl {
240 my ($v) = @_;
241 emit( $v & 0xFF );
242 emit( ( $v >> 8 ) & 0xFF );
243 emit( ( $v >> 16 ) & 0xFF );
244 emit( ( $v >> 24 ) & 0xFF );
245 }
246  
247 # emit REX if needed
248 sub emit_optional_rex_32 {
249 my ($reg) = @_;
250 if ( @_ == 1 && is_register($reg) ) {
251 Perlito5::X64::Assembler::emit(0x41) if $reg->high_bit();
252 }
253 else {
254 die "emit_optional_rex_32: don't know what to do with @_";
255 }
256 }
257  
258 sub emit_rex_64 {
259 my ($reg, $rm_reg) = @_;
260 if ( @_ == 0 ) {
261 # Emit a REX prefix that only sets REX.W to choose a 64-bit operand size.
262 emit(0x48);
263 }
264 elsif ( @_ == 1 && is_register($reg) ) {
265 # Emits a REX prefix that encodes a 64-bit operand size and
266 # the top bit of the register code.
267 # The high bit of register is used for REX.B.
268 # REX.W is set and REX.R and REX.X are clear.
269 emit(0x48 | $reg->high_bit());
270 }
271 elsif ( is_register($reg) && is_register($rm_reg) ) {
272 emit(0x48 | $reg->high_bit() << 2 | $rm_reg->high_bit());
273 }
274 else {
275 die "emit_rex_64: don't know what to do with @_";
276 }
277 }
278  
279 sub emit_operand {
280 my ($reg, $op) = @_;
281 if ( is_register($reg) && is_operand($op) ) {
282 # Emit the ModR/M byte, and optionally the SIB byte and
283 # 1- or 4-byte offset for a memory operand. Also encodes
284 # the second operand of the operation, a register or operation
285 # subcode, into the reg field of the ModR/M byte.
286 emit_operand($reg->low_bits(), $op);
287 }
288 elsif ( !ref($reg) && is_operand($op) ) {
289 # Emit the ModR/M byte, and optionally the SIB byte and
290 # 1- or 4-byte offset for a memory operand. Also used to encode
291 # a three-bit opcode extension into the ModR/M byte.
292  
293 # ASSERT(is_uint3(code));
294 # const unsigned length = adr.len_;
295 # ASSERT(length > 0);
296 #
297 # # Emit updated ModR/M byte containing the given register.
298 # ASSERT((adr.buf_[0] & 0x38) == 0);
299 # pc_[0] = adr.buf_[0] | code << 3;
300 #
301 # # Emit the rest of the encoded operand.
302 # for (unsigned i = 1; i < length; i++) pc_[i] = adr.buf_[i];
303 # pc_ += length;
304  
305 die "TODO";
306 }
307 else {
308 die "emit_operand: don't know what to do with @_";
309 }
310 }
311  
312 sub emit_modrm {
313 my ($reg, $rm_reg) = @_;
314 if ( is_register($reg) && is_register($rm_reg) ) {
315 # Emit a ModR/M byte with registers coded in the reg and rm_reg fields.
316 emit(0xC0 | $reg->low_bits() << 3 | $rm_reg->low_bits());
317 }
318 elsif ( is_register($rm_reg) ) {
319 # Emit a ModR/M byte with an operation subcode in the reg field and
320 # a register in the rm_reg field.
321 my ($code, $rm_reg) = @_;
322 emit(0xC0 | $code << 3 | $rm_reg->low_bits());
323 }
324 else {
325 die "emit_modrm: don't know what to do with @_";
326 }
327 }
328  
329 sub is_label {
330 ref($_[0]) eq 'Perlito5::X64::Label'
331 }
332  
333 sub is_register {
334 ref($_[0]) eq 'Perlito5::X64::Register'
335 }
336  
337 sub is_immediate {
338 ref($_[0]) eq 'Perlito5::X64::Immediate'
339 }
340  
341 sub is_operand {
342 ref($_[0]) eq 'Perlito5::X64::Operand'
343 }
344  
345 sub is_zero {
346 return $_[0] == 0;
347 }
348  
349 sub is_int8 {
350 return -128 <= $_[0] && $_[0] < 128;
351 }
352  
353 sub is_int16 {
354 return -32768 <= $_[0] && $_[0] < 32768;
355 }
356  
357 sub is_uint4 {
358 return 0 <= $_[0] && $_[0] < 16;
359 }
360  
361 sub is_uint8 {
362 return 0 <= $_[0] && $_[0] < 256;
363 }
364  
365 sub is_uint16 {
366 return 0 <= $_[0] && $_[0] < 65536;
367 }
368  
369 sub label {
370 return Perlito5::X64::Label->new();
371 }
372  
373 sub Immediate {
374 return Perlito5::X64::Immediate->new( value => $_[0] );
375 }
376  
377 sub Operand {
378 # Register base,
379 # Register index,
380 # ScaleFactor scale,
381 # int32_t disp
382  
383 # byte rex_;
384 # byte buf_[6];
385 # byte len_ # The number of bytes of buf_ in use.
386  
387 if ( @_ == 2 ) {
388 # [base + disp/r]
389 # Operand(Register base, int32_t disp);
390  
391 my $op = Perlito5::X64::Operand->( base => $_[0], disp => $_[1] );
392  
393 $op->{rex_} = 0;
394 $op->{len_} = 1;
395 if ($op->{base}->is(rsp) || $op->{base}->is(r12)) {
396 # SIB byte is needed to encode (rsp + offset) or (r12 + offset).
397 $op->set_sib(times_1, rsp, $op->{base});
398 }
399  
400 if ($op->{disp} == 0 && !$op->{base}->is(rbp) && !$op->{base}->is(r13)) {
401 $op->set_modrm(0, $op->{base});
402 }
403 elsif (is_int8($op->{disp})) {
404 $op->set_modrm(1, $op->{base});
405 $op->set_disp8($op->{disp});
406 }
407 else {
408 $op->set_modrm(2, $op->{base});
409 $op->set_disp32($op->{disp});
410 }
411  
412 return $op;
413 }
414 elsif ( @_ == 3 ) {
415 # [index*scale + disp/r]
416 # Operand(Register index,
417 # ScaleFactor scale,
418 # int32_t disp);
419  
420 die "TODO";
421 return Perlito5::X64::Operand->( index => $_[0], scale => $_[1], disp => $_[2] );
422 }
423 elsif ( @_ == 4 ) {
424 # [base + index*scale + disp/r]
425 # Operand(Register base,
426 # Register index,
427 # ScaleFactor scale,
428 # int32_t disp);
429  
430 die "TODO";
431 return Perlito5::X64::Operand->( base => $_[0], index => $_[1], scale => $_[2], disp => $_[3] );
432 }
433 else {
434 # TODO:
435 # Offset from existing memory operand.
436 # Offset is added to existing displacement as 32-bit signed values and
437 # this must not overflow.
438 # Operand(const Operand& base, int32_t offset);
439  
440 die "TODO";
441 die "Operand: don't know what to do with @_";
442 }
443 }
444  
445 sub pc_offset {
446 return scalar( @buffer );
447 }
448  
449 sub predictable_code_size {
450 return $predictable_code_size;
451 }
452  
453 #--- instructions
454  
455 # bind the current address to a label
456 sub _bind {
457 my ($label) = @_;
458 die "bind: expecting a label"
459 if ref($label) ne 'Perlito5::X64::Label';
460 $label->bind( scalar(@buffer) );
461 }
462  
463 sub _cpuid {
464 emit(0x0F);
465 emit(0xA2);
466 }
467  
468 sub _cqo {
469 emit_rex_64();
470 emit(0x99);
471 }
472  
473 sub _hlt {
474 emit(0xF4);
475 }
476  
477 sub _int3 {
478 emit(0xCC);
479 }
480  
481 sub _j {
482 my ( $condition, $label, $distance ) = @_;
483  
484 if ($condition == always) {
485 _jmp($label);
486 return;
487 }
488 elsif ($condition == never) {
489 return;
490 }
491 die if !is_uint4($condition);
492  
493 if ( $label->is_bound() ) {
494 my $short_size = 2;
495 my $long_size = 6;
496 my $offs = $label->pos() - pc_offset();
497 die if !($offs <= 0);
498 # Determine whether we can use 1-byte offsets for backwards branches,
499 # which have a max range of 128 bytes.
500  
501 # We also need to check predictable_code_size() flag here, because on x64,
502 # when the full code generator recompiles code for debugging, some places
503 # need to be padded out to a certain size. The debugger is keeping track of
504 # how often it did this so that it can adjust return addresses on the
505 # stack, but if the size of jump instructions can also change, that's not
506 # enough and the calculated offsets would be incorrect.
507 if ( is_int8( $offs - $short_size ) && !predictable_code_size() ) {
508 # 0111 tttn #8-bit disp.
509 emit(0x70 | $condition);
510 emit(($offs - $short_size) & 0xFF);
511 } else {
512 # 0000 1111 1000 tttn #32-bit disp.
513 emit(0x0F);
514 emit(0x80 | $condition);
515 emitl($offs - $long_size);
516 }
517 }
518 else {
519 die "j: don't know what to do with @_";
520 }
521 }
522  
523 sub _jmp {
524 my ( $label, $distance ) = @_;
525  
526 my $short_size = 1; # sizeof(int8_t);
527 my $long_size = 4; # sizeof(int32_t);
528 if ( is_label($label) && $label->is_bound() ) {
529 my $offs = $label->pos() - pc_offset() - 1;
530 die if ( $offs > 0 );
531 if ( is_int8( $offs - $short_size ) && !predictable_code_size() ) {
532 # 1110 1011 #8-bit disp.
533 emit(0xEB);
534 emit( ( $offs - $short_size ) & 0xFF );
535 }
536 else {
537 # 1110 1001 #32-bit disp.
538 emit(0xE9);
539 emitl( $offs - $long_size );
540 }
541 }
542 elsif ( is_register($label) ) {
543 my $target = $label;
544 # Opcode FF/4 r64.
545 emit_optional_rex_32($target);
546 emit(0xFF);
547 emit_modrm(0x4, $target);
548 }
549 else {
550 die "jmp: don't know what to do with @_";
551 }
552 }
553  
554 sub _leave {
555 emit(0xC9);
556 }
557  
558 sub _movb {
559 my ( $dst, $src ) = @_;
560 if ( @_ == 2 && is_register($dst) && is_immediate($src) ) {
561 # Immediate value
562 if (!$dst->is_byte_register()) {
563 emit_rex_32($dst);
564 }
565 emit(0xB0 + $dst->low_bits());
566 emit($src->{value});
567 }
568 else {
569 die "movb: don't know what to do with $dst, $src";
570 }
571 }
572  
573 sub _movl {
574 my ( $dst, $src ) = @_;
575 if ( is_register($dst) && is_register($src) ) {
576 if ($src->low_bits() == 4) {
577 emit_optional_rex_32($src, $dst);
578 emit(0x89);
579 emit_modrm($src, $dst);
580 }
581 else {
582 emit_optional_rex_32($dst, $src);
583 emit(0x8B);
584 emit_modrm($dst, $src);
585 }
586 }
587 elsif ( @_ == 2 && is_register($dst) && is_immediate($src) ) {
588 # Immediate value
589 my $value = $src;
590 emit_optional_rex_32($dst);
591 emit(0xB8 + $dst->low_bits());
592 emit($value);
593 }
594 else {
595 die "movl: don't know what to do with $dst, $src";
596 }
597 }
598  
599 sub _movq {
600 my ( $dst, $src ) = @_;
601 if ( is_register($dst) && is_register($src) ) {
602 if ($src->low_bits() == 4) {
603 emit_rex_64($src, $dst);
604 emit(0x89);
605 emit_modrm($src, $dst);
606 }
607 else {
608 emit_rex_64($dst, $src);
609 emit(0x8B);
610 emit_modrm($dst, $src);
611 }
612 }
613 elsif ( @_ == 2 && is_register($dst) && is_immediate($src) ) {
614 # Immediate value
615 my $value = $src;
616 emit_rex_64($dst);
617 emit(0xC7);
618 emit_modrm(0x0, $dst);
619 emit($src); # Only 32-bit immediates are possible, not 8-bit immediates.
620 }
621 else {
622 die "movq: don't know what to do with $dst, $src";
623 }
624 }
625  
626 sub _movsxlq {
627 my ( $dst, $src ) = @_;
628 if ( is_register($dst) && is_register($src) ) {
629 emit_rex_64($src, $dst);
630 emit(0x63);
631 emit_modrm($src, $dst);
632 }
633 else {
634 die "movsxlq: don't know what to do with $dst, $src";
635 }
636 }
637  
638 sub _repmovsb() {
639 emit(0xF3);
640 emit(0xA4);
641 }
642  
643 sub _repmovsw() {
644 emit(0x66); # Operand size override.
645 emit(0xF3);
646 emit(0xA4);
647 }
648  
649 sub _repmovsl() {
650 emit(0xF3);
651 emit(0xA5);
652 }
653  
654 sub _repmovsq() {
655 emit(0xF3);
656 emit_rex_64();
657 emit(0xA5);
658 }
659  
660 sub _mul {
661 my ($src) = @_;
662 emit_rex_64($src);
663 emit(0xF7);
664 emit_modrm( 0x4, $src );
665 }
666  
667 sub _neg {
668 my ($dst) = @_;
669 emit_rex_64($dst);
670 emit(0xF7);
671 emit_modrm( 0x3, $dst );
672 }
673  
674 sub _negl {
675 my ($dst) = @_;
676 emit_optional_rex_32($dst);
677 emit(0xF7);
678 emit_modrm( 0x3, $dst );
679 }
680  
681 sub _nop {
682 emit(0x90);
683 }
684  
685 sub _pop {
686 my ( $dst ) = @_;
687 if ( is_register($dst) ) {
688 emit_optional_rex_32($dst);
689 emit(0x58 | $dst->low_bits());
690 }
691 else {
692 die "pop: don't know what to do with $dst";
693 }
694 }
695  
696 sub _popfq {
697 emit(0x9D);
698 }
699  
700 sub _push {
701 my ( $src ) = @_;
702 if ( is_register($src) ) {
703 emit_optional_rex_32($src);
704 emit(0x50 | $src->low_bits());
705 }
706 elsif (is_int8($src)) {
707 emit(0x6A);
708 emit($src); # Emit low byte of value.
709 }
710 elsif (!ref($src)) {
711 emit(0x68);
712 emitl($src); # int32
713 }
714 else {
715 die "push: don't know what to do with $src";
716 }
717 }
718  
719 # Push a 32 bit integer, and guarantee that it is actually pushed as a
720 # 32 bit value, the normal push will optimize the 8 bit case.
721 sub _push_imm32 {
722 my ( $src ) = @_;
723 emit(0x68);
724 emitl($src); # int32
725 }
726  
727 sub _pushfq {
728 emit(0x9C);
729 }
730  
731 sub _rdtsc {
732 emit(0x0F);
733 emit(0x31);
734 }
735  
736 sub _ret {
737 my ( $imm16 ) = @_;
738 if ( !$imm16 ) {
739 emit(0xC3);
740 }
741 else {
742 emit(0xC2);
743 emit( $imm16 & 0xFF );
744 emit( ( $imm16 >> 8 ) & 0xFF );
745 }
746 }
747  
748 sub _shld {
749 my ( $dst, $src ) = @_;
750 emit_rex_64($src, $dst);
751 emit(0x0F);
752 emit(0xA5);
753 emit_modrm($src, $dst);
754 }
755  
756 sub _shrd {
757 my ( $dst, $src ) = @_;
758 emit_rex_64($src, $dst);
759 emit(0x0F);
760 emit(0xAD);
761 emit_modrm($src, $dst);
762 }
763  
764 sub _syscall {
765 emit(0x0F);
766 emit(0x05);
767 }
768  
769 sub _xchg {
770 my ( $dst, $src ) = @_;
771 if ( $src->is(rax) || $dst->is(rax) ) {
772 # Single-byte encoding
773 my $other = $src->is(rax) ? $dst : $src;
774 emit_rex_64($other);
775 emit( 0x90 | $other->low_bits() );
776 }
777 elsif ( $dst->low_bits() == 4 ) {
778 emit_rex_64( $dst, $src );
779 emit(0x87);
780 emit_modrm( $dst, $src );
781 }
782 else {
783 emit_rex_64( $src, $dst );
784 emit(0x87);
785 emit_modrm( $src, $dst );
786 }
787 }
788  
789 1;
790  
791 __END__