| [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 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
|  | 16 | #include "MakeLibCfa.h" | 
|---|
| [b87a5ed] | 17 | #include "SynTree/Visitor.h" | 
|---|
| [51b73452] | 18 | #include "SynTree/Declaration.h" | 
|---|
|  | 19 | #include "SynTree/Type.h" | 
|---|
|  | 20 | #include "SynTree/Expression.h" | 
|---|
|  | 21 | #include "SynTree/Statement.h" | 
|---|
|  | 22 | #include "SynTree/Initializer.h" | 
|---|
|  | 23 | #include "CodeGen/OperatorTable.h" | 
|---|
| [d3b7937] | 24 | #include "Common/UniqueName.h" | 
|---|
| [51b73452] | 25 |  | 
|---|
|  | 26 | namespace LibCfa { | 
|---|
| [b87a5ed] | 27 | class MakeLibCfa : public Visitor { | 
|---|
|  | 28 | public: | 
|---|
|  | 29 | void visit( FunctionDecl* funcDecl ); | 
|---|
|  | 30 | void visit( ObjectDecl* objDecl ); | 
|---|
| [974906e2] | 31 |  | 
|---|
| [b87a5ed] | 32 | std::list< Declaration* > &get_newDecls() { return newDecls; } | 
|---|
|  | 33 | private: | 
|---|
|  | 34 | std::list< Declaration* > newDecls; | 
|---|
|  | 35 | }; | 
|---|
| [51b73452] | 36 |  | 
|---|
| [b87a5ed] | 37 | void makeLibCfa( std::list< Declaration* > &prelude ) { | 
|---|
|  | 38 | MakeLibCfa maker; | 
|---|
|  | 39 | acceptAll( prelude, maker ); | 
|---|
|  | 40 | prelude.splice( prelude.end(), maker.get_newDecls() ); | 
|---|
|  | 41 | } | 
|---|
| [51b73452] | 42 |  | 
|---|
| [b87a5ed] | 43 | void MakeLibCfa::visit( FunctionDecl* origFuncDecl ) { | 
|---|
|  | 44 | if ( origFuncDecl->get_linkage() != LinkageSpec::Intrinsic ) return; | 
|---|
| [845cedc] | 45 | if ( origFuncDecl->get_statements() ) return; | 
|---|
| [974906e2] | 46 |  | 
|---|
| [b87a5ed] | 47 | FunctionDecl *funcDecl = origFuncDecl->clone(); | 
|---|
|  | 48 | CodeGen::OperatorInfo opInfo; | 
|---|
|  | 49 | bool lookResult = CodeGen::operatorLookup( funcDecl->get_name(), opInfo ); | 
|---|
|  | 50 | assert( lookResult ); | 
|---|
| [a32b204] | 51 | assert( ! funcDecl->get_statements() ); | 
|---|
| [b87a5ed] | 52 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( funcDecl->get_name() ) ); | 
|---|
|  | 53 | UniqueName paramNamer( "_p" ); | 
|---|
|  | 54 | std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); | 
|---|
|  | 55 | assert( param != funcDecl->get_functionType()->get_parameters().end() ); | 
|---|
| [51b73452] | 56 |  | 
|---|
| [f1e012b] | 57 | for ( ; param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { | 
|---|
|  | 58 | if ( (*param)->get_name() == "" ) { | 
|---|
|  | 59 | (*param)->set_name( paramNamer.newName() ); | 
|---|
|  | 60 | (*param)->set_linkage( LinkageSpec::C ); | 
|---|
|  | 61 | } | 
|---|
|  | 62 | newExpr->get_args().push_back( new VariableExpr( *param ) ); | 
|---|
|  | 63 | } // for | 
|---|
|  | 64 |  | 
|---|
|  | 65 | funcDecl->set_statements( new CompoundStmt( std::list< Label >() ) ); | 
|---|
|  | 66 | newDecls.push_back( funcDecl ); | 
|---|
| [b87a5ed] | 67 |  | 
|---|
| [a32b204] | 68 | switch ( opInfo.type ) { | 
|---|
| [b87a5ed] | 69 | case CodeGen::OT_INDEX: | 
|---|
|  | 70 | case CodeGen::OT_CALL: | 
|---|
|  | 71 | case CodeGen::OT_PREFIX: | 
|---|
|  | 72 | case CodeGen::OT_POSTFIX: | 
|---|
|  | 73 | case CodeGen::OT_INFIX: | 
|---|
|  | 74 | case CodeGen::OT_PREFIXASSIGN: | 
|---|
|  | 75 | case CodeGen::OT_POSTFIXASSIGN: | 
|---|
|  | 76 | case CodeGen::OT_INFIXASSIGN: | 
|---|
| [f1e012b] | 77 | funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( std::list< Label >(), newExpr ) ); | 
|---|
| [b87a5ed] | 78 | break; | 
|---|
| [2794fff] | 79 | case CodeGen::OT_CTOR: | 
|---|
| [f1e012b] | 80 | // ctors don't return a value | 
|---|
|  | 81 | if ( funcDecl->get_functionType()->get_parameters().size() == 1 ) { | 
|---|
|  | 82 | // intrinsic default constructors should do nothing | 
|---|
|  | 83 | // delete newExpr; | 
|---|
|  | 84 | break; | 
|---|
|  | 85 | } else { | 
|---|
|  | 86 | assert( funcDecl->get_functionType()->get_parameters().size() == 2 ); | 
|---|
|  | 87 | // anything else is a single parameter constructor that is effectively a C-style assignment | 
|---|
|  | 88 | // delete newExpr->get_function(); | 
|---|
|  | 89 | assert(newExpr->get_args().size()==2); | 
|---|
|  | 90 | newExpr->set_function( new NameExpr( "?=?" ) ); | 
|---|
|  | 91 | funcDecl->get_statements()->get_kids().push_back( new ExprStmt( std::list< Label >(), newExpr ) ); | 
|---|
|  | 92 | } | 
|---|
|  | 93 | break; | 
|---|
| [2794fff] | 94 | case CodeGen::OT_DTOR: | 
|---|
| [f1e012b] | 95 | // intrinsic destructors should do nothing | 
|---|
|  | 96 | // delete newExpr; | 
|---|
|  | 97 | break; | 
|---|
| [b87a5ed] | 98 | case CodeGen::OT_CONSTANT: | 
|---|
| [721f17a] | 99 | case CodeGen::OT_LABELADDRESS: | 
|---|
|  | 100 | // there are no intrinsic definitions of 0/1 or label addresses as functions | 
|---|
| [b87a5ed] | 101 | assert( false ); | 
|---|
|  | 102 | } // switch | 
|---|
|  | 103 | } | 
|---|
| [51b73452] | 104 |  | 
|---|
| [b87a5ed] | 105 | void MakeLibCfa::visit( ObjectDecl* origObjDecl ) { | 
|---|
|  | 106 | if ( origObjDecl->get_linkage() != LinkageSpec::Intrinsic ) return; | 
|---|
| [974906e2] | 107 |  | 
|---|
| [b87a5ed] | 108 | ObjectDecl *objDecl = origObjDecl->clone(); | 
|---|
| [a32b204] | 109 | assert( ! objDecl->get_init() ); | 
|---|
| [b87a5ed] | 110 | std::list< Expression* > noDesignators; | 
|---|
| [974906e2] | 111 | objDecl->set_init( new SingleInit( new NameExpr( objDecl->get_name() ), noDesignators, false ) ); // cannot be constructed | 
|---|
| [b87a5ed] | 112 | newDecls.push_back( objDecl ); | 
|---|
|  | 113 | } | 
|---|
| [51b73452] | 114 | } // namespace LibCfa | 
|---|