1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // LinkOnce.cc -- Translate the cfa_linkonce attribute. |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Thur May 13 10:10:00 2021 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Thur May 13 14:39:00 2021 |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | #include "LinkOnce.h" |
---|
17 | |
---|
18 | #include <algorithm> |
---|
19 | |
---|
20 | #include "Common/PassVisitor.h" // for PassVisitor, WithShortCircuiting |
---|
21 | |
---|
22 | namespace CodeGen { |
---|
23 | |
---|
24 | static bool is_cfa_linkonce( Attribute const * attr ) { |
---|
25 | return std::string("cfa_linkonce") == attr->name; |
---|
26 | } |
---|
27 | |
---|
28 | static bool is_section_attribute( Attribute const * attr ) { |
---|
29 | return std::string("section") == attr->name; |
---|
30 | } |
---|
31 | |
---|
32 | class LinkOnceVisitorCore : public WithShortCircuiting { |
---|
33 | public: |
---|
34 | void previsit( Declaration * ) { |
---|
35 | visit_children = false; |
---|
36 | } |
---|
37 | |
---|
38 | void previsit( DeclarationWithType * decl ) { |
---|
39 | std::list< Attribute * > & attributes = decl->attributes; |
---|
40 | // See if we can find the element: |
---|
41 | auto found = std::find_if(attributes.begin(), attributes.end(), is_cfa_linkonce ); |
---|
42 | if ( attributes.end() != found ) { |
---|
43 | // Remove any other sections: |
---|
44 | attributes.remove_if( is_section_attribute ); |
---|
45 | // Iterator to the cfa_linkonce attribute should still be valid. |
---|
46 | Attribute * attribute = *found; |
---|
47 | assert( attribute->parameters.empty() ); |
---|
48 | assert( !decl->mangleName.empty() ); |
---|
49 | // Overwrite the attribute in place. |
---|
50 | const std::string section_name = ".gnu.linkonce." + decl->mangleName; |
---|
51 | attribute->name = "section"; |
---|
52 | attribute->parameters.push_back( |
---|
53 | new ConstantExpr( Constant::from_string( section_name ) ) |
---|
54 | ); |
---|
55 | |
---|
56 | // Unconditionnaly add "visibility(default)" to anything with gnu.linkonce |
---|
57 | // visibility is a mess otherwise |
---|
58 | attributes.push_back(new Attribute("visibility", {new ConstantExpr( Constant::from_string( "default" ) )})); |
---|
59 | |
---|
60 | } |
---|
61 | visit_children = false; |
---|
62 | } |
---|
63 | }; |
---|
64 | |
---|
65 | void translateLinkOnce( std::list< Declaration *> & translationUnit ) { |
---|
66 | PassVisitor<LinkOnceVisitorCore> translator; |
---|
67 | acceptAll( translationUnit, translator ); |
---|
68 | } |
---|
69 | |
---|
70 | } |
---|