33import com .dat3m .dartagnan .exception .ParsingException ;
44import com .dat3m .dartagnan .expression .*;
55import com .dat3m .dartagnan .expression .integers .IntBinaryOp ;
6+ import com .dat3m .dartagnan .expression .integers .IntLiteral ;
67import com .dat3m .dartagnan .expression .misc .ConstructExpr ;
78import com .dat3m .dartagnan .expression .type .*;
89import com .dat3m .dartagnan .parsers .LLVMIRBaseVisitor ;
1112import com .dat3m .dartagnan .program .Function ;
1213import com .dat3m .dartagnan .program .Program ;
1314import com .dat3m .dartagnan .program .Register ;
15+ import com .dat3m .dartagnan .program .analysis .alias .TBAA ;
1416import com .dat3m .dartagnan .program .event .Event ;
1517import com .dat3m .dartagnan .program .event .EventFactory ;
1618import com .dat3m .dartagnan .program .event .Tag ;
@@ -50,6 +52,7 @@ public class VisitorLlvm extends LLVMIRBaseVisitor<Expression> {
5052 private final Map <String , TypeDefContext > typeDefinitionMap = new HashMap <>();
5153 private final Map <String , Type > typeMap = new HashMap <>();
5254 private final Map <String , MdNode > metadataSymbolTable = new LinkedHashMap <>();
55+ private final TBAABuilder tbaaBuilder = new TBAABuilder ();
5356 private int functionCounter ;
5457 // Nonnull, if the visitor is inside a function body.
5558 private Function function ;
@@ -314,6 +317,22 @@ private List<Metadata> parseMetadataAttachment(List<MetadataAttachmentContext> m
314317 assert mdNode instanceof MdReference ;
315318 mdNode = metadataSymbolTable .get (((MdReference ) mdNode ).mdName ());
316319
320+ final String metaDatatype = metadataCtx .MetadataName ().toString ();
321+
322+ if (metaDatatype .equals ("!tbaa" )) {
323+ // We assume a tbaa access tag
324+ assert mdNode instanceof MdTuple ;
325+ final MdTuple accessNode = (MdTuple ) mdNode ;
326+ final List <MdNode > children = accessNode .mdFields ();
327+
328+ final TBAA .Type base = tbaaBuilder .getTBAATypeFromNode (children .get (0 ));
329+ final TBAA .Type access = tbaaBuilder .getTBAATypeFromNode (children .get (1 ));
330+ final int offset = ((MdGenericValue <IntLiteral >)children .get (2 )).value ().getValueAsInt ();
331+ // final int isConstant = ((MdGenericValue<IntLiteral>)children.get(3)).value().getValueAsInt();
332+
333+ metadata .add (new TBAA .AccessTag (base , access , offset ));
334+ }
335+
317336 if (mdNode instanceof SpecialMdTupleNode diLocationNode && diLocationNode .nodeType () == SpecialMdTupleNode .Type .DILocation ) {
318337 SpecialMdTupleNode scope = diLocationNode ;
319338 while (scope .getField ("scope" ).isPresent ()) {
@@ -1463,4 +1482,51 @@ public <T extends MdNode> Optional<T> getField(String fieldName) {
14631482 }
14641483 }
14651484
1485+ // ----------------------------------------------------------------------------------------------------------------
1486+ // TBAA-metadata
1487+
1488+ private class TBAABuilder {
1489+ private final Map <MdNode , TBAA .Type > md2tbaaMap = new HashMap <>();
1490+
1491+ private TBAA .Type getTBAATypeFromNode (MdNode node ) {
1492+ if (node instanceof MdReference ref ) {
1493+ node = metadataSymbolTable .get (ref .mdName ());
1494+ }
1495+ assert node instanceof MdTuple ;
1496+
1497+ if (!md2tbaaMap .containsKey (node )) {
1498+ final MdTuple tbaaTypeMdNode = (MdTuple ) node ;
1499+ final List <MdNode > children = tbaaTypeMdNode .mdFields ();
1500+ if (children .size () <= 1 ) {
1501+ // Root
1502+ final String rootName = children .isEmpty () ? "root" : children .get (0 ).toString ();
1503+ final TBAA .Type root = new TBAA .ScalarType (rootName , null );
1504+ md2tbaaMap .put (tbaaTypeMdNode , root );
1505+ } else if (children .size () == 3 ) {
1506+ // Scalar type node
1507+ final String name = children .get (0 ).toString ();
1508+ final TBAA .Type parent = getTBAATypeFromNode (children .get (1 ));
1509+ assert children .get (2 ).toString ().equals ("0" );
1510+ final TBAA .Type typeNode = new TBAA .ScalarType (name , parent );
1511+ md2tbaaMap .put (tbaaTypeMdNode , typeNode );
1512+ } else {
1513+ // Struct type node
1514+ final String name = children .get (0 ).toString ();
1515+ final List <TBAA .TypeOffset > typeOffsets = new ArrayList <>();
1516+ for (int i = 1 ; i < children .size (); i += 2 ) {
1517+ final TBAA .Type innerType = getTBAATypeFromNode (children .get (i ));
1518+ final int offset = ((MdGenericValue <IntLiteral >)children .get (i +1 )).value ().getValueAsInt ();
1519+ typeOffsets .add (new TBAA .TypeOffset (innerType , offset ));
1520+ }
1521+ final TBAA .StructType typeNode = new TBAA .StructType (name , typeOffsets );
1522+ md2tbaaMap .put (tbaaTypeMdNode , typeNode );
1523+ }
1524+ }
1525+
1526+ return md2tbaaMap .get (node );
1527+ }
1528+
1529+ }
1530+
1531+
14661532}
0 commit comments