[b87a5ed] | 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 | // |
---|
[974906e2] | 7 | // MakeLibCfa.cc -- |
---|
[b87a5ed] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sat May 16 10:33:33 2015 |
---|
[2794fff] | 11 | // Last Modified By : Rob Schluntz |
---|
[845cedc] | 12 | // Last Modified On : Fri Apr 22 13:54:15 2016 |
---|
[f1e012b] | 13 | // Update Count : 40 |
---|
[974906e2] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "MakeLibCfa.h" |
---|
[bf2438c] | 17 | |
---|
| 18 | #include <cassert> // for assert |
---|
| 19 | #include <string> // for operator==, string |
---|
| 20 | |
---|
| 21 | #include "CodeGen/OperatorTable.h" // for OperatorInfo, operatorLookup, Ope... |
---|
| 22 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 23 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 24 | #include "Parser/LinkageSpec.h" // for Spec, Intrinsic, C |
---|
| 25 | #include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declara... |
---|
| 26 | #include "SynTree/Expression.h" // for NameExpr, UntypedExpr, VariableExpr |
---|
| 27 | #include "SynTree/Initializer.h" // for SingleInit |
---|
| 28 | #include "SynTree/Label.h" // for Label |
---|
| 29 | #include "SynTree/Statement.h" // for CompoundStmt, ReturnStmt |
---|
| 30 | #include "SynTree/Type.h" // for FunctionType |
---|
| 31 | #include "SynTree/Visitor.h" // for acceptAll, Visitor |
---|
[51b7345] | 32 | |
---|
| 33 | namespace LibCfa { |
---|
[b87a5ed] | 34 | class MakeLibCfa : public Visitor { |
---|
| 35 | public: |
---|
| 36 | void visit( FunctionDecl* funcDecl ); |
---|
| 37 | void visit( ObjectDecl* objDecl ); |
---|
[974906e2] | 38 | |
---|
[b87a5ed] | 39 | std::list< Declaration* > &get_newDecls() { return newDecls; } |
---|
| 40 | private: |
---|
| 41 | std::list< Declaration* > newDecls; |
---|
| 42 | }; |
---|
[51b7345] | 43 | |
---|
[b87a5ed] | 44 | void makeLibCfa( std::list< Declaration* > &prelude ) { |
---|
| 45 | MakeLibCfa maker; |
---|
| 46 | acceptAll( prelude, maker ); |
---|
| 47 | prelude.splice( prelude.end(), maker.get_newDecls() ); |
---|
| 48 | } |
---|
[51b7345] | 49 | |
---|
[b87a5ed] | 50 | void MakeLibCfa::visit( FunctionDecl* origFuncDecl ) { |
---|
| 51 | if ( origFuncDecl->get_linkage() != LinkageSpec::Intrinsic ) return; |
---|
[845cedc] | 52 | if ( origFuncDecl->get_statements() ) return; |
---|
[974906e2] | 53 | |
---|
[b87a5ed] | 54 | FunctionDecl *funcDecl = origFuncDecl->clone(); |
---|
| 55 | CodeGen::OperatorInfo opInfo; |
---|
| 56 | bool lookResult = CodeGen::operatorLookup( funcDecl->get_name(), opInfo ); |
---|
| 57 | assert( lookResult ); |
---|
[a32b204] | 58 | assert( ! funcDecl->get_statements() ); |
---|
[b87a5ed] | 59 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( funcDecl->get_name() ) ); |
---|
| 60 | UniqueName paramNamer( "_p" ); |
---|
| 61 | std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); |
---|
| 62 | assert( param != funcDecl->get_functionType()->get_parameters().end() ); |
---|
[51b7345] | 63 | |
---|
[f1e012b] | 64 | for ( ; param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { |
---|
| 65 | if ( (*param)->get_name() == "" ) { |
---|
| 66 | (*param)->set_name( paramNamer.newName() ); |
---|
| 67 | (*param)->set_linkage( LinkageSpec::C ); |
---|
| 68 | } |
---|
| 69 | newExpr->get_args().push_back( new VariableExpr( *param ) ); |
---|
| 70 | } // for |
---|
| 71 | |
---|
| 72 | funcDecl->set_statements( new CompoundStmt( std::list< Label >() ) ); |
---|
| 73 | newDecls.push_back( funcDecl ); |
---|
[b87a5ed] | 74 | |
---|
[a32b204] | 75 | switch ( opInfo.type ) { |
---|
[b87a5ed] | 76 | case CodeGen::OT_INDEX: |
---|
| 77 | case CodeGen::OT_CALL: |
---|
| 78 | case CodeGen::OT_PREFIX: |
---|
| 79 | case CodeGen::OT_POSTFIX: |
---|
| 80 | case CodeGen::OT_INFIX: |
---|
| 81 | case CodeGen::OT_PREFIXASSIGN: |
---|
| 82 | case CodeGen::OT_POSTFIXASSIGN: |
---|
| 83 | case CodeGen::OT_INFIXASSIGN: |
---|
[2794fff] | 84 | case CodeGen::OT_CTOR: |
---|
| 85 | case CodeGen::OT_DTOR: |
---|
[0b150ec] | 86 | funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( std::list< Label >(), newExpr ) ); |
---|
| 87 | break; |
---|
[b87a5ed] | 88 | case CodeGen::OT_CONSTANT: |
---|
[721f17a] | 89 | case CodeGen::OT_LABELADDRESS: |
---|
| 90 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[b87a5ed] | 91 | assert( false ); |
---|
| 92 | } // switch |
---|
| 93 | } |
---|
[51b7345] | 94 | |
---|
[b87a5ed] | 95 | void MakeLibCfa::visit( ObjectDecl* origObjDecl ) { |
---|
| 96 | if ( origObjDecl->get_linkage() != LinkageSpec::Intrinsic ) return; |
---|
[974906e2] | 97 | |
---|
[b87a5ed] | 98 | ObjectDecl *objDecl = origObjDecl->clone(); |
---|
[a32b204] | 99 | assert( ! objDecl->get_init() ); |
---|
[b87a5ed] | 100 | std::list< Expression* > noDesignators; |
---|
[e4d829b] | 101 | objDecl->set_init( new SingleInit( new NameExpr( objDecl->get_name() ), false ) ); // cannot be constructed |
---|
[b87a5ed] | 102 | newDecls.push_back( objDecl ); |
---|
| 103 | } |
---|
[51b7345] | 104 | } // namespace LibCfa |
---|