diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 60e9022e8c..212753f988 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -199,6 +199,7 @@ RUN(NAME exit_02c FAIL LABELS cpython llvm c) # Test all four backends RUN(NAME print_01 LABELS cpython llvm c wasm) # wasm not yet supports sep and end keywords RUN(NAME print_03 LABELS x86 c wasm_x86) # simple test case specifically for x86 and wasm_x86 +RUN(NAME print_04 LABELS cpython llvm c) # CPython and LLVM RUN(NAME const_01 LABELS cpython llvm c) diff --git a/integration_tests/print_04.py b/integration_tests/print_04.py new file mode 100644 index 0000000000..b6a7cc4766 --- /dev/null +++ b/integration_tests/print_04.py @@ -0,0 +1,10 @@ +from ltypes import i8, i16, i32, i64 + +u: i64 = i64(-922337203685477580) +print(u) +x: i32 = i32(-2147483648) +print(x) +y: i16 = i16(-32768) +print(y) +z: i8 = i8(-128) +print(z) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 2f8685b781..a8b92b29cd 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -5642,11 +5642,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } else if (ASRUtils::is_integer(*t)) { switch( a_kind ) { case 1 : { - fmt.push_back("%d"); + fmt.push_back("%hhi"); break; } case 2 : { - fmt.push_back("%d"); + fmt.push_back("%hi"); break; } case 4 : { diff --git a/tests/reference/llvm-ltypes1-dacf939.json b/tests/reference/llvm-ltypes1-dacf939.json index ebb4ce8446..340a363851 100644 --- a/tests/reference/llvm-ltypes1-dacf939.json +++ b/tests/reference/llvm-ltypes1-dacf939.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "llvm-ltypes1-dacf939.stdout", - "stdout_hash": "6bf240f208e3d2f3c9f0e5173b2b431f84123e56a293373b7327d7af", + "stdout_hash": "b05edd4fb6a315a5f992c3d0b7f5eb2121c4ac38b2bab21d1e0a998b", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/llvm-ltypes1-dacf939.stdout b/tests/reference/llvm-ltypes1-dacf939.stdout index 387f8bf401..4ce1760033 100644 --- a/tests/reference/llvm-ltypes1-dacf939.stdout +++ b/tests/reference/llvm-ltypes1-dacf939.stdout @@ -3,7 +3,7 @@ source_filename = "LFortran" @0 = private unnamed_addr constant [2 x i8] c" \00", align 1 @1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1 -@2 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1 +@2 = private unnamed_addr constant [6 x i8] c"%hi%s\00", align 1 @3 = private unnamed_addr constant [2 x i8] c" \00", align 1 @4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1 @5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1 @@ -12,14 +12,14 @@ source_filename = "LFortran" @8 = private unnamed_addr constant [7 x i8] c"%lld%s\00", align 1 @9 = private unnamed_addr constant [2 x i8] c" \00", align 1 @10 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1 -@11 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1 +@11 = private unnamed_addr constant [7 x i8] c"%hhi%s\00", align 1 define void @test_i16() { .entry: %i = alloca i16, align 2 store i16 4, i16* %i, align 2 %0 = load i16, i16* %i, align 2 - call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i16 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)) + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @2, i32 0, i32 0), i16 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)) br label %return return: ; preds = %.entry @@ -55,7 +55,7 @@ define void @test_i8() { %i = alloca i8, align 1 store i8 5, i8* %i, align 1 %0 = load i8, i8* %i, align 1 - call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @11, i32 0, i32 0), i8 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @10, i32 0, i32 0)) + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @11, i32 0, i32 0), i8 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @10, i32 0, i32 0)) br label %return return: ; preds = %.entry diff --git a/tests/reference/llvm-print_04-443a8d8.json b/tests/reference/llvm-print_04-443a8d8.json new file mode 100644 index 0000000000..8efbb6b0a0 --- /dev/null +++ b/tests/reference/llvm-print_04-443a8d8.json @@ -0,0 +1,13 @@ +{ + "basename": "llvm-print_04-443a8d8", + "cmd": "lpython --no-color --show-llvm {infile} -o {outfile}", + "infile": "tests/../integration_tests/print_04.py", + "infile_hash": "d9eb6feb9aaa52c5cac94b3edd6c62f2611eda2c5ad191e44b42f480", + "outfile": null, + "outfile_hash": null, + "stdout": "llvm-print_04-443a8d8.stdout", + "stdout_hash": "e31293d8330c55c3ec75e1d9b929a70bfabe43a5272a5dbf29c5aadb", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/llvm-print_04-443a8d8.stdout b/tests/reference/llvm-print_04-443a8d8.stdout new file mode 100644 index 0000000000..5c6bb695aa --- /dev/null +++ b/tests/reference/llvm-print_04-443a8d8.stdout @@ -0,0 +1,43 @@ +; ModuleID = 'LFortran' +source_filename = "LFortran" + +@u = global i64 -922337203685477580 +@x = global i32 -2147483648 +@y = global i16 -32768 +@z = global i8 -128 +@0 = private unnamed_addr constant [2 x i8] c" \00", align 1 +@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1 +@2 = private unnamed_addr constant [7 x i8] c"%lld%s\00", align 1 +@3 = private unnamed_addr constant [2 x i8] c" \00", align 1 +@4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1 +@5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1 +@6 = private unnamed_addr constant [2 x i8] c" \00", align 1 +@7 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1 +@8 = private unnamed_addr constant [6 x i8] c"%hi%s\00", align 1 +@9 = private unnamed_addr constant [2 x i8] c" \00", align 1 +@10 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1 +@11 = private unnamed_addr constant [7 x i8] c"%hhi%s\00", align 1 + +define void @_lpython_main_program() { +.entry: + %0 = load i64, i64* @u, align 4 + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @2, i32 0, i32 0), i64 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0)) + %1 = load i32, i32* @x, align 4 + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @5, i32 0, i32 0), i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @4, i32 0, i32 0)) + %2 = load i16, i16* @y, align 2 + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @8, i32 0, i32 0), i16 %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @7, i32 0, i32 0)) + %3 = load i8, i8* @z, align 1 + call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @11, i32 0, i32 0), i8 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @10, i32 0, i32 0)) + br label %return + +return: ; preds = %.entry + ret void +} + +declare void @_lfortran_printf(i8*, ...) + +define i32 @main() { +.entry: + call void @_lpython_main_program() + ret i32 0 +} diff --git a/tests/tests.toml b/tests/tests.toml index cfd14814e8..c8a54cd81c 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -400,6 +400,10 @@ filename = "../integration_tests/print_02.py" asr = true pass = "print_list" +[[test]] +filename = "../integration_tests/print_04.py" +llvm = true + [[test]] filename = "../integration_tests/generics_01.py" asr = true