Support for returning Secondary load counter
[hiphop-php.git] / hphp / runtime / test / bespoke-layout-mock.h
blob88a2ba57ab57f49ed027660a854e9db6892a328f
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef HPHP_RUNTIME_TEST_BESPOKE_LAYOUT_MOCK_H_
18 #define HPHP_RUNTIME_TEST_BESPOKE_LAYOUT_MOCK_H_
20 #include "hphp/runtime/base/bespoke/layout.h"
21 #include "hphp/runtime/vm/jit/irgen.h"
23 #include <folly/portability/GMock.h>
25 namespace HPHP{
26 namespace bespoke {
27 namespace testing {
29 std::atomic<uint16_t> s_num_abstract_layouts;
30 std::atomic<uint16_t> s_num_concrete_layouts;
32 struct MockLayout : public Layout {
33 MockLayout(const std::string& description, LayoutSet&& parents,
34 LayoutIndex idx, bool concrete)
35 : Layout(idx, description, std::move(parents), nullptr)
36 , m_concrete(concrete)
39 bool isConcrete() const override { return m_concrete; }
41 private:
42 bool m_concrete;
45 inline Layout* makeDummyLayout(const std::string& name,
46 std::vector<jit::ArrayLayout> parents,
47 bool concrete = true) {
48 using ::testing::Mock;
50 Layout::LayoutSet indices;
51 std::transform(
52 parents.cbegin(), parents.cend(),
53 std::inserter(indices, indices.end()),
54 [&](jit::ArrayLayout parent) {
55 always_assert(parent.bespoke());
56 return *parent.layoutIndex();
60 // In order to support type tests, we use a 1-hot encoding to encode leaf
61 // concrete layout indices. Abstract layout indices aren't constrained.
62 auto const index = [&]() -> LayoutIndex {
63 auto constexpr base = kLoggingLayoutByte << 8;
64 if (concrete) {
65 auto const index = s_num_concrete_layouts++;
66 return {safe_cast<uint16_t>(base + (1 << index))};
68 auto const index = s_num_abstract_layouts++;
69 return {safe_cast<uint16_t>(base + 0xff - index)};
70 }();
72 auto const ret = new MockLayout(name, std::move(indices), index, concrete);
73 Mock::AllowLeak(ret);
74 return ret;
77 inline Layout* makeDummyAbstractLayout(const std::string& name,
78 std::vector<jit::ArrayLayout> parents) {
79 return makeDummyLayout(name, parents, false);
83 } // namespace testing
84 } // namespace bespoke
85 } // namespace HPHP
87 #endif // HPHP_RUNTIME_TEST_BESPOKE_LAYOUT_MOCK_H_