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 test_deref(mc_expression_state_t state) {
95 ops[0].atom = DW_OP_addr;
96 ops[0].number = (Dwarf_Word) &foo;
97 ops[1].atom = DW_OP_deref;
98 state->stack_size = 0;
100 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
101 assert(state->stack_size==1);
102 assert(state->stack[state->stack_size-1] == foo);
105 int main(int argc, char** argv) {
106 s_mc_expression_state_t state;
107 memset(&state, 0, sizeof(s_mc_expression_state_t));
111 for(int i=0; i!=100; ++i) {
112 uintptr_t a = rand();
113 uintptr_t b = rand();
114 assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
117 for(int i=0; i!=100; ++i) {
118 uintptr_t a = rand();
119 uintptr_t b = rand();
120 assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
123 for(int i=0; i!=100; ++i) {
124 uintptr_t a = rand();
125 uintptr_t b = rand();
126 assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
129 for(int i=0; i!=100; ++i) {
130 uintptr_t a = rand();
131 uintptr_t b = rand();
132 assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));