Skip to content

Commit 0bc9558

Browse files
committed
Fix evaluation/load order of binary operators
Fixes issue #1327 by loading immediately from lvalues resulting from left- and right-hand sides.
1 parent 7fec36b commit 0bc9558

File tree

5 files changed

+52
-53
lines changed

5 files changed

+52
-53
lines changed

gen/binops.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919
//////////////////////////////////////////////////////////////////////////////
2020

21-
DValue *DtoBinAdd(DValue *lhs, DValue *rhs) {
21+
DImValue *DtoBinAdd(DRValue *lhs, DRValue *rhs) {
2222
Type *t = lhs->type;
23-
LLValue *l, *r;
24-
l = DtoRVal(lhs);
25-
r = DtoRVal(rhs);
23+
LLValue *l = DtoRVal(lhs);
24+
LLValue *r = DtoRVal(rhs);
2625

2726
LLValue *res;
2827
if (t->isfloating()) {
@@ -36,11 +35,10 @@ DValue *DtoBinAdd(DValue *lhs, DValue *rhs) {
3635

3736
//////////////////////////////////////////////////////////////////////////////
3837

39-
DValue *DtoBinSub(DValue *lhs, DValue *rhs) {
38+
DImValue *DtoBinSub(DRValue *lhs, DRValue *rhs) {
4039
Type *t = lhs->type;
41-
LLValue *l, *r;
42-
l = DtoRVal(lhs);
43-
r = DtoRVal(rhs);
40+
LLValue *l = DtoRVal(lhs);
41+
LLValue *r = DtoRVal(rhs);
4442

4543
LLValue *res;
4644
if (t->isfloating()) {
@@ -54,28 +52,27 @@ DValue *DtoBinSub(DValue *lhs, DValue *rhs) {
5452

5553
//////////////////////////////////////////////////////////////////////////////
5654

57-
DValue *DtoBinMul(Type *targettype, DValue *lhs, DValue *rhs) {
55+
DImValue *DtoBinMul(Type *targettype, DRValue *lhs, DRValue *rhs) {
5856
Type *t = lhs->type;
59-
LLValue *l, *r;
60-
l = DtoRVal(lhs);
61-
r = DtoRVal(rhs);
57+
LLValue *l = DtoRVal(lhs);
58+
LLValue *r = DtoRVal(rhs);
6259

6360
LLValue *res;
6461
if (t->isfloating()) {
6562
res = gIR->ir->CreateFMul(l, r);
6663
} else {
6764
res = gIR->ir->CreateMul(l, r);
6865
}
66+
6967
return new DImValue(targettype, res);
7068
}
7169

7270
//////////////////////////////////////////////////////////////////////////////
7371

74-
DValue *DtoBinDiv(Type *targettype, DValue *lhs, DValue *rhs) {
72+
DImValue *DtoBinDiv(Type *targettype, DRValue *lhs, DRValue *rhs) {
7573
Type *t = lhs->type;
76-
LLValue *l, *r;
77-
l = DtoRVal(lhs);
78-
r = DtoRVal(rhs);
74+
LLValue *l = DtoRVal(lhs);
75+
LLValue *r = DtoRVal(rhs);
7976

8077
LLValue *res;
8178
if (t->isfloating()) {
@@ -85,16 +82,17 @@ DValue *DtoBinDiv(Type *targettype, DValue *lhs, DValue *rhs) {
8582
} else {
8683
res = gIR->ir->CreateUDiv(l, r);
8784
}
85+
8886
return new DImValue(targettype, res);
8987
}
9088

9189
//////////////////////////////////////////////////////////////////////////////
9290

93-
DValue *DtoBinRem(Type *targettype, DValue *lhs, DValue *rhs) {
91+
DImValue *DtoBinRem(Type *targettype, DRValue *lhs, DRValue *rhs) {
9492
Type *t = lhs->type;
95-
LLValue *l, *r;
96-
l = DtoRVal(lhs);
97-
r = DtoRVal(rhs);
93+
LLValue *l = DtoRVal(lhs);
94+
LLValue *r = DtoRVal(rhs);
95+
9896
LLValue *res;
9997
if (t->isfloating()) {
10098
res = gIR->ir->CreateFRem(l, r);
@@ -103,6 +101,7 @@ DValue *DtoBinRem(Type *targettype, DValue *lhs, DValue *rhs) {
103101
} else {
104102
res = gIR->ir->CreateURem(l, r);
105103
}
104+
106105
return new DImValue(targettype, res);
107106
}
108107

gen/complex.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void DtoGetComplexParts(Loc &loc, Type *to, DValue *val, LLValue *&re,
181181

182182
////////////////////////////////////////////////////////////////////////////////
183183

184-
DValue *DtoComplexAdd(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
184+
DImValue *DtoComplexAdd(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs) {
185185
llvm::Value *lhs_re, *lhs_im, *rhs_re, *rhs_im, *res_re, *res_im;
186186

187187
// lhs values
@@ -212,7 +212,7 @@ DValue *DtoComplexAdd(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
212212

213213
////////////////////////////////////////////////////////////////////////////////
214214

215-
DValue *DtoComplexSub(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
215+
DImValue *DtoComplexSub(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs) {
216216
llvm::Value *lhs_re, *lhs_im, *rhs_re, *rhs_im, *res_re, *res_im;
217217

218218
// lhs values
@@ -243,7 +243,7 @@ DValue *DtoComplexSub(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
243243

244244
////////////////////////////////////////////////////////////////////////////////
245245

246-
DValue *DtoComplexMul(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
246+
DImValue *DtoComplexMul(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs) {
247247
llvm::Value *lhs_re, *lhs_im, *rhs_re, *rhs_im, *res_re, *res_im;
248248

249249
// lhs values
@@ -296,7 +296,7 @@ DValue *DtoComplexMul(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
296296

297297
////////////////////////////////////////////////////////////////////////////////
298298

299-
DValue *DtoComplexDiv(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
299+
DImValue *DtoComplexDiv(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs) {
300300
llvm::Value *lhs_re, *lhs_im, *rhs_re, *rhs_im, *res_re, *res_im;
301301

302302
// lhs values
@@ -369,7 +369,7 @@ DValue *DtoComplexDiv(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
369369

370370
////////////////////////////////////////////////////////////////////////////////
371371

372-
DValue *DtoComplexRem(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
372+
DImValue *DtoComplexRem(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs) {
373373
llvm::Value *lhs_re, *lhs_im, *rhs_re, *rhs_im, *res_re, *res_im, *divisor;
374374

375375
// lhs values
@@ -390,7 +390,7 @@ DValue *DtoComplexRem(Loc &loc, Type *type, DValue *lhs, DValue *rhs) {
390390

391391
////////////////////////////////////////////////////////////////////////////////
392392

393-
DValue *DtoComplexNeg(Loc &loc, Type *type, DValue *val) {
393+
DImValue *DtoComplexNeg(Loc &loc, Type *type, DRValue *val) {
394394
llvm::Value *a, *b, *re, *im;
395395

396396
// values

gen/complex.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
#include "tokens.h"
1818
#include "longdouble.h"
19+
#include "dvalue.h"
1920

20-
class DValue;
2121
struct Loc;
2222
class Type;
2323
namespace llvm {
@@ -43,12 +43,12 @@ void DtoGetComplexParts(Loc &loc, Type *to, DValue *c, DValue *&re,
4343
void DtoGetComplexParts(Loc &loc, Type *to, DValue *c, llvm::Value *&re,
4444
llvm::Value *&im);
4545

46-
DValue *DtoComplexAdd(Loc &loc, Type *type, DValue *lhs, DValue *rhs);
47-
DValue *DtoComplexSub(Loc &loc, Type *type, DValue *lhs, DValue *rhs);
48-
DValue *DtoComplexMul(Loc &loc, Type *type, DValue *lhs, DValue *rhs);
49-
DValue *DtoComplexDiv(Loc &loc, Type *type, DValue *lhs, DValue *rhs);
50-
DValue *DtoComplexRem(Loc &loc, Type *type, DValue *lhs, DValue *rhs);
51-
DValue *DtoComplexNeg(Loc &loc, Type *type, DValue *val);
46+
DImValue *DtoComplexAdd(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs);
47+
DImValue *DtoComplexSub(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs);
48+
DImValue *DtoComplexMul(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs);
49+
DImValue *DtoComplexDiv(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs);
50+
DImValue *DtoComplexRem(Loc &loc, Type *type, DRValue *lhs, DRValue *rhs);
51+
DImValue *DtoComplexNeg(Loc &loc, Type *type, DRValue *val);
5252

5353
llvm::Value *DtoComplexEquals(Loc &loc, TOK op, DValue *lhs, DValue *rhs);
5454

gen/llvmhelpers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ LLConstant *DtoConstExpInit(Loc &loc, Type *targetType, Expression *exp);
129129
LLConstant *DtoTypeInfoOf(Type *ty, bool base = true);
130130

131131
// binary operations
132-
DValue *DtoBinAdd(DValue *lhs, DValue *rhs);
133-
DValue *DtoBinSub(DValue *lhs, DValue *rhs);
132+
DImValue *DtoBinAdd(DRValue *lhs, DRValue *rhs);
133+
DImValue *DtoBinSub(DRValue *lhs, DRValue *rhs);
134134
// these binops need an explicit result type to handling
135135
// to give 'ifloat op float' and 'float op ifloat' the correct type
136-
DValue *DtoBinMul(Type *resulttype, DValue *lhs, DValue *rhs);
137-
DValue *DtoBinDiv(Type *resulttype, DValue *lhs, DValue *rhs);
138-
DValue *DtoBinRem(Type *resulttype, DValue *lhs, DValue *rhs);
136+
DImValue *DtoBinMul(Type *resulttype, DRValue *lhs, DRValue *rhs);
137+
DImValue *DtoBinDiv(Type *resulttype, DRValue *lhs, DRValue *rhs);
138+
DImValue *DtoBinRem(Type *resulttype, DRValue *lhs, DRValue *rhs);
139139
LLValue *DtoBinNumericEquals(Loc &loc, DValue *lhs, DValue *rhs, TOK op);
140140
LLValue *DtoBinFloatsEquals(Loc &loc, DValue *lhs, DValue *rhs, TOK op);
141141

gen/toir.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ class ToElemVisitor : public Visitor {
776776
auto &PGO = gIR->func()->pgo;
777777
PGO.setCurrentStmt(e);
778778

779-
DValue *l = toElem(e->e1);
779+
DRValue *l = toElem(e->e1)->getRVal();
780780

781781
Type *t = e->type->toBasetype();
782782
Type *e1type = e->e1->type->toBasetype();
@@ -788,9 +788,9 @@ class ToElemVisitor : public Visitor {
788788
Logger::println("Adding integer to pointer");
789789
result = emitPointerOffset(p, e->loc, l, e->e2, false, e->type);
790790
} else if (t->iscomplex()) {
791-
result = DtoComplexAdd(e->loc, e->type, l, toElem(e->e2));
791+
result = DtoComplexAdd(e->loc, e->type, l, toElem(e->e2)->getRVal());
792792
} else {
793-
result = DtoBinAdd(l, toElem(e->e2));
793+
result = DtoBinAdd(l, toElem(e->e2)->getRVal());
794794
}
795795
}
796796

@@ -802,7 +802,7 @@ class ToElemVisitor : public Visitor {
802802
auto &PGO = gIR->func()->pgo;
803803
PGO.setCurrentStmt(e);
804804

805-
DValue *l = toElem(e->e1);
805+
DRValue *l = toElem(e->e1)->getRVal();
806806

807807
Type *t = e->type->toBasetype();
808808
Type *t1 = e->e1->type->toBasetype();
@@ -825,9 +825,9 @@ class ToElemVisitor : public Visitor {
825825
Logger::println("Subtracting integer from pointer");
826826
result = emitPointerOffset(p, e->loc, l, e->e2, true, e->type);
827827
} else if (t->iscomplex()) {
828-
result = DtoComplexSub(e->loc, e->type, l, toElem(e->e2));
828+
result = DtoComplexSub(e->loc, e->type, l, toElem(e->e2)->getRVal());
829829
} else {
830-
result = DtoBinSub(l, toElem(e->e2));
830+
result = DtoBinSub(l, toElem(e->e2)->getRVal());
831831
}
832832
}
833833

@@ -841,8 +841,8 @@ class ToElemVisitor : public Visitor {
841841
auto &PGO = gIR->func()->pgo;
842842
PGO.setCurrentStmt(e);
843843

844-
DValue *l = toElem(e->e1);
845-
DValue *r = toElem(e->e2);
844+
DRValue *l = toElem(e->e1)->getRVal();
845+
DRValue *r = toElem(e->e2)->getRVal();
846846

847847
errorOnIllegalArrayOp(e, e->e1, e->e2);
848848

@@ -863,8 +863,8 @@ class ToElemVisitor : public Visitor {
863863
auto &PGO = gIR->func()->pgo;
864864
PGO.setCurrentStmt(e);
865865

866-
DValue *l = toElem(e->e1);
867-
DValue *r = toElem(e->e2);
866+
DRValue *l = toElem(e->e1)->getRVal();
867+
DRValue *r = toElem(e->e2)->getRVal();
868868

869869
errorOnIllegalArrayOp(e, e->e1, e->e2);
870870

@@ -885,8 +885,8 @@ class ToElemVisitor : public Visitor {
885885
auto &PGO = gIR->func()->pgo;
886886
PGO.setCurrentStmt(e);
887887

888-
DValue *l = toElem(e->e1);
889-
DValue *r = toElem(e->e2);
888+
DRValue *l = toElem(e->e1)->getRVal();
889+
DRValue *r = toElem(e->e2)->getRVal();
890890

891891
errorOnIllegalArrayOp(e, e->e1, e->e2);
892892

@@ -2418,14 +2418,14 @@ class ToElemVisitor : public Visitor {
24182418
e->type->toChars());
24192419
LOG_SCOPE;
24202420

2421-
DValue *l = toElem(e->e1);
2421+
DRValue *dval = toElem(e->e1)->getRVal();
24222422

24232423
if (e->type->iscomplex()) {
2424-
result = DtoComplexNeg(e->loc, e->type, l);
2424+
result = DtoComplexNeg(e->loc, e->type, dval);
24252425
return;
24262426
}
24272427

2428-
LLValue *val = DtoRVal(l);
2428+
LLValue *val = DtoRVal(dval);
24292429

24302430
if (e->type->isintegral()) {
24312431
val = p->ir->CreateNeg(val, "negval");

0 commit comments

Comments
 (0)