-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.c
165 lines (135 loc) · 3.83 KB
/
frame.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "frame.h"
void init_frame(Frame *f, void *jvm, uint32_t n_locals, uint32_t n_operands, cp_info *cp,
int32_t class_index, int32_t method_index) {
f->jvm = jvm;
f->n_locals = n_locals;
f->locals = calloc(sizeof(int64_t), n_locals);
assert(f->locals);
f->n_operands = n_operands;
f->operands = calloc(sizeof(int64_t), n_operands);
assert(f->operands);
f->i = 0;
f->cp = cp;
f->class_index = class_index;
f->method_index = method_index;
}
void deinit_frame(Frame *f) {
free(f->locals);
free(f->operands);
}
void push_stack(Frame *f, uint64_t operand) {
assert(f->i < f->n_operands);
f->operands[f->i++] = operand;
}
uint64_t pop_stack(Frame *f) {
assert(f->i > 0);
return f->operands[--f->i];
}
uint64_t peek_stack(Frame *f) {
return f->operands[f->i];
}
void push_stack_double(Frame *f, double d) {
uint64_t hex = *((uint64_t *) (& d));
uint32_t hi = hex >> 32;
uint32_t lo = hex;
push_stack(f, lo);
push_stack(f, hi);
}
double pop_stack_double(Frame *f) {
uint64_t hi = pop_stack(f);
uint64_t lo = pop_stack(f);
uint64_t hex = (hi << 32) | lo;
return *((double *)(& hex));
}
void push_stack_long(Frame *f, int64_t x) {
uint64_t hex = *((uint64_t *) (& x));
uint32_t hi = hex >> 32;
uint32_t lo = hex;
push_stack(f, lo);
push_stack(f, hi);
}
int64_t pop_stack_long(Frame *f) {
uint64_t hi = pop_stack(f);
uint64_t lo = pop_stack(f);
uint64_t hex = (hi << 32) | lo;
return *((int64_t *)(& hex));
}
void push_stack_float(Frame *f, float x) {
uint32_t hex = *((uint32_t *) (& x));
push_stack(f, hex);
}
float pop_stack_float(Frame *f) {
uint32_t hex = pop_stack(f);
return *((float *) (& hex));
}
void push_stack_int(Frame *f, int32_t x) {
uint64_t push = *((uint64_t *) (&x));
push_stack(f, push);
}
int32_t pop_stack_int(Frame *f) {
uint64_t pop = pop_stack(f);
int32_t int_value = *((int32_t *) (&pop));
return int_value;
}
void push_stack_pointer(Frame *f, void *ptr) {
push_stack(f, (uint64_t) ptr);
}
void *pop_stack_pointer(Frame *f) {
uint64_t pop = pop_stack(f);
return (void *) pop;
}
void push_stack_byte(Frame *f, int8_t x) {
uint64_t push = *((uint64_t *) (&x));
push_stack(f, push);
}
int8_t pop_stack_byte(Frame *f) {
uint64_t pop = pop_stack(f);
return *((int8_t *) &pop);
}
void frame_set_local(Frame *f, uint32_t index, uint64_t value) {
assert(index < f->n_locals);
f->locals[index] = value;
}
uint64_t frame_get_local(Frame *f, uint32_t index) {
assert(index < f->n_locals);
return f->locals[index];
}
void frame_set_local_int(Frame *f, uint32_t index, int32_t value) {
uint64_t x = *((uint64_t *) (& value));
frame_set_local(f, index, x);
}
int32_t frame_get_local_int(Frame *f, uint32_t index) {
uint64_t x = frame_get_local(f, index);
return *((int32_t *) (&x));
}
void frame_set_local_double(Frame *f, uint32_t index, double value) {
uint64_t x = *((uint64_t *) (& value));
frame_set_local(f, index, x);
}
double frame_get_local_double(Frame *f, uint32_t index) {
uint64_t x = frame_get_local(f, index);
return *((double *) (&x));
}
void frame_set_local_float(Frame *f, uint32_t index, float value) {
uint64_t x = *((uint64_t *) (& value));
frame_set_local(f, index, x);
}
float frame_get_local_float(Frame *f, uint32_t index) {
uint64_t x = frame_get_local(f, index);
return *((float *) (&x));
}
void frame_set_local_long(Frame *f, uint32_t index, int64_t value) {
uint64_t x = *((uint64_t *) (& value));
frame_set_local(f, index, x);
}
int64_t frame_get_local_long(Frame *f, uint32_t index) {
uint64_t x = frame_get_local(f, index);
return *((int64_t *) (&x));
}
void frame_set_local_pointer(Frame *f, uint32_t index, void *value) {
frame_set_local(f, index, ((uint64_t) value));
}
void* frame_get_local_pointer(Frame *f, uint32_t index) {
uint64_t x = frame_get_local(f, index);
return *((void **) (&x));
}