source: src/CodeGen/FixMain.cc @ d8c4fab

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since d8c4fab was d8c4fab, checked in by JiadaL <j82liang@…>, 2 years ago

Rename makeArgvObj

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