source: src/CodeGen/FixMain.cc@ 0c577f7

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 0c577f7 was 0c577f7, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Implemented new AST version of the Fix Names pass.

  • Property mode set to 100644
File size: 4.6 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 bool FixMain::replace_main = false;
35 std::unique_ptr<FunctionDecl> FixMain::main_signature = nullptr;
36
37 template<typename container>
38 std::string genTypeAt(const container& p, size_t idx) {
39 return genType((*std::next(p.begin(), idx))->get_type(), "");
40 }
41
42 void FixMain::registerMain(FunctionDecl* functionDecl)
43 {
44 if(main_signature) {
45 SemanticError(functionDecl, "Multiple definition of main routine\n");
46 }
47 main_signature.reset( functionDecl->clone() );
48 }
49
50 void FixMain::fix(std::ostream &os, const char* bootloader_filename) {
51 if( main_signature ) {
52 os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";
53 main_signature->mangleName = SymTab::Mangler::mangle(main_signature.get());
54
55 os << main_signature->get_scopedMangleName() << "(";
56 const auto& params = main_signature->get_functionType()->get_parameters();
57 switch(params.size()) {
58 case 3: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv, (" << genTypeAt(params, 2) << ")envp"; break;
59 case 2: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv"; break;
60 case 0: break;
61 default : assert(false);
62 }
63 os << "); }\n";
64
65 std::ifstream bootloader(bootloader_filename, std::ios::in);
66 assertf( bootloader.is_open(), "cannot open bootloader.c\n" );
67 os << bootloader.rdbuf();
68 }
69 }
70
71namespace {
72
73ObjectDecl * signedIntObj() {
74 return new ObjectDecl(
75 "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
76 new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr );
77}
78
79ObjectDecl * charStarObj() {
80 return new ObjectDecl(
81 "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
82 new PointerType( Type::Qualifiers(),
83 new PointerType( Type::Qualifiers(),
84 new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
85 nullptr );
86}
87
88std::string create_mangled_main_function_name( FunctionType * function_type ) {
89 std::unique_ptr<FunctionDecl> decl( new FunctionDecl(
90 "main", Type::StorageClasses(), LinkageSpec::Cforall,
91 function_type, nullptr ) );
92 return SymTab::Mangler::mangle( decl.get() );
93}
94
95std::string mangled_0_argument_main() {
96 FunctionType* main_type = new FunctionType( Type::Qualifiers(), true );
97 main_type->get_returnVals().push_back( signedIntObj() );
98 return create_mangled_main_function_name( main_type );
99}
100
101std::string mangled_2_argument_main() {
102 FunctionType* main_type = new FunctionType( Type::Qualifiers(), false );
103 main_type->get_returnVals().push_back( signedIntObj() );
104 main_type->get_parameters().push_back( signedIntObj() );
105 main_type->get_parameters().push_back( charStarObj() );
106 return create_mangled_main_function_name( main_type );
107}
108
109bool is_main( const std::string & mangled_name ) {
110 // This breaks if you move it out of the function.
111 static const std::string mangled_mains[] = {
112 mangled_0_argument_main(),
113 mangled_2_argument_main(),
114 //mangled_3_argument_main(),
115 };
116
117 for ( auto main_name : mangled_mains ) {
118 if ( main_name == mangled_name ) return true;
119 }
120 return false;
121}
122
123struct FindMainCore {
124 void previsit( FunctionDecl * decl ) {
125 if ( FixMain::isMain( decl ) ) {
126 FixMain::registerMain( decl );
127 }
128 }
129};
130
131} // namespace
132
133bool FixMain::isMain( FunctionDecl * decl ) {
134 if ( std::string("main") != decl->name ) {
135 return false;
136 }
137 return is_main( SymTab::Mangler::mangle( decl, true, true ) );
138}
139
140bool FixMain::isMain( const ast::FunctionDecl * decl ) {
141 if ( std::string("main") != decl->name ) {
142 return false;
143 }
144 return is_main( Mangle::mangle( decl, Mangle::Type ) );
145}
146
147void FixMain::findMain( std::list< Declaration * > & translationUnit ) {
148 PassVisitor< FindMainCore > mainFinder;
149 acceptAll( translationUnit, mainFinder );
150}
151
152};
Note: See TracBrowser for help on using the repository browser.