11/*******************************************************************************
2- * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
2+ * Copyright (c) 2011, 2024 Ericsson, Ecole Polytechnique de Montreal and others
33 *
44 * All rights reserved. This program and the accompanying materials are made
55 * available under the terms of the Eclipse Public License 2.0 which
2121import java .math .BigInteger ;
2222import java .nio .ByteOrder ;
2323
24+ import java .util .HashMap ;
25+ import java .util .List ;
26+ import java .util .Map ;
27+
2428import org .eclipse .jdt .annotation .NonNullByDefault ;
2529import org .eclipse .jdt .annotation .Nullable ;
2630import org .eclipse .tracecompass .ctf .core .CTFException ;
@@ -129,6 +133,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
129133 private final long fAlignment ;
130134 private final String fClock ;
131135 private boolean fVarint = false ;
136+ private Map <String , List <IntegerRange >> fMappings = new HashMap <>();
132137
133138 // ------------------------------------------------------------------------
134139 // Constructors
@@ -315,7 +320,38 @@ public static IntegerDeclaration createDeclaration(int len, boolean signed, int
315320 }
316321 }
317322 }
318- return new IntegerDeclaration (len , signed , base , byteOrder , encoding , clock , alignment , role );
323+ return new IntegerDeclaration (len , signed , base , byteOrder , encoding , clock , alignment , role , null );
324+ }
325+
326+ /**
327+ * Alternate create method for CTF2 integers which have roles and mappings
328+ *
329+ * @param len
330+ * The length in bits
331+ * @param signed
332+ * Is the integer signed? false == unsigned
333+ * @param base
334+ * The base (10-16 are most common)
335+ * @param byteOrder
336+ * Big-endian little-endian or other
337+ * @param encoding
338+ * ascii, utf8 or none.
339+ * @param clock
340+ * The clock path, can be null
341+ * @param alignment
342+ * The minimum alignment. Should be >= 1
343+ * @param role
344+ * The role of the declaration
345+ * @param mappings
346+ * A mapped range of integers
347+ * @return The integer declaration
348+ * @since 4.5
349+ */
350+ public static IntegerDeclaration createDeclaration (int len , boolean signed , int base ,
351+ @ Nullable ByteOrder byteOrder , Encoding encoding , String clock , long alignment , @ Nullable String role , Map <String , List <IntegerRange >> mappings ) {
352+ IntegerDeclaration decl = createDeclaration (len , signed , base , byteOrder , encoding , clock , alignment , role );
353+ decl .setMappings (mappings );
354+ return decl ;
319355 }
320356
321357 /**
@@ -340,6 +376,33 @@ public static IntegerDeclaration createVarintDeclaration(boolean signed, int bas
340376 return decl ;
341377 }
342378
379+ /**
380+ * Create method for CTF2 varints with mappings
381+ *
382+ * @param signed
383+ * Is the integer signed? false == unsigned
384+ * @param base
385+ * The base (10-16 are most common)
386+ * @param role
387+ * The role of the integer declaration
388+ * @param varint
389+ * A boolean indicating if the declaration is a varint
390+ * @param mappings
391+ * A mapped range of integers
392+ * @return IntegerDeclaration
393+ *
394+ * @since 4.5
395+ */
396+ public static IntegerDeclaration createVarintDeclaration (boolean signed , int base , @ Nullable String role , boolean varint , @ Nullable Map <String , List <IntegerRange >> mappings ) {
397+ IntegerDeclaration decl = new IntegerDeclaration (0 , signed , base , null , Encoding .NONE , "" , 0 ); //$NON-NLS-1$
398+ decl .setRole (role );
399+ decl .setVarint (varint );
400+ if (mappings != null ) {
401+ decl .setMappings (mappings );
402+ }
403+ return decl ;
404+ }
405+
343406 private static boolean isBigEndian (@ Nullable ByteOrder byteOrder ) {
344407 return (byteOrder != null ) && byteOrder .equals (ByteOrder .BIG_ENDIAN );
345408 }
@@ -396,13 +459,16 @@ private IntegerDeclaration(int len, boolean signed, int base,
396459 * The role of the integer declaration
397460 */
398461 private IntegerDeclaration (int len , boolean signed , int base ,
399- @ Nullable ByteOrder byteOrder , Encoding encoding , String clock , long alignment , @ Nullable String role ) {
462+ @ Nullable ByteOrder byteOrder , Encoding encoding , String clock , long alignment , @ Nullable String role , @ Nullable Map < String , List < IntegerRange >> mappings ) {
400463 this (len , signed , base , byteOrder , encoding , clock , alignment );
401464 setRole (role );
465+ if (mappings != null ) {
466+ setMappings (mappings );
467+ }
402468 }
403469
404470 private IntegerDeclaration (int len , boolean signed , @ Nullable ByteOrder byteOrder ) {
405- this (len , signed , BASE_10 , byteOrder , Encoding .NONE , "" , BYTE_ALIGN , null ); //$NON-NLS-1$
471+ this (len , signed , BASE_10 , byteOrder , Encoding .NONE , "" , BYTE_ALIGN , null , null ); //$NON-NLS-1$
406472 }
407473
408474 // ------------------------------------------------------------------------
@@ -438,6 +504,26 @@ private void setVarint(boolean varint) {
438504 fVarint = varint ;
439505 }
440506
507+ /**
508+ * Getter for mappings
509+ *
510+ * @return a mapped range of integers
511+ * @since 4.5
512+ */
513+ public Map <String , List <IntegerRange >> getMappings () {
514+ return fMappings ;
515+ }
516+
517+ /**
518+ * Setter for mappings
519+ *
520+ * @param mappings
521+ * a mapped range of integers
522+ */
523+ private void setMappings (Map <String , List <IntegerRange >> mappings ) {
524+ fMappings = mappings ;
525+ }
526+
441527 /**
442528 * Get the integer base commonly decimal or hex
443529 *
@@ -526,7 +612,11 @@ public IntegerDefinition createDefinition(@Nullable IDefinitionScope definitionS
526612 input .setByteOrder (fByteOrder );
527613 long value = read (input );
528614 input .setByteOrder (byteOrder );
529- return new IntegerDefinition (this , definitionScope , fieldName , value );
615+ if (fMappings .size () == 0 ) {
616+ return new IntegerDefinition (this , definitionScope , fieldName , value );
617+ }
618+ String mappingName = getMappingForValue (value );
619+ return new IntegerDefinition (this , definitionScope , fieldName , value , mappingName );
530620 }
531621
532622 @ Override
@@ -687,4 +777,25 @@ private boolean isBinaryEquivalent(IntegerDeclaration other) {
687777 return !((fLength != BYTE_ALIGN ) && !fByteOrder .equals (other .fByteOrder ));
688778 }
689779
780+ private String getMappingForValue (long value ) {
781+ String mapping = "" ; //$NON-NLS-1$
782+ int count = 0 ;
783+ for (Map .Entry <String , List <IntegerRange >> entry : fMappings .entrySet ()) {
784+ String mappingName = entry .getKey ();
785+ List <IntegerRange > ranges = entry .getValue ();
786+
787+ for (IntegerRange range : ranges ) {
788+ if (range .getStart () <= value && value <= range .getEnd ()) {
789+ if (count != 0 ) {
790+ mapping += " " + mappingName ; //$NON-NLS-1$
791+ } else {
792+ mapping += mappingName ;
793+ }
794+ count ++;
795+
796+ }
797+ }
798+ }
799+ return mapping ;
800+ }
690801}
0 commit comments