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
8#include <vector>
9
11
19
26
33
45
53
63
69
75
81
88
98template <class... Ts> struct overloaded_cfg_instruction : Ts... {
99 using Ts::operator()...;
100};
101template <class... Ts> overloaded_cfg_instruction(Ts...) -> overloaded_cfg_instruction<Ts...>;
102
103inline std::ostream& operator<<(std::ostream& os, const CFGInstruction& instruction)
104{
105 std::visit(
108 os << "InsertSimpleInstructionBlock " << arg.instruction_block_idx;
109 },
110 [&](JumpToNewBlock arg) { os << "JumpToNewBlock " << arg.target_program_block_instruction_block_idx; },
111 [&](JumpIfToNewBlock arg) {
112 os << "JumpIfToNewBlock " << arg.then_program_block_instruction_block_idx << " "
113 << arg.else_program_block_instruction_block_idx << " " << arg.condition_offset_index;
114 },
115 [&](JumpToBlock arg) { os << "JumpToBlock " << arg.target_block_idx; },
116 [&](JumpIfToBlock arg) {
117 os << "JumpIToBlock " << arg.target_then_block_idx << " " << arg.target_else_block_idx << " "
118 << arg.condition_offset_index;
119 },
120 [&](FinalizeWithReturn arg) {
121 os << "FinalizeWithReturn " << arg.return_options.return_size << " "
122 << arg.return_options.return_value_tag << " " << arg.return_options.return_value_offset_index;
123 },
124 [&](FinalizeWithRevert arg) {
125 os << "FinalizeWithRevert " << arg.revert_options.return_size << " "
126 << arg.revert_options.return_value_tag << " " << arg.revert_options.return_value_offset_index;
127 },
129 os << "SwitchToNonTerminatedBlock " << arg.non_terminated_block_idx;
130 },
131 [&](InsertInternalCall arg) {
132 os << "InsertInternalCall " << arg.target_program_block_instruction_block_idx;
133 },
134 },
136 return os;
137}
138
139// TODO(defkit) make a graph for jumps, loops, etc.
141 private:
146
150
154
159
165
171
176
180
184
189
195 static std::vector<ProgramBlock*> dfs_traverse(ProgramBlock* start_block, bool reverse = false);
196
198 std::vector<ProgramBlock*> get_non_terminated_blocks();
199
203 std::vector<ProgramBlock*> get_reachable_blocks(ProgramBlock* block);
204
205 public:
211
213 {
214 for (ProgramBlock* block : dfs_traverse(start_block)) {
215 delete block;
216 }
217 }
218
220
222 std::vector<uint8_t> build_bytecode(const ReturnOptions& return_options);
223};
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
ControlFlow(std::vector< InstructionBlock > &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...
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...
std::vector< InstructionBlock > * instruction_blocks
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)