1/**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef BYTECODE_OPTIMIZER_BYTECODE_ENCODER_H
17#define BYTECODE_OPTIMIZER_BYTECODE_ENCODER_H
18
19#include <stdint.h>
20
21#include "compiler/optimizer/code_generator/encode.h"
22
23namespace panda::bytecodeopt {
24class BytecodeEncoder final : public compiler::Encoder {
25public:
26    explicit BytecodeEncoder(ArenaAllocator *allocator) : Encoder(allocator, Arch::NONE) {}
27
28    void Finalize() override {};
29
30    static bool CanEncodeImmHelper(int64_t imm, uint32_t size, int64_t min, int64_t max)
31    {
32        constexpr uint8_t HALF_SIZE = 32;
33        if (size != HALF_SIZE) {
34            return false;
35        }
36
37        return imm >= min && imm <= max;
38    }
39
40    bool CanEncodeImmAddSubCmp(int64_t imm, uint32_t size, [[maybe_unused]] bool signed_compare) override
41    {
42        return CanEncodeImmHelper(imm, size, INT8_MIN, INT8_MAX);
43    }
44
45    bool CanEncodeImmMulDivMod(uint64_t imm, uint32_t size) override
46    {
47        return CanEncodeImmAddSubCmp(imm, size, false);
48    }
49
50    bool CanEncodeImmLogical(uint64_t imm, uint32_t size) override
51    {
52        return CanEncodeImmHelper(imm, size, INT32_MIN, INT32_MAX);
53    }
54
55    bool CanEncodeShift(uint32_t size) override
56    {
57        constexpr uint32_t UNSUPPORTED_SHIFT_SIZE = 64;
58        return size != UNSUPPORTED_SHIFT_SIZE;
59    }
60    size_t GetLabelAddress(compiler::LabelHolder::LabelId) override
61    {
62        return 0;
63    }
64    bool LabelHasLinks(compiler::LabelHolder::LabelId) override
65    {
66        return false;
67    }
68};  // BytecodeEncoder
69}  // namespace panda::bytecodeopt
70
71#endif  // BYTECODE_OPTIMIZER_BYTECODE_ENCODER_H
72