[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 | |
---|
[9aaac6e9] | 18 | #include <cassert> // for assert |
---|
[bf2438c] | 19 | #include <string> // for operator==, string |
---|
| 20 | |
---|
| 21 | #include "CodeGen/OperatorTable.h" // for OperatorInfo, operatorLookup, Ope... |
---|
[9aaac6e9] | 22 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
[bf2438c] | 23 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 24 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 25 | #include "Parser/LinkageSpec.h" // for Spec, Intrinsic, C |
---|
| 26 | #include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declara... |
---|
| 27 | #include "SynTree/Expression.h" // for NameExpr, UntypedExpr, VariableExpr |
---|
| 28 | #include "SynTree/Initializer.h" // for SingleInit |
---|
| 29 | #include "SynTree/Label.h" // for Label |
---|
| 30 | #include "SynTree/Statement.h" // for CompoundStmt, ReturnStmt |
---|
| 31 | #include "SynTree/Type.h" // for FunctionType |
---|
| 32 | #include "SynTree/Visitor.h" // for acceptAll, Visitor |
---|
[51b7345] | 33 | |
---|
| 34 | namespace LibCfa { |
---|
[9aaac6e9] | 35 | namespace { |
---|
| 36 | struct MakeLibCfa { |
---|
| 37 | public: |
---|
| 38 | void postvisit( FunctionDecl* funcDecl ); |
---|
[974906e2] | 39 | |
---|
[9aaac6e9] | 40 | std::list< Declaration* > newDecls; |
---|
| 41 | }; |
---|
| 42 | } |
---|
[51b7345] | 43 | |
---|
[b87a5ed] | 44 | void makeLibCfa( std::list< Declaration* > &prelude ) { |
---|
[9aaac6e9] | 45 | PassVisitor<MakeLibCfa> maker; |
---|
[b87a5ed] | 46 | acceptAll( prelude, maker ); |
---|
[9aaac6e9] | 47 | prelude.splice( prelude.end(), maker.pass.newDecls ); |
---|
[b87a5ed] | 48 | } |
---|
[51b7345] | 49 | |
---|
[9aaac6e9] | 50 | namespace { |
---|
| 51 | struct TypeFinder { |
---|
| 52 | void postvisit( TypeInstType * inst ) { |
---|
| 53 | // if a type variable is seen, assume all zero_t/one_t in the parameter list |
---|
| 54 | // can be replaced with the equivalent 'general' pointer. |
---|
| 55 | if ( type ) return; |
---|
| 56 | if ( inst->isFtype ) { |
---|
| 57 | type = new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), false ) ); |
---|
| 58 | } else { |
---|
| 59 | type = new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ); |
---|
| 60 | } |
---|
[f1e012b] | 61 | } |
---|
[9aaac6e9] | 62 | Type * type = nullptr; |
---|
| 63 | }; |
---|
[51b7345] | 64 | |
---|
[9aaac6e9] | 65 | struct ZeroOneReplacer { |
---|
| 66 | ZeroOneReplacer( Type * t ) : type( t ) {} |
---|
| 67 | ~ZeroOneReplacer() { delete type; } |
---|
| 68 | Type * type = nullptr; |
---|
[974906e2] | 69 | |
---|
[9aaac6e9] | 70 | Type * common( Type * t ) { |
---|
| 71 | if ( ! type ) return t; |
---|
| 72 | delete t; |
---|
| 73 | return type->clone(); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | Type * postmutate( OneType * t ) { return common( t ); } |
---|
| 77 | Type * postmutate( ZeroType * t ) { return common( t ); } |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | void fixZeroOneType( FunctionDecl * origFuncDecl ) { |
---|
| 81 | // find appropriate type to replace zero_t/one_t with |
---|
| 82 | PassVisitor<TypeFinder> finder; |
---|
| 83 | origFuncDecl->type->accept( finder ); |
---|
| 84 | // replace zero_t/one_t in function type |
---|
| 85 | PassVisitor<ZeroOneReplacer> replacer( finder.pass.type ); |
---|
| 86 | origFuncDecl->type->acceptMutator( replacer ); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void MakeLibCfa::postvisit( FunctionDecl* origFuncDecl ) { |
---|
| 90 | // don't change non-intrinsic functions |
---|
| 91 | if ( origFuncDecl->get_linkage() != LinkageSpec::Intrinsic ) return; |
---|
| 92 | // replace zero_t/one_t with void */void (*)(void) |
---|
| 93 | fixZeroOneType( origFuncDecl ); |
---|
| 94 | // skip functions already defined |
---|
| 95 | if ( origFuncDecl->get_statements() ) return; |
---|
| 96 | |
---|
| 97 | FunctionDecl *funcDecl = origFuncDecl->clone(); |
---|
| 98 | CodeGen::OperatorInfo opInfo; |
---|
| 99 | bool lookResult = CodeGen::operatorLookup( funcDecl->get_name(), opInfo ); |
---|
| 100 | assert( lookResult ); |
---|
| 101 | assert( ! funcDecl->get_statements() ); |
---|
| 102 | // build a recursive call - this is okay, as the call will actually be codegen'd using operator syntax |
---|
| 103 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( funcDecl->get_name() ) ); |
---|
| 104 | UniqueName paramNamer( "_p" ); |
---|
| 105 | std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); |
---|
| 106 | assert( param != funcDecl->get_functionType()->get_parameters().end() ); |
---|
| 107 | |
---|
| 108 | for ( ; param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { |
---|
| 109 | // name each unnamed parameter |
---|
| 110 | if ( (*param)->get_name() == "" ) { |
---|
| 111 | (*param)->set_name( paramNamer.newName() ); |
---|
| 112 | (*param)->set_linkage( LinkageSpec::C ); |
---|
| 113 | } |
---|
| 114 | // add parameter to the expression |
---|
| 115 | newExpr->get_args().push_back( new VariableExpr( *param ) ); |
---|
| 116 | } // for |
---|
| 117 | |
---|
| 118 | funcDecl->set_statements( new CompoundStmt( std::list< Label >() ) ); |
---|
| 119 | newDecls.push_back( funcDecl ); |
---|
| 120 | |
---|
| 121 | switch ( opInfo.type ) { |
---|
| 122 | case CodeGen::OT_INDEX: |
---|
| 123 | case CodeGen::OT_CALL: |
---|
| 124 | case CodeGen::OT_PREFIX: |
---|
| 125 | case CodeGen::OT_POSTFIX: |
---|
| 126 | case CodeGen::OT_INFIX: |
---|
| 127 | case CodeGen::OT_PREFIXASSIGN: |
---|
| 128 | case CodeGen::OT_POSTFIXASSIGN: |
---|
| 129 | case CodeGen::OT_INFIXASSIGN: |
---|
| 130 | case CodeGen::OT_CTOR: |
---|
| 131 | case CodeGen::OT_DTOR: |
---|
| 132 | // return the recursive call |
---|
| 133 | funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( std::list< Label >(), newExpr ) ); |
---|
| 134 | break; |
---|
| 135 | case CodeGen::OT_CONSTANT: |
---|
| 136 | case CodeGen::OT_LABELADDRESS: |
---|
| 137 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
| 138 | assert( false ); |
---|
| 139 | } // switch |
---|
| 140 | } |
---|
| 141 | } // namespace |
---|
[51b7345] | 142 | } // namespace LibCfa |
---|