Changeset 1159

Show
Ignore:
Timestamp:
05/08/08 18:27:05 (2 months ago)
Author:
asterite
Message:

optimize.c ported to DMD 1.028

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/descent.core/src/descent/internal/compiler/parser/ASTDmdNode.java

    r1158 r1159  
    14931493        if (null != v) 
    14941494        { 
    1495             if (v.isConst() && null != v.init()) 
     1495            boolean condition; 
     1496            if (context.apiLevel == Parser.D2) { 
     1497                condition = (v.isConst() || v.isInvariant()) && v.init != null && null == v.value; 
     1498            } else { 
     1499                condition = v.isConst() && null != v.init(); 
     1500            } 
     1501             
     1502            if (condition) 
    14961503            { 
    14971504                e = v.init().toExpression(context); 
    1498                 if (null == e.type) 
     1505                if (e != null && null == e.type) 
    14991506                    e.type = v.type; 
    15001507            } 
     
    15861593 
    15871594        /* A proper implementation of the various equals() overrides 
    1588          * should make it possible to just do o1->equals(o2), but 
     1595         * should make it possible to just do o1.equals(o2), but 
    15891596         * we'll do that another day. 
    15901597         */ 
     
    18301837    } 
    18311838     
     1839    public static Expression eval_builtin(BUILTIN b, Expressions args) { 
     1840        // XXX DMD 2 
     1841        return null; 
     1842    } 
     1843     
    18321844    public void errorOnModifier(int problemId, TOK tok, SemanticContext context) { 
    18331845        if (context.acceptsProblems()) { 
  • trunk/descent.core/src/descent/internal/compiler/parser/ArrayLengthExp.java

    r897 r1159  
    5353                || e1.op == TOKassocarrayliteral) { 
    5454            e = ArrayLength.call(type, e1, context); 
    55         } else 
     55        } else if (e1.op == TOKnull) { 
     56            e = new IntegerExp(loc, 0, type); 
     57        } else { 
    5658            return EXP_CANT_INTERPRET; 
     59        } 
    5760        return e; 
    5861    } 
  • trunk/descent.core/src/descent/internal/compiler/parser/ArrayLiteralExp.java

    r1157 r1159  
    236236                    if (null == expsx) { 
    237237                        expsx = new Expressions(); 
     238                        expsx.setDim(size(elements)); 
     239                        for (int j = 0; j < size(elements); j++) { 
     240                            expsx.set(j, elements.get(j)); 
     241                        } 
    238242                        expsx.addAll(elements); 
    239243                    } 
  • trunk/descent.core/src/descent/internal/compiler/parser/BinExp.java

    r1157 r1159  
    2929import static descent.internal.compiler.parser.TOK.TOKin; 
    3030import static descent.internal.compiler.parser.TOK.TOKindex; 
     31import static descent.internal.compiler.parser.TOK.TOKint64; 
    3132import static descent.internal.compiler.parser.TOK.TOKle; 
    3233import static descent.internal.compiler.parser.TOK.TOKlt; 
     
    586587            return EXP_CANT_INTERPRET; 
    587588        } 
    588         if (!e1.isConst() && e1.op != TOKstring && e1.op != TOKarrayliteral 
     589        if (!e1.isConst() && 
     590                e1.op != TOKnull && 
     591                e1.op != TOKstring && e1.op != TOKarrayliteral 
    589592                && e1.op != TOKstructliteral) { 
    590593            // goto Lcant; 
     
    597600            return EXP_CANT_INTERPRET; 
    598601        } 
    599         if (!e2.isConst() && e2.op != TOKstring && e2.op != TOKarrayliteral 
     602        if (!e2.isConst() && 
     603                e1.op != TOKnull && 
     604                e2.op != TOKstring && e2.op != TOKarrayliteral 
    600605                && e2.op != TOKstructliteral) { 
    601606            // goto Lcant; 
     
    642647                 */ 
    643648                if (null != v.value() && v.value().op == TOKvar) { 
    644                     ve = (VarExp) v.value(); 
    645                     v = ve.var.isVarDeclaration(); 
    646                     assert (null != v); 
     649                    VarExp ve2 = (VarExp) v.value; 
     650                    if (ve2.var.isSymbolDeclaration() != null) { 
     651                        /* This can happen if v is a struct initialized to 
     652                         * 0 using an __initZ SymbolDeclaration from 
     653                         * TypeStruct::defaultInit() 
     654                         */ 
     655                    } else { 
     656                        v = ve2.var.isVarDeclaration(); 
     657                    } 
    647658                } 
    648659 
     
    655666                    return e; 
    656667                } 
    657                 if (null != fp) 
     668                if (null != fp) { 
    658669                    e2 = fp.call(v.type, ev, e2, context); 
    659                 else 
     670                } else { 
     671                    /* Look for special case of struct being initialized with 0. 
     672                     */ 
     673                    if (v.type.toBasetype(context).ty == Tstruct 
     674                            && e2.op == TOKint64) { 
     675                        e2 = v.type.defaultInit(context); 
     676                    } 
    660677                    e2 = Constfold.Cast(v.type, v.type, e2, context); 
     678                } 
    661679                if (e2 != EXP_CANT_INTERPRET) { 
    662680                    if (!v.isParameter()) { 
     
    694712                return e; 
    695713            } 
    696             if (v.value().op != TOKstructliteral) 
     714            Expression vie = v.value; 
     715            if (vie.op == TOKvar) { 
     716                Declaration d = ((VarExp) vie).var; 
     717                vie = getVarExp(e1.loc, istate, d, context); 
     718            } 
     719            if (vie.op != TOKstructliteral) { 
    697720                return EXP_CANT_INTERPRET; 
    698             StructLiteralExp se = (StructLiteralExp) v.value(); 
     721            } 
     722            StructLiteralExp se = (StructLiteralExp) vie; 
    699723            int fieldi = se.getFieldIndex(type, soe.offset.intValue(), context); 
    700724            if (fieldi == -1) 
     
    11241148    public Expression optimize(int result, SemanticContext context) { 
    11251149        e1 = e1.optimize(result, context); 
    1126         e2 = e2.optimize(result, context); 
    1127         if (op == TOKshlass || op == TOKshrass || op == TOKushrass) 
    1128        
    1129         if (e2.isConst()) 
    1130         { 
    1131             integer_t i2 = e2.toInteger(context); 
    1132             integer_t sz = new integer_t(e1.type.size(context)).multiply(8); 
    1133             if (i2.compareTo(0) < 0 || i2.compareTo(sz) > 0) 
    1134             { 
    1135                if (context.acceptsProblems())
    1136                    context.acceptProblem(Problem.newSemanticTypeError(IProblem.ShiftAssignIsOutsideTheRange, sourceE1, sourceE2, new String[] { i2.toString(), sz.toString() })); 
    1137               } 
    1138               e2 = new IntegerExp(0); 
    1139            
    1140        
    1141        
    1142  
    1143         return this; 
     1150       e2 = e2.optimize(result, context); 
     1151       if (op == TOKshlass || op == TOKshrass || op == TOKushrass) { 
     1152           if (e2.isConst())
     1153               integer_t i2 = e2.toInteger(context); 
     1154               integer_t sz = new integer_t(e1.type.size(context)).multiply(8); 
     1155               if (i2.compareTo(0) < 0 || i2.compareTo(sz) > 0) { 
     1156                   if (context.acceptsProblems()) { 
     1157                       context.acceptProblem(Problem.newSemanticTypeError( 
     1158                               IProblem.ShiftAssignIsOutsideTheRange, 
     1159                               sourceE1, sourceE2, new String[]
     1160                                       i2.toString(), sz.toString() })); 
     1161                  } 
     1162                  e2 = new IntegerExp(0); 
     1163               
     1164           
     1165       
     1166 
     1167       return this; 
    11441168    } 
    11451169 
  • trunk/descent.core/src/descent/internal/compiler/parser/CallExp.java

    r1157 r1159  
    9999        if (e1.op == TOKvar) { 
    100100            FuncDeclaration fd = ((VarExp) e1).var.isFuncDeclaration(); 
    101             if (fd != null) { // Inline .dup 
    102                 if (fd.ident != null 
    103                         && equals(fd.ident, Id.adDup) 
    104                         && arguments != null && arguments.size() == 2) { 
    105                     e = arguments.get(1); 
    106                     e = e.interpret(istate, context); 
    107                     if (e != EXP_CANT_INTERPRET) { 
    108                         e = expType(type, e, context); 
    109                     } 
    110                 } else { 
    111                     Expression eresult = fd.interpret(istate, arguments, 
    112                             context); 
    113                     if (eresult != null) { 
    114                         e = eresult; 
    115                     } else if (fd.type.toBasetype(context).nextOf().ty == Tvoid) { 
    116                         e = EXP_VOID_INTERPRET; 
     101            if (fd != null) { 
     102                boolean doInlineDup = true; 
     103                if (context.apiLevel == Parser.D2) { 
     104                    doInlineDup = false; 
     105 
     106                    BUILTIN b = fd.isBuiltin(); 
     107                    if (b != BUILTIN.BUILTINunknown) { 
     108                        Expressions args = new Expressions(); 
     109                        args.setDim(size(arguments)); 
     110                        for (int i = 0; i < size(args); i++) { 
     111                            Expression earg = (Expression) arguments.get(i); 
     112                            earg = earg.interpret(istate, context); 
     113                            if (earg == EXP_CANT_INTERPRET) { 
     114                                return earg; 
     115                            } 
     116                            args.set(i, earg); 
     117                        } 
     118                        e = eval_builtin(b, args); 
     119                        if (null == e) { 
     120                            e = EXP_CANT_INTERPRET; 
     121                        } 
    117122                    } else { 
    118                         if (istate.stackOverflow) { 
    119                             if (context.acceptsProblems()) { 
    120                                 context.acceptProblem(Problem.newSemanticTypeError(IProblem.ExpressionLeadsToStackOverflowAtCompileTime, this, new String[] { toChars(context) })); 
    121                             } 
     123                        doInlineDup = true; 
     124                    } 
     125                } 
     126                 
     127                // Inline .dup 
     128                if (doInlineDup) { 
     129                    if (fd.ident != null 
     130                            && equals(fd.ident, Id.adDup) 
     131                            && arguments != null && arguments.size() == 2) { 
     132                        e = arguments.get(1); 
     133                        e = e.interpret(istate, context); 
     134                        if (e != EXP_CANT_INTERPRET) { 
     135                            e = expType(type, e, context); 
     136                        } 
     137                    } else { 
     138                        Expression eresult = fd.interpret(istate, arguments, 
     139                                context); 
     140                        if (eresult != null) { 
     141                            e = eresult; 
     142                        } else if (fd.type.toBasetype(context).nextOf().ty == Tvoid && 0 == context.global.errors) { 
     143                            e = EXP_VOID_INTERPRET; 
    122144                        } else { 
    123                             if (context.acceptsProblems()) { 
    124                                 context.acceptProblem(Problem.newSemanticTypeError(IProblem.ExpressionIsNotEvaluatableAtCompileTime, this, new String[] { toChars(context) })); 
     145                            if (istate.stackOverflow) { 
     146                                if (context.acceptsProblems()) { 
     147                                    context.acceptProblem(Problem.newSemanticTypeError(IProblem.ExpressionLeadsToStackOverflowAtCompileTime, this, new String[] { toChars(context) })); 
     148                                } 
     149                            } else { 
     150                                if (context.acceptsProblems()) { 
     151                                    context.acceptProblem(Problem.newSemanticTypeError(IProblem.ExpressionIsNotEvaluatableAtCompileTime, this, new String[] { toChars(context) })); 
     152                                } 
    125153                            } 
    126154                        } 
  • trunk/descent.core/src/descent/internal/compiler/parser/DeclarationExp.java

    r1154 r1159  
    4444                    e = null; 
    4545                } 
    46             } else if (s == v && v.isConst() && v.init() != null) { 
    47                 e = v.init().toExpression(context); 
    48                 if (null == e) { 
     46            } else {  
     47                boolean condition; 
     48                if (context.apiLevel == Parser.D2) { 
     49                    condition = s == v && (v.isConst() || v.isInvariant()) && v.init != null; 
     50                } else { 
     51                    condition = s == v && v.isConst() && v.init() != null; 
     52                } 
     53                 
     54                if (condition) { 
     55                    e = v.init().toExpression(context); 
     56                    if (null == e) { 
     57                        e = EXP_CANT_INTERPRET; 
     58                    } else if (null == e.type) { 
     59                        e.type = v.type; 
     60                    } 
     61                } else if (declaration.isAttribDeclaration() != null 
     62                        || declaration.isTemplateMixin() != null 
     63                        || declaration.isTupleDeclaration() != null) { // These can be made to work, too lazy now 
    4964                    e = EXP_CANT_INTERPRET; 
    50                 } else if (null == e.type) { 
    51                     e.type = v.type
     65                } else { // Others should not contain executable code, so are trivial to evaluate 
     66                    e = null
    5267                } 
    5368            } 
  • trunk/descent.core/src/descent/internal/compiler/parser/Expression.java

    r1157 r1159  
    55 
    66import descent.core.compiler.IProblem; 
     7import descent.internal.compiler.parser.Constfold.BinExp_fp; 
    78import static descent.internal.compiler.parser.LINK.LINKd; 
    89 
     
    544545        return toInteger(context).castToUns64(); 
    545546    } 
     547     
     548    public static Expression shift_optimize(int result, BinExp e, BinExp_fp fp, 
     549            SemanticContext context) { 
     550        Expression ex = e; 
     551 
     552        e.e1 = e.e1.optimize(result, context); 
     553        e.e2 = e.e2.optimize(result, context); 
     554        if (e.e2.isConst()) { 
     555            integer_t i2 = e.e2.toInteger(context); 
     556            integer_t sz = new integer_t(e.e1.type.size(context)).multiply(8); 
     557            if (i2.compareTo(0) < 0 || i2.compareTo(sz) > 0) { 
     558                if (context.acceptsProblems()) { 
     559                    context.acceptProblem(Problem.newSemanticTypeError( 
     560                            IProblem.ShiftAssignIsOutsideTheRange, e, 
     561                            new String[] { i2.toString(), sz.toString() })); 
     562                } 
     563                e.e2 = new IntegerExp(0); 
     564            } 
     565            if (e.e1.isConst()) 
     566                ex = fp.call(e.type, e.e1, e.e2, context); 
     567        } 
     568        return ex; 
     569    } 
    546570 
    547571    @Override 
  • trunk/descent.core/src/descent/internal/compiler/parser/ForStatement.java

    r1158 r1159  
    126126                    break; 
    127127                } 
    128                 // Lcontinue:  
    129                 e = increment.interpret(istate, context); 
    130                 if (e == EXP_CANT_INTERPRET) { 
    131                     break; 
     128                // Lcontinue: 
     129                if (increment != null) { 
     130                    e = increment.interpret(istate, context); 
     131                    if (e == EXP_CANT_INTERPRET) { 
     132                        break; 
     133                    } 
    132134                } 
    133135            } else if (e.isBool(false)) { 
  • trunk/descent.core/src/descent/internal/compiler/parser/FuncDeclaration.java

    r1155 r1159  
    3333import static descent.internal.compiler.parser.STC.STCvariadic; 
    3434 
     35import static descent.internal.compiler.parser.TOK.TOKaddress; 
    3536import static descent.internal.compiler.parser.TOK.TOKvar; 
    3637 
     
    4243import static descent.internal.compiler.parser.TY.Tint32; 
    4344import static descent.internal.compiler.parser.TY.Tpointer; 
     45import static descent.internal.compiler.parser.TY.Tsarray; 
    4446import static descent.internal.compiler.parser.TY.Ttuple; 
    4547import static descent.internal.compiler.parser.TY.Tvoid; 
     
    419421                        } 
    420422                    } 
    421                 } else { /* Value parameters 
    422                  */ 
    423                     earg = earg.interpret(istatex, context); 
     423                } else {  
     424                    /*  
     425                     * Value parameters 
     426                     */ 
     427                    Type ta = arg.type.toBasetype(context); 
     428                    if (ta.ty == Tsarray && earg.op == TOKaddress) { 
     429                        /* Static arrays are passed by a simple pointer. 
     430                         * Skip past this to get at the actual arg. 
     431                         */ 
     432                        earg = ((AddrExp) earg).e1; 
     433                    } 
     434                    earg = earg.interpret(istate != null ? istate : istatex, context); 
     435 
    424436                    if (earg == EXP_CANT_INTERPRET) { 
    425437                        return null; 
     
    19401952    } 
    19411953     
     1954    public BUILTIN isBuiltin() { 
     1955        // XXX DMD 2 
     1956        return BUILTIN.BUILTINunknown; 
     1957    } 
     1958     
    19421959    @Override 
    19431960    public int getLineNumber() { 
  • trunk/descent.core/src/descent/internal/compiler/parser/IfStatement.java

    r1118 r1159  
    7373        } 
    7474        if (e != EXP_CANT_INTERPRET) { 
    75             if (!e.isConst()) { 
     75            if (e.isBool(true)) { 
     76                e = ifbody != null ? ifbody.interpret(istate, context) 
     77                        : null; 
     78            } else if (e.isBool(false)) { 
     79                e = elsebody != null ? elsebody.interpret(istate, context) 
     80                        : null; 
     81            } else { 
    7682                e = EXP_CANT_INTERPRET; 
    77             } else { 
    78                 if (e.isBool(true)) { 
    79                     e = ifbody != null ? ifbody.interpret(istate, context) 
    80                             : null; 
    81                 } else if (e.isBool(false)) { 
    82                     e = elsebody != null ? elsebody.interpret(istate, context) 
    83                             : null; 
    84                 } else { 
    85                     e = EXP_CANT_INTERPRET; 
    86                 } 
    8783            } 
    8884        } 
  • trunk/descent.core/src/descent/internal/compiler/parser/ShlExp.java

    r1154 r1159  
    22 
    33import melnorme.miscutil.tree.TreeVisitor; 
    4 import descent.core.compiler.IProblem; 
    54import descent.internal.compiler.parser.ast.IASTVisitor; 
    65import static descent.internal.compiler.parser.Constfold.Shl; 
     
    4544    @Override 
    4645    public Expression optimize(int result, SemanticContext context) { 
    47         Expression e; 
    48  
    49         e1 = e1.optimize(result, context); 
    50         e2 = e2.optimize(result, context); 
    51         e = this; 
    52         if (e2.isConst()) { 
    53             integer_t i2 = e2.toInteger(context); 
    54             if (i2.compareTo(0) < 0 
    55                     || i2.compareTo(e1.type.size(context) * 8) > 0) { 
    56                 if (context.acceptsProblems()) { 
    57                     context.acceptProblem(Problem.newSemanticTypeError( 
    58                             IProblem.ShiftLeftExceeds, this, new String[] { i2.toString(), String.valueOf(e2.type.size(context) * 8) })); 
    59                 } 
    60                 e2 = new IntegerExp(0); 
    61             } 
    62             if (e1.isConst()) { 
    63                 e = new IntegerExp(loc, e1.toInteger(context).shiftLeft( 
    64                         e2.toInteger(context)), type); 
    65                 e.start = e1.start; 
    66                 e.length = e2.start + e2.length - e1.start; 
    67             } 
    68         } 
    69         return e; 
     46        return shift_optimize(result, this, Shl, context); 
    7047    } 
    7148 
  • trunk/descent.core/src/descent/internal/compiler/parser/ShrExp.java

    r964 r1159  
    4444    @Override 
    4545    public Expression optimize(int result, SemanticContext context) { 
    46         Expression e; 
    47  
    48         e1 = e1.optimize(result, context); 
    49         e2 = e2.optimize(result, context); 
    50  
    51         if (e1.isConst() && e2.isConst()) { 
    52             e = Shr.call(type, e1, e2, context); 
    53         } else { 
    54             e = this; 
    55         } 
    56         return e; 
     46        return shift_optimize(result, this, Shr, context); 
    5747    } 
    5848 
  • trunk/descent.core/src/descent/internal/compiler/parser/UshrExp.java

    r964 r1159  
    4444    @Override 
    4545    public Expression optimize(int result, SemanticContext context) { 
    46         Expression e; 
    47  
    48         e1 = e1.optimize(result, context); 
    49         e2 = e2.optimize(result, context); 
    50  
    51         if (e1.isConst() && e2.isConst()) { 
    52             e = Ushr.call(type, e1, e2, context); 
    53         } else { 
    54             e = this; 
    55         } 
    56         return e; 
     46        return shift_optimize(result, this, Ushr, context); 
    5747    } 
    5848 
  • trunk/update.txt

    r1158 r1159  
    33constfold.c 
    44func.c --> missing StaticCtorDeclaration::StaticCtorDeclaration and StaticDtorDeclaration::StaticDtorDeclaration 
    5 interpret.c 
    6 optimize.c