Changeset 131

Show
Ignore:
Timestamp:
11/10/07 01:33:33 (10 months ago)
Author:
Don Clugston
Message:

Scalar folding is now working for basic situations. Still not linked into the main code generator.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/blade/Blade.d

    r129 r131  
    108108        if (r==0) { 
    109109            ++numscalars; 
    110             continue; // assume we can always do scalars 
     110            continue; // BUG: assume we can always do scalars 
    111111        } 
    112112        if (startsWith(t, "double[")) { 
     
    275275} 
    276276 
    277 /+ 
     277 
    278278// Simplify a vector expression 
    279279//  - Use slicing distributive law: A[B..C] for expressions A,B,C 
     
    285285// Convert -A*B into +(-A)*B whenever possible. 
    286286// Combine all scalars into a single scalar. 
    287 void simplify(inout AbstractSyntaxTree tree) 
    288 { 
    289     // Part 1: simplifications which only change the expression string. 
    290     for (int i=0; i<tree.expression.length; ++i) { 
    291          int x = i+exprLength(expression[i..$]); 
    292          if (x==tree.expression.length) continue; 
    293          if (tree.expression[x+1]=='[') { // potentially a slice 
    294             int y = x + 1 + exprLength(tree.expression[x+1..$-1]) 
    295             if (y+2<tree.expression.length && tree.expression[y..y+1]=="..") { 
    296                 // it's a slice 
    297             } 
    298               
    299          }         
    300     } 
    301     // Part 2: simplifications which change the types 
    302 } 
    303 +/ 
    304287 
    305288// Categorise the expression, and dispatch to the appropriate code generator. 
    306289char [] makeVectorCode(AbstractSyntaxTree tree) 
    307290{ 
     291    /* 
     292    int [] ranks=[]; 
     293    for (int i=0; i<tree.symbolTable.length; ++i) {ranks~=tree.symbolTable[i].rank; } 
     294    RevisedExpression e = simplifyVectorExpression(tree.expression, ranks); 
     295    */ 
    308296    VecExpressionType exprType = categorizeExpression(tree); 
    309297    if (exprType == VecExpressionType.SSE2Expression || exprType == VecExpressionType.SSE1Expression) { 
  • trunk/blade/BladeRank.d

    r130 r131  
    3636 
    3737unittest { 
    38 static assert( elementTupleToString!(real [], double) == ["real", "double"]); 
    39 static assert( elementTupleToString!() == cast(char[][])([])); 
     38  static assert( elementTupleToString!(real [], double) == ["real", "double"]); 
     39  static assert( elementTupleToString!() == cast(char[][])([])); 
    4040} 
    4141 
     
    5858unittest { 
    5959    class Test { ulong opIndex(int k){ return 0; } } 
    60    double padding_for_test; 
     60    double padding_for_test; 
    6161     
    6262    assert(TupleRank!(int[][], float, double[], double[45])==[2,0,1,1]); 
     
    110110} 
    111111 
    112  
    113  
    114 /// Return the length of a sub-expression 
     112/** Return the length of a sub-expression 
     113 * The sub-expression must be  
     114 *  - a single character (eg "X"), OR 
     115 *  - an expression in parenthesis. 
     116 */ 
    115117int exprLength(char [] s) 
    116118{ 
    117     if ((s[0]>='A' && s[0]<='Z') || s[0]=='$') 
    118         return 0; 
    119     int i = 0; 
    120     if (s[0]>='a' && s[0]<='z') i=1; // next char is a parenthesis - so the code 
    121 // below works 
    122  
     119    if ((s[0]>='A' && s[0]<='Z') || s[0]=='$') return 0; 
     120    int i = 0;    
     121    if (s[0]>='a' && s[0]<='z'){ // function call 
     122        i=1; // next char is a parenthesis - so the code 
     123            // below works 
     124    } 
    123125    int numParens = 0; 
    124126    for (; i<s.length; ++i) { 
     
    279281 
    280282unittest { 
    281  assert(hasScalarMultiply("(A*B)+(B*C)",[1,0,1])); 
    282  assert(!hasScalarMultiply("(A*B)-(C*C)",[1,0,1])); 
    283  assert(!hasScalarMultiply("A+(B*C)",[1,0,1])); 
    284  assert(hasScalarMultiply("(A/B)-((A*B)+(C*B))",[1,0,1])); 
    285  assert(!hasScalarMultiply("A[B]",[2,0])); 
    286  assert(!hasScalarMultiply("(C[B])[B..A]",[0,0,2]) ); 
     283    assert(hasScalarMultiply("(A*B)+(B*C)",[1,0,1])); 
     284    assert(!hasScalarMultiply("(A*B)-(C*C)",[1,0,1])); 
     285    assert(!hasScalarMultiply("A+(B*C)",[1,0,1])); 
     286    assert(hasScalarMultiply("(A/B)-((A*B)+(C*B))",[1,0,1])); 
     287    assert(!hasScalarMultiply("A[B]",[2,0])); 
     288    assert(!hasScalarMultiply("(C[B])[B..A]",[0,0,2]) ); 
    287289} 
    288290 
     
    382384    if (op=="[") { 
    383385        // accumulate indexing and slicing operations 
    384         return subexprSimplify(left, rank, mulexpr, "[" ~ expr ~ "]" ~ indexexpr); 
     386        return subexprSimplify(left, rank, mulexpr, "[" ~ right ~ "]" ~ indexexpr); 
    385387    } 
    386388    int rrank = subexprRank(right, rank); 
     
    425427    char [][] compounds; // the compound variables 
    426428    int [] rank; // rank of all of the compound variables 
    427 
     429    char [] used; // which of the original variables are used. 
     430
     431 
     432int indexRank(char [] s) 
     433
     434   int r=0; 
     435   int numbrack=0; 
     436   for(int i=1; i<s.length; ++i) { 
     437        if (s[i]==']') numbrack--; 
     438        if (s[i]=='[') { 
     439            if (numbrack==0) ++r; 
     440            numbrack++; 
     441        } 
     442        if (numbrack==0 && s[i]=='.' && s[i-1]=='.') { 
     443            // if it's a slice, it does not increase the rank 
     444             r--; 
     445        } 
     446   } 
     447   return r; 
     448
     449 
    428450 
    429451RevisedExpression simplifyVectorExpression(char [] expr, int [] rank) 
     
    433455    char [][] comp; 
    434456    char [] used=""; 
     457    for (int i=0; i<rank.length; ++i) used~="-"; 
    435458    int [] r; 
    436459    char next = cast(char)('A' + rank.length); 
     
    441464            for (k=i+1; s[k]!=' '; ++k) {} 
    442465            comp ~= s[i+2..k-1]; 
    443             // Can't just use exprRank, because the [] aren't wrapped by (). 
    444             if (s[k-2]==']') { 
    445                 // it's a vector/matrix of some kind, with indexes. 
    446                 r~=100; // BUG - not correct 
     466            if (s[k-2]==']') {                 
     467                // it's a vector/matrix of some kind, with rank reduced 
     468                // by indices. Can't just use exprRank, because the [] 
     469                // aren't wrapped by (). 
     470                r ~= rank[s[i+2]-'A'] - indexRank(s[i+2..k-1]); 
    447471            } else { 
    448472                // it's a scalar expression. Note that it could involve 
     
    453477            ++next; 
    454478            i = k; 
    455         } else e ~= s[i]; 
    456     } 
    457     return RevisedExpression(e, comp, r); 
    458 
    459  
    460 unittest { 
    461      assert(exprSimplify("A+=(((D[E])*B)[E])", [1,0,3,3,0,0],"","")=="(A+=(B* {D[D[E]][((D[E])*B)[E]]} ))"); 
    462      assert(exprSimplify("A+=(B*((C[B])[B..E]))", [1,0,3,3,0,0],"","")=="(A+=(B* {C[C[B]][(C[B])[B..E]]} ))"); 
    463      assert(exprSimplify("A*=(B*C)", [1,0,0],"","")== "(A*= {(B*C)} )"); 
     479        } else { 
     480            e ~= s[i]; 
     481            if (s[i]>='A' && s[i]<='Z') used[s[i]-'A']=s[i]; 
     482        } 
     483    } 
     484    return RevisedExpression(e, comp, r, used); 
     485
     486 
     487unittest { 
     488    assert(exprSimplify("A+=(((D[E])*B)[E])", [1,0,3,3,0,0],"","")=="(A+=(B* {D[E][E]} ))"); 
     489    assert(exprSimplify("A+=(B*((C[B])[B..E]))", [1,0,3,3,0,0],"","")=="(A+=(B* {C[B][B..E]} ))"); 
     490    assert(exprSimplify("A*=(B*C)", [1,0,0],"","")== "(A*= {(B*C)} )"); 
    464491    
    465     RevisedExpression e = simplifyVectorExpression("A+=(((D[E])*B)[E])", [1,0,3,3,0, 0]); 
    466     assert(e.expr == "A+=(B*G)"); 
    467      
    468 //    char [] q = e.expr ~\n ~ e.compounds[0] ~\n;    
    469 //    assert(0, q); 
    470 } 
     492    RevisedExpression e = simplifyVectorExpression("A+=(((D[B])*C)[B])", [2,0,0,4]); 
     493    assert(e.expr == "A+=(C*E)"); 
     494    assert(e.rank[0]==2); 
     495    assert(e.compounds[0]=="D[B][B]"); 
     496    assert(e.used=="A-C-");  
     497}