heap.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef CARBON_EXPLORER_INTERPRETER_HEAP_H_
  5. #define CARBON_EXPLORER_INTERPRETER_HEAP_H_
  6. #include <vector>
  7. #include "common/ostream.h"
  8. #include "explorer/ast/address.h"
  9. #include "explorer/ast/value.h"
  10. #include "explorer/ast/value_node.h"
  11. #include "explorer/base/nonnull.h"
  12. #include "explorer/base/source_location.h"
  13. #include "explorer/base/trace_stream.h"
  14. #include "explorer/interpreter/heap_allocation_interface.h"
  15. namespace Carbon {
  16. // A Heap represents the abstract machine's dynamically allocated memory.
  17. class Heap : public HeapAllocationInterface, public Printable<Heap> {
  18. public:
  19. enum class ValueState {
  20. Uninitialized,
  21. Discarded,
  22. Alive,
  23. Dead,
  24. };
  25. // Constructs an empty Heap.
  26. explicit Heap(Nonnull<TraceStream*> trace_stream, Nonnull<Arena*> arena)
  27. : arena_(arena), trace_stream_(trace_stream) {}
  28. Heap(const Heap&) = delete;
  29. auto operator=(const Heap&) -> Heap& = delete;
  30. // Returns the value at the given address in the heap after
  31. // checking that it is alive.
  32. auto Read(const Address& a, SourceLocation source_loc) const
  33. -> ErrorOr<Nonnull<const Value*>> override;
  34. // Writes the given value at the address in the heap after
  35. // checking that the address is alive.
  36. auto Write(const Address& a, Nonnull<const Value*> v,
  37. SourceLocation source_loc) -> ErrorOr<Success> override;
  38. // Returns whether the value bound at the given node is still alive.
  39. auto is_bound_value_alive(const ValueNodeView& node, const Address& a) const
  40. -> bool override;
  41. void BindValueToReference(const ValueNodeView& node,
  42. const Address& a) override;
  43. // Put the given value on the heap and mark its state.
  44. // Mark UninitializedValue as uninitialized and other values as alive.
  45. auto AllocateValue(Nonnull<const Value*> v) -> AllocationId override;
  46. // Marks this allocation, and all of its sub-objects, as dead.
  47. auto Deallocate(AllocationId allocation) -> ErrorOr<Success> override;
  48. auto Deallocate(const Address& a) -> ErrorOr<Success>;
  49. // Marks this allocation, and all its sub-objects, as discarded.
  50. void Discard(AllocationId allocation);
  51. // Returns whether the given allocation was unused and discarded.
  52. auto is_discarded(AllocationId allocation) const -> bool;
  53. // Returns whether the given allocation was initialized.
  54. auto is_initialized(AllocationId allocation) const -> bool;
  55. // Print all the values on the heap to the stream `out`.
  56. void Print(llvm::raw_ostream& out) const;
  57. auto arena() const -> Arena& override { return *arena_; }
  58. private:
  59. // Returns whether the address have the same AllocationdId and their path
  60. // are strictly nested.
  61. static auto AddressesAreStrictlyNested(const Address& first,
  62. const Address& second) -> bool;
  63. // Returns whether the provided paths are strictly nested. This checks the
  64. // name, index, and base element only, and might not valid if used to
  65. // compare paths based on a different AllocationId.
  66. static auto PathsAreStrictlyNested(const ElementPath& first,
  67. const ElementPath& second) -> bool;
  68. // Signal an error if the allocation is no longer alive.
  69. auto CheckAlive(AllocationId allocation, SourceLocation source_loc) const
  70. -> ErrorOr<Success>;
  71. // Signal an error if the allocation has not been initialized.
  72. auto CheckInit(AllocationId allocation, SourceLocation source_loc) const
  73. -> ErrorOr<Success>;
  74. Nonnull<Arena*> arena_;
  75. std::vector<Nonnull<const Value*>> values_;
  76. std::vector<ValueState> states_;
  77. std::vector<llvm::DenseMap<const AstNode*, Address>> bound_values_;
  78. Nonnull<TraceStream*> trace_stream_;
  79. };
  80. } // namespace Carbon
  81. #endif // CARBON_EXPLORER_INTERPRETER_HEAP_H_