heap.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. namespace Carbon {
  6. auto Heap::AllocateValue(const Value* v) -> Address {
  7. // Putting the following two side effects together in this function
  8. // ensures that we don't do anything else in between, which is really bad!
  9. // Consider whether to include a copy of the input v in this function
  10. // or to leave it up to the caller.
  11. CHECK(v != nullptr);
  12. Address a(values_.size());
  13. values_.push_back(v);
  14. alive_.push_back(true);
  15. return a;
  16. }
  17. auto Heap::Read(const Address& a, int line_num) -> const Value* {
  18. this->CheckAlive(a, line_num);
  19. return values_[a.index]->GetField(a.field_path, line_num);
  20. }
  21. void Heap::Write(const Address& a, const Value* v, int line_num) {
  22. CHECK(v != nullptr);
  23. this->CheckAlive(a, line_num);
  24. values_[a.index] = values_[a.index]->SetField(a.field_path, v, line_num);
  25. }
  26. void Heap::CheckAlive(const Address& address, int line_num) {
  27. if (!alive_[address.index]) {
  28. llvm::errs() << line_num << ": undefined behavior: access to dead value "
  29. << *values_[address.index] << "\n";
  30. exit(-1);
  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. llvm::errs() << "runtime error, deallocating an already dead value\n";
  39. exit(-1);
  40. }
  41. }
  42. void Heap::Print(llvm::raw_ostream& out) const {
  43. for (size_t i = 0; i < values_.size(); ++i) {
  44. PrintAddress(Address(i), out);
  45. 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