IR Generation#

namespace stapl
namespace ir#

Classes related to LLVM IR generation.

class IRGen#
#include <irgen.h>

A visitor for generating LLVM IR.

Public Functions

IRGen()#

Default constructor.

void codegen(ast::Module &module_node)#

Generate IR for a module.

Parameters:

module_node – The module to generate IR for.

void write_ir(std::ostream &os)#

Write the generated IR to a stream.

Parameters:

os – The std::ostream to write to.

llvm::Value *operator()(ast::LiteralExprNode<int> &node)#

Generate IR for integer literal.

Parameters:

node – The node to generate IR for.

Returns:

The generated IR.

llvm::Value *operator()(ast::LiteralExprNode<double> &node)#

Generate IR for float literal.

Parameters:

node – The node to generate IR for.

Returns:

The generated IR.

llvm::Value *operator()(ast::LiteralExprNode<bool> &node)#

Generate IR for boolean literal.

Parameters:

node – The node to generate IR for.

Returns:

The generated IR.

llvm::Value *operator()(ast::VariableExprNode &node)#

Generate IR for variable expression node.

Parameters:

node – The node to generate IR for.

Returns:

The generated IR.

llvm::Value *operator()(std::unique_ptr<ast::UnaryExprNode> &node)#

Generate IR for unary expression node.

Parameters:

node – The node to generate IR for.

Returns:

The generated IR.

llvm::Value *operator()(std::unique_ptr<ast::BinaryExprNode> &node)#

Generate IR for binary expression node.

Parameters:

node – The node to generate IR for.

Returns:

The generated IR.

llvm::Value *operator()(std::unique_ptr<ast::CallExprNode> &node)#

Generate IR for call expression node.

Parameters:

node – The node to generate IR for.

Returns:

The generated IR.

void operator()(ast::LetStmtNode &node)#

Generate IR for let statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(ast::AssignmentStmtNode &node)#

Generate IR for assignment statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(std::unique_ptr<ast::IfStmtNode> &node)#

Generate IR for if statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(std::unique_ptr<ast::WhileStmtNode> &node)#

Generate IR for while statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(ast::BreakStmtNode &node)#

Generate IR for break statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(ast::ContinueStmtNode &node)#

Generate IR for continue statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(ast::ReturnStmtNode &node)#

Generate IR for return statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(std::unique_ptr<ast::CompoundStmtNode> &node)#

Generate IR for compound statement node and add to current block.

Parameters:

node – The node to generate IR for.

void operator()(ast::FunctionDeclNode &node)#

Generate IR for function statement node and add to current block.

Parameters:

node – The node to generate IR for.

Private Functions

llvm::Value *unary_op_pos(llvm::Value *rhs_val)#

Generate IR for positive prefix operation of llvm::Value *.

Note

This is basically a no-op, but it can be overloaded in the future.

Parameters:

rhs_val – The value to negate.

Returns:

The result of the negation.

llvm::Value *unary_op_neg(llvm::Value *rhs_val)#

Generate IR for integer negation of llvm::Value *.

Parameters:

rhs_val – The value to negate.

Returns:

The result of the negation.

llvm::Value *unary_op_not(llvm::Value *rhs_val)#

Generate IR for boolean negation of llvm::Value *.

Parameters:

rhs_val – The value to negate.

Returns:

The result of the negation.

llvm::Value *binary_op_add(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for addition of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the addition.

llvm::Value *binary_op_sub(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for subtraction of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the subtraction.

llvm::Value *binary_op_mul(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for multiplication of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the multiplication.

llvm::Value *binary_op_div(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for division of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the division.

llvm::Value *binary_op_mod(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for modulo of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the modulo.

llvm::Value *binary_op_eq(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for equality of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the modulo.

llvm::Value *binary_op_neq(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for inequality of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the modulo.

llvm::Value *binary_op_lt(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for less-than comparision of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the modulo.

llvm::Value *binary_op_gt(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for greater-than comparision of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the modulo.

llvm::Value *binary_op_le(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for less-than-or-equal comparision of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the modulo.

llvm::Value *binary_op_ge(llvm::Value *lhs_val, llvm::Value *rhs_val)#

Gernerate IR for greater-than-or-equal comparision of two llvm::Value *.

Parameters:
  • lhs_val – The value on LHS.

  • rhs_val – The value on RHS.

Returns:

The result of the modulo.

llvm::AllocaInst *create_entry_block_alloc(llvm::Function *func, llvm::StringRef name, llvm::Type *type)#

Create an alloca instruction in the entry block of the function.

This function is needed to create IR that can be optimized by LLVM mem2reg optimization pass.

Parameters:
  • func – The function to create the alloca in.

  • name – The name of the alloca.

  • type – The LLVM type of the alloca.

Returns:

The created alloca instruction.

llvm::Type *type_from_typename(const std::string &name)#

Get the LLVM type from a type name.

Parameters:

name – The name of the type.

Returns:

The LLVM type.

Private Members

std::unique_ptr<llvm::LLVMContext> context#

The LLVM context.

std::unique_ptr<llvm::Module> module#

The LLVM module.

std::unique_ptr<llvm::IRBuilder<>> builder#

The LLVM IR builder.

std::unordered_map<std::string, llvm::AllocaInst*> current_scope_symbols = {}#

The current scope’s symbols.

llvm::BasicBlock *current_loop_cond = nullptr#

The cond block of the current loop.

llvm::BasicBlock *current_loop_merge = nullptr#

The merge block of the current loop.