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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
|
#
#
# Miasm2 Extension: #
# - Miasm2 IR to LLVM IR #
# - JiT #
#
# Requires: #
# - llvmlite (tested on v0.15) #
#
# Authors : Fabrice DESCLAUX (CEA/DAM), Camille MOUGEY (CEA/DAM) #
#
#
from llvmlite import binding as llvm
from llvmlite import ir as llvm_ir
import miasm2.expression.expression as m2_expr
import miasm2.jitter.csts as m2_csts
import miasm2.core.asmbloc as m2_asmbloc
class LLVMType(llvm_ir.Type):
"Handle LLVM Type"
int_cache = {}
@classmethod
def IntType(cls, size=32):
try:
return cls.int_cache[size]
except KeyError:
cls.int_cache[size] = llvm_ir.IntType(size)
return cls.int_cache[size]
@classmethod
def pointer(cls, addr):
"Generic pointer for execution"
return llvm_e.GenericValue.pointer(addr)
@classmethod
def generic(cls, e):
"Generic value for execution"
if isinstance(e, m2_expr.ExprInt):
return llvm_e.GenericValue.int(LLVMType.IntType(e.size), int(e.arg))
elif isinstance(e, llvm_e.GenericValue):
return e
else:
raise ValueError()
class LLVMContext():
"Context for llvm binding. Stand for a LLVM Module"
known_fc = {}
def __init__(self, name="mod"):
"Initialize a context with a module named 'name'"
self.new_module(name)
def optimise_level(self, classic_passes=True, dead_passes=True):
"""Set the optimisation level :
classic_passes :
- combine instruction
- reassociate
- global value numbering
- simplify cfg
dead_passes :
- dead code
- dead store
- dead instructions
"""
# Set up the optimiser pipeline
"""
if classic_passes is True:
# self.pass_manager.add(llvm_p.PASS_INSTCOMBINE)
self.pass_manager.add(llvm_p.PASS_REASSOCIATE)
self.pass_manager.add(llvm_p.PASS_GVN)
self.pass_manager.add(llvm_p.PASS_SIMPLIFYCFG)
if dead_passes is True:
self.pass_manager.add(llvm_p.PASS_DCE)
self.pass_manager.add(llvm_p.PASS_DSE)
self.pass_manager.add(llvm_p.PASS_DIE)
self.pass_manager.initialize()
"""
def new_module(self, name="mod"):
self.mod = llvm_ir.Module(name=name)
# self.pass_manager = llvm.FunctionPassManager(self.mod)
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
backing_mod = llvm.parse_assembly("")
self.exec_engine = llvm.create_mcjit_compiler(backing_mod,
target_machine)
self.add_fc(self.known_fc)
def get_execengine(self):
"Return the Execution Engine associated with this context"
return self.exec_engine
def get_passmanager(self):
"Return the Pass Manager associated with this context"
return self.exec_engine
def get_module(self):
"Return the module associated with this context"
return self.mod
def add_shared_library(self, filename):
"Load the shared library 'filename'"
return llvm.load_library_permanently(filename)
def add_fc(self, fc):
"Add function into known_fc"
for name, detail in fc.iteritems():
fnty = llvm_ir.FunctionType(detail["ret"], detail["args"])
llvm_ir.Function(self.mod, fnty, name=name)
def memory_lookup(self, func, addr, size):
"""Perform a memory lookup at @addr of size @size (in bit)"""
raise NotImplementedError("Abstract method")
def memory_write(self, func, addr, size, value):
"""Perform a memory write at @addr of size @size (in bit) with LLVM IR @value"""
raise NotImplementedError("Abstract method")
class LLVMContext_JIT(LLVMContext):
"""Extend LLVMContext_JIT in order to handle memory management and custom
operations"""
def __init__(self, library_filenames, name="mod"):
"Init a LLVMContext object, and load the mem management shared library"
self.library_filenames = library_filenames
LLVMContext.__init__(self, name)
self.vmcpu = {}
self.engines = []
def new_module(self, name="mod"):
LLVMContext.new_module(self, name)
for lib_fname in self.library_filenames:
self.add_shared_library(lib_fname)
self.add_memlookups()
self.add_get_exceptionflag()
self.add_op()
self.add_log_functions()
def add_memlookups(self):
"Add MEM_LOOKUP functions"
fc = {}
p8 = llvm_ir.PointerType(LLVMType.IntType(8))
for i in [8, 16, 32, 64]:
fc["vm_MEM_LOOKUP_%02d" % i] = {"ret": LLVMType.IntType(i),
"args": [p8,
LLVMType.IntType(64)]}
fc["vm_MEM_WRITE_%02d" % i] = {"ret": llvm_ir.VoidType(),
"args": [p8,
LLVMType.IntType(64),
LLVMType.IntType(i)]}
self.add_fc(fc)
def add_get_exceptionflag(self):
"Add 'get_exception_flag' function"
p8 = llvm_ir.PointerType(LLVMType.IntType(8))
self.add_fc({"get_exception_flag": {"ret": LLVMType.IntType(64),
"args": [p8]}})
def add_op(self):
"Add operations functions"
p8 = llvm_ir.PointerType(LLVMType.IntType(8))
self.add_fc({"parity": {"ret": LLVMType.IntType(),
"args": [LLVMType.IntType()]}})
self.add_fc({"rot_left": {"ret": LLVMType.IntType(),
"args": [LLVMType.IntType(),
LLVMType.IntType(),
LLVMType.IntType()]}})
self.add_fc({"rot_right": {"ret": LLVMType.IntType(),
"args": [LLVMType.IntType(),
LLVMType.IntType(),
LLVMType.IntType()]}})
self.add_fc({"segm2addr": {"ret": LLVMType.IntType(64),
"args": [p8,
LLVMType.IntType(64),
LLVMType.IntType(64)]}})
for k in [8, 16]:
self.add_fc({"bcdadd_%s" % k: {"ret": LLVMType.IntType(k),
"args": [LLVMType.IntType(k),
LLVMType.IntType(k)]}})
self.add_fc({"bcdadd_cf_%s" % k: {"ret": LLVMType.IntType(k),
"args": [LLVMType.IntType(k),
LLVMType.IntType(k)]}})
for k in [16, 32, 64]:
self.add_fc({"imod%s" % k: {"ret": LLVMType.IntType(k),
"args": [p8,
LLVMType.IntType(k),
LLVMType.IntType(k)]}})
self.add_fc({"idiv%s" % k: {"ret": LLVMType.IntType(k),
"args": [p8,
LLVMType.IntType(k),
LLVMType.IntType(k)]}})
def add_log_functions(self):
"Add functions for state logging"
p8 = llvm_ir.PointerType(LLVMType.IntType(8))
self.add_fc({"dump_gpregs": {"ret": llvm_ir.VoidType(),
"args": [p8]}})
def set_vmcpu(self, lookup_table):
"Set the correspondance between register name and vmcpu offset"
self.vmcpu = lookup_table
def set_IR_transformation(self, *args):
"""Set a list of transformation to apply on expression before their
treatments.
args: function Expr(Expr)"""
self.IR_transformation_functions = args
def memory_lookup(self, func, addr, size):
"""Perform a memory lookup at @addr of size @size (in bit)"""
builder = func.builder
fc_name = "vm_MEM_LOOKUP_%02d" % size
fc_ptr = self.mod.get_global(fc_name)
addr_casted = builder.zext(addr,
LLVMType.IntType(64))
ret = builder.call(fc_ptr, [func.local_vars["vmmngr"],
addr_casted])
return ret
def memory_write(self, func, addr, size, value):
"""Perform a memory write at @addr of size @size (in bit) with LLVM IR @value"""
# Function call
builder = func.builder
fc_name = "vm_MEM_WRITE_%02d" % size
fc_ptr = self.mod.get_global(fc_name)
dst_casted = builder.zext(addr, LLVMType.IntType(64))
builder.call(fc_ptr, [func.local_vars["vmmngr"],
dst_casted,
value])
class LLVMContext_IRCompilation(LLVMContext):
"""Extend LLVMContext in order to handle memory management and custom
operations for Miasm IR compilation"""
def memory_lookup(self, func, addr, size):
"""Perform a memory lookup at @addr of size @size (in bit)"""
builder = func.builder
int_size = LLVMType.IntType(size)
ptr_casted = builder.inttoptr(addr,
llvm_ir.PointerType(int_size))
return builder.load(ptr_casted)
def memory_write(self, func, addr, size, value):
"""Perform a memory write at @addr of size @size (in bit) with LLVM IR @value"""
builder = func.builder
int_size = LLVMType.IntType(size)
ptr_casted = builder.inttoptr(addr,
llvm_ir.PointerType(int_size))
return builder.store(value, ptr_casted)
class LLVMFunction():
"Represent a llvm function"
# Default logging values
log_mn = False
log_regs = True
def __init__(self, llvm_context, name="fc"):
"Create a new function with name fc"
self.llvm_context = llvm_context
self.llvm_context.new_module()
self.mod = self.llvm_context.get_module()
self.my_args = [] # (Expr, LLVMType, Name)
self.ret_type = None
self.builder = None
self.entry_bbl = None
self.branch_counter = 0
self.name = name
def new_branch_name(self):
"Return a new branch name"
self.branch_counter += 1
return "%s" % self.branch_counter
def viewCFG(self):
"Show the CFG of the current function"
self.fc.viewCFG()
def append_basic_block(self, label):
"""Add a new basic block to the current function.
@label: str or asmlabel
Return the corresponding LLVM Basic Block"""
name = self.canonize_label_name(label)
bbl = self.fc.append_basic_block(name)
self.name2bbl[label] = bbl
return bbl
def init_fc(self):
"Init the function"
# Build type for fc signature
fc_type = llvm_ir.FunctionType(self.ret_type, [k[1] for k in self.my_args])
# Add fc in module
try:
fc = llvm_ir.Function(self.mod, fc_type, name=self.name)
except llvm.LLVMException:
# Overwrite the previous function
previous_fc = self.mod.get_global(self.name)
previous_fc.delete()
fc = self.mod.add_function(fc_type, self.name)
# Name args
for i, a in enumerate(self.my_args):
fc.args[i].name = a[2]
# Initialize local variable pool
self.local_vars = {}
self.local_vars_pointers = {}
for i, a in enumerate(self.my_args):
self.local_vars[a[2]] = fc.args[i]
# Init cache
self.expr_cache = {}
self.main_stream = True
self.name2bbl = {}
self.offsets_jitted = set()
# Function link
self.fc = fc
# Add a first BasicBlock
self.entry_bbl = self.append_basic_block("entry")
# Instruction builder
self.builder = llvm_ir.IRBuilder(self.entry_bbl)
def CreateEntryBlockAlloca(self, var_type):
"Create an alloca instruction at the beginning of the current fc"
builder = self.builder
current_bbl = builder.basic_block
builder.position_at_start(self.entry_bbl)
ret = builder.alloca(var_type)
builder.position_at_end(current_bbl)
return ret
def get_ptr_by_expr(self, expr):
""""Return a pointer casted corresponding to ExprId expr. If it is not
already computed, compute it at the end of entry_bloc"""
name = expr.name
try:
# If the pointer has already been computed
ptr_casted = self.local_vars_pointers[name]
except KeyError:
# Get current objects
builder = self.builder
current_bbl = builder.basic_block
# Go at the right position
entry_bloc_bbl = self.entry_bbl
builder.position_at_end(entry_bloc_bbl)
# Compute the pointer address
offset = self.llvm_context.vmcpu[name]
# Pointer cast
ptr = builder.gep(self.local_vars["vmcpu"],
[llvm_ir.Constant(LLVMType.IntType(),
offset)])
int_size = LLVMType.IntType(expr.size)
ptr_casted = builder.bitcast(ptr,
llvm_ir.PointerType(int_size))
# Store in cache
self.local_vars_pointers[name] = ptr_casted
# Reset builder
builder.position_at_end(current_bbl)
return ptr_casted
def clear_cache(self, regs_updated):
"Remove from the cache values which depends on regs_updated"
regs_updated_set = set(regs_updated)
for expr in self.expr_cache.keys():
if expr.get_r(True).isdisjoint(regs_updated_set) is not True:
self.expr_cache.pop(expr)
def update_cache(self, name, value):
"Add 'name' = 'value' to the cache iff main_stream = True"
if self.main_stream is True:
self.expr_cache[name] = value
def add_ir(self, expr):
"Add a Miasm2 IR to the last bbl. Return the var created"
if self.main_stream is True and expr in self.expr_cache:
return self.expr_cache[expr]
builder = self.builder
if isinstance(expr, m2_expr.ExprInt):
ret = llvm_ir.Constant(LLVMType.IntType(expr.size), int(expr.arg))
self.update_cache(expr, ret)
return ret
if isinstance(expr, m2_expr.ExprId):
name = expr.name
if not isinstance(name, str):
# Resolve label
offset = name.offset
ret = llvm_ir.Constant(LLVMType.IntType(expr.size), offset)
self.update_cache(expr, ret)
return ret
try:
# If expr.name is already known (args)
return self.local_vars[name]
except KeyError:
pass
ptr_casted = self.get_ptr_by_expr(expr)
var = builder.load(ptr_casted, name)
self.update_cache(expr, var)
return var
if isinstance(expr, m2_expr.ExprOp):
op = expr.op
if op == "parity":
fc_ptr = self.mod.get_global("parity")
arg = builder.zext(self.add_ir(expr.args[0]),
LLVMType.IntType())
ret = builder.call(fc_ptr, [arg])
ret = builder.trunc(ret, LLVMType.IntType(expr.size))
self.update_cache(expr, ret)
return ret
if op in ["<<<", ">>>"]:
fc_name = "rot_left" if op == "<<<" else "rot_right"
fc_ptr = self.mod.get_global(fc_name)
args = [self.add_ir(arg) for arg in expr.args]
arg_size = expr.args[0].size
if arg_size < 32:
# Cast args
args = [builder.zext(arg, LLVMType.IntType(32))
for arg in args]
arg_size_cst = llvm_ir.Constant(LLVMType.IntType(),
arg_size)
ret = builder.call(fc_ptr, [arg_size_cst] + args)
if arg_size < 32:
# Cast ret
ret = builder.trunc(ret, LLVMType.IntType(arg_size))
self.update_cache(expr, ret)
return ret
if op == "bcdadd":
size = expr.args[0].size
fc_ptr = self.mod.get_global("bcdadd_%s" % size)
args = [self.add_ir(arg) for arg in expr.args]
ret = builder.call(fc_ptr, args)
self.update_cache(expr, ret)
return ret
if op == "bcdadd_cf":
size = expr.args[0].size
fc_ptr = self.mod.get_global("bcdadd_cf_%s" % size)
args = [self.add_ir(arg) for arg in expr.args]
ret = builder.call(fc_ptr, args)
ret = builder.trunc(ret, LLVMType.IntType(expr.size))
self.update_cache(expr, ret)
return ret
if op == "-":
zero = llvm_ir.Constant(LLVMType.IntType(expr.size),
0)
ret = builder.sub(zero, self.add_ir(expr.args[0]))
self.update_cache(expr, ret)
return ret
if op == "segm":
fc_ptr = self.mod.get_global("segm2addr")
args_casted = [builder.zext(self.add_ir(arg), LLVMType.IntType(64))
for arg in expr.args]
args = [self.local_vars["vmcpu"]] + args_casted
ret = builder.call(fc_ptr, args)
ret = builder.trunc(ret, LLVMType.IntType(expr.size))
self.update_cache(expr, ret)
return ret
if op in ["imod", "idiv"]:
fc_ptr = self.mod.get_global(
"%s%s" % (op, expr.args[0].size))
args_casted = [self.add_ir(arg) for arg in expr.args]
args = [self.local_vars["vmcpu"]] + args_casted
ret = builder.call(fc_ptr, args)
self.update_cache(expr, ret)
return ret
if len(expr.args) > 1:
if op == "*":
callback = builder.mul
elif op == "+":
callback = builder.add
elif op == "&":
callback = builder.and_
elif op == "^":
callback = builder.xor
elif op == "|":
callback = builder.or_
elif op == ">>":
callback = builder.lshr
elif op == "<<":
callback = builder.shl
elif op == "a>>":
callback = builder.ashr
elif op == "udiv":
callback = builder.udiv
elif op == "umod":
callback = builder.urem
else:
raise NotImplementedError('Unknown op: %s' % op)
last = self.add_ir(expr.args[0])
for i in range(1, len(expr.args)):
last = callback(last,
self.add_ir(expr.args[i]))
self.update_cache(expr, last)
return last
raise NotImplementedError()
if isinstance(expr, m2_expr.ExprMem):
addr = self.add_ir(expr.arg)
return self.llvm_context.memory_lookup(self, addr, expr.size)
if isinstance(expr, m2_expr.ExprCond):
# Compute cond
cond = self.add_ir(expr.cond)
zero_casted = llvm_ir.Constant(LLVMType.IntType(expr.cond.size),
0)
condition_bool = builder.icmp_unsigned("!=", cond,
zero_casted)
# Alloc return var
alloca = self.CreateEntryBlockAlloca(LLVMType.IntType(expr.size))
# Create bbls
branch_id = self.new_branch_name()
then_block = self.append_basic_block('then%s' % branch_id)
else_block = self.append_basic_block('else%s' % branch_id)
merge_block = self.append_basic_block('ifcond%s' % branch_id)
builder.cbranch(condition_bool, then_block, else_block)
# Deactivate object caching
current_main_stream = self.main_stream
self.main_stream = False
# Then Bloc
builder.position_at_end(then_block)
then_value = self.add_ir(expr.src1)
builder.store(then_value, alloca)
builder.branch(merge_block)
# Else Bloc
builder.position_at_end(else_block)
else_value = self.add_ir(expr.src2)
builder.store(else_value, alloca)
builder.branch(merge_block)
# Merge bloc
builder.position_at_end(merge_block)
ret = builder.load(alloca)
# Reactivate object caching
self.main_stream = current_main_stream
self.update_cache(expr, ret)
return ret
if isinstance(expr, m2_expr.ExprSlice):
src = self.add_ir(expr.arg)
# Remove trailing bits
if expr.start != 0:
to_shr = llvm_ir.Constant(LLVMType.IntType(expr.arg.size),
expr.start)
shred = builder.lshr(src,
to_shr)
else:
shred = src
# Remove leading bits
to_and = llvm_ir.Constant(LLVMType.IntType(expr.arg.size),
(1 << (expr.stop - expr.start)) - 1)
anded = builder.and_(shred,
to_and)
# Cast into e.size
ret = builder.trunc(anded,
LLVMType.IntType(expr.size))
self.update_cache(expr, ret)
return ret
if isinstance(expr, m2_expr.ExprCompose):
args = []
# Build each part
for start, src in expr.iter_args():
# src & size
src = self.add_ir(src)
src_casted = builder.zext(src,
LLVMType.IntType(expr.size))
to_and = llvm_ir.Constant(LLVMType.IntType(expr.size),
(1 << src.type.width) - 1)
anded = builder.and_(src_casted,
to_and)
if (start != 0):
# result << start
to_shl = llvm_ir.Constant(LLVMType.IntType(expr.size),
start)
shled = builder.shl(anded, to_shl)
final = shled
else:
# Optimisation
final = anded
args.append(final)
# result = part1 | part2 | ...
last = args[0]
for i in xrange(1, len(expr.args)):
last = builder.or_(last, args[i])
self.update_cache(expr, last)
return last
raise Exception("UnkownExpression", expr.__class__.__name__)
def set_ret(self, var):
"Cast @var and return it at the end of current bbl"
if var.type.width < 64:
var_casted = self.builder.zext(var, LLVMType.IntType(64))
else:
var_casted = var
self.builder.ret(var_casted)
def from_expr(self, expr):
"Build the function from an expression"
# Build function signature
args = expr.get_r(True)
for a in args:
if not isinstance(a, m2_expr.ExprMem):
self.my_args.append((a, LLVMType.IntType(a.size), a.name))
self.ret_type = LLVMType.IntType(expr.size)
# Initialise the function
self.init_fc()
ret = self.add_ir(expr)
self.set_ret(ret)
def affect(self, src, dst):
"Affect from LLVM src to M2 dst"
# Destination
builder = self.builder
if isinstance(dst, m2_expr.ExprId):
dst_name = dst.name
if dst_name == "IRDst":
self.local_vars[dst_name] = src
else:
ptr_casted = self.get_ptr_by_expr(
m2_expr.ExprId(dst_name, dst.size))
builder.store(src, ptr_casted)
elif isinstance(dst, m2_expr.ExprMem):
addr = self.add_ir(dst.arg)
self.llvm_context.memory_write(self, addr, dst.size, src)
else:
raise Exception("UnknownAffectationType")
def check_error(self, line, except_do_not_update_pc=False):
"""Add a check for memory errors.
@line: Irbloc line corresponding to the current instruction
If except_do_not_update_pc, check only for exception which do not
require a pc update"""
# VmMngr "get_exception_flag" return's size
size = 64
t_size = LLVMType.IntType(size)
# Current address
pc_to_return = line.offset
# Get exception flag value
builder = self.builder
fc_ptr = self.mod.get_global("get_exception_flag")
exceptionflag = builder.call(fc_ptr, [self.local_vars["vmmngr"]])
if except_do_not_update_pc is True:
auto_mod_flag = m2_csts.EXCEPT_DO_NOT_UPDATE_PC
m2_flag = llvm_ir.Constant(t_size, auto_mod_flag)
exceptionflag = builder.and_(exceptionflag, m2_flag)
# Compute cond
zero_casted = llvm_ir.Constant(t_size, 0)
condition_bool = builder.icmp_unsigned("!=",
exceptionflag,
zero_casted)
# Create bbls
branch_id = self.new_branch_name()
then_block = self.append_basic_block('then%s' % branch_id)
merge_block = self.append_basic_block('ifcond%s' % branch_id)
builder.cbranch(condition_bool, then_block, merge_block)
# Deactivate object caching
current_main_stream = self.main_stream
self.main_stream = False
# Then Bloc
builder.position_at_end(then_block)
self.set_ret(llvm_ir.Constant(self.ret_type, pc_to_return))
builder.position_at_end(merge_block)
# Reactivate object caching
self.main_stream = current_main_stream
def log_instruction(self, instruction, line):
"Print current instruction and registers if options are set"
# Get builder
builder = self.builder
if self.log_mn is True:
print instruction # TODO
if self.log_regs is True:
# Call dump general purpose registers
fc_ptr = self.mod.get_global("dump_gpregs")
builder.call(fc_ptr, [self.local_vars["vmcpu"]])
def add_bloc(self, bloc, lines):
"Add a bloc of instruction in the current function"
for assignblk, line in zip(bloc, lines):
new_reg = {}
# Check general errors only at the beggining of instruction
if line.offset not in self.offsets_jitted:
self.offsets_jitted.add(line.offset)
self.check_error(line)
# Log mn and registers if options is set
self.log_instruction(assignblk, line)
# Pass on empty instruction
if not assignblk:
continue
for dst, src in assignblk.iteritems():
# Apply preinit transformation
for func in self.llvm_context.IR_transformation_functions:
dst = func(dst)
src = func(src)
# Treat current expression
if isinstance(dst, m2_expr.ExprId):
new_reg[dst] = self.add_ir(src)
else:
assert isinstance(dst, m2_expr.ExprMem)
# Source
src = self.add_ir(src)
self.affect(src, dst)
# Check for errors (without updating PC)
self.check_error(line, except_do_not_update_pc=True)
# new -> normal
for dst, src in new_reg.iteritems():
self.affect(src, dst)
# Clear cache
self.clear_cache(new_reg)
self.main_stream = True
def from_bloc(self, bloc, final_expr):
"""Build the function from a bloc, with the dst equation.
Prototype : f(i8* vmcpu, i8* vmmngr)"""
# Build function signature
self.my_args.append((m2_expr.ExprId("vmcpu"),
llvm_ir.PointerType.pointer(LLVMType.IntType(8)),
"vmcpu"))
self.my_args.append((m2_expr.ExprId("vmmngr"),
llvm_ir.PointerType.pointer(LLVMType.IntType(8)),
"vmmngr"))
self.ret_type = LLVMType.IntType(final_expr.size)
# Initialise the function
self.init_fc()
# Add content
self.add_bloc(bloc, [])
# Finalise the function
self.set_ret(self.add_ir(final_expr))
raise NotImplementedError("Not tested")
def canonize_label_name(self, label):
"""Canonize @label names to a common form.
@label: str or asmlabel instance"""
if isinstance(label, str):
return label
elif isinstance(label, m2_asmbloc.asm_label):
return "label_%s" % label.name
else:
raise ValueError("label must either be str or asmlabel")
def get_basic_bloc_by_label(self, label):
"Return the bbl corresponding to label, None otherwise"
return self.name2bbl.get(self.canonize_label_name(label), None)
def gen_ret_or_branch(self, dest):
"""Manage the dest ExprId. If label, branch on it if it is known.
Otherwise, return the ExprId or the offset value"""
builder = self.builder
if isinstance(dest, m2_expr.ExprId):
dest_name = dest.name
elif isinstance(dest, m2_expr.ExprSlice) and \
isinstance(dest.arg, m2_expr.ExprId):
# Manage ExprId mask case
dest_name = dest.arg.name
else:
raise ValueError()
if not isinstance(dest_name, str):
label = dest_name
target_bbl = self.get_basic_bloc_by_label(label)
if target_bbl is None:
self.set_ret(self.add_ir(dest))
else:
builder.branch(target_bbl)
else:
self.set_ret(self.add_ir(dest))
def add_irbloc(self, irbloc):
"Add the content of irbloc at the corresponding labeled block"
builder = self.builder
bloc = irbloc.irs
dest = irbloc.dst
label = irbloc.label
lines = irbloc.lines
# Get labeled basic bloc
label_block = self.get_basic_bloc_by_label(label)
builder.position_at_end(label_block)
# Erase cache
self.expr_cache = {}
# Add the content of the bloc with corresponding lines
self.add_bloc(bloc, lines)
# Erase cache
self.expr_cache = {}
# Manage ret
for func in self.llvm_context.IR_transformation_functions:
dest = func(dest)
if isinstance(dest, m2_expr.ExprCond):
# Compute cond
cond = self.add_ir(dest.cond)
zero_casted = llvm_ir.Constant(LLVMType.IntType(dest.cond.size),
0)
condition_bool = builder.icmp_unsigned("!=", cond,
zero_casted)
# Create bbls
branch_id = self.new_branch_name()
then_block = self.append_basic_block('then%s' % branch_id)
else_block = self.append_basic_block('else%s' % branch_id)
builder.cbranch(condition_bool, then_block, else_block)
# Then Bloc
builder.position_at_end(then_block)
self.gen_ret_or_branch(dest.src1)
# Else Bloc
builder.position_at_end(else_block)
self.gen_ret_or_branch(dest.src2)
elif isinstance(dest, m2_expr.ExprId):
self.gen_ret_or_branch(dest)
elif isinstance(dest, m2_expr.ExprSlice):
self.gen_ret_or_branch(dest)
elif isinstance(dest, m2_expr.ExprMem):
self.set_ret(self.add_ir(m2_expr.ExprId("IRDst")))
else:
raise Exception("Bloc dst has to be an ExprId or an ExprCond")
def from_blocs(self, blocs):
"""Build the function from a list of bloc (irbloc instances).
Prototype : f(i8* vmcpu, i8* vmmngr)"""
# Build function signature
self.my_args.append((m2_expr.ExprId("vmcpu"),
llvm_ir.PointerType(LLVMType.IntType(8)),
"vmcpu"))
self.my_args.append((m2_expr.ExprId("vmmngr"),
llvm_ir.PointerType(LLVMType.IntType(8)),
"vmmngr"))
ret_size = 64
self.ret_type = LLVMType.IntType(ret_size)
# Initialise the function
self.init_fc()
# Create basic blocks (for label branchs)
entry_bbl, builder = self.entry_bbl, self.builder
for irbloc in blocs:
name = self.canonize_label_name(irbloc.label)
self.append_basic_block(name)
# Add content
builder.position_at_end(entry_bbl)
for irbloc in blocs:
self.add_irbloc(irbloc)
# Branch entry_bbl on first label
builder.position_at_end(entry_bbl)
first_label_bbl = self.get_basic_bloc_by_label(blocs[0].label)
builder.branch(first_label_bbl)
def __str__(self):
"Print the llvm IR corresponding to the current module"
return str(self.fc)
def verify(self):
"Verify the module syntax"
return self.mod.verify()
def get_assembly(self):
"Return native assembly corresponding to the current module"
return self.mod.to_native_assembly()
def optimise(self):
"Optimise the function in place"
while self.llvm_context.pass_manager.run(self.fc):
continue
def __call__(self, *args):
"Eval the function with arguments args"
e = self.llvm_context.get_execengine()
genargs = [LLVMType.generic(a) for a in args]
ret = e.run_function(self.fc, genargs)
return ret.as_int()
def get_function_pointer(self):
"Return a pointer on the Jitted function"
# Parse our generated module
mod = llvm.parse_assembly( str( self.mod ) )
mod.verify()
# Now add the module and make sure it is ready for execution
target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
engine = llvm.create_mcjit_compiler(mod,
target_machine)
engine.finalize_object()
# For debug: obj_bin = target_machine.emit_object(mod)
self.llvm_context.engines.append(engine)
return engine.get_function_address(self.fc.name)
# TODO:
# - Add more expressions
|