Skip to content

Commit

Permalink
WIP progress :: add comments as TODOs to support LLVM IR generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodk committed Feb 28, 2021
1 parent 2f0b222 commit 2da2bc4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/codegen/llvm/codegen_llvm_helper_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ class CodegenLLVMHelperVisitor: public visitor::AstVisitor {
CodegenLLVMHelperVisitor(int vector_width)
: vector_width(vector_width){};

const InstanceVarHelper& get_instance_var_helper() {
return instance_var_helper;
}

/// run visitor and return code generation functions
CodegenFunctionVector get_codegen_functions(const ast::Program& node);

Expand Down
37 changes: 35 additions & 2 deletions src/codegen/llvm/codegen_llvm_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*************************************************************************/

#include "codegen/llvm/codegen_llvm_visitor.hpp"
#include "codegen/llvm/codegen_llvm_helper_visitor.hpp"

#include "ast/all.hpp"
#include "visitors/rename_visitor.hpp"
Expand Down Expand Up @@ -79,6 +78,8 @@ llvm::Type* CodegenLLVMVisitor::get_codegen_var_type(const ast::CodegenVarType&
return llvm::Type::getInt32Ty(*context);
case ast::AstNodeType::VOID:
return llvm::Type::getVoidTy(*context);
// TODO :: George/Ioannis : Here we have to also return INSTANCE_STRUCT type
// as it is used as an argument to nrn_state function
default:
throw std::runtime_error("Error: expecting a type in CodegenVarType node\n");
}
Expand Down Expand Up @@ -558,6 +559,11 @@ void CodegenLLVMVisitor::visit_program(const ast::Program& node) {
// and procedures is used only.
CodegenLLVMHelperVisitor v{vector_width};
const auto& functions = v.get_codegen_functions(node);
instance_var_helper = v.get_instance_var_helper();

// TODO :: George / Ioannis :: before emitting procedures, we have

This comment has been minimized.

Copy link
@georgemitenkov

georgemitenkov Mar 2, 2021

Collaborator

@pramodk @iomaganaris I do not think we actually need this. If the struct is a function argument (and not a global variable), then we emit the type as we process the function signature (i.e. in emit_procedure_or_function_declaration -> get_codegen_var_type -> get_instance_struct_type)

This comment has been minimized.

Copy link
@pramodk

pramodk Mar 2, 2021

Author Contributor

@georgemitenkov : Oh ok. If that works then its fine. What I was thinking is that you will need to know the full type definition before.

// to emmit INSTANCE_STRUCT type as it's used as an argument.
// Currently it's done in node.visit_children which is late.

// For every function, generate its declaration. Thus, we can look up
// `llvm::Function` in the symbol table in the module.
Expand Down Expand Up @@ -603,6 +609,16 @@ void CodegenLLVMVisitor::visit_var_name(const ast::VarName& node) {
if (!identifier->is_name() && !identifier->is_indexed_name())
throw std::runtime_error("Error: Unsupported variable type");

// TODO :: George :: here instance_var_helper can be used to query
// variable type and it's index into structure
auto name = node.get_node_name();

auto codegen_var_with_type = instance_var_helper.get_variable(name);
auto codegen_var_index = instance_var_helper.get_variable_index(name);
// this will be INTEGER or DOUBLE
auto var_type = codegen_var_with_type->get_type()->get_type();
auto is_pointer = codegen_var_with_type->get_is_pointer();

llvm::Value* ptr;
if (identifier->is_name())
ptr = lookup(node.get_node_name());
Expand All @@ -620,7 +636,24 @@ void CodegenLLVMVisitor::visit_var_name(const ast::VarName& node) {
void CodegenLLVMVisitor::visit_instance_struct(const ast::InstanceStruct& node) {
std::vector<llvm::Type*> members;
for (const auto& variable: node.get_codegen_vars()) {
members.push_back(get_default_fp_ptr_type());
// TODO :: Ioannis / George :: we have now double*, int*, double and int
// variables in the instance structure. Each variable is of type
// ast::CodegenVarWithType. So we can query variable type and if
// it's pointer.
auto is_pointer = variable->get_is_pointer();
auto type = variable->get_type()->get_type();

// todo : clean up ?
if (type == ast::AstNodeType::DOUBLE) {
auto llvm_type = is_pointer ? get_default_fp_ptr_type() : get_default_fp_type();
members.push_back(llvm_type);
} else {
if (is_pointer) {
members.push_back(llvm::Type::getInt32PtrTy(*context));
} else {
members.push_back(llvm::Type::getInt32Ty(*context));
}
}
}

llvm_struct = llvm::StructType::create(*context, mod_filename + "_Instance");
Expand Down
5 changes: 5 additions & 0 deletions src/codegen/llvm/codegen_llvm_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <ostream>
#include <string>

#include "codegen/llvm/codegen_llvm_helper_visitor.hpp"
#include "symtab/symbol_table.hpp"
#include "utils/logger.hpp"
#include "visitors/ast_visitor.hpp"
Expand Down Expand Up @@ -56,6 +57,9 @@ class CodegenLLVMVisitor: public visitor::ConstAstVisitor {
std::string output_dir;

private:

InstanceVarHelper instance_var_helper;

std::unique_ptr<llvm::LLVMContext> context = std::make_unique<llvm::LLVMContext>();

std::unique_ptr<llvm::Module> module = std::make_unique<llvm::Module>(mod_filename, *context);
Expand Down Expand Up @@ -264,6 +268,7 @@ class CodegenLLVMVisitor: public visitor::ConstAstVisitor {
void visit_program(const ast::Program& node) override;
void visit_unary_expression(const ast::UnaryExpression& node) override;
void visit_var_name(const ast::VarName& node) override;
void visit_codegen_instance_var(const ast::CodegenInstanceVar& node) override;
void visit_instance_struct(const ast::InstanceStruct& node) override;
void visit_while_statement(const ast::WhileStatement& node) override;

Expand Down

0 comments on commit 2da2bc4

Please sign in to comment.