2e9fcb6c54d2c23859d5593db994f668c0dce08e
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / ia32 / assembler-ia32-inl.h
1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // - Redistribution in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // - Neither the name of Sun Microsystems or the names of contributors may
16 // be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // The original source code covered by the above license above has been
32 // modified significantly by Google Inc.
33 // Copyright 2011 the V8 project authors. All rights reserved.
34
35 // A light-weight IA32 Assembler.
36
37 #ifndef V8_IA32_ASSEMBLER_IA32_INL_H_
38 #define V8_IA32_ASSEMBLER_IA32_INL_H_
39
40 #include "cpu.h"
41 #include "debug.h"
42
43 namespace v8 {
44 namespace internal {
45
46
47 // The modes possibly affected by apply must be in kApplyMask.
48 void RelocInfo::apply(intptr_t delta) {
49   if (rmode_ == RUNTIME_ENTRY || IsCodeTarget(rmode_)) {
50     int32_t* p = reinterpret_cast<int32_t*>(pc_);
51     *p -= delta;  // Relocate entry.
52     CPU::FlushICache(p, sizeof(uint32_t));
53   } else if (rmode_ == JS_RETURN && IsPatchedReturnSequence()) {
54     // Special handling of js_return when a break point is set (call
55     // instruction has been inserted).
56     int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
57     *p -= delta;  // Relocate entry.
58     CPU::FlushICache(p, sizeof(uint32_t));
59   } else if (rmode_ == DEBUG_BREAK_SLOT && IsPatchedDebugBreakSlotSequence()) {
60     // Special handling of a debug break slot when a break point is set (call
61     // instruction has been inserted).
62     int32_t* p = reinterpret_cast<int32_t*>(pc_ + 1);
63     *p -= delta;  // Relocate entry.
64     CPU::FlushICache(p, sizeof(uint32_t));
65   } else if (IsInternalReference(rmode_)) {
66     // absolute code pointer inside code object moves with the code object.
67     int32_t* p = reinterpret_cast<int32_t*>(pc_);
68     *p += delta;  // Relocate entry.
69     CPU::FlushICache(p, sizeof(uint32_t));
70   }
71 }
72
73
74 Address RelocInfo::target_address() {
75   ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
76   return Assembler::target_address_at(pc_);
77 }
78
79
80 Address RelocInfo::target_address_address() {
81   ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
82   return reinterpret_cast<Address>(pc_);
83 }
84
85
86 int RelocInfo::target_address_size() {
87   return Assembler::kExternalTargetSize;
88 }
89
90
91 void RelocInfo::set_target_address(Address target, WriteBarrierMode mode) {
92   Assembler::set_target_address_at(pc_, target);
93   ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY);
94   if (mode == UPDATE_WRITE_BARRIER && host() != NULL && IsCodeTarget(rmode_)) {
95     Object* target_code = Code::GetCodeFromTargetAddress(target);
96     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
97         host(), this, HeapObject::cast(target_code));
98   }
99 }
100
101
102 Object* RelocInfo::target_object() {
103   ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
104   return Memory::Object_at(pc_);
105 }
106
107
108 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
109   ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
110   return Memory::Object_Handle_at(pc_);
111 }
112
113
114 Object** RelocInfo::target_object_address() {
115   ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
116   return &Memory::Object_at(pc_);
117 }
118
119
120 void RelocInfo::set_target_object(Object* target, WriteBarrierMode mode) {
121   ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
122   Memory::Object_at(pc_) = target;
123   CPU::FlushICache(pc_, sizeof(Address));
124   if (mode == UPDATE_WRITE_BARRIER &&
125       host() != NULL &&
126       target->IsHeapObject()) {
127     host()->GetHeap()->incremental_marking()->RecordWrite(
128         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
129   }
130 }
131
132
133 Address* RelocInfo::target_reference_address() {
134   ASSERT(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
135   return reinterpret_cast<Address*>(pc_);
136 }
137
138
139 Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() {
140   ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
141   Address address = Memory::Address_at(pc_);
142   return Handle<JSGlobalPropertyCell>(
143       reinterpret_cast<JSGlobalPropertyCell**>(address));
144 }
145
146
147 JSGlobalPropertyCell* RelocInfo::target_cell() {
148   ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
149   Address address = Memory::Address_at(pc_);
150   Object* object = HeapObject::FromAddress(
151       address - JSGlobalPropertyCell::kValueOffset);
152   return reinterpret_cast<JSGlobalPropertyCell*>(object);
153 }
154
155
156 void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell,
157                                 WriteBarrierMode mode) {
158   ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL);
159   Address address = cell->address() + JSGlobalPropertyCell::kValueOffset;
160   Memory::Address_at(pc_) = address;
161   CPU::FlushICache(pc_, sizeof(Address));
162   if (mode == UPDATE_WRITE_BARRIER && host() != NULL) {
163     // TODO(1550) We are passing NULL as a slot because cell can never be on
164     // evacuation candidate.
165     host()->GetHeap()->incremental_marking()->RecordWrite(
166         host(), NULL, cell);
167   }
168 }
169
170
171 Address RelocInfo::call_address() {
172   ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
173          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
174   return Assembler::target_address_at(pc_ + 1);
175 }
176
177
178 void RelocInfo::set_call_address(Address target) {
179   ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
180          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
181   Assembler::set_target_address_at(pc_ + 1, target);
182   if (host() != NULL) {
183     Object* target_code = Code::GetCodeFromTargetAddress(target);
184     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
185         host(), this, HeapObject::cast(target_code));
186   }
187 }
188
189
190 Object* RelocInfo::call_object() {
191   return *call_object_address();
192 }
193
194
195 void RelocInfo::set_call_object(Object* target) {
196   *call_object_address() = target;
197 }
198
199
200 Object** RelocInfo::call_object_address() {
201   ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
202          (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
203   return reinterpret_cast<Object**>(pc_ + 1);
204 }
205
206
207 bool RelocInfo::IsPatchedReturnSequence() {
208   return *pc_ == 0xE8;
209 }
210
211
212 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
213   return !Assembler::IsNop(pc());
214 }
215
216
217 void RelocInfo::Visit(ObjectVisitor* visitor) {
218   RelocInfo::Mode mode = rmode();
219   if (mode == RelocInfo::EMBEDDED_OBJECT) {
220     visitor->VisitEmbeddedPointer(this);
221     CPU::FlushICache(pc_, sizeof(Address));
222   } else if (RelocInfo::IsCodeTarget(mode)) {
223     visitor->VisitCodeTarget(this);
224   } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
225     visitor->VisitGlobalPropertyCell(this);
226   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
227     visitor->VisitExternalReference(target_reference_address());
228     CPU::FlushICache(pc_, sizeof(Address));
229 #ifdef ENABLE_DEBUGGER_SUPPORT
230   // TODO(isolates): Get a cached isolate below.
231   } else if (((RelocInfo::IsJSReturn(mode) &&
232               IsPatchedReturnSequence()) ||
233              (RelocInfo::IsDebugBreakSlot(mode) &&
234               IsPatchedDebugBreakSlotSequence())) &&
235              Isolate::Current()->debug()->has_break_points()) {
236     visitor->VisitDebugTarget(this);
237 #endif
238   } else if (mode == RelocInfo::RUNTIME_ENTRY) {
239     visitor->VisitRuntimeEntry(this);
240   }
241 }
242
243
244 template<typename StaticVisitor>
245 void RelocInfo::Visit(Heap* heap) {
246   RelocInfo::Mode mode = rmode();
247   if (mode == RelocInfo::EMBEDDED_OBJECT) {
248     StaticVisitor::VisitEmbeddedPointer(heap, this);
249     CPU::FlushICache(pc_, sizeof(Address));
250   } else if (RelocInfo::IsCodeTarget(mode)) {
251     StaticVisitor::VisitCodeTarget(heap, this);
252   } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
253     StaticVisitor::VisitGlobalPropertyCell(heap, this);
254   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
255     StaticVisitor::VisitExternalReference(target_reference_address());
256     CPU::FlushICache(pc_, sizeof(Address));
257 #ifdef ENABLE_DEBUGGER_SUPPORT
258   } else if (heap->isolate()->debug()->has_break_points() &&
259              ((RelocInfo::IsJSReturn(mode) &&
260               IsPatchedReturnSequence()) ||
261              (RelocInfo::IsDebugBreakSlot(mode) &&
262               IsPatchedDebugBreakSlotSequence()))) {
263     StaticVisitor::VisitDebugTarget(heap, this);
264 #endif
265   } else if (mode == RelocInfo::RUNTIME_ENTRY) {
266     StaticVisitor::VisitRuntimeEntry(this);
267   }
268 }
269
270
271
272 Immediate::Immediate(int x)  {
273   x_ = x;
274   rmode_ = RelocInfo::NONE;
275 }
276
277
278 Immediate::Immediate(const ExternalReference& ext) {
279   x_ = reinterpret_cast<int32_t>(ext.address());
280   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
281 }
282
283
284 Immediate::Immediate(Label* internal_offset) {
285   x_ = reinterpret_cast<int32_t>(internal_offset);
286   rmode_ = RelocInfo::INTERNAL_REFERENCE;
287 }
288
289
290 Immediate::Immediate(Handle<Object> handle) {
291   // Verify all Objects referred by code are NOT in new space.
292   Object* obj = *handle;
293   ASSERT(!HEAP->InNewSpace(obj));
294   if (obj->IsHeapObject()) {
295     x_ = reinterpret_cast<intptr_t>(handle.location());
296     rmode_ = RelocInfo::EMBEDDED_OBJECT;
297   } else {
298     // no relocation needed
299     x_ =  reinterpret_cast<intptr_t>(obj);
300     rmode_ = RelocInfo::NONE;
301   }
302 }
303
304
305 Immediate::Immediate(Smi* value) {
306   x_ = reinterpret_cast<intptr_t>(value);
307   rmode_ = RelocInfo::NONE;
308 }
309
310
311 Immediate::Immediate(Address addr) {
312   x_ = reinterpret_cast<int32_t>(addr);
313   rmode_ = RelocInfo::NONE;
314 }
315
316
317 void Assembler::emit(uint32_t x) {
318   *reinterpret_cast<uint32_t*>(pc_) = x;
319   pc_ += sizeof(uint32_t);
320 }
321
322
323 void Assembler::emit(Handle<Object> handle) {
324   // Verify all Objects referred by code are NOT in new space.
325   Object* obj = *handle;
326   ASSERT(!isolate()->heap()->InNewSpace(obj));
327   if (obj->IsHeapObject()) {
328     emit(reinterpret_cast<intptr_t>(handle.location()),
329          RelocInfo::EMBEDDED_OBJECT);
330   } else {
331     // no relocation needed
332     emit(reinterpret_cast<intptr_t>(obj));
333   }
334 }
335
336
337 void Assembler::emit(uint32_t x, RelocInfo::Mode rmode, unsigned id) {
338   if (rmode == RelocInfo::CODE_TARGET && id != kNoASTId) {
339     RecordRelocInfo(RelocInfo::CODE_TARGET_WITH_ID, static_cast<intptr_t>(id));
340   } else if (rmode != RelocInfo::NONE) {
341     RecordRelocInfo(rmode);
342   }
343   emit(x);
344 }
345
346
347 void Assembler::emit(const Immediate& x) {
348   if (x.rmode_ == RelocInfo::INTERNAL_REFERENCE) {
349     Label* label = reinterpret_cast<Label*>(x.x_);
350     emit_code_relative_offset(label);
351     return;
352   }
353   if (x.rmode_ != RelocInfo::NONE) RecordRelocInfo(x.rmode_);
354   emit(x.x_);
355 }
356
357
358 void Assembler::emit_code_relative_offset(Label* label) {
359   if (label->is_bound()) {
360     int32_t pos;
361     pos = label->pos() + Code::kHeaderSize - kHeapObjectTag;
362     emit(pos);
363   } else {
364     emit_disp(label, Displacement::CODE_RELATIVE);
365   }
366 }
367
368
369 void Assembler::emit_w(const Immediate& x) {
370   ASSERT(x.rmode_ == RelocInfo::NONE);
371   uint16_t value = static_cast<uint16_t>(x.x_);
372   reinterpret_cast<uint16_t*>(pc_)[0] = value;
373   pc_ += sizeof(uint16_t);
374 }
375
376
377 Address Assembler::target_address_at(Address pc) {
378   return pc + sizeof(int32_t) + *reinterpret_cast<int32_t*>(pc);
379 }
380
381
382 void Assembler::set_target_address_at(Address pc, Address target) {
383   int32_t* p = reinterpret_cast<int32_t*>(pc);
384   *p = target - (pc + sizeof(int32_t));
385   CPU::FlushICache(p, sizeof(int32_t));
386 }
387
388
389 Displacement Assembler::disp_at(Label* L) {
390   return Displacement(long_at(L->pos()));
391 }
392
393
394 void Assembler::disp_at_put(Label* L, Displacement disp) {
395   long_at_put(L->pos(), disp.data());
396 }
397
398
399 void Assembler::emit_disp(Label* L, Displacement::Type type) {
400   Displacement disp(L, type);
401   L->link_to(pc_offset());
402   emit(static_cast<int>(disp.data()));
403 }
404
405
406 void Assembler::emit_near_disp(Label* L) {
407   byte disp = 0x00;
408   if (L->is_near_linked()) {
409     int offset = L->near_link_pos() - pc_offset();
410     ASSERT(is_int8(offset));
411     disp = static_cast<byte>(offset & 0xFF);
412   }
413   L->link_to(pc_offset(), Label::kNear);
414   *pc_++ = disp;
415 }
416
417
418 void Operand::set_modrm(int mod, Register rm) {
419   ASSERT((mod & -4) == 0);
420   buf_[0] = mod << 6 | rm.code();
421   len_ = 1;
422 }
423
424
425 void Operand::set_sib(ScaleFactor scale, Register index, Register base) {
426   ASSERT(len_ == 1);
427   ASSERT((scale & -4) == 0);
428   // Use SIB with no index register only for base esp.
429   ASSERT(!index.is(esp) || base.is(esp));
430   buf_[1] = scale << 6 | index.code() << 3 | base.code();
431   len_ = 2;
432 }
433
434
435 void Operand::set_disp8(int8_t disp) {
436   ASSERT(len_ == 1 || len_ == 2);
437   *reinterpret_cast<int8_t*>(&buf_[len_++]) = disp;
438 }
439
440
441 void Operand::set_dispr(int32_t disp, RelocInfo::Mode rmode) {
442   ASSERT(len_ == 1 || len_ == 2);
443   int32_t* p = reinterpret_cast<int32_t*>(&buf_[len_]);
444   *p = disp;
445   len_ += sizeof(int32_t);
446   rmode_ = rmode;
447 }
448
449 Operand::Operand(Register reg) {
450   // reg
451   set_modrm(3, reg);
452 }
453
454
455 Operand::Operand(XMMRegister xmm_reg) {
456   Register reg = { xmm_reg.code() };
457   set_modrm(3, reg);
458 }
459
460
461 Operand::Operand(int32_t disp, RelocInfo::Mode rmode) {
462   // [disp/r]
463   set_modrm(0, ebp);
464   set_dispr(disp, rmode);
465 }
466
467 } }  // namespace v8::internal
468
469 #endif  // V8_IA32_ASSEMBLER_IA32_INL_H_