Get load-store assign ops working again.
authorDavid Anderson <dvander@alliedmods.net>
Sun Nov 18 15:24:30 2012 -0800 (2012-11-18)
changeset 215b8f650a441a5
parent 214 e1a6deaa6ebe
child 216 ea63f1680097
Get load-store assign ops working again.
src/compiler/HIR.cpp
src/compiler/HIR.h
src/compiler/SemanticAnalysis.cpp
tests/basic/assignadd.out
tests/basic/assignadd.sp
tests/basic/bitops.sp
tests/basic/do.sp
tests/basic/floats.sp
     1.1 --- a/src/compiler/HIR.cpp	Sun Nov 18 15:00:08 2012 -0800
     1.2 +++ b/src/compiler/HIR.cpp	Sun Nov 18 15:24:30 2012 -0800
     1.3 @@ -89,6 +89,11 @@
     1.4  }
     1.5  
     1.6  void
     1.7 +HGlobal::hold(BytecodeEmitter &emitter_)
     1.8 +{
     1.9 +}
    1.10 +
    1.11 +void
    1.12  HGlobal::store(BytecodeEmitter &emitter_)
    1.13  {
    1.14      __ setglobal(sym_);
    1.15 @@ -108,6 +113,13 @@
    1.16  }
    1.17  
    1.18  void
    1.19 +HLocal::hold(BytecodeEmitter &emitter_)
    1.20 +{
    1.21 +    if (sym_->type()->isReference())
    1.22 +        __ dup();
    1.23 +}
    1.24 +
    1.25 +void
    1.26  HLocal::store(BytecodeEmitter &emitter_)
    1.27  {
    1.28      if (sym_->type()->isReference())
    1.29 @@ -133,6 +145,12 @@
    1.30  }
    1.31  
    1.32  void
    1.33 +HIndex::hold(BytecodeEmitter &emitter_)
    1.34 +{
    1.35 +    __ dup2();
    1.36 +}
    1.37 +
    1.38 +void
    1.39  HIndex::store(BytecodeEmitter &emitter_)
    1.40  {
    1.41      __ setelem(type());
    1.42 @@ -164,37 +182,39 @@
    1.43      __ getglobal(global->sym());
    1.44  }
    1.45  
    1.46 -void
    1.47 -HIRTranslator::visitStore(HStore *store)
    1.48 -{
    1.49 -    store->lval()->bind(this, emitter_);
    1.50 -    store->rval()->accept(this);
    1.51 -    store->lval()->store(emitter_);
    1.52 -}
    1.53 -
    1.54  static inline Opcode
    1.55  TokenToOpcode(TokenKind kind)
    1.56  {
    1.57      switch (kind) {
    1.58        case TOK_PLUS:
    1.59 +      case TOK_ASSIGN_ADD:
    1.60          return OP_ADD;
    1.61        case TOK_MINUS:
    1.62 +      case TOK_ASSIGN_SUB:
    1.63          return OP_SUB;
    1.64        case TOK_STAR:
    1.65 +      case TOK_ASSIGN_MUL:
    1.66          return OP_MUL;
    1.67        case TOK_SLASH:
    1.68 +      case TOK_ASSIGN_DIV:
    1.69          return OP_DIV;
    1.70        case TOK_PERCENT:
    1.71 +      case TOK_ASSIGN_MOD:
    1.72          return OP_MOD;
    1.73        case TOK_SHR:
    1.74 +      case TOK_ASSIGN_SHR:
    1.75          return OP_SHR;
    1.76        case TOK_USHR:
    1.77 +      case TOK_ASSIGN_USHR:
    1.78          return OP_USHR;
    1.79        case TOK_SHL:
    1.80 +      case TOK_ASSIGN_SHL:
    1.81          return OP_SHL;
    1.82        case TOK_BITAND:
    1.83 +      case TOK_ASSIGN_BITAND:
    1.84          return OP_BITAND;
    1.85        case TOK_BITOR:
    1.86 +      case TOK_ASSIGN_BITOR:
    1.87          return OP_BITOR;
    1.88        case TOK_GT:
    1.89            return OP_GT;
    1.90 @@ -215,6 +235,22 @@
    1.91  }
    1.92  
    1.93  void
    1.94 +HIRTranslator::visitStore(HStore *store)
    1.95 +{
    1.96 +    store->lval()->bind(this, emitter_);
    1.97 +    if (store->kind() == TOK_ASSIGN) {
    1.98 +        store->rval()->accept(this);
    1.99 +    } else {
   1.100 +        // Advanced case: dup the ref, load, op, store.
   1.101 +        store->lval()->hold(emitter_);
   1.102 +        store->lval()->load(emitter_);
   1.103 +        store->rval()->accept(this);
   1.104 +        __ binary(TokenToOpcode(store->kind()));
   1.105 +    }
   1.106 +    store->lval()->store(emitter_);
   1.107 +}
   1.108 +
   1.109 +void
   1.110  HIRTranslator::visitBinary(HBinary *binary)
   1.111  {
   1.112      binary->left()->accept(this);
     2.1 --- a/src/compiler/HIR.h	Sun Nov 18 15:00:08 2012 -0800
     2.2 +++ b/src/compiler/HIR.h	Sun Nov 18 15:24:30 2012 -0800
     2.3 @@ -150,6 +150,9 @@
     2.4  
     2.5      // Pushes a reference to the LValue on the stack.
     2.6      virtual void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_) = 0;
     2.7 +    
     2.8 +    // Duplicates a stack reference.
     2.9 +    virtual void hold(BytecodeEmitter &emitter_) = 0;
    2.10  
    2.11      // Given a stack layout of the form:
    2.12      //  [-1]    value
    2.13 @@ -177,6 +180,7 @@
    2.14  
    2.15      // Pushes a reference to the LValue on the stack.
    2.16      void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
    2.17 +    void hold(BytecodeEmitter &emitter_);
    2.18      void store(BytecodeEmitter &emitter_);
    2.19      void load(BytecodeEmitter &emitter_);
    2.20  
    2.21 @@ -197,6 +201,7 @@
    2.22      DEFINE_HIR(Index);
    2.23  
    2.24      void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
    2.25 +    void hold(BytecodeEmitter &emitter_);
    2.26      void store(BytecodeEmitter &emitter_);
    2.27      void load(BytecodeEmitter &emitter_);
    2.28  
    2.29 @@ -217,6 +222,7 @@
    2.30      DEFINE_HIR(Global);
    2.31  
    2.32      void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
    2.33 +    void hold(BytecodeEmitter &emitter_);
    2.34      void store(BytecodeEmitter &emitter_);
    2.35      void load(BytecodeEmitter &emitter_);
    2.36  
    2.37 @@ -279,6 +285,15 @@
    2.38    public:
    2.39      HStore(HLValue *lval, HIR *rval)
    2.40        : HIR(lval->type()),
    2.41 +        kind_(TOK_ASSIGN),
    2.42 +        lval_(lval),
    2.43 +        rval_(rval)
    2.44 +    {
    2.45 +    }
    2.46 +
    2.47 +    HStore(TokenKind kind, HLValue *lval, HIR *rval)
    2.48 +      : HIR(lval->type()),
    2.49 +        kind_(kind),
    2.50          lval_(lval),
    2.51          rval_(rval)
    2.52      {
    2.53 @@ -292,10 +307,14 @@
    2.54      HIR *rval() const {
    2.55          return rval_;
    2.56      }
    2.57 +    TokenKind kind() const {
    2.58 +        return kind_;
    2.59 +    }
    2.60  
    2.61    private:
    2.62      HLValue *lval_;
    2.63      HIR *rval_;
    2.64 +    TokenKind kind_;
    2.65  };
    2.66  
    2.67  class HCall : public HIR
     3.1 --- a/src/compiler/SemanticAnalysis.cpp	Sun Nov 18 15:00:08 2012 -0800
     3.2 +++ b/src/compiler/SemanticAnalysis.cpp	Sun Nov 18 15:24:30 2012 -0800
     3.3 @@ -148,7 +148,10 @@
     3.4  
     3.5      __ object(funobj);
     3.6      __ setglobal(sym);
     3.7 -    __ public_();
     3.8 +    if (stmt->token() == TOK_PUBLIC)
     3.9 +        __ public_();
    3.10 +    else
    3.11 +        __ pop();
    3.12  }
    3.13  
    3.14  Function *
    3.15 @@ -529,7 +532,7 @@
    3.16      if ((hir = coerce(hir, lval->type(), Coerce_Assign)) == NULL)
    3.17          return;
    3.18  
    3.19 -    hir_ = new (pool_) HStore(lval, hir);
    3.20 +    hir_ = new (pool_) HStore(node->token(), lval, hir);
    3.21  }
    3.22  
    3.23  void
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tests/basic/assignadd.out	Sun Nov 18 15:24:30 2012 -0800
     4.3 @@ -0,0 +1,1 @@
     4.4 +1683
     5.1 --- a/tests/basic/assignadd.sp	Sun Nov 18 15:00:08 2012 -0800
     5.2 +++ b/tests/basic/assignadd.sp	Sun Nov 18 15:24:30 2012 -0800
     5.3 @@ -1,8 +1,10 @@
     5.4 -main()
     5.5 +native PrintNum(num);
     5.6 +
     5.7 +public main()
     5.8  {
     5.9      new x[3];
    5.10      for (new i = 0; i < 100; i += 3) {
    5.11          x[2] += i;
    5.12      }
    5.13 -    return x[2];
    5.14 +    PrintNum(x[2]);
    5.15  }
     6.1 --- a/tests/basic/bitops.sp	Sun Nov 18 15:00:08 2012 -0800
     6.2 +++ b/tests/basic/bitops.sp	Sun Nov 18 15:24:30 2012 -0800
     6.3 @@ -1,6 +1,6 @@
     6.4  native PrintNum(num);
     6.5  
     6.6 -main()
     6.7 +public main()
     6.8  {
     6.9      new result = 5192342;
    6.10      for (new i = 0; i < 1000; i = i + 1) {
     7.1 --- a/tests/basic/do.sp	Sun Nov 18 15:00:08 2012 -0800
     7.2 +++ b/tests/basic/do.sp	Sun Nov 18 15:24:30 2012 -0800
     7.3 @@ -1,6 +1,6 @@
     7.4  native PrintNum(num);
     7.5  
     7.6 -main()
     7.7 +public main()
     7.8  {
     7.9     new i = 0;
    7.10     do  {
     8.1 --- a/tests/basic/floats.sp	Sun Nov 18 15:00:08 2012 -0800
     8.2 +++ b/tests/basic/floats.sp	Sun Nov 18 15:24:30 2012 -0800
     8.3 @@ -1,6 +1,6 @@
     8.4  native PrintFloat(float:num);
     8.5  
     8.6 -main()
     8.7 +public main()
     8.8  {
     8.9     new float:f = 12.0;
    8.10     for (new i = 0; i < 100; i = i + 1)