Nearly CC
An educational compiler skeleton
highlevel_codegen.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 #include <string>
22 #include <memory>
23 #ifdef SOLUTION
24 #include <map>
25 #endif
26 #include "highlevel.h"
27 #include "instruction_seq.h"
28 #include "ast_visitor.h"
29 #include "options.h"
30 #include "function.h"
31 
36 class HighLevelCodegen : public ASTVisitor {
37 private:
38  const Options &m_options;
39  std::shared_ptr<Function> m_function;
40  int m_next_label_num;
41 #ifdef SOLUTION
42  int m_start_temp_vreg, m_next_temp_vreg;
43  int m_num_vregs_used;
44  std::map<std::string, int> m_strmap;
45 #endif
46  std::string m_return_label_name; // name of the label that return instructions should target
47 
48 public:
54  HighLevelCodegen(const Options &options, int next_label_num);
55 
56  virtual ~HighLevelCodegen();
57 
62  void generate(const std::shared_ptr<Function> &function);
63 
67  std::shared_ptr<InstructionSequence> get_hl_iseq() { return m_function->get_hl_iseq(); }
68 
71  int get_next_label_num() const { return m_next_label_num; }
72 
73  virtual void visit_function_definition(Node *n);
74  virtual void visit_statement_list(Node *n);
75  virtual void visit_expression_statement(Node *n);
76  virtual void visit_return_statement(Node *n);
77  virtual void visit_return_expression_statement(Node *n);
78  virtual void visit_while_statement(Node *n);
79  virtual void visit_do_while_statement(Node *n);
80  virtual void visit_for_statement(Node *n);
81  virtual void visit_if_statement(Node *n);
82  virtual void visit_if_else_statement(Node *n);
83  virtual void visit_binary_expression(Node *n);
84  virtual void visit_unary_expression(Node *n);
85  virtual void visit_function_call_expression(Node *n);
86  virtual void visit_field_ref_expression(Node *n);
89  virtual void visit_variable_ref(Node *n);
90  virtual void visit_literal_value(Node *n);
91  virtual void visit_implicit_conversion(Node *n);
92 
93 private:
94  std::string next_label();
95 #ifdef SOLUTION
96  int next_temp_vreg();
97  Operand determine_storage_location(Node *n);
98  Operand determine_storage_location(Symbol *sym);
99  Operand to_rvalue(Operand operand, const std::shared_ptr<Type> &type);
100  Operand convert_lvalue_to_pointer(Operand lval_op);
101  Operand get_indirect_field_ref(Operand ptr_to_struct, unsigned field_offset);
102  Operand promote_index(Node *index_expr);
103 #else
104  // TODO: additional private member functions
105 #endif
106 };
Base class for AST visitors.
Definition: ast_visitor.h:7
A HighLevelCodegen visitor generates high-level IR code for a single function definition.
Definition: highlevel_codegen.h:36
virtual void visit_function_call_expression(Node *n)
Visit a Node with the AST_FUNCTION_CALL_EXPRESSION tag value.
Definition: highlevel_codegen.cpp:489
virtual void visit_do_while_statement(Node *n)
Visit a Node with the AST_DO_WHILE_STATEMENT tag value.
Definition: highlevel_codegen.cpp:258
virtual void visit_return_expression_statement(Node *n)
Visit a Node with the AST_RETURN_EXPRESSION_STATEMENT tag value.
Definition: highlevel_codegen.cpp:200
virtual void visit_indirect_field_ref_expression(Node *n)
Visit a Node with the AST_INDIRECT_FIELD_REF_EXPRESSION tag value.
Definition: highlevel_codegen.cpp:573
virtual void visit_array_element_ref_expression(Node *n)
Visit a Node with the AST_ARRAY_ELEMENT_REF_EXPRESSION tag value.
Definition: highlevel_codegen.cpp:604
int get_next_label_num() const
Get the next unused control-flow label number.
Definition: highlevel_codegen.h:71
virtual void visit_literal_value(Node *n)
Visit a Node with the AST_LITERAL_VALUE tag value.
Definition: highlevel_codegen.cpp:669
virtual void visit_for_statement(Node *n)
Visit a Node with the AST_FOR_STATEMENT tag value.
Definition: highlevel_codegen.cpp:277
virtual void visit_if_statement(Node *n)
Visit a Node with the AST_IF_STATEMENT tag value.
Definition: highlevel_codegen.cpp:306
virtual void visit_field_ref_expression(Node *n)
Visit a Node with the AST_FIELD_REF_EXPRESSION tag value.
Definition: highlevel_codegen.cpp:535
virtual void visit_unary_expression(Node *n)
Visit a Node with the AST_UNARY_EXPRESSION tag value.
Definition: highlevel_codegen.cpp:424
void generate(const std::shared_ptr< Function > &function)
Create a high-level InstructionSequence from a function definition AST.
Definition: highlevel_codegen.cpp:97
virtual void visit_function_definition(Node *n)
Visit a Node with the AST_FUNCTION_DEFINITION tag value.
Definition: highlevel_codegen.cpp:112
virtual void visit_statement_list(Node *n)
Visit a Node with the AST_STATEMENT_LIST tag value.
Definition: highlevel_codegen.cpp:167
virtual void visit_return_statement(Node *n)
Visit a Node with the AST_RETURN_STATEMENT tag value.
Definition: highlevel_codegen.cpp:191
virtual void visit_binary_expression(Node *n)
Visit a Node with the AST_BINARY_EXPRESSION tag value.
Definition: highlevel_codegen.cpp:351
virtual void visit_if_else_statement(Node *n)
Visit a Node with the AST_IF_ELSE_STATEMENT tag value.
Definition: highlevel_codegen.cpp:325
virtual void visit_variable_ref(Node *n)
Visit a Node with the AST_VARIABLE_REF tag value.
Definition: highlevel_codegen.cpp:656
HighLevelCodegen(const Options &options, int next_label_num)
Constructor.
Definition: highlevel_codegen.cpp:83
virtual void visit_expression_statement(Node *n)
Visit a Node with the AST_EXPRESSION_STATEMENT tag value.
Definition: highlevel_codegen.cpp:180
virtual void visit_while_statement(Node *n)
Visit a Node with the AST_WHILE_STATEMENT tag value.
Definition: highlevel_codegen.cpp:234
std::shared_ptr< InstructionSequence > get_hl_iseq()
Get a shared pointer to the high-level InstructionSequence containing the generated code.
Definition: highlevel_codegen.h:67
virtual void visit_implicit_conversion(Node *n)
Visit a Node with the AST_IMPLICIT_CONVERSION tag value.
Definition: highlevel_codegen.cpp:729
Definition: node.h:35
Definition: operand.h:31
Definition: options.h:47
Definition: symbol.h:28
HighLevelOpcode enumeration and associated helper functions and the HighLevelInstructionProperties cl...
InstructionSequence and friends.