Nearly CC
An educational compiler skeleton
local_storage_allocation.h
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef LOCAL_STORAGE_ALLOCATION_H
22 #define LOCAL_STORAGE_ALLOCATION_H
23 
24 #include "storage.h"
25 #include "ast_visitor.h"
26 #include "function.h"
27 
28 // A LocalStorageAllocation object is responsible for allocating local
29 // storage for variables in exactly one function. The allocate_storage()
30 // member function will be called with a shared pointer to the
31 // Function containing the information about the function
32 // (including its AST). The Function object is also intended to
33 // the place where storage allocation decisions for the function
34 // are recorded.
36 public:
37  // vr0 is the return value vreg
38  static const int VREG_RETVAL = 0;
39 
40  // vr1 is 1st argument vreg
41  static const int VREG_FIRST_ARG = 1;
42 
43  // local variable allocation starts at vr10
44  static const int VREG_FIRST_LOCAL = 10;
45 
46 private:
47  std::shared_ptr<Function> m_function;
48  StorageCalculator m_storage_calc;
49  unsigned m_total_local_storage;
50  int m_next_vreg;
51 
52 public:
54  virtual ~LocalStorageAllocation();
55 
56  void allocate_storage(const std::shared_ptr<Function> &function);
57 
58  virtual void visit_declarator_list(Node *n);
59  virtual void visit_function_definition(Node *n);
60  virtual void visit_function_parameter(Node *n);
61  virtual void visit_statement_list(Node *n);
62  virtual void visit_struct_type_definition(Node *n);
63 
64 private:
65 #ifdef SOLUTION
66  StorageCalculator enter_scope();
67  void leave_scope(StorageCalculator save);
68  void visit_scope(Node *n);
69  void allocate_storage(Node *n);
70 #else
71  // TODO: add private member functions
72 #endif
73 };
74 
75 #endif // LOCAL_STORAGE_ALLOCATION_H
Base class for AST visitors.
Definition: ast_visitor.h:7
Definition: local_storage_allocation.h:35
virtual void visit_declarator_list(Node *n)
Visit a Node with the AST_DECLARATOR_LIST tag value.
Definition: local_storage_allocation.cpp:42
virtual void visit_function_parameter(Node *n)
Visit a Node with the AST_FUNCTION_PARAMETER tag value.
Definition: local_storage_allocation.cpp:91
virtual void visit_function_definition(Node *n)
Visit a Node with the AST_FUNCTION_DEFINITION tag value.
Definition: local_storage_allocation.cpp:51
virtual void visit_struct_type_definition(Node *n)
Visit a Node with the AST_STRUCT_TYPE_DEFINITION tag value.
Definition: local_storage_allocation.cpp:107
virtual void visit_statement_list(Node *n)
Visit a Node with the AST_STATEMENT_LIST tag value.
Definition: local_storage_allocation.cpp:99
Definition: node.h:35
Definition: storage.h:31