Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
IR::FunctionValidationContext Struct Reference

Public Member Functions

 FunctionValidationContext (const Module &inModule, const FunctionDef &inFunctionDef)
 
Uptr getControlStackSize ()
 
void logOperator (const std::string &operatorDescription)
 
void unknown (Opcode opcode)
 
void block (ControlStructureImm imm)
 
void loop (ControlStructureImm imm)
 
void if_ (ControlStructureImm imm)
 
void else_ (NoImm imm)
 
void end (NoImm)
 
void return_ (NoImm)
 
void br (BranchImm imm)
 
void br_table (BranchTableImm imm)
 
void br_if (BranchImm imm)
 
void unreachable (NoImm)
 
void drop (NoImm)
 
void select (NoImm)
 
void get_local (GetOrSetVariableImm< false > imm)
 
void set_local (GetOrSetVariableImm< false > imm)
 
void tee_local (GetOrSetVariableImm< false > imm)
 
void get_global (GetOrSetVariableImm< true > imm)
 
void set_global (GetOrSetVariableImm< true > imm)
 
void call (CallImm imm)
 
void call_indirect (CallIndirectImm imm)
 
void validateImm (NoImm)
 
template<typename nativeType >
void validateImm (LiteralImm< nativeType > imm)
 
template<Uptr naturalAlignmentLog2>
void validateImm (LoadOrStoreImm< naturalAlignmentLog2 > imm)
 
void validateImm (MemoryImm)
 

Detailed Description

Definition at line 144 of file Validate.cpp.

Constructor & Destructor Documentation

◆ FunctionValidationContext()

IR::FunctionValidationContext::FunctionValidationContext ( const Module & inModule,
const FunctionDef & inFunctionDef )
inline

Definition at line 146 of file Validate.cpp.

147 : module(inModule), functionDef(inFunctionDef), functionType(inModule.types[inFunctionDef.type.index])
148 {
149 // Initialize the local types.
150 locals.reserve(functionType->parameters.size() + functionDef.nonParameterLocalTypes.size());
151 locals = functionType->parameters;
152 locals.insert(locals.end(),functionDef.nonParameterLocalTypes.begin(),functionDef.nonParameterLocalTypes.end());
153
154 // Push the function context onto the control stack.
155 pushControlStack(ControlContext::Type::function,functionType->ret,functionType->ret);
156 }
std::vector< ValueType > nonParameterLocalTypes
Definition Module.h:44
ResultType ret
Definition Types.h:195
std::vector< ValueType > parameters
Definition Types.h:196

Member Function Documentation

◆ block()

void IR::FunctionValidationContext::block ( ControlStructureImm imm)
inline

Definition at line 199 of file Validate.cpp.

200 {
201 validate(imm.resultType);
202 pushControlStack(ControlContext::Type::block,imm.resultType,imm.resultType);
203 }
void validate(ValueType valueType)
Definition Validate.cpp:22
Here is the call graph for this function:

◆ br()

void IR::FunctionValidationContext::br ( BranchImm imm)
inline

Definition at line 234 of file Validate.cpp.

235 {
236 popAndValidateResultType("br argument",getBranchTargetByDepth(imm.targetDepth).branchArgumentType);
237 enterUnreachable();
238 }

◆ br_if()

void IR::FunctionValidationContext::br_if ( BranchImm imm)
inline

Definition at line 255 of file Validate.cpp.

256 {
257 popAndValidateOperand("br_if condition",ValueType::i32);
258 popAndValidateResultType("br_if argument",getBranchTargetByDepth(imm.targetDepth).branchArgumentType);
259 pushOperand(getBranchTargetByDepth(imm.targetDepth).branchArgumentType);
260 }

◆ br_table()

void IR::FunctionValidationContext::br_table ( BranchTableImm imm)
inline

Definition at line 239 of file Validate.cpp.

240 {
241 popAndValidateOperand("br_table index",ValueType::i32);
242 const ResultType defaultTargetArgumentType = getBranchTargetByDepth(imm.defaultTargetDepth).branchArgumentType;
243 popAndValidateResultType("br_table argument",defaultTargetArgumentType);
244
245 WAVM_ASSERT_THROW(imm.branchTableIndex < functionDef.branchTables.size());
246 const std::vector<U32>& targetDepths = functionDef.branchTables[imm.branchTableIndex];
247 for(Uptr targetIndex = 0;targetIndex < targetDepths.size();++targetIndex)
248 {
249 const ResultType targetArgumentType = getBranchTargetByDepth(targetDepths[targetIndex]).branchArgumentType;
250 VALIDATE_UNLESS("br_table target argument must match default target argument: ",targetArgumentType != defaultTargetArgumentType);
251 }
252
253 enterUnreachable();
254 }
PointerIntHelper< sizeof(size_t)>::UnsignedIntType Uptr
Definition BasicTypes.h:22
#define WAVM_ASSERT_THROW(cond)
Definition Errors.h:29
#define VALIDATE_UNLESS(reason, comparison)
Definition Validate.cpp:14
ResultType
Definition Types.h:134
std::vector< std::vector< U32 > > branchTables
Definition Module.h:46

◆ call()

void IR::FunctionValidationContext::call ( CallImm imm)
inline

Definition at line 305 of file Validate.cpp.

306 {
307 const FunctionType* calleeType = validateFunctionIndex(module,imm.functionIndex);
308 popAndValidateOperands("call arguments",calleeType->parameters.data(),(Uptr)calleeType->parameters.size());
309 pushOperand(calleeType->ret);
310 }
const FunctionType * validateFunctionIndex(const Module &module, Uptr functionIndex)
Definition Validate.cpp:119
Here is the call graph for this function:

◆ call_indirect()

void IR::FunctionValidationContext::call_indirect ( CallIndirectImm imm)
inline

Definition at line 311 of file Validate.cpp.

312 {
313 VALIDATE_INDEX(imm.type.index,module.types.size());
314 VALIDATE_UNLESS("call_indirect is only valid if there is a default function table: ",module.tables.size()==0);
315 const FunctionType* calleeType = module.types[imm.type.index];
316 popAndValidateOperand("call_indirect function index",ValueType::i32);
317 popAndValidateOperands("call_indirect arguments",calleeType->parameters.data(),(Uptr)calleeType->parameters.size());
318 pushOperand(calleeType->ret);
319 }
#define VALIDATE_INDEX(index, arraySize)
Definition Validate.cpp:20
IndexSpace< TableDef, TableType > tables
Definition Module.h:134
std::vector< const FunctionType * > types
Definition Module.h:131

◆ drop()

void IR::FunctionValidationContext::drop ( NoImm )
inline

Definition at line 266 of file Validate.cpp.

267 {
268 popOperand();
269 }

◆ else_()

void IR::FunctionValidationContext::else_ ( NoImm imm)
inline

Definition at line 215 of file Validate.cpp.

216 {
217 if( controlStack.size() == 0 ) throw ValidationException( "control stack empty" );
218 popAndValidateResultType("if result",controlStack.back().resultType);
219 popControlStack(true);
220 }

◆ end()

void IR::FunctionValidationContext::end ( NoImm )
inline

Definition at line 221 of file Validate.cpp.

222 {
223 if( controlStack.size() == 0 ) throw ValidationException( "control stack empty" );
224 popAndValidateResultType("end result",controlStack.back().resultType);
225 popControlStack();
226 }

◆ get_global()

void IR::FunctionValidationContext::get_global ( GetOrSetVariableImm< true > imm)
inline

Definition at line 296 of file Validate.cpp.

297 {
298 pushOperand(validateGlobalIndex(module,imm.variableIndex,false,false,false,"get_global"));
299 }
ValueType validateGlobalIndex(const Module &module, Uptr globalIndex, bool mustBeMutable, bool mustBeImmutable, bool mustBeImport, const char *context)
Definition Validate.cpp:109
Here is the call graph for this function:

◆ get_local()

void IR::FunctionValidationContext::get_local ( GetOrSetVariableImm< false > imm)
inline

Definition at line 281 of file Validate.cpp.

282 {
283 pushOperand(validateLocalIndex(imm.variableIndex));
284 }

◆ getControlStackSize()

Uptr IR::FunctionValidationContext::getControlStackSize ( )
inline

Definition at line 158 of file Validate.cpp.

158{ return controlStack.size(); }

◆ if_()

void IR::FunctionValidationContext::if_ ( ControlStructureImm imm)
inline

Definition at line 209 of file Validate.cpp.

210 {
211 validate(imm.resultType);
212 popAndValidateOperand("if condition",ValueType::i32);
213 pushControlStack(ControlContext::Type::ifThen,imm.resultType,imm.resultType);
214 }
Here is the call graph for this function:

◆ logOperator()

void IR::FunctionValidationContext::logOperator ( const std::string & operatorDescription)
inline

Definition at line 160 of file Validate.cpp.

161 {
163 {
164 std::string controlStackString;
165 for(Uptr stackIndex = 0;stackIndex < controlStack.size();++stackIndex)
166 {
167 if(!controlStack[stackIndex].isReachable) { controlStackString += "("; }
168 switch(controlStack[stackIndex].type)
169 {
170 case ControlContext::Type::function: controlStackString += "F"; break;
171 case ControlContext::Type::block: controlStackString += "B"; break;
172 case ControlContext::Type::ifThen: controlStackString += "T"; break;
173 case ControlContext::Type::ifElse: controlStackString += "E"; break;
174 case ControlContext::Type::loop: controlStackString += "L"; break;
175 default: Errors::unreachable();
176 };
177 if(!controlStack[stackIndex].isReachable) { controlStackString += ")"; }
178 }
179
180 std::string stackString;
181 const Uptr stackBase = controlStack.size() == 0 ? 0 : controlStack.back().outerStackSize;
182 for(Uptr stackIndex = 0;stackIndex < stack.size();++stackIndex)
183 {
184 if(stackIndex == stackBase) { stackString += "| "; }
185 stackString += asString(stack[stackIndex]);
186 stackString += " ";
187 }
188 if(stack.size() == stackBase) { stackString += "|"; }
189
190 Log::printf(Log::Category::debug,"%-50s %-50s %-50s\n",controlStackString.c_str(),operatorDescription.c_str(),stackString.c_str());
191 }
192 }
#define ENABLE_LOGGING
void unreachable()
Definition Errors.h:22
std::string asString(I32 value)
Definition Types.h:50
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
Here is the call graph for this function:

◆ loop()

void IR::FunctionValidationContext::loop ( ControlStructureImm imm)
inline

Definition at line 204 of file Validate.cpp.

205 {
206 validate(imm.resultType);
207 pushControlStack(ControlContext::Type::loop,ResultType::none,imm.resultType);
208 }
Here is the call graph for this function:

◆ return_()

void IR::FunctionValidationContext::return_ ( NoImm )
inline

Definition at line 228 of file Validate.cpp.

229 {
230 popAndValidateResultType("ret",functionType->ret);
231 enterUnreachable();
232 }

◆ select()

void IR::FunctionValidationContext::select ( NoImm )
inline

Definition at line 271 of file Validate.cpp.

272 {
273 const ValueType condition = popOperand();
274 const ValueType falseType = popOperand();
275 const ValueType trueType = popOperand();
276 validateOperandType(ValueType::i32,condition,"select condition");
277 validateOperandType(falseType,trueType,"select operands");
278 pushOperand(falseType);
279 }
void validateOperandType(ValueType expectedType, ValueType actualType, const char *context)
Definition Validate.cpp:100
ValueType
Definition Types.h:14
Here is the call graph for this function:

◆ set_global()

void IR::FunctionValidationContext::set_global ( GetOrSetVariableImm< true > imm)
inline

Definition at line 300 of file Validate.cpp.

301 {
302 popAndValidateOperand("set_global",validateGlobalIndex(module,imm.variableIndex,true,false,false,"set_global"));
303 }
Here is the call graph for this function:

◆ set_local()

void IR::FunctionValidationContext::set_local ( GetOrSetVariableImm< false > imm)
inline

Definition at line 285 of file Validate.cpp.

286 {
287 popAndValidateOperand("set_local",validateLocalIndex(imm.variableIndex));
288 }

◆ tee_local()

void IR::FunctionValidationContext::tee_local ( GetOrSetVariableImm< false > imm)
inline

Definition at line 289 of file Validate.cpp.

290 {
291 const ValueType localType = validateLocalIndex(imm.variableIndex);
292 popAndValidateOperand("tee_local",localType);
293 pushOperand(localType);
294 }

◆ unknown()

void IR::FunctionValidationContext::unknown ( Opcode opcode)
inline

Definition at line 195 of file Validate.cpp.

196 {
197 throw ValidationException("Unknown opcode: " + std::to_string((Uptr)opcode));
198 }

◆ unreachable()

void IR::FunctionValidationContext::unreachable ( NoImm )
inline

Definition at line 262 of file Validate.cpp.

263 {
264 enterUnreachable();
265 }

◆ validateImm() [1/4]

template<typename nativeType >
void IR::FunctionValidationContext::validateImm ( LiteralImm< nativeType > imm)
inline

Definition at line 324 of file Validate.cpp.

324{}

◆ validateImm() [2/4]

template<Uptr naturalAlignmentLog2>
void IR::FunctionValidationContext::validateImm ( LoadOrStoreImm< naturalAlignmentLog2 > imm)
inline

Definition at line 327 of file Validate.cpp.

328 {
329 VALIDATE_UNLESS("load or store alignment greater than natural alignment: ",imm.alignmentLog2>naturalAlignmentLog2);
330 VALIDATE_UNLESS("load or store in module without default memory: ",module.memories.size()==0);
331 }
IndexSpace< MemoryDef, MemoryType > memories
Definition Module.h:135

◆ validateImm() [3/4]

void IR::FunctionValidationContext::validateImm ( MemoryImm )
inline

Definition at line 333 of file Validate.cpp.

334 {
335 VALIDATE_UNLESS("current_memory and grow_memory are only valid if there is a default memory",module.memories.size() == 0);
336 }

◆ validateImm() [4/4]

void IR::FunctionValidationContext::validateImm ( NoImm )
inline

Definition at line 321 of file Validate.cpp.

321{}

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