Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion software/luna/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "token.h"
#include "llvm-c/Core.h"

const char *VERSION = "0.1.3";
const char *VERSION = "0.1.4+d";

struct LunaString read_file(const char *path, struct ArenaAllocator *alloc) {
FILE *file = fopen(path, "rb");
Expand Down
38 changes: 17 additions & 21 deletions software/luna/src/llvm-backend/code_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,13 @@ bool cg_function_needs_struct_return_type(struct CodeGenerator *code_generator,
cg_get_type(code_generator, return_type)) > 16;
}

void cg_add_sret_to_call(LLVMValueRef call, LLVMTypeRef return_type){
unsigned sretKind =
LLVMGetEnumAttributeKindForName("sret", strlen("sret"));
void cg_add_sret_to_call(LLVMValueRef call, LLVMTypeRef return_type) {
unsigned sretKind = LLVMGetEnumAttributeKindForName("sret", strlen("sret"));

LLVMAttributeRef sretAttr = LLVMCreateTypeAttribute(
LLVMGetGlobalContext(), sretKind, return_type);
LLVMAttributeRef sretAttr =
LLVMCreateTypeAttribute(LLVMGetGlobalContext(), sretKind, return_type);

LLVMAddCallSiteAttribute(call, 1, sretAttr);
LLVMAddCallSiteAttribute(call, 1, sretAttr);
}

LLVMValueRef cg_visit_function_call(struct CodeGenerator *code_generator,
Expand Down Expand Up @@ -414,21 +413,22 @@ LLVMValueRef cg_visit_function_call(struct CodeGenerator *code_generator,

printf("arg count: %zu\n", argument_count);

LLVMValueRef call = LLVMBuildCall2(
code_generator->builder, type, value, arguments, argument_count, "");
LLVMValueRef call = LLVMBuildCall2(code_generator->builder, type, value,
arguments, argument_count, "");
puts("built call.");
if (is_struct_return_type) {
cg_add_sret_to_call(call,struct_return_type);
cg_add_sret_to_call(call, struct_return_type);
return LLVMBuildLoad2(code_generator->builder, struct_return_type,
struct_return_value, "");
}
return call;
} else {
if (is_struct_return_type) {
LLVMValueRef params[] = {struct_return_value};
LLVMValueRef call = LLVMBuildCall2(code_generator->builder, type, value, params, 1, "");
LLVMValueRef call =
LLVMBuildCall2(code_generator->builder, type, value, params, 1, "");

cg_add_sret_to_call(call,struct_return_type);
cg_add_sret_to_call(call, struct_return_type);

return LLVMBuildLoad2(code_generator->builder, struct_return_type,
struct_return_value, "");
Expand All @@ -438,7 +438,6 @@ LLVMValueRef cg_visit_function_call(struct CodeGenerator *code_generator,
}
}


// TODO/NOTE: this is an awful hack because the annotator infer_type uses the
// annotators current symbol table. Given code gen runs after all annotation,
// the annotator will likely have some root module as its current symbol table.
Expand Down Expand Up @@ -642,14 +641,11 @@ cg_visit_struct_init_expression(struct CodeGenerator *code_generator,
bool is_struct =
!entry->type->value.structure_definition.definition->is_union;

LLVMValueRef field_ptr;
if (field_init != NULL) {
if (!is_struct) {
indicies[1] = indicies[0];
}
field_ptr = LLVMBuildGEP2(code_generator->builder, struct_type,
local_struct, indicies, 2, "");
if (!is_struct) {
indicies[1] = indicies[0];
}
LLVMValueRef field_ptr = LLVMBuildGEP2(code_generator->builder, struct_type,
local_struct, indicies, 2, "");

printf("[cg_visit_struct_init_expression] is struct: %d\n", is_struct);

Expand All @@ -659,15 +655,15 @@ cg_visit_struct_init_expression(struct CodeGenerator *code_generator,
LLVMABISizeOfType(code_generator->target_data, field_type);
coereced = LLVMConstInt(LLVMIntType(size * 8), 0, false);

puts("Building store..");
puts("Building zerod store..");
LLVMBuildStore(code_generator->builder, coereced, field_ptr);
}
} else {
coereced = cg_coerce(
code_generator, cg_visit_expr(code_generator, field_init->expression),
field_type);

puts("Building store..");
puts("Building actual store..");
LLVMBuildStore(code_generator->builder, coereced, field_ptr);
}

Expand Down