heap.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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->GetElement(arena_, a.element_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.element_path_, v, source_loc));
  40. return Success();
  41. }
  42. auto Heap::GetAllocationId(Nonnull<const Value*> v) const
  43. -> std::optional<AllocationId> {
  44. auto iter = std::find(values_.begin(), values_.end(), v);
  45. if (iter != values_.end()) {
  46. auto index = iter - values_.begin();
  47. if (states_[index] == ValueState::Alive) {
  48. return AllocationId(index);
  49. }
  50. }
  51. return std::nullopt;
  52. }
  53. auto Heap::CheckAlive(AllocationId allocation, SourceLocation source_loc) const
  54. -> ErrorOr<Success> {
  55. if (states_[allocation.index_] == ValueState::Dead) {
  56. return ProgramError(source_loc)
  57. << "undefined behavior: access to dead value "
  58. << *values_[allocation.index_];
  59. }
  60. return Success();
  61. }
  62. auto Heap::CheckInit(AllocationId allocation, SourceLocation source_loc) const
  63. -> ErrorOr<Success> {
  64. if (states_[allocation.index_] == ValueState::Uninitialized) {
  65. return ProgramError(source_loc)
  66. << "undefined behavior: access to uninitialized value "
  67. << *values_[allocation.index_];
  68. }
  69. return Success();
  70. }
  71. void Heap::Deallocate(AllocationId allocation) {
  72. if (states_[allocation.index_] != ValueState::Dead) {
  73. states_[allocation.index_] = ValueState::Dead;
  74. } else {
  75. CARBON_FATAL() << "deallocating an already dead value: "
  76. << *values_[allocation.index_];
  77. }
  78. }
  79. void Heap::Deallocate(const Address& a) { Deallocate(a.allocation_); }
  80. void Heap::Print(llvm::raw_ostream& out) const {
  81. llvm::ListSeparator sep;
  82. for (size_t i = 0; i < values_.size(); ++i) {
  83. out << sep;
  84. out << i << ": ";
  85. if (states_[i] == ValueState::Uninitialized) {
  86. out << "!";
  87. } else if (states_[i] == ValueState::Dead) {
  88. out << "!!";
  89. }
  90. out << *values_[i];
  91. }
  92. }
  93. } // namespace Carbon