Skip to content

Commit

Permalink
Constant methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jan 10, 2025
1 parent 138de9d commit e961498
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion compiler/build_cf_graph.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions compiler/codegen.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 23 additions & 5 deletions compiler/structs.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 0 additions & 20 deletions compiler/types.jou
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "stdlib/mem.jou"
import "stdlib/str.jou"

import "./structs.jou"
import "./errors_and_warnings.jou"
import "./free.jou"

Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit e961498

Please sign in to comment.