1 /* Copyright (c) 2014. 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"
16 #include "../src/mc/mc_object_info.h"
19 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
20 state->stack_size = 0;
23 ops[0].atom = DW_OP_const8u;
25 ops[1].atom = DW_OP_const8u;
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];
35 void basic_test(mc_expression_state_t state) {
41 ops[0].atom = DW_OP_drop;
42 assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
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);
49 ops[0].atom = DW_OP_const8u;
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);
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);
60 ops[0].atom = DW_OP_lit21;
61 ops[1].atom = DW_OP_plus_uconst;
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);
67 state->stack_size = 0;
68 ops[0].atom = DW_OP_const8u;
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);
76 state->stack_size = 0;
77 ops[0].atom = DW_OP_const8u;
79 ops[1].atom = DW_OP_const8u;
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);
88 state->stack_size = 0;
89 ops[0].atom = DW_OP_const8u;
91 ops[1].atom = DW_OP_const8u;
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);
101 void test_deref(mc_expression_state_t state) {
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;
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);
115 int main(int argc, char** argv) {
116 s_mc_expression_state_t state;
117 memset(&state, 0, sizeof(s_mc_expression_state_t));
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));
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));
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));
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));