Skip to content

Commit a19e49d

Browse files
authored
Merge pull request #378 from namannimmo10/llvm
Handle integer printing in the llvm backend
2 parents 27b8989 + 0ea1a2d commit a19e49d

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3579,6 +3579,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
35793579
if (ASRUtils::is_integer(*t) ||
35803580
ASR::is_a<ASR::Logical_t>(*ASRUtils::type_get_past_pointer(t))) {
35813581
switch( a_kind ) {
3582+
case 1 : {
3583+
fmt.push_back("%d");
3584+
break;
3585+
}
3586+
case 2 : {
3587+
fmt.push_back("%d");
3588+
break;
3589+
}
35823590
case 4 : {
35833591
fmt.push_back("%d");
35843592
break;
@@ -3589,7 +3597,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
35893597
}
35903598
default: {
35913599
throw CodeGenError(R"""(Printing support is available only
3592-
for 32, and 64 bit integer kinds.)""",
3600+
for 8, 16, 32, and 64 bit integer kinds.)""",
35933601
x.base.base.loc);
35943602
}
35953603
}

tests/ltypes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def test_i8():
2+
i: i8
3+
i = 5
4+
print(i)
5+
6+
def test_i16():
7+
i: i16
8+
i = 4
9+
print(i)
10+
11+
def test_i32():
12+
i: i32
13+
i = 3
14+
print(i)
15+
16+
def test_i64():
17+
i: i64
18+
i = 2
19+
print(i)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "llvm-ltypes-1402bca",
3+
"cmd": "lpython --no-color --show-llvm {infile} -o {outfile}",
4+
"infile": "tests/ltypes.py",
5+
"infile_hash": "daac081f5e376233adabe24304126763178cfd3a6286c361b7955446",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "llvm-ltypes-1402bca.stdout",
9+
"stdout_hash": "cafb6dc418ad5ddc1ef709741df5c2ee0a45fcdee41b49c50f121ffb",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; ModuleID = 'LFortran'
2+
source_filename = "LFortran"
3+
4+
@0 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
5+
@1 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
6+
@2 = private unnamed_addr constant [6 x i8] c"%lld\0A\00", align 1
7+
@3 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
8+
9+
define void @test_i16() {
10+
.entry:
11+
%i = alloca i16, align 2
12+
store i16 4, i16* %i, align 2
13+
%0 = load i16, i16* %i, align 2
14+
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @0, i32 0, i32 0), i16 %0)
15+
br label %return
16+
17+
return: ; preds = %.entry
18+
ret void
19+
}
20+
21+
define void @test_i32() {
22+
.entry:
23+
%i = alloca i32, align 4
24+
store i32 3, i32* %i, align 4
25+
%0 = load i32, i32* %i, align 4
26+
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @1, i32 0, i32 0), i32 %0)
27+
br label %return
28+
29+
return: ; preds = %.entry
30+
ret void
31+
}
32+
33+
define void @test_i64() {
34+
.entry:
35+
%i = alloca i64, align 8
36+
store i64 2, i64* %i, align 4
37+
%0 = load i64, i64* %i, align 4
38+
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @2, i32 0, i32 0), i64 %0)
39+
br label %return
40+
41+
return: ; preds = %.entry
42+
ret void
43+
}
44+
45+
define void @test_i8() {
46+
.entry:
47+
%i = alloca i8, align 1
48+
store i8 5, i8* %i, align 1
49+
%0 = load i8, i8* %i, align 1
50+
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @3, i32 0, i32 0), i8 %0)
51+
br label %return
52+
53+
return: ; preds = %.entry
54+
ret void
55+
}
56+
57+
declare void @_lfortran_printf(i8*, ...)
58+
59+
define i32 @main() {
60+
.entry:
61+
ret i32 0
62+
}

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ asr = true
170170

171171
# integration_tests
172172

173+
[[test]]
174+
filename = "ltypes.py"
175+
llvm = true
176+
173177
[[test]]
174178
filename = "../integration_tests/test_builtin.py"
175179
asr = true

0 commit comments

Comments
 (0)