source: src/CodeGen/FixNames.cc@ 0270824

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 0270824 was 0270824, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Replace user main with custom main, prototype

  • Property mode set to 100644
File size: 4.1 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// FixNames.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Mon Apr 11 15:38:10 2016
13// Update Count : 1
14//
15
16#include <memory>
17
18#include "FixNames.h"
19#include "SynTree/Declaration.h"
20#include "SynTree/Expression.h"
21#include "SynTree/Visitor.h"
22#include "SymTab/Mangler.h"
23#include "OperatorTable.h"
24
25extern std::unique_ptr<FunctionDecl> translation_unit_main_signature;
26
27namespace CodeGen {
28 class FixNames : public Visitor {
29 public:
30 virtual void visit( ObjectDecl *objectDecl );
31 virtual void visit( FunctionDecl *functionDecl );
32
33 virtual void visit( CompoundStmt *compoundStmt );
34 private:
35 int scopeLevel = 1;
36
37 void fixDWT( DeclarationWithType *dwt );
38 };
39
40 std::string mangle_main() {
41 FunctionType* main_type;
42 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
43 "main",
44 DeclarationNode::NoStorageClass,
45 LinkageSpec::Cforall,
46 main_type = new FunctionType( Type::Qualifiers(), true ),
47 nullptr, false, false
48 ) };
49 main_type->get_returnVals().push_back(
50 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
51 );
52
53 auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
54 // std::cerr << name << std::endl;
55 return name;
56 }
57 std::string mangle_main_args() {
58 FunctionType* main_type;
59 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
60 "main",
61 DeclarationNode::NoStorageClass,
62 LinkageSpec::Cforall,
63 main_type = new FunctionType( Type::Qualifiers(), false ),
64 nullptr, false, false
65 ) };
66 main_type->get_returnVals().push_back(
67 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
68 );
69
70 mainDecl->get_functionType()->get_parameters().push_back(
71 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
72 );
73
74 mainDecl->get_functionType()->get_parameters().push_back(
75 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0,
76 new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
77 nullptr )
78 );
79
80 auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
81 // std::cerr << name << std::endl;
82 return name;
83 }
84
85 bool is_main(const std::string& name) {
86 static std::string mains[] = {
87 mangle_main(),
88 mangle_main_args()
89 };
90
91 for(const auto& m : mains) {
92 if( name == m ) return true;
93 }
94 return false;
95 }
96
97 void fixNames( std::list< Declaration* > translationUnit ) {
98 FixNames fixer;
99 acceptAll( translationUnit, fixer );
100 }
101
102 void FixNames::fixDWT( DeclarationWithType *dwt ) {
103 if ( dwt->get_name() != "" ) {
104 if ( LinkageSpec::isDecoratable( dwt->get_linkage() ) ) {
105 dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
106 dwt->set_scopeLevel( scopeLevel );
107 } // if
108 } // if
109 }
110
111 void FixNames::visit( ObjectDecl *objectDecl ) {
112 Visitor::visit( objectDecl );
113 fixDWT( objectDecl );
114 }
115
116 void FixNames::visit( FunctionDecl *functionDecl ) {
117 Visitor::visit( functionDecl );
118 fixDWT( functionDecl );
119
120 if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
121 if(translation_unit_main_signature) {
122 throw SemanticError("Multiple definition of main routine\n", functionDecl);
123 }
124
125 functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) );
126 translation_unit_main_signature.reset( functionDecl->clone() );
127 }
128 }
129
130 void FixNames::visit( CompoundStmt *compoundStmt ) {
131 scopeLevel++;
132 Visitor::visit( compoundStmt );
133 scopeLevel--;
134 }
135} // namespace CodeGen
136
137// Local Variables: //
138// tab-width: 4 //
139// mode: c++ //
140// compile-command: "make install" //
141// End: //
Note: See TracBrowser for help on using the repository browser.