| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 18:29:30 +02:00
										 |  |  | #include <AK/Format.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Register { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-06-07 20:58:36 -07:00
										 |  |  |     constexpr static u32 accumulator_index = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 19:29:28 +02:00
										 |  |  |     static constexpr Register accumulator() | 
					
						
							| 
									
										
										
										
											2021-06-07 20:58:36 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-07-12 19:29:28 +02:00
										 |  |  |         return Register(accumulator_index); | 
					
						
							| 
									
										
										
										
											2021-06-07 20:58:36 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 17:30:07 +02:00
										 |  |  |     constexpr static u32 saved_return_value_index = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static constexpr Register saved_return_value() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Register(saved_return_value_index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-26 11:57:42 +02:00
										 |  |  |     static constexpr u32 exception_index = 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static constexpr Register exception() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Register(exception_index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-26 14:42:30 +02:00
										 |  |  |     static constexpr Register this_value() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         constexpr u32 this_value_index = 3; | 
					
						
							|  |  |  |         return Register(this_value_index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-26 15:32:46 +02:00
										 |  |  |     static constexpr Register return_value() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         constexpr u32 return_value_index = 4; | 
					
						
							|  |  |  |         return Register(return_value_index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-03 20:47:33 +02:00
										 |  |  |     static constexpr Register saved_exception() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         constexpr u32 saved_exception_index = 5; | 
					
						
							|  |  |  |         return Register(saved_exception_index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static constexpr u32 reserved_register_count = 6; | 
					
						
							| 
									
										
										
										
											2023-09-26 14:42:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 19:29:28 +02:00
										 |  |  |     constexpr explicit Register(u32 index) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  |         : m_index(index) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 19:29:28 +02:00
										 |  |  |     constexpr bool operator==(Register reg) const { return m_index == reg.index(); } | 
					
						
							| 
									
										
										
										
											2022-10-22 20:20:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 19:29:28 +02:00
										 |  |  |     constexpr u32 index() const { return m_index; } | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-06-07 20:58:36 -07:00
										 |  |  |     u32 m_index; | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-03 18:29:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<JS::Bytecode::Register> : AK::Formatter<FormatString> { | 
					
						
							| 
									
										
										
										
											2021-11-16 01:15:21 +01:00
										 |  |  |     ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Register const& value) | 
					
						
							| 
									
										
										
										
											2021-06-03 18:29:30 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-07 20:58:36 -07:00
										 |  |  |         if (value.index() == JS::Bytecode::Register::accumulator_index) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |             return builder.put_string("acc"sv); | 
					
						
							|  |  |  |         return AK::Formatter<FormatString>::format(builder, "${}"sv, value.index()); | 
					
						
							| 
									
										
										
										
											2021-06-03 18:29:30 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |