1 /* Copyright (c) 2014-2015. 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"
17 #include "src/mc/Process.hpp"
18 #include "src/mc/Type.hpp"
19 #include "src/mc/ObjectInformation.hpp"
20 #include "src/mc/Variable.hpp"
22 static simgrid::mc::Process* process;
25 uintptr_t eval_binary_operation(
26 simgrid::dwarf::ExpressionContext& state, int op, uintptr_t a, uintptr_t b) {
29 ops[0].atom = DW_OP_const8u;
31 ops[1].atom = DW_OP_const8u;
35 simgrid::dwarf::ExpressionStack stack;
38 simgrid::dwarf::execute(ops, 3, state, stack);
40 catch(std::runtime_error& e) {
41 fprintf(stderr,"Expression evaluation error");
44 assert(stack.size() == 1);
49 void basic_test(simgrid::dwarf::ExpressionContext const& state) {
57 simgrid::dwarf::ExpressionStack stack;
60 ops[0].atom = DW_OP_drop;
61 simgrid::dwarf::execute(ops, 1, state, stack);
62 fprintf(stderr,"Exception expected");
64 catch(simgrid::dwarf::evaluation_error& e) {}
66 ops[0].atom = DW_OP_lit21;
67 simgrid::dwarf::execute(ops, 1, state, stack);
68 assert(stack.size() == 1);
69 assert(stack.top() == 21);
71 ops[0].atom = DW_OP_const8u;
73 simgrid::dwarf::execute(ops, 1, state, stack);
74 assert(stack.size() == 2);
75 assert(stack.top() == a);
77 ops[0].atom = DW_OP_drop;
78 ops[1].atom = DW_OP_drop;
79 simgrid::dwarf::execute(ops, 2, state, stack);
80 assert(stack.empty());
83 ops[0].atom = DW_OP_lit21;
84 ops[1].atom = DW_OP_plus_uconst;
86 simgrid::dwarf::execute(ops, 2, state, stack);
87 assert(stack.size() == 1);
88 assert(stack.top() == a + 21);
91 ops[0].atom = DW_OP_const8u;
93 ops[1].atom = DW_OP_dup;
94 ops[2].atom = DW_OP_plus;
95 simgrid::dwarf::execute(ops, 3, state, stack);
96 assert(stack.size() == 1);
97 assert(stack.top() == a + a);
100 ops[0].atom = DW_OP_const8u;
102 ops[1].atom = DW_OP_const8u;
104 ops[2].atom = DW_OP_over;
105 simgrid::dwarf::execute(ops, 3, state, stack);
106 assert(stack.size() == 3);
107 assert(stack.top() == a);
108 assert(stack.top(1) == b);
109 assert(stack.top(2) == a);
112 ops[0].atom = DW_OP_const8u;
114 ops[1].atom = DW_OP_const8u;
116 ops[2].atom = DW_OP_swap;
117 simgrid::dwarf::execute(ops, 3, state, stack);
118 assert(stack.size() == 2);
119 assert(stack.top() == a);
120 assert(stack.top(1) == b);
123 catch(std::runtime_error& e) {
124 fprintf(stderr,"Expression evaluation error");
129 void test_deref(simgrid::dwarf::ExpressionContext const& state) {
135 ops[0].atom = DW_OP_const8u;
136 ops[0].number = (uintptr_t) &foo;
137 ops[1].atom = DW_OP_deref;
139 simgrid::dwarf::ExpressionStack stack;
141 simgrid::dwarf::execute(ops, 2, state, stack);
142 assert(stack.size() == 1);
143 assert(stack.top() == foo);
146 catch(std::runtime_error& e) {
147 fprintf(stderr,"Expression evaluation error");
151 int main(int argc, char** argv) {
152 process = new simgrid::mc::Process(getpid(), -1);
155 simgrid::dwarf::ExpressionContext state;
156 state.address_space = (simgrid::mc::AddressSpace*) process;
160 for(int i=0; i!=100; ++i) {
161 uintptr_t a = rand();
162 uintptr_t b = rand();
163 assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
166 for(int i=0; i!=100; ++i) {
167 uintptr_t a = rand();
168 uintptr_t b = rand();
169 assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b));
172 for(int i=0; i!=100; ++i) {
173 uintptr_t a = rand();
174 uintptr_t b = rand();
175 assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b));
178 for(int i=0; i!=100; ++i) {
179 uintptr_t a = rand();
180 uintptr_t b = rand();
181 assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));