Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / test-platform.cc
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/base/platform/platform.h"
31 #include "test/cctest/cctest.h"
32
33 using namespace ::v8::internal;
34
35 #ifdef __GNUC__
36 #define ASM __asm__ __volatile__
37
38 #if defined(_M_X64) || defined(__x86_64__)
39 #define GET_STACK_POINTER() \
40   static int sp_addr = 0; \
41   do { \
42     ASM("mov %%rsp, %0" : "=g" (sp_addr)); \
43   } while (0)
44 #elif defined(_M_IX86) || defined(__i386__)
45 #define GET_STACK_POINTER() \
46   static int sp_addr = 0; \
47   do { \
48     ASM("mov %%esp, %0" : "=g" (sp_addr)); \
49   } while (0)
50 #elif defined(__ARMEL__)
51 #define GET_STACK_POINTER() \
52   static int sp_addr = 0; \
53   do { \
54     ASM("str %%sp, %0" : "=g" (sp_addr)); \
55   } while (0)
56 #elif defined(__AARCH64EL__)
57 #define GET_STACK_POINTER() \
58   static int sp_addr = 0; \
59   do { \
60     ASM("mov x16, sp; str x16, %0" : "=g" (sp_addr)); \
61   } while (0)
62 #elif defined(__MIPSEB__) || defined(__MIPSEL__)
63 #define GET_STACK_POINTER() \
64   static int sp_addr = 0; \
65   do { \
66     ASM("sw $sp, %0" : "=g" (sp_addr)); \
67   } while (0)
68 #else
69 #error Host architecture was not detected as supported by v8
70 #endif
71
72 void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
73   GET_STACK_POINTER();
74   args.GetReturnValue().Set(v8_num(sp_addr));
75 }
76
77
78 TEST(StackAlignment) {
79   v8::Isolate* isolate = CcTest::isolate();
80   v8::HandleScope handle_scope(isolate);
81   v8::Handle<v8::ObjectTemplate> global_template =
82       v8::ObjectTemplate::New(isolate);
83   global_template->Set(v8_str("get_stack_pointer"),
84                        v8::FunctionTemplate::New(isolate, GetStackPointer));
85
86   LocalContext env(NULL, global_template);
87   CompileRun(
88       "function foo() {"
89       "  return get_stack_pointer();"
90       "}");
91
92   v8::Local<v8::Object> global_object = env->Global();
93   v8::Local<v8::Function> foo =
94       v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo")));
95
96   v8::Local<v8::Value> result = foo->Call(global_object, 0, NULL);
97   CHECK_EQ(0, result->Int32Value() % v8::base::OS::ActivationFrameAlignment());
98 }
99
100 #undef GET_STACK_POINTERS
101 #undef ASM
102 #endif  // __GNUC__