Nearly CC
An educational compiler skeleton
function.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 FUNCTION_H
22 #define FUNCTION_H
23 
24 #include <string>
25 #include <cassert>
26 #include <map>
27 #include "node.h"
28 #include "symtab.h"
29 #include "instruction_seq.h"
30 #ifdef SOLUTION
31 #include "lowlevel.h"
32 #endif
33 
42 class Function {
43 #ifdef SOLUTION
44 public:
45  typedef std::map<int, MachineReg> GlobalRegMap;
46 
47 #endif
48 private:
49  std::string m_name;
50  Node *m_funcdef_ast; // function definition AST node
51  Symbol *m_symbol; // function symbol table entry
52  std::shared_ptr<InstructionSequence> m_hl_iseq; // high-level code
53  std::shared_ptr<InstructionSequence> m_ll_iseq; // low-level code
54 #ifdef SOLUTION
55  unsigned m_total_local_storage;
56  int m_first_temp_vreg;
57  int m_num_vregs_used;
58  std::map<int, unsigned> m_vreg_storage_offsets;
59  int m_num_spill_locs;
60  GlobalRegMap m_global_regs;
61 #endif
62 
63 public:
68  Function(const std::string &name, Node *funcdef_ast, Symbol *symbol);
69  ~Function();
70 
73  std::string get_name() const;
74 
77  Node *get_funcdef_ast() const;
78 
81  Symbol *get_symbol() const;
82 
85  std::shared_ptr<InstructionSequence> get_hl_iseq() const;
86 
89  void set_hl_iseq(const std::shared_ptr<InstructionSequence> &hl_iseq);
90 
93  std::shared_ptr<InstructionSequence> get_ll_iseq() const;
94 
97  void set_ll_iseq(const std::shared_ptr<InstructionSequence> &ll_iseq);
98 
99 #ifdef SOLUTION
100  void set_total_local_storage(unsigned total_local_storage) { m_total_local_storage = total_local_storage; }
101  unsigned get_total_local_storage() const { return m_total_local_storage; }
102 
103  void set_first_temp_vreg(int first_temp_vreg) { m_first_temp_vreg = first_temp_vreg; }
104  int get_first_temp_vreg() const { return m_first_temp_vreg; }
105 
106  void set_num_vregs_used(int num_vregs_used) { m_num_vregs_used = num_vregs_used; }
107  int get_num_vregs_used() const { return m_num_vregs_used; }
108 
109  void assign_vreg_storage(int vreg, unsigned offset) {
110  assert(!vreg_reqires_storage(vreg));
111  m_vreg_storage_offsets[vreg] = offset;
112  }
113 
114  bool vreg_reqires_storage(int vreg) const {
115  return m_vreg_storage_offsets.count(vreg) > 0;
116  }
117 
118  unsigned get_vreg_storage_offset(int vreg) const {
119  auto i = m_vreg_storage_offsets.find(vreg);
120  assert(i != m_vreg_storage_offsets.end());
121  return i->second;
122  }
123 
124  void set_num_spill_locs(int num_spill_locs) { m_num_spill_locs = num_spill_locs; }
125  int get_num_spill_locs() const { return m_num_spill_locs; }
126 
127  void allocate_global_reg(int vreg, MachineReg mreg) {
128  assert(!has_global_reg(vreg));
129  m_global_regs[vreg] = mreg;
130  }
131 
132  bool has_global_reg(int vreg) const { return m_global_regs.count(vreg) > 0; }
133 
134  MachineReg get_global_reg(int vreg) const {
135  auto i = m_global_regs.find(vreg);
136  assert(i != m_global_regs.end());
137  return i->second;
138  }
139 
140  // Iterators over vreg to global mreg assignments
141  GlobalRegMap::const_iterator greg_begin() const { return m_global_regs.cbegin(); }
142  GlobalRegMap::const_iterator greg_end() const { return m_global_regs.cend(); }
143 
144  // Reverse iterators over vreg to global mreg assignments
145  GlobalRegMap::const_reverse_iterator greg_rbegin() const { return m_global_regs.crbegin(); }
146  GlobalRegMap::const_reverse_iterator greg_rend() const { return m_global_regs.crend(); }
147 
148  unsigned get_num_global_regs_assigned() const { return unsigned(m_global_regs.size()); }
149 #endif
150 };
151 
152 #endif // FUNCTION_H
Function encapsulates all of the information needed to generate and optimize code for a function.
Definition: function.h:42
void set_hl_iseq(const std::shared_ptr< InstructionSequence > &hl_iseq)
Set the high-level InstructionSequence.
Definition: function.cpp:55
void set_ll_iseq(const std::shared_ptr< InstructionSequence > &ll_iseq)
Set the low-level InstructionSequence.
Definition: function.cpp:63
std::string get_name() const
Get the function name.
Definition: function.cpp:39
Node * get_funcdef_ast() const
Get the function definition AST.
Definition: function.cpp:43
std::shared_ptr< InstructionSequence > get_ll_iseq() const
Get the low-level InstructionSequence.
Definition: function.cpp:59
Symbol * get_symbol() const
Get the function's symbol table entry.
Definition: function.cpp:47
std::shared_ptr< InstructionSequence > get_hl_iseq() const
Get the high-level InstructionSequence.
Definition: function.cpp:51
Function(const std::string &name, Node *funcdef_ast, Symbol *symbol)
Constructor.
Definition: function.cpp:23
Definition: node.h:35
Definition: symbol.h:28
InstructionSequence and friends.