Changeset 153
- Timestamp:
- 12/01/07 03:36:16 (10 months ago)
- Files:
-
- trunk/blade/Blade.d (modified) (10 diffs)
- trunk/blade/BladeDemo.d (modified) (2 diffs)
- trunk/blade/BladeRank.d (modified) (1 diff)
- trunk/blade/BladeSimplify.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/Blade.d
r152 r153 47 47 public import blade.SyntaxTree : AbstractSyntaxTree, syntaxtreeof, AST, Symbol; 48 48 private import blade.BladeUtil : enquote; 49 private import blade.BladeSimplify : simplifySyntaxTree, RevisedExpression, isStrided; 49 private import blade.BladeRank : isStrided; 50 private import blade.BladeSimplify : simplifySyntaxTree, RevisedExpression; 50 51 public import blade.CodegenX86 : generateCodeForAsmX87, MAX_X87_VECTORS, 51 52 generateCodeForSSE, MAX_SSE_VECTORS; … … 280 281 // } 281 282 result ~= "assert(" 282 ~ getValueForSymbol(revised.mapping[i], tree, revised) 283 ~ ".length==" ~ getValueForSymbol(revised.mapping[firstVector], tree, revised) 284 ~ ".length, `Vector length mismatch`);"\n; 283 ~ getDimensionLengthForSymbol(revised.mapping[i], tree, revised, 0) 284 ~ "==" ~ getDimensionLengthForSymbol(revised.mapping[firstVector], tree, revised, 0) 285 ~ ", `Vector length mismatch`);"\n; 286 // ~ ".length==" ~ getValueForSymbol(revised.mapping[firstVector], tree, revised) 287 // ~ ".length, `Vector length mismatch`);"\n; 285 288 } 286 289 } … … 294 297 for (int i=0; i<revised.mapping.length;++i) { 295 298 if (revised.rank[i]=='1'){ 296 result ~= "assert( (cast(size_t)( " ~ getValueForSymbol(revised.mapping[i], tree, revised)297 ~ " .ptr)& 0x0F) == 0, `SSE Vector misalignment: " ~ getValueForSymbol(revised.mapping[i], tree, revised) ~ "`);"\n;299 result ~= "assert( (cast(size_t)(&" ~ getValueForSymbol(revised.mapping[i], tree, revised) 300 ~ "[0])& 0x0F) == 0, `SSE Vector misalignment: " ~ getValueForSymbol(revised.mapping[i], tree, revised) ~ "`);"\n; 298 301 } 299 302 } … … 310 313 311 314 // Return a vector which contains the length of the expression. 312 // If possible, a vector with known length will be chosen. 315 // If possible, a vector with known (static) length will be chosen. 316 // If this is not possible, a normal dynamic array will be used. 317 // If all else fails, a sliced vector will be used. 313 318 int findVectorForLength(AbstractSyntaxTree tree, RevisedExpression revised) 314 319 { … … 331 336 } 332 337 } 333 return dynamic>=0? dynamic : strided; 334 338 return dynamic>=0? dynamic : strided; 339 } 340 341 char [] getDimensionLengthForSymbol(char c, AbstractSyntaxTree tree, RevisedExpression revised, int dimension) 342 { 343 return getValueForSymbol(c, tree, revised) ~ ".length"; 335 344 } 336 345 … … 343 352 if (c-'A'<tree.symbolTable.length) { 344 353 v = tree.symbolTable[c-'A'].value; 345 } 346 // else it's a compound or an indexed array 347 else if (revised.compounds[c-'A'-tree.symbolTable.length][$-1]==']') { 354 } else { // else it's a compound or an indexed array 355 char [] comp = revised.compounds[c-'A'-tree.symbolTable.length]; 356 357 if (comp[$-1]==']') { 348 358 // indexed array, possibly involving slicing 349 359 int numbrack=0; … … 352 362 // Replace the last slice [a..b] operation with [a+firstIndexExpr] 353 363 // or if no slices exist, append [firstIndexExpr] to the end. 354 int endk = revised.compounds[c-'A'-tree.symbolTable.length].length;355 364 int numbracks = 0; 356 365 bool isSlice=false; 357 366 char [] newSlice; 358 367 359 for (int k = revised.compounds[c-'A'-tree.symbolTable.length].length-1;k>=1; --k) {360 char d = revised.compounds[c-'A'-tree.symbolTable.length][k];368 for (int k = comp.length-1;k>=1; --k) { 369 char d = comp[k]; 361 370 if (d == ']') { ++numbracks; } 362 371 if (d == '[') { --numbracks; } 363 372 364 if (d==']' && numbracks==1) { newSlice=""; endk = k;}373 if (d==']' && numbracks==1) { newSlice=""; } 365 374 else if (d=='.' && numbracks==1) { isSlice=true; 366 if(numSlicesRemaining>0){ newSlice=""; endk=k;}375 if(numSlicesRemaining>0){ newSlice=""; } 367 376 else newSlice = "." ~ newSlice; 368 377 } 369 378 else if (d=='[' && numbracks==0) { 370 379 if (isSlice && numSlicesRemaining>0) { 371 v = "[" ~ newSlice //revised.compounds[c-'A'-tree.symbolTable.length][k+1..endk]380 v = "[" ~ newSlice 372 381 ~ "+" ~ firstIndexExpr ~ "]" ~ v; 373 382 --numSlicesRemaining; 374 383 } else { 375 v = "[" ~ newSlice ~ "]" ~ v; //revised.compounds[c-'A'-tree.symbolTable.length][k+1..endk] ~ "]";384 v = "[" ~ newSlice ~ "]" ~ v; 376 385 } 377 386 newSlice=""; … … 379 388 } else if (d==',' && numbracks==1) { 380 389 if (isSlice && numSlicesRemaining>0) { 381 v = "[" ~ newSlice //revised.compounds[c-'A'-tree.symbolTable.length][k+1..endk]390 v = "[" ~ newSlice 382 391 ~ "+" ~ firstIndexExpr ~ "]" ~ v; 383 392 --numSlicesRemaining; … … 395 404 v~="[" ~ firstIndexExpr ~ "]"; 396 405 } 397 return tree.symbolTable[revised.compounds[c-'A'-tree.symbolTable.length][0]-'A'].value ~ v; 398 /* 399 foreach_reverse(d; revised.compounds[c-'A'-tree.symbolTable.length]) { 400 if (d>='A' && d<='Z') v = tree.symbolTable[d-'A'].value ~ v; 401 else if (d==',' && numbrack==1) { 402 if (!hasSliced) v = "][" ~ v; 403 else assert(0, "sliced operations not yet supported"); 404 } if (!hasSliced && numbrack==0 && d=='.') { 405 if (prev == '.') { 406 hasSliced=true; 407 v = "+" ~ firstIndexExpr ~ v 408 } // else we'll drop the . 409 }else v = d ~ v; 410 411 if (d=='[') --numbrack; 412 if (d==']') ++numbrack; 413 prev = d; 414 } 415 if (firstIndexExpr!="") return v ~ firstIndexExpr; 416 else return v; 417 */ 418 } else { // compound expression 419 foreach(d; revised.compounds[c-'A'-tree.symbolTable.length]) { 406 return tree.symbolTable[comp[0]-'A'].value ~ v; 407 } else { // simple compound expression 408 foreach(d; comp) { 420 409 if (d>='A' && d<='Z') v ~= tree.symbolTable[d-'A'].value; 421 410 else v ~= d; 422 411 } 423 412 } 413 } 424 414 for (; numSlicesRemaining>0; --numSlicesRemaining) { 425 415 v~="[" ~ firstIndexExpr ~ "]"; … … 428 418 } 429 419 420 // Convert the compound expression str back into its values. 421 char [] getValueForCompoundExpression(char [] str, Symbol[] table) 422 { 423 char [] v=""; 424 foreach(d; str) { 425 if (d>='A' && d<='Z') v ~= table[d-'A'].value; 426 else v ~= d; 427 } 428 return v; 429 } 430 430 431 431 432 // Generate inline D code for the expression 432 433 char [] DCodeGenerator(AbstractSyntaxTree tree, RevisedExpression revised) 433 434 { 434 // assert(!strided, tree.expression ~ "Strided vectors are not yet supported");435 436 435 int lenvec = findVectorForLength(tree, revised); 437 436 char [] result = "for (int blade_index=0; blade_index<" 438 ~ getValueForSymbol(revised.mapping[lenvec], tree, revised) ~ 439 //~ tree.symbolTable[lenvec].value ~ 440 ".length; ++blade_index) {"; 437 ~ getDimensionLengthForSymbol(revised.mapping[lenvec], tree, revised, 0) ~ 438 "; ++blade_index) {"\n; 441 439 foreach (c; revised.expression) { 442 440 if (c>='A' && c<'Z') { 443 441 // restore all symbols into the expression 444 // result ~= getValueForSymbol(c, tree, revised);445 442 // If it's a vector, index it 446 443 if (revised.rank[c-'A']=='1') trunk/blade/BladeDemo.d
r152 r153 35 35 36 36 mixin(vectorize(` a += (d[2..$-1]*2.01*a[2]-another[][1])["abc".length-3..$]`)); 37 37 /+ 38 38 mixin(vectorize(" a-= 2.01*( 3.04+k)*r")); 39 39 mixin(vectorize("q+= q*2.01")); … … 43 43 mixin(vectorize("a+=6*another[1]")); 44 44 mixin(vectorize("a+=6*another[1][]")); 45 45 +/ 46 46 mixin(vectorize("a[0..2]+=6*another[0..$,1]")); 47 47 trunk/blade/BladeRank.d
r152 r153 324 324 assert(!hasScalarMultiply("(C[B])[B..A]","002") ); 325 325 } 326 327 public: 328 // return true if the given symbol has a non-zero stride. 329 // This happens whenever there is a slice (including a complete slice) 330 // where there is a later incomplete slice or index. 331 // 332 bool isStrided(char [] s) 333 { 334 assert(s[$-1]==']', "BLADE ICE"); 335 if (s[$-2..$]=="[]") return false; 336 int numbrack=0; 337 int paren = 0; 338 bool hasSliced=false; 339 bool maybeSlice = false; 340 bool startIndex=false; 341 for(int i=1; i<s.length; ++i) { 342 if (s[i]=='(') ++paren; 343 else if (s[i]==')') --paren; 344 if (paren==0 && s[i]==']') { 345 if (startIndex && hasSliced) return true; 346 numbrack--; 347 if (s[i-1]=='[') { startIndex=false; } 348 } 349 if (paren==0 && s[i]=='[') { 350 startIndex = true; 351 maybeSlice = false; 352 numbrack++; 353 } 354 if (paren==0 && numbrack==1 && s[i]==',') { 355 if (hasSliced && startIndex) return true; 356 if (maybeSlice) hasSliced = true; 357 startIndex = true; 358 } 359 if (paren==0 && numbrack==2 && s[i]==',') { startIndex=false; hasSliced=true; } 360 if (paren==0 && numbrack==1 && s[i]=='.' && s[i-1]=='.') { 361 startIndex = false; // [..] slices don't move the index to the next value. 362 maybeSlice = true; 363 } 364 } 365 return false; 366 } 367 368 private: 369 unittest { 370 assert(!isStrided("A[3..5][]")); 371 assert(!isStrided("A[2..7][3]")); 372 assert(!isStrided("A[][2]")); 373 assert(!isStrided("A[2,[2,7]]")); 374 assert(isStrided("A[[2,7],3]")); 375 assert(isStrided("C[D..$,D]")); 376 assert(!isStrided("A[7][B[[1,3],2]..6]")); 377 } trunk/blade/BladeSimplify.d
r152 r153 29 29 30 30 public import blade.SyntaxTree : AbstractSyntaxTree, Symbol; 31 private import blade.BladeRank : exprLength, exprRank, subexprRank, hasScalarMultiply, getRankErrorText; 31 private import blade.BladeRank : exprLength, exprRank, subexprRank, 32 hasScalarMultiply, getRankErrorText, isStrided; 32 33 33 34 … … 248 249 return r; 249 250 } 250 251 public:252 // return true if the given symbol has a non-zero stride.253 // This happens whenever there is a slice (including a complete slice)254 // where there is a later incomplete slice or index.255 //256 bool isStrided(char [] s)257 {258 assert(s[$-1]==']', "BLADE ICE");259 if (s[$-2..$]=="[]") return false;260 int numbrack=0;261 int paren = 0;262 bool hasSliced=false;263 bool maybeSlice = false;264 bool startIndex=false;265 for(int i=1; i<s.length; ++i) {266 if (s[i]=='(') ++paren;267 else if (s[i]==')') --paren;268 if (paren==0 && s[i]==']') {269 if (startIndex && hasSliced) return true;270 numbrack--;271 if (s[i-1]=='[') { startIndex=false; }272 }273 if (paren==0 && s[i]=='[') {274 startIndex = true;275 maybeSlice = false;276 numbrack++;277 }278 if (paren==0 && numbrack==1 && s[i]==',') {279 if (hasSliced && startIndex) return true;280 if (maybeSlice) hasSliced = true;281 startIndex = true;282 }283 if (paren==0 && numbrack==2 && s[i]==',') { startIndex=false; hasSliced=true; }284 if (paren==0 && numbrack==1 && s[i]=='.' && s[i-1]=='.') {285 startIndex = false; // [..] slices don't move the index to the next value.286 maybeSlice = true;287 }288 }289 return false;290 }291 292 private:293 unittest {294 assert(!isStrided("A[3..5][]"));295 assert(!isStrided("A[2..7][3]"));296 assert(!isStrided("A[][2]"));297 assert(!isStrided("A[2,[2,7]]"));298 assert(isStrided("A[[2,7],3]"));299 assert(isStrided("C[D..$,D]"));300 assert(!isStrided("A[7][B[[1,3],2]..6]"));301 }302 303 251 304 252 RevisedExpression simplifyVectorExpression(char [] expr, char [] rank)
