Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
control_flow.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <vector>
8
16
23
30
42
50
60
66
72
78
85
95template <class... Ts> struct overloaded_cfg_instruction : Ts... {
96 using Ts::operator()...;
97};
98template <class... Ts> overloaded_cfg_instruction(Ts...) -> overloaded_cfg_instruction<Ts...>;
99
100inline std::ostream& operator<<(std::ostream& os, const CFGInstruction& instruction)
101{
102 std::visit(
105 os << "InsertSimpleInstructionBlock " << arg.instruction_block_idx;
106 },
107 [&](JumpToNewBlock arg) { os << "JumpToNewBlock " << arg.target_program_block_instruction_block_idx; },
108 [&](JumpIfToNewBlock arg) {
109 os << "JumpIfToNewBlock " << arg.then_program_block_instruction_block_idx << " "
110 << arg.else_program_block_instruction_block_idx << " " << arg.condition_offset_index;
111 },
112 [&](JumpToBlock arg) { os << "JumpToBlock " << arg.target_block_idx; },
113 [&](JumpIfToBlock arg) {
114 os << "JumpIToBlock " << arg.target_then_block_idx << " " << arg.target_else_block_idx << " "
115 << arg.condition_offset_index;
116 },
117 [&](FinalizeWithReturn arg) {
118 os << "FinalizeWithReturn " << arg.return_options.return_size << " "
119 << arg.return_options.return_value_tag << " " << arg.return_options.return_value_offset_index;
120 },
121 [&](FinalizeWithRevert arg) {
122 os << "FinalizeWithRevert " << arg.revert_options.return_size << " "
123 << arg.revert_options.return_value_tag << " " << arg.revert_options.return_value_offset_index;
124 },
126 os << "SwitchToNonTerminatedBlock " << arg.non_terminated_block_idx;
127 },
128 [&](InsertInternalCall arg) {
129 os << "InsertInternalCall " << arg.target_program_block_instruction_block_idx;
130 },
131 },
133 return os;
134}
135
136// TODO(defkit) make a graph for jumps, loops, etc.
138 private:
143
147
151
156
162
168
173
177
181
186
192 static std::vector<ProgramBlock*> dfs_traverse(ProgramBlock* start_block, bool reverse = false);
193
195 std::vector<ProgramBlock*> get_non_terminated_blocks();
196
200 std::vector<ProgramBlock*> get_reachable_blocks(ProgramBlock* block);
201
202 public:
208
210 {
211 for (ProgramBlock* block : dfs_traverse(start_block)) {
212 delete block;
213 }
214 }
215
217
219 std::vector<uint8_t> build_bytecode(const ReturnOptions& return_options);
220};
void process_jump_to_block(JumpToBlock instruction)
terminates the current block with a jump to the block, which does not create a loop in the graph (def...
ProgramBlock * current_block
std::vector< std::vector< FuzzInstruction > > * instruction_blocks
std::vector< ProgramBlock * > get_reachable_blocks(ProgramBlock *block)
get the list of blocks which are can be reached from the given block without creating a loop in the g...
ControlFlow(std::vector< std::vector< FuzzInstruction > > &instruction_blocks)
ProgramBlock * start_block
the entry block of the program
void process_cfg_instruction(CFGInstruction instruction)
void process_insert_simple_instruction_block(InsertSimpleInstructionBlock instruction)
add instructions to the current block from the instruction block at the given index taken modulo leng...
void process_finalize_with_revert(FinalizeWithRevert instruction)
terminates the current block with Revert and switches to the first non-terminated block
void process_insert_internal_call(InsertInternalCall instruction)
inserts INTERNALCALL instruction to the current block creates a new block and sets it as the current ...
void process_switch_to_non_terminated_block(SwitchToNonTerminatedBlock instruction)
switches to the non-terminated block with the chosen index
std::vector< ProgramBlock * > get_non_terminated_blocks()
get the list of non-terminated blocks
void process_finalize_with_return(FinalizeWithReturn instruction)
terminates the current block with Return and switches to the first non-terminated block
void process_jump_to_new_block(JumpToNewBlock instruction)
terminates the current block with a jump and creates a new block
std::vector< uint8_t > build_bytecode(const ReturnOptions &return_options)
build the bytecode, finalizing the current block with return
void process_jump_if_to_new_block(JumpIfToNewBlock instruction)
terminates the current block with a jump if and creates two new blocks, sets the first as the then bl...
void process_jump_if_to_block(JumpIfToBlock instruction)
terminates the current block with a jumpi and jump instructions to the blocks, which does not create ...
static std::vector< ProgramBlock * > dfs_traverse(ProgramBlock *start_block, bool reverse=false)
traverse the control flow graph using DFS
std::ostream & operator<<(std::ostream &os, const CFGInstruction &instruction)
std::variant< InsertSimpleInstructionBlock, JumpToNewBlock, JumpIfToNewBlock, JumpToBlock, JumpIfToBlock, FinalizeWithReturn, FinalizeWithRevert, SwitchToNonTerminatedBlock, InsertInternalCall > CFGInstruction
Instruction instruction
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
finalizes the current block with Return and switches to the first non-terminated block
MSGPACK_FIELDS(return_options)
ReturnOptions return_options
finalizes the current block with Revert and switches to the first non-terminated block
ReturnOptions revert_options
MSGPACK_FIELDS(revert_options)
inserts INTERNALCALL instruction to the current block creates a new block and sets it as the current ...
MSGPACK_FIELDS(target_program_block_instruction_block_idx)
uint16_t target_program_block_instruction_block_idx
insert instruction block to the current block
MSGPACK_FIELDS(instruction_block_idx)
finalizes the current block with a JumpI and Jump instructions to the block, which does not create a ...
uint16_t condition_offset_index
uint16_t target_then_block_idx
MSGPACK_FIELDS(target_then_block_idx, target_else_block_idx, condition_offset_index)
uint16_t target_else_block_idx
finalizes the current block with jump if, creates two new blocks, sets the first as the then block an...
uint16_t condition_offset_index
uint16_t else_program_block_instruction_block_idx
uint16_t then_program_block_instruction_block_idx
MSGPACK_FIELDS(then_program_block_instruction_block_idx, else_program_block_instruction_block_idx, condition_offset_index)
finalizes the current block with a jump to the block, which does not create a loop in the graph (defi...
MSGPACK_FIELDS(target_block_idx)
uint16_t target_block_idx
finalizes the current block with jump, creates a new block and sets it as the current block
MSGPACK_FIELDS(target_program_block_instruction_block_idx)
uint16_t target_program_block_instruction_block_idx
Wrapper for MemoryTag to allow for msgpack packing and unpacking.
uint8_t return_size
MemoryTagWrapper return_value_tag
MSGPACK_FIELDS(return_size, return_value_tag, return_value_offset_index)
uint16_t return_value_offset_index
switches to the non-terminated block with the chosen index
MSGPACK_FIELDS(non_terminated_block_idx)