heap.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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)
  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, SourceLocation source_loc) {
  31. if (!alive_[allocation.index_]) {
  32. FATAL_RUNTIME_ERROR(source_loc)
  33. << "undefined behavior: access to dead value "
  34. << *values_[allocation.index_];
  35. }
  36. }
  37. void Heap::Deallocate(AllocationId allocation) {
  38. if (alive_[allocation.index_]) {
  39. alive_[allocation.index_] = false;
  40. } else {
  41. FATAL() << "deallocating an already dead value: "
  42. << *values_[allocation.index_];
  43. }
  44. }
  45. void Heap::Print(llvm::raw_ostream& out) const {
  46. llvm::ListSeparator sep;
  47. for (size_t i = 0; i < values_.size(); ++i) {
  48. out << sep;
  49. PrintAllocation(AllocationId(i), out);
  50. }
  51. }
  52. void Heap::PrintAllocation(AllocationId allocation,
  53. llvm::raw_ostream& out) const {
  54. if (!alive_[allocation.index_]) {
  55. out << "!!";
  56. }
  57. out << *values_[allocation.index_];
  58. }
  59. } // namespace Carbon