1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.floatingpoint
;
12 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.childTypeError
;
13 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.concatenateUnaryStrings
;
14 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.isAnyUnaryString
;
16 import java
.nio
.ByteOrder
;
17 import java
.util
.List
;
19 import org
.antlr
.runtime
.tree
.CommonTree
;
20 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDeclaration
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
23 import org
.eclipse
.tracecompass
.ctf
.parser
.CTFParser
;
24 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ICommonTreeParser
;
25 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.MetadataStrings
;
26 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ParseException
;
27 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.AlignmentParser
;
28 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.ByteOrderParser
;
29 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.UnaryIntegerParser
;
33 * The floating point values byte ordering is defined in the TSDL metadata.
35 * Floating point values follow the IEEE 754-2008 standard interchange formats.
36 * Description of the floating point values include the exponent and mantissa
37 * size in bits. Some requirements are imposed on the floating point values:
39 * <li>FLT_RADIX must be 2.</li>
40 * <li>mant_dig is the number of digits represented in the mantissa. It is
41 * specified by the ISO C99 standard, section 5.2.4, as FLT_MANT_DIG,
42 * DBL_MANT_DIG and LDBL_MANT_DIG as defined by <float.h>.</li>
43 * <li>exp_dig is the number of digits represented in the exponent. Given that
44 * mant_dig is one bit more than its actual size in bits (leading 1 is not
45 * needed) and also given that the sign bit always takes one bit, exp_dig can be
48 * <li>sizeof(float) * CHAR_BIT - FLT_MANT_DIG</li>
49 * <li>sizeof(double) * CHAR_BIT - DBL_MANT_DIG</li>
50 * <li>sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG</li>
55 * @author Matthew Khouzam - initial API and implementation
56 * @auttor Efficios - Description
59 public class FloatDeclarationParser
implements ICommonTreeParser
{
61 private static final String FLOAT_UNKNOWN_ATTRIBUTE
= "Float: unknown attribute "; //$NON-NLS-1$
62 private static final String FLOAT_MISSING_SIZE_ATTRIBUTE
= "Float: missing size attribute"; //$NON-NLS-1$
63 private static final String IDENTIFIER_MUST_BE_A_STRING
= "Left side of ctf expression must be a string"; //$NON-NLS-1$
66 * Parameter object with only a trace in it
68 * @author Matthew Khouzam
72 public static final class Param
implements ICommonTreeParserParameter
{
73 private final CTFTrace fTrace
;
81 public Param(CTFTrace trace
) {
89 public static final FloatDeclarationParser INSTANCE
= new FloatDeclarationParser();
91 private static final int DEFAULT_FLOAT_EXPONENT
= 8;
92 private static final int DEFAULT_FLOAT_MANTISSA
= 24;
94 private FloatDeclarationParser() {
98 * Parses a float node and returns a floating point declaration of the
99 * type @link {@link FloatDeclaration}.
101 * @param floatingPoint
102 * AST node of type FLOAT
104 * parameter containing the trace
105 * @return the float declaration
106 * @throws ParseException
107 * if a float AST is malformed
110 public FloatDeclaration
parse(CommonTree floatingPoint
, ICommonTreeParserParameter param
) throws ParseException
{
111 if (!(param
instanceof Param
)) {
112 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
114 CTFTrace trace
= ((Param
) param
).fTrace
;
115 List
<CommonTree
> children
= floatingPoint
.getChildren();
118 * If the integer has no attributes, then it is missing the size
119 * attribute which is required
121 if (children
== null) {
122 throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE
);
125 /* The return value */
126 ByteOrder byteOrder
= trace
.getByteOrder();
129 int exponent
= DEFAULT_FLOAT_EXPONENT
;
130 int mantissa
= DEFAULT_FLOAT_MANTISSA
;
132 /* Iterate on all integer children */
133 for (CommonTree child
: children
) {
134 switch (child
.getType()) {
135 case CTFParser
.CTF_EXPRESSION_VAL
:
137 * An assignment expression must have 2 children, left and right
140 CommonTree leftNode
= (CommonTree
) child
.getChild(0);
141 CommonTree rightNode
= (CommonTree
) child
.getChild(1);
143 List
<CommonTree
> leftStrings
= leftNode
.getChildren();
145 if (!isAnyUnaryString(leftStrings
.get(0))) {
146 throw new ParseException(IDENTIFIER_MUST_BE_A_STRING
);
148 String left
= concatenateUnaryStrings(leftStrings
);
150 if (left
.equals(MetadataStrings
.EXP_DIG
)) {
151 exponent
= UnaryIntegerParser
.INSTANCE
.parse((CommonTree
) rightNode
.getChild(0), null).intValue();
152 } else if (left
.equals(MetadataStrings
.BYTE_ORDER
)) {
153 byteOrder
= ByteOrderParser
.INSTANCE
.parse(rightNode
, new ByteOrderParser
.Param(trace
));
154 } else if (left
.equals(MetadataStrings
.MANT_DIG
)) {
155 mantissa
= UnaryIntegerParser
.INSTANCE
.parse((CommonTree
) rightNode
.getChild(0), null).intValue();
156 } else if (left
.equals(MetadataStrings
.ALIGN
)) {
157 alignment
= AlignmentParser
.INSTANCE
.parse(rightNode
, null);
159 throw new ParseException(FLOAT_UNKNOWN_ATTRIBUTE
+ left
);
164 throw childTypeError(child
);
167 int size
= mantissa
+ exponent
;
169 throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE
);
172 if (alignment
== 0) {
176 return new FloatDeclaration(exponent
, mantissa
, byteOrder
, alignment
);