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
//! The decompiler used to get haxe sources back from the bytecode definitions.
//! More info on how everything works in the [wiki](https://github.com/Gui-Yom/hlbc/wiki/Decompilation).
//!
//! The decompiler takes bytecode elements as input and outputs [ast] structures that can be displayed.
use std::collections::{HashMap, HashSet};
use ast::*;
use hlbc::opcodes::Opcode;
use hlbc::types::{FunPtr, Function, RefField, Reg, Type, TypeObj};
use hlbc::Bytecode;
use scopes::*;
#[cfg(feature = "alt")]
mod alt;
/// A simple representation for the Haxe source code generated by the decompiler
pub mod ast;
/// Functions to render the [ast] to a string
pub mod fmt;
/// AST post-processing
mod post;
/// Scope handling structures
mod scopes;
enum ExprCtx {
Constructor {
reg: Reg,
pos: usize,
},
Anonymous {
pos: usize,
fields: HashMap<RefField, Expr>,
remaining: usize,
},
}
/// Decompile a function code to a list of [Statement]s.
/// This works by analyzing each opcodes in order while trying to reconstruct scopes, contexts and intents.
pub fn decompile_code(code: &Bytecode, f: &Function) -> Vec<Statement> {
// Scope stack, holds the statements
let mut scopes = Scopes::new();
// Current iteration statement, to be pushed onto the finished statements or the nesting
//let mut statement = None;
// Expression values for each registers
let mut reg_state = HashMap::with_capacity(f.regs.len());
// For parsing statements made of multiple instructions like constructor calls and anonymous structures
// TODO move this to another pass on the generated ast
let mut expr_ctx = Vec::new();
// Variable names we already declared
let mut seen = HashSet::new();
let mut start = 0;
// First argument / First register is 'this'
if f.is_method()
|| f.name
.map(|n| n.resolve(&code.strings) == "__constructor__")
.unwrap_or(false)
{
reg_state.insert(Reg(0), cst_this());
start = 1;
}
// Initialize register state with the function arguments
for i in start..f.ty(code).args.len() {
let name = f.arg_name(code, i - start).map(ToOwned::to_owned);
reg_state.insert(Reg(i as u32), Expr::Variable(Reg(i as u32), name.clone()));
if let Some(name) = name {
seen.insert(name);
}
}
macro_rules! push_stmt {
($stmt:expr) => {
//statement = Some($stmt);
scopes.push_stmt($stmt);
};
}
// Update the register state and create a statement depending on inline rules
macro_rules! push_expr {
($i:expr, $dst:expr, $e:expr) => {
let name = f.var_name(code, $i);
let expr = $e;
// Inline check
if name.is_none() {
reg_state.insert($dst, expr);
} else {
reg_state.insert($dst, Expr::Variable($dst, name.clone()));
push_stmt!(Statement::Assign {
declaration: seen.insert(name.clone().unwrap()),
variable: Expr::Variable($dst, name),
assign: expr,
});
}
};
}
let missing_expr = || Expr::Unknown("missing expr".to_owned());
// Get the expr for a register
macro_rules! expr {
($reg:expr) => {
reg_state.get(&$reg).cloned().unwrap_or_else(missing_expr)
};
}
// Crate a Vec<Expression> for a list of args
macro_rules! make_args {
($($arg:expr),* $(,)?) => {
vec![$(expr!($arg)),*]
}
}
macro_rules! push_call {
($i:ident, $dst:ident, $fun:ident, $arg0:expr $(, $args:expr)*) => {
if let Some(&ExprCtx::Constructor { reg, pos }) = expr_ctx.last() {
if reg == $arg0 {
push_expr!(
pos,
reg,
Expr::Constructor(ConstructorCall::new(f.regtype(reg), make_args!($($args),*)))
);
expr_ctx.pop();
}
} else {
match $fun.resolve(code) {
FunPtr::Fun(func) => {
let call = if func.is_method() {
call(Expr::Field(Box::new(expr!($arg0)), func.name.clone().unwrap().resolve(&code.strings).to_owned()), make_args!($($args),*))
} else {
call_fun($fun, make_args!($arg0 $(, $args)*))
};
if func.ty(code).ret.is_void() {
push_stmt!(stmt(call));
} else {
push_expr!($i, $dst, call);
}
}
FunPtr::Native(n) => {
let call = call_fun($fun, make_args!($arg0 $(, $args)*));
if n.ty(code).ret.is_void() {
push_stmt!(stmt(call));
} else {
push_expr!($i, $dst, call);
}
}
}
}
};
}
// Process a jmp instruction, might be the exit condition of a loop or an if
macro_rules! push_jmp {
($i:ident, $offset:ident, $cond:expr) => {
if $offset > 0 {
let cond = $cond;
// It's a loop
if matches!(f.ops[$i + $offset as usize], Opcode::JAlways { offset } if offset < 0) {
if let Some(loop_cond) = scopes.update_last_loop_cond() {
if matches!(loop_cond, Expr::Unknown(_)) {
println!("old loop cond : {:?}", loop_cond);
*loop_cond = cond;
} else {
scopes.push_if($offset + 1, cond);
}
} else {
scopes.push_if($offset + 1, cond);
}
} else {
// It's an if
scopes.push_if($offset + 1, cond);
}
}
}
}
let iter = f.ops.iter().enumerate();
for (i, o) in iter {
// Opcodes are grouped by semantic
// Control flow first because they are the most important
match o {
//region CONTROL FLOW
&Opcode::JTrue { cond, offset } => {
push_jmp!(i, offset, not(expr!(cond)))
}
&Opcode::JFalse { cond, offset } => {
push_jmp!(i, offset, expr!(cond))
}
&Opcode::JNull { reg, offset } => {
push_jmp!(i, offset, noteq(expr!(reg), cst_null()))
}
&Opcode::JNotNull { reg, offset } => {
push_jmp!(i, offset, eq(expr!(reg), cst_null()))
}
&Opcode::JSGte { a, b, offset } | &Opcode::JUGte { a, b, offset } => {
push_jmp!(i, offset, gt(expr!(b), expr!(a)))
}
&Opcode::JSGt { a, b, offset } => {
push_jmp!(i, offset, gte(expr!(b), expr!(a)))
}
&Opcode::JSLte { a, b, offset } => {
push_jmp!(i, offset, lt(expr!(b), expr!(a)))
}
&Opcode::JSLt { a, b, offset } | &Opcode::JULt { a, b, offset } => {
push_jmp!(i, offset, lte(expr!(b), expr!(a)))
}
&Opcode::JEq { a, b, offset } => {
push_jmp!(i, offset, noteq(expr!(a), expr!(b)))
}
&Opcode::JNotEq { a, b, offset } => {
push_jmp!(i, offset, eq(expr!(a), expr!(b)))
}
// Unconditional jumps can actually mean a lot of things
&Opcode::JAlways { offset } => {
if offset < 0 {
// It's either the jump backward of a loop or a continue statement
let loop_start = scopes
.last_loop_start()
.expect("Backward jump but we aren't in a loop ?");
// Scan the next instructions in order to find another jump to the same place
if f.ops.iter().enumerate().skip(i + 1).find_map(|(j, o)| {
// We found another jump to the same place !
if matches!(o, Opcode::JAlways {offset} if (j as i32 + offset + 1) as usize == loop_start) {
Some(true)
} else {
None
}
}).unwrap_or(false) {
// If this jump is not the last jump backward for the current loop, so it's definitely a continue; statement
push_stmt!(Statement::Continue);
} else {
// It's the last jump backward of the loop, which means the end of the loop
// we generate the loop statement
if let Some(stmt) = scopes.end_last_loop() {
push_stmt!(stmt);
} else {
panic!("Last scope is not a loop !");
}
}
} else {
if let Some(offsets) = scopes.last_is_switch_ctx() {
if let Some(pos) = offsets.iter().position(|o| *o == i) {
scopes.push_switch_case(pos);
} else {
panic!("no matching offset for switch case ({i})");
}
} else if scopes.last_loop_start().is_some() {
// Check the instruction just before the jump target
// If it's a jump backward of a loop
if matches!(f.ops[(i as i32 + offset) as usize], Opcode::JAlways {offset} if offset < 0)
{
// It's a break condition
push_stmt!(Statement::Break);
}
} else if scopes.last_is_if() {
// It's the jump over of an else clause
scopes.push_else(offset + 1);
} else {
eprintln!(
"{i}: JAlways has no matching scope (last: {:?})",
scopes.scopes.last()
);
}
}
}
Opcode::Switch { reg, offsets, end } => {
// Convert to absolute positions
scopes.push_switch(
*end + 1,
expr!(reg),
offsets.iter().map(|o| i + *o as usize).collect(),
);
// The default switch case is implicit
}
&Opcode::Label => scopes.push_loop(i),
&Opcode::Ret { ret } => {
// Do not display return void; only in case of an early return
if scopes.has_scopes() {
push_stmt!(Statement::Return(if f.regtype(ret).is_void() {
None
} else {
Some(expr!(ret))
}));
} else if !f.regtype(ret).is_void() {
push_stmt!(Statement::Return(Some(expr!(ret))));
}
}
//endregion
//region EXCEPTIONS
&Opcode::Throw { exc } | &Opcode::Rethrow { exc } => {
push_stmt!(Statement::Throw(expr!(exc)));
}
&Opcode::Trap { exc, offset } => {
scopes.push_try(offset + 1);
}
&Opcode::EndTrap { exc } => {
// TODO try catch
}
//endregion
//region CONSTANTS
&Opcode::Int { dst, ptr } => {
push_expr!(i, dst, cst_int(ptr.resolve(&code.ints)));
}
&Opcode::Float { dst, ptr } => {
push_expr!(i, dst, cst_float(ptr.resolve(&code.floats)));
}
&Opcode::Bool { dst, value } => {
push_expr!(i, dst, cst_bool(value.0));
}
&Opcode::String { dst, ptr } => {
push_expr!(i, dst, cst_refstring(ptr, code));
}
&Opcode::Null { dst } => {
push_expr!(i, dst, cst_null());
}
//endregion
//region OPERATORS
&Opcode::Mov { dst, src } => {
push_expr!(i, dst, expr!(src));
// Workaround for when the instructions after this one use dst and src interchangeably.
reg_state.insert(src, Expr::Variable(dst, f.var_name(code, i)));
}
&Opcode::Add { dst, a, b } => {
push_expr!(i, dst, add(expr!(a), expr!(b)));
}
&Opcode::Sub { dst, a, b } => {
push_expr!(i, dst, sub(expr!(a), expr!(b)));
}
&Opcode::Mul { dst, a, b } => {
push_expr!(i, dst, mul(expr!(a), expr!(b)));
}
&Opcode::SDiv { dst, a, b } | &Opcode::UDiv { dst, a, b } => {
push_expr!(i, dst, div(expr!(a), expr!(b)));
}
&Opcode::SMod { dst, a, b } | &Opcode::UMod { dst, a, b } => {
push_expr!(i, dst, modulo(expr!(a), expr!(b)));
}
&Opcode::Shl { dst, a, b } => {
push_expr!(i, dst, shl(expr!(a), expr!(b)));
}
&Opcode::SShr { dst, a, b } | &Opcode::UShr { dst, a, b } => {
push_expr!(i, dst, shr(expr!(a), expr!(b)));
}
&Opcode::And { dst, a, b } => {
push_expr!(i, dst, and(expr!(a), expr!(b)));
}
&Opcode::Or { dst, a, b } => {
push_expr!(i, dst, or(expr!(a), expr!(b)));
}
&Opcode::Xor { dst, a, b } => {
push_expr!(i, dst, xor(expr!(a), expr!(b)));
}
&Opcode::Neg { dst, src } => {
push_expr!(i, dst, neg(expr!(src)));
}
&Opcode::Not { dst, src } => {
push_expr!(i, dst, not(expr!(src)));
}
&Opcode::Incr { dst } => {
// FIXME sometimes it should be an expression
push_stmt!(stmt(incr(expr!(dst))));
}
&Opcode::Decr { dst } => {
push_stmt!(stmt(decr(expr!(dst))));
}
//endregion
//region CALLS
&Opcode::Call0 { dst, fun } => {
if fun.ty(code).ret.is_void() {
push_stmt!(stmt(call_fun(fun, Vec::new())));
} else {
push_expr!(i, dst, call_fun(fun, Vec::new()));
}
}
&Opcode::Call1 { dst, fun, arg0 } => {
push_call!(i, dst, fun, arg0)
}
&Opcode::Call2 {
dst,
fun,
arg0,
arg1,
} => {
push_call!(i, dst, fun, arg0, arg1)
}
&Opcode::Call3 {
dst,
fun,
arg0,
arg1,
arg2,
} => {
push_call!(i, dst, fun, arg0, arg1, arg2)
}
&Opcode::Call4 {
dst,
fun,
arg0,
arg1,
arg2,
arg3,
} => {
push_call!(i, dst, fun, arg0, arg1, arg2, arg3)
}
Opcode::CallN { dst, fun, args } => {
if let Some(&ExprCtx::Constructor { reg, pos }) = expr_ctx.last() {
if reg == args[0] {
push_expr!(
pos,
reg,
Expr::Constructor(ConstructorCall::new(
f.regtype(reg),
args[1..].iter().map(|x| expr!(x)).collect::<Vec<_>>()
))
);
}
} else {
let call = call_fun(*fun, args.iter().map(|x| expr!(x)).collect::<Vec<_>>());
if fun.ty(code).ret.is_void() {
push_stmt!(stmt(call));
} else {
push_expr!(i, *dst, call);
}
}
}
Opcode::CallMethod { dst, field, args } => {
let call = call(
ast::field(expr!(args[0]), f.regtype(args[0]), *field, code),
args.iter().skip(1).map(|x| expr!(x)).collect::<Vec<_>>(),
);
if f.regtype(args[0])
.method(field.0, code)
.and_then(|p| p.findex.resolve_as_fn(code))
.map(|fun| fun.ty(code).ret.is_void())
.unwrap_or(false)
{
push_stmt!(stmt(call));
} else {
push_expr!(i, *dst, call);
}
}
Opcode::CallThis { dst, field, args } => {
let method = f.regs[0].method(field.0, code).unwrap();
let call = call(
Expr::Field(
Box::new(cst_this()),
method.name.resolve(&code.strings).to_owned(),
),
args.iter().map(|x| expr!(x)).collect::<Vec<_>>(),
);
if method
.findex
.resolve_as_fn(code)
.map(|fun| fun.ty(code).ret.is_void())
.unwrap_or(false)
{
push_stmt!(stmt(call));
} else {
push_expr!(i, *dst, call);
}
}
Opcode::CallClosure { dst, fun, args } => {
let call = call(
expr!(*fun),
args.iter().map(|x| expr!(x)).collect::<Vec<_>>(),
);
if f.regtype(*fun)
.resolve_as_fun(&code.types)
.map(|ty| ty.ret.is_void())
.unwrap_or(false)
{
push_stmt!(stmt(call));
} else {
push_expr!(i, *dst, call);
}
}
//endregion
//region CLOSURES
&Opcode::StaticClosure { dst, fun } => {
push_expr!(
i,
dst,
Expr::Closure(fun, decompile_code(code, fun.resolve_as_fn(code).unwrap()))
);
}
&Opcode::InstanceClosure { dst, obj, fun } => {
match f.regtype(obj).resolve(&code.types) {
// This is an anonymous enum holding the capture for the closure
Type::Enum { .. } => {
push_expr!(
i,
dst,
Expr::Closure(
fun,
decompile_code(code, fun.resolve_as_fn(code).unwrap())
)
);
}
_ => {
push_expr!(
i,
dst,
Expr::Field(
Box::new(expr!(obj)),
fun.resolve_as_fn(code)
.unwrap()
.name(code)
.unwrap_or("_")
.to_owned(),
)
);
}
}
}
//endregion
//region ACCESSES
&Opcode::GetGlobal { dst, global } => {
// Is a string
if f.regtype(dst).0 == 13 {
push_expr!(
i,
dst,
cst_string(
code.globals_initializers
.get(&global)
.and_then(|&x| {
code.constants.as_ref().map(|constants| {
code.strings[constants[x].fields[0]].to_owned()
})
})
.unwrap()
)
);
} else {
match f.regtype(dst).resolve(&code.types) {
Type::Obj(obj) | Type::Struct(obj) => {
push_expr!(i, dst, Expr::Variable(dst, Some(obj.name.display(code))));
}
Type::Enum { .. } => {
push_expr!(i, dst, Expr::Unknown("unknown enum variant".to_owned()));
}
_ => {}
}
}
}
&Opcode::Field { dst, obj, field } => {
push_expr!(i, dst, ast::field(expr!(obj), f.regtype(obj), field, code));
}
&Opcode::SetField { obj, field, src } => {
let ctx = expr_ctx.pop();
// Might be a SetField for an anonymous structure
if let Some(ExprCtx::Anonymous {
pos,
mut fields,
mut remaining,
}) = ctx
{
fields.insert(field, expr!(src));
remaining -= 1;
// If we filled all the structure fields, we emit an expr
if remaining == 0 {
push_expr!(pos, obj, Expr::Anonymous(f.regtype(obj), fields));
} else {
expr_ctx.push(ExprCtx::Anonymous {
pos,
fields,
remaining,
});
}
} else if let Some(ctx) = ctx {
expr_ctx.push(ctx);
} else {
// Otherwise this is just a normal field set
push_stmt!(Statement::Assign {
declaration: false,
variable: ast::field(expr!(obj), f.regtype(obj), field, code),
assign: expr!(src),
});
}
}
&Opcode::GetThis { dst, field } => {
push_expr!(i, dst, ast::field(cst_this(), f.regs[0], field, code));
}
&Opcode::SetThis { field, src } => {
push_stmt!(Statement::Assign {
declaration: false,
variable: ast::field(cst_this(), f.regs[0], field, code),
assign: expr!(src),
});
}
&Opcode::DynGet { dst, obj, field } => {
push_expr!(i, dst, array(expr!(obj), cst_refstring(field, code)));
}
&Opcode::DynSet { obj, field, src } => {
push_stmt!(Statement::Assign {
declaration: false,
variable: array(expr!(obj), cst_refstring(field, code)),
assign: expr!(src)
});
}
//endregion
//region VALUES
&Opcode::ToDyn { dst, src }
| &Opcode::ToSFloat { dst, src }
| &Opcode::ToUFloat { dst, src }
| &Opcode::ToInt { dst, src }
| &Opcode::SafeCast { dst, src }
| &Opcode::UnsafeCast { dst, src }
| &Opcode::ToVirtual { dst, src } => {
push_expr!(i, dst, expr!(src));
}
&Opcode::Ref { dst, src } => {
push_expr!(i, dst, expr!(src));
}
&Opcode::Unref { dst, src } => {
push_expr!(i, dst, expr!(src));
}
&Opcode::Setref { dst, value } => {
push_stmt!(Statement::Assign {
declaration: false,
variable: expr!(dst),
assign: expr!(value)
});
}
&Opcode::RefData { dst, src } => {
push_expr!(i, dst, expr!(src));
}
&Opcode::New { dst } => {
// Constructor analysis
let ty = f.regtype(dst).resolve(&code.types);
match ty {
Type::Obj(_) | Type::Struct(_) => {
expr_ctx.push(ExprCtx::Constructor { reg: dst, pos: i });
}
Type::Virtual { fields } => {
expr_ctx.push(ExprCtx::Anonymous {
pos: i,
fields: HashMap::with_capacity(fields.len()),
remaining: fields.len(),
});
}
_ => {
push_expr!(
i,
dst,
Expr::Constructor(ConstructorCall::new(f.regtype(dst), Vec::new()))
);
}
}
}
//endregion
//region ENUMS
&Opcode::EnumAlloc { dst, construct } => {
push_expr!(
i,
dst,
Expr::EnumConstr(f.regtype(dst), construct, Vec::new())
);
}
Opcode::MakeEnum {
dst,
construct,
args,
} => {
push_expr!(
i,
*dst,
Expr::EnumConstr(
f.regtype(*dst),
*construct,
args.iter().map(|x| expr!(x)).collect()
)
);
}
&Opcode::EnumIndex { dst, value } => {
push_expr!(
i,
dst,
Expr::Field(Box::new(expr!(value)), "constructorIndex".to_owned())
);
//push_expr!(i, dst, expr!(value));
}
&Opcode::EnumField {
dst,
value,
construct,
field,
} => {
push_expr!(
i,
dst,
Expr::Field(Box::new(expr!(value)), field.0.to_string())
);
}
&Opcode::SetEnumField { value, field, src } => match expr!(value) {
Expr::Variable(r, name) => {
push_stmt!(Statement::Assign {
declaration: false,
variable: Expr::Field(Box::new(expr!(value)), field.0.to_string()),
assign: expr!(src)
});
}
_ => {
push_stmt!(comment("closure capture"));
push_stmt!(Statement::Assign {
declaration: false,
variable: Expr::Field(Box::new(expr!(value)), field.0.to_string()),
assign: expr!(src)
});
}
},
//endregion
//region ARRAYS
&Opcode::ArraySize { dst, array } => {
push_expr!(
i,
dst,
Expr::Field(Box::new(expr!(array)), "length".to_owned())
);
}
&Opcode::GetArray { dst, array, index } => {
push_expr!(i, dst, ast::array(expr!(array), expr!(index)));
}
&Opcode::SetArray { array, index, src } => {
push_stmt!(Statement::Assign {
declaration: false,
variable: ast::array(expr!(array), expr!(index)),
assign: expr!(src)
});
}
//endregion
//region MEM
&Opcode::GetMem { dst, bytes, index } => {
push_expr!(i, dst, array(expr!(bytes), expr!(index)));
}
&Opcode::SetMem { bytes, index, src } => {
push_stmt!(Statement::Assign {
declaration: false,
variable: array(expr!(bytes), expr!(index)),
assign: expr!(src)
});
}
//endregion
_ => {}
}
scopes.advance();
}
let mut statements = scopes.statements();
// AST post processing step !
let passes: [Box<dyn Fn(&mut [Statement])>; 3] = [
Box::new(|stmts| visit_stmt(stmts, &mut post::if_expression)),
Box::new(|stmts| visit_expr(stmts, &mut |e| post::string_concat(code, e))),
Box::new(|stmts| visit_expr(stmts, &mut |e| post::itos(code, e))),
];
for pass in passes {
pass(&mut statements);
}
statements
}
/// Decompile a function out of context
pub fn decompile_function(code: &Bytecode, f: &Function) -> Method {
Method {
fun: f.findex,
static_: true,
dynamic: false,
statements: decompile_code(code, f),
}
}
/// Decompile a class with its static and instance fields and methods.
pub fn decompile_class(code: &Bytecode, obj: &TypeObj) -> Class {
let static_type = obj.get_static_type(code);
let mut fields = Vec::new();
for (i, f) in obj.own_fields.iter().enumerate() {
if obj
.bindings
.get(&RefField(i + obj.fields.len() - obj.own_fields.len()))
.is_some()
{
continue;
}
fields.push(ClassField {
name: f.name.display(code),
static_: false,
ty: f.t,
});
}
if let Some(ty) = static_type {
for (i, f) in ty.own_fields.iter().enumerate() {
if ty
.bindings
.get(&RefField(i + ty.fields.len() - ty.own_fields.len()))
.is_some()
{
continue;
}
fields.push(ClassField {
name: f.name.display(code),
static_: true,
ty: f.t,
});
}
}
let mut methods = Vec::new();
for fun in obj.bindings.values() {
methods.push(Method {
fun: *fun,
static_: false,
dynamic: true,
statements: decompile_code(code, fun.resolve_as_fn(code).unwrap()),
})
}
if let Some(ty) = static_type {
for fun in ty.bindings.values() {
methods.push(Method {
fun: *fun,
static_: true,
dynamic: false,
statements: decompile_code(code, fun.resolve_as_fn(code).unwrap()),
})
}
}
for f in &obj.protos {
methods.push(Method {
fun: f.findex,
static_: false,
dynamic: false,
statements: decompile_code(code, f.findex.resolve_as_fn(code).unwrap()),
})
}
Class {
name: obj.name.resolve(&code.strings).to_owned(),
parent: obj
.super_
.and_then(|ty| ty.resolve_as_obj(&code.types))
.map(|ty| ty.name.display(code)),
fields,
methods,
}
}