diff --git a/compiler/build_cf_graph.jou b/compiler/build_cf_graph.jou index 1f02a67d..e54e39b5 100644 --- a/compiler/build_cf_graph.jou +++ b/compiler/build_cf_graph.jou @@ -132,7 +132,7 @@ def add_binary_op( def add_constant(st: State*, location: Location, c: Constant, target: LocalVariable*) -> CfInstruction*: - ins = CfInstruction{location = location, kind = CfInstructionKind::Constant, constant = copy_constant(c), destvar = target} + ins = CfInstruction{location = location, kind = CfInstructionKind::Constant, constant = c.copy(), destvar = target} return add_instruction(st, ins) diff --git a/compiler/codegen.jou b/compiler/codegen.jou index 9fd0fee4..3052e841 100644 --- a/compiler/codegen.jou +++ b/compiler/codegen.jou @@ -236,9 +236,9 @@ class CodeGen: if c->kind == ConstantKind::Bool: return LLVMConstInt(LLVMInt1Type(), c->boolean as long, False as int) if c->kind == ConstantKind::Integer: - return LLVMConstInt(codegen_type(type_of_constant(c)), c->integer.value, c->integer.is_signed as int) + return LLVMConstInt(codegen_type(c->get_type()), c->integer.value, c->integer.is_signed as int) if c->kind == ConstantKind::Float or c->kind == ConstantKind::Double: - return LLVMConstRealOfString(codegen_type(type_of_constant(c)), c->double_or_float_text) + return LLVMConstRealOfString(codegen_type(c->get_type()), c->double_or_float_text) if c->kind == ConstantKind::Null: return LLVMConstNull(codegen_type(voidPtrType)) if c->kind == ConstantKind::String: diff --git a/compiler/structs.jou b/compiler/structs.jou index 5ae69b84..6dc9810c 100644 --- a/compiler/structs.jou +++ b/compiler/structs.jou @@ -51,11 +51,29 @@ class Constant: boolean: bool enum_member: EnumMemberConstant -def copy_constant(c: Constant) -> Constant: - if c.kind == ConstantKind::String: - c.str = strdup(c.str) - assert c.str != NULL - return c + def get_type(self) -> Type*: + if self->kind == ConstantKind::EnumMember: + return self->enum_member.enumtype + if self->kind == ConstantKind::Null: + return voidPtrType + if self->kind == ConstantKind::Double: + return doubleType + if self->kind == ConstantKind::Float: + return floatType + if self->kind == ConstantKind::Bool: + return boolType + if self->kind == ConstantKind::String: + return byteType->pointer() + if self->kind == ConstantKind::Integer: + return get_integer_type(self->integer.size_in_bits, self->integer.is_signed) + assert False + + def copy(self: Constant) -> Constant: + if self.kind == ConstantKind::String: + self.str = strdup(self.str) + assert self.str != NULL + return self + def int_constant(type: Type*, value: long) -> Constant: assert type->is_integer_type() diff --git a/compiler/types.jou b/compiler/types.jou index 017be749..bc9bb28f 100644 --- a/compiler/types.jou +++ b/compiler/types.jou @@ -1,7 +1,6 @@ import "stdlib/mem.jou" import "stdlib/str.jou" -import "./structs.jou" import "./errors_and_warnings.jou" import "./free.jou" @@ -204,25 +203,6 @@ def get_integer_type(size_in_bits: int, is_signed: bool) -> Type*: return &global_type_state.integers[size_in_bits][is_signed as int].type -# TODO: move out -def type_of_constant(c: Constant*) -> Type*: - if c->kind == ConstantKind::EnumMember: - return c->enum_member.enumtype - if c->kind == ConstantKind::Null: - return voidPtrType - if c->kind == ConstantKind::Double: - return doubleType - if c->kind == ConstantKind::Float: - return floatType - if c->kind == ConstantKind::Bool: - return boolType - if c->kind == ConstantKind::String: - return byteType->pointer() - if c->kind == ConstantKind::Integer: - return get_integer_type(c->integer.size_in_bits, c->integer.is_signed) - assert False - - def create_opaque_class(name: byte*) -> Type*: result: TypeInfo* = calloc(1, sizeof *result) result->type = Type{kind = TypeKind::OpaqueClass}