Skip to content

Commit

Permalink
Add support for __imag__ References #8, References #9
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schwarz committed Jan 16, 2020
1 parent b8c0705 commit 1cde476
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,10 @@ and checkExp (isconst: bool) (e: exp) : typ =

| Real e ->
let te = checkExp isconst e in
typeOfReal te
| Imag e -> E.s (E.bug "unimplemented")

typeOfRealAndImagComponents te
| Imag e ->
let te = checkExp isconst e in
typeOfRealAndImagComponents te
| AlignOf(t) -> begin
(* Sizeof cannot be applied to certain types *)
checkType t CTSizeof;
Expand Down
8 changes: 4 additions & 4 deletions src/cil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1597,8 +1597,8 @@ let isVoidPtrType t =
TPtr(tau,_) when isVoidType tau -> true
| _ -> false

(* get the typ of __real__(e) for e of typ t*)
let typeOfReal t =
(* get the typ of __real__(e) or __imag__(e) for e of typ t*)
let typeOfRealAndImagComponents t =
match unrollType t with
| TInt _ -> t
| TFloat (fkind, attrs) ->
Expand Down Expand Up @@ -1920,8 +1920,8 @@ let rec typeOf (e: exp) : typ =
| Const(CReal (_, fk, _)) -> TFloat(fk, [])

| Const(CEnum(tag, _, ei)) -> typeOf tag
| Real e -> typeOfReal @@ typeOf e
| Imag e -> E.s (E.bug "unsupported")
| Real e -> typeOfRealAndImagComponents @@ typeOf e
| Imag e -> typeOfRealAndImagComponents @@ typeOf e
| Lval(lv) -> typeOfLval lv
| SizeOf _ | SizeOfE _ | SizeOfStr _ -> !typeOfSizeOf
| AlignOf _ | AlignOfE _ -> !typeOfSizeOf
Expand Down
4 changes: 2 additions & 2 deletions src/cil.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1324,8 +1324,8 @@ val isVoidType: typ -> bool
(** is the given type "void *"? *)
val isVoidPtrType: typ -> bool

(** for numerical __complex types return type of corresponding real part *)
val typeOfReal: typ -> typ
(** for numerical __complex types return type of corresponding real part and imaginary parts *)
val typeOfRealAndImagComponents: typ -> typ

(** for an fkind, return the corresponding complex fkind *)
val getComplexFkind: fkind -> fkind
Expand Down
7 changes: 5 additions & 2 deletions src/frontc/cabs2cil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3658,8 +3658,11 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
| A.REAL e ->
let (se, e', t) = doExp false e (AExp None) in
let real = Real e' in
finishExp se real (typeOfReal t)
| A.IMAG e -> E.s (bug "cabs2cil: unsupported")
finishExp se real (typeOfRealAndImagComponents t)
| A.IMAG e ->
let (se, e', t) = doExp false e (AExp None) in
let imag = Imag e' in
finishExp se imag (typeOfRealAndImagComponents t)
| A.CLASSIFYTYPE e ->
let classify_type t =
match unrollTypeDeep t with (* See gcc/typeclass.h *)
Expand Down
6 changes: 6 additions & 0 deletions test/small1/c99-complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ int main(void)
}

double d = creal(x1);
double i = cimag(x1);

double j = __imag__(1.0if);

if(d != 0.5)
E(2);

if(i != 1.0 || j != 1.0)
E(3);

double complex z1 = 1.0iF + 1;
printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));

Expand Down

0 comments on commit 1cde476

Please sign in to comment.