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