diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-02-05 13:46:32 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-02-05 13:46:32 -0800 |
| commit | 314795b5d8ff5845624f93e152face325659dd0c (patch) | |
| tree | 132dfad977c64218c55bf2b0f46db082c4e2297d /source | |
| parent | a17b68a0ecc4c8e91d2ba3685f6e5bc30e3ead8c (diff) | |
Fix confused definitions of pre/post increment/decrement (#827)
* Fix confused definitions of pre/post increment/decrement
We somehow have been compiling `++i` as `i++` (and vice versa) for a long time without noticing.
This change fixes the implementation of these pseudo-ops in IR codegen, and adds a comment to explain the rationale for why their definitions should be what they are.
* fixup: typo
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/lower-to-ir.cpp | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 9cfb857e8..3a5e0cd2e 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -534,7 +534,7 @@ IRInst* getOneValOfType( UNREACHABLE_RETURN(nullptr); } -LoweredValInfo emitPreOp( +LoweredValInfo emitPrefixIncDecOp( IRGenContext* context, IRType* type, IROp op, @@ -555,10 +555,15 @@ LoweredValInfo emitPreOp( builder->emitStore(argPtr, innerOp); - return LoweredValInfo::simple(preVal); + // For a prefix operator like `++i` we return + // the value after the increment/decrement has + // been applied. In casual terms we "increment + // the varaible, then return its value." + // + return LoweredValInfo::simple(innerOp); } -LoweredValInfo emitPostOp( +LoweredValInfo emitPostfixIncDecOp( IRGenContext* context, IRType* type, IROp op, @@ -579,7 +584,12 @@ LoweredValInfo emitPostOp( builder->emitStore(argPtr, innerOp); - return LoweredValInfo::ptr(argPtr); + // For a postfix operator like `i++` we return + // the value that we read before the increment/decrement + // gets applied. In casual terms we "read + // the variable, then increment it." + // + return LoweredValInfo::simple(preVal); } LoweredValInfo lowerRValueExpr( @@ -707,13 +717,13 @@ LoweredValInfo emitCallToDeclRef( #undef CASE #define CASE(COMPOUND, OP) \ - case COMPOUND: return emitPreOp(context, type, OP, argCount, args) + case COMPOUND: return emitPrefixIncDecOp(context, type, OP, argCount, args) CASE(kIRPseudoOp_PreInc, kIROp_Add); CASE(kIRPseudoOp_PreDec, kIROp_Sub); #undef CASE #define CASE(COMPOUND, OP) \ - case COMPOUND: return emitPostOp(context, type, OP, argCount, args) + case COMPOUND: return emitPostfixIncDecOp(context, type, OP, argCount, args) CASE(kIRPseudoOp_PostInc, kIROp_Add); CASE(kIRPseudoOp_PostDec, kIROp_Sub); #undef CASE |
