mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes | Static Private Attributes | Friends
m::StackMachine Struct Reference

A stack machine that evaluates an expression. More...

#include <StackMachine.hpp>

Collaboration diagram for m::StackMachine:
[legend]

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 Schemaschema_in () const
 Returns the Schema of input Tuples.
 
const std::vector< const Type * > & schema_out () const
 Returns a sequence of Types defining the schema of output Tuples.
 
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 Types.
 
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 Types.
 
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 Tuples 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< Opcodeops
 the sequence of operations to perform
 
std::vector< Valuecontext_
 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
 
Valuevalues_ = 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, OpcodeSTR_TO_OPCODE
 

Friends

struct Interpreter
 
struct StackMachineBuilder
 

Detailed Description

A stack machine that evaluates an expression.

Definition at line 25 of file StackMachine.hpp.

Member Typedef Documentation

◆ index_t

using m::StackMachine::index_t = std::size_t

Definition at line 41 of file StackMachine.hpp.

Member Enumeration Documentation

◆ Opcode

enum class m::StackMachine::Opcode : uint8_t
strong
Enumerator
M_OPCODE 
Last 

Definition at line 32 of file StackMachine.hpp.

Constructor & Destructor Documentation

◆ StackMachine() [1/6]

m::StackMachine::StackMachine ( )
inline

Create a StackMachine that does not accept input.

Definition at line 74 of file StackMachine.hpp.

◆ StackMachine() [2/6]

m::StackMachine::StackMachine ( Schema  in_schema)
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.

◆ StackMachine() [3/6]

StackMachine::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.

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().

◆ StackMachine() [4/6]

StackMachine::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.

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().

◆ StackMachine() [5/6]

m::StackMachine::StackMachine ( const StackMachine )
delete

◆ StackMachine() [6/6]

m::StackMachine::StackMachine ( StackMachine &&  )
default

◆ ~StackMachine()

m::StackMachine::~StackMachine ( )
inline

Definition at line 93 of file StackMachine.hpp.

References null_bits_, and values_.

Member Function Documentation

◆ add()

std::size_t m::StackMachine::add ( Value  val)
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().

◆ add_and_emit_load()

std::size_t m::StackMachine::add_and_emit_load ( Value  val)
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()().

◆ dump() [1/2]

void StackMachine::dump ( ) const

Definition at line 1622 of file StackMachine.cpp.

References dump().

Referenced by dump().

◆ dump() [2/2]

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() [1/6]

void StackMachine::emit ( const ast::Expr expr,
const Schema schema,
std::size_t  tuple_id = 0 
)

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.

◆ emit() [2/6]

void StackMachine::emit ( const ast::Expr expr,
std::size_t  tuple_id = 0 
)

◆ emit() [3/6]

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.

◆ emit() [4/6]

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.

References emit(), and ops.

◆ emit() [5/6]

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.

References emit(), and ops.

◆ emit() [6/6]

void m::StackMachine::emit ( Opcode  opc)
inline

Append the given opcode to the opcode sequence.

Definition at line 176 of file StackMachine.hpp.

References ops.

◆ emit_Cast()

void StackMachine::emit_Cast ( const Type to_ty,
const Type from_ty 
)

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().

◆ emit_Ld()

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().

◆ emit_Print()

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 Types.

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().

◆ emit_St()

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().

◆ emit_St_Tup()

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 Types.

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().

◆ num_ops()

std::size_t m::StackMachine::num_ops ( ) const
inline

Definition at line 104 of file StackMachine.hpp.

References ops.

◆ operator()()

void StackMachine::operator() ( Tuple **  tuples) const

Evaluate this StackMachine given the Tuples referenced by tuples.

By convention, the output Tuples should be given before the input Tuples. 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_.

◆ required_stack_size()

std::size_t m::StackMachine::required_stack_size ( ) const
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()().

◆ schema_in()

const Schema & m::StackMachine::schema_in ( ) const
inline

Returns the Schema of input Tuples.

Definition at line 99 of file StackMachine.hpp.

References in_schema.

◆ schema_out()

const std::vector< const Type * > & m::StackMachine::schema_out ( ) const
inline

Returns a sequence of Types defining the schema of output Tuples.

Definition at line 102 of file StackMachine.hpp.

References out_schema.

◆ set()

void m::StackMachine::set ( std::size_t  idx,
Value  val 
)
inline

Sets the Value in the context at index idx to val.

Definition at line 201 of file StackMachine.hpp.

References context_, and M_insist.

◆ str_to_opcode()

static Opcode m::StackMachine::str_to_opcode ( const std::string &  str)
inlinestatic

Definition at line 52 of file StackMachine.hpp.

References STR_TO_OPCODE.

Friends And Related Function Documentation

◆ Interpreter

friend struct Interpreter
friend

Definition at line 27 of file StackMachine.hpp.

◆ StackMachineBuilder

friend struct StackMachineBuilder
friend

Definition at line 28 of file StackMachine.hpp.

Referenced by emit().

Field Documentation

◆ context_

std::vector<Value> m::StackMachine::context_
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().

◆ current_stack_size_

int64_t m::StackMachine::current_stack_size_ = 0
private

the "current" stack size; i.e. after the last operation is executed

Definition at line 63 of file StackMachine.hpp.

◆ in_schema

Schema m::StackMachine::in_schema
private

schema of the input tuple

Definition at line 56 of file StackMachine.hpp.

Referenced by dump(), emit(), and schema_in().

◆ memory_

uint8_t m::StackMachine::memory_[SIZE_OF_MEMORY]
mutableprivate

memory usable by the stack machine, e.g. to work on BLOBs

Definition at line 70 of file StackMachine.hpp.

Referenced by operator()().

◆ null_bits_

bool* m::StackMachine::null_bits_ = nullptr
mutableprivate

array of NULL bits used as a stack

Definition at line 67 of file StackMachine.hpp.

Referenced by dump(), operator()(), and ~StackMachine().

◆ op_

decltype(ops) ::const_iterator m::StackMachine::op_
mutableprivate

the next operation to execute

Definition at line 68 of file StackMachine.hpp.

Referenced by dump(), and operator()().

◆ OPCODE_TO_STR

constexpr const char* m::StackMachine::OPCODE_TO_STR[]
staticconstexprprivate
Initial value:
= {
#define M_OPCODE(CODE, ...)
}

Definition at line 44 of file StackMachine.hpp.

Referenced by dump().

◆ ops

std::vector<Opcode> m::StackMachine::ops
private

the sequence of operations to perform

Definition at line 60 of file StackMachine.hpp.

Referenced by dump(), emit(), num_ops(), and operator()().

◆ out_schema

std::vector<const Type*> m::StackMachine::out_schema
private

schema of the output tuple

Definition at line 57 of file StackMachine.hpp.

Referenced by dump(), and schema_out().

◆ required_stack_size_

int64_t m::StackMachine::required_stack_size_ = 0
private

the required size of the stack

Definition at line 62 of file StackMachine.hpp.

Referenced by required_stack_size().

◆ SIZE_OF_MEMORY

constexpr std::size_t m::StackMachine::SIZE_OF_MEMORY = 4 * 1024
staticconstexpr

Definition at line 30 of file StackMachine.hpp.

◆ STR_TO_OPCODE

const std::unordered_map< std::string, StackMachine::Opcode > StackMachine::STR_TO_OPCODE
staticprivate
Initial value:
= {
#define M_OPCODE(CODE, ...)
}

Definition at line 49 of file StackMachine.hpp.

Referenced by emit_Print(), emit_St_Tup(), m::StackMachineBuilder::operator()(), and str_to_opcode().

◆ top_

std::size_t m::StackMachine::top_ = 0
mutableprivate

the top of the stack

Definition at line 69 of file StackMachine.hpp.

Referenced by dump(), and operator()().

◆ values_

Value* m::StackMachine::values_ = nullptr
mutableprivate

array of values used as a stack

Definition at line 66 of file StackMachine.hpp.

Referenced by dump(), operator()(), and ~StackMachine().


The documentation for this struct was generated from the following files: