Changeset 1159
- Timestamp:
- 05/08/08 18:27:05 (2 months ago)
- Files:
-
- trunk/descent.core/src/descent/internal/compiler/parser/ASTDmdNode.java (modified) (3 diffs)
- trunk/descent.core/src/descent/internal/compiler/parser/ArrayLengthExp.java (modified) (1 diff)
- trunk/descent.core/src/descent/internal/compiler/parser/ArrayLiteralExp.java (modified) (1 diff)
- trunk/descent.core/src/descent/internal/compiler/parser/BinExp.java (modified) (7 diffs)
- trunk/descent.core/src/descent/internal/compiler/parser/CallExp.java (modified) (1 diff)
- trunk/descent.core/src/descent/internal/compiler/parser/DeclarationExp.java (modified) (1 diff)
- trunk/descent.core/src/descent/internal/compiler/parser/Expression.java (modified) (2 diffs)
- trunk/descent.core/src/descent/internal/compiler/parser/ForStatement.java (modified) (1 diff)
- trunk/descent.core/src/descent/internal/compiler/parser/FuncDeclaration.java (modified) (4 diffs)
- trunk/descent.core/src/descent/internal/compiler/parser/IfStatement.java (modified) (1 diff)
- trunk/descent.core/src/descent/internal/compiler/parser/ShlExp.java (modified) (2 diffs)
- trunk/descent.core/src/descent/internal/compiler/parser/ShrExp.java (modified) (1 diff)
- trunk/descent.core/src/descent/internal/compiler/parser/UshrExp.java (modified) (1 diff)
- trunk/update.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/descent.core/src/descent/internal/compiler/parser/ASTDmdNode.java
r1158 r1159 1493 1493 if (null != v) 1494 1494 { 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) 1496 1503 { 1497 1504 e = v.init().toExpression(context); 1498 if ( null == e.type)1505 if (e != null && null == e.type) 1499 1506 e.type = v.type; 1500 1507 } … … 1586 1593 1587 1594 /* A proper implementation of the various equals() overrides 1588 * should make it possible to just do o1 ->equals(o2), but1595 * should make it possible to just do o1.equals(o2), but 1589 1596 * we'll do that another day. 1590 1597 */ … … 1830 1837 } 1831 1838 1839 public static Expression eval_builtin(BUILTIN b, Expressions args) { 1840 // XXX DMD 2 1841 return null; 1842 } 1843 1832 1844 public void errorOnModifier(int problemId, TOK tok, SemanticContext context) { 1833 1845 if (context.acceptsProblems()) { trunk/descent.core/src/descent/internal/compiler/parser/ArrayLengthExp.java
r897 r1159 53 53 || e1.op == TOKassocarrayliteral) { 54 54 e = ArrayLength.call(type, e1, context); 55 } else 55 } else if (e1.op == TOKnull) { 56 e = new IntegerExp(loc, 0, type); 57 } else { 56 58 return EXP_CANT_INTERPRET; 59 } 57 60 return e; 58 61 } trunk/descent.core/src/descent/internal/compiler/parser/ArrayLiteralExp.java
r1157 r1159 236 236 if (null == expsx) { 237 237 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 } 238 242 expsx.addAll(elements); 239 243 } trunk/descent.core/src/descent/internal/compiler/parser/BinExp.java
r1157 r1159 29 29 import static descent.internal.compiler.parser.TOK.TOKin; 30 30 import static descent.internal.compiler.parser.TOK.TOKindex; 31 import static descent.internal.compiler.parser.TOK.TOKint64; 31 32 import static descent.internal.compiler.parser.TOK.TOKle; 32 33 import static descent.internal.compiler.parser.TOK.TOKlt; … … 586 587 return EXP_CANT_INTERPRET; 587 588 } 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 589 592 && e1.op != TOKstructliteral) { 590 593 // goto Lcant; … … 597 600 return EXP_CANT_INTERPRET; 598 601 } 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 600 605 && e2.op != TOKstructliteral) { 601 606 // goto Lcant; … … 642 647 */ 643 648 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 } 647 658 } 648 659 … … 655 666 return e; 656 667 } 657 if (null != fp) 668 if (null != fp) { 658 669 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 } 660 677 e2 = Constfold.Cast(v.type, v.type, e2, context); 678 } 661 679 if (e2 != EXP_CANT_INTERPRET) { 662 680 if (!v.isParameter()) { … … 694 712 return e; 695 713 } 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) { 697 720 return EXP_CANT_INTERPRET; 698 StructLiteralExp se = (StructLiteralExp) v.value(); 721 } 722 StructLiteralExp se = (StructLiteralExp) vie; 699 723 int fieldi = se.getFieldIndex(type, soe.offset.intValue(), context); 700 724 if (fieldi == -1) … … 1124 1148 public Expression optimize(int result, SemanticContext context) { 1125 1149 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; 1144 1168 } 1145 1169 trunk/descent.core/src/descent/internal/compiler/parser/CallExp.java
r1157 r1159 99 99 if (e1.op == TOKvar) { 100 100 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 } 117 122 } 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; 122 144 } 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 } 125 153 } 126 154 } trunk/descent.core/src/descent/internal/compiler/parser/DeclarationExp.java
r1154 r1159 44 44 e = null; 45 45 } 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 49 64 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; 52 67 } 53 68 } trunk/descent.core/src/descent/internal/compiler/parser/Expression.java
r1157 r1159 5 5 6 6 import descent.core.compiler.IProblem; 7 import descent.internal.compiler.parser.Constfold.BinExp_fp; 7 8 import static descent.internal.compiler.parser.LINK.LINKd; 8 9 … … 544 545 return toInteger(context).castToUns64(); 545 546 } 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 } 546 570 547 571 @Override trunk/descent.core/src/descent/internal/compiler/parser/ForStatement.java
r1158 r1159 126 126 break; 127 127 } 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 } 132 134 } 133 135 } else if (e.isBool(false)) { trunk/descent.core/src/descent/internal/compiler/parser/FuncDeclaration.java
r1155 r1159 33 33 import static descent.internal.compiler.parser.STC.STCvariadic; 34 34 35 import static descent.internal.compiler.parser.TOK.TOKaddress; 35 36 import static descent.internal.compiler.parser.TOK.TOKvar; 36 37 … … 42 43 import static descent.internal.compiler.parser.TY.Tint32; 43 44 import static descent.internal.compiler.parser.TY.Tpointer; 45 import static descent.internal.compiler.parser.TY.Tsarray; 44 46 import static descent.internal.compiler.parser.TY.Ttuple; 45 47 import static descent.internal.compiler.parser.TY.Tvoid; … … 419 421 } 420 422 } 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 424 436 if (earg == EXP_CANT_INTERPRET) { 425 437 return null; … … 1940 1952 } 1941 1953 1954 public BUILTIN isBuiltin() { 1955 // XXX DMD 2 1956 return BUILTIN.BUILTINunknown; 1957 } 1958 1942 1959 @Override 1943 1960 public int getLineNumber() { trunk/descent.core/src/descent/internal/compiler/parser/IfStatement.java
r1118 r1159 73 73 } 74 74 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 { 76 82 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 }87 83 } 88 84 } trunk/descent.core/src/descent/internal/compiler/parser/ShlExp.java
r1154 r1159 2 2 3 3 import melnorme.miscutil.tree.TreeVisitor; 4 import descent.core.compiler.IProblem;5 4 import descent.internal.compiler.parser.ast.IASTVisitor; 6 5 import static descent.internal.compiler.parser.Constfold.Shl; … … 45 44 @Override 46 45 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); 70 47 } 71 48 trunk/descent.core/src/descent/internal/compiler/parser/ShrExp.java
r964 r1159 44 44 @Override 45 45 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); 57 47 } 58 48 trunk/descent.core/src/descent/internal/compiler/parser/UshrExp.java
r964 r1159 44 44 @Override 45 45 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); 57 47 } 58 48 trunk/update.txt
r1158 r1159 3 3 constfold.c 4 4 func.c --> missing StaticCtorDeclaration::StaticCtorDeclaration and StaticDtorDeclaration::StaticDtorDeclaration 5 interpret.c6 optimize.c
