validate_debug.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/val/validate.h"
  15. #include "source/opcode.h"
  16. #include "source/spirv_target_env.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. namespace {
  22. spv_result_t ValidateMemberName(ValidationState_t& _, const Instruction* inst) {
  23. const auto type_id = inst->GetOperandAs<uint32_t>(0);
  24. const auto type = _.FindDef(type_id);
  25. if (!type || SpvOpTypeStruct != type->opcode()) {
  26. return _.diag(SPV_ERROR_INVALID_ID, inst)
  27. << "OpMemberName Type <id> '" << _.getIdName(type_id)
  28. << "' is not a struct type.";
  29. }
  30. const auto member_id = inst->GetOperandAs<uint32_t>(1);
  31. const auto member_count = (uint32_t)(type->words().size() - 2);
  32. if (member_count <= member_id) {
  33. return _.diag(SPV_ERROR_INVALID_ID, inst)
  34. << "OpMemberName Member <id> '" << _.getIdName(member_id)
  35. << "' index is larger than Type <id> '" << _.getIdName(type->id())
  36. << "'s member count.";
  37. }
  38. return SPV_SUCCESS;
  39. }
  40. spv_result_t ValidateLine(ValidationState_t& _, const Instruction* inst) {
  41. const auto file_id = inst->GetOperandAs<uint32_t>(0);
  42. const auto file = _.FindDef(file_id);
  43. if (!file || SpvOpString != file->opcode()) {
  44. return _.diag(SPV_ERROR_INVALID_ID, inst)
  45. << "OpLine Target <id> '" << _.getIdName(file_id)
  46. << "' is not an OpString.";
  47. }
  48. return SPV_SUCCESS;
  49. }
  50. } // namespace
  51. spv_result_t DebugPass(ValidationState_t& _, const Instruction* inst) {
  52. switch (inst->opcode()) {
  53. case SpvOpMemberName:
  54. if (auto error = ValidateMemberName(_, inst)) return error;
  55. break;
  56. case SpvOpLine:
  57. if (auto error = ValidateLine(_, inst)) return error;
  58. break;
  59. default:
  60. break;
  61. }
  62. return SPV_SUCCESS;
  63. }
  64. } // namespace val
  65. } // namespace spvtools