Re-add postinc support.
authorDavid Anderson <dvander@alliedmods.net>
Sun Nov 18 16:10:43 2012 -0800 (2012-11-18)
changeset 218ccb089827f87
parent 217 c22e983b3b6e
child 219 99e534728b76
Re-add postinc support.
src/compiler/HIR.cpp
src/compiler/HIR.h
src/compiler/SemanticAnalysis.cpp
tests/basic/postinc-array.out
tests/basic/postinc-array.sp
tests/basic/postinc-local.out
tests/basic/postinc-local.sp
tests/prototype/postinc-array.sp
tests/prototype/postinc-local.sp
     1.1 --- a/src/compiler/HIR.cpp	Sun Nov 18 15:50:41 2012 -0800
     1.2 +++ b/src/compiler/HIR.cpp	Sun Nov 18 16:10:43 2012 -0800
     1.3 @@ -45,6 +45,7 @@
     1.4      void visitGlobal(HGlobal *global);
     1.5      void visitConvert(HConvert *convert);
     1.6      void visitIndex(HIndex *index);
     1.7 +    void visitPostIncDec(HPostIncDec *hir);
     1.8  
     1.9    private:
    1.10      Zone *zone_;
    1.11 @@ -94,6 +95,12 @@
    1.12  }
    1.13  
    1.14  void
    1.15 +HGlobal::swapAndPick(BytecodeEmitter &emitter_)
    1.16 +{
    1.17 +    __ dup();
    1.18 +}
    1.19 +
    1.20 +void
    1.21  HGlobal::store(BytecodeEmitter &emitter_)
    1.22  {
    1.23      __ setglobal(sym_);
    1.24 @@ -120,6 +127,17 @@
    1.25  }
    1.26  
    1.27  void
    1.28 +HLocal::swapAndPick(BytecodeEmitter &emitter_)
    1.29 +{
    1.30 +    if (sym_->type()->isReference()) {
    1.31 +        __ swap();
    1.32 +        __ pick2();
    1.33 +    } else {
    1.34 +        __ dup();
    1.35 +    }
    1.36 +}
    1.37 +
    1.38 +void
    1.39  HLocal::store(BytecodeEmitter &emitter_)
    1.40  {
    1.41      if (sym_->type()->isReference())
    1.42 @@ -151,6 +169,15 @@
    1.43  }
    1.44  
    1.45  void
    1.46 +HIndex::swapAndPick(BytecodeEmitter &emitter_)
    1.47 +{
    1.48 +    // BASE INDEX VALUE
    1.49 +    __ roll3();         // INDEX VALUE BASE
    1.50 +    __ roll3();         // VALUE BASE INDEX
    1.51 +    __ pick3();         // VALUE BASE INDEX VALUE
    1.52 +}
    1.53 +
    1.54 +void
    1.55  HIndex::store(BytecodeEmitter &emitter_)
    1.56  {
    1.57      __ setelem(type());
    1.58 @@ -253,6 +280,19 @@
    1.59  }
    1.60  
    1.61  void
    1.62 +HIRTranslator::visitPostIncDec(HPostIncDec *hir)
    1.63 +{
    1.64 +    hir->lval()->bind(this, emitter_);
    1.65 +    hir->lval()->hold(emitter_);
    1.66 +    hir->lval()->load(emitter_);
    1.67 +    hir->lval()->swapAndPick(emitter_);
    1.68 +    hir->rval()->accept(this);
    1.69 +    __ binary(TokenToOpcode(hir->kind()));
    1.70 +    hir->lval()->store(emitter_);
    1.71 +    __ pop();
    1.72 +}
    1.73 +
    1.74 +void
    1.75  HIRTranslator::visitBinary(HBinary *binary)
    1.76  {
    1.77      binary->left()->accept(this);
     2.1 --- a/src/compiler/HIR.h	Sun Nov 18 15:50:41 2012 -0800
     2.2 +++ b/src/compiler/HIR.h	Sun Nov 18 16:10:43 2012 -0800
     2.3 @@ -36,7 +36,8 @@
     2.4      _(Global)                           \
     2.5      /* End LValues */                   \
     2.6      _(Binary)                           \
     2.7 -    _(Convert)
     2.8 +    _(Convert)                          \
     2.9 +    _(PostIncDec)
    2.10  
    2.11  // Forward declarations.
    2.12  #define _(name)    class H##name;
    2.13 @@ -154,6 +155,10 @@
    2.14      // Duplicates a stack reference.
    2.15      virtual void hold(BytecodeEmitter &emitter_) = 0;
    2.16  
    2.17 +    // Reorders the stack for a post-increment operation:
    2.18 +    //   REF VAL -> VAL REF VAL
    2.19 +    virtual void swapAndPick(BytecodeEmitter &emitter_) = 0;
    2.20 +
    2.21      // Given a stack layout of the form:
    2.22      //  [-1]    value
    2.23      //  [-2..n] ref
    2.24 @@ -181,6 +186,7 @@
    2.25      // Pushes a reference to the LValue on the stack.
    2.26      void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
    2.27      void hold(BytecodeEmitter &emitter_);
    2.28 +    void swapAndPick(BytecodeEmitter &emitter_);
    2.29      void store(BytecodeEmitter &emitter_);
    2.30      void load(BytecodeEmitter &emitter_);
    2.31  
    2.32 @@ -202,6 +208,7 @@
    2.33  
    2.34      void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
    2.35      void hold(BytecodeEmitter &emitter_);
    2.36 +    void swapAndPick(BytecodeEmitter &emitter_);
    2.37      void store(BytecodeEmitter &emitter_);
    2.38      void load(BytecodeEmitter &emitter_);
    2.39  
    2.40 @@ -223,6 +230,7 @@
    2.41  
    2.42      void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
    2.43      void hold(BytecodeEmitter &emitter_);
    2.44 +    void swapAndPick(BytecodeEmitter &emitter_);
    2.45      void store(BytecodeEmitter &emitter_);
    2.46      void load(BytecodeEmitter &emitter_);
    2.47  
    2.48 @@ -317,6 +325,35 @@
    2.49      HIR *rval_;
    2.50  };
    2.51  
    2.52 +class HPostIncDec : public HIR
    2.53 +{
    2.54 +  public:
    2.55 +    HPostIncDec(TokenKind kind, HLValue *lval, HIR *rval)
    2.56 +      : HIR(lval->type()),
    2.57 +        kind_(kind),
    2.58 +        lval_(lval),
    2.59 +        rval_(rval)
    2.60 +    {
    2.61 +    }
    2.62 +
    2.63 +    DEFINE_HIR(PostIncDec);
    2.64 +
    2.65 +    HLValue *lval() const {
    2.66 +        return lval_;
    2.67 +    }
    2.68 +    HIR *rval() const {
    2.69 +        return rval_;
    2.70 +    }
    2.71 +    TokenKind kind() const {
    2.72 +        return kind_;
    2.73 +    }
    2.74 +
    2.75 +  private:
    2.76 +    TokenKind kind_;
    2.77 +    HLValue *lval_;
    2.78 +    HIR *rval_;
    2.79 +};
    2.80 +
    2.81  class HCall : public HIR
    2.82  {
    2.83    public:
     3.1 --- a/src/compiler/SemanticAnalysis.cpp	Sun Nov 18 15:50:41 2012 -0800
     3.2 +++ b/src/compiler/SemanticAnalysis.cpp	Sun Nov 18 16:10:43 2012 -0800
     3.3 @@ -604,11 +604,10 @@
     3.4      else
     3.5          rval = new (pool_) HFloat(lval->type(), 1.0);
     3.6  
     3.7 -    if (node->postfix()) {
     3.8 -        assert(false);
     3.9 -    } else {
    3.10 +    if (node->postfix())
    3.11 +        hir_ = new (pool_) HPostIncDec(node->token(), lval, rval);
    3.12 +    else
    3.13          hir_ = new (pool_) HStore(node->token(), lval, rval);
    3.14 -    }
    3.15  }
    3.16  
    3.17  Type *
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tests/basic/postinc-array.out	Sun Nov 18 16:10:43 2012 -0800
     4.3 @@ -0,0 +1,1 @@
     4.4 +01
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tests/basic/postinc-array.sp	Sun Nov 18 16:10:43 2012 -0800
     5.3 @@ -0,0 +1,8 @@
     5.4 +native PrintNum(num);
     5.5 +
     5.6 +public main()
     5.7 +{
     5.8 +    new x[5];
     5.9 +    PrintNum(x[3]++);
    5.10 +    PrintNum(x[3]);
    5.11 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/tests/basic/postinc-local.out	Sun Nov 18 16:10:43 2012 -0800
     6.3 @@ -0,0 +1,1 @@
     6.4 +01
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/tests/basic/postinc-local.sp	Sun Nov 18 16:10:43 2012 -0800
     7.3 @@ -0,0 +1,8 @@
     7.4 +native PrintNum(num);
     7.5 +
     7.6 +public main()
     7.7 +{
     7.8 +    new i = 0;
     7.9 +    PrintNum(i++);
    7.10 +    PrintNum(i);
    7.11 +}
     8.1 --- a/tests/prototype/postinc-array.sp	Sun Nov 18 15:50:41 2012 -0800
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,5 +0,0 @@
     8.4 -main()
     8.5 -{
     8.6 -    new x[5];
     8.7 -    return x[3]++;
     8.8 -}
     9.1 --- a/tests/prototype/postinc-local.sp	Sun Nov 18 15:50:41 2012 -0800
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,5 +0,0 @@
     9.4 -main()
     9.5 -{
     9.6 -    new i = 0;
     9.7 -    return i++;
     9.8 -}