Commit | Line | Data |
---|---|---|
b1ea73b5 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
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 | *******************************************************************************/ | |
9 | ||
10 | package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.floatingpoint; | |
11 | ||
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; | |
15 | ||
16 | import java.nio.ByteOrder; | |
17 | import java.util.List; | |
18 | ||
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; | |
30 | ||
31 | /** | |
32 | * | |
33 | * The floating point values byte ordering is defined in the TSDL metadata. | |
34 | * <p> | |
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: | |
38 | * <ul> | |
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 | |
46 | * specified as:</li> | |
47 | * <ul> | |
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> | |
51 | * <li> | |
52 | * </ul> | |
53 | * </ul> | |
54 | * | |
55 | * @author Matthew Khouzam - initial API and implementation | |
56 | * @auttor Efficios - Description | |
57 | * | |
58 | */ | |
4055c3a1 | 59 | public final class FloatDeclarationParser implements ICommonTreeParser { |
b1ea73b5 MK |
60 | |
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$ | |
64 | ||
65 | /** | |
66 | * Parameter object with only a trace in it | |
67 | * | |
68 | * @author Matthew Khouzam | |
69 | * | |
70 | */ | |
71 | @NonNullByDefault | |
72 | public static final class Param implements ICommonTreeParserParameter { | |
73 | private final CTFTrace fTrace; | |
74 | ||
75 | /** | |
76 | * Constructor | |
77 | * | |
78 | * @param trace | |
79 | * the trace | |
80 | */ | |
81 | public Param(CTFTrace trace) { | |
82 | fTrace = trace; | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * Instance | |
88 | */ | |
89 | public static final FloatDeclarationParser INSTANCE = new FloatDeclarationParser(); | |
90 | ||
91 | private static final int DEFAULT_FLOAT_EXPONENT = 8; | |
92 | private static final int DEFAULT_FLOAT_MANTISSA = 24; | |
93 | ||
94 | private FloatDeclarationParser() { | |
95 | } | |
96 | ||
97 | /** | |
98 | * Parses a float node and returns a floating point declaration of the | |
99 | * type @link {@link FloatDeclaration}. | |
100 | * | |
101 | * @param floatingPoint | |
102 | * AST node of type FLOAT | |
103 | * @param param | |
104 | * parameter containing the trace | |
105 | * @return the float declaration | |
106 | * @throws ParseException | |
107 | * if a float AST is malformed | |
108 | */ | |
109 | @Override | |
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$ | |
113 | } | |
114 | CTFTrace trace = ((Param) param).fTrace; | |
115 | List<CommonTree> children = floatingPoint.getChildren(); | |
116 | ||
117 | /* | |
118 | * If the integer has no attributes, then it is missing the size | |
119 | * attribute which is required | |
120 | */ | |
121 | if (children == null) { | |
122 | throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE); | |
123 | } | |
124 | ||
125 | /* The return value */ | |
126 | ByteOrder byteOrder = trace.getByteOrder(); | |
127 | long alignment = 0; | |
128 | ||
129 | int exponent = DEFAULT_FLOAT_EXPONENT; | |
130 | int mantissa = DEFAULT_FLOAT_MANTISSA; | |
131 | ||
132 | /* Iterate on all integer children */ | |
133 | for (CommonTree child : children) { | |
134 | switch (child.getType()) { | |
135 | case CTFParser.CTF_EXPRESSION_VAL: | |
136 | /* | |
137 | * An assignment expression must have 2 children, left and right | |
138 | */ | |
139 | ||
140 | CommonTree leftNode = (CommonTree) child.getChild(0); | |
141 | CommonTree rightNode = (CommonTree) child.getChild(1); | |
142 | ||
143 | List<CommonTree> leftStrings = leftNode.getChildren(); | |
144 | ||
145 | if (!isAnyUnaryString(leftStrings.get(0))) { | |
146 | throw new ParseException(IDENTIFIER_MUST_BE_A_STRING); | |
147 | } | |
148 | String left = concatenateUnaryStrings(leftStrings); | |
149 | ||
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); | |
158 | } else { | |
159 | throw new ParseException(FLOAT_UNKNOWN_ATTRIBUTE + left); | |
160 | } | |
161 | ||
162 | break; | |
163 | default: | |
164 | throw childTypeError(child); | |
165 | } | |
166 | } | |
167 | int size = mantissa + exponent; | |
168 | if (size == 0) { | |
169 | throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE); | |
170 | } | |
171 | ||
172 | if (alignment == 0) { | |
173 | alignment = 1; | |
174 | } | |
175 | ||
176 | return new FloatDeclaration(exponent, mantissa, byteOrder, alignment); | |
177 | ||
178 | } | |
179 | ||
180 | } |