aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-08-14 17:31:49 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-06-23 07:46:40 +0200
commit0d19a077e74ce523f05b64708517bd657721a68e (patch)
tree7396d978a7a037f7b2c321253184a2074f9e2665
parenta1219bf5f756925870671497a890e0aa1f0c7354 (diff)
downloadsparse-dev-0d19a077e74ce523f05b64708517bd657721a68e.tar.gz
cast: make pointer casts always size preserving
Currently casts to pointers can be done from any integer types. However, casts to (or from) pointers are only meaningful if value preserving and thus between objects of the same size. To avoid to have to worry about sign/zero extension while doing casts to pointers it's good to only have to deal with the value preserving ones. Do this by doing first, if needed, a cast an integer of the same size as a pointer before doing the cast to a pointer. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--Documentation/IR.rst2
-rw-r--r--linearize.c2
-rw-r--r--validation/cast-weirds.c18
-rw-r--r--validation/linear/cast-kinds.c62
4 files changed, 45 insertions, 39 deletions
diff --git a/Documentation/IR.rst b/Documentation/IR.rst
index 56f02318..b7fe98a4 100644
--- a/Documentation/IR.rst
+++ b/Documentation/IR.rst
@@ -271,7 +271,7 @@ They all have the following signature:
Cast to signed integer.
.. op:: OP_UTPTR
- Cast from unsigned integer to pointer type.
+ Cast from pointer-sized unsigned integer to pointer type.
.. op:: OP_PTRCAST
Cast to pointer.
diff --git a/linearize.c b/linearize.c
index 0f40682c..413b8ab5 100644
--- a/linearize.c
+++ b/linearize.c
@@ -1273,6 +1273,8 @@ static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *
break;
if (Wint_to_pointer_cast)
warning(to->pos, "non size-preserving integer to pointer cast");
+ src = cast_pseudo(ep, src, from, size_t_ctype);
+ from = size_t_ctype;
break;
default:
break;
diff --git a/validation/cast-weirds.c b/validation/cast-weirds.c
index 136137b4..71e52ff5 100644
--- a/validation/cast-weirds.c
+++ b/validation/cast-weirds.c
@@ -20,29 +20,31 @@ cast-weirds.c:5:44: warning: non size-preserving integer to pointer cast
int_2_iptr:
.L0:
<entry-point>
- utptr.64 %r2 <- (32) %arg1
- ret.64 %r2
+ scast.64 %r2 <- (32) %arg1
+ utptr.64 %r3 <- (64) %r2
+ ret.64 %r3
uint_2_iptr:
.L2:
<entry-point>
- utptr.64 %r5 <- (32) %arg1
- ret.64 %r5
+ cast.64 %r6 <- (32) %arg1
+ utptr.64 %r7 <- (64) %r6
+ ret.64 %r7
int_2_vptr:
.L4:
<entry-point>
- scast.64 %r8 <- (32) %arg1
- ret.64 %r8
+ scast.64 %r10 <- (32) %arg1
+ ret.64 %r10
uint_2_vptr:
.L6:
<entry-point>
- cast.64 %r11 <- (32) %arg1
- ret.64 %r11
+ cast.64 %r13 <- (32) %arg1
+ ret.64 %r13
* check-output-end
diff --git a/validation/linear/cast-kinds.c b/validation/linear/cast-kinds.c
index 4449d0af..1742cd1e 100644
--- a/validation/linear/cast-kinds.c
+++ b/validation/linear/cast-kinds.c
@@ -288,106 +288,108 @@ iptr_2_vptr:
int_2_iptr:
.L66:
<entry-point>
- utptr.64 %r101 <- (32) %arg1
- ret.64 %r101
+ scast.64 %r101 <- (32) %arg1
+ utptr.64 %r102 <- (64) %r101
+ ret.64 %r102
uint_2_iptr:
.L68:
<entry-point>
- utptr.64 %r104 <- (32) %arg1
- ret.64 %r104
+ cast.64 %r105 <- (32) %arg1
+ utptr.64 %r106 <- (64) %r105
+ ret.64 %r106
long_2_iptr:
.L70:
<entry-point>
- utptr.64 %r107 <- (64) %arg1
- ret.64 %r107
+ utptr.64 %r109 <- (64) %arg1
+ ret.64 %r109
ulong_2_iptr:
.L72:
<entry-point>
- utptr.64 %r110 <- (64) %arg1
- ret.64 %r110
+ utptr.64 %r112 <- (64) %arg1
+ ret.64 %r112
vptr_2_iptr:
.L74:
<entry-point>
- ptrcast.64 %r113 <- (64) %arg1
- ret.64 %r113
+ ptrcast.64 %r115 <- (64) %arg1
+ ret.64 %r115
int_2_float:
.L76:
<entry-point>
- scvtf.32 %r116 <- (32) %arg1
- ret.32 %r116
+ scvtf.32 %r118 <- (32) %arg1
+ ret.32 %r118
uint_2_float:
.L78:
<entry-point>
- ucvtf.32 %r119 <- (32) %arg1
- ret.32 %r119
+ ucvtf.32 %r121 <- (32) %arg1
+ ret.32 %r121
long_2_float:
.L80:
<entry-point>
- scvtf.32 %r122 <- (64) %arg1
- ret.32 %r122
+ scvtf.32 %r124 <- (64) %arg1
+ ret.32 %r124
ulong_2_float:
.L82:
<entry-point>
- ucvtf.32 %r125 <- (64) %arg1
- ret.32 %r125
+ ucvtf.32 %r127 <- (64) %arg1
+ ret.32 %r127
double_2_float:
.L84:
<entry-point>
- fcvtf.32 %r128 <- (64) %arg1
- ret.32 %r128
+ fcvtf.32 %r130 <- (64) %arg1
+ ret.32 %r130
int_2_double:
.L86:
<entry-point>
- scvtf.64 %r131 <- (32) %arg1
- ret.64 %r131
+ scvtf.64 %r133 <- (32) %arg1
+ ret.64 %r133
uint_2_double:
.L88:
<entry-point>
- ucvtf.64 %r134 <- (32) %arg1
- ret.64 %r134
+ ucvtf.64 %r136 <- (32) %arg1
+ ret.64 %r136
long_2_double:
.L90:
<entry-point>
- scvtf.64 %r137 <- (64) %arg1
- ret.64 %r137
+ scvtf.64 %r139 <- (64) %arg1
+ ret.64 %r139
ulong_2_double:
.L92:
<entry-point>
- ucvtf.64 %r140 <- (64) %arg1
- ret.64 %r140
+ ucvtf.64 %r142 <- (64) %arg1
+ ret.64 %r142
float_2_double:
.L94:
<entry-point>
- fcvtf.64 %r143 <- (32) %arg1
- ret.64 %r143
+ fcvtf.64 %r145 <- (32) %arg1
+ ret.64 %r145
float_2_float: