aboutsummaryrefslogtreecommitdiff
path: root/src/lj_opt_fold.c
diff options
context:
space:
mode:
authorMike Pall <mike>2022-12-22 00:03:06 +0100
committerMike Pall <mike>2022-12-22 00:03:06 +0100
commit8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b (patch)
treedae089564f58db2963bae8e3530c1faae104cc61 /src/lj_opt_fold.c
parentb2791179ef96d652d00d78d2a8780af690537f6a (diff)
downloadluajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.gz
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.bz2
luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.zip
Avoid negation of signed integers in C that may hold INT*_MIN.
Reported by minoki. Recent C compilers 'take advantage' of the undefined behavior. This completely changes the meaning of expressions like (k == -k).
Diffstat (limited to 'src/lj_opt_fold.c')
-rw-r--r--src/lj_opt_fold.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c
index e9a6532a..482abdef 100644
--- a/src/lj_opt_fold.c
+++ b/src/lj_opt_fold.c
@@ -236,7 +236,7 @@ static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op)
236 case IR_SUB: k1 -= k2; break; 236 case IR_SUB: k1 -= k2; break;
237 case IR_MUL: k1 *= k2; break; 237 case IR_MUL: k1 *= k2; break;
238 case IR_MOD: k1 = lj_vm_modi(k1, k2); break; 238 case IR_MOD: k1 = lj_vm_modi(k1, k2); break;
239 case IR_NEG: k1 = -k1; break; 239 case IR_NEG: k1 = (int32_t)(~(uint32_t)k1+1u); break;
240 case IR_BAND: k1 &= k2; break; 240 case IR_BAND: k1 &= k2; break;
241 case IR_BOR: k1 |= k2; break; 241 case IR_BOR: k1 |= k2; break;
242 case IR_BXOR: k1 ^= k2; break; 242 case IR_BXOR: k1 ^= k2; break;
@@ -1160,7 +1160,7 @@ LJFOLDF(simplify_intsub_k)
1160 if (fright->i == 0) /* i - 0 ==> i */ 1160 if (fright->i == 0) /* i - 0 ==> i */
1161 return LEFTFOLD; 1161 return LEFTFOLD;
1162 fins->o = IR_ADD; /* i - k ==> i + (-k) */ 1162 fins->o = IR_ADD; /* i - k ==> i + (-k) */
1163 fins->op2 = (IRRef1)lj_ir_kint(J, -fright->i); /* Overflow for -2^31 ok. */ 1163 fins->op2 = (IRRef1)lj_ir_kint(J, (int32_t)(~(uint32_t)fright->i+1u)); /* Overflow for -2^31 ok. */
1164 return RETRYFOLD; 1164 return RETRYFOLD;
1165} 1165}
1166 1166
@@ -1191,7 +1191,7 @@ LJFOLDF(simplify_intsub_k64)
1191 if (k == 0) /* i - 0 ==> i */ 1191 if (k == 0) /* i - 0 ==> i */
1192 return LEFTFOLD; 1192 return LEFTFOLD;
1193 fins->o = IR_ADD; /* i - k ==> i + (-k) */ 1193 fins->o = IR_ADD; /* i - k ==> i + (-k) */
1194 fins->op2 = (IRRef1)lj_ir_kint64(J, (uint64_t)-(int64_t)k); 1194 fins->op2 = (IRRef1)lj_ir_kint64(J, ~k+1u);
1195 return RETRYFOLD; 1195 return RETRYFOLD;
1196} 1196}
1197 1197