1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "../src/mc/mc_private.h"
18 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
19 state->stack_size = 0;
22 ops[0].atom = DW_OP_const8u;
24 ops[1].atom = DW_OP_const8u;
28 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
29 assert(state->stack_size==1);
30 return state->stack[state->stack_size - 1];
34 void basic_test(mc_expression_state_t state) {
40 ops[0].atom = DW_OP_drop;
41 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
43 ops[0].atom = DW_OP_lit21;
44 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
45 assert(state->stack_size==1);
46 assert(state->stack[state->stack_size-1]==21);
48 ops[0].atom = DW_OP_const8u;
50 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
51 assert(state->stack_size==2);
52 assert(state->stack[state->stack_size-1] == a);
54 ops[0].atom = DW_OP_drop;
55 ops[1].atom = DW_OP_drop;
56 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
57 assert(state->stack_size==0);
59 ops[0].atom = DW_OP_lit21;
60 ops[1].atom = DW_OP_plus_uconst;
62 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
63 assert(state->stack_size==1);
64 assert(state->stack[state->stack_size-1]== a + 21);
66 state->stack_size = 0;
67 ops[0].atom = DW_OP_const8u;
69 ops[1].atom = DW_OP_dup;
70 ops[2].atom = DW_OP_plus;
71 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
72 assert(state->stack_size==1);
73 assert(state->stack[state->stack_size-1]== a + a);
75 state->stack_size = 0;
76 ops[0].atom = DW_OP_const8u;
78 ops[1].atom = DW_OP_const8u;
80 ops[2].atom = DW_OP_over;
81 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
82 assert(state->stack_size==3);
83 assert(state->stack[state->stack_size-1]== a);
84 assert(state->stack[state->stack_size-2]== b);
85 assert(state->stack[state->stack_size-3]== a);
87 state->stack_size = 0;
88 ops[0].atom = DW_OP_const8u;
90 ops[1].atom = DW_OP_const8u;
92 ops[2].atom = DW_OP_swap;
93 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
94 assert(state->stack_size=2);
95 assert(state->stack[state->stack_size-1]== a);
96 assert(state->stack[state->stack_size-2]== b);
100 void test_deref(mc_expression_state_t state) {
104 ops[0].atom = DW_OP_const8u;
105 ops[0].number = (uintptr_t) &foo;
106 ops[1].atom = DW_OP_deref;
107 state->stack_size = 0;
109 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
110 assert(state->stack_size==1);
111 assert(state->stack[state->stack_size-1] == foo);
114 int main(int argc, char** argv) {
115 s_mc_expression_state_t state;
116 memset(&state, 0, sizeof(s_mc_expression_state_t));
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_plus, 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_or, 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_and, a, b) == (a & b));
138 for(int i=0; i!=100; ++i) {
139 uintptr_t a = rand();
140 uintptr_t b = rand();
141 assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));