-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx86_64.py
1316 lines (1000 loc) · 38.6 KB
/
x86_64.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2012 Madhusudan C.S.
#
# This file x86_64.py is part of PL241-MCS compiler.
#
# PL241-MCS compiler is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PL241-MCS compiler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PL241-MCS compiler. If not, see <http://www.gnu.org/licenses/>.
#
"""This module contains the x86_64 specific classes and definitions.
"""
import logging
import struct
from argparse import ArgumentParser
from ir import Immediate
from ir import Memory
from regalloc import Register
# Module level logger object
LOGGER = logging.getLogger(__name__)
# The following two constants holds for everything within x86_64 architecture.
# This won't change within the architecture. And across architectures when
# one changes the other should also change. The first is the name of the
# byte ordering, second is the format required by Python's struct module to
# define byte ordering.
ENDIANNESS = 'little'
BYTE_ORDERING_FMT = '<'
# All the objects in memory are 8-bytes (64 bits) wide.
MEMORY_WIDTH = 0x8
# From the register allocator, we have the registers as the numbers from 0
# to N - 1 where N is the number of registers allowed for allocation. This
# map translates those colors into the x86_64 register codes. The names of
# the registers are given as comments next to each mapping.
# IMPORTANT: The REX value here is the binary bit that should be set or not.
# Since the position in the REX byte changes depending on whether the register
# is source or destination, it will be encoded to right place in REX byte
# while building the instruction.
REGISTER_COLOR_TO_CODE_MAP = {
0: { 'REX': 0b0, 'REG': 0b000 }, # rax
1: { 'REX': 0b0, 'REG': 0b011 }, # rbx
2: { 'REX': 0b0, 'REG': 0b001 }, # rcx
3: { 'REX': 0b0, 'REG': 0b010 }, # rdx
4: { 'REX': 0b0, 'REG': 0b110 }, # rsi
5: { 'REX': 0b0, 'REG': 0b111 }, # rdi
6: { 'REX': 0b1, 'REG': 0b000 }, # r8
7: { 'REX': 0b1, 'REG': 0b001 }, # r9
8: { 'REX': 0b1, 'REG': 0b010 }, # r10
9: { 'REX': 0b1, 'REG': 0b011 }, # r11
10: { 'REX': 0b1, 'REG': 0b100 }, # r12
11: { 'REX': 0b1, 'REG': 0b101 }, # r13
12: { 'REX': 0b1, 'REG': 0b110 }, # r14
13: { 'REX': 0b1, 'REG': 0b111 }, # r15
'rsp': { 'REX': 0b0, 'REG': 0b100 }, # rsp
'rbp': { 'REX': 0b0, 'REG': 0b101 }, # rbp
'rip': { 'REX': 0b0, 'REG': 0b101 }, # rip
}
# Keep this pre-computed for faster set operations on register colors, like
# set difference operation etc. which will be used in code generator.
REGISTERS_COLOR_SET = set(range(0, 14))
FUNCTION_ARGUMENTS_COLORS = [5, 4, 3, 2, 6, 7]
class InvalidInstructionException(Exception):
"""Represents the exception when the instruction is invalid.
"""
def __init__(self, msg, *args, **kwargs):
"""Constructs the exception with the message.
Args:
msg: The exception message (this is optional).
"""
super(InvalidInstructionException, self).__init__(*args, **kwargs)
self.msg = msg if msg else ''
def __str__(self):
return '%s: %s' % (self.__class__.__name__, self._msg)
class Instruction(object):
"""Abstracts the instructions in x86_64 architecture.
"""
def __init__(self, destination=None, source=None, offset=None, immediate=None):
"""Constructs the instruction object for x86_64.
Args:
mnemonic: The mnemonic of this instruction.
source: The source operand of this instruction.
destination: The destination operand of this instruction.
offset: Offset from the starting of the program for this instruction.
"""
self.source = source
self.destination = destination
self.offset = offset
self.immediate = immediate
self.rex = None
self.opcode = None
self.modregrm = None
self.displacement = None
# The actual binary that is built for this instruction.
self.binary = None
self.build()
def mod_reg_rm_byte(self, mod, reg, rm):
"""Returns the byte containing the mod, reg and rm values.
Args:
mod: The 2-bits mod value.
reg: The 3-bits reg value.
rm: The 3-bits rm value.
"""
return ((mod << 6) | (reg << 3) | ( rm ))
def rex_byte(self, base=0b0, R=0b0, X=0b0, B=0b0):
"""Returns the byte containing the mod, reg and rm values.
Args:
base: The base rex byte defined with the instruction opcode in the
Intel spec sheet.
R: The single bit R value.
X: The single bit X value.
B: The single bit B value.
"""
return ((base) | (R << 2) | (X << 1) | (B))
def sib_byte(self, scale=0b00, index=0b000, base=0b000):
"""Returns the byte containing the scale, index and base values.
Args:
scale: The scale part of the SIB encoding
index: The index register corresponding to the SIB encoding.
base: The base register corresponding to the SIB encoding.
"""
return ((scale << 6) | (index << 3) | (base))
def reg64_reg64(self):
"""Encodes the instruction for register to register instructions.
"""
source_reg = REGISTER_COLOR_TO_CODE_MAP[self.source.color]
dest_reg = REGISTER_COLOR_TO_CODE_MAP[self.destination.color]
opcode_entry = self.OPCODE_TABLE[('reg64', 'rm64')]
mod = 0b11
reg = dest_reg['REG']
rm = source_reg['REG']
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
R=dest_reg['REX'],
B=source_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
def reg64_rm64(self):
"""Encodes the instruction for memory to register instructions.
"""
source_mem = self.source
dest_reg = REGISTER_COLOR_TO_CODE_MAP[self.destination.color]
opcode_entry = self.OPCODE_TABLE[('reg64', 'rm64')]
reg = dest_reg['REG']
sib = None
if isinstance(source_mem.base, Memory):
# SIB encoding must be used in this case.
if source_mem.base.base == 'rip':
mod = 0b00
else:
mod = 0b10
rm = 0b100
index_reg = REGISTER_COLOR_TO_CODE_MAP[source_mem.offset.color]
sib = self.sib_byte(scale=0b11, index=index_reg['REG'], base=0b101)
offset = self.displacement if self.displacement else source_mem.base.offset
rex = self.rex_byte(base=opcode_entry['REX'],
R=dest_reg['REX'],
X=index_reg['REX'])
else:
if source_mem.base == 'rip':
# mod is 00 for RIP-relative addressing
mod = 0b00
# For %rip register
source_reg = REGISTER_COLOR_TO_CODE_MAP['rip']
rm = source_reg['REG']
offset = self.displacement if self.displacement else source_mem.offset
else:
mod = 0b10
source_reg = REGISTER_COLOR_TO_CODE_MAP['rbp']
rm = source_reg['REG']
offset = -source_mem.offset if source_mem.offset else \
(source_mem.offset if source_mem.offset else 0) * MEMORY_WIDTH
rex = self.rex_byte(base=opcode_entry['REX'],
R=dest_reg['REX'],
B=source_reg['REX'])
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
if sib:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, sib)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, offset)
def rm64_reg64(self):
"""Encodes the instruction for register to memory instructions.
"""
source_reg = REGISTER_COLOR_TO_CODE_MAP[self.source.color]
dest_mem = self.destination
opcode_entry = self.OPCODE_TABLE[('rm64', 'reg64')]
reg = source_reg['REG']
sib = None
if isinstance(dest_mem.base, Memory):
# SIB encoding must be used in this case.
if dest_mem.base.base == 'rip':
mod = 0b00
else:
mod = 0b10
rm = 0b100
index_reg = REGISTER_COLOR_TO_CODE_MAP[dest_mem.offset.color]
sib = self.sib_byte(scale=0b11, index=index_reg['REG'], base=0b101)
offset = self.displacement if self.displacement else dest_mem.base.offset
rex = self.rex_byte(base=opcode_entry['REX'],
R=source_reg['REX'],
X=index_reg['REX'])
else:
if dest_mem.base == 'rip':
# mod is 00 for RIP-relative addressing
mod = 0b00
# For %rip register
dest_reg = REGISTER_COLOR_TO_CODE_MAP['rip']
rm = dest_reg['REG']
offset = self.displacement if self.displacement else dest_mem.offset
else:
mod = 0b10
dest_reg = REGISTER_COLOR_TO_CODE_MAP['rbp']
rm = dest_reg['REG']
offset = -dest_mem.offset if dest_mem.offset != None else 0
rex = self.rex_byte(base=opcode_entry['REX'],
R=source_reg['REX'],
B=dest_reg['REX'])
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
if sib:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, sib)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, offset)
def reg64_imm32(self):
"""Encodes the instruction for immediate to register instructions.
"""
source_imm = self.source.value
dest_reg = REGISTER_COLOR_TO_CODE_MAP[self.destination.color]
opcode_entry = self.OPCODE_TABLE[('rm64', 'imm32')]
mod = 0b11
reg = opcode_entry['OPCODE_EXT']
rm = dest_reg['REG']
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
B=dest_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, source_imm)
def rm64_imm32(self):
"""Encodes the instruction for immediate to memory instructions.
"""
source_imm = self.source.value
dest_mem = self.destination
opcode_entry = self.OPCODE_TABLE[('rm64', 'imm32')]
reg = opcode_entry.get('OPCODE_EXT', 0x000)
sib = None
if isinstance(dest_mem.base, Memory):
# SIB encoding must be used in this case.
if dest_mem.base.base == 'rip':
mod = 0b00
else:
mod = 0b10
rm = 0b100
index_reg = REGISTER_COLOR_TO_CODE_MAP[dest_mem.offset.color]
sib = self.sib_byte(scale=0b11, index=index_reg['REG'], base=0b101)
offset = self.displacement if self.displacement else dest_mem.base.offset
rex = self.rex_byte(base=opcode_entry['REX'],
X=index_reg['REX'])
else:
if dest_mem.base == 'rip':
# mod is 00 for RIP-relative addressing
mod = 0b00
# For %rip register
dest_reg = REGISTER_COLOR_TO_CODE_MAP['rip']
rm = dest_reg['REG']
offset = self.displacement if self.displacement else dest_mem.offset
else:
mod = 0b01
dest_reg = REGISTER_COLOR_TO_CODE_MAP['rbp']
rm = dest_reg['REG']
offset = -dest_mem.offset if dest_mem.offset != None else 0
rex = self.rex_byte(base=opcode_entry['REX'], B=dest_reg['REX'])
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
if sib:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, sib)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, offset)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, source_imm)
def build(self):
"""Builds the instruction bytes.
"""
self.binary = ''
if (isinstance(self.destination, Register) and
isinstance(self.source, Register)):
self.reg64_reg64()
elif (isinstance(self.destination, Register) and
isinstance(self.source, Memory)):
self.reg64_rm64()
elif (isinstance(self.destination, Memory) and
isinstance(self.source, Register)):
self.rm64_reg64()
# This case is very important since the destination register is encoded
# into opcode. See the binary generation for OPCODE
elif (isinstance(self.destination, Register) and
isinstance(self.source, Immediate)):
self.reg64_imm32()
elif (isinstance(self.destination, Memory) and
isinstance(self.source, Immediate)):
self.rm64_imm32()
else:
raise NotImplementedError('The operands for the instruction could not '
'be encoded. Destination: %s Source: %s' % (
self.destination, self.source))
def set_displacement(self, displacement):
"""Sets the new displacement offset and rebuilds the instruction binary.
"""
self.displacement = displacement
self.build()
def __len__(self):
"""Returns the length of the binary bytes for the instruction.
"""
return len(self.binary)
def __str__(self):
"""Returns the binary bytes for the instruction.
"""
return self.binary
class JumpInstruction(Instruction):
"""Abstracts the jump instructions (this includes call also).
"""
def __init__(self, target=None):
"""Constructs the JE instruction.
"""
self.target = target
super(JumpInstruction, self).__init__()
def build(self):
"""Builds the instruction bytes.
"""
self.binary = ''
if not self.target:
self.target = 0x0
if not isinstance(self.target, int):
raise InvalidInstructionException(
'Target of branch instruction is not integer. The value given is '
'%s.' % self.target)
# NOTE: The opcode is assumed to be given in the byte ordered format
# required by the architecture, so render in big-endian to preserve
# ordering.
self.binary += struct.pack('>H', self.OPCODE_TABLE['rel32']['OPCODE'])
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, self.target)
def set_target(self, target):
"""Sets the target offset and rebuilds the instruction binary.
"""
self.target = target
self.build()
class ADD(Instruction):
"""Implements the ADD instruction.
"""
OPCODE_TABLE = {
('reg64', 'rm64'): { 'REX': 0x48, 'OPCODE': 0x03 },
('rm64', 'reg64'): { 'REX': 0x48, 'OPCODE': 0x01 },
('rm64', 'imm32'): { 'REX': 0x48, 'OPCODE': 0x81, 'OPCODE_EXT': 0x0 }
}
def __init__(self, destination, source):
"""Constructs the ADD instruction.
"""
super(ADD, self).__init__(destination, source)
class CALL(JumpInstruction):
"""Implements the CALL instruction.
"""
OPCODE_TABLE = {
'rel32': { 'REX': 0x0, 'OPCODE': 0xE8 }
}
def __init__(self, target=None):
"""Constructs the CALL instruction.
"""
super(CALL, self).__init__()
def build(self):
"""Builds the instruction bytes.
"""
self.binary = ''
if not self.target:
self.target = 0x0
if not isinstance(self.target, int):
raise InvalidInstructionException(
'Target of branch instruction is not integer. The value given is '
'%s.' % self.target)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', self.OPCODE_TABLE['rel32']['OPCODE'])
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, self.target)
class CMP(Instruction):
"""Implements the CMP instruction.
"""
OPCODE_TABLE = {
('reg64', 'rm64'): { 'REX': 0x48, 'OPCODE': 0x3B },
('rm64', 'reg64'): { 'REX': 0x48, 'OPCODE': 0x39 },
('rm64', 'imm32'): { 'REX': 0x48, 'OPCODE': 0x81, 'OPCODE_EXT': 0x7 }
}
def __init__(self, destination, source):
"""Constructs the CMP instruction.
"""
super(CMP, self).__init__(destination, source)
class HLT(Instruction):
"""Implements the HLT instruction.
"""
OPCODE_TABLE = {
('nooperand'): { 'REX': 0x00, 'OPCODE': 0xF4 },
}
def __init__(self):
"""Constructs the HLT instruction.
"""
super(HLT, self).__init__(destination=None, source=None)
def build(self):
"""Builds the instruction bytes.
"""
self.binary = ''
self.binary += struct.pack('%sH' % BYTE_ORDERING_FMT,
self.OPCODE_TABLE['nooperand']['OPCODE'])
class IDIV(Instruction):
"""Implements the IDIV instruction.
NOTES: RDX and RAX must be cleared for division. RAX holds the quotient
and RDX will hold the Remainder. So we will have to check if the existing
registers for the result are RAX and RDX if not we will have to spill and
immediately reload after the DIV instruction.
"""
OPCODE_TABLE = {
('rm64'): { 'REX': 0x48, 'OPCODE': 0xF7, 'OPCODE_EXT': 0x7 },
}
def __init__(self, source):
"""Constructs the IDIV instruction.
"""
super(IDIV, self).__init__(destination=None, source=source)
def build(self):
"""Builds the instruction bytes.
"""
self.binary = ''
if isinstance(self.source, Register):
source_reg = REGISTER_COLOR_TO_CODE_MAP[self.source.color]
opcode_entry = self.OPCODE_TABLE['rm64']
# FIXME: May be buggy because reg may be 0xb111 instead of 0
mod = 0b11
reg = opcode_entry['OPCODE_EXT']
rm = source_reg['REG']
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
B=source_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
elif isinstance(self.source, Memory):
source_mem = self.source
opcode_entry = self.OPCODE_TABLE['rm64']
mod = 0b10
reg = opcode_entry['OPCODE_EXT']
if isinstance(source_mem.base, Register):
source_reg = REGISTER_COLOR_TO_CODE_MAP[source_mem.base.color]
rm = source_reg['REG']
offset = self.displacement if self.displacement else source_mem.offset
elif source_mem.base == 'rip':
# mod is 00 for RIP-relative addressing
mod = 0b00
# For %rip register
source_reg = REGISTER_COLOR_TO_CODE_MAP['rip']
rm = source_reg['REG']
offset = self.displacement if self.displacement else source_mem.offset
else:
source_reg = REGISTER_COLOR_TO_CODE_MAP['rbp']
rm = source_reg['REG']
offset = -source_mem.offset
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
B=source_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, offset)
else:
raise NotImplementedError('Signed integer division is not implemented '
'on non register source operands.')
class IMUL(Instruction):
"""Implements the IMUL instruction.
"""
OPCODE_TABLE = {
('reg64', 'rm64'): { 'REX': 0x48, 'OPCODE': 0x0FAF },
('reg64', 'rm64', 'imm32'): { 'REX': 0x48, 'OPCODE': 0x69 },
}
def __init__(self, destination, source, immediate=None):
"""Constructs the IMUL instruction.
"""
super(IMUL, self).__init__(destination, source, immediate=immediate)
def reg64_rm64_imm32(self):
"""Builds the instruction bytes for reg64, rm64, imm32 operands.
"""
if isinstance(self.source, Register):
source_reg = REGISTER_COLOR_TO_CODE_MAP[self.source.color]
dest_reg = REGISTER_COLOR_TO_CODE_MAP[self.destination.color]
imm32 = self.immediate.value
opcode_entry = self.OPCODE_TABLE[('reg64', 'rm64', 'imm32')]
mod = 0b11
reg = dest_reg['REG']
rm = source_reg['REG']
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
R=dest_reg['REX'],
B=source_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT,
imm32)
elif isinstance(self.source, Memory):
source_mem = self.source
dest_reg = REGISTER_COLOR_TO_CODE_MAP[self.destination.color]
imm32 = self.immediate.value
opcode_entry = self.OPCODE_TABLE[('reg64', 'rm64', 'imm32')]
mod = 0b10
reg = dest_reg['REG']
if isinstance(source_mem.base, Register):
source_reg = REGISTER_COLOR_TO_CODE_MAP[source_mem.base.color]
rm = source_reg['REG']
offset = self.displacement if self.displacement else source_mem.offset
elif source_mem.base == 'rip':
# mod is 00 for RIP-relative addressing
mod = 0b00
# For %rip register
source_reg = REGISTER_COLOR_TO_CODE_MAP['rip']
rm = source_reg['REG']
offset = self.displacement if self.displacement else source_mem.offset
else:
source_reg = REGISTER_COLOR_TO_CODE_MAP['rbp']
rm = source_reg['REG']
offset = -source_mem.offset if source_mem.offset else \
(source_mem.offset if source_mem.offset else 0) * MEMORY_WIDTH
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
R=dest_reg['REX'],
B=source_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, offset)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT,
imm32)
else:
raise NotImplementedError('Instruction could not be encoded.')
def build(self):
"""Builds the instruction bytes.
IMPORTANT: The only difference between this method and the Instruction
class's build method is that the instruction opcode is encoded into
2 bytes not a single byte.
"""
self.binary = ''
if self.immediate != None:
self.reg64_rm64_imm32()
elif (isinstance(self.destination, Register) and
(isinstance(self.source, Register))):
source_reg = REGISTER_COLOR_TO_CODE_MAP[self.source.color]
dest_reg = REGISTER_COLOR_TO_CODE_MAP[self.destination.color]
opcode_entry = self.OPCODE_TABLE[('reg64', 'rm64')]
mod = 0b11
reg = dest_reg['REG']
rm = source_reg['REG']
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
R=dest_reg['REX'],
B=source_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>H', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
elif (isinstance(self.destination, Register) and
(isinstance(self.source, Memory))):
source_mem = self.source
dest_reg = REGISTER_COLOR_TO_CODE_MAP[self.destination.color]
opcode_entry = self.OPCODE_TABLE[('reg64', 'rm64')]
mod = 0b10
reg = dest_reg['REG']
if isinstance(source_mem.base, Register):
source_reg = REGISTER_COLOR_TO_CODE_MAP[source_mem.base.color]
rm = source_reg['REG']
offset = self.displacement if self.displacement else source_mem.offset
elif source_mem.base == 'rip':
# mod is 00 for RIP-relative addressing
mod = 0b00
# For %rip register
source_reg = REGISTER_COLOR_TO_CODE_MAP['rip']
rm = source_reg['REG']
offset = self.displacement if self.displacement else source_mem.offset
else:
source_reg = REGISTER_COLOR_TO_CODE_MAP['rbp']
rm = source_reg['REG']
offset = -source_mem.offset if source_mem.offset else \
(source_mem.offset if source_mem.offset else 0) * MEMORY_WIDTH
modregrm = self.mod_reg_rm_byte(mod, reg, rm)
rex = self.rex_byte(base=opcode_entry['REX'],
R=dest_reg['REX'],
B=source_reg['REX'])
if rex:
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT, rex)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>H', opcode_entry['OPCODE'])
self.binary += struct.pack('%sB' % BYTE_ORDERING_FMT,
modregrm)
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, offset)
else:
raise NotImplementedError('Only register to register multiplications '
'are implemented.')
class JE(JumpInstruction):
"""Implements the JE instruction.
"""
OPCODE_TABLE = {
'rel8': { 'REX': 0x0, 'OPCODE': 0x74 },
'rel32': { 'REX': 0x0, 'OPCODE': 0x0F84 }
}
def __init__(self, target=None):
"""Constructs the JE instruction.
"""
super(JE, self).__init__()
class JG(JumpInstruction):
"""Implements the JG instruction.
"""
OPCODE_TABLE = {
'rel8': { 'REX': 0x0, 'OPCODE': 0x7F },
'rel32': { 'REX': 0x0, 'OPCODE': 0x0F8F }
}
def __init__(self, target=None):
"""Constructs the JG instruction.
"""
super(JG, self).__init__()
class JGE(JumpInstruction):
"""Implements the JGE instruction.
"""
OPCODE_TABLE = {
'rel8': { 'REX': 0x0, 'OPCODE': 0x7D },
'rel32': { 'REX': 0x0, 'OPCODE': 0x0F8D }
}
def __init__(self, target=None):
"""Constructs the JGE instruction.
"""
super(JGE, self).__init__()
class JL(JumpInstruction):
"""Implements the JL instruction.
"""
OPCODE_TABLE = {
'rel8': { 'REX': 0x0, 'OPCODE': 0x7C },
'rel32': { 'REX': 0x0, 'OPCODE': 0x0F8C }
}
def __init__(self, target=None):
"""Constructs the instruction.
"""
super(JL, self).__init__()
class JLE(JumpInstruction):
"""Implements the JLE instruction.
"""
OPCODE_TABLE = {
'rel8': { 'REX': 0x0, 'OPCODE': 0x7E },
'rel32': { 'REX': 0x0, 'OPCODE': 0x0F8E }
}
def __init__(self, target=None):
"""Constructs the JLE instruction.
"""
super(JLE, self).__init__()
class JMP(JumpInstruction):
"""Implements the JMP instruction.
"""
OPCODE_TABLE = {
'rel8': { 'REX': 0x0, 'OPCODE': 0xEB },
'rel32': { 'REX': 0x0, 'OPCODE': 0xE9 }
}
def __init__(self, target=None):
"""Constructs the JMP instruction.
"""
super(JMP, self).__init__()
def build(self):
"""Builds the instruction bytes.
"""
self.binary = ''
if not self.target:
self.target = 0x0
if not isinstance(self.target, int):
raise InvalidInstructionException(
'Target of branch instruction is not integer. The value given is '
'%s.' % self.target)
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', self.OPCODE_TABLE['rel32']['OPCODE'])
self.binary += struct.pack('%si' % BYTE_ORDERING_FMT, self.target)
class JNE(JumpInstruction):
"""Implements the JNE instruction.
"""
OPCODE_TABLE = {
'rel8': { 'REX': 0x0, 'OPCODE': 0x75 },
'rel32': { 'REX': 0x0, 'OPCODE': 0x0F85 }
}
def __init__(self, target=None):
"""Constructs the JNE instruction.
"""
super(JNE, self).__init__()
class LEA(Instruction):
"""Implements the LEA instruction.
"""
OPCODE_TABLE = {
('reg64', 'rm64'): { 'REX': 0x48, 'OPCODE': 0x8D },
}
def __init__(self, destination, source):
"""Constructs the LEA instruction.
"""
super(LEA, self).__init__(destination, source)
class LEAVE(Instruction):
"""Implements the LEAVE instruction.
"""
OPCODE_TABLE = {
('nooperand'): { 'REX': 0x00, 'OPCODE': 0xC9 },
}
def __init__(self):
"""Constructs the LEAVE instruction.
"""
super(LEAVE, self).__init__(destination=None, source=None)
def build(self):
"""Builds the instruction bytes.
"""
self.binary = ''
# Opcode entries are properly byte ordered, so preserve the order
# using big-endian
self.binary += struct.pack('>B', self.OPCODE_TABLE['nooperand']['OPCODE'])
class MOV(Instruction):
"""Implements the MOV instruction.
"""