Rizin
unix-like reverse engineering framework and cli tools
theory_init.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 heersin <teablearcher@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_il/rz_il_opcodes.h>
5 #include <rz_il/rz_il_vm.h>
6 
9 
10  RzILOpArgsIte *op_ite = &op->op.ite;
11 
12  RzILBool *condition = rz_il_evaluate_bool(vm, op_ite->condition);
13  if (!condition) {
14  return NULL;
15  }
16  RzILVal *ret;
17  if (condition->b) {
18  ret = rz_il_evaluate_pure(vm, op_ite->x, type); // true branch
19  } else {
20  ret = rz_il_evaluate_pure(vm, op_ite->y, type); // false branch
21  }
22  rz_il_bool_free(condition);
23  return ret;
24 }
25 
27  rz_return_val_if_fail(vm && op && type, NULL);
28 
29  RzILOpArgsVar *var_op = &op->op.var;
30  RzILVal *val = rz_il_vm_get_var_value(vm, var_op->kind, var_op->v);
31  if (!val) {
32  RZ_LOG_ERROR("RzIL: reading value of variable \"%s\" of kind %s failed.\n",
33  var_op->v, rz_il_var_kind_name(var_op->kind));
34  return NULL;
35  }
36 
37  if (var_op->kind == RZ_IL_VAR_KIND_GLOBAL) {
39  }
40 
41  void *ret = NULL;
42  switch (val->type) {
45  ret = rz_il_bool_new(val->data.b->b);
46  break;
49  ret = rz_bv_dup(val->data.bv);
50  break;
51  default:
52  break;
53  }
54  return ret;
55 }
56 
58  rz_return_val_if_fail(vm && op && type, NULL);
59  RzILOpArgsLet *args = &op->op.let;
60  RzILVal *v = rz_il_evaluate_val(vm, args->exp);
61  if (!v) {
62  return NULL;
63  }
65  void *r = rz_il_evaluate_pure(vm, args->body, type);
66  rz_il_vm_pop_local_pure_var(vm, args->name, prev);
67  return r;
68 }
69 
71  rz_return_val_if_fail(vm && op && type, NULL);
72  RZ_LOG_ERROR("RzIL: unimplemented op handler (%d).\n", (int)op->code);
73  return NULL;
74 }
75 
78  RZ_LOG_ERROR("RzIL: unimplemented op handler (%d).\n", (int)op->code);
79  return false;
80 }
ut16 val
Definition: armass64_const.h:6
RZ_API RzILBool * rz_il_bool_new(bool true_or_false)
Definition: bool.c:11
RZ_API void rz_il_bool_free(RzILBool *bool_var)
Definition: bool.c:74
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
const char * v
Definition: dsignal.c:12
RZ_API RZ_OWN RzILEvent * rz_il_event_var_read_new(RZ_NONNULL const char *name, RZ_NULLABLE const RzILVal *value)
Definition: il_events.c:152
RZ_API RzILLocalPurePrev rz_il_vm_push_local_pure_var(RZ_NONNULL RzILVM *vm, RZ_NONNULL const char *name, RzILVal *val)
Create and assign a new local let binding.
Definition: il_vm.c:215
RZ_API void rz_il_vm_pop_local_pure_var(RZ_NONNULL RzILVM *vm, RZ_NONNULL const char *name, RzILLocalPurePrev prev)
Remove a local let binding and restore the state for the outer context.
Definition: il_vm.c:227
RZ_API RZ_BORROW RzILVal * rz_il_vm_get_var_value(RZ_NONNULL RzILVM *vm, RzILVarKind kind, const char *name)
Definition: il_vm.c:264
RZ_API RZ_NULLABLE RZ_OWN void * rz_il_evaluate_pure(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpPure *op, RZ_NONNULL RzILTypePure *type)
Definition: il_vm_eval.c:332
RZ_API void rz_il_vm_event_add(RzILVM *vm, RzILEvent *evt)
Definition: il_vm_eval.c:201
RZ_API RZ_NULLABLE RZ_OWN RzILBool * rz_il_evaluate_bool(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpBool *op)
Definition: il_vm_eval.c:289
RZ_API RZ_NULLABLE RZ_OWN RzILVal * rz_il_evaluate_val(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpPure *op)
Definition: il_vm_eval.c:309
int args
Definition: mipsasm.c:18
int type
Definition: mipsasm.c:17
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RZ_OWN RzBitVector * rz_bv_dup(const RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:167
signatures of core theory opcodes
RZ_NULLABLE RzILVal * RzILLocalPurePrev
Definition: rz_il_vm.h:86
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RzILTypePure
Definition: sort.h:23
@ RZ_IL_TYPE_PURE_BOOL
Definition: sort.h:24
@ RZ_IL_TYPE_PURE_BITVECTOR
Definition: sort.h:25
bool b
Definition: bool.h:15
op structure for ite (bool -> 'a pure -> 'a pure -> 'a pure)
RzILOpPure * y
index of RzILVal operand 2
RzILOpPure * x
index of RzILVal operand 1
RzILOpBool * condition
index of BOOL condition
op structure for let_ : 'a var -> 'a pure -> 'b pure -> 'b pure
op structure for var ('a var -> 'a pure)
RzILVarKind kind
set of variables to pick from
const char * v
name of variable, const one
An IL op performing a pure computation, 'a pure.
Low-level VM to execute raw IL code.
Definition: rz_il_vm.h:37
void * rz_il_handler_ite(RzILVM *vm, RzILOpPure *op, RzILTypePure *type)
Definition: theory_init.c:7
void * rz_il_handler_let(RzILVM *vm, RzILOpPure *op, RzILTypePure *type)
Definition: theory_init.c:57
bool rz_il_handler_effect_unimplemented(RzILVM *vm, RzILOpEffect *op)
Definition: theory_init.c:76
void * rz_il_handler_pure_unimplemented(RzILVM *vm, RzILOpPure *op, RzILTypePure *type)
Definition: theory_init.c:70
void * rz_il_handler_var(RzILVM *vm, RzILOpPure *op, RzILTypePure *type)
Definition: theory_init.c:26
Definition: dis.c:32
struct op_code code
Definition: dis.c:33
const char * rz_il_var_kind_name(RzILVarKind kind)
Definition: variable.c:181
@ RZ_IL_VAR_KIND_GLOBAL
global var, usually bound to a physical representation like a register.
Definition: variable.h:47