[1c9568f0] | 1 | #include <algorithm> |
---|
[70a3e16] | 2 | #include <queue> |
---|
| 3 | #include <iostream> |
---|
| 4 | #include <iomanip> |
---|
| 5 | #include <fstream> |
---|
| 6 | #include <utility> |
---|
| 7 | #include <string> |
---|
| 8 | using namespace std; |
---|
| 9 | #include <assert.h> |
---|
| 10 | #include <string.h> // strlen |
---|
[6ca6811] | 11 | #include "config.h" // configure info |
---|
[70a3e16] | 12 | |
---|
| 13 | enum Kind { |
---|
| 14 | Bool, |
---|
| 15 | Char, |
---|
| 16 | SignedChar, |
---|
| 17 | UnsignedChar, |
---|
| 18 | ShortSignedInt, |
---|
| 19 | ShortUnsignedInt, |
---|
| 20 | SignedInt, |
---|
| 21 | UnsignedInt, |
---|
| 22 | LongSignedInt, |
---|
| 23 | LongUnsignedInt, |
---|
| 24 | LongLongSignedInt, |
---|
| 25 | LongLongUnsignedInt, |
---|
| 26 | SignedInt128, |
---|
| 27 | UnsignedInt128, |
---|
| 28 | uFloat16, |
---|
| 29 | uFloat16Complex, |
---|
| 30 | uFloat32, |
---|
| 31 | uFloat32Complex, |
---|
| 32 | Float, |
---|
| 33 | FloatComplex, |
---|
| 34 | // FloatImaginary, |
---|
| 35 | uFloat32x, |
---|
| 36 | uFloat32xComplex, |
---|
| 37 | uFloat64, |
---|
| 38 | uFloat64Complex, |
---|
| 39 | Double, |
---|
| 40 | DoubleComplex, |
---|
| 41 | // DoubleImaginary, |
---|
| 42 | uFloat64x, |
---|
| 43 | uFloat64xComplex, |
---|
| 44 | uuFloat80, |
---|
| 45 | uFloat128, |
---|
| 46 | uFloat128Complex, |
---|
| 47 | uuFloat128, |
---|
| 48 | LongDouble, |
---|
| 49 | LongDoubleComplex, |
---|
| 50 | // LongDoubleImaginary, |
---|
| 51 | uFloat128x, |
---|
| 52 | uFloat128xComplex, |
---|
| 53 | NUMBER_OF_BASIC_TYPES |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | enum NumSort { // floating point types act as both signed and unsigned |
---|
| 57 | Signed = 0x1, |
---|
| 58 | Unsigned = 0x2, |
---|
| 59 | Floating = 0x3 |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | struct Node { |
---|
| 63 | Kind basicType; // basic type |
---|
| 64 | const char * name; // basic-type name |
---|
| 65 | const char * abbrev; // internal abbreviation (documentation only) |
---|
| 66 | const char * type; // actual type name |
---|
| 67 | const char * mangled; // mangled abbreviation |
---|
| 68 | NumSort sign; // is this a signed integral type? |
---|
| 69 | int left, middle, right; // 3-ary tree, -1 => nullptr |
---|
| 70 | int rank; // integral rank (C standard 6.3.1.1.1, extended) |
---|
| 71 | } graph[NUMBER_OF_BASIC_TYPES] = { |
---|
| 72 | { Bool, "Bool", "B", "_Bool", "b", Signed, Char, SignedChar, -1, 0 }, // root |
---|
[f910df5] | 73 | |
---|
[70a3e16] | 74 | { Char, "Char", "C", "char", "c", Signed, SignedChar, UnsignedChar, ShortSignedInt, 1 }, |
---|
| 75 | { SignedChar, "SignedChar", "SC", "signed char", "a", Signed, UnsignedChar, ShortSignedInt, -1, 1 }, |
---|
| 76 | { UnsignedChar, "UnsignedChar", "UC", "unsigned char", "h", Unsigned, ShortUnsignedInt, ShortSignedInt, -1, 1 }, |
---|
[f910df5] | 77 | |
---|
[70a3e16] | 78 | { ShortSignedInt, "ShortSignedInt", "SI", "signed short int", "s", Signed, ShortUnsignedInt, SignedInt, -1, 2 }, |
---|
| 79 | { ShortUnsignedInt, "ShortUnsignedInt", "SUI", "unsigned short int", "t", Unsigned, UnsignedInt, SignedInt, -1, 2 }, |
---|
[f910df5] | 80 | |
---|
[70a3e16] | 81 | { SignedInt, "SignedInt", "I", "signed int", "i", Signed, UnsignedInt, LongSignedInt, -1, 3 }, |
---|
| 82 | { UnsignedInt, "UnsignedInt", "UI", "unsigned int", "j", Unsigned, LongUnsignedInt, LongSignedInt, -1, 3 }, |
---|
[f910df5] | 83 | |
---|
[70a3e16] | 84 | { LongSignedInt, "LongSignedInt", "LI", "signed long int", "l", Signed, LongUnsignedInt, LongLongSignedInt, -1, 4 }, |
---|
| 85 | { LongUnsignedInt, "LongUnsignedInt", "LUI", "unsigned long int", "m", Unsigned, LongLongSignedInt, LongLongUnsignedInt, -1, 4 }, |
---|
[f910df5] | 86 | |
---|
[70a3e16] | 87 | { LongLongSignedInt, "LongLongSignedInt", "LLI", "signed long long int", "x", Signed, LongLongUnsignedInt, SignedInt128, -1, 5 }, |
---|
| 88 | { LongLongUnsignedInt, "LongLongUnsignedInt", "LLUI", "unsigned long long int", "y", Unsigned, SignedInt128, UnsignedInt128, -1, 5 }, |
---|
[f910df5] | 89 | |
---|
[70a3e16] | 90 | { SignedInt128, "SignedInt128", "IB", "__int128", "n", Signed, UnsignedInt128, uFloat16, -1, 6 }, |
---|
| 91 | { UnsignedInt128, "UnsignedInt128", "UIB", "unsigned __int128", "o", Unsigned, uFloat16, -1, -1, 6 }, |
---|
[f910df5] | 92 | |
---|
[70a3e16] | 93 | { uFloat16, "uFloat16", "_FH", "_Float16", "DF16_", Floating, uFloat32, uFloat16Complex, -1, 7 }, |
---|
| 94 | { uFloat16Complex, "uFloat16Complex", "_FH", "_Float16 _Complex", "CDF16_", Floating, uFloat32Complex, -1, -1, 7 }, |
---|
| 95 | { uFloat32, "uFloat32", "_F", "_Float32", "DF32_", Floating, Float, uFloat32Complex, -1, 8 }, |
---|
| 96 | { uFloat32Complex, "uFloat32Complex", "_FC", "_Float32 _Complex", "CDF32_", Floating, FloatComplex, -1, -1, 8 }, |
---|
| 97 | { Float, "Float", "F", "float", "f", Floating, uFloat32x, FloatComplex, -1, 9 }, |
---|
| 98 | { FloatComplex, "FloatComplex", "FC", "float _Complex", "Cf", Floating, uFloat32xComplex, -1, -1, 9 }, |
---|
| 99 | // { FloatImaginary, "FloatImaginary", "FI", "float _Imaginary", "If", false, DoubleImaginary, FloatComplex, -1, 9 }, |
---|
[f910df5] | 100 | |
---|
[70a3e16] | 101 | { uFloat32x, "uFloat32x", "_FX", "_Float32x", "DF32x_", Floating, uFloat64, uFloat32xComplex, -1, 10 }, |
---|
| 102 | { uFloat32xComplex, "uFloat32xComplex", "_FXC", "_Float32x _Complex", "CDF32x_", Floating, uFloat64Complex, -1, -1, 10 }, |
---|
| 103 | { uFloat64, "uFloat64", "FD", "_Float64", "DF64_", Floating, Double, uFloat64Complex, -1, 11 }, |
---|
| 104 | { uFloat64Complex, "uFloat64Complex", "_FDC", "_Float64 _Complex", "CDF64_", Floating, DoubleComplex, -1, -1, 11 }, |
---|
| 105 | { Double, "Double", "D", "double", "d", Floating, uFloat64x, DoubleComplex, -1, 12 }, |
---|
| 106 | { DoubleComplex, "DoubleComplex", "DC", "double _Complex", "Cd", Floating, uFloat64xComplex, -1, -1, 12 }, |
---|
| 107 | // { DoubleImaginary, "DoubleImaginary", "DI", "double _Imaginary", "Id", false, LongDoubleImaginary, DoubleComplex, -1, 12 }, |
---|
[f910df5] | 108 | |
---|
[70a3e16] | 109 | { uFloat64x, "uFloat64x", "F80X", "_Float64x", "DF64x_", Floating, uuFloat80, uFloat64xComplex, -1, 13 }, |
---|
| 110 | { uFloat64xComplex, "uFloat64xComplex", "_FDXC", "_Float64x _Complex", "CDF64x_", Floating, uFloat128Complex, -1, -1, 13 }, |
---|
| 111 | { uuFloat80, "uuFloat80", "F80", "__float80", "Dq", Floating, uFloat128, uFloat64xComplex, -1, 14 }, |
---|
| 112 | { uFloat128, "uFloat128", "_FB", "_Float128", "DF128_", Floating, uuFloat128, uFloat128Complex, -1, 15 }, |
---|
| 113 | { uFloat128Complex, "uFloat128Complex", "_FLDC", "_Float128 _Complex", "CDF128_", Floating, LongDoubleComplex, -1, -1, 15 }, |
---|
| 114 | { uuFloat128, "uuFloat128", "FB", "__float128", "g", Floating, LongDouble, uFloat128Complex, -1, 16 }, |
---|
| 115 | { LongDouble, "LongDouble", "LD", "long double", "e", Floating, uFloat128x, LongDoubleComplex, -1, 17 }, |
---|
| 116 | { LongDoubleComplex, "LongDoubleComplex", "LDC", "long double _Complex", "Ce", Floating, uFloat128xComplex, -1, -1, 17 }, |
---|
| 117 | // { LongDoubleImaginary, "LongDoubleImaginary", "LDI", "long double _Imaginary", "Ie", false, LongDoubleComplex, -1, -1, 17 }, |
---|
[f910df5] | 118 | |
---|
[7d55e4d] | 119 | { uFloat128x, "uFloat128x", "_FBX", "_Float128x", "DF128x_", Floating, uFloat128xComplex, -1, -1, 18 }, |
---|
[70a3e16] | 120 | { uFloat128xComplex, "uFloat128xComplex", "_FLDXC", "_Float128x _Complex", "CDF128x_", Floating, -1, -1, -1, 18 } |
---|
| 121 | }; // graph |
---|
| 122 | |
---|
| 123 | static int costMatrix[NUMBER_OF_BASIC_TYPES][NUMBER_OF_BASIC_TYPES]; |
---|
| 124 | static int signMatrix[NUMBER_OF_BASIC_TYPES][NUMBER_OF_BASIC_TYPES]; |
---|
| 125 | static Kind commonTypeMatrix[NUMBER_OF_BASIC_TYPES][NUMBER_OF_BASIC_TYPES]; |
---|
| 126 | |
---|
| 127 | void generateCosts( int row ) { |
---|
| 128 | bool seen[NUMBER_OF_BASIC_TYPES] = { false /*, ... */ }; |
---|
[7d55e4d] | 129 | |
---|
[70a3e16] | 130 | struct el_cost { |
---|
| 131 | int i; |
---|
| 132 | int path; |
---|
| 133 | int sign; |
---|
[7d55e4d] | 134 | |
---|
[70a3e16] | 135 | el_cost( int i = 0, int p = 0, int s = 0 ) : i(i), path(p), sign(s) {} |
---|
[7d55e4d] | 136 | |
---|
[70a3e16] | 137 | // reverse the sense for use in largest-on-top priority queue |
---|
| 138 | bool operator< (const el_cost& o) const { |
---|
| 139 | return path > o.path || (path == o.path && sign > o.sign); |
---|
| 140 | } |
---|
| 141 | }; |
---|
| 142 | |
---|
| 143 | // initialize BFS queue with root of traversal |
---|
| 144 | priority_queue< el_cost > q; |
---|
| 145 | q.emplace( row, 0, 0 ); |
---|
| 146 | |
---|
| 147 | // BFS costs |
---|
| 148 | do { |
---|
| 149 | // visit cost element |
---|
| 150 | int col = q.top().i; |
---|
| 151 | // skip if already set |
---|
| 152 | if ( seen[col] ) { |
---|
| 153 | q.pop(); |
---|
| 154 | continue; |
---|
| 155 | } else { |
---|
| 156 | seen[col] = true; |
---|
| 157 | } // if |
---|
| 158 | |
---|
| 159 | // otherwise set min-costs into matrix |
---|
| 160 | int cost = q.top().path; |
---|
| 161 | int scost = q.top().sign; |
---|
| 162 | costMatrix[row][col] = cost; |
---|
| 163 | signMatrix[row][col] = scost; |
---|
| 164 | q.pop(); |
---|
| 165 | |
---|
| 166 | // traverse children |
---|
| 167 | int i = graph[col].left; |
---|
| 168 | if ( i == -1 ) continue; |
---|
| 169 | q.emplace( i, cost + 1, scost + ! (graph[col].sign & graph[i].sign) ); |
---|
| 170 | |
---|
| 171 | i = graph[col].middle; |
---|
| 172 | if ( i == -1 ) continue; |
---|
| 173 | q.emplace( i, cost + 1, scost + !(graph[col].sign & graph[i].sign) ); |
---|
| 174 | |
---|
| 175 | i = graph[col].right; |
---|
| 176 | if ( i == -1 ) continue; |
---|
| 177 | q.emplace( i, cost + 1, scost + !(graph[col].sign & graph[i].sign) ); |
---|
| 178 | } while ( ! q.empty() ); |
---|
| 179 | } // generateCosts |
---|
| 180 | |
---|
| 181 | void generateCommonType( int row, int col ) { // row <= col |
---|
| 182 | if ( costMatrix[row][col] >= 0 ) { |
---|
| 183 | // safe conversion from row => col |
---|
| 184 | commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[col].basicType; |
---|
| 185 | } else if ( costMatrix[col][row] >= 0 ) { |
---|
| 186 | // safe conversion from col => row |
---|
| 187 | commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[row].basicType; |
---|
| 188 | } else { |
---|
| 189 | // need to find least common ancestor |
---|
| 190 | // can cheat a bit here, in that there is always a direct ancestor of the later (col) element |
---|
| 191 | int i = graph[col].left; |
---|
| 192 | if ( i == -1 ) assert("invalid ancestor assumption"); |
---|
| 193 | if ( costMatrix[row][i] >= 0 ) { |
---|
| 194 | commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[i].basicType; |
---|
| 195 | return; |
---|
| 196 | } // if |
---|
[7d55e4d] | 197 | |
---|
[70a3e16] | 198 | i = graph[col].middle; |
---|
| 199 | if ( i == -1 ) assert("invalid ancestor assumption"); |
---|
| 200 | if ( costMatrix[row][i] >= 0 ) { |
---|
| 201 | commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[i].basicType; |
---|
| 202 | return; |
---|
| 203 | } // if |
---|
| 204 | |
---|
| 205 | i = graph[col].right; |
---|
| 206 | if ( i == -1 ) assert("invalid ancestor assumption"); |
---|
| 207 | if ( costMatrix[row][i] >= 0 ) { |
---|
| 208 | commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[i].basicType; |
---|
| 209 | return; |
---|
| 210 | } // if |
---|
| 211 | |
---|
| 212 | assert("invalid ancestor assumption"); |
---|
| 213 | } // if |
---|
| 214 | } // generateCommonType |
---|
| 215 | |
---|
| 216 | void resetInput( fstream & file, const char * filename, stringstream & buffer, stringstream & code, string & str ) { |
---|
| 217 | file.close(); |
---|
| 218 | buffer.str( "" ); |
---|
| 219 | code.str( "" ); |
---|
| 220 | file.open( filename, fstream::in ); |
---|
| 221 | if ( file.fail() ) { |
---|
| 222 | cout << "Internal error, could not open " << filename << " for input." << endl; |
---|
| 223 | abort(); |
---|
| 224 | } // if |
---|
| 225 | buffer << file.rdbuf(); |
---|
| 226 | str = buffer.str(); |
---|
| 227 | } // resetInput |
---|
| 228 | |
---|
| 229 | void output( fstream & file, const char * filename, stringstream & code ) { |
---|
| 230 | file.close(); |
---|
| 231 | file.open( filename, fstream::out ); |
---|
| 232 | if ( file.fail() ) { |
---|
| 233 | cout << "Internal error, could not open " << filename << " for output." << endl; |
---|
| 234 | abort(); |
---|
| 235 | } // if |
---|
| 236 | file << code.rdbuf(); // overwrite file |
---|
| 237 | } // output |
---|
| 238 | |
---|
| 239 | void Abort( const char * kind, const char * file ) { |
---|
| 240 | cerr << "Internal error, could not find " << kind << " of generated code for " << file << endl; |
---|
| 241 | } // Abort |
---|
| 242 | |
---|
| 243 | int main() { |
---|
| 244 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // initialization |
---|
| 245 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
| 246 | costMatrix[r][c] = -1; |
---|
| 247 | signMatrix[r][c] = -1; |
---|
| 248 | commonTypeMatrix[r][c] = NUMBER_OF_BASIC_TYPES; |
---|
| 249 | } // for |
---|
| 250 | } // for |
---|
| 251 | |
---|
| 252 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // perform breath-first traversal to generate cost graph |
---|
| 253 | generateCosts(r); |
---|
| 254 | } // for |
---|
| 255 | |
---|
| 256 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // use cost graph to find nearest-common-ancestor |
---|
| 257 | for (int c = r; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
| 258 | generateCommonType(r, c); |
---|
| 259 | } // for |
---|
| 260 | } // for |
---|
| 261 | |
---|
[ada4575] | 262 | #define STARTMK "// GENERATED START, DO NOT EDIT" |
---|
| 263 | #define ENDMK "// GENERATED END" |
---|
[c2fe922] | 264 | string BYMK( __FILE__ ); |
---|
| 265 | string::size_type posn = BYMK.find_last_of( "/" ); |
---|
| 266 | if ( posn != string::npos ) BYMK.erase( 0, posn - 1); // remove directories |
---|
| 267 | BYMK = "// GENERATED BY " + BYMK; |
---|
| 268 | |
---|
[70a3e16] | 269 | fstream file; |
---|
| 270 | stringstream buffer, code; |
---|
| 271 | string str; |
---|
| 272 | size_t start, end; |
---|
| 273 | |
---|
[cfaa2873] | 274 | #define TypeH_AST TOP_SRCDIR "src/AST/Type.hpp" |
---|
| 275 | resetInput( file, TypeH_AST, buffer, code, str ); |
---|
[10cb642] | 276 | |
---|
[cfaa2873] | 277 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeH_AST ); |
---|
[10cb642] | 278 | start += sizeof( STARTMK ); // includes newline |
---|
| 279 | code << str.substr( 0, start ); |
---|
| 280 | |
---|
| 281 | code << "\t" << BYMK << endl; |
---|
| 282 | code << "\tenum Kind {" << endl; |
---|
| 283 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { |
---|
| 284 | code << "\t\t" << graph[r].name << "," << endl; |
---|
[7d55e4d] | 285 | } // for |
---|
[10cb642] | 286 | code << "\t\tNUMBER_OF_BASIC_TYPES" << endl; |
---|
| 287 | code << "\t} kind;" << endl; |
---|
| 288 | code << "\t"; // indentation for end marker |
---|
| 289 | |
---|
[cfaa2873] | 290 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeH_AST ); |
---|
| 291 | code << str.substr( start ); |
---|
| 292 | |
---|
| 293 | output( file, TypeH_AST, code ); |
---|
| 294 | // cout << code.str(); |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | #define TypeC_AST TOP_SRCDIR "src/AST/Type.cpp" |
---|
| 298 | resetInput( file, TypeC_AST, buffer, code, str ); |
---|
| 299 | |
---|
| 300 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeC_AST ); |
---|
| 301 | start += sizeof( STARTMK ); // includes newline |
---|
| 302 | code << str.substr( 0, start ); |
---|
| 303 | |
---|
| 304 | code << BYMK << endl; |
---|
| 305 | code << "const char * BasicType::typeNames[] = {" << endl; |
---|
| 306 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { |
---|
| 307 | code << "\t\"" << graph[r].type << "\"," << endl; |
---|
[7d55e4d] | 308 | } // for |
---|
[cfaa2873] | 309 | code << "};" << endl; |
---|
| 310 | |
---|
| 311 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeC_AST ); |
---|
[10cb642] | 312 | code << str.substr( start ); |
---|
| 313 | |
---|
[cfaa2873] | 314 | output( file, TypeC_AST, code ); |
---|
[10cb642] | 315 | // cout << code.str(); |
---|
| 316 | |
---|
| 317 | |
---|
[70a3e16] | 318 | #define ConversionCost TOP_SRCDIR "src/ResolvExpr/ConversionCost.cc" |
---|
| 319 | resetInput( file, ConversionCost, buffer, code, str ); |
---|
| 320 | |
---|
[ada4575] | 321 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ConversionCost ); |
---|
| 322 | start += sizeof( STARTMK ); // includes newline |
---|
[70a3e16] | 323 | code << str.substr( 0, start ); |
---|
| 324 | |
---|
[ada4575] | 325 | code << "\t" << BYMK << endl; |
---|
[70a3e16] | 326 | code << "\t/* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves)" << endl; |
---|
| 327 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
| 328 | code << '\t' << left; |
---|
| 329 | if ( graph[c].rank != graph[c + 1].rank ) { |
---|
| 330 | code << right << setw(30) << graph[c].type << left; |
---|
| 331 | } else if ( graph[c].rank != graph[c + 2].rank ) { |
---|
[10cb642] | 332 | code << string( 10, ' ' ) << setw(25) << graph[c].type << graph[c + 1].type; |
---|
[70a3e16] | 333 | c += 1; |
---|
| 334 | } else { |
---|
[10cb642] | 335 | code << setw(20) << graph[c].type << setw(20) << graph[c + 1].type << graph[c + 2].type; |
---|
[70a3e16] | 336 | c += 2; |
---|
| 337 | } // if |
---|
| 338 | code << endl; |
---|
| 339 | } // for |
---|
| 340 | code << right << "\t*/" << endl; |
---|
| 341 | code << "\t"; // indentation for end marker |
---|
| 342 | |
---|
[ada4575] | 343 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost ); |
---|
| 344 | if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost ); |
---|
| 345 | end += sizeof( STARTMK ); |
---|
[70a3e16] | 346 | code << str.substr( start, end - start ); |
---|
[7d55e4d] | 347 | |
---|
[ada4575] | 348 | code << "\t" << BYMK << endl; |
---|
[7d55e4d] | 349 | code << "\tstatic const int costMatrix[ast::BasicType::NUMBER_OF_BASIC_TYPES][ast::BasicType::NUMBER_OF_BASIC_TYPES] = { // path length from root to node" << endl |
---|
[3a55d9f] | 350 | << "\t\t/* "; |
---|
[70a3e16] | 351 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles |
---|
[fd9ae1d] | 352 | code << setw(5) << graph[r].abbrev; |
---|
[70a3e16] | 353 | } // for |
---|
[fd9ae1d] | 354 | code << " */" << endl; |
---|
[70a3e16] | 355 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs |
---|
[3a55d9f] | 356 | code << "\t\t/* " << setw(6) << graph[r].abbrev << " */ {"; |
---|
[70a3e16] | 357 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
[fd9ae1d] | 358 | code << setw(4) << costMatrix[r][c] << ","; |
---|
[70a3e16] | 359 | } // for |
---|
[fd9ae1d] | 360 | code << " }," << endl; |
---|
[70a3e16] | 361 | } // for |
---|
| 362 | code << "\t}; // costMatrix" << endl; |
---|
| 363 | |
---|
[1c9568f0] | 364 | // maximum conversion cost from int |
---|
| 365 | code << "\tstatic const int maxIntCost = " << *max_element(costMatrix[SignedInt], costMatrix[SignedInt] + NUMBER_OF_BASIC_TYPES) << ";" << endl; |
---|
| 366 | code << "\t"; // indentation for end marker |
---|
[7d55e4d] | 367 | |
---|
[ada4575] | 368 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost ); |
---|
| 369 | if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost ); |
---|
| 370 | end += sizeof( STARTMK ); |
---|
[70a3e16] | 371 | code << str.substr( start, end - start ); |
---|
| 372 | |
---|
[ada4575] | 373 | code << "\t" << BYMK << endl; |
---|
[7d55e4d] | 374 | code << "\tstatic const int signMatrix[ast::BasicType::NUMBER_OF_BASIC_TYPES][ast::BasicType::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion" << endl |
---|
[3a55d9f] | 375 | << "\t\t/* "; |
---|
[70a3e16] | 376 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles |
---|
[fd9ae1d] | 377 | code << setw(5) << graph[r].abbrev; |
---|
[70a3e16] | 378 | } // for |
---|
[fd9ae1d] | 379 | code << " */" << endl; |
---|
[70a3e16] | 380 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs |
---|
[3a55d9f] | 381 | code << "\t\t/* " << setw(6) << graph[r].abbrev << " */ {"; |
---|
[70a3e16] | 382 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
[fd9ae1d] | 383 | code << setw(4) << signMatrix[r][c] << ","; |
---|
[70a3e16] | 384 | } // for |
---|
[fd9ae1d] | 385 | code << " }," << endl; |
---|
[70a3e16] | 386 | } // for |
---|
| 387 | code << "\t}; // signMatrix" << endl; |
---|
| 388 | code << "\t"; // indentation for end marker |
---|
| 389 | |
---|
[ada4575] | 390 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost ); |
---|
[70a3e16] | 391 | code << str.substr( start ); |
---|
| 392 | |
---|
| 393 | output( file, ConversionCost, code ); |
---|
| 394 | // cout << code.str(); |
---|
| 395 | |
---|
| 396 | |
---|
| 397 | #define CommonType TOP_SRCDIR "src/ResolvExpr/CommonType.cc" |
---|
| 398 | resetInput( file, CommonType, buffer, code, str ); |
---|
| 399 | |
---|
[ada4575] | 400 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", CommonType ); |
---|
| 401 | start += sizeof( STARTMK ); // includes newline |
---|
[70a3e16] | 402 | code << str.substr( 0, start ); |
---|
| 403 | |
---|
| 404 | enum { PER_ROW = 6 }; |
---|
[ada4575] | 405 | code << "\t" << BYMK << endl; |
---|
[7d55e4d] | 406 | code << "\t#define BT ast::BasicType::" << endl; |
---|
| 407 | code << "\tstatic const BT Kind commonTypes[BT NUMBER_OF_BASIC_TYPES][BT NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor" << endl |
---|
[96df1329] | 408 | << "\t\t/*\t\t "; |
---|
[70a3e16] | 409 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles |
---|
| 410 | code << setw(24) << graph[r].abbrev; |
---|
| 411 | if ( (r+1) % PER_ROW == 0 ) { |
---|
[96df1329] | 412 | code << endl << "\t\t\t\t "; |
---|
| 413 | } // if |
---|
[70a3e16] | 414 | } // for |
---|
| 415 | code << "*/" << endl; |
---|
| 416 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs |
---|
[3a55d9f] | 417 | code << "\t\t\t\t {\n\t\t/* " << setw(6) << graph[r].abbrev << " */"; |
---|
[70a3e16] | 418 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
| 419 | string s = string{"BT "} + graph[commonTypeMatrix[r][c]].name; |
---|
| 420 | code << setw(23) << s << ","; |
---|
| 421 | if ( (c+1) % PER_ROW == 0 ) { |
---|
[96df1329] | 422 | code << endl << "\t\t\t\t "; |
---|
| 423 | } // if |
---|
[70a3e16] | 424 | } // for |
---|
[96df1329] | 425 | code << "}," << endl; |
---|
[70a3e16] | 426 | } // for |
---|
[fd9ae1d] | 427 | code << "\t}; // commonTypes" << endl; |
---|
[70a3e16] | 428 | code << "\t#undef BT" << endl; |
---|
| 429 | code << "\t"; // indentation for end marker |
---|
| 430 | |
---|
[ada4575] | 431 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", CommonType ); |
---|
[70a3e16] | 432 | code << str.substr( start ); |
---|
| 433 | |
---|
| 434 | output( file, CommonType, code ); |
---|
| 435 | // cout << code.str(); |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | #define ManglerCommon TOP_SRCDIR "src/SymTab/ManglerCommon.cc" |
---|
| 439 | resetInput( file, ManglerCommon, buffer, code, str ); |
---|
| 440 | |
---|
[ada4575] | 441 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ManglerCommon ); |
---|
| 442 | start += sizeof( STARTMK ); // includes newline |
---|
[70a3e16] | 443 | code << str.substr( 0, start ); |
---|
| 444 | |
---|
| 445 | code << |
---|
[b0845f9] | 446 | "// GENERATED BY " __FILE__ "\n" |
---|
| 447 | "// NOTES ON MANGLING:\n" |
---|
| 448 | "// * Itanium spec says that Float80 encodes to \"e\" (like LongDouble), but the distinct lengths cause resolution problems.\n" |
---|
| 449 | "// * Float128 is supposed to encode to \"g\", but I wanted it to mangle equal to LongDouble.\n" |
---|
| 450 | "// * Mangling for non-standard complex types is by best guess\n" |
---|
| 451 | "// * _FloatN is supposed to encode as \"DF\"N\"_\"; modified for same reason as above.\n" |
---|
| 452 | "// * unused mangling identifiers:\n" |
---|
| 453 | "// - \"z\" ellipsis\n" |
---|
| 454 | "// - \"Dd\" IEEE 754r 64-bit decimal floating point (borrowed for _Float32x)\n" |
---|
| 455 | "// - \"De\" IEEE 754r 128-bit decimal floating point\n" |
---|
| 456 | "// - \"Df\" IEEE 754r 32-bit decimal floating point\n" |
---|
| 457 | "// - \"Dh\" IEEE 754r 16-bit decimal floating point (borrowed for _Float16)\n" |
---|
| 458 | "// - \"DF\"N\"_\" ISO/IEC TS 18661 N-bit binary floating point (_FloatN)\n" |
---|
| 459 | "// - \"Di\" char32_t\n" |
---|
| 460 | "// - \"Ds\" char16_t\n"; |
---|
| 461 | |
---|
| 462 | code << "const std::string basicTypes[ast::BasicType::NUMBER_OF_BASIC_TYPES] = {" << endl; |
---|
[70a3e16] | 463 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { |
---|
[b0845f9] | 464 | code << "\t\"" << graph[r].mangled << "\"," << setw(9 - strlen(graph[r].mangled)) << ' ' << "// " << graph[r].type << endl; |
---|
[7d55e4d] | 465 | } // for |
---|
[b0845f9] | 466 | code << "}; // basicTypes" << endl; |
---|
[70a3e16] | 467 | |
---|
[ada4575] | 468 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ManglerCommon ); |
---|
[70a3e16] | 469 | code << str.substr( start ); |
---|
| 470 | |
---|
| 471 | output( file, ManglerCommon, code ); |
---|
| 472 | // cout << code.str(); |
---|
| 473 | } // main |
---|
| 474 | |
---|
| 475 | // Local Variables: // |
---|
| 476 | // tab-width: 4 // |
---|
| 477 | // mode: c++ // |
---|
| 478 | // compile-command: "g++-8 -Wall -Wextra BasicTypes-gen.cc" // |
---|
| 479 | // End: // |
---|