![]() |
mutable
A Database System for Research and Fast Prototyping
|
A stack machine that evaluates an expression. More...
#include <StackMachine.hpp>
Public Types | |
enum class | Opcode : uint8_t { M_OPCODE , Last } |
using | index_t = std::size_t |
Public Member Functions | |
StackMachine () | |
Create a StackMachine that does not accept input. | |
StackMachine (Schema in_schema) | |
Create a StackMachine with the given input Schema in_schema . | |
StackMachine (Schema in_schema, const ast::Expr &expr) | |
Create a StackMachine with the given input Schema in_schema , compile the Expr expr , and emit the result to the output Tuple at index 0 . | |
StackMachine (Schema in_schema, const cnf::CNF &cnf) | |
Create a StackMachine with the given input Schema in_schema , compile the cnf::CNF cnf , and emit the result to the output Tuple at index 0 . | |
StackMachine (const StackMachine &)=delete | |
StackMachine (StackMachine &&)=default | |
~StackMachine () | |
const Schema & | schema_in () const |
Returns the Schema of input Tuple s. | |
const std::vector< const Type * > & | schema_out () const |
Returns a sequence of Type s defining the schema of output Tuple s. | |
std::size_t | num_ops () const |
std::size_t | required_stack_size () const |
Returns the required size of the stack to evaluate the opcode sequence. | |
void | emit (const ast::Expr &expr, std::size_t tuple_id=0) |
Emit operations evaluating the Expr expr . | |
void | emit (const ast::Expr &expr, const Schema &schema, std::size_t tuple_id=0) |
Emit operations evaluating the Expr expr . | |
void | emit (const ast::Expr &expr, std::vector< Schema > &schemas, std::vector< std::size_t > &tuple_ids) |
Emit operations evaluating the Expr expr . | |
void | emit (const cnf::CNF &cnf, std::size_t tuple_id=0) |
Emit operations evaluating the CNF formula cnf . | |
void | emit (const cnf::CNF &cnf, std::vector< Schema > &schemas, std::vector< std::size_t > &tuple_ids) |
Emit operations evaluating the Expr expr . | |
void | emit (Opcode opc) |
Append the given opcode to the opcode sequence. | |
void | emit_Ld (const Type *ty) |
Emit a Ld_X instruction based on Type ty , e.g. Ld_i32 for 4 byte integral types. | |
void | emit_St (const Type *ty) |
Emit a St_X instruction based on Type ty , e.g. St_i32 for 4 byte integral types. | |
void | emit_St_Tup (std::size_t tuple_id, std::size_t index, const Type *ty) |
Emit a St_Tup_X instruction based on Type ty , e.g. St_Tup_i for integral Type s. | |
void | emit_Print (std::size_t ostream_index, const Type *ty) |
Emit a Print_X instruction based on Type ty , e.g. Print_i for integral Type s. | |
void | emit_Cast (const Type *to_ty, const Type *from_ty) |
Emit opcodes to convert a value of Type from_ty to Type to_ty . | |
std::size_t | add (Value val) |
Appends the Value val to the context and returns its assigned index. | |
void | set (std::size_t idx, Value val) |
Sets the Value in the context at index idx to val . | |
std::size_t | add_and_emit_load (Value val) |
Adds the Value val to the context and emits a load instruction to load this value to the top of the stack. | |
void | operator() (Tuple **tuples) const |
Evaluate this StackMachine given the Tuple s referenced by tuples . | |
void | dump (std::ostream &out) const |
void | dump () const |
Static Public Member Functions | |
static Opcode | str_to_opcode (const std::string &str) |
Static Public Attributes | |
static constexpr std::size_t | SIZE_OF_MEMORY = 4 * 1024 |
Private Attributes | |
Schema | in_schema |
schema of the input tuple | |
std::vector< const Type * > | out_schema |
schema of the output tuple | |
std::vector< Opcode > | ops |
the sequence of operations to perform | |
std::vector< Value > | context_ |
the context of the stack machine, e.g. constants or global variables | |
int64_t | required_stack_size_ = 0 |
the required size of the stack | |
int64_t | current_stack_size_ = 0 |
the "current" stack size; i.e. after the last operation is executed | |
Value * | values_ = nullptr |
array of values used as a stack | |
bool * | null_bits_ = nullptr |
array of NULL bits used as a stack | |
decltype(ops) ::const_iterator | op_ |
the next operation to execute | |
std::size_t | top_ = 0 |
the top of the stack | |
uint8_t | memory_ [SIZE_OF_MEMORY] |
memory usable by the stack machine, e.g. to work on BLOBs | |
Static Private Attributes | |
static constexpr const char * | OPCODE_TO_STR [] |
static const std::unordered_map< std::string, Opcode > | STR_TO_OPCODE |
Friends | |
struct | Interpreter |
struct | StackMachineBuilder |
A stack machine that evaluates an expression.
Definition at line 25 of file StackMachine.hpp.
using m::StackMachine::index_t = std::size_t |
Definition at line 41 of file StackMachine.hpp.
|
strong |
Enumerator | |
---|---|
M_OPCODE | |
Last |
Definition at line 32 of file StackMachine.hpp.
|
inline |
Create a StackMachine
that does not accept input.
Definition at line 74 of file StackMachine.hpp.
|
inlineexplicit |
Create a StackMachine
with the given input Schema
in_schema
.
This is the c'tor used when constructing the opcode sequence from the outside.
Definition at line 78 of file StackMachine.hpp.
Create a StackMachine
with the given input Schema
in_schema
, compile the Expr
expr
, and emit the result to the output Tuple
at index 0
.
This is a convenience c'tor to construct a StackMachine
that evaluates exactly one expression.
Definition at line 533 of file StackMachine.cpp.
References emit().
Create a StackMachine
with the given input Schema
in_schema
, compile the cnf::CNF
cnf
, and emit the result to the output Tuple
at index 0
.
This is a convenience c'tor to construct a StackMachine
that evaluates exactly one CNF formula.
Definition at line 540 of file StackMachine.cpp.
References emit().
|
delete |
|
default |
|
inline |
Definition at line 93 of file StackMachine.hpp.
References null_bits_, and values_.
|
inline |
Appends the Value
val
to the context and returns its assigned index.
Definition at line 194 of file StackMachine.hpp.
References context_.
Referenced by add_and_emit_load(), and compile_data_layout().
|
inline |
Adds the Value
val
to the context and emits a load
instruction to load this value to the top of the stack.
Definition at line 208 of file StackMachine.hpp.
References add().
Referenced by compile_data_layout(), emit_Cast(), emit_Ld(), emit_St(), emit_St_Tup(), and m::StackMachineBuilder::operator()().
void StackMachine::dump | ( | ) | const |
M_LCOV_EXCL_START void StackMachine::dump | ( | std::ostream & | out | ) | const |
Definition at line 1548 of file StackMachine.cpp.
References context_, in_schema, null_bits_, op_, OPCODE_TO_STR, ops, out_schema, top_, and values_.
Emit operations evaluating the Expr
expr
.
Definition at line 561 of file StackMachine.cpp.
References m::Schema::begin(), m::Schema::end(), m::Schema::find(), in_schema, M_insist, m::Schema::num_entries(), and StackMachineBuilder.
void StackMachine::emit | ( | const ast::Expr & | expr, |
std::size_t | tuple_id = 0 |
||
) |
Emit operations evaluating the Expr
expr
.
Definition at line 547 of file StackMachine.cpp.
References m::Schema::begin(), m::Schema::end(), m::Schema::find(), in_schema, M_insist, m::Schema::num_entries(), and StackMachineBuilder.
Referenced by emit(), emit_Print(), emit_St_Tup(), m::StackMachineBuilder::operator()(), and StackMachine().
void StackMachine::emit | ( | const ast::Expr & | expr, |
std::vector< Schema > & | schemas, | ||
std::vector< std::size_t > & | tuple_ids | ||
) |
Emit operations evaluating the Expr
expr
.
Definition at line 575 of file StackMachine.cpp.
References StackMachineBuilder.
void StackMachine::emit | ( | const cnf::CNF & | cnf, |
std::size_t | tuple_id = 0 |
||
) |
Emit operations evaluating the CNF
formula cnf
.
Definition at line 582 of file StackMachine.cpp.
void StackMachine::emit | ( | const cnf::CNF & | cnf, |
std::vector< Schema > & | schemas, | ||
std::vector< std::size_t > & | tuple_ids | ||
) |
Emit operations evaluating the Expr
expr
.
Definition at line 600 of file StackMachine.cpp.
|
inline |
Append the given opcode to the opcode sequence.
Definition at line 176 of file StackMachine.hpp.
References ops.
Emit opcodes to convert a value of Type
from_ty
to Type
to_ty
.
Definition at line 733 of file StackMachine.cpp.
References add_and_emit_load(), m::Type::is_character_sequence(), m::Type::is_numeric(), M_insist, M_unreachable, and m::powi().
void StackMachine::emit_Ld | ( | const Type * | ty | ) |
Emit a Ld_X
instruction based on Type
ty
, e.g. Ld_i32
for 4 byte integral types.
Definition at line 620 of file StackMachine.cpp.
References add_and_emit_load(), m::Type::is_boolean(), M_insist, and M_unreachable.
Referenced by compile_data_layout().
void StackMachine::emit_Print | ( | std::size_t | ostream_index, |
const Type * | ty | ||
) |
Emit a Print_X
instruction based on Type
ty
, e.g. Print_i
for integral Type
s.
Definition at line 713 of file StackMachine.cpp.
References emit(), m::Type::is_date(), m::Type::is_date_time(), m::Type::is_none(), STR_TO_OPCODE, and tystr().
void StackMachine::emit_St | ( | const Type * | ty | ) |
Emit a St_X
instruction based on Type
ty
, e.g. St_i32
for 4 byte integral types.
Definition at line 658 of file StackMachine.cpp.
References add_and_emit_load(), m::Type::is_boolean(), M_insist, and M_unreachable.
Referenced by compile_data_layout().
void StackMachine::emit_St_Tup | ( | std::size_t | tuple_id, |
std::size_t | index, | ||
const Type * | ty | ||
) |
Emit a St_Tup_X
instruction based on Type
ty
, e.g. St_Tup_i
for integral Type
s.
Definition at line 696 of file StackMachine.cpp.
References add_and_emit_load(), emit(), m::Type::is_none(), STR_TO_OPCODE, and tystr().
Referenced by compile_data_layout().
|
inline |
Definition at line 104 of file StackMachine.hpp.
References ops.
void StackMachine::operator() | ( | Tuple ** | tuples | ) | const |
Evaluate this StackMachine
given the Tuple
s referenced by tuples
.
By convention, the output Tuple
s should be given before the input Tuple
s. However, a Tuple
can be used for both input and output.
Definition at line 856 of file StackMachine.cpp.
References m::and, m::Value::as(), m::Value::as_b(), BINARY, CMP, context_, m::like(), LOAD, M_insist, memory_, NEXT, m::Tuple::null(), null_bits_, op_, ops, POP, PUSH, required_stack_size(), m::Tuple::set(), m::setbit(), STORE, m::streq(), TOP, top_, TOP_IS_NULL, UNARY, and values_.
|
inline |
Returns the required size of the stack to evaluate the opcode sequence.
Definition at line 107 of file StackMachine.hpp.
References required_stack_size_.
Referenced by operator()().
|
inline |
Returns the Schema
of input Tuple
s.
Definition at line 99 of file StackMachine.hpp.
References in_schema.
|
inline |
Returns a sequence of Type
s defining the schema of output Tuple
s.
Definition at line 102 of file StackMachine.hpp.
References out_schema.
|
inline |
Sets the Value
in the context at index idx
to val
.
Definition at line 201 of file StackMachine.hpp.
|
inlinestatic |
Definition at line 52 of file StackMachine.hpp.
References STR_TO_OPCODE.
|
friend |
Definition at line 27 of file StackMachine.hpp.
|
friend |
Definition at line 28 of file StackMachine.hpp.
Referenced by emit().
|
private |
the context of the stack machine, e.g. constants or global variables
Definition at line 61 of file StackMachine.hpp.
Referenced by add(), dump(), operator()(), and set().
|
private |
the "current" stack size; i.e. after the last operation is executed
Definition at line 63 of file StackMachine.hpp.
|
private |
schema of the input tuple
Definition at line 56 of file StackMachine.hpp.
Referenced by dump(), emit(), and schema_in().
|
mutableprivate |
memory usable by the stack machine, e.g. to work on BLOBs
Definition at line 70 of file StackMachine.hpp.
Referenced by operator()().
|
mutableprivate |
array of NULL bits used as a stack
Definition at line 67 of file StackMachine.hpp.
Referenced by dump(), operator()(), and ~StackMachine().
|
mutableprivate |
the next operation to execute
Definition at line 68 of file StackMachine.hpp.
Referenced by dump(), and operator()().
|
staticconstexprprivate |
Definition at line 44 of file StackMachine.hpp.
Referenced by dump().
|
private |
the sequence of operations to perform
Definition at line 60 of file StackMachine.hpp.
Referenced by dump(), emit(), num_ops(), and operator()().
|
private |
schema of the output tuple
Definition at line 57 of file StackMachine.hpp.
Referenced by dump(), and schema_out().
|
private |
the required size of the stack
Definition at line 62 of file StackMachine.hpp.
Referenced by required_stack_size().
|
staticconstexpr |
Definition at line 30 of file StackMachine.hpp.
|
staticprivate |
Definition at line 49 of file StackMachine.hpp.
Referenced by emit_Print(), emit_St_Tup(), m::StackMachineBuilder::operator()(), and str_to_opcode().
|
mutableprivate |
the top of the stack
Definition at line 69 of file StackMachine.hpp.
Referenced by dump(), and operator()().
|
mutableprivate |
array of values used as a stack
Definition at line 66 of file StackMachine.hpp.
Referenced by dump(), operator()(), and ~StackMachine().