| 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 | // MakeLibCfa.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sat May 16 10:33:33 2015 | 
|---|
| 11 | // Last Modified By : Rob Schluntz | 
|---|
| 12 | // Last Modified On : Fri Apr 22 13:54:15 2016 | 
|---|
| 13 | // Update Count     : 40 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "MakeLibCfa.h" | 
|---|
| 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 | 
|---|
| 32 |  | 
|---|
| 33 | namespace LibCfa { | 
|---|
| 34 | class MakeLibCfa : public Visitor { | 
|---|
| 35 | public: | 
|---|
| 36 | void visit( FunctionDecl* funcDecl ); | 
|---|
| 37 | void visit( ObjectDecl* objDecl ); | 
|---|
| 38 |  | 
|---|
| 39 | std::list< Declaration* > &get_newDecls() { return newDecls; } | 
|---|
| 40 | private: | 
|---|
| 41 | std::list< Declaration* > newDecls; | 
|---|
| 42 | }; | 
|---|
| 43 |  | 
|---|
| 44 | void makeLibCfa( std::list< Declaration* > &prelude ) { | 
|---|
| 45 | MakeLibCfa maker; | 
|---|
| 46 | acceptAll( prelude, maker ); | 
|---|
| 47 | prelude.splice( prelude.end(), maker.get_newDecls() ); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | void MakeLibCfa::visit( FunctionDecl* origFuncDecl ) { | 
|---|
| 51 | if ( origFuncDecl->get_linkage() != LinkageSpec::Intrinsic ) return; | 
|---|
| 52 | if ( origFuncDecl->get_statements() ) return; | 
|---|
| 53 |  | 
|---|
| 54 | FunctionDecl *funcDecl = origFuncDecl->clone(); | 
|---|
| 55 | CodeGen::OperatorInfo opInfo; | 
|---|
| 56 | bool lookResult = CodeGen::operatorLookup( funcDecl->get_name(), opInfo ); | 
|---|
| 57 | assert( lookResult ); | 
|---|
| 58 | assert( ! funcDecl->get_statements() ); | 
|---|
| 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() ); | 
|---|
| 63 |  | 
|---|
| 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 ); | 
|---|
| 74 |  | 
|---|
| 75 | switch ( opInfo.type ) { | 
|---|
| 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: | 
|---|
| 84 | case CodeGen::OT_CTOR: | 
|---|
| 85 | case CodeGen::OT_DTOR: | 
|---|
| 86 | funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( std::list< Label >(), newExpr ) ); | 
|---|
| 87 | break; | 
|---|
| 88 | case CodeGen::OT_CONSTANT: | 
|---|
| 89 | case CodeGen::OT_LABELADDRESS: | 
|---|
| 90 | // there are no intrinsic definitions of 0/1 or label addresses as functions | 
|---|
| 91 | assert( false ); | 
|---|
| 92 | } // switch | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | void MakeLibCfa::visit( ObjectDecl* origObjDecl ) { | 
|---|
| 96 | if ( origObjDecl->get_linkage() != LinkageSpec::Intrinsic ) return; | 
|---|
| 97 |  | 
|---|
| 98 | ObjectDecl *objDecl = origObjDecl->clone(); | 
|---|
| 99 | assert( ! objDecl->get_init() ); | 
|---|
| 100 | std::list< Expression* > noDesignators; | 
|---|
| 101 | objDecl->set_init( new SingleInit( new NameExpr( objDecl->get_name() ), false ) ); // cannot be constructed | 
|---|
| 102 | newDecls.push_back( objDecl ); | 
|---|
| 103 | } | 
|---|
| 104 | } // namespace LibCfa | 
|---|