From 115a9e9bbd6a29844effbc00582e6d53ef28e480 Mon Sep 17 00:00:00 2001 From: Rini Banerjee Date: Thu, 27 Jun 2024 23:42:47 +0100 Subject: [PATCH] CN-exec: Add binop shift left and shift right translation and C library macros --- backend/cn/cn_internal_to_ail.ml | 4 +++- runtime/libcn/include/cn-executable/utils.h | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/backend/cn/cn_internal_to_ail.ml b/backend/cn/cn_internal_to_ail.ml index 556651661..cdacb265e 100644 --- a/backend/cn/cn_internal_to_ail.ml +++ b/backend/cn/cn_internal_to_ail.ml @@ -299,6 +299,8 @@ let cn_to_ail_binop_internal bt1 bt2 = | XORNoSMT -> (A.(Arithmetic Bxor), None) | BWAndNoSMT -> (A.(Arithmetic Band), None) | BWOrNoSMT -> (A.(Arithmetic Bor), None) + | ShiftLeft -> (A.(Arithmetic Shl), Some (get_cn_int_type_str bt1 bt2 ^ "_shift_left")) + | ShiftRight -> (A.(Arithmetic Shr), Some (get_cn_int_type_str bt1 bt2 ^ "_shift_right")) | LT -> (A.Lt, Some (get_cn_int_type_str bt1 bt2 ^ "_lt")) | LE -> (A.Le, Some (get_cn_int_type_str bt1 bt2 ^ "_le")) @@ -310,7 +312,7 @@ let cn_to_ail_binop_internal bt1 bt2 = | None -> None in (A.Eq, fn_str) *) - | _ -> (A.And, None) (* TODO: Change *) + | _ -> failwith "TODO cn_to_ail_binop: Translation not implemented" (* | LTPointer | LEPointer | SetUnion diff --git a/runtime/libcn/include/cn-executable/utils.h b/runtime/libcn/include/cn-executable/utils.h index 3ac20f83d..46894065f 100644 --- a/runtime/libcn/include/cn-executable/utils.h +++ b/runtime/libcn/include/cn-executable/utils.h @@ -186,6 +186,22 @@ cn_bool *cn_pointer_is_null(cn_pointer *); return res;\ } +#define CN_GEN_SHIFT_LEFT(CTYPE, CNTYPE)\ + static inline CNTYPE *CNTYPE##_shift_left(CNTYPE *i1, CNTYPE *i2) {\ + CNTYPE *res = (CNTYPE *) alloc(sizeof(CNTYPE));\ + res->val = (CTYPE *) alloc(sizeof(CTYPE));\ + *(res->val) = *(i1->val) << *(i2->val);\ + return res;\ + } + +#define CN_GEN_SHIFT_RIGHT(CTYPE, CNTYPE)\ + static inline CNTYPE *CNTYPE##_shift_right(CNTYPE *i1, CNTYPE *i2) {\ + CNTYPE *res = (CNTYPE *) alloc(sizeof(CNTYPE));\ + res->val = (CTYPE *) alloc(sizeof(CTYPE));\ + *(res->val) = *(i1->val) >> *(i2->val);\ + return res;\ + } + #define CN_GEN_MIN(CNTYPE)\ static inline CNTYPE *CNTYPE##_min(CNTYPE *i1, CNTYPE *i2) {\ return CNTYPE##_lt(i1, i2) ? i1 : i2;\ @@ -313,6 +329,8 @@ static inline int ipow(int base, int exp) CN_GEN_SUB(CTYPE, CNTYPE)\ CN_GEN_MUL(CTYPE, CNTYPE)\ CN_GEN_DIV(CTYPE, CNTYPE)\ + CN_GEN_SHIFT_LEFT(CTYPE, CNTYPE)\ + CN_GEN_SHIFT_RIGHT(CTYPE, CNTYPE)\ CN_GEN_MIN(CNTYPE)\ CN_GEN_MAX(CNTYPE)\ CN_GEN_MOD(CTYPE, CNTYPE)\