source: src/CodeGen/FixMain.cc@ 4852232

Last change on this file since 4852232 was 9939dc3, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Reduced the number of object files linked into the demangler. Some of the divisions are rather odd, Lvalue2 and FixMain2, but they should be a better base to work from. Also improved the calling of the impurity detector visitors slightly.

  • Property mode set to 100644
File size: 4.5 KB
Line 
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 "AST/Decl.hpp"
25#include "AST/Type.hpp"
26#include "Common/PassVisitor.h"
27#include "Common/SemanticError.h" // for SemanticError
28#include "CodeGen/GenType.h" // for GenType
29#include "SynTree/Declaration.h" // for FunctionDecl, operator<<
30#include "SynTree/Type.h" // for FunctionType
31#include "SymTab/Mangler.h"
32
33namespace CodeGen {
34
35namespace {
36
37struct 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
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
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;
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 ";
65 main_signature->mangleName = SymTab::Mangler::mangle(main_signature);
66
67 os << main_signature->get_scopedMangleName() << "(";
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;
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 }
82
83namespace {
84
85ObjectDecl * signedIntObj() {
86 return new ObjectDecl(
87 "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
88 new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr );
89}
90
91ObjectDecl * makeArgvObj() {
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
100std::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
107std::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
113std::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() );
117 main_type->get_parameters().push_back( makeArgvObj() );
118 return create_mangled_main_function_name( main_type );
119}
120
121bool 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
135} // namespace
136
137bool FixMain::isMain( FunctionDecl * decl ) {
138 if ( std::string("main") != decl->name ) {
139 return false;
140 }
141 return is_main( SymTab::Mangler::mangle( decl, true, true ) );
142}
143
144bool FixMain::isMain( const ast::FunctionDecl * decl ) {
145 if ( std::string("main") != decl->name ) {
146 return false;
147 }
148 return is_main( Mangle::mangle( decl, Mangle::Type ) );
149}
150
151};
Note: See TracBrowser for help on using the repository browser.