source: src/CodeGen/FixMain.cc @ 1619acd

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 1619acd was 1619acd, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

[fixes #72] only fixes the cast problem

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