Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fbe890e8aefbc936a604f508c72fcdac5ddd419f
[simgrid.git] / teshsuite / mc / dwarf_expression / dwarf_expression.cpp
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifdef NDEBUG
8 #undef NDEBUG
9 #endif
10
11 #include <string.h>
12 #include <assert.h>
13 #include <stdlib.h>
14
15 #include "src/mc/mc_private.h"
16 #include "src/mc/mc_object_info.h"
17
18 #include "src/mc/Process.hpp"
19 #include "src/mc/Type.hpp"
20 #include "src/mc/ObjectInformation.hpp"
21 #include "src/mc/Variable.hpp"
22
23 static simgrid::mc::Process* process;
24
25 static
26 uintptr_t eval_binary_operation(
27   simgrid::dwarf::ExpressionContext& state, int op, uintptr_t a, uintptr_t b) {
28
29   Dwarf_Op ops[15];
30   ops[0].atom = DW_OP_const8u;
31   ops[0].number = a;
32   ops[1].atom = DW_OP_const8u;
33   ops[1].number = b;
34   ops[2].atom = op;
35
36   simgrid::dwarf::ExpressionStack stack;
37
38   try {
39     simgrid::dwarf::execute(ops, 3, state, stack);
40   }
41   catch(std::runtime_error& e) {
42     fprintf(stderr,"Expression evaluation error");
43   }
44
45   assert(stack.size() == 1);
46   return stack.top();
47 }
48
49 static
50 void basic_test(simgrid::dwarf::ExpressionContext const& state) {
51   try {
52
53   Dwarf_Op ops[60];
54
55   uintptr_t a = rand();
56   uintptr_t b = rand();
57
58   simgrid::dwarf::ExpressionStack stack;
59
60   try {
61     ops[0].atom = DW_OP_drop;
62     simgrid::dwarf::execute(ops, 1, state, stack);
63     fprintf(stderr,"Exception expected");
64   }
65   catch(simgrid::dwarf::evaluation_error& e) {}
66
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);
71
72   ops[0].atom = DW_OP_const8u;
73   ops[0].number = a;
74   simgrid::dwarf::execute(ops, 1, state, stack);
75   assert(stack.size() == 2);
76   assert(stack.top() == a);
77
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());
82
83   stack.clear();
84   ops[0].atom = DW_OP_lit21;
85   ops[1].atom = DW_OP_plus_uconst;
86   ops[1].number = a;
87   simgrid::dwarf::execute(ops, 2, state, stack);
88   assert(stack.size() == 1);
89   assert(stack.top() == a + 21);
90
91   stack.clear();
92   ops[0].atom = DW_OP_const8u;
93   ops[0].number = a;
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);
99
100   stack.clear();
101   ops[0].atom = DW_OP_const8u;
102   ops[0].number = a;
103   ops[1].atom = DW_OP_const8u;
104   ops[1].number = b;
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);
111
112   stack.clear();
113   ops[0].atom = DW_OP_const8u;
114   ops[0].number = a;
115   ops[1].atom = DW_OP_const8u;
116   ops[1].number = b;
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);
122
123   }
124   catch(std::runtime_error& e) {
125     fprintf(stderr,"Expression evaluation error");
126   }
127 }
128
129 static
130 void test_deref(simgrid::dwarf::ExpressionContext const& state) {
131   try {
132
133   uintptr_t foo = 42;
134
135   Dwarf_Op ops[60];
136   ops[0].atom = DW_OP_const8u;
137   ops[0].number = (uintptr_t) &foo;
138   ops[1].atom = DW_OP_deref;
139
140   simgrid::dwarf::ExpressionStack stack;
141
142   simgrid::dwarf::execute(ops, 2, state, stack);
143   assert(stack.size() == 1);
144   assert(stack.top()  == foo);
145
146   }
147   catch(std::runtime_error& e) {
148     fprintf(stderr,"Expression evaluation error");
149   }
150 }
151
152 int main(int argc, char** argv) {
153   process = new simgrid::mc::Process(getpid(), -1);
154   process->init();
155
156   simgrid::dwarf::ExpressionContext state;
157   state.address_space = (simgrid::mc::AddressSpace*) process;
158
159   basic_test(state);
160
161   for(int i=0; i!=100; ++i) {
162     uintptr_t a = rand();
163     uintptr_t b = rand();
164     assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
165   }
166
167   for(int i=0; i!=100; ++i) {
168     uintptr_t a = rand();
169     uintptr_t b = rand();
170     assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b));
171   }
172
173   for(int i=0; i!=100; ++i) {
174     uintptr_t a = rand();
175     uintptr_t b = rand();
176     assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b));
177   }
178
179   for(int i=0; i!=100; ++i) {
180     uintptr_t a = rand();
181     uintptr_t b = rand();
182     assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));
183   }
184
185   test_deref(state);
186
187   return 0;
188 }