Files
carbon-lang/executable_semantics/interpreter/heap.h
T
Geoff RomerandJon Meow d5124256a2 Split interpreter into smaller modules (#662)
- Move Heap and Frame/Scope to their own headers.
- Move some functions to more appropriate headers (e.g. CopyValue -> value.h).
- Define a separate Bazel rule for each header/cpp pair.
- Modify PrintValue to not print the frames of a ContinuationValue. This was necessary to break a dependency cycle between PrintValue, PrintFrame, and Action::Print.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-07-19 13:51:35 -07:00

56 lines
1.7 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_MEMORY_H_
#define EXECUTABLE_SEMANTICS_INTERPRETER_MEMORY_H_
#include <ostream>
#include <vector>
#include "executable_semantics/interpreter/address.h"
#include "executable_semantics/interpreter/value.h"
namespace Carbon {
// A Heap represents the abstract machine's dynamically allocated memory.
class Heap {
public:
// Constructs an empty Heap.
Heap() = default;
Heap(const Heap&) = delete;
Heap& operator=(const Heap&) = delete;
// Returns the value at the given address in the heap after
// checking that it is alive.
auto Read(const Address& a, int line_num) -> const Value*;
// Writes the given value at the address in the heap after
// checking that the address is alive.
auto Write(const Address& a, const Value* v, int line_num) -> void;
// Put the given value on the heap and mark it as alive.
auto AllocateValue(const Value* v) -> Address;
// Marks the object at this address, and all of its sub-objects, as dead.
auto Deallocate(const Address& address) -> void;
// Print the value at the given address to the stream `out`.
auto PrintAddress(const Address& a, std::ostream& out) -> void;
// Print all the values on the heap to the stream `out`.
auto PrintHeap(std::ostream& out) -> void;
private:
// Signal an error if the address is no longer alive.
void CheckAlive(const Address& address, int line_num);
std::vector<const Value*> values_;
std::vector<bool> alive_;
};
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_INTERPRETER_MEMORY_H_