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