@@ -49,16 +49,6 @@ abstract class TranslatedCall extends TranslatedExpr {
49
49
tag = CallTag ( ) and
50
50
opcode instanceof Opcode:: Call and
51
51
resultType = getTypeForPRValue ( getCallResultType ( ) )
52
- or
53
- tag = CallSideEffectTag ( ) and
54
- opcode = getCallSideEffectOpcode ( expr ) and
55
- (
56
- opcode instanceof Opcode:: CallSideEffect and
57
- resultType = getUnknownType ( )
58
- or
59
- opcode instanceof Opcode:: CallReadSideEffect and
60
- resultType = getVoidType ( )
61
- )
62
52
}
63
53
64
54
override Instruction getChildSuccessor ( TranslatedElement child ) {
@@ -81,25 +71,8 @@ abstract class TranslatedCall extends TranslatedExpr {
81
71
82
72
override Instruction getInstructionSuccessor ( InstructionTag tag , EdgeKind kind ) {
83
73
kind instanceof GotoEdge and
84
- (
85
- (
86
- tag = CallTag ( ) and
87
- if hasSideEffect ( )
88
- then result = getInstruction ( CallSideEffectTag ( ) )
89
- else
90
- if hasPreciseSideEffect ( )
91
- then result = getSideEffects ( ) .getFirstInstruction ( )
92
- else result = getParent ( ) .getChildSuccessor ( this )
93
- )
94
- or
95
- (
96
- hasSideEffect ( ) and
97
- tag = CallSideEffectTag ( ) and
98
- if hasPreciseSideEffect ( )
99
- then result = getSideEffects ( ) .getFirstInstruction ( )
100
- else result = getParent ( ) .getChildSuccessor ( this )
101
- )
102
- )
74
+ tag = CallTag ( ) and
75
+ result = getSideEffects ( ) .getFirstInstruction ( )
103
76
}
104
77
105
78
override Instruction getInstructionRegisterOperand ( InstructionTag tag , OperandTag operandTag ) {
@@ -118,15 +91,6 @@ abstract class TranslatedCall extends TranslatedExpr {
118
91
)
119
92
}
120
93
121
- final override CppType getInstructionMemoryOperandType (
122
- InstructionTag tag , TypedOperandTag operandTag
123
- ) {
124
- tag = CallSideEffectTag ( ) and
125
- hasSideEffect ( ) and
126
- operandTag instanceof SideEffectOperandTag and
127
- result = getUnknownType ( )
128
- }
129
-
130
94
final override Instruction getResult ( ) { result = getInstruction ( CallTag ( ) ) }
131
95
132
96
/**
@@ -197,27 +161,25 @@ abstract class TranslatedCall extends TranslatedExpr {
197
161
*/
198
162
abstract predicate hasArguments ( ) ;
199
163
200
- final private predicate hasSideEffect ( ) { exists ( getCallSideEffectOpcode ( expr ) ) }
201
-
202
- override Instruction getPrimaryInstructionForSideEffect ( InstructionTag tag ) {
203
- hasSideEffect ( ) and
204
- tag = CallSideEffectTag ( ) and
205
- result = getResult ( )
206
- }
207
-
208
- predicate hasPreciseSideEffect ( ) { exists ( getSideEffects ( ) ) }
209
-
210
164
final TranslatedSideEffects getSideEffects ( ) { result .getExpr ( ) = expr }
211
165
}
212
166
167
+ /**
168
+ * The IR translation of the side effects of the parent `TranslatedElement`.
169
+ *
170
+ * This object does not itself generate the side effect instructions. Instead, its children provide
171
+ * the actual side effects, with this object acting as a placeholder so the parent only needs to
172
+ * insert this one element at the point where all the side effects are supposed to occur.
173
+ */
213
174
abstract class TranslatedSideEffects extends TranslatedElement {
175
+ /** Gets the expression whose side effects are being modeled. */
214
176
abstract Expr getExpr ( ) ;
215
177
216
178
final override Locatable getAST ( ) { result = getExpr ( ) }
217
179
218
180
final override Function getFunction ( ) { result = getExpr ( ) .getEnclosingFunction ( ) }
219
181
220
- override TranslatedElement getChild ( int i ) {
182
+ final override TranslatedElement getChild ( int i ) {
221
183
result =
222
184
rank [ i + 1 ] ( TranslatedSideEffect tse , int group , int indexInGroup |
223
185
tse .getPrimaryExpr ( ) = getExpr ( ) and
@@ -236,12 +198,21 @@ abstract class TranslatedSideEffects extends TranslatedElement {
236
198
)
237
199
}
238
200
239
- /**
240
- * Gets the `TranslatedFunction` containing this expression.
241
- */
242
- final TranslatedFunction getEnclosingFunction ( ) {
243
- result = getTranslatedFunction ( getExpr ( ) .getEnclosingFunction ( ) )
201
+ final override predicate hasInstruction ( Opcode opcode , InstructionTag tag , CppType type ) {
202
+ none ( )
203
+ }
204
+
205
+ final override Instruction getFirstInstruction ( ) {
206
+ result = getChild ( 0 ) .getFirstInstruction ( )
207
+ or
208
+ // Some functions, like `std::move()`, have no side effects whatsoever.
209
+ not exists ( getChild ( 0 ) ) and result = getParent ( ) .getChildSuccessor ( this )
244
210
}
211
+
212
+ final override Instruction getInstructionSuccessor ( InstructionTag tag , EdgeKind kind ) { none ( ) }
213
+
214
+ /** Gets the primary instruction to be associated with each side effect instruction. */
215
+ abstract Instruction getPrimaryInstruction ( ) ;
245
216
}
246
217
247
218
/**
@@ -345,64 +316,24 @@ class TranslatedStructorCall extends TranslatedFunctionCall {
345
316
override predicate hasQualifier ( ) { any ( ) }
346
317
}
347
318
348
- class TranslatedAllocationSideEffects extends TranslatedSideEffects ,
349
- TTranslatedAllocationSideEffects {
350
- AllocationExpr expr ;
351
-
352
- TranslatedAllocationSideEffects ( ) { this = TTranslatedAllocationSideEffects ( expr ) }
353
-
354
- final override AllocationExpr getExpr ( ) { result = expr }
355
-
356
- override string toString ( ) { result = "(allocation side effects for " + expr .toString ( ) + ")" }
357
-
358
- override Instruction getFirstInstruction ( ) { result = getInstruction ( OnlyInstructionTag ( ) ) }
359
-
360
- override predicate hasInstruction ( Opcode opcode , InstructionTag tag , CppType type ) {
361
- opcode instanceof Opcode:: InitializeDynamicAllocation and
362
- tag = OnlyInstructionTag ( ) and
363
- type = getUnknownType ( )
364
- }
365
-
366
- override Instruction getInstructionSuccessor ( InstructionTag tag , EdgeKind kind ) {
367
- tag = OnlyInstructionTag ( ) and
368
- kind = EdgeKind:: gotoEdge ( ) and
369
- if exists ( getChild ( 0 ) )
370
- then result = getChild ( 0 ) .getFirstInstruction ( )
371
- else result = getParent ( ) .getChildSuccessor ( this )
372
- }
373
-
374
- override Instruction getInstructionRegisterOperand ( InstructionTag tag , OperandTag operandTag ) {
375
- tag = OnlyInstructionTag ( ) and
376
- operandTag = addressOperand ( ) and
377
- result = getPrimaryInstructionForSideEffect ( OnlyInstructionTag ( ) )
378
- }
379
-
380
- override Instruction getPrimaryInstructionForSideEffect ( InstructionTag tag ) {
381
- tag = OnlyInstructionTag ( ) and
382
- if expr instanceof NewOrNewArrayExpr
383
- then result = getTranslatedAllocatorCall ( expr ) .getInstruction ( CallTag ( ) )
384
- else result = getTranslatedCallInstruction ( expr )
385
- }
386
- }
387
-
319
+ /**
320
+ * The IR translation of the side effects of a function call, including the implicit allocator
321
+ * call in a `new` or `new[]` expression.
322
+ */
388
323
class TranslatedCallSideEffects extends TranslatedSideEffects , TTranslatedCallSideEffects {
389
- Call expr ;
324
+ Expr expr ;
390
325
391
326
TranslatedCallSideEffects ( ) { this = TTranslatedCallSideEffects ( expr ) }
392
327
393
- override string toString ( ) { result = "(side effects for " + expr .toString ( ) + ")" }
394
-
395
- override Call getExpr ( ) { result = expr }
396
-
397
- override predicate hasInstruction ( Opcode opcode , InstructionTag tag , CppType type ) { none ( ) }
328
+ final override string toString ( ) { result = "(side effects for " + expr .toString ( ) + ")" }
398
329
399
- override Instruction getFirstInstruction ( ) { result = getChild ( 0 ) . getFirstInstruction ( ) }
330
+ final override Expr getExpr ( ) { result = expr }
400
331
401
- override Instruction getInstructionSuccessor ( InstructionTag tag , EdgeKind kind ) { none ( ) }
402
-
403
- override Instruction getPrimaryInstructionForSideEffect ( InstructionTag tag ) {
404
- tag = OnlyInstructionTag ( ) and
405
- result = getTranslatedCallInstruction ( expr )
332
+ final override Instruction getPrimaryInstruction ( ) {
333
+ expr instanceof Call and result = getTranslatedCallInstruction ( expr )
334
+ or
335
+ expr instanceof NewOrNewArrayExpr and
336
+ result = getTranslatedAllocatorCall ( expr ) . getInstruction ( CallTag ( ) )
406
337
}
407
338
}
408
339
@@ -441,6 +372,13 @@ abstract class TranslatedSideEffect extends TranslatedElement {
441
372
kind instanceof GotoEdge
442
373
}
443
374
375
+ final override Function getFunction ( ) { result = getParent ( ) .getFunction ( ) }
376
+
377
+ final override Instruction getPrimaryInstructionForSideEffect ( InstructionTag tag ) {
378
+ tag = OnlyInstructionTag ( ) and
379
+ result = getParent ( ) .( TranslatedSideEffects ) .getPrimaryInstruction ( )
380
+ }
381
+
444
382
/**
445
383
* Gets the expression that caused this side effect.
446
384
*
@@ -490,11 +428,6 @@ abstract class TranslatedArgumentSideEffect extends TranslatedSideEffect {
490
428
if isWrite ( ) then group = argumentWriteGroup ( ) else group = argumentReadGroup ( )
491
429
}
492
430
493
- override Instruction getPrimaryInstructionForSideEffect ( InstructionTag tag ) {
494
- tag = OnlyInstructionTag ( ) and
495
- result = getTranslatedCallInstruction ( call )
496
- }
497
-
498
431
final override int getInstructionIndex ( InstructionTag tag ) {
499
432
tag = OnlyInstructionTag ( ) and
500
433
result = index
@@ -507,11 +440,6 @@ abstract class TranslatedArgumentSideEffect extends TranslatedSideEffect {
507
440
result = getTranslatedFunction ( call .getEnclosingFunction ( ) )
508
441
}
509
442
510
- /**
511
- * Gets the `Function` containing this expression.
512
- */
513
- final override Function getFunction ( ) { result = call .getEnclosingFunction ( ) }
514
-
515
443
final override predicate sideEffectInstruction ( Opcode opcode , CppType type ) {
516
444
opcode = sideEffectOpcode and
517
445
(
@@ -638,3 +566,70 @@ class TranslatedStructorQualifierSideEffect extends TranslatedArgumentSideEffect
638
566
)
639
567
}
640
568
}
569
+
570
+ /** The IR translation of the non-argument-specific side effect of a call. */
571
+ class TranslatedCallSideEffect extends TranslatedSideEffect , TTranslatedCallSideEffect {
572
+ Expr expr ;
573
+ SideEffectOpcode sideEffectOpcode ;
574
+
575
+ TranslatedCallSideEffect ( ) { this = TTranslatedCallSideEffect ( expr , sideEffectOpcode ) }
576
+
577
+ override Locatable getAST ( ) { result = expr }
578
+
579
+ override Expr getPrimaryExpr ( ) { result = expr }
580
+
581
+ override predicate sortOrder ( int group , int indexInGroup ) {
582
+ group = callSideEffectGroup ( ) and indexInGroup = 0
583
+ }
584
+
585
+ override string toString ( ) { result = "(call side effect for '" + expr .toString ( ) + "')" }
586
+
587
+ override predicate sideEffectInstruction ( Opcode opcode , CppType type ) {
588
+ opcode = sideEffectOpcode and
589
+ (
590
+ opcode instanceof Opcode:: CallSideEffect and
591
+ type = getUnknownType ( )
592
+ or
593
+ opcode instanceof Opcode:: CallReadSideEffect and
594
+ type = getVoidType ( )
595
+ )
596
+ }
597
+
598
+ override CppType getInstructionMemoryOperandType ( InstructionTag tag , TypedOperandTag operandTag ) {
599
+ tag instanceof OnlyInstructionTag and
600
+ operandTag instanceof SideEffectOperandTag and
601
+ result = getUnknownType ( )
602
+ }
603
+ }
604
+
605
+ /**
606
+ * The IR translation of the allocation side effect of a call to a memory allocation function.
607
+ *
608
+ * This side effect provides a definition for the newly-allocated memory.
609
+ */
610
+ class TranslatedAllocationSideEffect extends TranslatedSideEffect , TTranslatedAllocationSideEffect {
611
+ AllocationExpr expr ;
612
+
613
+ TranslatedAllocationSideEffect ( ) { this = TTranslatedAllocationSideEffect ( expr ) }
614
+
615
+ override Locatable getAST ( ) { result = expr }
616
+
617
+ override Expr getPrimaryExpr ( ) { result = expr }
618
+
619
+ override predicate sortOrder ( int group , int indexInGroup ) {
620
+ group = initializeAllocationGroup ( ) and indexInGroup = 0
621
+ }
622
+
623
+ override string toString ( ) { result = "(allocation side effect for '" + expr .toString ( ) + "')" }
624
+
625
+ override Instruction getInstructionRegisterOperand ( InstructionTag tag , OperandTag operandTag ) {
626
+ tag = OnlyInstructionTag ( ) and
627
+ operandTag = addressOperand ( ) and
628
+ result = getPrimaryInstructionForSideEffect ( OnlyInstructionTag ( ) )
629
+ }
630
+
631
+ override predicate sideEffectInstruction ( Opcode opcode , CppType type ) {
632
+ opcode instanceof Opcode:: InitializeDynamicAllocation and
633
+ type = getUnknownType ( )
634
+ }
635
+ }
0 commit comments