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
79
88template <class... Ts> struct overloaded_cfg_instruction : Ts... {
89 using Ts::operator()...;
90};
91template <class... Ts> overloaded_cfg_instruction(Ts...) -> overloaded_cfg_instruction<Ts...>;
92
93inline std::ostream& operator<<(std::ostream& os, const CFGInstruction& instruction)
94{
95 std::visit(
98 os << "InsertSimpleInstructionBlock " << arg.instruction_block_idx;
99 },
100 [&](JumpToNewBlock arg) { os << "JumpToNewBlock " << arg.target_program_block_instruction_block_idx; },
101 [&](JumpIfToNewBlock arg) {
102 os << "JumpIfToNewBlock " << arg.then_program_block_instruction_block_idx << " "
103 << arg.else_program_block_instruction_block_idx << " " << arg.condition_offset_index;
104 },
105 [&](JumpToBlock arg) { os << "JumpToBlock " << arg.target_block_idx; },
106 [&](JumpIfToBlock arg) {
107 os << "JumpIToBlock " << arg.target_then_block_idx << " " << arg.target_else_block_idx << " "
108 << arg.condition_offset_index;
109 },
110 [&](FinalizeWithReturn arg) {
111 os << "FinalizeWithReturn " << arg.return_options.return_size << " "
112 << arg.return_options.return_value_tag << " " << arg.return_options.return_value_offset_index;
113 },
115 os << "SwitchToNonTerminatedBlock " << arg.non_terminated_block_idx;
116 },
117 [&](InsertInternalCall arg) {
118 os << "InsertInternalCall " << arg.target_program_block_instruction_block_idx;
119 },
120 },
122 return os;
123}
124
125// TODO(defkit) make a graph for jumps, loops, etc.
127 private:
132
136
140
145
151
157
162
166
171
177 static std::vector<ProgramBlock*> dfs_traverse(ProgramBlock* start_block, bool reverse = false);
178
180 std::vector<ProgramBlock*> get_non_terminated_blocks();
181
185 std::vector<ProgramBlock*> get_reachable_blocks(ProgramBlock* block);
186
187 public:
193
195 {
196 for (ProgramBlock* block : dfs_traverse(start_block)) {
197 delete block;
198 }
199 }
200
202
204 std::vector<uint8_t> build_bytecode(const ReturnOptions& return_options);
205};
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_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::variant< InsertSimpleInstructionBlock, JumpToNewBlock, JumpIfToNewBlock, JumpToBlock, JumpIfToBlock, FinalizeWithReturn, SwitchToNonTerminatedBlock, InsertInternalCall > CFGInstruction
std::ostream & operator<<(std::ostream &os, const CFGInstruction &instruction)
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
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)