Skip to content

Commit

Permalink
Fix decompiling calls to native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-Yom committed Jul 18, 2022
1 parent 967c0f1 commit bfec8a7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/decompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::{HashMap, HashSet};

use ast::*;

use crate::types::{Function, RefField, Reg, Type, TypeObj};
use crate::types::{FunPtr, Function, RefField, Reg, Type, TypeObj};
use crate::Bytecode;
use crate::Opcode;

Expand Down Expand Up @@ -344,16 +344,27 @@ pub fn decompile_function(code: &Bytecode, f: &Function) -> Vec<Statement> {
expr_ctx.pop();
}
} else {
let func = $fun.resolve_as_fn(code).unwrap();
let call = if func.is_method() {
Call::new(Expr::Field(Box::new(expr!($arg0)), func.name.clone().unwrap().resolve(&code.strings).to_owned()), make_args!($($args),*))
} else {
Call::new_fun($fun, make_args!($arg0 $(, $args)*))
};
if func.ty(code).ret.is_void() {
push_stmt!(Statement::Call(call));
} else {
push_expr!($i, $dst, Expr::Call(Box::new(call)));
match $fun.resolve(code) {
FunPtr::Fun(func) => {
let call = if func.is_method() {
Call::new(Expr::Field(Box::new(expr!($arg0)), func.name.clone().unwrap().resolve(&code.strings).to_owned()), make_args!($($args),*))
} else {
Call::new_fun($fun, make_args!($arg0 $(, $args)*))
};
if func.ty(code).ret.is_void() {
push_stmt!(Statement::Call(call));
} else {
push_expr!($i, $dst, Expr::Call(Box::new(call)));
}
}
FunPtr::Native(n) => {
let call = Call::new_fun($fun, make_args!($arg0 $(, $args)*));
if n.ty(code).ret.is_void() {
push_stmt!(Statement::Call(call));
} else {
push_expr!($i, $dst, Expr::Call(Box::new(call)));
}
}
}
}
};
Expand Down Expand Up @@ -459,7 +470,7 @@ pub fn decompile_function(code: &Bytecode, f: &Function) -> Vec<Statement> {

//region CALLS
&Opcode::Call0 { dst, fun } => {
if fun.resolve_as_fn(code).unwrap().ty(code).ret.is_void() {
if fun.ty(code).ret.is_void() {
push_stmt!(Statement::Call(Call::new_fun(fun, Vec::new())));
} else {
push_expr!(i, dst, call_fun(fun, Vec::new()));
Expand Down Expand Up @@ -836,7 +847,11 @@ pub fn decompile_function(code: &Bytecode, f: &Function) -> Vec<Statement> {
}
}
&Opcode::EnumAlloc { dst, construct } => {
// TODO enum constructor
push_expr!(
i,
dst,
Expr::EnumConstr(f.regtype(dst), construct, Vec::new())
);
}
Opcode::MakeEnum {
dst,
Expand Down
42 changes: 42 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ pub struct Native {
pub findex: RefFun,
}

impl Native {
pub fn name<'a>(&self, code: &'a Bytecode) -> &'a str {
self.name.resolve(&code.strings)
}

/// Get the native function signature type
pub fn ty<'a>(&self, code: &'a Bytecode) -> &'a TypeFun {
// Guaranteed to be a TypeFun
self.t.resolve_as_fun(&code.types).expect("Unknown type ?")
}

pub fn args<'a>(&self, code: &'a Bytecode) -> &'a [RefType] {
&self.ty(code).args
}

pub fn ret<'a>(&self, code: &'a Bytecode) -> &'a Type {
self.ty(code).ret.resolve(&code.types)
}
}

/// A function definition with its code.
#[derive(Debug, Clone)]
pub struct Function {
Expand Down Expand Up @@ -325,6 +345,28 @@ impl RefFun {
pub fn resolve_as_fn<'a>(&self, code: &'a Bytecode) -> Option<&'a Function> {
code.findexes[self.0].resolve_as_fn(code)
}

pub fn name<'a>(&self, code: &'a Bytecode) -> Option<&'a str> {
match self.resolve(code) {
FunPtr::Fun(fun) => fun.name(code),
FunPtr::Native(n) => Some(n.name.resolve(&code.strings)),
}
}

pub fn ty<'a>(&self, code: &'a Bytecode) -> &'a TypeFun {
match self.resolve(code) {
FunPtr::Fun(fun) => fun.ty(code),
FunPtr::Native(n) => n.ty(code),
}
}

pub fn args<'a>(&self, code: &'a Bytecode) -> &'a [RefType] {
&self.ty(code).args
}

pub fn ret<'a>(&self, code: &'a Bytecode) -> &'a Type {
self.ty(code).ret.resolve(&code.types)
}
}

// Reference to a function or a native in the pool, but we know what is is.
Expand Down

0 comments on commit bfec8a7

Please sign in to comment.