Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
998b779aa1f31395eab39a9d26b6bce4e9be2dbc
[simgrid.git] / teshsuite / mc / dwarf_expression / dwarf_expression.c
1 /* Copyright (c) 2014. 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 static
19 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
20   state->stack_size = 0;
21
22   Dwarf_Op ops[15];
23   ops[0].atom = DW_OP_const8u;
24   ops[0].number = a;
25   ops[1].atom = DW_OP_const8u;
26   ops[1].number = b;
27   ops[2].atom = op;
28
29   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
30   assert(state->stack_size==1);
31   return state->stack[state->stack_size - 1];
32 }
33
34 static
35 void basic_test(mc_expression_state_t state) {
36   Dwarf_Op ops[60];
37
38   uintptr_t a = rand();
39   uintptr_t b = rand();
40
41   ops[0].atom = DW_OP_drop;
42   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
43
44   ops[0].atom = DW_OP_lit21;
45   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
46   assert(state->stack_size==1);
47   assert(state->stack[state->stack_size-1]==21);
48
49   ops[0].atom = DW_OP_const8u;
50   ops[0].number = a;
51   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
52   assert(state->stack_size==2);
53   assert(state->stack[state->stack_size-1] == a);
54
55   ops[0].atom = DW_OP_drop;
56   ops[1].atom = DW_OP_drop;
57   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
58   assert(state->stack_size==0);
59
60   ops[0].atom = DW_OP_lit21;
61   ops[1].atom = DW_OP_plus_uconst;
62   ops[1].number = a;
63   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
64   assert(state->stack_size==1);
65   assert(state->stack[state->stack_size-1]== a + 21);
66
67   state->stack_size = 0;
68   ops[0].atom = DW_OP_const8u;
69   ops[0].number = a;
70   ops[1].atom = DW_OP_dup;
71   ops[2].atom = DW_OP_plus;
72   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
73   assert(state->stack_size==1);
74   assert(state->stack[state->stack_size-1]== a + a);
75
76   state->stack_size = 0;
77   ops[0].atom = DW_OP_const8u;
78   ops[0].number = a;
79   ops[1].atom = DW_OP_const8u;
80   ops[1].number = b;
81   ops[2].atom = DW_OP_over;
82   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
83   assert(state->stack_size==3);
84   assert(state->stack[state->stack_size-1]== a);
85   assert(state->stack[state->stack_size-2]== b);
86   assert(state->stack[state->stack_size-3]== a);
87
88   state->stack_size = 0;
89   ops[0].atom = DW_OP_const8u;
90   ops[0].number = a;
91   ops[1].atom = DW_OP_const8u;
92   ops[1].number = b;
93   ops[2].atom = DW_OP_swap;
94   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
95   assert(state->stack_size=2);
96   assert(state->stack[state->stack_size-1]== a);
97   assert(state->stack[state->stack_size-2]== b);
98 }
99
100 static
101 void test_deref(mc_expression_state_t state) {
102   uintptr_t foo = 42;
103
104   Dwarf_Op ops[60];
105   ops[0].atom = DW_OP_const8u;
106   ops[0].number = (uintptr_t) &foo;
107   ops[1].atom = DW_OP_deref;
108   state->stack_size = 0;
109
110   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
111   assert(state->stack_size==1);
112   assert(state->stack[state->stack_size-1] == foo);
113 }
114
115 int main(int argc, char** argv) {
116   s_mc_expression_state_t state;
117   memset(&state, 0, sizeof(s_mc_expression_state_t));
118
119   basic_test(&state);
120
121   for(int i=0; i!=100; ++i) {
122     uintptr_t a = rand();
123     uintptr_t b = rand();
124     assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
125   }
126
127   for(int i=0; i!=100; ++i) {
128     uintptr_t a = rand();
129     uintptr_t b = rand();
130     assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
131   }
132
133   for(int i=0; i!=100; ++i) {
134     uintptr_t a = rand();
135     uintptr_t b = rand();
136     assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
137   }
138
139   for(int i=0; i!=100; ++i) {
140     uintptr_t a = rand();
141     uintptr_t b = rand();
142     assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));
143   }
144
145   test_deref(&state);
146
147   return 0;
148 }