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 "mc/mc_private.h"
16 #include "mc/mc_object_info.h"
18 #include "mc/Process.hpp"
19 #include "mc/Type.hpp"
20 #include "mc/ObjectInformation.hpp"
21 #include "mc/Variable.hpp"
23 static simgrid::mc::Process* process;
26 uintptr_t eval_binary_operation(
27 simgrid::dwarf::ExpressionContext& state, int op, uintptr_t a, uintptr_t b) {
30 ops[0].atom = DW_OP_const8u;
32 ops[1].atom = DW_OP_const8u;
36 simgrid::dwarf::ExpressionStack stack;
39 simgrid::dwarf::execute(ops, 3, state, stack);
41 catch(std::runtime_error& e) {
42 fprintf(stderr,"Expression evaluation error");
45 assert(stack.size() == 1);
50 void basic_test(simgrid::dwarf::ExpressionContext const& state) {
58 simgrid::dwarf::ExpressionStack stack;
61 ops[0].atom = DW_OP_drop;
62 simgrid::dwarf::execute(ops, 1, state, stack);
63 fprintf(stderr,"Exception expected");
65 catch(simgrid::dwarf::evaluation_error& e) {}
67 ops[0].atom = DW_OP_lit21;
68 simgrid::dwarf::execute(ops, 1, state, stack);
69 assert(stack.size() == 1);
70 assert(stack.top() == 21);
72 ops[0].atom = DW_OP_const8u;
74 simgrid::dwarf::execute(ops, 1, state, stack);
75 assert(stack.size() == 2);
76 assert(stack.top() == a);
78 ops[0].atom = DW_OP_drop;
79 ops[1].atom = DW_OP_drop;
80 simgrid::dwarf::execute(ops, 2, state, stack);
81 assert(stack.empty());
84 ops[0].atom = DW_OP_lit21;
85 ops[1].atom = DW_OP_plus_uconst;
87 simgrid::dwarf::execute(ops, 2, state, stack);
88 assert(stack.size() == 1);
89 assert(stack.top() == a + 21);
92 ops[0].atom = DW_OP_const8u;
94 ops[1].atom = DW_OP_dup;
95 ops[2].atom = DW_OP_plus;
96 simgrid::dwarf::execute(ops, 3, state, stack);
97 assert(stack.size() == 1);
98 assert(stack.top() == a + a);
101 ops[0].atom = DW_OP_const8u;
103 ops[1].atom = DW_OP_const8u;
105 ops[2].atom = DW_OP_over;
106 simgrid::dwarf::execute(ops, 3, state, stack);
107 assert(stack.size() == 3);
108 assert(stack.top() == a);
109 assert(stack.top(1) == b);
110 assert(stack.top(2) == a);
113 ops[0].atom = DW_OP_const8u;
115 ops[1].atom = DW_OP_const8u;
117 ops[2].atom = DW_OP_swap;
118 simgrid::dwarf::execute(ops, 3, state, stack);
119 assert(stack.size() == 2);
120 assert(stack.top() == a);
121 assert(stack.top(1) == b);
124 catch(std::runtime_error& e) {
125 fprintf(stderr,"Expression evaluation error");
130 void test_deref(simgrid::dwarf::ExpressionContext const& state) {
136 ops[0].atom = DW_OP_const8u;
137 ops[0].number = (uintptr_t) &foo;
138 ops[1].atom = DW_OP_deref;
140 simgrid::dwarf::ExpressionStack stack;
142 simgrid::dwarf::execute(ops, 2, state, stack);
143 assert(stack.size() == 1);
144 assert(stack.top() == foo);
147 catch(std::runtime_error& e) {
148 fprintf(stderr,"Expression evaluation error");
152 int main(int argc, char** argv) {
153 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));