Skip to content

Commit 78a008d

Browse files
authored
Merge pull request #1057 from ronnuriel/feature/BitAnd_op
Fixing & for C backend
2 parents fb97b8d + 693ed9f commit 78a008d

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

integration_tests/expr_01.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
def add(x: i32, y: i32) -> i32:
55
return x + y
66

7+
@inline
8+
def and_op(x: i32, y: i32) -> i32:
9+
return x & y
10+
711
def main0():
8-
x: i32; y: i32
12+
x: i32
13+
y: i32
14+
z: i32
915
x = (2+3)*5
1016
y = add(x, 2)*2
1117
assert x == 25
1218
assert y == 54
1319

20+
z = and_op(x, y)
21+
assert z == 16
22+
23+
1424
main0()
1525

1626
# Not implemented yet in LPython:

src/libasr/codegen/asr_to_c_cpp.h

+1
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ R"(#include <stdio.h>
823823
case (ASR::binopType::Sub) : { last_expr_precedence = 6; break; }
824824
case (ASR::binopType::Mul) : { last_expr_precedence = 5; break; }
825825
case (ASR::binopType::Div) : { last_expr_precedence = 5; break; }
826+
case (ASR::binopType::BitAnd) : { last_expr_precedence = 5; break; }
826827
case (ASR::binopType::Pow) : {
827828
src = "pow(" + left + ", " + right + ")";
828829
if (is_c) {

tests/reference/asr-expr_01-a0d4829.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-expr_01-a0d4829",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/expr_01.py",
5-
"infile_hash": "d9a85a1c6bc50a17c74af218dcd9ee87047b4c3f3bc54f699810d339",
5+
"infile_hash": "c391a2740682aec6070efdfb508697052e45950589fdabd9728127a4",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-expr_01-a0d4829.stdout",
9-
"stdout_hash": "3b7c5a7a71d5398c67c630cb06af463e785676b02a2cfb077e290500",
9+
"stdout_hash": "c07cae48da2ca27369c507eacdd4b8635ffc2af555ae48bc7614874c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 5 {}) _lpython_main_program [] [] [(SubroutineCall 1 main0 () [] ())] () Source Public Implementation () .false. .false. .false. .false.), add: (Function (SymbolTable 2 {_lpython_return_variable: (Variable 2 _lpython_return_variable ReturnVar () () Default (Integer 4 []) Source Public Required .false.), x: (Variable 2 x In () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y In () () Default (Integer 4 []) Source Public Required .false.)}) add [(Var 2 x) (Var 2 y)] [] [(= (Var 2 _lpython_return_variable) (IntegerBinOp (Var 2 x) Add (Var 2 y) (Integer 4 []) ()) ()) (Return)] (Var 2 _lpython_return_variable) Source Public Implementation () .false. .false. .false. .true.), main0: (Function (SymbolTable 3 {x: (Variable 3 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 3 y Local () () Default (Integer 4 []) Source Public Required .false.)}) main0 [] [] [(= (Var 3 x) (IntegerBinOp (IntegerBinOp (IntegerConstant 2 (Integer 4 [])) Add (IntegerConstant 3 (Integer 4 [])) (Integer 4 []) (IntegerConstant 5 (Integer 4 []))) Mul (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant 25 (Integer 4 []))) ()) (= (Var 3 y) (IntegerBinOp (FunctionCall 1 add () [((Var 3 x)) ((IntegerConstant 2 (Integer 4 [])))] (Integer 4 []) () ()) Mul (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) ()) ()) (Assert (IntegerCompare (Var 3 x) Eq (IntegerConstant 25 (Integer 4 [])) (Logical 4 []) ()) ()) (Assert (IntegerCompare (Var 3 y) Eq (IntegerConstant 54 (Integer 4 [])) (Logical 4 []) ()) ())] () Source Public Implementation () .false. .false. .false. .false.), main_program: (Program (SymbolTable 4 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])
1+
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 6 {}) _lpython_main_program [] [] [(SubroutineCall 1 main0 () [] ())] () Source Public Implementation () .false. .false. .false. .false.), add: (Function (SymbolTable 2 {_lpython_return_variable: (Variable 2 _lpython_return_variable ReturnVar () () Default (Integer 4 []) Source Public Required .false.), x: (Variable 2 x In () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y In () () Default (Integer 4 []) Source Public Required .false.)}) add [(Var 2 x) (Var 2 y)] [] [(= (Var 2 _lpython_return_variable) (IntegerBinOp (Var 2 x) Add (Var 2 y) (Integer 4 []) ()) ()) (Return)] (Var 2 _lpython_return_variable) Source Public Implementation () .false. .false. .false. .true.), and_op: (Function (SymbolTable 3 {_lpython_return_variable: (Variable 3 _lpython_return_variable ReturnVar () () Default (Integer 4 []) Source Public Required .false.), x: (Variable 3 x In () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 3 y In () () Default (Integer 4 []) Source Public Required .false.)}) and_op [(Var 3 x) (Var 3 y)] [] [(= (Var 3 _lpython_return_variable) (IntegerBinOp (Var 3 x) BitAnd (Var 3 y) (Integer 4 []) ()) ()) (Return)] (Var 3 _lpython_return_variable) Source Public Implementation () .false. .false. .false. .true.), main0: (Function (SymbolTable 4 {x: (Variable 4 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 4 y Local () () Default (Integer 4 []) Source Public Required .false.), z: (Variable 4 z Local () () Default (Integer 4 []) Source Public Required .false.)}) main0 [] [] [(= (Var 4 x) (IntegerBinOp (IntegerBinOp (IntegerConstant 2 (Integer 4 [])) Add (IntegerConstant 3 (Integer 4 [])) (Integer 4 []) (IntegerConstant 5 (Integer 4 []))) Mul (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant 25 (Integer 4 []))) ()) (= (Var 4 y) (IntegerBinOp (FunctionCall 1 add () [((Var 4 x)) ((IntegerConstant 2 (Integer 4 [])))] (Integer 4 []) () ()) Mul (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) ()) ()) (Assert (IntegerCompare (Var 4 x) Eq (IntegerConstant 25 (Integer 4 [])) (Logical 4 []) ()) ()) (Assert (IntegerCompare (Var 4 y) Eq (IntegerConstant 54 (Integer 4 [])) (Logical 4 []) ()) ()) (= (Var 4 z) (FunctionCall 1 and_op () [((Var 4 x)) ((Var 4 y))] (Integer 4 []) () ()) ()) (Assert (IntegerCompare (Var 4 z) Eq (IntegerConstant 16 (Integer 4 [])) (Logical 4 []) ()) ())] () Source Public Implementation () .false. .false. .false. .false.), main_program: (Program (SymbolTable 5 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])

tests/reference/c-expr_01-28f449f.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "c-expr_01-28f449f",
33
"cmd": "lpython --no-color --show-c {infile}",
44
"infile": "tests/../integration_tests/expr_01.py",
5-
"infile_hash": "d9a85a1c6bc50a17c74af218dcd9ee87047b4c3f3bc54f699810d339",
5+
"infile_hash": "c391a2740682aec6070efdfb508697052e45950589fdabd9728127a4",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "c-expr_01-28f449f.stdout",
9-
"stdout_hash": "a6972cc7e2bcbfe51adf6a85e7ced9a87f630b63e0e13db7520866f1",
9+
"stdout_hash": "5a99318a6ea27779a9b7e2608adc497a92f1033ac2fe51365d2dc151",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/c-expr_01-28f449f.stdout

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct dimension_descriptor
3737
// Forward declarations
3838
void _lpython_main_program();
3939
inline __attribute__((always_inline)) int32_t add(int32_t x, int32_t y);
40+
inline __attribute__((always_inline)) int32_t and_op(int32_t x, int32_t y);
4041
void main0();
4142

4243
// Implementations
@@ -52,14 +53,24 @@ inline __attribute__((always_inline)) int32_t add(int32_t x, int32_t y)
5253
return _lpython_return_variable;
5354
}
5455

56+
inline __attribute__((always_inline)) int32_t and_op(int32_t x, int32_t y)
57+
{
58+
int32_t _lpython_return_variable;
59+
_lpython_return_variable = x&y;
60+
return _lpython_return_variable;
61+
}
62+
5563
void main0()
5664
{
5765
int32_t x;
5866
int32_t y;
67+
int32_t z;
5968
x = (2 + 3)*5;
6069
y = add(x, 2)*2;
6170
ASSERT(x == 25);
6271
ASSERT(y == 54);
72+
z = and_op(x, y);
73+
ASSERT(z == 16);
6374
}
6475

6576
int main(int argc, char* argv[])

0 commit comments

Comments
 (0)