summaryrefslogtreecommitdiff
path: root/src/ast.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast.h')
-rw-r--r--src/ast.h307
1 files changed, 169 insertions, 138 deletions
diff --git a/src/ast.h b/src/ast.h
index 1e9d177d..ebdf49d3 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -27,8 +27,6 @@
#include "binding-hash.h"
#include "common.h"
-#include "type-vector.h"
-#include "vector.h"
namespace wabt {
@@ -38,6 +36,11 @@ enum class VarType {
};
struct Var {
+ // Keep the default constructor trivial so it can be used as a union member.
+ Var() = default;
+ explicit Var(int64_t index);
+ explicit Var(const StringSlice& name);
+
Location loc;
VarType type;
union {
@@ -45,12 +48,24 @@ struct Var {
StringSlice name;
};
};
-WABT_DEFINE_VECTOR(var, Var);
+typedef std::vector<Var> VarVector;
typedef StringSlice Label;
-WABT_DEFINE_VECTOR(string_slice, StringSlice);
struct Const {
+ // Struct tags to differentiate constructors.
+ struct I32 {};
+ struct I64 {};
+ struct F32 {};
+ struct F64 {};
+
+ // Keep the default constructor trivial so it can be used as a union member.
+ Const() = default;
+ Const(I32, uint32_t);
+ Const(I64, uint64_t);
+ Const(F32, uint32_t);
+ Const(F64, uint64_t);
+
Location loc;
Type type;
union {
@@ -60,7 +75,7 @@ struct Const {
uint64_t f64_bits;
};
};
-WABT_DEFINE_VECTOR(const, Const);
+typedef std::vector<Const> ConstVector;
enum class ExprType {
Binary,
@@ -95,25 +110,63 @@ enum class ExprType {
typedef TypeVector BlockSignature;
struct Block {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Block);
+ Block();
+ explicit Block(struct Expr* first);
+ ~Block();
+
Label label;
BlockSignature sig;
struct Expr* first;
};
struct Expr {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Expr);
+ Expr();
+ explicit Expr(ExprType);
+ ~Expr();
+
+ static Expr* CreateBinary(Opcode);
+ static Expr* CreateBlock(Block*);
+ static Expr* CreateBr(Var);
+ static Expr* CreateBrIf(Var);
+ static Expr* CreateBrTable(VarVector* targets, Var default_target);
+ static Expr* CreateCall(Var);
+ static Expr* CreateCallIndirect(Var);
+ static Expr* CreateCompare(Opcode);
+ static Expr* CreateConst(const Const&);
+ static Expr* CreateConvert(Opcode);
+ static Expr* CreateCurrentMemory();
+ static Expr* CreateDrop();
+ static Expr* CreateGetGlobal(Var);
+ static Expr* CreateGetLocal(Var);
+ static Expr* CreateGrowMemory();
+ static Expr* CreateIf(struct Block* true_, struct Expr* false_ = nullptr);
+ static Expr* CreateLoad(Opcode, uint32_t align, uint64_t offset);
+ static Expr* CreateLoop(struct Block*);
+ static Expr* CreateNop();
+ static Expr* CreateReturn();
+ static Expr* CreateSelect();
+ static Expr* CreateSetGlobal(Var);
+ static Expr* CreateSetLocal(Var);
+ static Expr* CreateStore(Opcode, uint32_t align, uint64_t offset);
+ static Expr* CreateTeeLocal(Var);
+ static Expr* CreateUnary(Opcode);
+ static Expr* CreateUnreachable();
+
Location loc;
ExprType type;
Expr* next;
union {
struct { Opcode opcode; } binary, compare, convert, unary;
- Block block, loop;
+ struct Block *block, *loop;
struct { Var var; } br, br_if;
- struct { VarVector targets; Var default_target; } br_table;
+ struct { VarVector* targets; Var default_target; } br_table;
struct { Var var; } call, call_indirect;
- Const const_;
+ struct Const const_;
struct { Var var; } get_global, set_global;
struct { Var var; } get_local, set_local, tee_local;
- struct { Block true_; struct Expr* false_; } if_;
+ struct { struct Block* true_; struct Expr* false_; } if_;
struct { Opcode opcode; uint32_t align; uint64_t offset; } load, store;
};
};
@@ -124,21 +177,20 @@ struct FuncSignature {
};
struct FuncType {
+ WABT_DISALLOW_COPY_AND_ASSIGN(FuncType);
+ FuncType();
+ ~FuncType();
+
StringSlice name;
FuncSignature sig;
};
-typedef FuncType* FuncTypePtr;
-WABT_DEFINE_VECTOR(func_type_ptr, FuncTypePtr);
-
-enum {
- WABT_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE = 1,
- /* set if the signature is owned by module */
- WABT_FUNC_DECLARATION_FLAG_SHARED_SIGNATURE = 2,
-};
-typedef uint32_t FuncDeclarationFlags;
struct FuncDeclaration {
- FuncDeclarationFlags flags;
+ WABT_DISALLOW_COPY_AND_ASSIGN(FuncDeclaration);
+ FuncDeclaration();
+ ~FuncDeclaration();
+
+ bool has_func_type;
Var type_var;
FuncSignature sig;
};
@@ -155,50 +207,62 @@ struct Func {
BindingHash local_bindings;
Expr* first_expr;
};
-typedef Func* FuncPtr;
-WABT_DEFINE_VECTOR(func_ptr, FuncPtr);
struct Global {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Global);
+ Global();
+ ~Global();
+
StringSlice name;
Type type;
bool mutable_;
Expr* init_expr;
};
-typedef Global* GlobalPtr;
-WABT_DEFINE_VECTOR(global_ptr, GlobalPtr);
struct Table {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Table);
+ Table();
+ ~Table();
+
StringSlice name;
Limits elem_limits;
};
-typedef Table* TablePtr;
-WABT_DEFINE_VECTOR(table_ptr, TablePtr);
struct ElemSegment {
+ WABT_DISALLOW_COPY_AND_ASSIGN(ElemSegment);
+ ElemSegment();
+ ~ElemSegment();
+
Var table_var;
Expr* offset;
VarVector vars;
};
-typedef ElemSegment* ElemSegmentPtr;
-WABT_DEFINE_VECTOR(elem_segment_ptr, ElemSegmentPtr);
struct Memory {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Memory);
+ Memory();
+ ~Memory();
+
StringSlice name;
Limits page_limits;
};
-typedef Memory* MemoryPtr;
-WABT_DEFINE_VECTOR(memory_ptr, MemoryPtr);
struct DataSegment {
+ WABT_DISALLOW_COPY_AND_ASSIGN(DataSegment);
+ DataSegment();
+ ~DataSegment();
+
Var memory_var;
Expr* offset;
char* data;
size_t size;
};
-typedef DataSegment* DataSegmentPtr;
-WABT_DEFINE_VECTOR(data_segment_ptr, DataSegmentPtr);
struct Import {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Import);
+ Import();
+ ~Import();
+
StringSlice module_name;
StringSlice field_name;
ExternalKind kind;
@@ -207,21 +271,21 @@ struct Import {
* included in the Module's vector of funcs; but only the
* FuncDeclaration will have any useful information */
Func* func;
- Table table;
- Memory memory;
- Global global;
+ Table* table;
+ Memory* memory;
+ Global* global;
};
};
-typedef Import* ImportPtr;
-WABT_DEFINE_VECTOR(import_ptr, ImportPtr);
struct Export {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Export);
+ Export();
+ ~Export();
+
StringSlice name;
ExternalKind kind;
Var var;
};
-typedef Export* ExportPtr;
-WABT_DEFINE_VECTOR(export_ptr, ExportPtr);
enum class ModuleFieldType {
Func,
@@ -237,19 +301,23 @@ enum class ModuleFieldType {
};
struct ModuleField {
+ WABT_DISALLOW_COPY_AND_ASSIGN(ModuleField);
+ ModuleField();
+ ~ModuleField();
+
Location loc;
ModuleFieldType type;
struct ModuleField* next;
union {
Func* func;
- Global global;
+ Global* global;
Import* import;
- Export export_;
- FuncType func_type;
- Table table;
- ElemSegment elem_segment;
- Memory memory;
- DataSegment data_segment;
+ Export* export_;
+ FuncType* func_type;
+ Table* table;
+ ElemSegment* elem_segment;
+ Memory* memory;
+ DataSegment* data_segment;
Var start;
};
};
@@ -271,15 +339,15 @@ struct Module {
/* cached for convenience; the pointers are shared with values that are
* stored in either ModuleField or Import. */
- FuncPtrVector funcs;
- GlobalPtrVector globals;
- ImportPtrVector imports;
- ExportPtrVector exports;
- FuncTypePtrVector func_types;
- TablePtrVector tables;
- ElemSegmentPtrVector elem_segments;
- MemoryPtrVector memories;
- DataSegmentPtrVector data_segments;
+ std::vector<Func*> funcs;
+ std::vector<Global*> globals;
+ std::vector<Import*> imports;
+ std::vector<Export*> exports;
+ std::vector<FuncType*> func_types;
+ std::vector<Table*> tables;
+ std::vector<ElemSegment*> elem_segments;
+ std::vector<Memory*> memories;
+ std::vector<DataSegment*> data_segments;
Var* start;
BindingHash func_bindings;
@@ -301,6 +369,10 @@ enum class RawModuleType {
* when parsing text, as assert_invalid always assumes that text parsing
* succeeds. */
struct RawModule {
+ WABT_DISALLOW_COPY_AND_ASSIGN(RawModule);
+ RawModule();
+ ~RawModule();
+
RawModuleType type;
union {
Module* text;
@@ -319,21 +391,24 @@ enum class ActionType {
};
struct ActionInvoke {
- StringSlice name;
- ConstVector args;
-};
+ WABT_DISALLOW_COPY_AND_ASSIGN(ActionInvoke);
+ ActionInvoke();
-struct ActionGet {
- StringSlice name;
+ ConstVector args;
};
struct Action {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Action);
+ Action();
+ ~Action();
+
Location loc;
ActionType type;
Var module_var;
+ StringSlice name;
union {
- ActionInvoke invoke;
- ActionGet get;
+ ActionInvoke* invoke;
+ struct {} get;
};
};
@@ -359,29 +434,32 @@ enum class CommandType {
static const int kCommandTypeCount = WABT_ENUM_COUNT(CommandType);
struct Command {
+ WABT_DISALLOW_COPY_AND_ASSIGN(Command);
+ Command();
+ ~Command();
+
CommandType type;
union {
Module* module;
- Action action;
+ Action* action;
struct { StringSlice module_name; Var var; } register_;
- struct { Action action; ConstVector expected; } assert_return;
- struct { Action action; } assert_return_nan;
- struct { Action action; StringSlice text; } assert_trap;
+ struct { Action* action; ConstVector* expected; } assert_return;
+ struct { Action* action; } assert_return_nan;
+ struct { Action* action; StringSlice text; } assert_trap;
struct {
- RawModule module;
+ RawModule* module;
StringSlice text;
} assert_malformed, assert_invalid, assert_unlinkable,
assert_uninstantiable;
};
};
-WABT_DEFINE_VECTOR(command, Command);
+typedef std::vector<std::unique_ptr<Command>> CommandPtrVector;
struct Script {
WABT_DISALLOW_COPY_AND_ASSIGN(Script);
Script();
- ~Script();
- CommandVector commands;
+ CommandPtrVector commands;
BindingHash module_bindings;
};
@@ -424,54 +502,9 @@ ModuleField* append_module_field(Module*);
/* ownership of the function signature is passed to the module */
FuncType* append_implicit_func_type(Location*, Module*, FuncSignature*);
-/* Expr creation functions */
-Expr* new_binary_expr(void);
-Expr* new_block_expr(void);
-Expr* new_br_expr(void);
-Expr* new_br_if_expr(void);
-Expr* new_br_table_expr(void);
-Expr* new_call_expr(void);
-Expr* new_call_indirect_expr(void);
-Expr* new_compare_expr(void);
-Expr* new_const_expr(void);
-Expr* new_convert_expr(void);
-Expr* new_current_memory_expr(void);
-Expr* new_drop_expr(void);
-Expr* new_get_global_expr(void);
-Expr* new_get_local_expr(void);
-Expr* new_grow_memory_expr(void);
-Expr* new_if_expr(void);
-Expr* new_load_expr(void);
-Expr* new_loop_expr(void);
-Expr* new_nop_expr(void);
-Expr* new_return_expr(void);
-Expr* new_select_expr(void);
-Expr* new_set_global_expr(void);
-Expr* new_set_local_expr(void);
-Expr* new_store_expr(void);
-Expr* new_tee_local_expr(void);
-Expr* new_unary_expr(void);
-Expr* new_unreachable_expr(void);
-
/* destruction functions. not needed unless you're creating your own AST
elements */
-void destroy_action(struct Action*);
-void destroy_block(struct Block*);
-void destroy_command_vector_and_elements(CommandVector*);
-void destroy_command(Command*);
-void destroy_data_segment(DataSegment*);
-void destroy_elem_segment(ElemSegment*);
-void destroy_export(Export*);
-void destroy_expr(Expr*);
void destroy_expr_list(Expr*);
-void destroy_func_declaration(FuncDeclaration*);
-void destroy_func_signature(FuncSignature*);
-void destroy_func_type(FuncType*);
-void destroy_import(Import*);
-void destroy_memory(Memory*);
-void destroy_raw_module(RawModule*);
-void destroy_table(Table*);
-void destroy_var_vector_and_elements(VarVector*);
void destroy_var(Var*);
/* traversal functions */
@@ -492,13 +525,13 @@ int get_import_index_by_var(const Module* module, const Var* var);
int get_local_index_by_var(const Func* func, const Var* var);
int get_module_index_by_var(const Script* script, const Var* var);
-FuncPtr get_func_by_var(const Module* module, const Var* var);
-GlobalPtr get_global_by_var(const Module* func, const Var* var);
-FuncTypePtr get_func_type_by_var(const Module* module, const Var* var);
-TablePtr get_table_by_var(const Module* module, const Var* var);
-MemoryPtr get_memory_by_var(const Module* module, const Var* var);
-ImportPtr get_import_by_var(const Module* module, const Var* var);
-ExportPtr get_export_by_name(const Module* module, const StringSlice* name);
+Func* get_func_by_var(const Module* module, const Var* var);
+Global* get_global_by_var(const Module* func, const Var* var);
+FuncType* get_func_type_by_var(const Module* module, const Var* var);
+Table* get_table_by_var(const Module* module, const Var* var);
+Memory* get_memory_by_var(const Module* module, const Var* var);
+Import* get_import_by_var(const Module* module, const Var* var);
+Export* get_export_by_name(const Module* module, const StringSlice* name);
Module* get_first_module(const Script* script);
Module* get_module_by_var(const Script* script, const Var* var);
@@ -508,27 +541,25 @@ void make_type_binding_reverse_mapping(
std::vector<std::string>* out_reverse_mapping);
static WABT_INLINE bool decl_has_func_type(const FuncDeclaration* decl) {
- return static_cast<bool>(
- (decl->flags & WABT_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE) != 0);
+ return decl->has_func_type;
}
static WABT_INLINE bool signatures_are_equal(const FuncSignature* sig1,
const FuncSignature* sig2) {
- return static_cast<bool>(
- type_vectors_are_equal(&sig1->param_types, &sig2->param_types) &&
- type_vectors_are_equal(&sig1->result_types, &sig2->result_types));
+ return sig1->param_types == sig2->param_types &&
+ sig1->result_types == sig2->result_types;
}
static WABT_INLINE size_t get_num_params(const Func* func) {
- return func->decl.sig.param_types.size;
+ return func->decl.sig.param_types.size();
}
static WABT_INLINE size_t get_num_results(const Func* func) {
- return func->decl.sig.result_types.size;
+ return func->decl.sig.result_types.size();
}
static WABT_INLINE size_t get_num_locals(const Func* func) {
- return func->local_types.size;
+ return func->local_types.size();
}
static WABT_INLINE size_t get_num_params_and_locals(const Func* func) {
@@ -536,36 +567,36 @@ static WABT_INLINE size_t get_num_params_and_locals(const Func* func) {
}
static WABT_INLINE Type get_param_type(const Func* func, int index) {
- assert(static_cast<size_t>(index) < func->decl.sig.param_types.size);
- return func->decl.sig.param_types.data[index];
+ assert(static_cast<size_t>(index) < func->decl.sig.param_types.size());
+ return func->decl.sig.param_types[index];
}
static WABT_INLINE Type get_local_type(const Func* func, int index) {
assert(static_cast<size_t>(index) < get_num_locals(func));
- return func->local_types.data[index];
+ return func->local_types[index];
}
static WABT_INLINE Type get_result_type(const Func* func, int index) {
- assert(static_cast<size_t>(index) < func->decl.sig.result_types.size);
- return func->decl.sig.result_types.data[index];
+ assert(static_cast<size_t>(index) < func->decl.sig.result_types.size());
+ return func->decl.sig.result_types[index];
}
static WABT_INLINE Type get_func_type_param_type(const FuncType* func_type,
int index) {
- return func_type->sig.param_types.data[index];
+ return func_type->sig.param_types[index];
}
static WABT_INLINE size_t get_func_type_num_params(const FuncType* func_type) {
- return func_type->sig.param_types.size;
+ return func_type->sig.param_types.size();
}
static WABT_INLINE Type get_func_type_result_type(const FuncType* func_type,
int index) {
- return func_type->sig.result_types.data[index];
+ return func_type->sig.result_types[index];
}
static WABT_INLINE size_t get_func_type_num_results(const FuncType* func_type) {
- return func_type->sig.result_types.size;
+ return func_type->sig.result_types.size();
}
static WABT_INLINE const Location* get_raw_module_location(