| 1 | #include <algorithm>
 | 
|---|
| 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
 | 
|---|
| 11 | #include "config.h"                                                                     // configure info
 | 
|---|
| 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
 | 
|---|
| 73 | 
 | 
|---|
| 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 },
 | 
|---|
| 77 | 
 | 
|---|
| 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 },
 | 
|---|
| 80 | 
 | 
|---|
| 81 |         { SignedInt, "SignedInt", "I", "signed int", "i", Signed, UnsignedInt, LongSignedInt, -1, 3 },
 | 
|---|
| 82 |         { UnsignedInt, "UnsignedInt", "UI", "unsigned int", "j", Unsigned, LongUnsignedInt, LongSignedInt, -1, 3 },
 | 
|---|
| 83 | 
 | 
|---|
| 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 },
 | 
|---|
| 86 | 
 | 
|---|
| 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 },
 | 
|---|
| 89 | 
 | 
|---|
| 90 |         { SignedInt128, "SignedInt128", "IB", "__int128", "n", Signed, UnsignedInt128, uFloat16, -1, 6 },
 | 
|---|
| 91 |         { UnsignedInt128, "UnsignedInt128", "UIB", "unsigned __int128", "o", Unsigned, uFloat16, -1, -1, 6 },
 | 
|---|
| 92 | 
 | 
|---|
| 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 },
 | 
|---|
| 100 | 
 | 
|---|
| 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 },
 | 
|---|
| 108 | 
 | 
|---|
| 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 },
 | 
|---|
| 118 | 
 | 
|---|
| 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 | 
 | 
|---|
| 262 |         #define STARTMK "// GENERATED START, DO NOT EDIT"
 | 
|---|
| 263 |         #define ENDMK "// GENERATED END"
 | 
|---|
| 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 | 
 | 
|---|
| 269 |         fstream file;
 | 
|---|
| 270 |         stringstream buffer, code;
 | 
|---|
| 271 |         string str;
 | 
|---|
| 272 |         size_t start, end;
 | 
|---|
| 273 | 
 | 
|---|
| 274 | 
 | 
|---|
| 275 |         #define Type TOP_SRCDIR "src/SynTree/Type.h"
 | 
|---|
| 276 |         resetInput( file, Type, buffer, code, str );
 | 
|---|
| 277 | 
 | 
|---|
| 278 |         if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", Type );
 | 
|---|
| 279 |         start += sizeof( STARTMK );                                                     // includes newline
 | 
|---|
| 280 |         code << str.substr( 0, start );
 | 
|---|
| 281 | 
 | 
|---|
| 282 |         code << "\t" << BYMK << endl;
 | 
|---|
| 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 | 
 | 
|---|
| 291 |         if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", Type );
 | 
|---|
| 292 |         code << str.substr( start );
 | 
|---|
| 293 | 
 | 
|---|
| 294 |         output( file, Type, code );
 | 
|---|
| 295 |         // cout << code.str();
 | 
|---|
| 296 | 
 | 
|---|
| 297 |         
 | 
|---|
| 298 |         #define ConversionCost TOP_SRCDIR "src/ResolvExpr/ConversionCost.cc"
 | 
|---|
| 299 |         resetInput( file, ConversionCost, buffer, code, str );
 | 
|---|
| 300 | 
 | 
|---|
| 301 |         if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ConversionCost );
 | 
|---|
| 302 |         start += sizeof( STARTMK );                                                     // includes newline
 | 
|---|
| 303 |         code << str.substr( 0, start );
 | 
|---|
| 304 | 
 | 
|---|
| 305 |         code << "\t" << BYMK << endl;
 | 
|---|
| 306 |         code << "\t/* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves)" << endl;
 | 
|---|
| 307 |         for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
 | 
|---|
| 308 |                 code << '\t' << left;
 | 
|---|
| 309 |                 if ( graph[c].rank != graph[c + 1].rank ) {
 | 
|---|
| 310 |                         code << right << setw(30) << graph[c].type << left;
 | 
|---|
| 311 |                 } else if ( graph[c].rank != graph[c + 2].rank ) {
 | 
|---|
| 312 |                         code << string( 10, ' ' ) << setw(25) << graph[c].type << setw(25) << graph[c + 1].type;
 | 
|---|
| 313 |                         c += 1;
 | 
|---|
| 314 |                 } else {
 | 
|---|
| 315 |                         code << setw(20) << graph[c].type << setw(20) << graph[c + 1].type << setw(20) << graph[c + 2].type;
 | 
|---|
| 316 |                         c += 2;
 | 
|---|
| 317 |                 } // if
 | 
|---|
| 318 |                 code << endl;
 | 
|---|
| 319 |         } // for
 | 
|---|
| 320 |         code << right << "\t*/" << endl;
 | 
|---|
| 321 |         code << "\t";                                                                           // indentation for end marker
 | 
|---|
| 322 | 
 | 
|---|
| 323 |         if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
 | 
|---|
| 324 |         if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
 | 
|---|
| 325 |         end += sizeof( STARTMK );
 | 
|---|
| 326 |         code << str.substr( start, end - start );
 | 
|---|
| 327 |         
 | 
|---|
| 328 |         code << "\t" << BYMK << endl;
 | 
|---|
| 329 |         code << "\tstatic const int costMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // path length from root to node" << endl
 | 
|---|
| 330 |                  << "\t\t/*         ";
 | 
|---|
| 331 |         for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
 | 
|---|
| 332 |                 code << setw(5) << graph[r].abbrev;
 | 
|---|
| 333 |         } // for
 | 
|---|
| 334 |         code << " */" << endl;
 | 
|---|
| 335 |         for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
 | 
|---|
| 336 |                 code << "\t\t/*" << setw(6) << graph[r].abbrev << "*/ {";
 | 
|---|
| 337 |                 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
 | 
|---|
| 338 |                         code << setw(4) << costMatrix[r][c] << ",";
 | 
|---|
| 339 |                 } // for
 | 
|---|
| 340 |                 code << " }," << endl;
 | 
|---|
| 341 |         } // for
 | 
|---|
| 342 |         code << "\t}; // costMatrix" << endl;
 | 
|---|
| 343 | 
 | 
|---|
| 344 |         // maximum conversion cost from int
 | 
|---|
| 345 |         code << "\tstatic const int maxIntCost = " << *max_element(costMatrix[SignedInt], costMatrix[SignedInt] + NUMBER_OF_BASIC_TYPES) << ";" << endl;
 | 
|---|
| 346 |         code << "\t";                                                                           // indentation for end marker
 | 
|---|
| 347 |         
 | 
|---|
| 348 |         if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
 | 
|---|
| 349 |         if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
 | 
|---|
| 350 |         end += sizeof( STARTMK );
 | 
|---|
| 351 |         code << str.substr( start, end - start );
 | 
|---|
| 352 | 
 | 
|---|
| 353 |         code << "\t" << BYMK << endl;
 | 
|---|
| 354 |         code << "\tstatic const int signMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion" << endl
 | 
|---|
| 355 |                  << "\t\t/*         ";
 | 
|---|
| 356 |         for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
 | 
|---|
| 357 |                 code << setw(5) << graph[r].abbrev;
 | 
|---|
| 358 |         } // for
 | 
|---|
| 359 |         code << " */" << endl;
 | 
|---|
| 360 |         for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
 | 
|---|
| 361 |                 code << "\t\t/*" << setw(6) << graph[r].abbrev << "*/ {";
 | 
|---|
| 362 |                 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
 | 
|---|
| 363 |                         code << setw(4) << signMatrix[r][c] << ",";
 | 
|---|
| 364 |                 } // for
 | 
|---|
| 365 |                 code << " }," << endl;
 | 
|---|
| 366 |         } // for
 | 
|---|
| 367 |         code << "\t}; // signMatrix" << endl;
 | 
|---|
| 368 |         code << "\t";                                                                           // indentation for end marker
 | 
|---|
| 369 | 
 | 
|---|
| 370 |         if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
 | 
|---|
| 371 |         code << str.substr( start );
 | 
|---|
| 372 | 
 | 
|---|
| 373 |         output( file, ConversionCost, code );
 | 
|---|
| 374 |         // cout << code.str();
 | 
|---|
| 375 | 
 | 
|---|
| 376 | 
 | 
|---|
| 377 |         #define CommonType TOP_SRCDIR "src/ResolvExpr/CommonType.cc"
 | 
|---|
| 378 |         resetInput( file, CommonType, buffer, code, str );
 | 
|---|
| 379 | 
 | 
|---|
| 380 |         if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", CommonType );
 | 
|---|
| 381 |         start += sizeof( STARTMK );                                                     // includes newline
 | 
|---|
| 382 |         code << str.substr( 0, start );
 | 
|---|
| 383 | 
 | 
|---|
| 384 |         enum { PER_ROW = 6 };
 | 
|---|
| 385 |         code << "\t" << BYMK << endl;
 | 
|---|
| 386 |         code << "\t#define BT BasicType::" << endl;
 | 
|---|
| 387 |         code << "\tstatic const BasicType::Kind commonTypes[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor" << endl
 | 
|---|
| 388 |              << "\t\t/*\t\t ";
 | 
|---|
| 389 |         for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
 | 
|---|
| 390 |                 code << setw(24) << graph[r].abbrev;
 | 
|---|
| 391 |                 if ( (r+1) % PER_ROW == 0 ) {
 | 
|---|
| 392 |                         code << endl << "\t\t\t\t ";
 | 
|---|
| 393 |                 } // if
 | 
|---|
| 394 |         } // for
 | 
|---|
| 395 |         code << "*/" << endl;
 | 
|---|
| 396 |         for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
 | 
|---|
| 397 |                 code << "\t\t\t\t  {\n\t\t/*" << setw(6) << graph[r].abbrev << "*/";
 | 
|---|
| 398 |                 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
 | 
|---|
| 399 |                         string s = string{"BT "} + graph[commonTypeMatrix[r][c]].name;
 | 
|---|
| 400 |                         code << setw(23) << s << ",";
 | 
|---|
| 401 |                         if ( (c+1) % PER_ROW == 0 ) {
 | 
|---|
| 402 |                                 code << endl << "\t\t\t\t  ";
 | 
|---|
| 403 |                         } // if
 | 
|---|
| 404 |                 } // for
 | 
|---|
| 405 |                 code << "}," << endl;
 | 
|---|
| 406 |         } // for
 | 
|---|
| 407 |         code << "\t}; // commonTypes" << endl;
 | 
|---|
| 408 |         code << "\t#undef BT" << endl;
 | 
|---|
| 409 |         code << "\t";                                                                           // indentation for end marker
 | 
|---|
| 410 | 
 | 
|---|
| 411 |         if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", CommonType );
 | 
|---|
| 412 |         code << str.substr( start );
 | 
|---|
| 413 | 
 | 
|---|
| 414 |         output( file, CommonType, code );
 | 
|---|
| 415 |         // cout << code.str();
 | 
|---|
| 416 | 
 | 
|---|
| 417 | 
 | 
|---|
| 418 |         #define ManglerCommon TOP_SRCDIR "src/SymTab/ManglerCommon.cc"
 | 
|---|
| 419 |         resetInput( file, ManglerCommon, buffer, code, str );
 | 
|---|
| 420 | 
 | 
|---|
| 421 |         if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ManglerCommon );
 | 
|---|
| 422 |         start += sizeof( STARTMK );                                                     // includes newline
 | 
|---|
| 423 |         code << str.substr( 0, start );
 | 
|---|
| 424 | 
 | 
|---|
| 425 |         code << "\t\t\t// GENERATED BY " __FILE__ << endl;
 | 
|---|
| 426 |         code <<
 | 
|---|
| 427 |                 "\t\t\t// NOTES ON MANGLING:\n"
 | 
|---|
| 428 |                 "\t\t\t// * Itanium spec says that Float80 encodes to \"e\" (like LongDouble), but the distinct lengths cause resolution problems.\n"
 | 
|---|
| 429 |                 "\t\t\t// * Float128 is supposed to encode to \"g\", but I wanted it to mangle equal to LongDouble.\n"
 | 
|---|
| 430 |                 "\t\t\t// * Mangling for non-standard complex types is by best guess\n"
 | 
|---|
| 431 |                 "\t\t\t// * _FloatN is supposed to encode as \"DF\"N\"_\"; modified for same reason as above.\n"
 | 
|---|
| 432 |                 "\t\t\t// * unused mangling identifiers:\n"
 | 
|---|
| 433 |                 "\t\t\t//   - \"z\" ellipsis\n"
 | 
|---|
| 434 |                 "\t\t\t//   - \"Dd\" IEEE 754r 64-bit decimal floating point (borrowed for _Float32x)\n"
 | 
|---|
| 435 |                 "\t\t\t//   - \"De\" IEEE 754r 128-bit decimal floating point\n"
 | 
|---|
| 436 |                 "\t\t\t//   - \"Df\" IEEE 754r 32-bit decimal floating point\n"
 | 
|---|
| 437 |                 "\t\t\t//   - \"Dh\" IEEE 754r 16-bit decimal floating point (borrowed for _Float16)\n"
 | 
|---|
| 438 |                 "\t\t\t//   - \"DF\"N\"_\" ISO/IEC TS 18661 N-bit binary floating point (_FloatN)\n"
 | 
|---|
| 439 |                 "\t\t\t//   - \"Di\" char32_t\n"
 | 
|---|
| 440 |                 "\t\t\t//   - \"Ds\" char16_t\n";
 | 
|---|
| 441 |                 
 | 
|---|
| 442 |         code << "\t\t\tconst std::string basicTypes[BasicType::NUMBER_OF_BASIC_TYPES] = {" << endl;
 | 
|---|
| 443 |         for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
 | 
|---|
| 444 |                 code << "\t\t\t\t\"" << graph[r].mangled << "\"," << setw(9 - strlen(graph[r].mangled)) << ' ' << "// " << graph[r].type << endl;
 | 
|---|
| 445 |         } // for        
 | 
|---|
| 446 |         code << "\t\t\t}; // basicTypes" << endl;
 | 
|---|
| 447 |         code << "\t\t\t";                                                                       // indentation for end marker
 | 
|---|
| 448 | 
 | 
|---|
| 449 |         if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ManglerCommon );
 | 
|---|
| 450 |         code << str.substr( start );
 | 
|---|
| 451 | 
 | 
|---|
| 452 |         output( file, ManglerCommon, code );
 | 
|---|
| 453 |         // cout << code.str();
 | 
|---|
| 454 | } // main
 | 
|---|
| 455 | 
 | 
|---|
| 456 | // Local Variables: //
 | 
|---|
| 457 | // tab-width: 4 //
 | 
|---|
| 458 | // mode: c++ //
 | 
|---|
| 459 | // compile-command: "g++-8 -Wall -Wextra BasicTypes-gen.cc" //
 | 
|---|
| 460 | // End: //
 | 
|---|