Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
32798ad382052a7f7e3549175edbd42f978062e6
[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 "mc/mc_process.h"
16 #include "mc/mc_private.h"
17 #include "mc/mc_object_info.h"
18
19 #include "mc/Type.hpp"
20 #include "mc/ObjectInformation.hpp"
21 #include "mc/Variable.hpp"
22
23 static simgrid::mc::Process* process;
24
25 static
26 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
27   state->stack_size = 0;
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   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
37   assert(state->stack_size==1);
38   return state->stack[state->stack_size - 1];
39 }
40
41 static
42 void basic_test(mc_expression_state_t state) {
43   Dwarf_Op ops[60];
44
45   uintptr_t a = rand();
46   uintptr_t b = rand();
47
48   ops[0].atom = DW_OP_drop;
49   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
50
51   ops[0].atom = DW_OP_lit21;
52   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
53   assert(state->stack_size==1);
54   assert(state->stack[state->stack_size-1]==21);
55
56   ops[0].atom = DW_OP_const8u;
57   ops[0].number = a;
58   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
59   assert(state->stack_size==2);
60   assert(state->stack[state->stack_size-1] == a);
61
62   ops[0].atom = DW_OP_drop;
63   ops[1].atom = DW_OP_drop;
64   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
65   assert(state->stack_size==0);
66
67   ops[0].atom = DW_OP_lit21;
68   ops[1].atom = DW_OP_plus_uconst;
69   ops[1].number = a;
70   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
71   assert(state->stack_size==1);
72   assert(state->stack[state->stack_size-1]== a + 21);
73
74   state->stack_size = 0;
75   ops[0].atom = DW_OP_const8u;
76   ops[0].number = a;
77   ops[1].atom = DW_OP_dup;
78   ops[2].atom = DW_OP_plus;
79   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
80   assert(state->stack_size==1);
81   assert(state->stack[state->stack_size-1]== a + a);
82
83   state->stack_size = 0;
84   ops[0].atom = DW_OP_const8u;
85   ops[0].number = a;
86   ops[1].atom = DW_OP_const8u;
87   ops[1].number = b;
88   ops[2].atom = DW_OP_over;
89   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
90   assert(state->stack_size==3);
91   assert(state->stack[state->stack_size-1]== a);
92   assert(state->stack[state->stack_size-2]== b);
93   assert(state->stack[state->stack_size-3]== a);
94
95   state->stack_size = 0;
96   ops[0].atom = DW_OP_const8u;
97   ops[0].number = a;
98   ops[1].atom = DW_OP_const8u;
99   ops[1].number = b;
100   ops[2].atom = DW_OP_swap;
101   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
102   assert(state->stack_size=2);
103   assert(state->stack[state->stack_size-1]== a);
104   assert(state->stack[state->stack_size-2]== b);
105 }
106
107 static
108 void test_deref(mc_expression_state_t state) {
109   uintptr_t foo = 42;
110
111   Dwarf_Op ops[60];
112   ops[0].atom = DW_OP_const8u;
113   ops[0].number = (uintptr_t) &foo;
114   ops[1].atom = DW_OP_deref;
115   state->stack_size = 0;
116
117   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
118   assert(state->stack_size==1);
119   assert(state->stack[state->stack_size-1] == foo);
120 }
121
122 int main(int argc, char** argv) {
123   process = new simgrid::mc::Process(getpid(), -1);
124
125   s_mc_expression_state_t state;
126   memset(&state, 0, sizeof(s_mc_expression_state_t));
127   state.address_space = (simgrid::mc::AddressSpace*) process;
128
129   basic_test(&state);
130
131   for(int i=0; i!=100; ++i) {
132     uintptr_t a = rand();
133     uintptr_t b = rand();
134     assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
135   }
136
137   for(int i=0; i!=100; ++i) {
138     uintptr_t a = rand();
139     uintptr_t b = rand();
140     assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
141   }
142
143   for(int i=0; i!=100; ++i) {
144     uintptr_t a = rand();
145     uintptr_t b = rand();
146     assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
147   }
148
149   for(int i=0; i!=100; ++i) {
150     uintptr_t a = rand();
151     uintptr_t b = rand();
152     assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));
153   }
154
155   test_deref(&state);
156
157   return 0;
158 }