1// Copyright 2013 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#include <stdlib.h>
29
30#include "src/v8.h"
31#include "test/cctest/cctest.h"
32
33#include "src/base/platform/platform.h"
34#include "src/factory.h"
35#include "src/macro-assembler.h"
36#include "src/serialize.h"
37
38using namespace v8::internal;
39
40#if __GNUC__
41#define STDCALL  __attribute__((stdcall))
42#else
43#define STDCALL  __stdcall
44#endif
45
46typedef int STDCALL F0Type();
47typedef F0Type* F0;
48
49#define __ masm->
50
51
52TEST(LoadAndStoreWithRepresentation) {
53  // Allocate an executable page of memory.
54  size_t actual_size;
55  byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
56      Assembler::kMinimalBufferSize, &actual_size, true));
57  CHECK(buffer);
58  Isolate* isolate = CcTest::i_isolate();
59  HandleScope handles(isolate);
60  MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
61  MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
62  __ push(ebx);
63  __ push(edx);
64  __ sub(esp, Immediate(1 * kPointerSize));
65  Label exit;
66
67  // Test 1.
68  __ mov(eax, Immediate(1));  // Test number.
69  __ mov(Operand(esp, 0 * kPointerSize), Immediate(0));
70  __ mov(ebx, Immediate(-1));
71  __ Store(ebx, Operand(esp, 0 * kPointerSize), Representation::UInteger8());
72  __ mov(ebx, Operand(esp, 0 * kPointerSize));
73  __ mov(edx, Immediate(255));
74  __ cmp(ebx, edx);
75  __ j(not_equal, &exit);
76  __ Load(ebx, Operand(esp, 0 * kPointerSize), Representation::UInteger8());
77  __ cmp(ebx, edx);
78  __ j(not_equal, &exit);
79
80
81  // Test 2.
82  __ mov(eax, Immediate(2));  // Test number.
83  __ mov(Operand(esp, 0 * kPointerSize), Immediate(0));
84  __ mov(ebx, Immediate(-1));
85  __ Store(ebx, Operand(esp, 0 * kPointerSize), Representation::Integer8());
86  __ mov(ebx, Operand(esp, 0 * kPointerSize));
87  __ mov(edx, Immediate(255));
88  __ cmp(ebx, edx);
89  __ j(not_equal, &exit);
90  __ Load(ebx, Operand(esp, 0 * kPointerSize), Representation::Integer8());
91  __ mov(edx, Immediate(-1));
92  __ cmp(ebx, edx);
93  __ j(not_equal, &exit);
94
95  // Test 3.
96  __ mov(eax, Immediate(3));  // Test number.
97  __ mov(Operand(esp, 0 * kPointerSize), Immediate(0));
98  __ mov(ebx, Immediate(-1));
99  __ Store(ebx, Operand(esp, 0 * kPointerSize), Representation::Integer16());
100  __ mov(ebx, Operand(esp, 0 * kPointerSize));
101  __ mov(edx, Immediate(65535));
102  __ cmp(ebx, edx);
103  __ j(not_equal, &exit);
104  __ Load(edx, Operand(esp, 0 * kPointerSize), Representation::Integer16());
105  __ mov(ebx, Immediate(-1));
106  __ cmp(ebx, edx);
107  __ j(not_equal, &exit);
108
109  // Test 4.
110  __ mov(eax, Immediate(4));  // Test number.
111  __ mov(Operand(esp, 0 * kPointerSize), Immediate(0));
112  __ mov(ebx, Immediate(-1));
113  __ Store(ebx, Operand(esp, 0 * kPointerSize), Representation::UInteger16());
114  __ mov(ebx, Operand(esp, 0 * kPointerSize));
115  __ mov(edx, Immediate(65535));
116  __ cmp(ebx, edx);
117  __ j(not_equal, &exit);
118  __ Load(edx, Operand(esp, 0 * kPointerSize), Representation::UInteger16());
119  __ cmp(ebx, edx);
120  __ j(not_equal, &exit);
121
122  // Test 5.
123  __ mov(eax, Immediate(5));  // Test XMM move immediate.
124  __ Move(xmm0, 0.0);
125  __ Move(xmm1, 0.0);
126  __ ucomisd(xmm0, xmm1);
127  __ j(not_equal, &exit);
128  __ Move(xmm2, 991.01);
129  __ ucomisd(xmm0, xmm2);
130  __ j(equal, &exit);
131  __ Move(xmm0, 991.01);
132  __ ucomisd(xmm0, xmm2);
133  __ j(not_equal, &exit);
134
135  // Test 6.
136  __ mov(eax, Immediate(6));
137  __ Move(edx, Immediate(0));  // Test Move()
138  __ cmp(edx, Immediate(0));
139  __ j(not_equal, &exit);
140  __ Move(ecx, Immediate(-1));
141  __ cmp(ecx, Immediate(-1));
142  __ j(not_equal, &exit);
143  __ Move(ebx, Immediate(0x77));
144  __ cmp(ebx, Immediate(0x77));
145  __ j(not_equal, &exit);
146
147  __ xor_(eax, eax);  // Success.
148  __ bind(&exit);
149  __ add(esp, Immediate(1 * kPointerSize));
150  __ pop(edx);
151  __ pop(ebx);
152  __ ret(0);
153
154  CodeDesc desc;
155  masm->GetCode(&desc);
156  // Call the function from C++.
157  int result = FUNCTION_CAST<F0>(buffer)();
158  CHECK_EQ(0, result);
159}
160
161#undef __
162