| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | #include <AK/DeprecatedString.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | #include <LibJS/Bytecode/BasicBlock.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  | #include <LibJS/Bytecode/Op.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Bytecode { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | NonnullOwnPtr<BasicBlock> BasicBlock::create(DeprecatedString name, size_t size) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-12 20:36:39 +04:30
										 |  |  |     return adopt_own(*new BasicBlock(move(name), max(size, static_cast<size_t>(4 * KiB)))); | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | BasicBlock::BasicBlock(DeprecatedString name, size_t size) | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     : m_name(move(name)) | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: This is not the smartest solution ever. Find something cleverer!
 | 
					
						
							|  |  |  |     // The main issue we're working around here is that we don't want pointers into the bytecode stream to become invalidated
 | 
					
						
							|  |  |  |     // during code generation due to dynamic buffer resizing. Otherwise we could just use a Vector.
 | 
					
						
							| 
									
										
										
										
											2021-06-12 20:36:39 +04:30
										 |  |  |     m_buffer_capacity = size; | 
					
						
							| 
									
										
										
										
											2023-07-13 09:48:05 +02:00
										 |  |  |     m_buffer = new u8[m_buffer_capacity]; | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | BasicBlock::~BasicBlock() | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     Bytecode::InstructionStreamIterator it(instruction_stream()); | 
					
						
							|  |  |  |     while (!it.at_end()) { | 
					
						
							|  |  |  |         auto& to_destroy = (*it); | 
					
						
							|  |  |  |         ++it; | 
					
						
							|  |  |  |         Instruction::destroy(const_cast<Instruction&>(to_destroy)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 09:48:05 +02:00
										 |  |  |     delete[] m_buffer; | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | void BasicBlock::seal() | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: mprotect the instruction stream as PROT_READ
 | 
					
						
							|  |  |  |     // This is currently not possible because instructions can have destructors (that clean up strings)
 | 
					
						
							|  |  |  |     // Instructions should instead be destructor-less and refer to strings in a string table on the Bytecode::Block.
 | 
					
						
							|  |  |  |     // It also doesn't work because instructions that have String members use RefPtr internally which must be in writable memory.
 | 
					
						
							| 
									
										
										
										
											2021-06-07 15:24:33 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 10:02:01 +02:00
										 |  |  | void BasicBlock::dump(Bytecode::Executable const& executable) const | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     Bytecode::InstructionStreamIterator it(instruction_stream()); | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  |     if (!m_name.is_empty()) | 
					
						
							|  |  |  |         warnln("{}:", m_name); | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |     while (!it.at_end()) { | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |         warnln("[{:4x}] {}", it.offset(), (*it).to_deprecated_string(executable)); | 
					
						
							| 
									
										
										
										
											2021-06-07 15:12:43 +02:00
										 |  |  |         ++it; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 06:49:58 +04:30
										 |  |  | void BasicBlock::grow(size_t additional_size) | 
					
						
							| 
									
										
										
										
											2021-06-09 00:50:42 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_buffer_size += additional_size; | 
					
						
							|  |  |  |     VERIFY(m_buffer_size <= m_buffer_capacity); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:46:30 +02:00
										 |  |  | } |