9 #include "../src/mc/mc_private.h"
11 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
12 state->stack_size = 0;
15 ops[0].atom = DW_OP_addr;
17 ops[1].atom = DW_OP_addr;
21 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
22 assert(state->stack_size==1);
23 return state->stack[state->stack_size - 1];
26 void basic_test(mc_expression_state_t state) {
32 ops[0].atom = DW_OP_drop;
33 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
35 ops[0].atom = DW_OP_lit21;
36 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
37 assert(state->stack_size==1);
38 assert(state->stack[state->stack_size-1]==21);
40 ops[0].atom = DW_OP_addr;
42 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
43 assert(state->stack_size==2);
44 assert(state->stack[state->stack_size-1] == a);
46 ops[0].atom = DW_OP_drop;
47 ops[1].atom = DW_OP_drop;
48 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
49 assert(state->stack_size==0);
51 ops[0].atom = DW_OP_lit21;
52 ops[1].atom = DW_OP_plus_uconst;
54 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
55 assert(state->stack_size==1);
56 assert(state->stack[state->stack_size-1]== a + 21);
58 state->stack_size = 0;
59 ops[0].atom = DW_OP_addr;
61 ops[1].atom = DW_OP_dup;
62 ops[2].atom = DW_OP_plus;
63 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
64 assert(state->stack_size==1);
65 assert(state->stack[state->stack_size-1]== a + a);
67 state->stack_size = 0;
68 ops[0].atom = DW_OP_addr;
70 ops[1].atom = DW_OP_addr;
72 ops[2].atom = DW_OP_over;
73 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
74 assert(state->stack_size==3);
75 assert(state->stack[state->stack_size-1]== a);
76 assert(state->stack[state->stack_size-2]== b);
77 assert(state->stack[state->stack_size-3]== a);
79 state->stack_size = 0;
80 ops[0].atom = DW_OP_addr;
82 ops[1].atom = DW_OP_addr;
84 ops[2].atom = DW_OP_swap;
85 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
86 assert(state->stack_size=2);
87 assert(state->stack[state->stack_size-1]== a);
88 assert(state->stack[state->stack_size-2]== b);
91 int main(int argc, char** argv) {
92 s_mc_expression_state_t state;
93 memset(&state, 0, sizeof(s_mc_expression_state_t));
97 for(int i=0; i!=100; ++i) {
100 assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
103 for(int i=0; i!=100; ++i) {
104 uintptr_t a = rand();
105 uintptr_t b = rand();
106 assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
109 for(int i=0; i!=100; ++i) {
110 uintptr_t a = rand();
111 uintptr_t b = rand();
112 assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
115 for(int i=0; i!=100; ++i) {
116 uintptr_t a = rand();
117 uintptr_t b = rand();
118 assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));