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"
16 #include "../src/mc/mc_object_info.h"
18 static mc_process_t process;
21 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
22 state->stack_size = 0;
25 ops[0].atom = DW_OP_const8u;
27 ops[1].atom = DW_OP_const8u;
31 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
32 assert(state->stack_size==1);
33 return state->stack[state->stack_size - 1];
37 void basic_test(mc_expression_state_t state) {
43 ops[0].atom = DW_OP_drop;
44 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
46 ops[0].atom = DW_OP_lit21;
47 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
48 assert(state->stack_size==1);
49 assert(state->stack[state->stack_size-1]==21);
51 ops[0].atom = DW_OP_const8u;
53 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
54 assert(state->stack_size==2);
55 assert(state->stack[state->stack_size-1] == a);
57 ops[0].atom = DW_OP_drop;
58 ops[1].atom = DW_OP_drop;
59 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
60 assert(state->stack_size==0);
62 ops[0].atom = DW_OP_lit21;
63 ops[1].atom = DW_OP_plus_uconst;
65 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
66 assert(state->stack_size==1);
67 assert(state->stack[state->stack_size-1]== a + 21);
69 state->stack_size = 0;
70 ops[0].atom = DW_OP_const8u;
72 ops[1].atom = DW_OP_dup;
73 ops[2].atom = DW_OP_plus;
74 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
75 assert(state->stack_size==1);
76 assert(state->stack[state->stack_size-1]== a + a);
78 state->stack_size = 0;
79 ops[0].atom = DW_OP_const8u;
81 ops[1].atom = DW_OP_const8u;
83 ops[2].atom = DW_OP_over;
84 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
85 assert(state->stack_size==3);
86 assert(state->stack[state->stack_size-1]== a);
87 assert(state->stack[state->stack_size-2]== b);
88 assert(state->stack[state->stack_size-3]== a);
90 state->stack_size = 0;
91 ops[0].atom = DW_OP_const8u;
93 ops[1].atom = DW_OP_const8u;
95 ops[2].atom = DW_OP_swap;
96 assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
97 assert(state->stack_size=2);
98 assert(state->stack[state->stack_size-1]== a);
99 assert(state->stack[state->stack_size-2]== b);
103 void test_deref(mc_expression_state_t state) {
107 ops[0].atom = DW_OP_const8u;
108 ops[0].number = (uintptr_t) &foo;
109 ops[1].atom = DW_OP_deref;
110 state->stack_size = 0;
112 assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
113 assert(state->stack_size==1);
114 assert(state->stack[state->stack_size-1] == foo);
117 int main(int argc, char** argv) {
118 process = new simgrid::mc::Process(getpid(), -1);
120 s_mc_expression_state_t state;
121 memset(&state, 0, sizeof(s_mc_expression_state_t));
122 state.address_space = (mc_address_space_t) process;
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_plus, 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_or, 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_and, a, b) == (a & b));
144 for(int i=0; i!=100; ++i) {
145 uintptr_t a = rand();
146 uintptr_t b = rand();
147 assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));