source: src/InitTweak/FixGlobalInit.cc@ 6fbe9a5

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 6fbe9a5 was 6fbe9a5, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

Fixing code-gen of constants. Fixes #182 Removes workaround 58b6d1.

Forcing recent GCC versions to place CFA-initialized constants in writeable memory, so CFA initialization doesn't segfault when writing them. See the const-init test for specifics about recent GCC versions.

src/InitTweak/FixGlobalInit.cc: generating the attribute to control GCC's placement
libcfa/src/limits.* : removing workaround from 58b6d1, making these limits const again
tests//limits.* : commenting old test that uses the constants from licfa-limits, explaining what the test doesn't exercise
tests/
/const-init.* : new test of static constants, taken from #182, and comments explaining how to test this issue

  • Property mode set to 100644
File size: 6.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 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// FixGlobalInit.cc --
8//
9// Author : Rob Schluntz
10// Created On : Mon May 04 15:14:56 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Dec 13 23:41:10 2019
13// Update Count : 19
14//
15
16#include "FixGlobalInit.h"
17
18#include <cassert> // for assert
19#include <stddef.h> // for NULL
20#include <algorithm> // for replace_if
21
22#include "Common/PassVisitor.h"
23#include "Common/UniqueName.h" // for UniqueName
24#include "InitTweak.h" // for isIntrinsicSingleArgCallStmt
25#include "SynTree/LinkageSpec.h" // for C
26#include "SynTree/Attribute.h" // for Attribute
27#include "SynTree/Constant.h" // for Constant
28#include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declaration
29#include "SynTree/Expression.h" // for ConstantExpr, Expression (ptr only)
30#include "SynTree/Initializer.h" // for ConstructorInit, Initializer
31#include "SynTree/Label.h" // for Label
32#include "SynTree/Statement.h" // for CompoundStmt, Statement (ptr only)
33#include "SynTree/Type.h" // for Type, Type::StorageClasses, Functi...
34#include "SynTree/Visitor.h" // for acceptAll, Visitor
35
36namespace InitTweak {
37 class GlobalFixer : public WithShortCircuiting {
38 public:
39 GlobalFixer( bool inLibrary );
40
41 void previsit( ObjectDecl *objDecl );
42 void previsit( FunctionDecl *functionDecl );
43 void previsit( StructDecl *aggregateDecl );
44 void previsit( UnionDecl *aggregateDecl );
45 void previsit( EnumDecl *aggregateDecl );
46 void previsit( TraitDecl *aggregateDecl );
47 void previsit( TypeDecl *typeDecl );
48
49 UniqueName tempNamer;
50 FunctionDecl * initFunction;
51 FunctionDecl * destroyFunction;
52 };
53
54 void fixGlobalInit( std::list< Declaration * > & translationUnit, bool inLibrary ) {
55 PassVisitor<GlobalFixer> visitor( inLibrary );
56 acceptAll( translationUnit, visitor );
57 GlobalFixer & fixer = visitor.pass;
58 // don't need to include function if it's empty
59 if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
60 delete fixer.initFunction;
61 } else {
62 translationUnit.push_back( fixer.initFunction );
63 } // if
64
65 if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
66 delete fixer.destroyFunction;
67 } else {
68 translationUnit.push_back( fixer.destroyFunction );
69 } // if
70 }
71
72 GlobalFixer::GlobalFixer( bool inLibrary ) : tempNamer( "_global_init" ) {
73 std::list< Expression * > ctorParameters;
74 std::list< Expression * > dtorParameters;
75 if ( inLibrary ) {
76 // Constructor/destructor attributes take a single parameter which
77 // is the priority, with lower numbers meaning higher priority.
78 // Functions specified with priority are guaranteed to run before
79 // functions without a priority. To ensure that constructors and destructors
80 // for library code are run before constructors and destructors for user code,
81 // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
82 // Priorities 101-200 are reserved by cfa, so use priority 200 for CFA library globals,
83 // allowing room for overriding with a higher priority.
84 ctorParameters.push_back( new ConstantExpr( Constant::from_int( 200 ) ) );
85 dtorParameters.push_back( new ConstantExpr( Constant::from_int( 200 ) ) );
86 }
87 initFunction = new FunctionDecl( "__global_init__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
88 initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
89 destroyFunction = new FunctionDecl( "__global_destroy__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
90 destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
91 }
92
93 void GlobalFixer::previsit( ObjectDecl *objDecl ) {
94 std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
95 std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
96
97 // C allows you to initialize objects with constant expressions
98 // xxx - this is an optimization. Need to first resolve constructors before we decide
99 // to keep C-style initializer.
100 // if ( isConstExpr( objDecl->get_init() ) ) return;
101
102 if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
103 // a decision should have been made by the resolver, so ctor and init are not both non-NULL
104 assert( ! ctorInit->ctor || ! ctorInit->init );
105
106 Statement * dtor = ctorInit->dtor;
107 if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
108 // don't need to call intrinsic dtor, because it does nothing, but
109 // non-intrinsic dtors must be called
110 destroyStatements.push_front( dtor );
111 ctorInit->dtor = nullptr;
112 } // if
113 if ( Statement * ctor = ctorInit->ctor ) {
114 // Translation 1: Add this attribute on the global declaration:
115 // __attribute__((section (".data#")))
116 // which makes gcc put the global in the data section,
117 // so that the global is writeable (via a const cast) in the init function.
118 // The trailing # is an injected assembly comment, to suppress the "a" in
119 // .section .data,"a"
120 // .section .data#,"a"
121 // to avoid assembler warning "ignoring changed section attributes for .data"
122 Type *strLitT = new PointerType( Type::Qualifiers( ),
123 new BasicType( Type::Qualifiers( ), BasicType::Char ) );
124 std::list< Expression * > attr_params;
125 attr_params.push_back(
126 new ConstantExpr( Constant( strLitT, "\".data#\"", std::nullopt ) ) );
127 objDecl->attributes.push_back(new Attribute("section", attr_params));
128 // Translation 2: Move the initizliation off the global declaration,
129 // into the startup function.
130 initStatements.push_back( ctor );
131 objDecl->init = nullptr;
132 ctorInit->ctor = nullptr;
133 } else if ( Initializer * init = ctorInit->init ) {
134 objDecl->init = init;
135 ctorInit->init = nullptr;
136 } else {
137 // no constructor and no initializer, which is okay
138 objDecl->init = nullptr;
139 } // if
140 delete ctorInit;
141 } // if
142 }
143
144 // only modify global variables
145 void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; }
146 void GlobalFixer::previsit( StructDecl * ) { visit_children = false; }
147 void GlobalFixer::previsit( UnionDecl * ) { visit_children = false; }
148 void GlobalFixer::previsit( EnumDecl * ) { visit_children = false; }
149 void GlobalFixer::previsit( TraitDecl * ) { visit_children = false; }
150 void GlobalFixer::previsit( TypeDecl * ) { visit_children = false; }
151
152} // namespace InitTweak
153
154// Local Variables: //
155// tab-width: 4 //
156// mode: c++ //
157// compile-command: "make install" //
158// End: //
Note: See TracBrowser for help on using the repository browser.