[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 |
---|
[7a780ad] | 251 | int lastInteger = NUMBER_OF_BASIC_TYPES; |
---|
[70a3e16] | 252 | |
---|
| 253 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // perform breath-first traversal to generate cost graph |
---|
| 254 | generateCosts(r); |
---|
| 255 | } // for |
---|
| 256 | |
---|
| 257 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // use cost graph to find nearest-common-ancestor |
---|
| 258 | for (int c = r; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
| 259 | generateCommonType(r, c); |
---|
| 260 | } // for |
---|
| 261 | } // for |
---|
| 262 | |
---|
[7a780ad] | 263 | // Find the last integer type. |
---|
| 264 | // Assumes at least 1, and all come before the floating types. |
---|
| 265 | for ( int i = 1 ; i < NUMBER_OF_BASIC_TYPES ; i += 1 ) { |
---|
| 266 | if ( Floating == graph[i].sign ) { |
---|
| 267 | lastInteger = (i - 1); |
---|
| 268 | break; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
[ada4575] | 272 | #define STARTMK "// GENERATED START, DO NOT EDIT" |
---|
| 273 | #define ENDMK "// GENERATED END" |
---|
[c2fe922] | 274 | string BYMK( __FILE__ ); |
---|
| 275 | string::size_type posn = BYMK.find_last_of( "/" ); |
---|
| 276 | if ( posn != string::npos ) BYMK.erase( 0, posn - 1); // remove directories |
---|
| 277 | BYMK = "// GENERATED BY " + BYMK; |
---|
| 278 | |
---|
[70a3e16] | 279 | fstream file; |
---|
| 280 | stringstream buffer, code; |
---|
| 281 | string str; |
---|
| 282 | size_t start, end; |
---|
| 283 | |
---|
[7a780ad] | 284 | #define TypeH_AST TOP_SRCDIR "src/AST/BasicKind.hpp" |
---|
[cfaa2873] | 285 | resetInput( file, TypeH_AST, buffer, code, str ); |
---|
[10cb642] | 286 | |
---|
[cfaa2873] | 287 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeH_AST ); |
---|
[10cb642] | 288 | start += sizeof( STARTMK ); // includes newline |
---|
| 289 | code << str.substr( 0, start ); |
---|
| 290 | |
---|
[7a780ad] | 291 | code << BYMK << endl; |
---|
| 292 | code << "enum BasicKind {" << endl; |
---|
[10cb642] | 293 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { |
---|
[7a780ad] | 294 | code << "\t" << graph[r].name << "," << endl; |
---|
[7d55e4d] | 295 | } // for |
---|
[7a780ad] | 296 | code << "\tNUMBER_OF_BASIC_TYPES," << endl; |
---|
| 297 | code << "\tMAX_INTEGER_TYPE = " << graph[lastInteger].name << "," << endl; |
---|
| 298 | code << "};" << endl; |
---|
[10cb642] | 299 | |
---|
[cfaa2873] | 300 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeH_AST ); |
---|
| 301 | code << str.substr( start ); |
---|
| 302 | |
---|
| 303 | output( file, TypeH_AST, code ); |
---|
| 304 | // cout << code.str(); |
---|
| 305 | |
---|
| 306 | |
---|
| 307 | #define TypeC_AST TOP_SRCDIR "src/AST/Type.cpp" |
---|
| 308 | resetInput( file, TypeC_AST, buffer, code, str ); |
---|
| 309 | |
---|
| 310 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeC_AST ); |
---|
| 311 | start += sizeof( STARTMK ); // includes newline |
---|
| 312 | code << str.substr( 0, start ); |
---|
| 313 | |
---|
| 314 | code << BYMK << endl; |
---|
| 315 | code << "const char * BasicType::typeNames[] = {" << endl; |
---|
| 316 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { |
---|
| 317 | code << "\t\"" << graph[r].type << "\"," << endl; |
---|
[7d55e4d] | 318 | } // for |
---|
[cfaa2873] | 319 | code << "};" << endl; |
---|
| 320 | |
---|
| 321 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeC_AST ); |
---|
[10cb642] | 322 | code << str.substr( start ); |
---|
| 323 | |
---|
[cfaa2873] | 324 | output( file, TypeC_AST, code ); |
---|
[10cb642] | 325 | // cout << code.str(); |
---|
| 326 | |
---|
| 327 | |
---|
[c92bdcc] | 328 | #define ConversionCost TOP_SRCDIR "src/ResolvExpr/ConversionCost.cpp" |
---|
[70a3e16] | 329 | resetInput( file, ConversionCost, buffer, code, str ); |
---|
| 330 | |
---|
[ada4575] | 331 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ConversionCost ); |
---|
| 332 | start += sizeof( STARTMK ); // includes newline |
---|
[70a3e16] | 333 | code << str.substr( 0, start ); |
---|
| 334 | |
---|
[ada4575] | 335 | code << "\t" << BYMK << endl; |
---|
[70a3e16] | 336 | code << "\t/* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves)" << endl; |
---|
| 337 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
| 338 | code << '\t' << left; |
---|
| 339 | if ( graph[c].rank != graph[c + 1].rank ) { |
---|
| 340 | code << right << setw(30) << graph[c].type << left; |
---|
| 341 | } else if ( graph[c].rank != graph[c + 2].rank ) { |
---|
[10cb642] | 342 | code << string( 10, ' ' ) << setw(25) << graph[c].type << graph[c + 1].type; |
---|
[70a3e16] | 343 | c += 1; |
---|
| 344 | } else { |
---|
[10cb642] | 345 | code << setw(20) << graph[c].type << setw(20) << graph[c + 1].type << graph[c + 2].type; |
---|
[70a3e16] | 346 | c += 2; |
---|
| 347 | } // if |
---|
| 348 | code << endl; |
---|
| 349 | } // for |
---|
| 350 | code << right << "\t*/" << endl; |
---|
| 351 | code << "\t"; // indentation for end marker |
---|
| 352 | |
---|
[ada4575] | 353 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost ); |
---|
| 354 | if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost ); |
---|
| 355 | end += sizeof( STARTMK ); |
---|
[70a3e16] | 356 | code << str.substr( start, end - start ); |
---|
[7d55e4d] | 357 | |
---|
[ada4575] | 358 | code << "\t" << BYMK << endl; |
---|
[7a780ad] | 359 | code << "\tstatic const int costMatrix[ast::BasicKind::NUMBER_OF_BASIC_TYPES][ast::BasicKind::NUMBER_OF_BASIC_TYPES] = { // path length from root to node" << endl |
---|
[3a55d9f] | 360 | << "\t\t/* "; |
---|
[70a3e16] | 361 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles |
---|
[fd9ae1d] | 362 | code << setw(5) << graph[r].abbrev; |
---|
[70a3e16] | 363 | } // for |
---|
[fd9ae1d] | 364 | code << " */" << endl; |
---|
[70a3e16] | 365 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs |
---|
[3a55d9f] | 366 | code << "\t\t/* " << setw(6) << graph[r].abbrev << " */ {"; |
---|
[70a3e16] | 367 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
[fd9ae1d] | 368 | code << setw(4) << costMatrix[r][c] << ","; |
---|
[70a3e16] | 369 | } // for |
---|
[fd9ae1d] | 370 | code << " }," << endl; |
---|
[70a3e16] | 371 | } // for |
---|
| 372 | code << "\t}; // costMatrix" << endl; |
---|
| 373 | |
---|
[1c9568f0] | 374 | // maximum conversion cost from int |
---|
| 375 | code << "\tstatic const int maxIntCost = " << *max_element(costMatrix[SignedInt], costMatrix[SignedInt] + NUMBER_OF_BASIC_TYPES) << ";" << endl; |
---|
| 376 | code << "\t"; // indentation for end marker |
---|
[7d55e4d] | 377 | |
---|
[ada4575] | 378 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost ); |
---|
| 379 | if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost ); |
---|
| 380 | end += sizeof( STARTMK ); |
---|
[70a3e16] | 381 | code << str.substr( start, end - start ); |
---|
| 382 | |
---|
[ada4575] | 383 | code << "\t" << BYMK << endl; |
---|
[7a780ad] | 384 | code << "\tstatic const int signMatrix[ast::BasicKind::NUMBER_OF_BASIC_TYPES][ast::BasicKind::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion" << endl |
---|
[3a55d9f] | 385 | << "\t\t/* "; |
---|
[70a3e16] | 386 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles |
---|
[fd9ae1d] | 387 | code << setw(5) << graph[r].abbrev; |
---|
[70a3e16] | 388 | } // for |
---|
[fd9ae1d] | 389 | code << " */" << endl; |
---|
[70a3e16] | 390 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs |
---|
[3a55d9f] | 391 | code << "\t\t/* " << setw(6) << graph[r].abbrev << " */ {"; |
---|
[70a3e16] | 392 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
[fd9ae1d] | 393 | code << setw(4) << signMatrix[r][c] << ","; |
---|
[70a3e16] | 394 | } // for |
---|
[fd9ae1d] | 395 | code << " }," << endl; |
---|
[70a3e16] | 396 | } // for |
---|
| 397 | code << "\t}; // signMatrix" << endl; |
---|
| 398 | code << "\t"; // indentation for end marker |
---|
| 399 | |
---|
[ada4575] | 400 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost ); |
---|
[70a3e16] | 401 | code << str.substr( start ); |
---|
| 402 | |
---|
| 403 | output( file, ConversionCost, code ); |
---|
| 404 | // cout << code.str(); |
---|
| 405 | |
---|
| 406 | |
---|
[c92bdcc] | 407 | #define CommonType TOP_SRCDIR "src/ResolvExpr/CommonType.cpp" |
---|
[70a3e16] | 408 | resetInput( file, CommonType, buffer, code, str ); |
---|
| 409 | |
---|
[ada4575] | 410 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", CommonType ); |
---|
| 411 | start += sizeof( STARTMK ); // includes newline |
---|
[70a3e16] | 412 | code << str.substr( 0, start ); |
---|
| 413 | |
---|
| 414 | enum { PER_ROW = 6 }; |
---|
[ada4575] | 415 | code << "\t" << BYMK << endl; |
---|
[7a780ad] | 416 | code << "\t#define BT ast::BasicKind::" << endl; |
---|
[fc1a3e2] | 417 | code << "\tstatic const ast::BasicKind commonTypes[BT NUMBER_OF_BASIC_TYPES][BT NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor" << endl |
---|
[96df1329] | 418 | << "\t\t/*\t\t "; |
---|
[70a3e16] | 419 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles |
---|
| 420 | code << setw(24) << graph[r].abbrev; |
---|
| 421 | if ( (r+1) % PER_ROW == 0 ) { |
---|
[96df1329] | 422 | code << endl << "\t\t\t\t "; |
---|
| 423 | } // if |
---|
[70a3e16] | 424 | } // for |
---|
| 425 | code << "*/" << endl; |
---|
| 426 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs |
---|
[3a55d9f] | 427 | code << "\t\t\t\t {\n\t\t/* " << setw(6) << graph[r].abbrev << " */"; |
---|
[70a3e16] | 428 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) { |
---|
| 429 | string s = string{"BT "} + graph[commonTypeMatrix[r][c]].name; |
---|
| 430 | code << setw(23) << s << ","; |
---|
| 431 | if ( (c+1) % PER_ROW == 0 ) { |
---|
[96df1329] | 432 | code << endl << "\t\t\t\t "; |
---|
| 433 | } // if |
---|
[70a3e16] | 434 | } // for |
---|
[96df1329] | 435 | code << "}," << endl; |
---|
[70a3e16] | 436 | } // for |
---|
[fd9ae1d] | 437 | code << "\t}; // commonTypes" << endl; |
---|
[70a3e16] | 438 | code << "\t#undef BT" << endl; |
---|
| 439 | code << "\t"; // indentation for end marker |
---|
| 440 | |
---|
[ada4575] | 441 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", CommonType ); |
---|
[70a3e16] | 442 | code << str.substr( start ); |
---|
| 443 | |
---|
| 444 | output( file, CommonType, code ); |
---|
| 445 | // cout << code.str(); |
---|
| 446 | |
---|
| 447 | |
---|
[c92bdcc] | 448 | #define ManglerCommon TOP_SRCDIR "src/SymTab/ManglerCommon.cpp" |
---|
[70a3e16] | 449 | resetInput( file, ManglerCommon, buffer, code, str ); |
---|
| 450 | |
---|
[ada4575] | 451 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ManglerCommon ); |
---|
| 452 | start += sizeof( STARTMK ); // includes newline |
---|
[70a3e16] | 453 | code << str.substr( 0, start ); |
---|
| 454 | |
---|
| 455 | code << |
---|
[b0845f9] | 456 | "// GENERATED BY " __FILE__ "\n" |
---|
| 457 | "// NOTES ON MANGLING:\n" |
---|
| 458 | "// * Itanium spec says that Float80 encodes to \"e\" (like LongDouble), but the distinct lengths cause resolution problems.\n" |
---|
| 459 | "// * Float128 is supposed to encode to \"g\", but I wanted it to mangle equal to LongDouble.\n" |
---|
| 460 | "// * Mangling for non-standard complex types is by best guess\n" |
---|
| 461 | "// * _FloatN is supposed to encode as \"DF\"N\"_\"; modified for same reason as above.\n" |
---|
| 462 | "// * unused mangling identifiers:\n" |
---|
| 463 | "// - \"z\" ellipsis\n" |
---|
| 464 | "// - \"Dd\" IEEE 754r 64-bit decimal floating point (borrowed for _Float32x)\n" |
---|
| 465 | "// - \"De\" IEEE 754r 128-bit decimal floating point\n" |
---|
| 466 | "// - \"Df\" IEEE 754r 32-bit decimal floating point\n" |
---|
| 467 | "// - \"Dh\" IEEE 754r 16-bit decimal floating point (borrowed for _Float16)\n" |
---|
| 468 | "// - \"DF\"N\"_\" ISO/IEC TS 18661 N-bit binary floating point (_FloatN)\n" |
---|
| 469 | "// - \"Di\" char32_t\n" |
---|
| 470 | "// - \"Ds\" char16_t\n"; |
---|
| 471 | |
---|
[7a780ad] | 472 | code << "const std::string basicTypes[ast::BasicKind::NUMBER_OF_BASIC_TYPES] = {" << endl; |
---|
[70a3e16] | 473 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { |
---|
[b0845f9] | 474 | code << "\t\"" << graph[r].mangled << "\"," << setw(9 - strlen(graph[r].mangled)) << ' ' << "// " << graph[r].type << endl; |
---|
[7d55e4d] | 475 | } // for |
---|
[b0845f9] | 476 | code << "}; // basicTypes" << endl; |
---|
[70a3e16] | 477 | |
---|
[ada4575] | 478 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ManglerCommon ); |
---|
[70a3e16] | 479 | code << str.substr( start ); |
---|
| 480 | |
---|
| 481 | output( file, ManglerCommon, code ); |
---|
| 482 | // cout << code.str(); |
---|
| 483 | } // main |
---|
| 484 | |
---|
| 485 | // Local Variables: // |
---|
| 486 | // tab-width: 4 // |
---|
| 487 | // mode: c++ // |
---|
[b2ea0cd] | 488 | // compile-command: "g++-8 -Wall -Wextra BasicTypes-gen.cpp" // |
---|
[70a3e16] | 489 | // End: // |
---|