Changeset 3cbe320


Ignore:
Timestamp:
Oct 4, 2023, 11:04:22 AM (8 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
15b5abac
Parents:
cf34e82
Message:

Translated the Link-Once pass to the new ast.

Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/LinkOnce.cc

    rcf34e82 r3cbe320  
    1010// Created On       : Thur May 13 10:10:00 2021
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Thur May 13 14:39:00 2021
    13 // Update Count     : 0
     12// Last Modified On : Wed Oct  4 10:52:00 2023
     13// Update Count     : 1
    1414//
    1515
     
    1818#include <algorithm>
    1919
     20#include "AST/Attribute.hpp"
     21#include "AST/Decl.hpp"
     22#include "AST/Expr.hpp"
     23#include "AST/Pass.hpp"
    2024#include "Common/PassVisitor.h"       // for PassVisitor, WithShortCircuiting
    2125
    2226namespace CodeGen {
    2327
    24 static bool is_cfa_linkonce( Attribute const * attr ) {
     28namespace {
     29
     30bool is_cfa_linkonce_old( Attribute const * attr ) {
    2531        return std::string("cfa_linkonce") == attr->name;
    2632}
    2733
    28 static bool is_section_attribute( Attribute const * attr ) {
     34bool is_section_attribute_old( Attribute const * attr ) {
    2935        return std::string("section") == attr->name;
    3036}
     
    3945                std::list< Attribute * > & attributes = decl->attributes;
    4046                // See if we can find the element:
    41                 auto found = std::find_if(attributes.begin(), attributes.end(), is_cfa_linkonce );
     47                auto found = std::find_if(attributes.begin(), attributes.end(), is_cfa_linkonce_old );
    4248                if ( attributes.end() != found ) {
    4349                        // Remove any other sections:
    44                         attributes.remove_if( is_section_attribute );
     50                        attributes.remove_if( is_section_attribute_old );
    4551                        // Iterator to the cfa_linkonce attribute should still be valid.
    4652                        Attribute * attribute = *found;
     
    6369};
    6470
     71bool is_cfa_linkonce( ast::Attribute const * attr ) {
     72        return "cfa_linkonce" == attr->name;
     73}
     74
     75bool is_section_attribute( ast::Attribute const * attr ) {
     76        return "section" == attr->name;
     77}
     78
     79struct LinkOnceCore : public ast::WithShortCircuiting {
     80        void previsit( ast::Decl const * ) {
     81                visit_children = false;
     82        }
     83
     84        ast::DeclWithType const * postvisit( ast::DeclWithType const * decl ) {
     85                // Check to see if we have to mutate, because should be uncommon.
     86                {
     87                        auto & attributes = decl->attributes;
     88                        auto found = std::find_if( attributes.begin(), attributes.end(),
     89                                        is_cfa_linkonce );
     90                        if ( attributes.end() == found ) return decl;
     91                }
     92                auto mutDecl = mutate( decl );
     93                auto & attributes = mutDecl->attributes;
     94
     95                // Remove all conflicting section attributes.
     96                erase_if( attributes, is_section_attribute );
     97
     98                // Get the attribute, and overwrite it as a section attribute.
     99                auto found = std::find_if( attributes.begin(), attributes.end(),
     100                                is_cfa_linkonce );
     101                assert( attributes.end() != found );
     102                ast::Attribute * attribute = found->get_and_mutate();
     103                assert( attribute->params.empty() );
     104                assert( !decl->mangleName.empty() );
     105
     106                attribute->name = "section";
     107                attribute->params.push_back(
     108                        ast::ConstantExpr::from_string( mutDecl->location,
     109                                ".gnu.linkonce." + decl->mangleName
     110                        )
     111                );
     112
     113                // Unconditionnaly add "visibility(default)" to anything with
     114                // .gnu.linkonce visibility is a mess otherwise.
     115                attributes.push_back( new ast::Attribute( "visibility", {
     116                        ast::ConstantExpr::from_string( mutDecl->location, "default" )
     117                } ) );
     118                return mutDecl;
     119        }
     120};
     121
     122} // namespace
     123
    65124void translateLinkOnce( std::list< Declaration *> & translationUnit ) {
    66125        PassVisitor<LinkOnceVisitorCore> translator;
     
    68127}
    69128
     129void translateLinkOnce( ast::TranslationUnit & translationUnit ) {
     130        ast::Pass<LinkOnceCore>::run( translationUnit );
    70131}
     132
     133} // namespace CodeGen
  • src/CodeGen/LinkOnce.h

    rcf34e82 r3cbe320  
    1010// Created On       : Thur May 13 10:06:00 2021
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Thur May 13 14:38:00 2021
    13 // Update Count     : 0
     12// Last Modified On : Wed Oct  4 10:52:00 2023
     13// Update Count     : 1
    1414//
    1515
     
    2323
    2424class Declaration;
     25namespace ast {
     26        class TranslationUnit;
     27}
    2528
    2629namespace CodeGen {
    2730
    2831void translateLinkOnce( std::list< Declaration *> & translationUnit );
     32void translateLinkOnce( ast::TranslationUnit & translationUnit );
    2933/* Convert the cfa_linkonce attribute on top level declaration into
    3034 * a special section declaration (.gnu.linkonce) so that it may be defined
  • src/main.cc

    rcf34e82 r3cbe320  
    421421                DUMP( bboxp, std::move( transUnit ) );
    422422                PASS( "Box", GenPoly::box, transUnit );
     423                PASS( "Link-Once", CodeGen::translateLinkOnce, transUnit );
    423424
    424425                translationUnit = convert( std::move( transUnit ) );
    425 
    426                 PASS( "Link-Once", CodeGen::translateLinkOnce, translationUnit );
    427426
    428427                // Code has been lowered to C, now we can start generation.
Note: See TracChangeset for help on using the changeset viewer.