[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 | //
|
---|
| 7 | // MakeLibCfa.cc --
|
---|
| 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sat May 16 10:33:33 2015
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[721f17a] | 12 | // Last Modified On : Fri Jun 26 16:52:59 2015
|
---|
| 13 | // Update Count : 14
|
---|
[b87a5ed] | 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"
|
---|
| 24 | #include "UniqueName.h"
|
---|
| 25 |
|
---|
| 26 | namespace LibCfa {
|
---|
[b87a5ed] | 27 | class MakeLibCfa : public Visitor {
|
---|
| 28 | public:
|
---|
| 29 | void visit( FunctionDecl* funcDecl );
|
---|
| 30 | void visit( ObjectDecl* objDecl );
|
---|
[51b73452] | 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;
|
---|
[51b73452] | 45 |
|
---|
[b87a5ed] | 46 | FunctionDecl *funcDecl = origFuncDecl->clone();
|
---|
| 47 | CodeGen::OperatorInfo opInfo;
|
---|
| 48 | bool lookResult = CodeGen::operatorLookup( funcDecl->get_name(), opInfo );
|
---|
| 49 | assert( lookResult );
|
---|
[a32b204] | 50 | assert( ! funcDecl->get_statements() );
|
---|
[b87a5ed] | 51 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( funcDecl->get_name() ) );
|
---|
| 52 | UniqueName paramNamer( "_p" );
|
---|
| 53 | std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin();
|
---|
| 54 | assert( param != funcDecl->get_functionType()->get_parameters().end() );
|
---|
[51b73452] | 55 |
|
---|
[b87a5ed] | 56 | if ( (*param)->get_name() == "" ) {
|
---|
| 57 | (*param)->set_name( paramNamer.newName() );
|
---|
| 58 | (*param)->set_linkage( LinkageSpec::C );
|
---|
| 59 | } // if
|
---|
| 60 |
|
---|
[a32b204] | 61 | switch ( opInfo.type ) {
|
---|
[b87a5ed] | 62 | case CodeGen::OT_INDEX:
|
---|
| 63 | case CodeGen::OT_CALL:
|
---|
| 64 | case CodeGen::OT_PREFIX:
|
---|
| 65 | case CodeGen::OT_POSTFIX:
|
---|
| 66 | case CodeGen::OT_INFIX:
|
---|
| 67 | newExpr->get_args().push_back( new VariableExpr( *param ) );
|
---|
| 68 | break;
|
---|
| 69 | case CodeGen::OT_PREFIXASSIGN:
|
---|
| 70 | case CodeGen::OT_POSTFIXASSIGN:
|
---|
| 71 | case CodeGen::OT_INFIXASSIGN:
|
---|
| 72 | {
|
---|
| 73 | newExpr->get_args().push_back( new VariableExpr( *param ) );
|
---|
| 74 | // UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
| 75 | // deref->get_args().push_back( new VariableExpr( *param ) );
|
---|
| 76 | // newExpr->get_args().push_back( deref );
|
---|
| 77 | break;
|
---|
| 78 | }
|
---|
| 79 | case CodeGen::OT_CONSTANT:
|
---|
[721f17a] | 80 | case CodeGen::OT_LABELADDRESS:
|
---|
| 81 | // there are no intrinsic definitions of 0/1 or label addresses as functions
|
---|
[b87a5ed] | 82 | assert( false );
|
---|
| 83 | } // switch
|
---|
| 84 |
|
---|
| 85 | for ( param++; param != funcDecl->get_functionType()->get_parameters().end(); ++param ) {
|
---|
| 86 | if ( (*param)->get_name() == "" ) {
|
---|
| 87 | (*param)->set_name( paramNamer.newName() );
|
---|
| 88 | (*param)->set_linkage( LinkageSpec::C );
|
---|
| 89 | }
|
---|
| 90 | newExpr->get_args().push_back( new VariableExpr( *param ) );
|
---|
| 91 | } // for
|
---|
| 92 | funcDecl->set_statements( new CompoundStmt( std::list< Label >() ) );
|
---|
| 93 | funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( std::list< Label >(), newExpr ) );
|
---|
| 94 | newDecls.push_back( funcDecl );
|
---|
| 95 | }
|
---|
[51b73452] | 96 |
|
---|
[b87a5ed] | 97 | void MakeLibCfa::visit( ObjectDecl* origObjDecl ) {
|
---|
| 98 | if ( origObjDecl->get_linkage() != LinkageSpec::Intrinsic ) return;
|
---|
| 99 |
|
---|
| 100 | ObjectDecl *objDecl = origObjDecl->clone();
|
---|
[a32b204] | 101 | assert( ! objDecl->get_init() );
|
---|
[b87a5ed] | 102 | std::list< Expression* > noDesignators;
|
---|
| 103 | objDecl->set_init( new SingleInit( new NameExpr( objDecl->get_name() ), noDesignators ) );
|
---|
| 104 | newDecls.push_back( objDecl );
|
---|
| 105 | }
|
---|
[51b73452] | 106 | } // namespace LibCfa
|
---|
[b87a5ed] | 107 |
|
---|
| 108 | // Local Variables: //
|
---|
| 109 | // tab-width: 4 //
|
---|
| 110 | // mode: c++ //
|
---|
| 111 | // compile-command: "make install" //
|
---|
| 112 | // End: //
|
---|