Nearly CC
An educational compiler skeleton
Public Member Functions | List of all members
HighLevelCodegen Class Reference

A HighLevelCodegen visitor generates high-level IR code for a single function definition. More...

#include "highlevel_codegen.h"

Inheritance diagram for HighLevelCodegen:
Inheritance graph
[legend]
Collaboration diagram for HighLevelCodegen:
Collaboration graph
[legend]

Public Member Functions

 HighLevelCodegen (const Options &options, int next_label_num)
 Constructor. More...
 
void generate (const std::shared_ptr< Function > &function)
 Create a high-level InstructionSequence from a function definition AST. More...
 
std::shared_ptr< InstructionSequenceget_hl_iseq ()
 Get a shared pointer to the high-level InstructionSequence containing the generated code. More...
 
int get_next_label_num () const
 Get the next unused control-flow label number. More...
 
virtual void visit_function_definition (Node *n)
 Visit a Node with the AST_FUNCTION_DEFINITION tag value. More...
 
virtual void visit_statement_list (Node *n)
 Visit a Node with the AST_STATEMENT_LIST tag value. More...
 
virtual void visit_expression_statement (Node *n)
 Visit a Node with the AST_EXPRESSION_STATEMENT tag value. More...
 
virtual void visit_return_statement (Node *n)
 Visit a Node with the AST_RETURN_STATEMENT tag value. More...
 
virtual void visit_return_expression_statement (Node *n)
 Visit a Node with the AST_RETURN_EXPRESSION_STATEMENT tag value. More...
 
virtual void visit_while_statement (Node *n)
 Visit a Node with the AST_WHILE_STATEMENT tag value. More...
 
virtual void visit_do_while_statement (Node *n)
 Visit a Node with the AST_DO_WHILE_STATEMENT tag value. More...
 
virtual void visit_for_statement (Node *n)
 Visit a Node with the AST_FOR_STATEMENT tag value. More...
 
virtual void visit_if_statement (Node *n)
 Visit a Node with the AST_IF_STATEMENT tag value. More...
 
virtual void visit_if_else_statement (Node *n)
 Visit a Node with the AST_IF_ELSE_STATEMENT tag value. More...
 
virtual void visit_binary_expression (Node *n)
 Visit a Node with the AST_BINARY_EXPRESSION tag value. More...
 
virtual void visit_unary_expression (Node *n)
 Visit a Node with the AST_UNARY_EXPRESSION tag value. More...
 
virtual void visit_function_call_expression (Node *n)
 Visit a Node with the AST_FUNCTION_CALL_EXPRESSION tag value. More...
 
virtual void visit_field_ref_expression (Node *n)
 Visit a Node with the AST_FIELD_REF_EXPRESSION tag value. More...
 
virtual void visit_indirect_field_ref_expression (Node *n)
 Visit a Node with the AST_INDIRECT_FIELD_REF_EXPRESSION tag value. More...
 
virtual void visit_array_element_ref_expression (Node *n)
 Visit a Node with the AST_ARRAY_ELEMENT_REF_EXPRESSION tag value. More...
 
virtual void visit_variable_ref (Node *n)
 Visit a Node with the AST_VARIABLE_REF tag value. More...
 
virtual void visit_literal_value (Node *n)
 Visit a Node with the AST_LITERAL_VALUE tag value. More...
 
virtual void visit_implicit_conversion (Node *n)
 Visit a Node with the AST_IMPLICIT_CONVERSION tag value. More...
 
virtual void visit (Node *n)
 Visit given AST Node. More...
 
virtual void visit_unit (Node *n)
 Visit a Node with the AST_UNIT tag value. More...
 
virtual void visit_variable_declaration (Node *n)
 Visit a Node with the AST_VARIABLE_DECLARATION tag value. More...
 
virtual void visit_struct_type (Node *n)
 Visit a Node with the AST_STRUCT_TYPE tag value. More...
 
virtual void visit_union_type (Node *n)
 Visit a Node with the AST_UNION_TYPE tag value. More...
 
virtual void visit_basic_type (Node *n)
 Visit a Node with the AST_BASIC_TYPE tag value. More...
 
virtual void visit_declarator_list (Node *n)
 Visit a Node with the AST_DECLARATOR_LIST tag value. More...
 
virtual void visit_named_declarator (Node *n)
 Visit a Node with the AST_NAMED_DECLARATOR tag value. More...
 
virtual void visit_pointer_declarator (Node *n)
 Visit a Node with the AST_POINTER_DECLARATOR tag value. More...
 
virtual void visit_array_declarator (Node *n)
 Visit a Node with the AST_ARRAY_DECLARATOR tag value. More...
 
virtual void visit_function_declaration (Node *n)
 Visit a Node with the AST_FUNCTION_DECLARATION tag value. More...
 
virtual void visit_function_parameter_list (Node *n)
 Visit a Node with the AST_FUNCTION_PARAMETER_LIST tag value. More...
 
virtual void visit_function_parameter (Node *n)
 Visit a Node with the AST_FUNCTION_PARAMETER tag value. More...
 
virtual void visit_empty_statement (Node *n)
 Visit a Node with the AST_EMPTY_STATEMENT tag value. More...
 
virtual void visit_struct_type_definition (Node *n)
 Visit a Node with the AST_STRUCT_TYPE_DEFINITION tag value. More...
 
virtual void visit_union_type_definition (Node *n)
 Visit a Node with the AST_UNION_TYPE_DEFINITION tag value. More...
 
virtual void visit_field_definition_list (Node *n)
 Visit a Node with the AST_FIELD_DEFINITION_LIST tag value. More...
 
virtual void visit_postfix_expression (Node *n)
 Visit a Node with the AST_POSTFIX_EXPRESSION tag value. More...
 
virtual void visit_conditional_expression (Node *n)
 Visit a Node with the AST_CONDITIONAL_EXPRESSION tag value. More...
 
virtual void visit_cast_expression (Node *n)
 Visit a Node with the AST_CAST_EXPRESSION tag value. More...
 
virtual void visit_argument_expression_list (Node *n)
 Visit a Node with the AST_ARGUMENT_EXPRESSION_LIST tag value. More...
 
virtual void visit_children (Node *n)
 Call visit on each child Node of the given parent Node. More...
 
virtual void visit_token (Node *n)
 This method is called if the Node being visited is a token (terminal symbol). More...
 

Detailed Description

A HighLevelCodegen visitor generates high-level IR code for a single function definition.

Code generation is initiated by calling the generate() member function, which in turn visits the function definition AST node.

Constructor & Destructor Documentation

◆ HighLevelCodegen()

HighLevelCodegen::HighLevelCodegen ( const Options options,
int  next_label_num 
)

Constructor.

Parameters
optionsthe command-line Options
next_label_numthe next value that should be used when generating a control-flow label (this is used to ensure that control-flow labels aren't reused between functions)

Member Function Documentation

◆ generate()

void HighLevelCodegen::generate ( const std::shared_ptr< Function > &  function)

Create a high-level InstructionSequence from a function definition AST.

The resulting InstructionSequence should be stored in the Function object.

Parameters
functionshared pointer to the Function object

◆ get_hl_iseq()

std::shared_ptr<InstructionSequence> HighLevelCodegen::get_hl_iseq ( )
inline

Get a shared pointer to the high-level InstructionSequence containing the generated code.

Returns
shared pointer to the high-level InstructionSequence

◆ get_next_label_num()

int HighLevelCodegen::get_next_label_num ( ) const
inline

Get the next unused control-flow label number.

Returns
the next unused control-flow label number

◆ visit()

void ASTVisitor::visit ( Node n)
virtualinherited

Visit given AST Node.

This will result in a call to an appropriate visitation member function depending on the Node object's tag value. For example, if the tag value is AST_STATEMENT_LIST, the visit_statement_list member function will be called.

Parameters
nthe Node to visit

◆ visit_argument_expression_list()

void ASTVisitor::visit_argument_expression_list ( Node n)
virtualinherited

Visit a Node with the AST_ARGUMENT_EXPRESSION_LIST tag value.

Parameters
na Node with the AST_ARGUMENT_EXPRESSION_LIST tag value

◆ visit_array_declarator()

void ASTVisitor::visit_array_declarator ( Node n)
virtualinherited

Visit a Node with the AST_ARRAY_DECLARATOR tag value.

Parameters
na Node with the AST_ARRAY_DECLARATOR tag value

◆ visit_array_element_ref_expression()

void HighLevelCodegen::visit_array_element_ref_expression ( Node n)
virtual

Visit a Node with the AST_ARRAY_ELEMENT_REF_EXPRESSION tag value.

Parameters
na Node with the AST_ARRAY_ELEMENT_REF_EXPRESSION tag value

Reimplemented from ASTVisitor.

◆ visit_basic_type()

void ASTVisitor::visit_basic_type ( Node n)
virtualinherited

Visit a Node with the AST_BASIC_TYPE tag value.

Parameters
na Node with the AST_BASIC_TYPE tag value

Reimplemented in SemanticAnalysis.

◆ visit_binary_expression()

void HighLevelCodegen::visit_binary_expression ( Node n)
virtual

Visit a Node with the AST_BINARY_EXPRESSION tag value.

Parameters
na Node with the AST_BINARY_EXPRESSION tag value

Reimplemented from ASTVisitor.

◆ visit_cast_expression()

void ASTVisitor::visit_cast_expression ( Node n)
virtualinherited

Visit a Node with the AST_CAST_EXPRESSION tag value.

Parameters
na Node with the AST_CAST_EXPRESSION tag value

Reimplemented in SemanticAnalysis.

◆ visit_children()

void ASTVisitor::visit_children ( Node n)
virtualinherited

Call visit on each child Node of the given parent Node.

Parameters
nthe parent Node whose children should be visited

◆ visit_conditional_expression()

void ASTVisitor::visit_conditional_expression ( Node n)
virtualinherited

Visit a Node with the AST_CONDITIONAL_EXPRESSION tag value.

Parameters
na Node with the AST_CONDITIONAL_EXPRESSION tag value

Reimplemented in SemanticAnalysis.

◆ visit_declarator_list()

void ASTVisitor::visit_declarator_list ( Node n)
virtualinherited

Visit a Node with the AST_DECLARATOR_LIST tag value.

Parameters
na Node with the AST_DECLARATOR_LIST tag value

Reimplemented in LocalStorageAllocation.

◆ visit_do_while_statement()

void HighLevelCodegen::visit_do_while_statement ( Node n)
virtual

Visit a Node with the AST_DO_WHILE_STATEMENT tag value.

Parameters
na Node with the AST_DO_WHILE_STATEMENT tag value

Reimplemented from ASTVisitor.

◆ visit_empty_statement()

void ASTVisitor::visit_empty_statement ( Node n)
virtualinherited

Visit a Node with the AST_EMPTY_STATEMENT tag value.

Parameters
na Node with the AST_EMPTY_STATEMENT tag value

◆ visit_expression_statement()

void HighLevelCodegen::visit_expression_statement ( Node n)
virtual

Visit a Node with the AST_EXPRESSION_STATEMENT tag value.

Parameters
na Node with the AST_EXPRESSION_STATEMENT tag value

Reimplemented from ASTVisitor.

◆ visit_field_definition_list()

void ASTVisitor::visit_field_definition_list ( Node n)
virtualinherited

Visit a Node with the AST_FIELD_DEFINITION_LIST tag value.

Parameters
na Node with the AST_FIELD_DEFINITION_LIST tag value

◆ visit_field_ref_expression()

void HighLevelCodegen::visit_field_ref_expression ( Node n)
virtual

Visit a Node with the AST_FIELD_REF_EXPRESSION tag value.

Parameters
na Node with the AST_FIELD_REF_EXPRESSION tag value

Reimplemented from ASTVisitor.

◆ visit_for_statement()

void HighLevelCodegen::visit_for_statement ( Node n)
virtual

Visit a Node with the AST_FOR_STATEMENT tag value.

Parameters
na Node with the AST_FOR_STATEMENT tag value

Reimplemented from ASTVisitor.

◆ visit_function_call_expression()

void HighLevelCodegen::visit_function_call_expression ( Node n)
virtual

Visit a Node with the AST_FUNCTION_CALL_EXPRESSION tag value.

Parameters
na Node with the AST_FUNCTION_CALL_EXPRESSION tag value

Reimplemented from ASTVisitor.

◆ visit_function_declaration()

void ASTVisitor::visit_function_declaration ( Node n)
virtualinherited

Visit a Node with the AST_FUNCTION_DECLARATION tag value.

Parameters
na Node with the AST_FUNCTION_DECLARATION tag value

Reimplemented in SemanticAnalysis.

◆ visit_function_definition()

void HighLevelCodegen::visit_function_definition ( Node n)
virtual

Visit a Node with the AST_FUNCTION_DEFINITION tag value.

Parameters
na Node with the AST_FUNCTION_DEFINITION tag value

Reimplemented from ASTVisitor.

◆ visit_function_parameter()

void ASTVisitor::visit_function_parameter ( Node n)
virtualinherited

Visit a Node with the AST_FUNCTION_PARAMETER tag value.

Parameters
na Node with the AST_FUNCTION_PARAMETER tag value

Reimplemented in SemanticAnalysis, and LocalStorageAllocation.

◆ visit_function_parameter_list()

void ASTVisitor::visit_function_parameter_list ( Node n)
virtualinherited

Visit a Node with the AST_FUNCTION_PARAMETER_LIST tag value.

Parameters
na Node with the AST_FUNCTION_PARAMETER_LIST tag value

◆ visit_if_else_statement()

void HighLevelCodegen::visit_if_else_statement ( Node n)
virtual

Visit a Node with the AST_IF_ELSE_STATEMENT tag value.

Parameters
na Node with the AST_IF_ELSE_STATEMENT tag value

Reimplemented from ASTVisitor.

◆ visit_if_statement()

void HighLevelCodegen::visit_if_statement ( Node n)
virtual

Visit a Node with the AST_IF_STATEMENT tag value.

Parameters
na Node with the AST_IF_STATEMENT tag value

Reimplemented from ASTVisitor.

◆ visit_implicit_conversion()

void HighLevelCodegen::visit_implicit_conversion ( Node n)
virtual

Visit a Node with the AST_IMPLICIT_CONVERSION tag value.

Parameters
na Node with the AST_IMPLICIT_CONVERSION tag value

Reimplemented from ASTVisitor.

◆ visit_indirect_field_ref_expression()

void HighLevelCodegen::visit_indirect_field_ref_expression ( Node n)
virtual

Visit a Node with the AST_INDIRECT_FIELD_REF_EXPRESSION tag value.

Parameters
na Node with the AST_INDIRECT_FIELD_REF_EXPRESSION tag value

Reimplemented from ASTVisitor.

◆ visit_literal_value()

void HighLevelCodegen::visit_literal_value ( Node n)
virtual

Visit a Node with the AST_LITERAL_VALUE tag value.

Parameters
na Node with the AST_LITERAL_VALUE tag value

Reimplemented from ASTVisitor.

◆ visit_named_declarator()

void ASTVisitor::visit_named_declarator ( Node n)
virtualinherited

Visit a Node with the AST_NAMED_DECLARATOR tag value.

Parameters
na Node with the AST_NAMED_DECLARATOR tag value

◆ visit_pointer_declarator()

void ASTVisitor::visit_pointer_declarator ( Node n)
virtualinherited

Visit a Node with the AST_POINTER_DECLARATOR tag value.

Parameters
na Node with the AST_POINTER_DECLARATOR tag value

◆ visit_postfix_expression()

void ASTVisitor::visit_postfix_expression ( Node n)
virtualinherited

Visit a Node with the AST_POSTFIX_EXPRESSION tag value.

Parameters
na Node with the AST_POSTFIX_EXPRESSION tag value

Reimplemented in SemanticAnalysis.

◆ visit_return_expression_statement()

void HighLevelCodegen::visit_return_expression_statement ( Node n)
virtual

Visit a Node with the AST_RETURN_EXPRESSION_STATEMENT tag value.

Parameters
na Node with the AST_RETURN_EXPRESSION_STATEMENT tag value

Reimplemented from ASTVisitor.

◆ visit_return_statement()

void HighLevelCodegen::visit_return_statement ( Node n)
virtual

Visit a Node with the AST_RETURN_STATEMENT tag value.

Parameters
na Node with the AST_RETURN_STATEMENT tag value

Reimplemented from ASTVisitor.

◆ visit_statement_list()

void HighLevelCodegen::visit_statement_list ( Node n)
virtual

Visit a Node with the AST_STATEMENT_LIST tag value.

Parameters
na Node with the AST_STATEMENT_LIST tag value

Reimplemented from ASTVisitor.

◆ visit_struct_type()

void ASTVisitor::visit_struct_type ( Node n)
virtualinherited

Visit a Node with the AST_STRUCT_TYPE tag value.

Parameters
na Node with the AST_STRUCT_TYPE tag value

Reimplemented in SemanticAnalysis.

◆ visit_struct_type_definition()

void ASTVisitor::visit_struct_type_definition ( Node n)
virtualinherited

Visit a Node with the AST_STRUCT_TYPE_DEFINITION tag value.

Parameters
na Node with the AST_STRUCT_TYPE_DEFINITION tag value

Reimplemented in SemanticAnalysis, and LocalStorageAllocation.

◆ visit_token()

void ASTVisitor::visit_token ( Node n)
virtualinherited

This method is called if the Node being visited is a token (terminal symbol).

Parameters
nthe token (terminal symbol) Node

◆ visit_unary_expression()

void HighLevelCodegen::visit_unary_expression ( Node n)
virtual

Visit a Node with the AST_UNARY_EXPRESSION tag value.

Parameters
na Node with the AST_UNARY_EXPRESSION tag value

Reimplemented from ASTVisitor.

◆ visit_union_type()

void ASTVisitor::visit_union_type ( Node n)
virtualinherited

Visit a Node with the AST_UNION_TYPE tag value.

Parameters
na Node with the AST_UNION_TYPE tag value

Reimplemented in SemanticAnalysis.

◆ visit_union_type_definition()

void ASTVisitor::visit_union_type_definition ( Node n)
virtualinherited

Visit a Node with the AST_UNION_TYPE_DEFINITION tag value.

Parameters
na Node with the AST_UNION_TYPE_DEFINITION tag value

◆ visit_unit()

void ASTVisitor::visit_unit ( Node n)
virtualinherited

Visit a Node with the AST_UNIT tag value.

Parameters
na Node with the AST_UNIT tag value

◆ visit_variable_declaration()

void ASTVisitor::visit_variable_declaration ( Node n)
virtualinherited

Visit a Node with the AST_VARIABLE_DECLARATION tag value.

Parameters
na Node with the AST_VARIABLE_DECLARATION tag value

Reimplemented in SemanticAnalysis.

◆ visit_variable_ref()

void HighLevelCodegen::visit_variable_ref ( Node n)
virtual

Visit a Node with the AST_VARIABLE_REF tag value.

Parameters
na Node with the AST_VARIABLE_REF tag value

Reimplemented from ASTVisitor.

◆ visit_while_statement()

void HighLevelCodegen::visit_while_statement ( Node n)
virtual

Visit a Node with the AST_WHILE_STATEMENT tag value.

Parameters
na Node with the AST_WHILE_STATEMENT tag value

Reimplemented from ASTVisitor.


The documentation for this class was generated from the following files: