source: src/CodeGen/FixMain.cc @ f42fc13

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since f42fc13 was f42fc13, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Reorganize FixNames/FixMain? to avoid storing main_signature for so long.

  • Property mode set to 100644
File size: 4.4 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// FixMain.cc --
8//
9// Author           : Thierry Delisle
10// Created On       : Thr Jan 12 14:11:09 2017
11// Last Modified By :
12// Last Modified On :
13// Update Count     : 0
14//
15
16
17#include "FixMain.h"
18
19#include <cassert>                 // for assert, assertf
20#include <fstream>                 // for operator<<, basic_ostream::operator<<
21#include <list>                    // for list
22#include <string>                  // for operator<<
23
24#include "Common/PassVisitor.h"
25#include "Common/SemanticError.h"  // for SemanticError
26#include "CodeGen/GenType.h"       // for GenType
27#include "SynTree/Declaration.h"   // for FunctionDecl, operator<<
28#include "SynTree/Type.h"          // for FunctionType
29#include "SymTab/Mangler.h"
30
31namespace CodeGen {
32        bool FixMain::replace_main = false;
33        std::unique_ptr<FunctionDecl> FixMain::main_signature = nullptr;
34
35        template<typename container>
36        std::string genTypeAt(const container& p, size_t idx) {
37                return genType((*std::next(p.begin(), idx))->get_type(), "");
38        }
39
40        void FixMain::registerMain(FunctionDecl* functionDecl)
41        {
42                if(main_signature) {
43                        SemanticError(functionDecl, "Multiple definition of main routine\n");
44                }
45                main_signature.reset( functionDecl->clone() );
46        }
47
48        void FixMain::fix(std::ostream &os, const char* bootloader_filename) {
49                if( main_signature ) {
50                        os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";
51                        main_signature->mangleName = SymTab::Mangler::mangle(main_signature.get());
52
53                        os << main_signature->get_scopedMangleName() << "(";
54                        const auto& params = main_signature->get_functionType()->get_parameters();
55                        switch(params.size()) {
56                                case 3: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv, (" << genTypeAt(params, 2) << ")envp"; break;
57                                case 2: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv"; break;
58                                case 0: break;
59                                default : assert(false);
60                        }
61                        os << "); }\n";
62
63                        std::ifstream bootloader(bootloader_filename, std::ios::in);
64                        assertf( bootloader.is_open(), "cannot open bootloader.c\n" );
65                        os << bootloader.rdbuf();
66                }
67        }
68
69namespace {
70
71ObjectDecl * signedIntObj() {
72        return new ObjectDecl(
73                "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
74                new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr );
75}
76
77ObjectDecl * charStarObj() {
78        return new ObjectDecl(
79                "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
80                new PointerType( Type::Qualifiers(),
81                        new PointerType( Type::Qualifiers(),
82                                new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
83                nullptr );
84}
85
86std::string create_mangled_main_function_name( FunctionType * function_type ) {
87        std::unique_ptr<FunctionDecl> decl( new FunctionDecl(
88                "main", Type::StorageClasses(), LinkageSpec::Cforall,
89                function_type, nullptr ) );
90        return SymTab::Mangler::mangle( decl.get() );
91}
92
93std::string mangled_0_argument_main() {
94        FunctionType* main_type = new FunctionType( Type::Qualifiers(), true );
95        main_type->get_returnVals().push_back( signedIntObj() );
96        return create_mangled_main_function_name( main_type );
97}
98
99std::string mangled_2_argument_main() {
100        FunctionType* main_type = new FunctionType( Type::Qualifiers(), false );
101        main_type->get_returnVals().push_back( signedIntObj() );
102        main_type->get_parameters().push_back( signedIntObj() );
103        main_type->get_parameters().push_back( charStarObj() );
104        return create_mangled_main_function_name( main_type );
105}
106
107struct FindMainCore {
108        void previsit( FunctionDecl * decl ) {
109                if ( FixMain::isMain( decl ) ) {
110                        FixMain::registerMain( decl );
111                }
112        }
113};
114
115} // namespace
116
117bool FixMain::isMain( FunctionDecl * decl ) {
118        // This breaks if you move it out of the function.
119        static const std::string mangled_mains[] = {
120                mangled_0_argument_main(),
121                mangled_2_argument_main(),
122                //mangled_3_argument_main(),
123        };
124
125        if ( std::string("main") != decl->name ) {
126                return false;
127        }
128        auto mangled_name = SymTab::Mangler::mangle( decl, true, true );
129        for ( auto main_name : mangled_mains ) {
130                if ( main_name == mangled_name ) return true;
131        }
132        return false;
133}
134
135void FixMain::findMain( std::list< Declaration * > & translationUnit ) {
136        PassVisitor< FindMainCore > mainFinder;
137        acceptAll( translationUnit, mainFinder );
138}
139
140};
Note: See TracBrowser for help on using the repository browser.