Skip to content

Commit

Permalink
Strings generation (#56 from wesuRage/main)
Browse files Browse the repository at this point in the history
Strings generation
  • Loading branch information
wesuRage authored Dec 13, 2024
2 parents 68e511a + 8cdb334 commit 3969de2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/backend/generator/expressions/generate_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef GENERATE_STRING_LITERAL_H
#define GENERATE_STRING_LITERAL_H

extern "C" {
#include "frontend/ast/definitions.h"
}
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/IRBuilder.h>
#include "llvm/IR/Module.h"

llvm::Value *generate_string_literal(StringNode *string_node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module);

#endif // GENERATE_STRING_LITERAL_H
5 changes: 5 additions & 0 deletions src/backend/generator/expressions/generate_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
#include "backend/generator/expressions/generate_pre_decrement.hpp"
#include "backend/generator/expressions/generate_assignment_expr.hpp"
#include "backend/generator/expressions/generate_call.hpp"
#include "backend/generator/expressions/generate_string.hpp"

llvm::Value *generate_expr(AstNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) {
// Checks the node kind, casts the node data into
// the perspective node type and then generates it.
switch (node->kind) {
case NODE_STRING: {
StringNode *string_node = (StringNode *)node->data;
return generate_string_literal(string_node, Context, Builder, Module);
}
case NODE_NUMERIC_LITERAL: {
NumericLiteralNode *num_node = (NumericLiteralNode *)node->data;
return generate_numeric_literal(num_node, Context);
Expand Down
33 changes: 33 additions & 0 deletions src/backend/generator/expressions/generate_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "backend/generator/expressions/generate_string.hpp"

llvm::Value *generate_string_literal(StringNode *string_node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) {
// Ensure that the StringNode and the string data are valid
if (!string_node || !string_node->string) {
throw std::runtime_error("Invalid string node: missing string value.");
}

// This represents the string as a constant array of characters in LLVM
llvm::Constant *string_constant = llvm::ConstantDataArray::getString(Context, string_node->string, true);

// This global variable will hold the constant array
llvm::GlobalVariable *global_string = new llvm::GlobalVariable(
Module, // The module to which this global variable belongs
string_constant->getType(), // The type of the string constant (constant array of chars)
true, // Whether the variable is constant
llvm::GlobalValue::PrivateLinkage, // The linkage type (private to the module)
string_constant, // The actual constant value of the string
".str" // The name of the global variable (used for debugging and identification)
);

// We use `getGetElementPtr` to get a pointer to the start of the array (the first character)
llvm::Constant *zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 0); // The index for the first element of the array
llvm::Constant *indices[] = {zero, zero}; // Indices for the GetElementPtr operation
llvm::Constant *string_ptr = llvm::ConstantExpr::getGetElementPtr(
string_constant->getType(), // The type of the string constant (constant array)
global_string, // The global variable holding the string
indices // The indices to access the first character
);

// This pointer can now be used wherever a pointer to the string is needed in the generated IR
return string_ptr;
}

0 comments on commit 3969de2

Please sign in to comment.