heap.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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) -> Address {
  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. Address 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, source_loc);
  21. return values_[a.index_]->GetField(arena_, a.field_path_, source_loc);
  22. }
  23. void Heap::Write(const Address& a, Nonnull<const Value*> v,
  24. SourceLocation source_loc) {
  25. this->CheckAlive(a, source_loc);
  26. values_[a.index_] =
  27. values_[a.index_]->SetField(arena_, a.field_path_, v, source_loc);
  28. }
  29. void Heap::CheckAlive(const Address& address, SourceLocation source_loc) {
  30. if (!alive_[address.index_]) {
  31. FATAL_RUNTIME_ERROR(source_loc)
  32. << "undefined behavior: access to dead value "
  33. << *values_[address.index_];
  34. }
  35. }
  36. void Heap::Deallocate(const Address& address) {
  37. CHECK(address.field_path_.IsEmpty());
  38. if (alive_[address.index_]) {
  39. alive_[address.index_] = false;
  40. } else {
  41. FATAL_RUNTIME_ERROR_NO_LINE() << "deallocating an already dead value";
  42. }
  43. }
  44. void Heap::Print(llvm::raw_ostream& out) const {
  45. llvm::ListSeparator sep;
  46. for (size_t i = 0; i < values_.size(); ++i) {
  47. out << sep;
  48. PrintAddress(Address(i), out);
  49. }
  50. }
  51. void Heap::PrintAddress(const Address& a, llvm::raw_ostream& out) const {
  52. if (!alive_[a.index_]) {
  53. out << "!!";
  54. }
  55. out << *values_[a.index_];
  56. }
  57. } // namespace Carbon