blob: 2755eee648d37e1b4e9bef13bcffbb4f1fecf169 [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_VIRTUAL_FRAME_HEAVY_INL_H_
29#define V8_VIRTUAL_FRAME_HEAVY_INL_H_
30
31#include "type-info.h"
32#include "register-allocator.h"
33#include "scopes.h"
Kristian Monsen25f61362010-05-21 11:50:48 +010034#include "register-allocator-inl.h"
35#include "codegen-inl.h"
Steve Block6ded16b2010-05-10 14:33:55 +010036
37namespace v8 {
38namespace internal {
39
40// On entry to a function, the virtual frame already contains the receiver,
41// the parameters, and a return address. All frame elements are in memory.
42VirtualFrame::VirtualFrame()
43 : elements_(parameter_count() + local_count() + kPreallocatedElements),
44 stack_pointer_(parameter_count() + 1) { // 0-based index of TOS.
45 for (int i = 0; i <= stack_pointer_; i++) {
46 elements_.Add(FrameElement::MemoryElement(TypeInfo::Unknown()));
47 }
48 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
49 register_locations_[i] = kIllegalIndex;
50 }
51}
52
53
54// When cloned, a frame is a deep copy of the original.
55VirtualFrame::VirtualFrame(VirtualFrame* original)
56 : elements_(original->element_count()),
57 stack_pointer_(original->stack_pointer_) {
58 elements_.AddAll(original->elements_);
59 // Copy register locations from original.
60 memcpy(&register_locations_,
61 original->register_locations_,
62 sizeof(register_locations_));
63}
64
65
66void VirtualFrame::PushFrameSlotAt(int index) {
67 elements_.Add(CopyElementAt(index));
68}
69
70
71void VirtualFrame::Push(Register reg, TypeInfo info) {
72 if (is_used(reg)) {
73 int index = register_location(reg);
74 FrameElement element = CopyElementAt(index, info);
75 elements_.Add(element);
76 } else {
77 Use(reg, element_count());
78 FrameElement element =
79 FrameElement::RegisterElement(reg, FrameElement::NOT_SYNCED, info);
80 elements_.Add(element);
81 }
82}
83
84
85void VirtualFrame::Push(Handle<Object> value) {
86 FrameElement element =
87 FrameElement::ConstantElement(value, FrameElement::NOT_SYNCED);
88 elements_.Add(element);
89}
90
91
92bool VirtualFrame::Equals(VirtualFrame* other) {
93#ifdef DEBUG
94 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
95 if (register_location(i) != other->register_location(i)) {
96 return false;
97 }
98 }
99 if (element_count() != other->element_count()) return false;
100#endif
101 if (stack_pointer_ != other->stack_pointer_) return false;
102 for (int i = 0; i < element_count(); i++) {
103 if (!elements_[i].Equals(other->elements_[i])) return false;
104 }
105
106 return true;
107}
108
109
110void VirtualFrame::SetTypeForLocalAt(int index, TypeInfo info) {
111 elements_[local0_index() + index].set_type_info(info);
112}
113
114
115// Make the type of all elements be MEMORY.
116void VirtualFrame::SpillAll() {
117 for (int i = 0; i < element_count(); i++) {
118 SpillElementAt(i);
119 }
120}
121
122
123void VirtualFrame::PrepareForReturn() {
124 // Spill all locals. This is necessary to make sure all locals have
125 // the right value when breaking at the return site in the debugger.
126 for (int i = 0; i < expression_base_index(); i++) {
127 SpillElementAt(i);
128 }
129}
130
131
132void VirtualFrame::SetTypeForParamAt(int index, TypeInfo info) {
133 elements_[param0_index() + index].set_type_info(info);
134}
135
136
137void VirtualFrame::Nip(int num_dropped) {
138 ASSERT(num_dropped >= 0);
139 if (num_dropped == 0) return;
140 Result tos = Pop();
141 if (num_dropped > 1) {
142 Drop(num_dropped - 1);
143 }
144 SetElementAt(0, &tos);
145}
146
147
148void VirtualFrame::Push(Smi* value) {
149 Push(Handle<Object> (value));
150}
151
Kristian Monsen25f61362010-05-21 11:50:48 +0100152
153int VirtualFrame::register_location(Register reg) {
154 return register_locations_[RegisterAllocator::ToNumber(reg)];
155}
156
157
158void VirtualFrame::set_register_location(Register reg, int index) {
159 register_locations_[RegisterAllocator::ToNumber(reg)] = index;
160}
161
162
163bool VirtualFrame::is_used(Register reg) {
164 return register_locations_[RegisterAllocator::ToNumber(reg)]
165 != kIllegalIndex;
166}
167
168
169void VirtualFrame::SetElementAt(int index, Handle<Object> value) {
170 Result temp(value);
171 SetElementAt(index, &temp);
172}
173
174
175Result VirtualFrame::CallStub(CodeStub* stub, int arg_count) {
176 PrepareForCall(arg_count, arg_count);
177 return RawCallStub(stub);
178}
179
180
181int VirtualFrame::parameter_count() {
182 return cgen()->scope()->num_parameters();
183}
184
185
186int VirtualFrame::local_count() {
187 return cgen()->scope()->num_stack_slots();
188}
189
Steve Block6ded16b2010-05-10 14:33:55 +0100190} } // namespace v8::internal
191
192#endif // V8_VIRTUAL_FRAME_HEAVY_INL_H_