Skip to content

Commit 0a0d710

Browse files
authored
Fix printing of i8 and i16 integers in LLVM (#1355)
1 parent e6c2a3b commit 0a0d710

File tree

8 files changed

+78
-7
lines changed

8 files changed

+78
-7
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ RUN(NAME exit_02c FAIL LABELS cpython llvm c)
199199
# Test all four backends
200200
RUN(NAME print_01 LABELS cpython llvm c wasm) # wasm not yet supports sep and end keywords
201201
RUN(NAME print_03 LABELS x86 c wasm_x86) # simple test case specifically for x86 and wasm_x86
202+
RUN(NAME print_04 LABELS cpython llvm c)
202203

203204
# CPython and LLVM
204205
RUN(NAME const_01 LABELS cpython llvm c)

integration_tests/print_04.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from ltypes import i8, i16, i32, i64
2+
3+
u: i64 = i64(-922337203685477580)
4+
print(u)
5+
x: i32 = i32(-2147483648)
6+
print(x)
7+
y: i16 = i16(-32768)
8+
print(y)
9+
z: i8 = i8(-128)
10+
print(z)

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5642,11 +5642,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
56425642
} else if (ASRUtils::is_integer(*t)) {
56435643
switch( a_kind ) {
56445644
case 1 : {
5645-
fmt.push_back("%d");
5645+
fmt.push_back("%hhi");
56465646
break;
56475647
}
56485648
case 2 : {
5649-
fmt.push_back("%d");
5649+
fmt.push_back("%hi");
56505650
break;
56515651
}
56525652
case 4 : {

tests/reference/llvm-ltypes1-dacf939.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "llvm-ltypes1-dacf939.stdout",
9-
"stdout_hash": "6bf240f208e3d2f3c9f0e5173b2b431f84123e56a293373b7327d7af",
9+
"stdout_hash": "b05edd4fb6a315a5f992c3d0b7f5eb2121c4ac38b2bab21d1e0a998b",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/llvm-ltypes1-dacf939.stdout

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ source_filename = "LFortran"
33

44
@0 = private unnamed_addr constant [2 x i8] c" \00", align 1
55
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
6-
@2 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
6+
@2 = private unnamed_addr constant [6 x i8] c"%hi%s\00", align 1
77
@3 = private unnamed_addr constant [2 x i8] c" \00", align 1
88
@4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
99
@5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
@@ -12,14 +12,14 @@ source_filename = "LFortran"
1212
@8 = private unnamed_addr constant [7 x i8] c"%lld%s\00", align 1
1313
@9 = private unnamed_addr constant [2 x i8] c" \00", align 1
1414
@10 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
15-
@11 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
15+
@11 = private unnamed_addr constant [7 x i8] c"%hhi%s\00", align 1
1616

1717
define void @test_i16() {
1818
.entry:
1919
%i = alloca i16, align 2
2020
store i16 4, i16* %i, align 2
2121
%0 = load i16, i16* %i, align 2
22-
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))
22+
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))
2323
br label %return
2424

2525
return: ; preds = %.entry
@@ -55,7 +55,7 @@ define void @test_i8() {
5555
%i = alloca i8, align 1
5656
store i8 5, i8* %i, align 1
5757
%0 = load i8, i8* %i, align 1
58-
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))
58+
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))
5959
br label %return
6060

6161
return: ; preds = %.entry
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "llvm-print_04-443a8d8",
3+
"cmd": "lpython --no-color --show-llvm {infile} -o {outfile}",
4+
"infile": "tests/../integration_tests/print_04.py",
5+
"infile_hash": "d9eb6feb9aaa52c5cac94b3edd6c62f2611eda2c5ad191e44b42f480",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "llvm-print_04-443a8d8.stdout",
9+
"stdout_hash": "e31293d8330c55c3ec75e1d9b929a70bfabe43a5272a5dbf29c5aadb",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; ModuleID = 'LFortran'
2+
source_filename = "LFortran"
3+
4+
@u = global i64 -922337203685477580
5+
@x = global i32 -2147483648
6+
@y = global i16 -32768
7+
@z = global i8 -128
8+
@0 = private unnamed_addr constant [2 x i8] c" \00", align 1
9+
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
10+
@2 = private unnamed_addr constant [7 x i8] c"%lld%s\00", align 1
11+
@3 = private unnamed_addr constant [2 x i8] c" \00", align 1
12+
@4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
13+
@5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
14+
@6 = private unnamed_addr constant [2 x i8] c" \00", align 1
15+
@7 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
16+
@8 = private unnamed_addr constant [6 x i8] c"%hi%s\00", align 1
17+
@9 = private unnamed_addr constant [2 x i8] c" \00", align 1
18+
@10 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
19+
@11 = private unnamed_addr constant [7 x i8] c"%hhi%s\00", align 1
20+
21+
define void @_lpython_main_program() {
22+
.entry:
23+
%0 = load i64, i64* @u, align 4
24+
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))
25+
%1 = load i32, i32* @x, align 4
26+
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))
27+
%2 = load i16, i16* @y, align 2
28+
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))
29+
%3 = load i8, i8* @z, align 1
30+
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))
31+
br label %return
32+
33+
return: ; preds = %.entry
34+
ret void
35+
}
36+
37+
declare void @_lfortran_printf(i8*, ...)
38+
39+
define i32 @main() {
40+
.entry:
41+
call void @_lpython_main_program()
42+
ret i32 0
43+
}

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ filename = "../integration_tests/print_02.py"
400400
asr = true
401401
pass = "print_list"
402402

403+
[[test]]
404+
filename = "../integration_tests/print_04.py"
405+
llvm = true
406+
403407
[[test]]
404408
filename = "../integration_tests/generics_01.py"
405409
asr = true

0 commit comments

Comments
 (0)