heap.cpp 1.9 KB

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