c1_ValueStack.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25#include "incls/_precompiled.incl"
26#include "incls/_c1_ValueStack.cpp.incl"
27
28
29// Implementation of ValueStack
30
31ValueStack::ValueStack(IRScope* scope, int locals_size, int max_stack_size)
32: _scope(scope)
33, _locals(locals_size, NULL)
34, _stack(max_stack_size)
35, _lock_stack(false)
36, _locks(1)
37{
38  assert(scope != NULL, "scope must exist");
39}
40
41ValueStack* ValueStack::copy() {
42  ValueStack* s = new ValueStack(scope(), locals_size(), max_stack_size());
43  s->_stack.appendAll(&_stack);
44  s->_locks.appendAll(&_locks);
45  s->replace_locals(this);
46  return s;
47}
48
49
50ValueStack* ValueStack::copy_locks() {
51  int sz = scope()->lock_stack_size();
52  if (stack_size() == 0) {
53    sz = 0;
54  }
55  ValueStack* s = new ValueStack(scope(), locals_size(), sz);
56  s->_lock_stack = true;
57  s->_locks.appendAll(&_locks);
58  s->replace_locals(this);
59  if (sz > 0) {
60    assert(sz <= stack_size(), "lock stack underflow");
61    for (int i = 0; i < sz; i++) {
62      s->_stack.append(_stack[i]);
63    }
64  }
65  return s;
66}
67
68bool ValueStack::is_same(ValueStack* s) {
69  assert(s != NULL, "state must exist");
70  assert(scope      () == s->scope      (), "scopes       must correspond");
71  assert(locals_size() == s->locals_size(), "locals sizes must correspond");
72  return is_same_across_scopes(s);
73}
74
75
76bool ValueStack::is_same_across_scopes(ValueStack* s) {
77  assert(s != NULL, "state must exist");
78  assert(stack_size () == s->stack_size (), "stack  sizes must correspond");
79  assert(locks_size () == s->locks_size (), "locks  sizes must correspond");
80  // compare each stack element with the corresponding stack element of s
81  int index;
82  Value value;
83  for_each_stack_value(this, index, value) {
84    if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;
85  }
86  for_each_lock_value(this, index, value) {
87    if (value != s->lock_at(index)) return false;
88  }
89  return true;
90}
91
92
93ValueStack* ValueStack::caller_state() const {
94  return scope()->caller_state();
95}
96
97
98void ValueStack::clear_locals() {
99  for (int i = _locals.length() - 1; i >= 0; i--) {
100    _locals.at_put(i, NULL);
101  }
102}
103
104
105void ValueStack::replace_locals(ValueStack* with) {
106  assert(locals_size() == with->locals_size(), "number of locals must match");
107  for (int i = locals_size() - 1; i >= 0; i--) {
108    _locals.at_put(i, with->_locals.at(i));
109  }
110}
111
112void ValueStack::pin_stack_for_linear_scan() {
113  for_each_state_value(this, v,
114    if (v->as_Constant() == NULL && v->as_Local() == NULL) {
115      v->pin(Instruction::PinStackForStateSplit);
116    }
117  );
118}
119
120
121// apply function to all values of a list; factored out from values_do(f)
122void ValueStack::apply(Values list, void f(Value*)) {
123  for (int i = 0; i < list.length(); i++) {
124    Value* va = list.adr_at(i);
125    Value v0 = *va;
126    if (v0 != NULL) {
127      if (!v0->type()->is_illegal()) {
128        assert(v0->as_HiWord() == NULL, "should never see HiWord during traversal");
129        f(va);
130#ifdef ASSERT
131        Value v1 = *va;
132        if (v0 != v1) {
133          assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
134          if (v0->type()->is_double_word()) {
135            list.at_put(i + 1, v0->hi_word());
136          }
137        }
138#endif
139        if (v0->type()->is_double_word()) i++;
140      }
141    }
142  }
143}
144
145
146void ValueStack::values_do(void f(Value*)) {
147  apply(_stack, f);
148  apply(_locks, f);
149
150  ValueStack* state = this;
151  for_each_state(state) {
152    apply(state->_locals, f);
153  }
154}
155
156
157Values* ValueStack::pop_arguments(int argument_size) {
158  assert(stack_size() >= argument_size, "stack too small or too many arguments");
159  int base = stack_size() - argument_size;
160  Values* args = new Values(argument_size);
161  for (int i = base; i < stack_size();) args->push(stack_at_inc(i));
162  truncate_stack(base);
163  return args;
164}
165
166
167int ValueStack::lock(IRScope* scope, Value obj) {
168  _locks.push(obj);
169  scope->set_min_number_of_locks(locks_size());
170  return locks_size() - 1;
171}
172
173
174int ValueStack::unlock() {
175  _locks.pop();
176  return locks_size();
177}
178
179
180ValueStack* ValueStack::push_scope(IRScope* scope) {
181  assert(scope->caller() == _scope, "scopes must have caller/callee relationship");
182  ValueStack* res = new ValueStack(scope,
183                                   scope->method()->max_locals(),
184                                   max_stack_size() + scope->method()->max_stack());
185  // Preserves stack and monitors.
186  res->_stack.appendAll(&_stack);
187  res->_locks.appendAll(&_locks);
188  assert(res->_stack.size() <= res->max_stack_size(), "stack overflow");
189  return res;
190}
191
192
193ValueStack* ValueStack::pop_scope() {
194  assert(_scope->caller() != NULL, "scope must have caller");
195  IRScope* scope = _scope->caller();
196  int max_stack = max_stack_size() - _scope->method()->max_stack();
197  assert(max_stack >= 0, "stack underflow");
198  ValueStack* res = new ValueStack(scope,
199                                   scope->method()->max_locals(),
200                                   max_stack);
201  // Preserves stack and monitors. Restores local and store state from caller scope.
202  res->_stack.appendAll(&_stack);
203  res->_locks.appendAll(&_locks);
204  ValueStack* caller = caller_state();
205  if (caller != NULL) {
206    for (int i = 0; i < caller->_locals.length(); i++) {
207      res->_locals.at_put(i, caller->_locals.at(i));
208    }
209    assert(res->_locals.length() == res->scope()->method()->max_locals(), "just checking");
210  }
211  assert(res->_stack.size() <= res->max_stack_size(), "stack overflow");
212  return res;
213}
214
215
216void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {
217  assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created");
218
219  ValueType* t = stack_at(index)->type();
220  Value phi = new Phi(t, b, -index - 1);
221  _stack[index] = phi;
222
223#ifdef ASSERT
224  if (t->is_double_word()) {
225    _stack[index + 1] = phi->hi_word();
226  }
227#endif
228}
229
230void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {
231  assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created");
232
233  ValueType* t = local_at(index)->type();
234  Value phi = new Phi(t, b, index);
235  store_local(index, phi);
236}
237
238#ifndef PRODUCT
239void ValueStack::print() {
240  if (stack_is_empty()) {
241    tty->print_cr("empty stack");
242  } else {
243    InstructionPrinter ip;
244    for (int i = 0; i < stack_size();) {
245      Value t = stack_at_inc(i);
246      tty->print("%2d  ", i);
247      ip.print_instr(t);
248      tty->cr();
249    }
250  }
251  if (!no_active_locks()) {
252    InstructionPrinter ip;
253    for (int i = 0; i < locks_size(); i--) {
254      Value t = lock_at(i);
255      tty->print("lock %2d  ", i);
256      if (t == NULL) {
257        tty->print("this");
258      } else {
259        ip.print_instr(t);
260      }
261      tty->cr();
262    }
263  }
264  if (locals_size() > 0) {
265    InstructionPrinter ip;
266    for (int i = 0; i < locals_size();) {
267      Value l = _locals[i];
268      tty->print("local %d ", i);
269      if (l == NULL) {
270        tty->print("null");
271        i ++;
272      } else {
273        ip.print_instr(l);
274        if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;
275      }
276      tty->cr();
277    }
278  }
279}
280
281
282void ValueStack::verify() {
283  Unimplemented();
284}
285#endif // PRODUCT
286