/
RecordLayoutComputer.java
235 lines (209 loc) · 9.21 KB
/
RecordLayoutComputer.java
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
package org.openjdk.jextract.impl;
import java.lang.foreign.AddressLayout;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.MemoryLayout;
import org.openjdk.jextract.Declaration;
import org.openjdk.jextract.clang.Cursor;
import org.openjdk.jextract.clang.CursorKind;
import org.openjdk.jextract.clang.Type;
import org.openjdk.jextract.clang.TypeKind;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.ValueLayout;
import java.util.ArrayList;
import java.util.List;
/**
* Base class for C struct, union MemoryLayout computer helper classes.
*/
abstract class RecordLayoutComputer {
// enclosing struct type (or this struct type for top level structs)
final Type parent;
// this struct type
final Type type;
// cursor of this struct
final Cursor cursor;
final List<Declaration> fieldDecls;
final List<MemoryLayout> fieldLayouts;
final TypeMaker typeMaker;
private int anonCount = 0;
RecordLayoutComputer(TypeMaker typeMaker, Type parent, Type type) {
this.parent = parent;
this.type = type;
this.cursor = type.getDeclarationCursor().getDefinition();
this.fieldDecls = new ArrayList<>();
this.fieldLayouts = new ArrayList<>();
this.typeMaker = typeMaker;
}
static org.openjdk.jextract.Type compute(TypeMaker typeMaker, long offsetInParent, Type parent, Type type) {
return computeInternal(typeMaker, offsetInParent, parent, type, null);
}
private static org.openjdk.jextract.Type computeAnonymous(TypeMaker typeMaker, long offsetInParent, Type parent, Type type, String name) {
return computeInternal(typeMaker, offsetInParent, parent, type, name);
}
static final org.openjdk.jextract.Type.Declared ERRONEOUS = org.openjdk.jextract.Type.declared(
Declaration.struct(TreeMaker.CursorPosition.NO_POSITION, "", MemoryLayout.paddingLayout(8)));
private static org.openjdk.jextract.Type computeInternal(TypeMaker typeMaker, long offsetInParent, Type parent, Type type, String name) {
Cursor cursor = type.getDeclarationCursor().getDefinition();
if (cursor.isInvalid()) {
return ERRONEOUS;
}
final boolean isUnion = cursor.kind() == CursorKind.UnionDecl;
return isUnion? new UnionLayoutComputer(typeMaker, offsetInParent, parent, type).compute(name) :
new StructLayoutComputer(typeMaker, offsetInParent, parent, type).compute(name);
}
final org.openjdk.jextract.Type.Declared compute(String anonName) {
cursor.forEach(fc -> {
if (Utils.isFlattenable(fc)) {
/*
* Ignore bitfields of zero width.
*
* struct Foo {
* int i:0;
* }
*
* And bitfields without a name.
* (padding is computed automatically)
*/
if (fc.isBitField() && (fc.getBitFieldWidth() == 0 || fc.spelling().isEmpty())) {
startBitfield();
} else {
processField(fc);
}
}
});
Declaration.Scoped declaration = finishRecord(anonName);
if (cursor.isAnonymousStruct()) {
// record this with a declaration attribute, so we don't have to rely on the cursor again later
declaration = (Declaration.Scoped)declaration.withAttribute("ANONYMOUS", true);
}
return org.openjdk.jextract.Type.declared(declaration);
}
abstract void startBitfield();
abstract void processField(Cursor c);
abstract Declaration.Scoped finishRecord(String anonName);
void addField(long offset, Declaration declaration) {
fieldDecls.add(declaration);
MemoryLayout layout = null;
if (declaration instanceof Declaration.Scoped scoped) {
layout = scoped.layout().orElse(null);
} else if (declaration instanceof Declaration.Variable var) {
layout = org.openjdk.jextract.Type.layoutFor(var.type()).orElse(null);
}
if (layout != null) {
fieldLayouts.add(declaration.name().isEmpty() ? layout : layout.withName(declaration.name()));
}
}
void addPadding(long bits) {
fieldLayouts.add(MemoryLayout.paddingLayout(bits / 8));
}
void addField(long offset, Type parent, Cursor c) {
if (c.isAnonymousStruct()) {
addField(offset, ((org.openjdk.jextract.Type.Declared)computeAnonymous(typeMaker, offset, parent, c.type(), nextAnonymousName())).tree());
} else {
addField(offset, field(offset, c));
}
}
private String nextAnonymousName() {
return "$anon$" + anonCount++;
}
Declaration field(long offset, Cursor c) {
org.openjdk.jextract.Type type = typeMaker.makeType(c.type());
String name = c.spelling();
if (c.isBitField()) {
return Declaration.bitfield(TreeMaker.CursorPosition.of(c), name, type, offset, c.getBitFieldWidth());
} else if (c.isAnonymousStruct() && type instanceof org.openjdk.jextract.Type.Declared decl) {
return decl.tree();
} else {
return Declaration.field(TreeMaker.CursorPosition.of(c), name, type);
}
}
long fieldSize(Cursor c) {
if (c.type().kind() == TypeKind.IncompleteArray) {
return 0;
}
return c.isBitField() ? c.getBitFieldWidth() : c.type().size() * 8;
}
Declaration.Scoped bitfield(Declaration.Variable... declarations) {
return Declaration.bitfields(declarations[0].pos(), declarations);
}
long offsetOf(Type parent, Cursor c) {
if (c.kind() == CursorKind.FieldDecl) {
return parent.getOffsetOf(c.spelling());
} else {
List<Long> offsets = new ArrayList<>();
c.forEach(child -> {
if (Utils.isFlattenable(child)) {
offsets.add(offsetOf(parent, child));
}
});
return offsets.stream().findFirst()
.orElseThrow(() -> new IllegalStateException(
"Can not find offset of: " + c + ", in: " + parent));
}
}
void checkSize(GroupLayout layout) {
// sanity check
if (cursor.type().size() != layout.byteSize()) {
throw new AssertionError(
String.format("Unexpected size for layout %s. Found %d ; expected %d",
layout, layout.byteSize(), cursor.type().size()));
}
}
MemoryLayout[] alignFields() {
long align = cursor.type().align();
return fieldLayouts.stream()
.map(l -> forceAlign(l, align))
.toArray(MemoryLayout[]::new);
}
private static MemoryLayout forceAlign(MemoryLayout layout, long align) {
if (align >= layout.byteAlignment()) {
return layout; // fast-path
}
MemoryLayout res = switch (layout) {
case GroupLayout groupLayout -> {
MemoryLayout[] newMembers = groupLayout.memberLayouts()
.stream().map(l -> forceAlign(l, align)).toArray(MemoryLayout[]::new);
yield groupLayout instanceof StructLayout ?
MemoryLayout.structLayout(newMembers) :
MemoryLayout.unionLayout(newMembers);
}
case SequenceLayout sequenceLayout ->
MemoryLayout.sequenceLayout(sequenceLayout.elementCount(),
forceAlign(sequenceLayout.elementLayout(), align));
default -> layout.withByteAlignment(align);
};
// copy name and target layout, if present
if (layout.name().isPresent()) {
res = res.withName(layout.name().get());
}
if (layout instanceof AddressLayout addressLayout && addressLayout.targetLayout().isPresent()) {
((AddressLayout)res).withTargetLayout(addressLayout.targetLayout().get());
}
return res;
}
}