interpreter.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
  6. #include <list>
  7. #include <utility>
  8. #include <vector>
  9. #include "executable_semantics/ast/declaration.h"
  10. #include "executable_semantics/interpreter/action.h"
  11. #include "executable_semantics/interpreter/dictionary.h"
  12. #include "executable_semantics/interpreter/stack.h"
  13. #include "executable_semantics/interpreter/value.h"
  14. namespace Carbon {
  15. using Env = Dictionary<std::string, Address>;
  16. /***** Scopes *****/
  17. struct Scope {
  18. Scope(Env values, std::list<std::string> l)
  19. : values(values), locals(std::move(l)) {}
  20. Env values;
  21. std::list<std::string> locals;
  22. };
  23. /***** Frames and State *****/
  24. // A frame represents either a function call or a delimited continuation.
  25. struct Frame {
  26. // The name of the function.
  27. std::string name;
  28. // If the frame represents a function call, the bottom scope
  29. // contains the parameter-argument bindings for this function
  30. // call. The rest of the scopes contain local variables defined by
  31. // blocks within the function. The scope at the top of the stack is
  32. // the current scope and its environment is the one used for looking
  33. // up the value associated with a variable.
  34. Stack<Scope*> scopes;
  35. // The actions that need to be executed in the future of the
  36. // current function call. The top of the stack is the action
  37. // that is executed first.
  38. Stack<Action*> todo;
  39. // If this frame is the bottom frame of a continuation, then it stores
  40. // the address of the continuation.
  41. // Otherwise the `continuation` field is the sentinel UINT_MAX.
  42. Address continuation;
  43. // Returns whether this frame is the bottom frame of a continuation.
  44. auto IsContinuation() -> bool { return continuation != UINT_MAX; }
  45. Frame(std::string n, Stack<Scope*> s, Stack<Action*> c)
  46. : name(std::move(std::move(n))),
  47. scopes(s),
  48. todo(c),
  49. continuation(UINT_MAX) {}
  50. };
  51. // A Heap represents the abstract machine's dynamically allocated memory.
  52. class Heap {
  53. public:
  54. // Constructs an empty Heap.
  55. Heap() = default;
  56. Heap(const Heap&) = delete;
  57. Heap& operator=(const Heap&) = delete;
  58. // Returns the value at the given address in the heap after
  59. // checking that it is alive.
  60. auto Read(Address a, int line_num) -> const Value*;
  61. // Writes the given value at the address in the heap after
  62. // checking that the address is alive.
  63. auto Write(Address a, const Value* v, int line_num) -> void;
  64. // Put the given value on the heap and mark it as alive.
  65. auto AllocateValue(const Value* v) -> Address;
  66. // Marks the object at this address, and all of its sub-objects, as dead.
  67. auto Deallocate(Address address) -> void;
  68. // Print the value at the given address to the stream `out`.
  69. auto PrintAddress(Address a, std::ostream& out) -> void;
  70. // Print all the values on the heap to the stream `out`.
  71. auto PrintHeap(std::ostream& out) -> void;
  72. private:
  73. // Signal an error if the address is no longer alive.
  74. void CheckAlive(Address address, int line_num);
  75. // Marks all sub-objects of this value as dead.
  76. void DeallocateSubObjects(const Value* val);
  77. std::vector<const Value*> values_;
  78. std::vector<bool> alive_;
  79. };
  80. struct State {
  81. Stack<Frame*> stack;
  82. Heap heap;
  83. };
  84. extern State* state;
  85. auto PrintFrame(Frame* frame, std::ostream& out) -> void;
  86. void PrintStack(Stack<Frame*> ls, std::ostream& out);
  87. void PrintEnv(Env values);
  88. auto CopyVal(const Value* val, int line_num) -> const Value*;
  89. auto ToInteger(const Value* v) -> int;
  90. /***** Interpreters *****/
  91. auto InterpProgram(std::list<Declaration>* fs) -> int;
  92. auto InterpExp(Env values, const Expression* e) -> const Value*;
  93. } // namespace Carbon
  94. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_