From 314795b5d8ff5845624f93e152face325659dd0c Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Tue, 5 Feb 2019 13:46:32 -0800 Subject: 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 --- source/slang/lower-to-ir.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'source') 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 -- cgit v1.2.3