From a01abf68b321b1c1c0e9a541d372e7bf8cb568fc Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 26 Dec 2023 17:14:49 +0200 Subject: [PATCH 1/3] Fix comparing two unsigned types --- src/codegen.c | 6 +++--- tests/should_succeed/compare_bytes.jou | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/should_succeed/compare_bytes.jou diff --git a/src/codegen.c b/src/codegen.c index 96fccb99..470cf29b 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -392,11 +392,11 @@ static void codegen_instruction(const struct State *st, const CfInstruction *ins setdest(LLVMBuildFCmp(st->builder, LLVMRealOEQ, getop(0), getop(1), "num_eq")); break; case CF_NUM_LT: - if (is_integer_type(ins->operands[0]->type)) - // TODO: unsigned less than + if (ins->operands[0]->type->kind == TYPE_UNSIGNED_INTEGER && ins->operands[1]->type->kind == TYPE_UNSIGNED_INTEGER) + setdest(LLVMBuildICmp(st->builder, LLVMIntULT, getop(0), getop(1), "num_lt")); + else if (is_integer_type(ins->operands[0]->type) && is_integer_type(ins->operands[1]->type)) setdest(LLVMBuildICmp(st->builder, LLVMIntSLT, getop(0), getop(1), "num_lt")); else - // TODO: signed less than setdest(LLVMBuildFCmp(st->builder, LLVMRealOLT, getop(0), getop(1), "num_lt")); break; } diff --git a/tests/should_succeed/compare_bytes.jou b/tests/should_succeed/compare_bytes.jou new file mode 100644 index 00000000..553f484f --- /dev/null +++ b/tests/should_succeed/compare_bytes.jou @@ -0,0 +1,18 @@ +import "stdlib/io.jou" + + +def main() -> int: + a = 0 as byte + b = 100 as byte + c = 200 as byte + + printf("%d %d %d\n", a < b, b < b, c < b) # Output: 1 0 0 + printf("%d %d %d\n", a > b, b > b, c > b) # Output: 0 0 1 + + printf("%d %d %d\n", a <= b, b <= b, c <= b) # Output: 1 1 0 + printf("%d %d %d\n", a >= b, b >= b, c >= b) # Output: 0 1 1 + + printf("%d %d %d\n", a == b, b == b, c == b) # Output: 0 1 0 + printf("%d %d %d\n", a != b, b != b, c != b) # Output: 1 0 1 + + return 0 From f7c0a3332e989122e4e227e92e03d43fa9171454 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 26 Dec 2023 17:59:01 +0200 Subject: [PATCH 2/3] Make self-hosted consistent (no way to test it yet) --- self_hosted/create_llvm_ir.jou | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/self_hosted/create_llvm_ir.jou b/self_hosted/create_llvm_ir.jou index 4c6f6a93..36c19f93 100644 --- a/self_hosted/create_llvm_ir.jou +++ b/self_hosted/create_llvm_ir.jou @@ -272,7 +272,7 @@ class AstToIR: assert False if lhs_type->is_integer_type() and rhs_type->is_integer_type(): - is_signed = lhs_type->kind == TypeKind::SignedInteger and rhs_type->kind == TypeKind::SignedInteger + is_signed = lhs_type->kind == TypeKind::SignedInteger or rhs_type->kind == TypeKind::SignedInteger if op == AstExpressionKind::Add: return LLVMBuildAdd(self->builder, lhs, rhs, "add") if op == AstExpressionKind::Subtract: From 7b87461e20bbe32adba7b25fb1b5cbbfc7c5cdc5 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 26 Dec 2023 18:52:23 +0200 Subject: [PATCH 3/3] Update tests/should_succeed/compare_bytes.jou --- tests/should_succeed/compare_bytes.jou | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/should_succeed/compare_bytes.jou b/tests/should_succeed/compare_bytes.jou index 553f484f..9aad815a 100644 --- a/tests/should_succeed/compare_bytes.jou +++ b/tests/should_succeed/compare_bytes.jou @@ -1,6 +1,8 @@ import "stdlib/io.jou" +# TODO: It's not possible to test things like "signed < unsigned" yet with types of the same size. +# For 8-bit there's only unsigned (byte), for 16-bit only signed (short), 32-bit only signed (int), 64-bit only signed (long). def main() -> int: a = 0 as byte b = 100 as byte