Skip to content

Commit b031686

Browse files
ctf: add double float support for ctf2
made in part by claude sonnet 4.5. Many bad hallucinations needed to be manually fixed. Corrected by CodeRabbit (GPT5) Change-Id: Ibee4b0cf144f2afa922ae968fd17d6065e18b257 Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
1 parent 01b90fa commit b031686

2 files changed

Lines changed: 101 additions & 43 deletions

File tree

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/tsdl/floatingpoint/FloatDeclarationParser.java

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222
import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
2323
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
2424
import org.eclipse.tracecompass.ctf.parser.CTFParser;
25+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.CTFJsonMetadataNode;
2526
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser;
27+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
2628
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
2729
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
2830
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.AlignmentParser;
2931
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.ByteOrderParser;
3032
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.UnaryIntegerParser;
3133
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
34+
import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings;
35+
36+
import com.google.gson.JsonElement;
37+
import com.google.gson.JsonObject;
3238

3339
/**
3440
*
@@ -114,49 +120,84 @@ public FloatDeclaration parse(ICTFMetadataNode floatingPoint, ICommonTreeParserP
114120
throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
115121
}
116122
CTFTrace trace = ((Param) param).fTrace;
117-
List<ICTFMetadataNode> children = floatingPoint.getChildren();
118-
119-
/*
120-
* If the integer has no attributes, then it is missing the size
121-
* attribute which is required
122-
*/
123-
if (children == null) {
124-
throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE);
125-
}
126123

127124
/* The return value */
128125
ByteOrder byteOrder = trace.getByteOrder();
129126
long alignment = 0;
130-
131127
int exponent = DEFAULT_FLOAT_EXPONENT;
132128
int mantissa = DEFAULT_FLOAT_MANTISSA;
133129

134-
/* Iterate on all integer children */
135-
for (ICTFMetadataNode child : children) {
136-
String type = child.getType();
137-
if (CTFParser.tokenNames[CTFParser.CTF_EXPRESSION_VAL].equals(type)) {
138-
ICTFMetadataNode leftNode = child.getChild(0);
139-
ICTFMetadataNode rightNode = child.getChild(1);
140-
List<ICTFMetadataNode> leftStrings = leftNode.getChildren();
141-
if (!isAnyUnaryString(leftStrings.get(0))) {
142-
throw new ParseException(IDENTIFIER_MUST_BE_A_STRING);
130+
if (floatingPoint instanceof JsonStructureFieldMemberMetadataNode member) {
131+
JsonElement fieldClassElement = member.getFieldClass();
132+
if (fieldClassElement == null || !fieldClassElement.isJsonObject()) {
133+
throw new ParseException(getClass().getName() + " fieldclass must be a json object."); //$NON-NLS-1$
134+
}
135+
JsonObject fieldclass = member.getFieldClass().getAsJsonObject();
136+
137+
if (fieldclass.has(JsonMetadataStrings.LENGTH)) {
138+
int size = fieldclass.get(JsonMetadataStrings.LENGTH).getAsInt();
139+
// Standard IEEE 754 sizes
140+
if (size == 32) {
141+
exponent = 8;
142+
mantissa = 24;
143+
} else if (size == 64) {
144+
exponent = 11;
145+
mantissa = 53;
146+
} else if (size == 16 || (size - 128) % 64 == 0) {
147+
// TODO: make it work with 16, 128, and 128 + N*64 sizes.
148+
throw new ParseException("Unsupported valid floating point size: " + size); //$NON-NLS-1$
149+
} else {
150+
throw new ParseException("Unsupported floating point size: " + size); //$NON-NLS-1$
143151
}
144-
String left = concatenateUnaryStrings(leftStrings);
145-
if (left.equals(MetadataStrings.EXP_DIG)) {
146-
exponent = UnaryIntegerParser.INSTANCE.parse(rightNode.getChild(0), null).intValue();
147-
} else if (left.equals(MetadataStrings.BYTE_ORDER)) {
148-
byteOrder = ByteOrderParser.INSTANCE.parse(rightNode, new ByteOrderParser.Param(trace));
149-
} else if (left.equals(MetadataStrings.MANT_DIG)) {
150-
mantissa = UnaryIntegerParser.INSTANCE.parse(rightNode.getChild(0), null).intValue();
151-
} else if (left.equals(MetadataStrings.ALIGN)) {
152-
alignment = AlignmentParser.INSTANCE.parse(rightNode, null);
152+
}
153+
154+
if (fieldclass.has(JsonMetadataStrings.BYTE_ORDER)) {
155+
CTFJsonMetadataNode bo = new CTFJsonMetadataNode(floatingPoint, CTFParser.tokenNames[CTFParser.UNARY_EXPRESSION_STRING], fieldclass.get(JsonMetadataStrings.BYTE_ORDER).getAsString());
156+
byteOrder = ByteOrderParser.INSTANCE.parse(bo, new ByteOrderParser.Param(trace));
157+
}
158+
159+
if (fieldclass.has(JsonMetadataStrings.ALIGNMENT)) {
160+
alignment = AlignmentParser.INSTANCE.parse(floatingPoint, null);
161+
}
162+
} else {
163+
List<ICTFMetadataNode> children = floatingPoint.getChildren();
164+
165+
/*
166+
* If the integer has no attributes, then it is missing the size
167+
* attribute which is required
168+
*/
169+
if (children == null) {
170+
throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE);
171+
}
172+
173+
/* Iterate on all integer children */
174+
for (ICTFMetadataNode child : children) {
175+
String type = child.getType();
176+
if (CTFParser.tokenNames[CTFParser.CTF_EXPRESSION_VAL].equals(type)) {
177+
ICTFMetadataNode leftNode = child.getChild(0);
178+
ICTFMetadataNode rightNode = child.getChild(1);
179+
List<ICTFMetadataNode> leftStrings = leftNode.getChildren();
180+
if (!isAnyUnaryString(leftStrings.get(0))) {
181+
throw new ParseException(IDENTIFIER_MUST_BE_A_STRING);
182+
}
183+
String left = concatenateUnaryStrings(leftStrings);
184+
if (left.equals(MetadataStrings.EXP_DIG)) {
185+
exponent = UnaryIntegerParser.INSTANCE.parse(rightNode.getChild(0), null).intValue();
186+
} else if (left.equals(MetadataStrings.BYTE_ORDER)) {
187+
byteOrder = ByteOrderParser.INSTANCE.parse(rightNode, new ByteOrderParser.Param(trace));
188+
} else if (left.equals(MetadataStrings.MANT_DIG)) {
189+
mantissa = UnaryIntegerParser.INSTANCE.parse(rightNode.getChild(0), null).intValue();
190+
} else if (left.equals(MetadataStrings.ALIGN)) {
191+
alignment = AlignmentParser.INSTANCE.parse(rightNode, null);
192+
} else {
193+
throw new ParseException(FLOAT_UNKNOWN_ATTRIBUTE + left);
194+
}
153195
} else {
154-
throw new ParseException(FLOAT_UNKNOWN_ATTRIBUTE + left);
196+
throw childTypeError(child);
155197
}
156-
} else {
157-
throw childTypeError(child);
158198
}
159199
}
200+
160201
int size = mantissa + exponent;
161202
if (size == 0) {
162203
throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE);
@@ -167,7 +208,6 @@ public FloatDeclaration parse(ICTFMetadataNode floatingPoint, ICommonTreeParserP
167208
}
168209

169210
return new FloatDeclaration(exponent, mantissa, byteOrder, alignment);
170-
171211
}
172212

173213
}

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/utils/JsonMetadataStrings.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,12 @@ private JsonMetadataStrings() {
158158
public static final String VARIABLE_SIGNED_INTEGER_FIELD = "variable-length-signed-integer"; //$NON-NLS-1$
159159

160160
/**
161-
* Type string for a fixed length floating point field class
161+
* Type string for a fixed length floating point number field class
162162
*/
163163
public static final String FIXED_LENGTH_FLOATING_POINT = "fixed-length-floating-point-number"; //$NON-NLS-1$
164+
164165
/**
165-
* Type string for a variable length floating point field class
166+
* Type string for a variable length floating point number field class
166167
*/
167168
public static final String VARIABLE_LENGTH_FLOATING_POINT = "variable-length-floating-point-number"; //$NON-NLS-1$
168169

@@ -177,12 +178,12 @@ private JsonMetadataStrings() {
177178
public static final String NULL_TERMINATED_STRING = "null-terminated-string"; //$NON-NLS-1$
178179

179180
/**
180-
* The length of a dynamic string
181+
* Field string for the length of a field class
181182
*/
182183
public static final String LENGTH = "length"; //$NON-NLS-1$
183184

184185
/**
185-
* Type string for a null terminated string field class
186+
* Type string for a dynamic length string field class
186187
*/
187188
public static final String DYNAMIC_LENGTH_STRING = "dynamic-length-string"; //$NON-NLS-1$
188189

@@ -201,7 +202,7 @@ private JsonMetadataStrings() {
201202
public static final String VARIANT = "variant"; //$NON-NLS-1$
202203

203204
/**
204-
* Type string for a structure field class
205+
* Field string for a path
205206
*/
206207
public static final String PATH = "path"; //$NON-NLS-1$
207208

@@ -214,40 +215,57 @@ private JsonMetadataStrings() {
214215
* Type string for an internal structure field class
215216
*/
216217
public static final String STRUCT = "struct"; //$NON-NLS-1$
218+
217219
/**
218-
* Encodings map
220+
* Map of encoding names to Charset objects
219221
*/
220222
public static final Map<String, Charset> ENCODINGS = Map.of("utf-8",StandardCharsets.UTF_8, //$NON-NLS-1$
221223
"utf-16be",StandardCharsets.UTF_16BE,"utf-16le",StandardCharsets.UTF_16LE, //$NON-NLS-1$ //$NON-NLS-2$
222224
"utf-32be", Charset.forName("UTF-32BE"),"utf-32le", Charset.forName("UTF-32LE")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
223225

224226
/**
225-
* Type of string encoding
227+
* Field string for encoding
226228
*/
227229
public static final String ENCODING = "encoding"; //$NON-NLS-1$
228230

229231
/**
230-
* Static length string
232+
* Type string for a static length string field class
231233
*/
232234
public static final String STATIC_LENGTH_STRING = "static-length-string"; //$NON-NLS-1$
233235

234236
/**
235-
* Static length array
237+
* Type string for a static length array field class
236238
*/
237239
public static final String STATIC_LENGTH_ARRAY = "static-length-array"; //$NON-NLS-1$
238240

239241
/**
240-
* Dynamic length array
242+
* Type string for a dynamic length array field class
241243
*/
242244
public static final String DYNAMIC_LENGTH_ARRAY = "dynamic-length-array"; //$NON-NLS-1$
243245

244246
/**
245-
* Element field class
247+
* Field string for element field class
246248
*/
247249
public static final String ELEMENT_FIELD_CLASS = "element-field-class"; //$NON-NLS-1$
248250

251+
/**
252+
* Field string for member classes in a structure
253+
*/
249254
public static final String MEMBER_CLASSES = "member-classes"; //$NON-NLS-1$
250255

256+
/**
257+
* Field string for minimum alignment
258+
*/
251259
public static final String MINIMUM_ALIGNMENT = "minimum-alignment"; //$NON-NLS-1$
252260

261+
/**
262+
* Field string for alignment
263+
*/
264+
public static final String ALIGNMENT = "alignment"; //$NON-NLS-1$
265+
266+
/**
267+
* Field string for byte order
268+
*/
269+
public static final String BYTE_ORDER = "byte-order"; //$NON-NLS-1$
270+
253271
}

0 commit comments

Comments
 (0)