/src/serenity/Userland/Libraries/LibJS/Bytecode/Register.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Format.h> |
10 | | |
11 | | namespace JS::Bytecode { |
12 | | |
13 | | class Register { |
14 | | public: |
15 | | constexpr static u32 accumulator_index = 0; |
16 | | |
17 | | static constexpr Register accumulator() |
18 | 66 | { |
19 | 66 | return Register(accumulator_index); |
20 | 66 | } |
21 | | |
22 | | constexpr static u32 saved_return_value_index = 1; |
23 | | |
24 | | static constexpr Register saved_return_value() |
25 | 22 | { |
26 | 22 | return Register(saved_return_value_index); |
27 | 22 | } |
28 | | |
29 | | static constexpr u32 exception_index = 2; |
30 | | |
31 | | static constexpr Register exception() |
32 | 35 | { |
33 | 35 | return Register(exception_index); |
34 | 35 | } |
35 | | |
36 | | static constexpr Register this_value() |
37 | 66 | { |
38 | 66 | constexpr u32 this_value_index = 3; |
39 | 66 | return Register(this_value_index); |
40 | 66 | } |
41 | | |
42 | | static constexpr Register return_value() |
43 | 44 | { |
44 | 44 | constexpr u32 return_value_index = 4; |
45 | 44 | return Register(return_value_index); |
46 | 44 | } |
47 | | |
48 | | static constexpr Register saved_exception() |
49 | 0 | { |
50 | 0 | constexpr u32 saved_exception_index = 5; |
51 | 0 | return Register(saved_exception_index); |
52 | 0 | } |
53 | | |
54 | | static constexpr u32 reserved_register_count = 6; |
55 | | |
56 | | constexpr explicit Register(u32 index) |
57 | 287 | : m_index(index) |
58 | 287 | { |
59 | 287 | } |
60 | | |
61 | 0 | constexpr bool operator==(Register reg) const { return m_index == reg.index(); } |
62 | | |
63 | 283 | constexpr u32 index() const { return m_index; } |
64 | | |
65 | | private: |
66 | | u32 m_index; |
67 | | }; |
68 | | |
69 | | } |
70 | | |
71 | | template<> |
72 | | struct AK::Formatter<JS::Bytecode::Register> : AK::Formatter<FormatString> { |
73 | | ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Register const& value) |
74 | 0 | { |
75 | 0 | if (value.index() == JS::Bytecode::Register::accumulator_index) |
76 | 0 | return builder.put_string("acc"sv); |
77 | 0 | return AK::Formatter<FormatString>::format(builder, "${}"sv, value.index()); |
78 | 0 | } |
79 | | }; |