heap.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/interpreter/heap.h"
  5. #include "explorer/common/error_builders.h"
  6. #include "explorer/interpreter/value.h"
  7. #include "llvm/ADT/StringExtras.h"
  8. #include "llvm/Support/Error.h"
  9. namespace Carbon {
  10. auto Heap::AllocateValue(Nonnull<const Value*> v) -> AllocationId {
  11. // Putting the following two side effects together in this function
  12. // ensures that we don't do anything else in between, which is really bad!
  13. // Consider whether to include a copy of the input v in this function
  14. // or to leave it up to the caller.
  15. AllocationId a(values_.size());
  16. values_.push_back(v);
  17. if (v->kind() == Carbon::Value::Kind::UninitializedValue) {
  18. states_.push_back(ValueState::Uninitialized);
  19. } else {
  20. states_.push_back(ValueState::Alive);
  21. }
  22. return a;
  23. }
  24. auto Heap::Read(const Address& a, SourceLocation source_loc) const
  25. -> ErrorOr<Nonnull<const Value*>> {
  26. CARBON_RETURN_IF_ERROR(this->CheckInit(a.allocation_, source_loc));
  27. CARBON_RETURN_IF_ERROR(this->CheckAlive(a.allocation_, source_loc));
  28. Nonnull<const Value*> value = values_[a.allocation_.index_];
  29. return value->GetMember(arena_, a.field_path_, source_loc, value);
  30. }
  31. auto Heap::Write(const Address& a, Nonnull<const Value*> v,
  32. SourceLocation source_loc) -> ErrorOr<Success> {
  33. CARBON_RETURN_IF_ERROR(this->CheckAlive(a.allocation_, source_loc));
  34. if (states_[a.allocation_.index_] == ValueState::Uninitialized) {
  35. states_[a.allocation_.index_] = ValueState::Alive;
  36. }
  37. CARBON_ASSIGN_OR_RETURN(values_[a.allocation_.index_],
  38. values_[a.allocation_.index_]->SetField(
  39. arena_, a.field_path_, v, source_loc));
  40. return Success();
  41. }
  42. auto Heap::CheckAlive(AllocationId allocation, SourceLocation source_loc) const
  43. -> ErrorOr<Success> {
  44. if (states_[allocation.index_] == ValueState::Dead) {
  45. return ProgramError(source_loc)
  46. << "undefined behavior: access to dead value "
  47. << *values_[allocation.index_];
  48. }
  49. return Success();
  50. }
  51. auto Heap::CheckInit(AllocationId allocation, SourceLocation source_loc) const
  52. -> ErrorOr<Success> {
  53. if (states_[allocation.index_] == ValueState::Uninitialized) {
  54. return ProgramError(source_loc)
  55. << "undefined behavior: access to uninitialized value "
  56. << *values_[allocation.index_];
  57. }
  58. return Success();
  59. }
  60. void Heap::Deallocate(AllocationId allocation) {
  61. if (states_[allocation.index_] != ValueState::Dead) {
  62. states_[allocation.index_] = ValueState::Dead;
  63. } else {
  64. CARBON_FATAL() << "deallocating an already dead value: "
  65. << *values_[allocation.index_];
  66. }
  67. }
  68. void Heap::Deallocate(const Address& a) { Deallocate(a.allocation_); }
  69. void Heap::Print(llvm::raw_ostream& out) const {
  70. llvm::ListSeparator sep;
  71. for (size_t i = 0; i < values_.size(); ++i) {
  72. out << sep;
  73. out << i << ": ";
  74. if (states_[i] == ValueState::Uninitialized) {
  75. out << "!";
  76. } else if (states_[i] == ValueState::Dead) {
  77. out << "!!";
  78. }
  79. out << *values_[i];
  80. }
  81. }
  82. } // namespace Carbon