[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 |
|
---|
[70a3e16] | 119 | { uFloat128x, "uFloat128x", "_FBX", "_Float128x", "DF128x_", Floating, uFloat128xComplex, -1, -1, 18 },
|
---|
| 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 /*, ... */ };
|
---|
| 129 |
|
---|
| 130 | struct el_cost {
|
---|
| 131 | int i;
|
---|
| 132 | int path;
|
---|
| 133 | int sign;
|
---|
| 134 |
|
---|
| 135 | el_cost( int i = 0, int p = 0, int s = 0 ) : i(i), path(p), sign(s) {}
|
---|
| 136 |
|
---|
| 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
|
---|
| 197 |
|
---|
| 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 |
|
---|
| 274 |
|
---|
[cfaa2873] | 275 | #define TypeH TOP_SRCDIR "src/SynTree/Type.h"
|
---|
| 276 | resetInput( file, TypeH, buffer, code, str );
|
---|
[70a3e16] | 277 |
|
---|
[cfaa2873] | 278 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeH );
|
---|
[ada4575] | 279 | start += sizeof( STARTMK ); // includes newline
|
---|
[70a3e16] | 280 | code << str.substr( 0, start );
|
---|
| 281 |
|
---|
[ada4575] | 282 | code << "\t" << BYMK << endl;
|
---|
[70a3e16] | 283 | code << "\tenum Kind {" << endl;
|
---|
| 284 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
|
---|
| 285 | code << "\t\t" << graph[r].name << "," << endl;
|
---|
| 286 | } // for
|
---|
| 287 | code << "\t\tNUMBER_OF_BASIC_TYPES" << endl;
|
---|
| 288 | code << "\t} kind;" << endl;
|
---|
| 289 | code << "\t"; // indentation for end marker
|
---|
| 290 |
|
---|
[cfaa2873] | 291 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeH );
|
---|
[70a3e16] | 292 | code << str.substr( start );
|
---|
| 293 |
|
---|
[cfaa2873] | 294 | output( file, TypeH, code );
|
---|
| 295 | // cout << code.str();
|
---|
| 296 |
|
---|
| 297 |
|
---|
| 298 | #define TypeC TOP_SRCDIR "src/SynTree/Type.cc"
|
---|
| 299 | resetInput( file, TypeC, buffer, code, str );
|
---|
| 300 |
|
---|
| 301 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeC );
|
---|
| 302 | start += sizeof( STARTMK ); // includes newline
|
---|
| 303 | code << str.substr( 0, start );
|
---|
| 304 |
|
---|
| 305 | code << BYMK << endl;
|
---|
| 306 | code << "const char * BasicType::typeNames[] = {" << endl;
|
---|
| 307 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
|
---|
| 308 | code << "\t\"" << graph[r].type << "\"," << endl;
|
---|
| 309 | } // for
|
---|
| 310 | code << "};" << endl;
|
---|
| 311 |
|
---|
| 312 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeC );
|
---|
| 313 | code << str.substr( start );
|
---|
| 314 |
|
---|
| 315 | output( file, TypeC, code );
|
---|
[70a3e16] | 316 | // cout << code.str();
|
---|
| 317 |
|
---|
[10cb642] | 318 |
|
---|
| 319 | // TEMPORARY DURING CHANGE OVER
|
---|
[cfaa2873] | 320 | #define TypeH_AST TOP_SRCDIR "src/AST/Type.hpp"
|
---|
| 321 | resetInput( file, TypeH_AST, buffer, code, str );
|
---|
[10cb642] | 322 |
|
---|
[cfaa2873] | 323 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeH_AST );
|
---|
[10cb642] | 324 | start += sizeof( STARTMK ); // includes newline
|
---|
| 325 | code << str.substr( 0, start );
|
---|
| 326 |
|
---|
| 327 | code << "\t" << BYMK << endl;
|
---|
| 328 | code << "\tenum Kind {" << endl;
|
---|
| 329 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
|
---|
| 330 | code << "\t\t" << graph[r].name << "," << endl;
|
---|
| 331 | } // for
|
---|
| 332 | code << "\t\tNUMBER_OF_BASIC_TYPES" << endl;
|
---|
| 333 | code << "\t} kind;" << endl;
|
---|
| 334 | code << "\t"; // indentation for end marker
|
---|
| 335 |
|
---|
[cfaa2873] | 336 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeH_AST );
|
---|
| 337 | code << str.substr( start );
|
---|
| 338 |
|
---|
| 339 | output( file, TypeH_AST, code );
|
---|
| 340 | // cout << code.str();
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | #define TypeC_AST TOP_SRCDIR "src/AST/Type.cpp"
|
---|
| 344 | resetInput( file, TypeC_AST, buffer, code, str );
|
---|
| 345 |
|
---|
| 346 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", TypeC_AST );
|
---|
| 347 | start += sizeof( STARTMK ); // includes newline
|
---|
| 348 | code << str.substr( 0, start );
|
---|
| 349 |
|
---|
| 350 | code << BYMK << endl;
|
---|
| 351 | code << "const char * BasicType::typeNames[] = {" << endl;
|
---|
| 352 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
|
---|
| 353 | code << "\t\"" << graph[r].type << "\"," << endl;
|
---|
| 354 | } // for
|
---|
| 355 | code << "};" << endl;
|
---|
| 356 |
|
---|
| 357 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", TypeC_AST );
|
---|
[10cb642] | 358 | code << str.substr( start );
|
---|
| 359 |
|
---|
[cfaa2873] | 360 | output( file, TypeC_AST, code );
|
---|
[10cb642] | 361 | // cout << code.str();
|
---|
| 362 |
|
---|
| 363 |
|
---|
[70a3e16] | 364 | #define ConversionCost TOP_SRCDIR "src/ResolvExpr/ConversionCost.cc"
|
---|
| 365 | resetInput( file, ConversionCost, buffer, code, str );
|
---|
| 366 |
|
---|
[ada4575] | 367 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ConversionCost );
|
---|
| 368 | start += sizeof( STARTMK ); // includes newline
|
---|
[70a3e16] | 369 | code << str.substr( 0, start );
|
---|
| 370 |
|
---|
[ada4575] | 371 | code << "\t" << BYMK << endl;
|
---|
[70a3e16] | 372 | code << "\t/* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves)" << endl;
|
---|
| 373 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
|
---|
| 374 | code << '\t' << left;
|
---|
| 375 | if ( graph[c].rank != graph[c + 1].rank ) {
|
---|
| 376 | code << right << setw(30) << graph[c].type << left;
|
---|
| 377 | } else if ( graph[c].rank != graph[c + 2].rank ) {
|
---|
[10cb642] | 378 | code << string( 10, ' ' ) << setw(25) << graph[c].type << graph[c + 1].type;
|
---|
[70a3e16] | 379 | c += 1;
|
---|
| 380 | } else {
|
---|
[10cb642] | 381 | code << setw(20) << graph[c].type << setw(20) << graph[c + 1].type << graph[c + 2].type;
|
---|
[70a3e16] | 382 | c += 2;
|
---|
| 383 | } // if
|
---|
| 384 | code << endl;
|
---|
| 385 | } // for
|
---|
| 386 | code << right << "\t*/" << endl;
|
---|
| 387 | code << "\t"; // indentation for end marker
|
---|
| 388 |
|
---|
[ada4575] | 389 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
|
---|
| 390 | if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
|
---|
| 391 | end += sizeof( STARTMK );
|
---|
[70a3e16] | 392 | code << str.substr( start, end - start );
|
---|
| 393 |
|
---|
[ada4575] | 394 | code << "\t" << BYMK << endl;
|
---|
[70a3e16] | 395 | code << "\tstatic const int costMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // path length from root to node" << endl
|
---|
[3a55d9f] | 396 | << "\t\t/* ";
|
---|
[70a3e16] | 397 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
|
---|
[fd9ae1d] | 398 | code << setw(5) << graph[r].abbrev;
|
---|
[70a3e16] | 399 | } // for
|
---|
[fd9ae1d] | 400 | code << " */" << endl;
|
---|
[70a3e16] | 401 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
|
---|
[3a55d9f] | 402 | code << "\t\t/* " << setw(6) << graph[r].abbrev << " */ {";
|
---|
[70a3e16] | 403 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
|
---|
[fd9ae1d] | 404 | code << setw(4) << costMatrix[r][c] << ",";
|
---|
[70a3e16] | 405 | } // for
|
---|
[fd9ae1d] | 406 | code << " }," << endl;
|
---|
[70a3e16] | 407 | } // for
|
---|
| 408 | code << "\t}; // costMatrix" << endl;
|
---|
| 409 |
|
---|
[1c9568f0] | 410 | // maximum conversion cost from int
|
---|
| 411 | code << "\tstatic const int maxIntCost = " << *max_element(costMatrix[SignedInt], costMatrix[SignedInt] + NUMBER_OF_BASIC_TYPES) << ";" << endl;
|
---|
| 412 | code << "\t"; // indentation for end marker
|
---|
| 413 |
|
---|
[ada4575] | 414 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
|
---|
| 415 | if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
|
---|
| 416 | end += sizeof( STARTMK );
|
---|
[70a3e16] | 417 | code << str.substr( start, end - start );
|
---|
| 418 |
|
---|
[ada4575] | 419 | code << "\t" << BYMK << endl;
|
---|
[70a3e16] | 420 | code << "\tstatic const int signMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion" << endl
|
---|
[3a55d9f] | 421 | << "\t\t/* ";
|
---|
[70a3e16] | 422 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
|
---|
[fd9ae1d] | 423 | code << setw(5) << graph[r].abbrev;
|
---|
[70a3e16] | 424 | } // for
|
---|
[fd9ae1d] | 425 | code << " */" << endl;
|
---|
[70a3e16] | 426 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
|
---|
[3a55d9f] | 427 | code << "\t\t/* " << setw(6) << graph[r].abbrev << " */ {";
|
---|
[70a3e16] | 428 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
|
---|
[fd9ae1d] | 429 | code << setw(4) << signMatrix[r][c] << ",";
|
---|
[70a3e16] | 430 | } // for
|
---|
[fd9ae1d] | 431 | code << " }," << endl;
|
---|
[70a3e16] | 432 | } // for
|
---|
| 433 | code << "\t}; // signMatrix" << endl;
|
---|
| 434 | code << "\t"; // indentation for end marker
|
---|
| 435 |
|
---|
[ada4575] | 436 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
|
---|
[70a3e16] | 437 | code << str.substr( start );
|
---|
| 438 |
|
---|
| 439 | output( file, ConversionCost, code );
|
---|
| 440 | // cout << code.str();
|
---|
| 441 |
|
---|
| 442 |
|
---|
| 443 | #define CommonType TOP_SRCDIR "src/ResolvExpr/CommonType.cc"
|
---|
| 444 | resetInput( file, CommonType, buffer, code, str );
|
---|
| 445 |
|
---|
[ada4575] | 446 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", CommonType );
|
---|
| 447 | start += sizeof( STARTMK ); // includes newline
|
---|
[70a3e16] | 448 | code << str.substr( 0, start );
|
---|
| 449 |
|
---|
| 450 | enum { PER_ROW = 6 };
|
---|
[ada4575] | 451 | code << "\t" << BYMK << endl;
|
---|
[70a3e16] | 452 | code << "\t#define BT BasicType::" << endl;
|
---|
| 453 | code << "\tstatic const BasicType::Kind commonTypes[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor" << endl
|
---|
[96df1329] | 454 | << "\t\t/*\t\t ";
|
---|
[70a3e16] | 455 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
|
---|
| 456 | code << setw(24) << graph[r].abbrev;
|
---|
| 457 | if ( (r+1) % PER_ROW == 0 ) {
|
---|
[96df1329] | 458 | code << endl << "\t\t\t\t ";
|
---|
| 459 | } // if
|
---|
[70a3e16] | 460 | } // for
|
---|
| 461 | code << "*/" << endl;
|
---|
| 462 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
|
---|
[3a55d9f] | 463 | code << "\t\t\t\t {\n\t\t/* " << setw(6) << graph[r].abbrev << " */";
|
---|
[70a3e16] | 464 | for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
|
---|
| 465 | string s = string{"BT "} + graph[commonTypeMatrix[r][c]].name;
|
---|
| 466 | code << setw(23) << s << ",";
|
---|
| 467 | if ( (c+1) % PER_ROW == 0 ) {
|
---|
[96df1329] | 468 | code << endl << "\t\t\t\t ";
|
---|
| 469 | } // if
|
---|
[70a3e16] | 470 | } // for
|
---|
[96df1329] | 471 | code << "}," << endl;
|
---|
[70a3e16] | 472 | } // for
|
---|
[fd9ae1d] | 473 | code << "\t}; // commonTypes" << endl;
|
---|
[70a3e16] | 474 | code << "\t#undef BT" << endl;
|
---|
| 475 | code << "\t"; // indentation for end marker
|
---|
| 476 |
|
---|
[ada4575] | 477 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", CommonType );
|
---|
[70a3e16] | 478 | code << str.substr( start );
|
---|
| 479 |
|
---|
| 480 | output( file, CommonType, code );
|
---|
| 481 | // cout << code.str();
|
---|
| 482 |
|
---|
| 483 |
|
---|
| 484 | #define ManglerCommon TOP_SRCDIR "src/SymTab/ManglerCommon.cc"
|
---|
| 485 | resetInput( file, ManglerCommon, buffer, code, str );
|
---|
| 486 |
|
---|
[ada4575] | 487 | if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ManglerCommon );
|
---|
| 488 | start += sizeof( STARTMK ); // includes newline
|
---|
[70a3e16] | 489 | code << str.substr( 0, start );
|
---|
| 490 |
|
---|
| 491 | code << "\t\t\t// GENERATED BY " __FILE__ << endl;
|
---|
| 492 | code <<
|
---|
| 493 | "\t\t\t// NOTES ON MANGLING:\n"
|
---|
| 494 | "\t\t\t// * Itanium spec says that Float80 encodes to \"e\" (like LongDouble), but the distinct lengths cause resolution problems.\n"
|
---|
| 495 | "\t\t\t// * Float128 is supposed to encode to \"g\", but I wanted it to mangle equal to LongDouble.\n"
|
---|
| 496 | "\t\t\t// * Mangling for non-standard complex types is by best guess\n"
|
---|
| 497 | "\t\t\t// * _FloatN is supposed to encode as \"DF\"N\"_\"; modified for same reason as above.\n"
|
---|
| 498 | "\t\t\t// * unused mangling identifiers:\n"
|
---|
| 499 | "\t\t\t// - \"z\" ellipsis\n"
|
---|
| 500 | "\t\t\t// - \"Dd\" IEEE 754r 64-bit decimal floating point (borrowed for _Float32x)\n"
|
---|
| 501 | "\t\t\t// - \"De\" IEEE 754r 128-bit decimal floating point\n"
|
---|
| 502 | "\t\t\t// - \"Df\" IEEE 754r 32-bit decimal floating point\n"
|
---|
| 503 | "\t\t\t// - \"Dh\" IEEE 754r 16-bit decimal floating point (borrowed for _Float16)\n"
|
---|
| 504 | "\t\t\t// - \"DF\"N\"_\" ISO/IEC TS 18661 N-bit binary floating point (_FloatN)\n"
|
---|
| 505 | "\t\t\t// - \"Di\" char32_t\n"
|
---|
| 506 | "\t\t\t// - \"Ds\" char16_t\n";
|
---|
| 507 |
|
---|
| 508 | code << "\t\t\tconst std::string basicTypes[BasicType::NUMBER_OF_BASIC_TYPES] = {" << endl;
|
---|
| 509 | for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
|
---|
| 510 | code << "\t\t\t\t\"" << graph[r].mangled << "\"," << setw(9 - strlen(graph[r].mangled)) << ' ' << "// " << graph[r].type << endl;
|
---|
| 511 | } // for
|
---|
| 512 | code << "\t\t\t}; // basicTypes" << endl;
|
---|
| 513 | code << "\t\t\t"; // indentation for end marker
|
---|
| 514 |
|
---|
[ada4575] | 515 | if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ManglerCommon );
|
---|
[70a3e16] | 516 | code << str.substr( start );
|
---|
| 517 |
|
---|
| 518 | output( file, ManglerCommon, code );
|
---|
| 519 | // cout << code.str();
|
---|
| 520 | } // main
|
---|
| 521 |
|
---|
| 522 | // Local Variables: //
|
---|
| 523 | // tab-width: 4 //
|
---|
| 524 | // mode: c++ //
|
---|
| 525 | // compile-command: "g++-8 -Wall -Wextra BasicTypes-gen.cc" //
|
---|
| 526 | // End: //
|
---|