Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
4311ac8b | 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
4311ac8b MAL |
9 | * Contributors: |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | * Simon Marchi - Initial API and implementation | |
12 | * Marc-Andre Laperle - Add min/maximum for validation | |
866e5b51 FC |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.linuxtools.ctf.core.event.types; | |
16 | ||
4311ac8b | 17 | import java.math.BigInteger; |
866e5b51 FC |
18 | import java.nio.ByteOrder; |
19 | ||
20 | /** | |
d37aaa7f | 21 | * A CTF integer declaration. |
4311ac8b | 22 | * |
d37aaa7f FC |
23 | * The declaration of a integer basic data type. |
24 | * | |
25 | * @version 1.0 | |
26 | * @author Matthew Khouzam | |
27 | * @author Simon Marchi | |
866e5b51 FC |
28 | */ |
29 | public class IntegerDeclaration implements IDeclaration { | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
0594c61c AM |
35 | private final int length; |
36 | private final boolean signed; | |
37 | private final int base; | |
38 | private final ByteOrder byteOrder; | |
39 | private final Encoding encoding; | |
40 | private final long alignment; | |
41 | private final String clock; | |
866e5b51 FC |
42 | |
43 | // ------------------------------------------------------------------------ | |
44 | // Constructors | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
9ac2eb62 | 47 | /** |
a511da0d | 48 | * Constructor |
056ebaf1 AM |
49 | * |
50 | * @param len | |
51 | * The length in bits | |
52 | * @param signed | |
53 | * Is the integer signed? false == unsigned | |
54 | * @param base | |
55 | * The base (10-16 are most common) | |
56 | * @param byteOrder | |
a511da0d | 57 | * Big-endian little-endian or other |
056ebaf1 AM |
58 | * @param encoding |
59 | * ascii, utf8 or none. | |
60 | * @param clock | |
61 | * The clock path, can be null | |
62 | * @param alignment | |
ab04fc6b | 63 | * The minimum alignment. Should be ≥ 1 |
9ac2eb62 | 64 | */ |
866e5b51 | 65 | public IntegerDeclaration(int len, boolean signed, int base, |
fd74e6c1 | 66 | ByteOrder byteOrder, Encoding encoding, String clock, long alignment) { |
4311ac8b MAL |
67 | if (len <= 0 || len == 1 && signed) { |
68 | throw new IllegalArgumentException(); | |
69 | } | |
866e5b51 FC |
70 | this.length = len; |
71 | this.signed = signed; | |
72 | this.base = base; | |
73 | this.byteOrder = byteOrder; | |
74 | this.encoding = encoding; | |
284fdee8 | 75 | this.clock = clock; |
056ebaf1 | 76 | this.alignment = Math.max(alignment, 1); |
866e5b51 FC |
77 | } |
78 | ||
79 | // ------------------------------------------------------------------------ | |
a511da0d | 80 | // Getters/Setters/Predicates |
866e5b51 FC |
81 | // ------------------------------------------------------------------------ |
82 | ||
9ac2eb62 MK |
83 | /** |
84 | * Is the integer signed? | |
bdc1590e | 85 | * |
9ac2eb62 MK |
86 | * @return the is the integer signed |
87 | */ | |
866e5b51 FC |
88 | public boolean isSigned() { |
89 | return signed; | |
90 | } | |
91 | ||
9ac2eb62 | 92 | /** |
a511da0d | 93 | * Get the integer base commonly decimal or hex |
bdc1590e | 94 | * |
9ac2eb62 MK |
95 | * @return the integer base |
96 | */ | |
866e5b51 FC |
97 | public int getBase() { |
98 | return base; | |
99 | } | |
100 | ||
9ac2eb62 | 101 | /** |
bdc1590e EB |
102 | * Get the byte order |
103 | * | |
9ac2eb62 MK |
104 | * @return the byte order |
105 | */ | |
866e5b51 FC |
106 | public ByteOrder getByteOrder() { |
107 | return byteOrder; | |
108 | } | |
109 | ||
9ac2eb62 | 110 | /** |
a511da0d | 111 | * Get encoding, chars are 8 bit ints |
bdc1590e | 112 | * |
9ac2eb62 MK |
113 | * @return the encoding |
114 | */ | |
866e5b51 FC |
115 | public Encoding getEncoding() { |
116 | return encoding; | |
117 | } | |
118 | ||
9ac2eb62 | 119 | /** |
a511da0d | 120 | * Is the integer a character (8 bits and encoded?) |
bdc1590e | 121 | * |
9ac2eb62 MK |
122 | * @return is the integer a char |
123 | */ | |
bdc1590e | 124 | public boolean isCharacter() { |
866e5b51 FC |
125 | return (length == 8) && (encoding != Encoding.NONE); |
126 | } | |
127 | ||
bdc1590e EB |
128 | /** |
129 | * Get the length in bits for this integer | |
130 | * | |
131 | * @return the length of the integer | |
132 | */ | |
866e5b51 FC |
133 | public int getLength() { |
134 | return length; | |
135 | } | |
136 | ||
07002e0a | 137 | @Override |
6f04e06c | 138 | public long getAlignment() { |
fd74e6c1 MK |
139 | return alignment; |
140 | } | |
141 | ||
9ac2eb62 MK |
142 | /** |
143 | * The integer's clock, since timestamps are stored in ints | |
bdc1590e | 144 | * |
9ac2eb62 MK |
145 | * @return the integer's clock, can be null. (most often it is) |
146 | */ | |
6f04e06c | 147 | public String getClock() { |
fd74e6c1 MK |
148 | return clock; |
149 | } | |
bdc1590e | 150 | |
866e5b51 FC |
151 | // ------------------------------------------------------------------------ |
152 | // Operations | |
153 | // ------------------------------------------------------------------------ | |
154 | ||
155 | @Override | |
156 | public IntegerDefinition createDefinition(IDefinitionScope definitionScope, | |
157 | String fieldName) { | |
158 | return new IntegerDefinition(this, definitionScope, fieldName); | |
159 | } | |
160 | ||
161 | @Override | |
162 | public String toString() { | |
163 | /* Only used for debugging */ | |
164 | return "[declaration] integer[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
165 | } | |
166 | ||
4311ac8b MAL |
167 | /** |
168 | * Get the maximum value for this integer declaration | |
169 | * | |
170 | * @return The maximum value for this integer declaration | |
171 | * @since 2.0 | |
172 | */ | |
173 | public BigInteger getMaxValue() { | |
bdc1590e EB |
174 | /* |
175 | * Compute the number of bits able to represent an unsigned number, | |
176 | * ignoring sign bit. | |
177 | */ | |
178 | int significant_bits = length - (signed ? 1 : 0); | |
179 | /* | |
180 | * For a given N significant bits, compute the maximal value which is | |
181 | * (1 << N) - 1. | |
182 | */ | |
183 | return BigInteger.ONE.shiftLeft(significant_bits).subtract(BigInteger.ONE); | |
4311ac8b MAL |
184 | } |
185 | ||
186 | /** | |
187 | * Get the minimum value for this integer declaration | |
188 | * | |
189 | * @return The minimum value for this integer declaration | |
190 | * @since 2.0 | |
191 | */ | |
192 | public BigInteger getMinValue() { | |
193 | if (!signed) { | |
194 | return BigInteger.ZERO; | |
195 | } | |
196 | ||
bdc1590e EB |
197 | /* |
198 | * Compute the number of bits able to represent an unsigned number, | |
199 | * without the sign bit. | |
200 | */ | |
201 | int significant_bits = length - 1; | |
202 | /* | |
203 | * For a given N significant bits, compute the minimal value which is | |
204 | * - (1 << N). | |
205 | */ | |
206 | return BigInteger.ONE.shiftLeft(significant_bits).negate(); | |
4311ac8b MAL |
207 | } |
208 | ||
866e5b51 | 209 | } |