[51587aa] | 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 | // |
---|
[486341f] | 7 | // GenType.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[1db21619] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Jul 9 16:43:52 2015 |
---|
| 13 | // Update Count : 13 |
---|
[51587aa] | 14 | // |
---|
| 15 | |
---|
[5f2f2d7] | 16 | #include <sstream> |
---|
[51b7345] | 17 | #include <cassert> |
---|
| 18 | |
---|
| 19 | #include "GenType.h" |
---|
[a61fea9a] | 20 | #include "CodeGenerator.h" |
---|
[e8032b0] | 21 | |
---|
| 22 | #include "SynTree/Declaration.h" |
---|
[51b7345] | 23 | #include "SynTree/Expression.h" |
---|
[e8032b0] | 24 | #include "SynTree/Type.h" |
---|
| 25 | #include "SynTree/Visitor.h" |
---|
[51b7345] | 26 | |
---|
| 27 | namespace CodeGen { |
---|
[51587aa] | 28 | class GenType : public Visitor { |
---|
| 29 | public: |
---|
[486341f] | 30 | GenType( const std::string &typeString, bool mangle = true ); |
---|
[51587aa] | 31 | std::string get_typeString() const { return typeString; } |
---|
| 32 | void set_typeString( const std::string &newValue ) { typeString = newValue; } |
---|
[486341f] | 33 | |
---|
[51587aa] | 34 | virtual void visit( FunctionType *funcType ); |
---|
| 35 | virtual void visit( VoidType *voidType ); |
---|
| 36 | virtual void visit( BasicType *basicType ); |
---|
| 37 | virtual void visit( PointerType *pointerType ); |
---|
| 38 | virtual void visit( ArrayType *arrayType ); |
---|
| 39 | virtual void visit( StructInstType *structInst ); |
---|
| 40 | virtual void visit( UnionInstType *unionInst ); |
---|
| 41 | virtual void visit( EnumInstType *enumInst ); |
---|
| 42 | virtual void visit( TypeInstType *typeInst ); |
---|
[6d4d1a6] | 43 | virtual void visit( TupleType * tupleType ); |
---|
[44b7088] | 44 | virtual void visit( VarArgsType *varArgsType ); |
---|
[89e6ffc] | 45 | virtual void visit( ZeroType *zeroType ); |
---|
| 46 | virtual void visit( OneType *oneType ); |
---|
[486341f] | 47 | |
---|
[51587aa] | 48 | private: |
---|
| 49 | void handleQualifiers( Type *type ); |
---|
| 50 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); |
---|
[486341f] | 51 | |
---|
[51587aa] | 52 | std::string typeString; |
---|
[486341f] | 53 | bool mangle = true; |
---|
[51587aa] | 54 | }; |
---|
| 55 | |
---|
[486341f] | 56 | std::string genType( Type *type, const std::string &baseString, bool mangle ) { |
---|
| 57 | GenType gt( baseString, mangle ); |
---|
[51587aa] | 58 | type->accept( gt ); |
---|
| 59 | return gt.get_typeString(); |
---|
| 60 | } |
---|
| 61 | |
---|
[486341f] | 62 | GenType::GenType( const std::string &typeString, bool mangle ) : typeString( typeString ), mangle( mangle ) {} |
---|
[51587aa] | 63 | |
---|
| 64 | void GenType::visit( VoidType *voidType ) { |
---|
| 65 | typeString = "void " + typeString; |
---|
| 66 | handleQualifiers( voidType ); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void GenType::visit( BasicType *basicType ) { |
---|
| 70 | BasicType::Kind kind = basicType->get_kind(); |
---|
| 71 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); |
---|
| 72 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; |
---|
| 73 | handleQualifiers( basicType ); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { |
---|
[5f2f2d7] | 77 | std::ostringstream os; |
---|
[51587aa] | 78 | if ( typeString != "" ) { |
---|
| 79 | if ( typeString[ 0 ] == '*' ) { |
---|
| 80 | os << "(" << typeString << ")"; |
---|
| 81 | } else { |
---|
| 82 | os << typeString; |
---|
| 83 | } // if |
---|
| 84 | } // if |
---|
| 85 | os << "["; |
---|
| 86 | |
---|
| 87 | if ( isStatic ) { |
---|
| 88 | os << "static "; |
---|
| 89 | } // if |
---|
| 90 | if ( qualifiers.isConst ) { |
---|
| 91 | os << "const "; |
---|
| 92 | } // if |
---|
| 93 | if ( qualifiers.isVolatile ) { |
---|
| 94 | os << "volatile "; |
---|
| 95 | } // if |
---|
| 96 | if ( qualifiers.isRestrict ) { |
---|
| 97 | os << "__restrict "; |
---|
| 98 | } // if |
---|
| 99 | if ( qualifiers.isAtomic ) { |
---|
| 100 | os << "_Atomic "; |
---|
| 101 | } // if |
---|
[1db21619] | 102 | if ( qualifiers.isAttribute ) { |
---|
| 103 | os << "__attribute(( )) "; |
---|
| 104 | } // if |
---|
[51587aa] | 105 | if ( dimension != 0 ) { |
---|
[486341f] | 106 | CodeGenerator cg( os, mangle ); |
---|
[51587aa] | 107 | dimension->accept( cg ); |
---|
[71bd8c6] | 108 | } else if ( isVarLen ) { |
---|
[1db21619] | 109 | // no dimension expression on a VLA means it came in with the * token |
---|
[71bd8c6] | 110 | os << "*"; |
---|
[51587aa] | 111 | } // if |
---|
| 112 | os << "]"; |
---|
| 113 | |
---|
[5f2f2d7] | 114 | typeString = os.str(); |
---|
[486341f] | 115 | |
---|
[51587aa] | 116 | base->accept( *this ); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void GenType::visit( PointerType *pointerType ) { |
---|
| 120 | assert( pointerType->get_base() != 0); |
---|
| 121 | if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { |
---|
| 122 | genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); |
---|
| 123 | } else { |
---|
| 124 | handleQualifiers( pointerType ); |
---|
| 125 | if ( typeString[ 0 ] == '?' ) { |
---|
| 126 | typeString = "* " + typeString; |
---|
| 127 | } else { |
---|
| 128 | typeString = "*" + typeString; |
---|
| 129 | } // if |
---|
| 130 | pointerType->get_base()->accept( *this ); |
---|
| 131 | } // if |
---|
| 132 | } |
---|
| 133 | |
---|
[a08ba92] | 134 | void GenType::visit( ArrayType *arrayType ) { |
---|
[51587aa] | 135 | genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void GenType::visit( FunctionType *funcType ) { |
---|
[5f2f2d7] | 139 | std::ostringstream os; |
---|
[51587aa] | 140 | |
---|
| 141 | if ( typeString != "" ) { |
---|
| 142 | if ( typeString[ 0 ] == '*' ) { |
---|
| 143 | os << "(" << typeString << ")"; |
---|
| 144 | } else { |
---|
| 145 | os << typeString; |
---|
| 146 | } // if |
---|
| 147 | } // if |
---|
[486341f] | 148 | |
---|
[51587aa] | 149 | /************* parameters ***************/ |
---|
| 150 | |
---|
| 151 | const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); |
---|
| 152 | |
---|
| 153 | if ( pars.empty() ) { |
---|
| 154 | if ( funcType->get_isVarArgs() ) { |
---|
| 155 | os << "()"; |
---|
| 156 | } else { |
---|
| 157 | os << "(void)"; |
---|
| 158 | } // if |
---|
| 159 | } else { |
---|
[486341f] | 160 | CodeGenerator cg( os, mangle ); |
---|
[51587aa] | 161 | os << "(" ; |
---|
| 162 | |
---|
| 163 | cg.genCommaList( pars.begin(), pars.end() ); |
---|
| 164 | |
---|
[a08ba92] | 165 | if ( funcType->get_isVarArgs() ) { |
---|
[51587aa] | 166 | os << ", ..."; |
---|
| 167 | } // if |
---|
| 168 | os << ")"; |
---|
| 169 | } // if |
---|
[486341f] | 170 | |
---|
[5f2f2d7] | 171 | typeString = os.str(); |
---|
[51587aa] | 172 | |
---|
| 173 | if ( funcType->get_returnVals().size() == 0 ) { |
---|
| 174 | typeString = "void " + typeString; |
---|
| 175 | } else { |
---|
| 176 | funcType->get_returnVals().front()->get_type()->accept( *this ); |
---|
| 177 | } // if |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | void GenType::visit( StructInstType *structInst ) { |
---|
| 181 | typeString = "struct " + structInst->get_name() + " " + typeString; |
---|
| 182 | handleQualifiers( structInst ); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void GenType::visit( UnionInstType *unionInst ) { |
---|
| 186 | typeString = "union " + unionInst->get_name() + " " + typeString; |
---|
| 187 | handleQualifiers( unionInst ); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void GenType::visit( EnumInstType *enumInst ) { |
---|
| 191 | typeString = "enum " + enumInst->get_name() + " " + typeString; |
---|
| 192 | handleQualifiers( enumInst ); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | void GenType::visit( TypeInstType *typeInst ) { |
---|
| 196 | typeString = typeInst->get_name() + " " + typeString; |
---|
| 197 | handleQualifiers( typeInst ); |
---|
| 198 | } |
---|
| 199 | |
---|
[6d4d1a6] | 200 | void GenType::visit( TupleType * tupleType ) { |
---|
| 201 | assertf( ! mangle, "Tuple types should not make it to Code Gen." ); |
---|
| 202 | Visitor::visit( tupleType ); |
---|
| 203 | } |
---|
| 204 | |
---|
[44b7088] | 205 | void GenType::visit( VarArgsType *varArgsType ) { |
---|
| 206 | typeString = "__builtin_va_list " + typeString; |
---|
[540ddb7d] | 207 | handleQualifiers( varArgsType ); |
---|
[44b7088] | 208 | } |
---|
| 209 | |
---|
[89e6ffc] | 210 | void GenType::visit( ZeroType *zeroType ) { |
---|
| 211 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
[d78f4c0] | 212 | typeString = "long int " + typeString; |
---|
[89e6ffc] | 213 | handleQualifiers( zeroType ); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | void GenType::visit( OneType *oneType ) { |
---|
| 217 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
[d78f4c0] | 218 | typeString = "long int " + typeString; |
---|
[89e6ffc] | 219 | handleQualifiers( oneType ); |
---|
| 220 | } |
---|
| 221 | |
---|
[51587aa] | 222 | void GenType::handleQualifiers( Type *type ) { |
---|
| 223 | if ( type->get_isConst() ) { |
---|
| 224 | typeString = "const " + typeString; |
---|
| 225 | } // if |
---|
| 226 | if ( type->get_isVolatile() ) { |
---|
| 227 | typeString = "volatile " + typeString; |
---|
| 228 | } // if |
---|
| 229 | if ( type->get_isRestrict() ) { |
---|
| 230 | typeString = "__restrict " + typeString; |
---|
| 231 | } // if |
---|
| 232 | if ( type->get_isAtomic() ) { |
---|
| 233 | typeString = "_Atomic " + typeString; |
---|
| 234 | } // if |
---|
| 235 | } |
---|
[51b7345] | 236 | } // namespace CodeGen |
---|
[51587aa] | 237 | |
---|
| 238 | // Local Variables: // |
---|
| 239 | // tab-width: 4 // |
---|
| 240 | // mode: c++ // |
---|
| 241 | // compile-command: "make install" // |
---|
| 242 | // End: // |
---|