added default values for composites
This commit is contained in:
parent
6967770d0e
commit
3b78d117b5
|
@ -48,6 +48,8 @@ static BackendError llvm_backend_codegen(const AST_NODE_PTR module_node, void**)
|
|||
GemstoneDeclRef decl = ((GemstoneDeclRef*) decls->data)[i];
|
||||
type_scope_add_variable(global_scope, decl);
|
||||
}
|
||||
// TODO: create LLVMValueRef of global/static variables
|
||||
|
||||
break;
|
||||
default:
|
||||
PANIC("NOT IMPLEMENTED");
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#include "llvm/types/scope.h"
|
||||
#include "llvm/types/structs.h"
|
||||
#include <llvm-c/Types.h>
|
||||
#include <llvm/types/type.h>
|
||||
#include <ast/ast.h>
|
||||
#include <llvm/decl/variable.h>
|
||||
#include <sys/log.h>
|
||||
#include <stdlib.h>
|
||||
#include <llvm-c/Core.h>
|
||||
|
||||
static StorageQualifier get_storage_qualifier_from_ast(AST_NODE_PTR storageQualifierNode) {
|
||||
if (storageQualifierNode->kind != AST_Storage) {
|
||||
|
@ -34,7 +37,7 @@ GArray* declaration_from_ast(TypeScopeRef scope, const AST_NODE_PTR node) {
|
|||
|
||||
AST_NODE_PTR first_child = AST_get_node(node, 0);
|
||||
|
||||
StorageQualifier qualifier = StorageQualifierLocal;
|
||||
StorageQualifier qualifier = StorageQualifierStatic;
|
||||
GemstoneTypeRef type = NULL;
|
||||
AST_NODE_PTR list = NULL;
|
||||
|
||||
|
@ -63,3 +66,27 @@ GArray* declaration_from_ast(TypeScopeRef scope, const AST_NODE_PTR node) {
|
|||
|
||||
return decls;
|
||||
}
|
||||
|
||||
LLVMValueRef create_declaration_reference(LLVMModuleRef module, LLVMBuilderRef builder, GemstoneDeclRef decl) {
|
||||
LLVMContextRef context = LLVMGetModuleContext(module);
|
||||
LLVMTypeRef llvmTypeRef = llvm_type_from_gemstone_type(context, decl->type);
|
||||
LLVMValueRef defaultValue = llvm_default_value_of_type(context, decl->type);
|
||||
LLVMValueRef variable = NULL;
|
||||
|
||||
switch(decl->storageQualifier) {
|
||||
case StorageQualifierLocal:
|
||||
variable = LLVMBuildAlloca(builder, llvmTypeRef, decl->name);
|
||||
LLVMBuildStore(builder, defaultValue, variable);
|
||||
break;
|
||||
case StorageQualifierStatic:
|
||||
// add global
|
||||
variable = LLVMAddGlobal(module, llvmTypeRef, decl->name);
|
||||
LLVMSetInitializer(variable, defaultValue);
|
||||
break;
|
||||
case StorageQualifierGlobal:
|
||||
PANIC("Global not implemented");
|
||||
break;
|
||||
}
|
||||
|
||||
return variable;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
|
||||
#include "llvm/types/composite-types.h"
|
||||
#include "llvm/types/structs.h"
|
||||
#include <llvm-c/Core.h>
|
||||
#include <llvm-c/Types.h>
|
||||
#include <llvm/types/scope.h>
|
||||
#include <llvm/types/composite.h>
|
||||
#include <ast/ast.h>
|
||||
|
@ -85,3 +88,33 @@ LLVMTypeRef llvm_type_from_gemstone_type(LLVMContextRef context, GemstoneTypeRef
|
|||
|
||||
return llvmTypeRef;
|
||||
}
|
||||
|
||||
LLVMValueRef llvm_default_value_of_composite(LLVMContextRef context, CompositeRef composite) {
|
||||
LLVMTypeRef type = llvm_type_from_composite(context, composite);
|
||||
LLVMValueRef value;
|
||||
|
||||
if (composite->prim == Int) {
|
||||
value = LLVMConstInt(type, 0, 0);
|
||||
} else if (composite->prim == Float) {
|
||||
value = LLVMConstReal(type, 0.0);
|
||||
} else {
|
||||
PANIC("Invalid composite type: %d", composite->prim);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
LLVMValueRef llvm_default_value_of_type(LLVMContextRef context, GemstoneTypeRef ref) {
|
||||
LLVMValueRef value = NULL;
|
||||
|
||||
switch (ref->kind) {
|
||||
case TypeComposite:
|
||||
value = llvm_default_value_of_composite(context, &ref->specs.composite);
|
||||
break;
|
||||
default:
|
||||
PANIC("type not implemented");
|
||||
break;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
|
|
@ -49,4 +49,6 @@ LLVMTypeRef llvm_type_from_gemstone_type(LLVMContextRef context, GemstoneTypeRef
|
|||
*/
|
||||
void delete_typedefref(GemstoneTypedefRef ref);
|
||||
|
||||
LLVMValueRef llvm_default_value_of_type(LLVMContextRef context, GemstoneTypeRef ref);
|
||||
|
||||
#endif // GEMSTONE_TYPE_H_
|
||||
|
|
Loading…
Reference in New Issue