9 #include "../src/mc/mc_private.h"
12 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
13 state->stack_size = 0;
16 ops[0].atom = DW_OP_const8u;
18 ops[1].atom = DW_OP_const8u;
22 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
23 assert(state->stack_size==1);
24 return state->stack[state->stack_size - 1];
28 void basic_test(mc_expression_state_t state) {
34 ops[0].atom = DW_OP_drop;
35 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
37 ops[0].atom = DW_OP_lit21;
38 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
39 assert(state->stack_size==1);
40 assert(state->stack[state->stack_size-1]==21);
42 ops[0].atom = DW_OP_const8u;
44 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
45 assert(state->stack_size==2);
46 assert(state->stack[state->stack_size-1] == a);
48 ops[0].atom = DW_OP_drop;
49 ops[1].atom = DW_OP_drop;
50 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
51 assert(state->stack_size==0);
53 ops[0].atom = DW_OP_lit21;
54 ops[1].atom = DW_OP_plus_uconst;
56 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
57 assert(state->stack_size==1);
58 assert(state->stack[state->stack_size-1]== a + 21);
60 state->stack_size = 0;
61 ops[0].atom = DW_OP_const8u;
63 ops[1].atom = DW_OP_dup;
64 ops[2].atom = DW_OP_plus;
65 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
66 assert(state->stack_size==1);
67 assert(state->stack[state->stack_size-1]== a + a);
69 state->stack_size = 0;
70 ops[0].atom = DW_OP_const8u;
72 ops[1].atom = DW_OP_const8u;
74 ops[2].atom = DW_OP_over;
75 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
76 assert(state->stack_size==3);
77 assert(state->stack[state->stack_size-1]== a);
78 assert(state->stack[state->stack_size-2]== b);
79 assert(state->stack[state->stack_size-3]== a);
81 state->stack_size = 0;
82 ops[0].atom = DW_OP_const8u;
84 ops[1].atom = DW_OP_const8u;
86 ops[2].atom = DW_OP_swap;
87 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
88 assert(state->stack_size=2);
89 assert(state->stack[state->stack_size-1]== a);
90 assert(state->stack[state->stack_size-2]== b);
94 void test_deref(mc_expression_state_t state) {
98 ops[0].atom = DW_OP_const8u;
99 ops[0].number = (Dwarf_Word) &foo;
100 ops[1].atom = DW_OP_deref;
101 state->stack_size = 0;
103 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
104 assert(state->stack_size==1);
105 assert(state->stack[state->stack_size-1] == foo);
108 int main(int argc, char** argv) {
109 s_mc_expression_state_t state;
110 memset(&state, 0, sizeof(s_mc_expression_state_t));
114 for(int i=0; i!=100; ++i) {
115 uintptr_t a = rand();
116 uintptr_t b = rand();
117 assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
120 for(int i=0; i!=100; ++i) {
121 uintptr_t a = rand();
122 uintptr_t b = rand();
123 assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
126 for(int i=0; i!=100; ++i) {
127 uintptr_t a = rand();
128 uintptr_t b = rand();
129 assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
132 for(int i=0; i!=100; ++i) {
133 uintptr_t a = rand();
134 uintptr_t b = rand();
135 assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));