| [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 |  | 
|---|
| [bf2438c] | 24 | #include "Common/SemanticError.h"  // for SemanticError | 
|---|
|  | 25 | #include "SynTree/Declaration.h"   // for FunctionDecl, operator<< | 
|---|
|  | 26 | #include "SynTree/Type.h"          // for FunctionType | 
|---|
| [13de47bc] | 27 |  | 
|---|
|  | 28 | namespace CodeGen { | 
|---|
|  | 29 | bool FixMain::replace_main = false; | 
|---|
|  | 30 | std::unique_ptr<FunctionDecl> FixMain::main_signature = nullptr; | 
|---|
| [bf2438c] | 31 |  | 
|---|
|  | 32 | void FixMain::registerMain(FunctionDecl* functionDecl) | 
|---|
| [13de47bc] | 33 | { | 
|---|
| [bf2438c] | 34 | if(main_signature) { | 
|---|
|  | 35 | throw SemanticError("Multiple definition of main routine\n", functionDecl); | 
|---|
| [13de47bc] | 36 | } | 
|---|
|  | 37 | main_signature.reset( functionDecl->clone() ); | 
|---|
|  | 38 | } | 
|---|
|  | 39 |  | 
|---|
|  | 40 | void FixMain::fix(std::ostream &os, const char* bootloader_filename) { | 
|---|
|  | 41 | if( main_signature ) { | 
|---|
|  | 42 | os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return "; | 
|---|
|  | 43 |  | 
|---|
|  | 44 | os << main_signature->get_scopedMangleName() << "("; | 
|---|
|  | 45 | switch(main_signature->get_functionType()->get_parameters().size()) { | 
|---|
|  | 46 | case 3: os << "argc, argv, envp"; break; | 
|---|
|  | 47 | case 2: os << "argc, argv"; break; | 
|---|
|  | 48 | case 0: break; | 
|---|
|  | 49 | default : assert(false); | 
|---|
|  | 50 | } | 
|---|
|  | 51 | os << "); }\n"; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | std::ifstream bootloader(bootloader_filename, std::ios::in); | 
|---|
|  | 54 | assertf( bootloader.is_open(), "cannot open bootloader.c\n" ); | 
|---|
|  | 55 | os << bootloader.rdbuf(); | 
|---|
|  | 56 | } | 
|---|
|  | 57 | } | 
|---|
| [bf2438c] | 58 | }; | 
|---|