2016-01-14 04:25:11 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-01-14 04:25:11 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-14 04:25:11 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-01-14 04:25:11 +00:00
|
|
|
|
|
|
|
// Source code for logical layout validation as described in section 2.4
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validate.h"
|
2016-06-02 22:51:05 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/diagnostic.h"
|
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/operand.h"
|
|
|
|
#include "source/val/function.h"
|
|
|
|
#include "source/val/instruction.h"
|
|
|
|
#include "source/val/validation_state.h"
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2016-01-14 04:25:11 +00:00
|
|
|
namespace {
|
2018-07-07 13:38:00 +00:00
|
|
|
|
2016-01-14 04:25:11 +00:00
|
|
|
// Module scoped instructions are processed by determining if the opcode
|
|
|
|
// is part of the current layout section. If it is not then the next sections is
|
|
|
|
// checked.
|
|
|
|
spv_result_t ModuleScopedInstructions(ValidationState_t& _,
|
2018-07-10 14:57:52 +00:00
|
|
|
const Instruction* inst, SpvOp opcode) {
|
2016-06-25 03:45:25 +00:00
|
|
|
while (_.IsOpcodeInCurrentLayoutSection(opcode) == false) {
|
|
|
|
_.ProgressToNextLayoutSectionOrder();
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
switch (_.current_layout_section()) {
|
2016-01-14 04:25:11 +00:00
|
|
|
case kLayoutMemoryModel:
|
|
|
|
if (opcode != SpvOpMemoryModel) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< " cannot appear before the memory model instruction";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kLayoutFunctionDeclarations:
|
2017-09-07 21:38:31 +00:00
|
|
|
// All module sections have been processed. Recursively call
|
2016-01-14 04:25:11 +00:00
|
|
|
// ModuleLayoutPass to process the next section of the module
|
2018-07-07 13:38:00 +00:00
|
|
|
return ModuleLayoutPass(_, inst);
|
2016-01-14 04:25:11 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function declaration validation is performed by making sure that the
|
|
|
|
// FunctionParameter and FunctionEnd instructions only appear inside of
|
|
|
|
// functions. It also ensures that the Function instruction does not appear
|
|
|
|
// inside of another function. This stage ends when the first label is
|
|
|
|
// encountered inside of a function.
|
|
|
|
spv_result_t FunctionScopedInstructions(ValidationState_t& _,
|
2018-07-10 14:57:52 +00:00
|
|
|
const Instruction* inst, SpvOp opcode) {
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.IsOpcodeInCurrentLayoutSection(opcode)) {
|
2016-01-14 04:25:11 +00:00
|
|
|
switch (opcode) {
|
2016-03-16 21:20:02 +00:00
|
|
|
case SpvOpFunction: {
|
2016-01-14 04:25:11 +00:00
|
|
|
if (_.in_function_body()) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< "Cannot declare a function in a function body";
|
|
|
|
}
|
2018-07-10 14:57:52 +00:00
|
|
|
auto control_mask = inst->GetOperandAs<SpvFunctionControlMask>(2);
|
2016-09-02 13:51:44 +00:00
|
|
|
if (auto error =
|
2018-07-10 14:57:52 +00:00
|
|
|
_.RegisterFunction(inst->id(), inst->type_id(), control_mask,
|
|
|
|
inst->GetOperandAs<uint32_t>(3)))
|
2016-09-02 13:51:44 +00:00
|
|
|
return error;
|
|
|
|
if (_.current_layout_section() == kLayoutFunctionDefinitions) {
|
|
|
|
if (auto error = _.current_function().RegisterSetFunctionDeclType(
|
|
|
|
FunctionDecl::kFunctionDeclDefinition))
|
|
|
|
return error;
|
|
|
|
}
|
2016-03-16 21:20:02 +00:00
|
|
|
} break;
|
2016-01-14 04:25:11 +00:00
|
|
|
|
|
|
|
case SpvOpFunctionParameter:
|
|
|
|
if (_.in_function_body() == false) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
|
|
|
<< "Function parameter instructions must be in a "
|
|
|
|
"function body";
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.current_function().block_count() != 0) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-06-02 22:51:05 +00:00
|
|
|
<< "Function parameters must only appear immediately after "
|
2016-06-25 03:45:25 +00:00
|
|
|
"the function definition";
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
2016-09-02 13:51:44 +00:00
|
|
|
if (auto error = _.current_function().RegisterFunctionParameter(
|
2018-07-10 14:57:52 +00:00
|
|
|
inst->id(), inst->type_id()))
|
2016-09-02 13:51:44 +00:00
|
|
|
return error;
|
2016-01-14 04:25:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SpvOpFunctionEnd:
|
|
|
|
if (_.in_function_body() == false) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< "Function end instructions must be in a function body";
|
|
|
|
}
|
|
|
|
if (_.in_block()) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< "Function end cannot be called in blocks";
|
|
|
|
}
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.current_function().block_count() == 0 &&
|
|
|
|
_.current_layout_section() == kLayoutFunctionDefinitions) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
|
|
|
<< "Function declarations must appear before "
|
|
|
|
"function definitions.";
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.current_layout_section() == kLayoutFunctionDeclarations) {
|
2016-09-02 13:51:44 +00:00
|
|
|
if (auto error = _.current_function().RegisterSetFunctionDeclType(
|
|
|
|
FunctionDecl::kFunctionDeclDeclaration))
|
|
|
|
return error;
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
2016-09-02 13:51:44 +00:00
|
|
|
if (auto error = _.RegisterFunctionEnd()) return error;
|
2016-01-14 04:25:11 +00:00
|
|
|
break;
|
|
|
|
|
2016-01-18 18:41:52 +00:00
|
|
|
case SpvOpLine:
|
|
|
|
case SpvOpNoLine:
|
2016-01-14 04:25:11 +00:00
|
|
|
break;
|
|
|
|
case SpvOpLabel:
|
|
|
|
// If the label is encountered then the current function is a
|
|
|
|
// definition so set the function to a declaration and update the
|
|
|
|
// module section
|
|
|
|
if (_.in_function_body() == false) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< "Label instructions must be in a function body";
|
|
|
|
}
|
|
|
|
if (_.in_block()) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< "A block must end with a branch instruction.";
|
|
|
|
}
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.current_layout_section() == kLayoutFunctionDeclarations) {
|
|
|
|
_.ProgressToNextLayoutSectionOrder();
|
2016-09-02 13:51:44 +00:00
|
|
|
if (auto error = _.current_function().RegisterSetFunctionDeclType(
|
|
|
|
FunctionDecl::kFunctionDeclDefinition))
|
|
|
|
return error;
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-25 03:45:25 +00:00
|
|
|
if (_.current_layout_section() == kLayoutFunctionDeclarations &&
|
2016-06-09 19:51:39 +00:00
|
|
|
_.in_function_body()) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< "A function must begin with a label";
|
|
|
|
} else {
|
|
|
|
if (_.in_block() == false) {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< spvOpcodeString(opcode) << " must appear in a block";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-01 16:01:35 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
|
2016-01-14 04:25:11 +00:00
|
|
|
<< spvOpcodeString(opcode)
|
|
|
|
<< " cannot appear in a function declaration";
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
2018-07-07 13:38:00 +00:00
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
} // namespace
|
2016-01-14 04:25:11 +00:00
|
|
|
|
|
|
|
// TODO(umar): Check linkage capabilities for function declarations
|
|
|
|
// TODO(umar): Better error messages
|
|
|
|
// NOTE: This function does not handle CFG related validation
|
|
|
|
// Performs logical layout validation. See Section 2.4
|
2018-07-10 14:57:52 +00:00
|
|
|
spv_result_t ModuleLayoutPass(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
const SpvOp opcode = inst->opcode();
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2016-06-25 03:45:25 +00:00
|
|
|
switch (_.current_layout_section()) {
|
2016-02-22 20:25:50 +00:00
|
|
|
case kLayoutCapabilities:
|
|
|
|
case kLayoutExtensions:
|
|
|
|
case kLayoutExtInstImport:
|
|
|
|
case kLayoutMemoryModel:
|
|
|
|
case kLayoutEntryPoint:
|
|
|
|
case kLayoutExecutionMode:
|
|
|
|
case kLayoutDebug1:
|
|
|
|
case kLayoutDebug2:
|
2017-09-07 21:38:31 +00:00
|
|
|
case kLayoutDebug3:
|
2016-02-22 20:25:50 +00:00
|
|
|
case kLayoutAnnotations:
|
|
|
|
case kLayoutTypes:
|
2016-09-02 13:51:44 +00:00
|
|
|
if (auto error = ModuleScopedInstructions(_, inst, opcode)) return error;
|
2016-02-22 20:25:50 +00:00
|
|
|
break;
|
|
|
|
case kLayoutFunctionDeclarations:
|
|
|
|
case kLayoutFunctionDefinitions:
|
2016-09-02 13:51:44 +00:00
|
|
|
if (auto error = FunctionScopedInstructions(_, inst, opcode)) {
|
|
|
|
return error;
|
|
|
|
}
|
2016-02-22 20:25:50 +00:00
|
|
|
break;
|
2016-09-02 13:51:44 +00:00
|
|
|
}
|
2016-01-14 04:25:11 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
2018-07-10 03:18:44 +00:00
|
|
|
|
|
|
|
} // namespace val
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace spvtools
|