Skip to content

Commit f443b0e

Browse files
committed
CHC: allow for pointer types in lifting to api
1 parent 1d8bd65 commit f443b0e

3 files changed

Lines changed: 106 additions & 35 deletions

File tree

‎CodeHawk/CHC/cchanalyze/cCHPOQuery.ml‎

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,14 +1621,25 @@ object (self)
16211621
match (self#x2global x1, self#x2global x2) with
16221622
| (Some a1, Some a2) ->
16231623
TR.tfold
1624-
~ok:(fun ty ->
1625-
begin
1626-
match op with
1627-
| XPlus -> Some (BinOp (PlusA, a1, a2, ty))
1628-
| XMinus -> Some (BinOp (MinusA, a1, a2, ty))
1629-
| XMult -> Some (BinOp (Mult, a1, a2, ty))
1630-
| _ -> None
1631-
end)
1624+
~ok:(fun ty1 ->
1625+
TR.tfold
1626+
~ok:(fun ty2 ->
1627+
if is_arithmetic_type ty1 && is_arithmetic_type ty2 then
1628+
self#x2api_arithmetic_expr op a1 a2 ty1 ty2
1629+
else
1630+
self#x2api_pointer_expr op a1 a2 ty1 ty2)
1631+
~error:(fun e ->
1632+
begin
1633+
log_diagnostics_result
1634+
~tag:"x2global"
1635+
~msg:self#fname
1636+
__FILE__ __LINE__
1637+
["Unable to determine type of exp: "
1638+
^ (p2s (exp_to_pretty a2));
1639+
String.concat "; " e];
1640+
None
1641+
end)
1642+
(type_of_exp fenv a2))
16321643
~error:(fun e ->
16331644
begin
16341645
log_diagnostics_result
@@ -1640,7 +1651,7 @@ object (self)
16401651
String.concat "; " e];
16411652
None
16421653
end)
1643-
(type_of_exp fenv a1)
1654+
(type_of_exp fenv a1)
16441655
| _ -> None
16451656
end
16461657
| _ ->
@@ -1649,6 +1660,80 @@ object (self)
16491660
None
16501661
end
16511662

1663+
method private x2api_arithmetic_expr
1664+
(op: xop_t) (a1: exp) (a2: exp) (ty1: typ) (ty2: typ): exp option =
1665+
TR.tfold
1666+
~ok:(fun resulttyp ->
1667+
begin
1668+
match op with
1669+
| XPlus -> Some (BinOp (PlusA, a1, a2, resulttyp))
1670+
| XMinus -> Some (BinOp (MinusA, a1, a2, resulttyp))
1671+
| XMult -> Some (BinOp (Mult, a1, a2, resulttyp))
1672+
| XDiv -> Some (BinOp (Div, a1, a2, resulttyp))
1673+
| XShiftrt -> Some (BinOp (Shiftrt, a1, a2, ty1))
1674+
| XShiftlt -> Some (BinOp (Shiftlt, a1, a2, ty1))
1675+
| XLt -> Some (BinOp (Lt, a1, a2, TInt (IBool, [])))
1676+
| XLe -> Some (BinOp (Le, a1, a2, TInt (IBool, [])))
1677+
| XGt -> Some (BinOp (Gt, a1, a2, TInt (IBool, [])))
1678+
| XGe -> Some (BinOp (Ge, a1, a2, TInt (IBool, [])))
1679+
| _ -> None
1680+
end)
1681+
~error:(fun err ->
1682+
begin
1683+
log_diagnostics_result
1684+
~tag:"x2api_arithmetic_expr"
1685+
~msg:self#env#get_functionname
1686+
__FILE__ __LINE__
1687+
[String.concat ", " err];
1688+
None
1689+
end)
1690+
(usual_arithmetic_conversion ty1 ty2)
1691+
1692+
method private x2api_pointer_expr
1693+
(op: xop_t) (a1: exp) (a2: exp) (ty1: typ) (ty2: typ): exp option =
1694+
if is_pointer_type ty1 && is_arithmetic_type ty2 then
1695+
match op with
1696+
| XPlus -> Some (BinOp (PlusPI, a1, a2, ty1))
1697+
| XMinus -> Some (BinOp (MinusPI, a1, a2, ty1))
1698+
| _ ->
1699+
begin
1700+
log_diagnostics_result
1701+
~tag:"x2api_pointer_expr"
1702+
~msg:self#env#get_functionname
1703+
__FILE__ __LINE__
1704+
["pointer type and arithmetic type with operator "
1705+
^ (XprToPretty.xop_to_string op)];
1706+
None
1707+
end
1708+
else if is_pointer_type ty1 && is_pointer_type ty2 then
1709+
match op with
1710+
| XMinus -> Some (BinOp (MinusPP, a1, a2, TInt (IInt, [])))
1711+
| XLt -> Some (BinOp (Lt, a1, a2, TInt (IBool, [])))
1712+
| XLe -> Some (BinOp (Le, a1, a2, TInt (IBool, [])))
1713+
| XGt -> Some (BinOp (Gt, a1, a2, TInt (IBool, [])))
1714+
| XGe -> Some (BinOp (Ge, a1, a2, TInt (IBool, [])))
1715+
| _ ->
1716+
begin
1717+
log_diagnostics_result
1718+
~tag:"x2api_pointer_expr"
1719+
~msg:self#env#get_functionname
1720+
__FILE__ __LINE__
1721+
["2-pointer expr with operator: "
1722+
^ (XprToPretty.xop_to_string op)];
1723+
None
1724+
end
1725+
else
1726+
begin
1727+
log_diagnostics_result
1728+
~tag:"x2api_pointer_expr"
1729+
~msg:self#env#get_functionname
1730+
__FILE__ __LINE__
1731+
["op: " ^ (XprToPretty.xop_to_string op);
1732+
"ty1: " ^ (p2s (typ_to_pretty ty1));
1733+
"ty2: " ^ (p2s (typ_to_pretty ty2))];
1734+
None
1735+
end
1736+
16521737
method x2api (x:xpr_t):exp option =
16531738
match x with
16541739
| XConst (IntConst n) -> Some (make_constant_exp n)
@@ -1686,32 +1771,10 @@ object (self)
16861771
~ok:(fun ty1 ->
16871772
TR.tfold
16881773
~ok:(fun ty2 ->
1689-
TR.tfold
1690-
~ok:(fun resulttyp ->
1691-
begin
1692-
match op with
1693-
| XPlus -> Some (BinOp (PlusA, a1, a2, resulttyp))
1694-
| XMinus -> Some (BinOp (MinusA, a1, a2, resulttyp))
1695-
| XMult -> Some (BinOp (Mult, a1, a2, resulttyp))
1696-
| XDiv -> Some (BinOp (Div, a1, a2, resulttyp))
1697-
| XShiftrt -> Some (BinOp (Shiftrt, a1, a2, ty1))
1698-
| XShiftlt -> Some (BinOp (Shiftlt, a1, a2, ty1))
1699-
| XLt -> Some (BinOp (Lt, a1, a2, TInt (IBool, [])))
1700-
| XLe -> Some (BinOp (Le, a1, a2, TInt (IBool, [])))
1701-
| XGt -> Some (BinOp (Gt, a1, a2, TInt (IBool, [])))
1702-
| XGe -> Some (BinOp (Ge, a1, a2, TInt (IBool, [])))
1703-
| _ -> None
1704-
end)
1705-
~error:(fun err ->
1706-
begin
1707-
log_diagnostics_result
1708-
~tag:"x2api:integer-promotion"
1709-
~msg:self#env#get_functionname
1710-
__FILE__ __LINE__
1711-
[String.concat ", " err];
1712-
None
1713-
end)
1714-
(usual_arithmetic_conversion ty1 ty2))
1774+
if is_arithmetic_type ty1 && is_arithmetic_type ty2 then
1775+
self#x2api_arithmetic_expr op a1 a2 ty1 ty2
1776+
else
1777+
self#x2api_pointer_expr op a1 a2 ty1 ty2)
17151778
~error:(fun err ->
17161779
begin
17171780
log_diagnostics_result

‎CodeHawk/CHC/cchlib/cCHTypesUtil.ml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@ let is_system_struct (t: typ): bool =
716716
| _ -> false
717717

718718

719+
let is_arithmetic_type (t: typ): bool =
720+
match fenv#get_type_unrolled t with
721+
| TInt _ | TEnum _ | TFloat _ -> true
722+
| _ -> false
723+
724+
719725
let is_integral_type (t:typ) =
720726
match fenv#get_type_unrolled t with
721727
| TInt _ -> true

‎CodeHawk/CHC/cchlib/cCHTypesUtil.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ val returntype_of_exp: cfundeclarations_int -> exp -> typ traceresult
105105

106106
val is_system_struct: typ -> bool
107107

108+
val is_arithmetic_type: typ -> bool
109+
108110
val is_integral_type: typ -> bool
109111

110112
val is_pointer_type : typ -> bool

0 commit comments

Comments
 (0)