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