| 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/SemanticError.h"  // for SemanticError
 | 
|---|
| 25 | #include "CodeGen/GenType.h"       // for GenType
 | 
|---|
| 26 | #include "SynTree/Declaration.h"   // for FunctionDecl, operator<<
 | 
|---|
| 27 | #include "SynTree/Type.h"          // for FunctionType
 | 
|---|
| 28 | #include "SymTab/Mangler.h"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | namespace CodeGen {
 | 
|---|
| 31 |         bool FixMain::replace_main = false;
 | 
|---|
| 32 |         std::unique_ptr<FunctionDecl> FixMain::main_signature = nullptr;
 | 
|---|
| 33 | 
 | 
|---|
| 34 |         template<typename container>
 | 
|---|
| 35 |         std::string genTypeAt(const container& p, size_t idx) {
 | 
|---|
| 36 |                 return genType((*std::next(p.begin(), idx))->get_type(), "");
 | 
|---|
| 37 |         }
 | 
|---|
| 38 | 
 | 
|---|
| 39 |         void FixMain::registerMain(FunctionDecl* functionDecl)
 | 
|---|
| 40 |         {
 | 
|---|
| 41 |                 if(main_signature) {
 | 
|---|
| 42 |                         SemanticError(functionDecl, "Multiple definition of main routine\n");
 | 
|---|
| 43 |                 }
 | 
|---|
| 44 |                 main_signature.reset( functionDecl->clone() );
 | 
|---|
| 45 |         }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         void FixMain::fix(std::ostream &os, const char* bootloader_filename) {
 | 
|---|
| 48 |                 if( main_signature ) {
 | 
|---|
| 49 |                         os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";
 | 
|---|
| 50 |                         main_signature->mangleName = SymTab::Mangler::mangle(main_signature.get());
 | 
|---|
| 51 | 
 | 
|---|
| 52 |                         os << main_signature->get_scopedMangleName() << "(";
 | 
|---|
| 53 |                         const auto& params = main_signature->get_functionType()->get_parameters();
 | 
|---|
| 54 |                         switch(params.size()) {
 | 
|---|
| 55 |                                 case 3: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv, (" << genTypeAt(params, 2) << ")envp"; break;
 | 
|---|
| 56 |                                 case 2: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv"; break;
 | 
|---|
| 57 |                                 case 0: break;
 | 
|---|
| 58 |                                 default : assert(false);
 | 
|---|
| 59 |                         }
 | 
|---|
| 60 |                         os << "); }\n";
 | 
|---|
| 61 | 
 | 
|---|
| 62 |                         std::ifstream bootloader(bootloader_filename, std::ios::in);
 | 
|---|
| 63 |                         assertf( bootloader.is_open(), "cannot open bootloader.c\n" );
 | 
|---|
| 64 |                         os << bootloader.rdbuf();
 | 
|---|
| 65 |                 }
 | 
|---|
| 66 |         }
 | 
|---|
| 67 | };
 | 
|---|