-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpz_data.cpp
More file actions
162 lines (140 loc) · 3.42 KB
/
pz_data.cpp
File metadata and controls
162 lines (140 loc) · 3.42 KB
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
/*
* Plasma bytecode data and types loading and runtime
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#include <stdio.h>
#include <string.h>
#include "pz_common.h"
#include "pz_data.h"
#include "pz_util.h"
/*
* Structs
*
**********/
namespace pz {
void Struct::calculate_layout()
{
#ifdef PZ_DEV
assert(!m_layout_calculated);
m_layout_calculated = true;
#endif
unsigned size = 0;
for (unsigned i = 0; i < num_fields(); i++) {
unsigned field_size = width_to_bytes(m_fields[i].width);
size = AlignUp(size, field_size);
m_fields[i].offset = size;
size += field_size;
}
m_total_size = size;
}
/*
* Data
*
**********/
void * data_new_array_data(GCCapability & gc_tracer, PZ_Width width,
uint32_t num_elements)
{
return gc_tracer.alloc_bytes(width_to_bytes(width) * num_elements);
}
void * data_new_struct_data(GCCapability & gc_tracer, size_t size)
{
// TODO: Use this during execution of PZT_ALLOC.
return gc_tracer.alloc_bytes(size);
}
/*
* Functions for storing data in memory
***************************************/
void data_write_normal_uint8(void * dest, uint8_t value)
{
*((uint8_t *)dest) = value;
}
void data_write_normal_uint16(void * dest, uint16_t value)
{
*((uint16_t *)dest) = value;
}
void data_write_normal_uint32(void * dest, uint32_t value)
{
*((uint32_t *)dest) = value;
}
void data_write_normal_uint64(void * dest, uint64_t value)
{
*((uint64_t *)dest) = value;
}
void data_write_fast_from_int32(void * dest, int32_t value)
{
*((PZ_FAST_INTEGER_TYPE *)dest) = (PZ_FAST_INTEGER_TYPE)value;
}
void data_write_wptr(void * dest, intptr_t value)
{
*((intptr_t *)dest) = value;
}
Optional<PZ_Width> width_from_int(uint8_t w)
{
switch (w) {
case PZW_8:
return PZW_8;
case PZW_16:
return PZW_16;
case PZW_32:
return PZW_32;
case PZW_64:
return PZW_64;
case PZW_FAST:
return PZW_FAST;
case PZW_PTR:
return PZW_PTR;
default:
return Optional<PZ_Width>::Nothing();
}
}
PZ_Width width_normalize(PZ_Width width)
{
switch (width) {
case PZW_FAST:
switch (PZ_FAST_INTEGER_WIDTH) {
case 32:
return PZW_32;
case 64:
return PZW_64;
default:
fprintf(stderr,
"PZ_FAST_INTEGER_WIDTH has unanticipated value\n");
abort();
}
break;
case PZW_PTR:
switch (sizeof(intptr_t)) {
case 4:
return PZW_32;
case 8:
return PZW_64;
default:
fprintf(stderr, "Unknown pointer width\n");
abort();
}
break;
default:
return width;
}
}
unsigned width_to_bytes(PZ_Width width)
{
width = width_normalize(width);
switch (width) {
case PZW_8:
return 1;
case PZW_16:
return 2;
case PZW_32:
return 4;
case PZW_64:
return 8;
default:
fprintf(stderr, "Width should have been normalized");
abort();
}
}
} // namespace pz