heap.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 "executable_semantics/interpreter/heap.h"
  5. #include "executable_semantics/common/error.h"
  6. #include "llvm/ADT/StringExtras.h"
  7. namespace Carbon {
  8. auto Heap::AllocateValue(Nonnull<const Value*> v) -> AllocationId {
  9. // Putting the following two side effects together in this function
  10. // ensures that we don't do anything else in between, which is really bad!
  11. // Consider whether to include a copy of the input v in this function
  12. // or to leave it up to the caller.
  13. AllocationId a(values_.size());
  14. values_.push_back(v);
  15. alive_.push_back(true);
  16. return a;
  17. }
  18. auto Heap::Read(const Address& a, SourceLocation source_loc) const
  19. -> Nonnull<const Value*> {
  20. this->CheckAlive(a.allocation_, source_loc);
  21. return values_[a.allocation_.index_]->GetField(arena_, a.field_path_,
  22. source_loc);
  23. }
  24. void Heap::Write(const Address& a, Nonnull<const Value*> v,
  25. SourceLocation source_loc) {
  26. this->CheckAlive(a.allocation_, source_loc);
  27. values_[a.allocation_.index_] = values_[a.allocation_.index_]->SetField(
  28. arena_, a.field_path_, v, source_loc);
  29. }
  30. void Heap::CheckAlive(AllocationId allocation,
  31. SourceLocation source_loc) const {
  32. if (!alive_[allocation.index_]) {
  33. FATAL_RUNTIME_ERROR(source_loc)
  34. << "undefined behavior: access to dead value "
  35. << *values_[allocation.index_];
  36. }
  37. }
  38. void Heap::Deallocate(AllocationId allocation) {
  39. if (alive_[allocation.index_]) {
  40. alive_[allocation.index_] = false;
  41. } else {
  42. FATAL() << "deallocating an already dead value: "
  43. << *values_[allocation.index_];
  44. }
  45. }
  46. void Heap::Print(llvm::raw_ostream& out) const {
  47. llvm::ListSeparator sep;
  48. for (size_t i = 0; i < values_.size(); ++i) {
  49. out << sep;
  50. if (!alive_[i]) {
  51. out << "!!";
  52. }
  53. out << *values_[i];
  54. }
  55. }
  56. } // namespace Carbon