mirror of
				https://github.com/LadybirdBrowser/ladybird.git
				synced 2025-10-31 21:30:58 +00:00 
			
		
		
		
	 69dddd4ef5
			
		
	
	
		69dddd4ef5
		
	
	
	
	
		
			
			This patch begins the work of implementing JavaScript execution in a bytecode VM instead of an AST tree-walk interpreter. It's probably quite naive, but we have to start somewhere. The basic idea is that you call Bytecode::Generator::generate() on an AST node and it hands you back a Bytecode::Block filled with instructions that can then be interpreted by a Bytecode::Interpreter. This first version only implements two instructions: Load and Add. :^) Each bytecode block has infinity registers, and the interpreter resizes its register file to fit the block being executed. Two new `js` options are added in this patch as well: `-d` will dump the generated bytecode `-b` will execute the generated bytecode Note that unless `-d` and/or `-b` are specified, none of the bytecode related stuff in LibJS runs at all. This is implemented in parallel with the existing AST interpreter. :^)
		
			
				
	
	
		
			42 lines
		
	
	
	
		
			1,006 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
	
		
			1,006 B
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibJS/Bytecode/Block.h>
 | |
| #include <LibJS/Bytecode/Instruction.h>
 | |
| #include <LibJS/Bytecode/Interpreter.h>
 | |
| 
 | |
| namespace JS::Bytecode {
 | |
| 
 | |
| Interpreter::Interpreter(GlobalObject& global_object)
 | |
|     : m_global_object(global_object)
 | |
| {
 | |
| }
 | |
| 
 | |
| Interpreter::~Interpreter()
 | |
| {
 | |
| }
 | |
| 
 | |
| void Interpreter::run(Bytecode::Block const& block)
 | |
| {
 | |
|     dbgln("Bytecode::Interpreter will run block {:p}", &block);
 | |
| 
 | |
|     m_registers.resize(block.register_count());
 | |
| 
 | |
|     for (auto& instruction : block.instructions())
 | |
|         instruction.execute(*this);
 | |
| 
 | |
|     dbgln("Bytecode::Interpreter did run block {:p}", &block);
 | |
|     for (size_t i = 0; i < m_registers.size(); ++i) {
 | |
|         String value_string;
 | |
|         if (m_registers[i].is_empty())
 | |
|             value_string = "(empty)";
 | |
|         else
 | |
|             value_string = m_registers[i].to_string_without_side_effects();
 | |
|         dbgln("[{:3}] {}", i, value_string);
 | |
|     }
 | |
| }
 | |
| 
 | |
| }
 |