Skip to content

Commit

Permalink
Merge pull request lcompilers#658 from namannimmo10/cleanup
Browse files Browse the repository at this point in the history
Remove unused bitwise functions
  • Loading branch information
namannimmo10 committed Jun 21, 2022
2 parents 64f1af3 + 6830575 commit 84c50b4
Show file tree
Hide file tree
Showing 44 changed files with 43 additions and 147 deletions.
54 changes: 1 addition & 53 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ struct PythonIntrinsicProcedures {
{"_lpython_floordiv", {m_builtin, &eval__lpython_floordiv}},
{"_mod", {m_builtin, &eval__mod}},
{"max" , {m_builtin , &eval_max}},
{"min" , {m_builtin , &eval_min}},
{"_bitwise_or", {m_builtin, &eval__bitwise_or}},
{"_bitwise_and", {m_builtin, &eval__bitwise_and}},
{"_bitwise_xor", {m_builtin, &eval__bitwise_xor}},
{"_bitwise_lshift", {m_builtin, &eval__bitwise_lshift}},
{"_bitwise_rshift", {m_builtin, &eval__bitwise_rshift}}
{"min" , {m_builtin , &eval_min}}
};
}

Expand Down Expand Up @@ -248,53 +243,6 @@ struct PythonIntrinsicProcedures {
}
}

#define BITWISE(X) \
static ASR::expr_t *X(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) { \
return eval_bitwise(al, loc, args, &X); \
}

BITWISE(eval__bitwise_or)
BITWISE(eval__bitwise_and)
BITWISE(eval__bitwise_xor)
BITWISE(eval__bitwise_lshift)
BITWISE(eval__bitwise_rshift)

static ASR::expr_t *eval_bitwise(Allocator &al, const Location &loc,
Vec<ASR::expr_t*> &args, const comptime_eval_callback X) {
if (args.size() != 2) {
throw SemanticError("Bitwise Operation must have two integer arguments.", loc);
}
ASR::expr_t* arg1 = ASRUtils::expr_value(args[0]), *arg2 = ASRUtils::expr_value(args[1]);
LFORTRAN_ASSERT(ASRUtils::check_equal_type(ASRUtils::expr_type(arg1),
ASRUtils::expr_type(arg2)));
ASR::ttype_t* type = ASRUtils::expr_type(arg1);
if (ASRUtils::is_integer(*type)) {
int64_t a = ASR::down_cast<ASR::IntegerConstant_t>(arg1)->m_n;
int64_t b = ASR::down_cast<ASR::IntegerConstant_t>(arg2)->m_n;
int64_t result = 0;
if (X == eval__bitwise_or) {
result = a | b;
} else if (X == eval__bitwise_and) {
result = a & b;
} else if (X == eval__bitwise_xor) {
result = a ^ b;
} else if (X == eval__bitwise_lshift) {
if (b < 0) {
throw SemanticError("Negative shift count not allowed.", loc);
}
result = a << b;
} else if (X == eval__bitwise_rshift) {
if (b < 0) {
throw SemanticError("Negative shift count not allowed.", loc);
}
result = a >> b;
}
return ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(al, loc, result, type));
} else {
throw SemanticError("Bitwise Operation must have both integer arguments.", loc);
}
}

static ASR::expr_t *eval_ord(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
ASR::expr_t* char_expr = args[0];
Expand Down
52 changes: 0 additions & 52 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,55 +619,3 @@ def min(a: f64, b: f64) -> f64:
return a
else:
return b

@overload
def _bitwise_or(a: i32, b: i32) -> i32:
pass

@overload
def _bitwise_or(a: i64, b: i64) -> i64:
pass

@overload
def _bitwise_and(a: i32, b: i32) -> i32:
pass

@overload
def _bitwise_and(a: i64, b: i64) -> i64:
pass

@overload
def _bitwise_xor(a: i32, b: i32) -> i32:
pass

@overload
def _bitwise_xor(a: i64, b: i64) -> i64:
pass

@overload
def _bitwise_lshift(a: i32, b: i32) -> i32:
if b < 0:
raise ValueError("Negative shift count not allowed.")
return a*2**b

@overload
def _bitwise_lshift(a: i64, b: i64) -> i64:
if b < 0:
raise ValueError("Negative shift count not allowed.")
return a*2**b

@overload
def _bitwise_rshift(a: i32, b: i32) -> i32:
if b < 0:
raise ValueError("Negative shift count not allowed.")
i: i32
i = 2
return _lpython_floordiv(a, i**b)

@overload
def _bitwise_rshift(a: i64, b: i64) -> i64:
if b < 0:
raise ValueError("Negative shift count not allowed.")
i: i64
i = 2
return _lpython_floordiv(a, i**b)
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-complex1-f26c460.stdout",
"stdout_hash": "8009382d8380163f45025b65ac70c788b747c9be316ed4444ea9d393",
"stdout_hash": "1f3a36692052496d65fe70091d9fdb05b215a19a10684ecbd5d66929",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-constants1-5828e8a.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-constants1-5828e8a.stdout",
"stdout_hash": "fbbcebe747146850c5cdf11173c4b5dd5cdd302843e2885679b34b37",
"stdout_hash": "5fc7daefb8b017b78b19632fefd03079b3b54587cb8e763b1463ec3c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-constants1-5828e8a.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-expr1-8df2d66.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr1-8df2d66.stdout",
"stdout_hash": "6e99eaec011293d399daff348fa94ac105d0065dc81105df8e170e54",
"stdout_hash": "f4ce9138933a733e6f68a8a55e85053b622cf08a6f30ab7a93ef155d",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-expr1-8df2d66.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 99 {}) main_program [] []), test_namedexpr: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), ord: (ExternalSymbol 2 ord 4 ord lpython_builtin [] ord Private), x: (Variable 2 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y Local () () Default (Integer 4 []) Source Public Required .false.)}) test_namedexpr [] [(= (Var 2 x) (NamedExpr (Var 2 y) (IntegerConstant 0 (Integer 4 [])) (Integer 4 [])) ()) (If (NamedExpr (Var 2 a) (FunctionCall 2 ord () [((StringConstant "3" (Character 1 1 () [])))] (Integer 4 []) (IntegerConstant 51 (Integer 4 [])) ()) (Integer 4 [])) [(= (Var 2 x) (IntegerConstant 1 (Integer 4 [])) ())] []) (WhileLoop (NamedExpr (Var 2 a) (IntegerConstant 1 (Integer 4 [])) (Integer 4 [])) [(= (Var 2 y) (IntegerConstant 1 (Integer 4 [])) ())])] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 89 {}) main_program [] []), test_namedexpr: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), ord: (ExternalSymbol 2 ord 4 ord lpython_builtin [] ord Private), x: (Variable 2 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y Local () () Default (Integer 4 []) Source Public Required .false.)}) test_namedexpr [] [(= (Var 2 x) (NamedExpr (Var 2 y) (IntegerConstant 0 (Integer 4 [])) (Integer 4 [])) ()) (If (NamedExpr (Var 2 a) (FunctionCall 2 ord () [((StringConstant "3" (Character 1 1 () [])))] (Integer 4 []) (IntegerConstant 51 (Integer 4 [])) ()) (Integer 4 [])) [(= (Var 2 x) (IntegerConstant 1 (Integer 4 [])) ())] []) (WhileLoop (NamedExpr (Var 2 a) (IntegerConstant 1 (Integer 4 [])) (Integer 4 [])) [(= (Var 2 y) (IntegerConstant 1 (Integer 4 [])) ())])] Source Public Implementation () .false. .false.)}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-expr10-efcbb1b.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr10-efcbb1b.stdout",
"stdout_hash": "f4df7a6a8dcdf6a9cd7d66ad095b897bf84c4ba74e323d1a97d9daac",
"stdout_hash": "fc256fc32933368cd0f0a7db2252268ad423b17bce11324bfc83b34e",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-expr10-efcbb1b.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {complex@__lpython_overloaded_13__complex: (ExternalSymbol 1 complex@__lpython_overloaded_13__complex 4 __lpython_overloaded_13__complex lpython_builtin [] __lpython_overloaded_13__complex Public), complex@__lpython_overloaded_9__complex: (ExternalSymbol 1 complex@__lpython_overloaded_9__complex 4 __lpython_overloaded_9__complex lpython_builtin [] __lpython_overloaded_9__complex Public), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 99 {}) main_program [] []), test_UnaryOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 4 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Logical 4 []) Source Public Required .false.), b2: (Variable 2 b2 Local () () Default (Logical 4 []) Source Public Required .false.), b3: (Variable 2 b3 Local () () Default (Logical 4 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), complex: (ExternalSymbol 2 complex 4 complex lpython_builtin [] complex Private), f: (Variable 2 f Local () () Default (Real 4 []) Source Public Required .false.)}) test_UnaryOp [] [(= (Var 2 a) (IntegerConstant 4 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (IntegerConstant 500 (Integer 4 [])) (Integer 4 []) (IntegerConstant -500 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant -6 (Integer 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 5 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerUnaryMinus (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 0 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 f) (Cast (RealConstant 1.00000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.00000000000000000e+00 (Real 4 []))) ()) (= (Var 2 f) (Cast (RealUnaryMinus (RealConstant 1.83745534000000014e+05 (Real 8 [])) (Real 8 []) (RealConstant -1.83745534000000014e+05 (Real 8 []))) RealToReal (Real 4 []) (RealConstant -1.83745534000000014e+05 (Real 4 []))) ()) (= (Var 2 b1) (LogicalConstant .true. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalNot (LogicalConstant .false. (Logical 4 [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 b3) (LogicalNot (Var 2 b2) (Logical 4 []) ()) ()) (= (Var 2 a) (IntegerConstant 1 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (Cast (LogicalConstant .false. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) ()) (= (Var 2 c) (Cast (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 8 [])) ComplexToComplex (Complex 4 []) (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 4 []))) ()) (= (Var 2 c) (Cast (ComplexUnaryMinus (FunctionCall 1 complex@__lpython_overloaded_13__complex 2 complex [((IntegerConstant 3 (Integer 4 []))) ((RealConstant 6.50000000000000000e+01 (Real 8 [])))] (Complex 8 []) (ComplexConstant 3.00000000000000000e+00 6.50000000000000000e+01 (Complex 8 [])) ()) (Complex 8 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 8 []))) ComplexToComplex (Complex 4 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 4 []))) ()) (= (Var 2 b1) (LogicalConstant .false. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalConstant .true. (Logical 4 [])) ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {complex@__lpython_overloaded_13__complex: (ExternalSymbol 1 complex@__lpython_overloaded_13__complex 4 __lpython_overloaded_13__complex lpython_builtin [] __lpython_overloaded_13__complex Public), complex@__lpython_overloaded_9__complex: (ExternalSymbol 1 complex@__lpython_overloaded_9__complex 4 __lpython_overloaded_9__complex lpython_builtin [] __lpython_overloaded_9__complex Public), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 89 {}) main_program [] []), test_UnaryOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 4 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Logical 4 []) Source Public Required .false.), b2: (Variable 2 b2 Local () () Default (Logical 4 []) Source Public Required .false.), b3: (Variable 2 b3 Local () () Default (Logical 4 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), complex: (ExternalSymbol 2 complex 4 complex lpython_builtin [] complex Private), f: (Variable 2 f Local () () Default (Real 4 []) Source Public Required .false.)}) test_UnaryOp [] [(= (Var 2 a) (IntegerConstant 4 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (IntegerConstant 500 (Integer 4 [])) (Integer 4 []) (IntegerConstant -500 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant -6 (Integer 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 5 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerUnaryMinus (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 0 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 f) (Cast (RealConstant 1.00000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.00000000000000000e+00 (Real 4 []))) ()) (= (Var 2 f) (Cast (RealUnaryMinus (RealConstant 1.83745534000000014e+05 (Real 8 [])) (Real 8 []) (RealConstant -1.83745534000000014e+05 (Real 8 []))) RealToReal (Real 4 []) (RealConstant -1.83745534000000014e+05 (Real 4 []))) ()) (= (Var 2 b1) (LogicalConstant .true. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalNot (LogicalConstant .false. (Logical 4 [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 b3) (LogicalNot (Var 2 b2) (Logical 4 []) ()) ()) (= (Var 2 a) (IntegerConstant 1 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (Cast (LogicalConstant .false. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) ()) (= (Var 2 c) (Cast (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 8 [])) ComplexToComplex (Complex 4 []) (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 4 []))) ()) (= (Var 2 c) (Cast (ComplexUnaryMinus (FunctionCall 1 complex@__lpython_overloaded_13__complex 2 complex [((IntegerConstant 3 (Integer 4 []))) ((RealConstant 6.50000000000000000e+01 (Real 8 [])))] (Complex 8 []) (ComplexConstant 3.00000000000000000e+00 6.50000000000000000e+01 (Complex 8 [])) ()) (Complex 8 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 8 []))) ComplexToComplex (Complex 4 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 4 []))) ()) (= (Var 2 b1) (LogicalConstant .false. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalConstant .true. (Logical 4 [])) ())] Source Public Implementation () .false. .false.)}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-expr13-81bdb5a.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr13-81bdb5a.stdout",
"stdout_hash": "4c4f8288354c7b09ebd2b52bce63b218545569c5810a6d3403910da3",
"stdout_hash": "fc597fba661d49f5e6638a35b6d4e9b666166e1646486aec9d396b28",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit 84c50b4

Please sign in to comment.