src/share/vm/c1/c1_ValueStack.cpp

changeset 2174
f02a8bbe6ed4
parent 1939
b812ff5abc73
child 2314
f95d63e2154a
     1.1 --- a/src/share/vm/c1/c1_ValueStack.cpp	Wed Sep 22 23:51:03 2010 -0700
     1.2 +++ b/src/share/vm/c1/c1_ValueStack.cpp	Tue Dec 29 19:08:54 2009 +0100
     1.3 @@ -28,55 +28,60 @@
     1.4  
     1.5  // Implementation of ValueStack
     1.6  
     1.7 -ValueStack::ValueStack(IRScope* scope, int locals_size, int max_stack_size)
     1.8 +ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)
     1.9  : _scope(scope)
    1.10 -, _locals(locals_size, NULL)
    1.11 -, _stack(max_stack_size)
    1.12 -, _lock_stack(false)
    1.13 -, _locks(1)
    1.14 +, _caller_state(caller_state)
    1.15 +, _bci(-99)
    1.16 +, _kind(Parsing)
    1.17 +, _locals(scope->method()->max_locals(), NULL)
    1.18 +, _stack(scope->method()->max_stack())
    1.19 +, _locks()
    1.20  {
    1.21 -  assert(scope != NULL, "scope must exist");
    1.22 +  verify();
    1.23  }
    1.24  
    1.25 -ValueStack* ValueStack::copy() {
    1.26 -  ValueStack* s = new ValueStack(scope(), locals_size(), max_stack_size());
    1.27 -  s->_stack.appendAll(&_stack);
    1.28 -  s->_locks.appendAll(&_locks);
    1.29 -  s->replace_locals(this);
    1.30 -  return s;
    1.31 +
    1.32 +ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci)
    1.33 +  : _scope(copy_from->scope())
    1.34 +  , _caller_state(copy_from->caller_state())
    1.35 +  , _bci(bci)
    1.36 +  , _kind(kind)
    1.37 +  , _locals()
    1.38 +  , _stack()
    1.39 +  , _locks(copy_from->locks_size())
    1.40 +{
    1.41 +  assert(kind != EmptyExceptionState || !Compilation::current()->env()->jvmti_can_access_local_variables(), "need locals");
    1.42 +  if (kind != EmptyExceptionState) {
    1.43 +    // only allocate space if we need to copy the locals-array
    1.44 +    _locals = Values(copy_from->locals_size());
    1.45 +    _locals.appendAll(&copy_from->_locals);
    1.46 +  }
    1.47 +
    1.48 +  if (kind != ExceptionState && kind != EmptyExceptionState) {
    1.49 +    if (kind == Parsing) {
    1.50 +      // stack will be modified, so reserve enough space to avoid resizing
    1.51 +      _stack = Values(scope()->method()->max_stack());
    1.52 +    } else {
    1.53 +      // stack will not be modified, so do not waste space
    1.54 +      _stack = Values(copy_from->stack_size());
    1.55 +    }
    1.56 +    _stack.appendAll(&copy_from->_stack);
    1.57 +  }
    1.58 +
    1.59 +  _locks.appendAll(&copy_from->_locks);
    1.60 +
    1.61 +  verify();
    1.62  }
    1.63  
    1.64  
    1.65 -ValueStack* ValueStack::copy_locks() {
    1.66 -  int sz = scope()->lock_stack_size();
    1.67 -  if (stack_size() == 0) {
    1.68 -    sz = 0;
    1.69 -  }
    1.70 -  ValueStack* s = new ValueStack(scope(), locals_size(), sz);
    1.71 -  s->_lock_stack = true;
    1.72 -  s->_locks.appendAll(&_locks);
    1.73 -  s->replace_locals(this);
    1.74 -  if (sz > 0) {
    1.75 -    assert(sz <= stack_size(), "lock stack underflow");
    1.76 -    for (int i = 0; i < sz; i++) {
    1.77 -      s->_stack.append(_stack[i]);
    1.78 -    }
    1.79 -  }
    1.80 -  return s;
    1.81 -}
    1.82 +bool ValueStack::is_same(ValueStack* s) {
    1.83 +  if (scope() != s->scope()) return false;
    1.84 +  if (caller_state() != s->caller_state()) return false;
    1.85  
    1.86 -bool ValueStack::is_same(ValueStack* s) {
    1.87 -  assert(s != NULL, "state must exist");
    1.88 -  assert(scope      () == s->scope      (), "scopes       must correspond");
    1.89 -  assert(locals_size() == s->locals_size(), "locals sizes must correspond");
    1.90 -  return is_same_across_scopes(s);
    1.91 -}
    1.92 +  if (locals_size() != s->locals_size()) return false;
    1.93 +  if (stack_size() != s->stack_size()) return false;
    1.94 +  if (locks_size() != s->locks_size()) return false;
    1.95  
    1.96 -
    1.97 -bool ValueStack::is_same_across_scopes(ValueStack* s) {
    1.98 -  assert(s != NULL, "state must exist");
    1.99 -  assert(stack_size () == s->stack_size (), "stack  sizes must correspond");
   1.100 -  assert(locks_size () == s->locks_size (), "locks  sizes must correspond");
   1.101    // compare each stack element with the corresponding stack element of s
   1.102    int index;
   1.103    Value value;
   1.104 @@ -89,12 +94,6 @@
   1.105    return true;
   1.106  }
   1.107  
   1.108 -
   1.109 -ValueStack* ValueStack::caller_state() const {
   1.110 -  return scope()->caller_state();
   1.111 -}
   1.112 -
   1.113 -
   1.114  void ValueStack::clear_locals() {
   1.115    for (int i = _locals.length() - 1; i >= 0; i--) {
   1.116      _locals.at_put(i, NULL);
   1.117 @@ -102,13 +101,6 @@
   1.118  }
   1.119  
   1.120  
   1.121 -void ValueStack::replace_locals(ValueStack* with) {
   1.122 -  assert(locals_size() == with->locals_size(), "number of locals must match");
   1.123 -  for (int i = locals_size() - 1; i >= 0; i--) {
   1.124 -    _locals.at_put(i, with->_locals.at(i));
   1.125 -  }
   1.126 -}
   1.127 -
   1.128  void ValueStack::pin_stack_for_linear_scan() {
   1.129    for_each_state_value(this, v,
   1.130      if (v->as_Constant() == NULL && v->as_Local() == NULL) {
   1.131 @@ -123,33 +115,25 @@
   1.132    for (int i = 0; i < list.length(); i++) {
   1.133      Value* va = list.adr_at(i);
   1.134      Value v0 = *va;
   1.135 -    if (v0 != NULL) {
   1.136 -      if (!v0->type()->is_illegal()) {
   1.137 -        assert(v0->as_HiWord() == NULL, "should never see HiWord during traversal");
   1.138 -        f->visit(va);
   1.139 +    if (v0 != NULL && !v0->type()->is_illegal()) {
   1.140 +      f->visit(va);
   1.141  #ifdef ASSERT
   1.142 -        Value v1 = *va;
   1.143 -        if (v0 != v1) {
   1.144 -          assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
   1.145 -          if (v0->type()->is_double_word()) {
   1.146 -            list.at_put(i + 1, v0->hi_word());
   1.147 -          }
   1.148 -        }
   1.149 +      Value v1 = *va;
   1.150 +      assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
   1.151 +      assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
   1.152  #endif
   1.153 -        if (v0->type()->is_double_word()) i++;
   1.154 -      }
   1.155 +      if (v0->type()->is_double_word()) i++;
   1.156      }
   1.157    }
   1.158  }
   1.159  
   1.160  
   1.161  void ValueStack::values_do(ValueVisitor* f) {
   1.162 -  apply(_stack, f);
   1.163 -  apply(_locks, f);
   1.164 -
   1.165    ValueStack* state = this;
   1.166    for_each_state(state) {
   1.167      apply(state->_locals, f);
   1.168 +    apply(state->_stack, f);
   1.169 +    apply(state->_locks, f);
   1.170    }
   1.171  }
   1.172  
   1.173 @@ -164,52 +148,26 @@
   1.174  }
   1.175  
   1.176  
   1.177 -int ValueStack::lock(IRScope* scope, Value obj) {
   1.178 +int ValueStack::total_locks_size() const {
   1.179 +  int num_locks = 0;
   1.180 +  const ValueStack* state = this;
   1.181 +  for_each_state(state) {
   1.182 +    num_locks += state->locks_size();
   1.183 +  }
   1.184 +  return num_locks;
   1.185 +}
   1.186 +
   1.187 +int ValueStack::lock(Value obj) {
   1.188    _locks.push(obj);
   1.189 -  scope->set_min_number_of_locks(locks_size());
   1.190 -  return locks_size() - 1;
   1.191 +  int num_locks = total_locks_size();
   1.192 +  scope()->set_min_number_of_locks(num_locks);
   1.193 +  return num_locks - 1;
   1.194  }
   1.195  
   1.196  
   1.197  int ValueStack::unlock() {
   1.198    _locks.pop();
   1.199 -  return locks_size();
   1.200 -}
   1.201 -
   1.202 -
   1.203 -ValueStack* ValueStack::push_scope(IRScope* scope) {
   1.204 -  assert(scope->caller() == _scope, "scopes must have caller/callee relationship");
   1.205 -  ValueStack* res = new ValueStack(scope,
   1.206 -                                   scope->method()->max_locals(),
   1.207 -                                   max_stack_size() + scope->method()->max_stack());
   1.208 -  // Preserves stack and monitors.
   1.209 -  res->_stack.appendAll(&_stack);
   1.210 -  res->_locks.appendAll(&_locks);
   1.211 -  assert(res->_stack.size() <= res->max_stack_size(), "stack overflow");
   1.212 -  return res;
   1.213 -}
   1.214 -
   1.215 -
   1.216 -ValueStack* ValueStack::pop_scope() {
   1.217 -  assert(_scope->caller() != NULL, "scope must have caller");
   1.218 -  IRScope* scope = _scope->caller();
   1.219 -  int max_stack = max_stack_size() - _scope->method()->max_stack();
   1.220 -  assert(max_stack >= 0, "stack underflow");
   1.221 -  ValueStack* res = new ValueStack(scope,
   1.222 -                                   scope->method()->max_locals(),
   1.223 -                                   max_stack);
   1.224 -  // Preserves stack and monitors. Restores local and store state from caller scope.
   1.225 -  res->_stack.appendAll(&_stack);
   1.226 -  res->_locks.appendAll(&_locks);
   1.227 -  ValueStack* caller = caller_state();
   1.228 -  if (caller != NULL) {
   1.229 -    for (int i = 0; i < caller->_locals.length(); i++) {
   1.230 -      res->_locals.at_put(i, caller->_locals.at(i));
   1.231 -    }
   1.232 -    assert(res->_locals.length() == res->scope()->method()->max_locals(), "just checking");
   1.233 -  }
   1.234 -  assert(res->_stack.size() <= res->max_stack_size(), "stack overflow");
   1.235 -  return res;
   1.236 +  return total_locks_size();
   1.237  }
   1.238  
   1.239  
   1.240 @@ -220,11 +178,7 @@
   1.241    Value phi = new Phi(t, b, -index - 1);
   1.242    _stack[index] = phi;
   1.243  
   1.244 -#ifdef ASSERT
   1.245 -  if (t->is_double_word()) {
   1.246 -    _stack[index + 1] = phi->hi_word();
   1.247 -  }
   1.248 -#endif
   1.249 +  assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL");
   1.250  }
   1.251  
   1.252  void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {
   1.253 @@ -236,7 +190,9 @@
   1.254  }
   1.255  
   1.256  #ifndef PRODUCT
   1.257 +
   1.258  void ValueStack::print() {
   1.259 +  scope()->method()->print_name();
   1.260    if (stack_is_empty()) {
   1.261      tty->print_cr("empty stack");
   1.262    } else {
   1.263 @@ -244,18 +200,20 @@
   1.264      for (int i = 0; i < stack_size();) {
   1.265        Value t = stack_at_inc(i);
   1.266        tty->print("%2d  ", i);
   1.267 +      tty->print("%c%d ", t->type()->tchar(), t->id());
   1.268        ip.print_instr(t);
   1.269        tty->cr();
   1.270      }
   1.271    }
   1.272    if (!no_active_locks()) {
   1.273      InstructionPrinter ip;
   1.274 -    for (int i = 0; i < locks_size(); i--) {
   1.275 +    for (int i = 0; i < locks_size(); i++) {
   1.276        Value t = lock_at(i);
   1.277        tty->print("lock %2d  ", i);
   1.278        if (t == NULL) {
   1.279          tty->print("this");
   1.280        } else {
   1.281 +        tty->print("%c%d ", t->type()->tchar(), t->id());
   1.282          ip.print_instr(t);
   1.283        }
   1.284        tty->cr();
   1.285 @@ -270,16 +228,55 @@
   1.286          tty->print("null");
   1.287          i ++;
   1.288        } else {
   1.289 +        tty->print("%c%d ", l->type()->tchar(), l->id());
   1.290          ip.print_instr(l);
   1.291          if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;
   1.292        }
   1.293        tty->cr();
   1.294      }
   1.295    }
   1.296 +
   1.297 +  if (caller_state() != NULL) {
   1.298 +    caller_state()->print();
   1.299 +  }
   1.300  }
   1.301  
   1.302  
   1.303  void ValueStack::verify() {
   1.304 -  Unimplemented();
   1.305 +  assert(scope() != NULL, "scope must exist");
   1.306 +  if (caller_state() != NULL) {
   1.307 +    assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");
   1.308 +    caller_state()->verify();
   1.309 +  }
   1.310 +
   1.311 +  if (kind() == Parsing) {
   1.312 +    assert(bci() == -99, "bci not defined during parsing");
   1.313 +  } else {
   1.314 +    assert(bci() >= -1, "bci out of range");
   1.315 +    assert(bci() < scope()->method()->code_size(), "bci out of range");
   1.316 +    assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");
   1.317 +    assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");
   1.318 +  }
   1.319 +
   1.320 +  int i;
   1.321 +  for (i = 0; i < stack_size(); i++) {
   1.322 +    Value v = _stack.at(i);
   1.323 +    if (v == NULL) {
   1.324 +      assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack");
   1.325 +    } else if (v->type()->is_double_word()) {
   1.326 +      assert(_stack.at(i + 1) == NULL, "hi-word must be NULL");
   1.327 +    }
   1.328 +  }
   1.329 +
   1.330 +  for (i = 0; i < locals_size(); i++) {
   1.331 +    Value v = _locals.at(i);
   1.332 +    if (v != NULL && v->type()->is_double_word()) {
   1.333 +      assert(_locals.at(i + 1) == NULL, "hi-word must be NULL");
   1.334 +    }
   1.335 +  }
   1.336 +
   1.337 +  for_each_state_value(this, v,
   1.338 +    assert(v != NULL, "just test if state-iteration succeeds");
   1.339 +  );
   1.340  }
   1.341  #endif // PRODUCT

mercurial