Changeset 102
- Timestamp:
- 05/01/07 02:24:53 (1 year ago)
- Files:
-
- trunk/blade/Blade.d (modified) (4 diffs)
- trunk/blade/BladeMixin.d (added)
- trunk/blade/BladeParse.d (modified) (1 diff)
- trunk/blade/CodegenX87.d (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/Blade.d
r101 r102 243 243 // of the types in the tuple. 244 244 245 // Create a string representing the type A.246 245 // Pure imaginary types are treated identically to pure reals, since they result in the 247 246 // same asm code. 247 template fpTypeToString(A) 248 { 249 static if (is(A==real) || is(A==ireal)|| is(A==creal)) const char [] fpTypeToString="R"; 250 else static if (is(A==double) || is(A==idouble) || is(A==cdouble)) const char [] fpTypeToString="D"; 251 else static if (is(A==float) || is(A==ifloat) || is(A==cfloat)) const char [] fpTypeToString="F"; 252 } 253 254 // Create a string representing the type A. 248 255 template singleTypeToString(A) 249 256 { 250 static if (is(A == real[]) || is(A == ireal[])) const char [] singleTypeToString = "R"; 251 else static if (is(A == double[])|| is(A == idouble[])) const char [] singleTypeToString = "D"; 252 else static if (is(A == float[]) || is(A == ifloat[])) const char [] singleTypeToString = "F"; 253 else static if (is(A == creal[]) || is(A == cdouble[]) || is(A==cfloat[])) const char [] singleTypeToString = "Z"; 254 255 else static if (is(A == real) || is (A == ireal)) const char [] singleTypeToString = "r"; 256 else static if (is(A == double) || is (A == idouble)) const char [] singleTypeToString = "d"; 257 else static if (is(A == float) || is (A == ifloat)) const char [] singleTypeToString = "f"; 258 else static if (is(A : creal)) const char [] singleTypeToString = "z"; 259 else const char [] singleTypeToString = "?"; 257 const char [] singleTypeToString = "S" ~ fpTypeToString!(A); // scalar 258 } 259 260 template singleTypeToString(A: A[]) 261 { 262 static if (is(A:creal)) 263 const char [] singleTypeToString = "Z" ~ fpTypeToString!(A); // complex vector 264 else 265 const char [] singleTypeToString = "V" ~ fpTypeToString!(A); // vector 260 266 } 261 267 … … 264 270 template vectorTupleToString(X...) 265 271 { 266 //static if (X.length==1) const char [] vectorTupleToString = singleTypeToString!(X[0])~\0;267 //else const char [] vectorTupleToString = singleTypeToString!(X[0]) ~ \0 ~ vectorTupleToString!(X[1..$]);268 static if (X.length==1) const char [] vectorTupleToString = X[0].stringof ~\0;269 else const char [] vectorTupleToString = X[0].stringof ~ \0 ~ vectorTupleToString!(X[1..$]);272 static if (X.length==1) const char [] vectorTupleToString = singleTypeToString!(X[0])~\0; 273 else const char [] vectorTupleToString = singleTypeToString!(X[0]) ~ \0 ~ vectorTupleToString!(X[1..$]); 274 // static if (X.length==1) const char [] vectorTupleToString = X[0].stringof ~\0; 275 // else const char [] vectorTupleToString = X[0].stringof ~ \0 ~ vectorTupleToString!(X[1..$]); 270 276 } 271 277 … … 280 286 if (raw[i]=='\0') { 281 287 char [] a; 288 /* 282 289 switch(raw[start..i]) { 283 290 case (ifloat[]).stringof: … … 302 309 // a = "??" ~ raw[start..i]; break; 303 310 } 304 s~=a; start=i+1; } 311 s~=a; 312 */ 313 s~=raw[start..i]; 314 start=i+1; } 305 315 } 306 316 return s; trunk/blade/BladeParse.d
r101 r102 18 18 return s; 19 19 } 20 21 bool isDigit(char c) { return c>='0' && c<='9'; } 22 bool isHexDigit(char c) { return c>='0' && c<='9' || c>='a' && c<='f' || c>='A' && c<='F'; } 23 bool isAlpha(char c) { 24 return (c>='a' && c<='z') || (c>='A' && c<='Z'); 25 } 26 // Underscores allowed 27 bool isUnderscoreDigit(char c) { return c=='_' || c>='0' && c<='9'; } 28 bool isUnderscoreHexDigit(char c) { return c=='_' || c>='0' && c<='9' || c>='a' && c<='f' || c>='A' && c<='F'; } 29 bool isUnderscoreAlpha(char c) { 30 return c=='_' || (c>='a' && c<='z') || (c>='A' && c<='Z'); 31 } 32 33 unittest { 34 assert(isHexDigit('9')); 35 } 36 37 // Extract a D identifier. Return the remainder of the string. 38 char [] parseIdentifier(char [] s, out char [] rest) 39 { 40 int i=0; 41 if (!isUnderscoreAlpha(s[0])) { assert(0, "Identifier expected"); return "";} 42 while (i<s.length && (isUnderscoreAlpha(s[i])||isDigit(s[i]))) ++i; 43 char [] ident=s[0..i]; 44 while (i<s.length && s[i]==' ') ++i; // skip trailing spaces 45 rest = s[i..$]; 46 return ident; 47 } 48 49 char [] parseNumber(char [] s, out char[] rest) 50 { 51 int k=0; 52 while(k<s.length && isDigit(s[k])) ++k; 53 if (k<s.length-1 && s[k]=='.' && s[k+1]!='.') { 54 ++k; 55 while(k<s.length && isDigit(s[k])) ++k; 56 if (k<s.length && s[k]=='e') { ++k; 57 if (k<s.length && (s[k]=='+' || s[k]=='-')) ++k; 58 while(k<s.length && isDigit(s[k])) ++k; 59 } 60 if (k<s.length && (s[k]=='L' || s[k]=='f')) ++k; 61 } 62 char [] ident = s[0..k]; 63 int i=k; 64 while (i<s.length && s[i]==' ') ++i; // skip trailing spaces 65 rest = s[i..$]; 66 return ident; 67 } 68 69 char [] parseOperator(char [] s, out char [] rest) 70 { 71 int i=1; 72 if (s[0]=='+' || s[0]=='-' || s[0]=='*') { 73 if (s.length>1 && s[1]=='=') ++i; 74 int k=i; 75 while (k<s.length && s[k]==' ') ++k; // skip trailing spaces 76 rest = s[k..$]; 77 return s[0..i]; 78 } else { 79 assert(0, `Operator expected, not "` ~ s~ `"`); 80 return ""; 81 } 82 } 83 84 char [] parseQualifiedName(char [] s, out char[] rest) 85 { 86 char [] r; 87 char [] id = parseIdentifier(s, r); 88 while (r.length>0 && r[0]=='.') { 89 int i; 90 for (i=1; r[i]==' '; ++i) {} 91 id ~= "." ~ parseIdentifier(r[i..$], r); 92 } 93 rest=r; 94 return id; 95 } 96 20 97 21 98 // Some functions to grab information from the typestring. trunk/blade/CodegenX87.d
r101 r102 20 20 } 21 21 22 int operatorLength(char [] s) 23 { 24 if (s[1]=='=') return 2; else return 1; 25 } 26 22 27 // Converts an infix string into postfix. Also strips off the # symbols. 23 28 // Apply x87-specific optimisations during the conversion. … … 28 33 29 34 int x = exprLength(operations); 35 int y = x + 2; // end of the operator 36 if (operations[y]=='=') ++y; // Support +=, -=, *=, /=. 30 37 char [] first = operations[0..x+1]; 31 char [] second = operations[ x+2..$];38 char [] second = operations[y..$]; 32 39 if (first[0]=='(') { 33 40 first = makePostfixForX87(first[1..first.length-1], typelist); … … 35 42 if (second[0]=='(') { 36 43 second = makePostfixForX87(second[1..second.length-1], typelist); 37 } else if (second[0]=='#') second = operations[ x+3..$];44 } else if (second[0]=='#') second = operations[y+1..$]; 38 45 39 46 // x87 OPTIMISATION #1 … … 48 55 if (operations[x+1]=='+') { 49 56 if (second[second.length-1]=='*'&& first[first.length-1]!='*') { 50 return second ~ first ~ operations[x+1.. x+2];57 return second ~ first ~ operations[x+1..y]; 51 58 } 52 59 } … … 67 74 return second ~ first ~ "_"; 68 75 } 69 return first ~ second ~ operations[x+1.. x+2];76 return first ~ second ~ operations[x+1..y]; 70 77 } 71 78 … … 126 133 char [] operandSize(char [] typestr) 127 134 { 128 switch(typestr) { 129 case "SR": 130 case "VR": return "real ptr "; 131 case "SD": 132 case "VD": return "double ptr "; 133 case "SF": 134 case "VF": return "float ptr "; 135 } 136 } 137 138 char [] opToX87(char op) 139 { 140 switch (op) { 141 case '*': 142 case '.': return "fmul"; 143 case '+': return "fadd"; 144 case '-': return "fsub"; 145 case '_': return "fsubr"; 146 } 147 } 135 switch(typestr[1]) { 136 case 'R': return "real ptr "; 137 case 'D': return "double ptr "; 138 case 'F': return "float ptr "; 139 } 140 } 141 142 char [][char] opToX87() { 143 return ['*':"fmul"[], '.': "fmul", '+': "fadd", '-': "fsub", '_': "fsubr"]; } 144 148 145 149 146 static if (real.sizeof==10) const char [] REALSIZE = "10"; … … 153 150 char [] vectorSize(char [] typestr) 154 151 { 155 switch (typestr ) {156 case "VD": return "8";157 case "VF": return "4";158 case "VR": return REALSIZE;152 switch (typestr[1]) { 153 case 'D': return "8"; 154 case 'F': return "4"; 155 case 'R': return REALSIZE; 159 156 } 160 157 } … … 380 377 if (isInstruction(operations[done])) { 381 378 // Perform an arithemetic operation on the top two FPU stack items. 382 next = " " ~ opToX87 (operations[done])~ "p ST(1), ST;"\n;379 next = " " ~ opToX87[operations[done]] ~ "p ST(1), ST;"\n; 383 380 mainbody ~= next; firstbody ~= next; 384 381 ++done; … … 398 395 // 80-bit vectors must be loaded onto the FPU stack first 399 396 next = " fld real ptr [" ~ vectorRegister[vectorNum(typelist, operations[done])] ~ "];\n" 400 ~ " " ~ opToX87 (operations[done+1])~ "p ST(1), ST;\n";397 ~ " " ~ opToX87[operations[done+1]] ~ "p ST(1), ST;\n"; 401 398 } else { 402 next = " " ~ opToX87 (operations[done+1])~ " "399 next = " " ~ opToX87[operations[done+1]] ~ " " 403 400 ~ indexedVector(typelist, operations[done] ) ~ ";\n"; 404 401 }
