Skip to content

Commit

Permalink
pointer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 14, 2023
1 parent 80f468c commit 10c17ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
27 changes: 25 additions & 2 deletions self_hosted/create_llvm_ir.jou
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,15 @@ class AstToIR:
if op == AstExpressionKind::Eq:
return LLVMBuildICmp(self->builder, LLVMIntPredicate::EQ, lhs_int, rhs_int, "ptreq")
if op == AstExpressionKind::Ne:
return LLVMBuildICmp(self->builder, LLVMIntPredicate::NE, lhs_int, rhs_int, "ptreq")
return LLVMBuildICmp(self->builder, LLVMIntPredicate::NE, lhs_int, rhs_int, "ptrne")
if op == AstExpressionKind::Gt:
return LLVMBuildICmp(self->builder, LLVMIntPredicate::UGT, lhs_int, rhs_int, "ptrgt")
if op == AstExpressionKind::Ge:
return LLVMBuildICmp(self->builder, LLVMIntPredicate::UGE, lhs_int, rhs_int, "ptrge")
if op == AstExpressionKind::Lt:
return LLVMBuildICmp(self->builder, LLVMIntPredicate::ULT, lhs_int, rhs_int, "ptrlt")
if op == AstExpressionKind::Le:
return LLVMBuildICmp(self->builder, LLVMIntPredicate::ULE, lhs_int, rhs_int, "ptrle")
assert False

printf("%s %d %s\n", lhs_type->name, op, rhs_type->name)
Expand Down Expand Up @@ -375,6 +383,21 @@ class AstToIR:
LLVMBuildStore(self->builder, new_value, pointer)
return [old_value, new_value]

# LLVM always interprets indexes as signed numbers.
# For example, "200 as byte" would get interpreted as -56.
# This function fixes that.
# https://github.com/Akuli/jou/issues/48
def index_to_llvm(self, ast: AstExpression*) -> LLVMValue*:
types = self->function_or_method_types->get_expression_types(ast)
assert types != NULL
type = types->get_type_after_implicit_cast()

result = self->do_expression(ast)
if type->kind == TypeKind::UnsignedInteger:
return LLVMBuildZExt(self->builder, result, LLVMInt64Type(), "ptr_indexing_implicit_cast")
else:
return result

def do_address_of_expression(self, ast: AstExpression*) -> LLVMValue*:
if ast->kind == AstExpressionKind::GetVariable:
local_var = self->get_local_var_pointer(ast->varname)
Expand Down Expand Up @@ -421,7 +444,7 @@ class AstToIR:
if ast->kind == AstExpressionKind::Indexing:
# &pointer[index] = pointer + some offset
pointer = self->do_expression(&ast->operands[0])
index = self->do_expression(&ast->operands[1])
index = self->index_to_llvm(&ast->operands[1])
return LLVMBuildGEP(self->builder, pointer, &index, 1, "indexing_ptr")

if ast->kind == AstExpressionKind::Dereference:
Expand Down
1 change: 0 additions & 1 deletion self_hosted/runs_wrong.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ tests/other_errors/missing_value_in_return.jou
tests/other_errors/noreturn_but_return_with_value.jou
tests/other_errors/noreturn_but_return_without_value.jou
tests/should_succeed/compiler_cli.jou
tests/should_succeed/pointer.jou
tests/other_errors/return_void.jou
tests/wrong_type/cannot_be_indexed.jou
tests/wrong_type/index.jou
Expand Down
11 changes: 10 additions & 1 deletion self_hosted/typecheck.jou
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,16 @@ def check_binop(

if (
(not got_numbers and not got_enums and not got_pointers)
or (op != AstExpressionKind::Eq and op != AstExpressionKind::Ne and not got_numbers)
or (got_enums and op != AstExpressionKind::Eq and op != AstExpressionKind::Ne)
or (
got_pointers
and op != AstExpressionKind::Eq
and op != AstExpressionKind::Ne
and op != AstExpressionKind::Gt
and op != AstExpressionKind::Ge
and op != AstExpressionKind::Lt
and op != AstExpressionKind::Le
)
):
message: byte[500]
snprintf(
Expand Down

0 comments on commit 10c17ec

Please sign in to comment.