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