/src/hermes/include/hermes/BCGen/HBC/SimpleBytecodeBuilder.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #ifndef HERMES_BCGEN_HBC_SIMPLEBYTECODEBUILDER_H |
9 | | #define HERMES_BCGEN_HBC_SIMPLEBYTECODEBUILDER_H |
10 | | |
11 | | #include "hermes/BCGen/HBC/BytecodeFileFormat.h" |
12 | | #include "hermes/BCGen/HBC/BytecodeInstructionGenerator.h" |
13 | | #include "hermes/Public/Buffer.h" |
14 | | |
15 | | #include <memory> |
16 | | #include <vector> |
17 | | |
18 | | namespace hermes { |
19 | | namespace hbc { |
20 | | /// A builder that is able to generate bytecode for very simple functions. |
21 | | /// This can be used to generate a few special functions needed by the Runtime. |
22 | | class SimpleBytecodeBuilder { |
23 | | /// A wrapper for a few crutial fields needed to represent a function. |
24 | | /// We can add more fields to it if things get more complicated, but let's |
25 | | /// try to keep it as simple as we can. |
26 | | struct SimpleFunction { |
27 | | /// Offset of the bytecode. This will be populated later during buffer |
28 | | /// generation. |
29 | | uint32_t offset{}; |
30 | | /// Number of parameters including "this". |
31 | | uint32_t paramCount; |
32 | | /// Size of the frame. |
33 | | uint32_t frameSize; |
34 | | /// The opcodes. |
35 | | std::vector<opcode_atom_t> opcodes; |
36 | | |
37 | | SimpleFunction( |
38 | | uint32_t paramCount, |
39 | | uint32_t frameSize, |
40 | | std::vector<opcode_atom_t> &&opcodes) |
41 | | : paramCount(paramCount), |
42 | | frameSize(frameSize), |
43 | 198 | opcodes(std::move(opcodes)) { |
44 | 198 | assert(paramCount > 0 && "paramCount must include 'this'"); |
45 | 198 | } |
46 | | }; |
47 | | |
48 | | /// List of functions in the builder to be concatenated into the final |
49 | | /// bytecode buffer. |
50 | | std::vector<SimpleFunction> functions_{}; |
51 | | |
52 | | public: |
53 | | /// Add a function to the builder. We only need the \p frameSize and |
54 | | /// \p opcodes. |
55 | | void addFunction( |
56 | | uint32_t paramCount, |
57 | | uint32_t frameSize, |
58 | 198 | std::vector<opcode_atom_t> &&opcodes) { |
59 | 198 | functions_.emplace_back(paramCount, frameSize, std::move(opcodes)); |
60 | 198 | } |
61 | | |
62 | | /// Generate the bytecode buffer given the list of functions in the builder. |
63 | | std::unique_ptr<Buffer> generateBytecodeBuffer(); |
64 | | }; |
65 | | } // namespace hbc |
66 | | } // namespace hermes |
67 | | |
68 | | #endif // HERMES_BCGEN_HBC_SIMPLEBYTECODEBUILDER_H |