Skip to content

Commit

Permalink
CN-exec: Add binop shift left and shift right translation and C libra…
Browse files Browse the repository at this point in the history
…ry macros
  • Loading branch information
rbanerjee20 committed Jun 27, 2024
1 parent c25c09b commit 115a9e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion backend/cn/cn_internal_to_ail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions runtime/libcn/include/cn-executable/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;\
Expand Down Expand Up @@ -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)\
Expand Down

0 comments on commit 115a9e9

Please sign in to comment.