| [13de47bc] | 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 | //
 | 
|---|
| [bf2438c] | 7 | // FixMain.cc --
 | 
|---|
| [13de47bc] | 8 | //
 | 
|---|
 | 9 | // Author           : Thierry Delisle
 | 
|---|
 | 10 | // Created On       : Thr Jan 12 14:11:09 2017
 | 
|---|
| [bf2438c] | 11 | // Last Modified By :
 | 
|---|
 | 12 | // Last Modified On :
 | 
|---|
| [13de47bc] | 13 | // Update Count     : 0
 | 
|---|
 | 14 | //
 | 
|---|
| [7cc2c8d] | 15 | 
 | 
|---|
| [13de47bc] | 16 | 
 | 
|---|
| [bf2438c] | 17 | #include "FixMain.h"
 | 
|---|
| [13de47bc] | 18 | 
 | 
|---|
| [bf2438c] | 19 | #include <cassert>                 // for assert, assertf
 | 
|---|
 | 20 | #include <fstream>                 // for operator<<, basic_ostream::operator<<
 | 
|---|
 | 21 | #include <list>                    // for list
 | 
|---|
 | 22 | #include <string>                  // for operator<<
 | 
|---|
| [13de47bc] | 23 | 
 | 
|---|
| [0c577f7] | 24 | #include "AST/Decl.hpp"
 | 
|---|
 | 25 | #include "AST/Type.hpp"
 | 
|---|
| [f42fc13] | 26 | #include "Common/PassVisitor.h"
 | 
|---|
| [bf2438c] | 27 | #include "Common/SemanticError.h"  // for SemanticError
 | 
|---|
| [1619acd] | 28 | #include "CodeGen/GenType.h"       // for GenType
 | 
|---|
| [bf2438c] | 29 | #include "SynTree/Declaration.h"   // for FunctionDecl, operator<<
 | 
|---|
 | 30 | #include "SynTree/Type.h"          // for FunctionType
 | 
|---|
| [16ba4a6f] | 31 | #include "SymTab/Mangler.h"
 | 
|---|
| [13de47bc] | 32 | 
 | 
|---|
 | 33 | namespace CodeGen {
 | 
|---|
| [8e48fca4] | 34 | 
 | 
|---|
 | 35 | namespace {
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | struct 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 | 
 | 
|---|
| [1619acd] | 52 |         template<typename container>
 | 
|---|
 | 53 |         std::string genTypeAt(const container& p, size_t idx) {
 | 
|---|
 | 54 |                 return genType((*std::next(p.begin(), idx))->get_type(), "");
 | 
|---|
 | 55 |         }
 | 
|---|
 | 56 | 
 | 
|---|
| [8e48fca4] | 57 |         void FixMain::fix( std::list< Declaration * > & translationUnit,
 | 
|---|
 | 58 |                         std::ostream &os, const char* bootloader_filename ) {
 | 
|---|
 | 59 |                 PassVisitor< FindMainCore > main_finder;
 | 
|---|
 | 60 |                 acceptAll( translationUnit, main_finder );
 | 
|---|
 | 61 |                 FunctionDecl * main_signature = main_finder.pass.main_signature;
 | 
|---|
| [13de47bc] | 62 | 
 | 
|---|
 | 63 |                 if( main_signature ) {
 | 
|---|
 | 64 |                         os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";
 | 
|---|
| [8e48fca4] | 65 |                         main_signature->mangleName = SymTab::Mangler::mangle(main_signature);
 | 
|---|
| [13de47bc] | 66 | 
 | 
|---|
 | 67 |                         os << main_signature->get_scopedMangleName() << "(";
 | 
|---|
| [1619acd] | 68 |                         const auto& params = main_signature->get_functionType()->get_parameters();
 | 
|---|
 | 69 |                         switch(params.size()) {
 | 
|---|
 | 70 |                                 case 3: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv, (" << genTypeAt(params, 2) << ")envp"; break;
 | 
|---|
 | 71 |                                 case 2: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv"; break;
 | 
|---|
| [13de47bc] | 72 |                                 case 0: break;
 | 
|---|
 | 73 |                                 default : assert(false);
 | 
|---|
 | 74 |                         }
 | 
|---|
 | 75 |                         os << "); }\n";
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |                         std::ifstream bootloader(bootloader_filename, std::ios::in);
 | 
|---|
 | 78 |                         assertf( bootloader.is_open(), "cannot open bootloader.c\n" );
 | 
|---|
 | 79 |                         os << bootloader.rdbuf();
 | 
|---|
 | 80 |                 }
 | 
|---|
 | 81 |         }
 | 
|---|
| [f42fc13] | 82 | 
 | 
|---|
 | 83 | namespace {
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 | ObjectDecl * signedIntObj() {
 | 
|---|
 | 86 |         return new ObjectDecl(
 | 
|---|
 | 87 |                 "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
 | 
|---|
 | 88 |                 new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr );
 | 
|---|
 | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
| [d8c4fab] | 91 | ObjectDecl * makeArgvObj() {
 | 
|---|
| [f42fc13] | 92 |         return new ObjectDecl(
 | 
|---|
 | 93 |                 "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
 | 
|---|
 | 94 |                 new PointerType( Type::Qualifiers(),
 | 
|---|
 | 95 |                         new PointerType( Type::Qualifiers(),
 | 
|---|
 | 96 |                                 new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
 | 
|---|
 | 97 |                 nullptr );
 | 
|---|
 | 98 | }
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 | std::string create_mangled_main_function_name( FunctionType * function_type ) {
 | 
|---|
 | 101 |         std::unique_ptr<FunctionDecl> decl( new FunctionDecl(
 | 
|---|
 | 102 |                 "main", Type::StorageClasses(), LinkageSpec::Cforall,
 | 
|---|
 | 103 |                 function_type, nullptr ) );
 | 
|---|
 | 104 |         return SymTab::Mangler::mangle( decl.get() );
 | 
|---|
 | 105 | }
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 | std::string mangled_0_argument_main() {
 | 
|---|
 | 108 |         FunctionType* main_type = new FunctionType( Type::Qualifiers(), true );
 | 
|---|
 | 109 |         main_type->get_returnVals().push_back( signedIntObj() );
 | 
|---|
 | 110 |         return create_mangled_main_function_name( main_type );
 | 
|---|
 | 111 | }
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 | std::string mangled_2_argument_main() {
 | 
|---|
 | 114 |         FunctionType* main_type = new FunctionType( Type::Qualifiers(), false );
 | 
|---|
 | 115 |         main_type->get_returnVals().push_back( signedIntObj() );
 | 
|---|
 | 116 |         main_type->get_parameters().push_back( signedIntObj() );
 | 
|---|
| [d8c4fab] | 117 |         main_type->get_parameters().push_back( makeArgvObj() );
 | 
|---|
| [f42fc13] | 118 |         return create_mangled_main_function_name( main_type );
 | 
|---|
 | 119 | }
 | 
|---|
 | 120 | 
 | 
|---|
| [0c577f7] | 121 | bool is_main( const std::string & mangled_name ) {
 | 
|---|
 | 122 |         // This breaks if you move it out of the function.
 | 
|---|
 | 123 |         static const std::string mangled_mains[] = {
 | 
|---|
 | 124 |                 mangled_0_argument_main(),
 | 
|---|
 | 125 |                 mangled_2_argument_main(),
 | 
|---|
 | 126 |                 //mangled_3_argument_main(),
 | 
|---|
 | 127 |         };
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |         for ( auto main_name : mangled_mains ) {
 | 
|---|
 | 130 |                 if ( main_name == mangled_name ) return true;
 | 
|---|
 | 131 |         }
 | 
|---|
 | 132 |         return false;
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
| [f42fc13] | 135 | } // namespace
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 | bool FixMain::isMain( FunctionDecl * decl ) {
 | 
|---|
 | 138 |         if ( std::string("main") != decl->name ) {
 | 
|---|
 | 139 |                 return false;
 | 
|---|
 | 140 |         }
 | 
|---|
| [0c577f7] | 141 |         return is_main( SymTab::Mangler::mangle( decl, true, true ) );
 | 
|---|
 | 142 | }
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | bool FixMain::isMain( const ast::FunctionDecl * decl ) {
 | 
|---|
 | 145 |         if ( std::string("main") != decl->name ) {
 | 
|---|
 | 146 |                 return false;
 | 
|---|
| [f42fc13] | 147 |         }
 | 
|---|
| [0c577f7] | 148 |         return is_main( Mangle::mangle( decl, Mangle::Type ) );
 | 
|---|
| [f42fc13] | 149 | }
 | 
|---|
 | 150 | 
 | 
|---|
| [bf2438c] | 151 | };
 | 
|---|