[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 | //
|
---|
[c92bdcc] | 7 | // FixMain.cpp -- Tools to change a Cforall main into a C main.
|
---|
[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 :
|
---|
[ca9d65e] | 13 | // Update Count : 1
|
---|
[13de47bc] | 14 | //
|
---|
[7cc2c8d] | 15 |
|
---|
[c92bdcc] | 16 | #include "FixMain.hpp"
|
---|
[13de47bc] | 17 |
|
---|
[c92bdcc] | 18 | #include <cassert> // for assert, assertf
|
---|
| 19 | #include <fstream> // for operator<<, basic_ostream::oper...
|
---|
| 20 | #include <list> // for list
|
---|
| 21 | #include <string> // for operator<<
|
---|
[13de47bc] | 22 |
|
---|
[0c577f7] | 23 | #include "AST/Decl.hpp"
|
---|
[bef4f1a] | 24 | #include "AST/Pass.hpp"
|
---|
[0c577f7] | 25 | #include "AST/Type.hpp"
|
---|
[bef4f1a] | 26 | #include "AST/Vector.hpp"
|
---|
[c92bdcc] | 27 | #include "Common/SemanticError.hpp" // for SemanticError
|
---|
| 28 | #include "CodeGen/GenType.hpp" // for GenType
|
---|
| 29 | #include "SymTab/Mangler.hpp"
|
---|
[13de47bc] | 30 |
|
---|
| 31 | namespace CodeGen {
|
---|
[8e48fca4] | 32 |
|
---|
| 33 | namespace {
|
---|
| 34 |
|
---|
[61efa42] | 35 | struct FindMainCore final {
|
---|
[bef4f1a] | 36 | ast::FunctionDecl const * main_declaration = nullptr;
|
---|
| 37 |
|
---|
| 38 | void previsit( ast::FunctionDecl const * decl ) {
|
---|
[61efa42] | 39 | if ( isMain( decl ) ) {
|
---|
[bef4f1a] | 40 | if ( main_declaration ) {
|
---|
[ca9d65e] | 41 | SemanticError( decl, "Multiple definition of main routine" );
|
---|
[bef4f1a] | 42 | }
|
---|
| 43 | main_declaration = decl;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | std::string genTypeAt( const ast::vector<ast::Type> & types, size_t at ) {
|
---|
| 49 | return genType( types[at], "", Options( false, false, false, false ) );
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | ast::ObjectDecl * makeIntObj(){
|
---|
| 53 | return new ast::ObjectDecl( CodeLocation(), "",
|
---|
[7a780ad] | 54 | new ast::BasicType( ast::BasicKind::SignedInt ) );
|
---|
[f42fc13] | 55 | }
|
---|
| 56 |
|
---|
[bef4f1a] | 57 | ast::ObjectDecl * makeCharStarStarObj() {
|
---|
| 58 | return new ast::ObjectDecl( CodeLocation(), "",
|
---|
| 59 | new ast::PointerType(
|
---|
| 60 | new ast::PointerType(
|
---|
[7a780ad] | 61 | new ast::BasicType( ast::BasicKind::Char ) ) ) );
|
---|
[f42fc13] | 62 | }
|
---|
| 63 |
|
---|
[bef4f1a] | 64 | std::string getMangledNameOfMain(
|
---|
| 65 | ast::vector<ast::DeclWithType> && params, ast::ArgumentFlag isVarArgs ) {
|
---|
| 66 | ast::ptr<ast::FunctionDecl> decl = new ast::FunctionDecl(
|
---|
| 67 | CodeLocation(),
|
---|
| 68 | "main",
|
---|
| 69 | ast::vector<ast::TypeDecl>(),
|
---|
| 70 | ast::vector<ast::DeclWithType>(),
|
---|
| 71 | std::move( params ),
|
---|
| 72 | { makeIntObj() },
|
---|
| 73 | nullptr,
|
---|
| 74 | ast::Storage::Classes(),
|
---|
| 75 | ast::Linkage::Spec(),
|
---|
| 76 | ast::vector<ast::Attribute>(),
|
---|
| 77 | ast::Function::Specs(),
|
---|
| 78 | isVarArgs
|
---|
| 79 | );
|
---|
| 80 | return Mangle::mangle( decl.get() );
|
---|
[f42fc13] | 81 | }
|
---|
| 82 |
|
---|
[bef4f1a] | 83 | std::string getMangledNameOf0ParameterMain() {
|
---|
| 84 | return getMangledNameOfMain( {}, ast::VariableArgs );
|
---|
[f42fc13] | 85 | }
|
---|
| 86 |
|
---|
[bef4f1a] | 87 | std::string getMangledNameOf2ParameterMain() {
|
---|
| 88 | return getMangledNameOfMain( {
|
---|
| 89 | makeIntObj(),
|
---|
| 90 | makeCharStarStarObj(),
|
---|
| 91 | }, ast::FixedArgs );
|
---|
[f42fc13] | 92 | }
|
---|
| 93 |
|
---|
[0c577f7] | 94 | bool is_main( const std::string & mangled_name ) {
|
---|
| 95 | // This breaks if you move it out of the function.
|
---|
| 96 | static const std::string mangled_mains[] = {
|
---|
[bef4f1a] | 97 | getMangledNameOf0ParameterMain(),
|
---|
| 98 | getMangledNameOf2ParameterMain(),
|
---|
| 99 | //getMangledNameOf3ParameterMain(),
|
---|
[0c577f7] | 100 | };
|
---|
| 101 |
|
---|
| 102 | for ( auto main_name : mangled_mains ) {
|
---|
| 103 | if ( main_name == mangled_name ) return true;
|
---|
| 104 | }
|
---|
| 105 | return false;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[61efa42] | 108 | struct FixLinkageCore final {
|
---|
| 109 | ast::Linkage::Spec const spec;
|
---|
| 110 | FixLinkageCore( ast::Linkage::Spec spec ) : spec( spec ) {}
|
---|
| 111 |
|
---|
| 112 | ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ) {
|
---|
| 113 | if ( decl->name != "main" ) return decl;
|
---|
| 114 | return ast::mutate_field( decl, &ast::FunctionDecl::linkage, spec );
|
---|
| 115 | }
|
---|
| 116 | };
|
---|
| 117 |
|
---|
[f42fc13] | 118 | } // namespace
|
---|
| 119 |
|
---|
[61efa42] | 120 | bool isMain( const ast::FunctionDecl * decl ) {
|
---|
[0c577f7] | 121 | if ( std::string("main") != decl->name ) {
|
---|
| 122 | return false;
|
---|
[f42fc13] | 123 | }
|
---|
[0c577f7] | 124 | return is_main( Mangle::mangle( decl, Mangle::Type ) );
|
---|
[f42fc13] | 125 | }
|
---|
| 126 |
|
---|
[61efa42] | 127 | void fixMainLinkage( ast::TranslationUnit & translationUnit,
|
---|
| 128 | bool replace_main ) {
|
---|
| 129 | ast::Linkage::Spec const spec =
|
---|
| 130 | ( replace_main ) ? ast::Linkage::Cforall : ast::Linkage::C;
|
---|
| 131 | ast::Pass<FixLinkageCore>::run( translationUnit, spec );
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void fixMainInvoke( ast::TranslationUnit & translationUnit,
|
---|
[bef4f1a] | 135 | std::ostream &os, const char * bootloader_filename ) {
|
---|
| 136 |
|
---|
[61efa42] | 137 | ast::Pass<FindMainCore> main_finder;
|
---|
[bef4f1a] | 138 | ast::accept_all( translationUnit, main_finder );
|
---|
| 139 | if ( nullptr == main_finder.core.main_declaration ) return;
|
---|
| 140 |
|
---|
| 141 | ast::FunctionDecl * main_declaration =
|
---|
| 142 | ast::mutate( main_finder.core.main_declaration );
|
---|
| 143 |
|
---|
| 144 | main_declaration->mangleName = Mangle::mangle( main_declaration );
|
---|
| 145 |
|
---|
| 146 | os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";
|
---|
| 147 | os << main_declaration->scopedMangleName() << "(";
|
---|
| 148 | const auto& params = main_declaration->type->params;
|
---|
| 149 | switch ( params.size() ) {
|
---|
| 150 | case 3: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv, (" << genTypeAt(params, 2) << ")envp"; break;
|
---|
| 151 | case 2: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv"; break;
|
---|
| 152 | case 0: break;
|
---|
| 153 | default : assert(false);
|
---|
| 154 | }
|
---|
| 155 | os << "); }\n";
|
---|
| 156 |
|
---|
| 157 | std::ifstream bootloader( bootloader_filename, std::ios::in );
|
---|
| 158 | assertf( bootloader.is_open(), "cannot open bootloader.c\n" );
|
---|
| 159 | os << bootloader.rdbuf();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | } // namespace CodeGen
|
---|