Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53d4a8e

Browse files
author
Dave Bartolomeo
committed
C++: Refactor IR construction interface
Now that `TInstruction` is shared between IR stages, several of the per-stage IR construction predicates can now be moved into the `Raw` interface exposed only by the initial construction of IR from the ASTs. This also removed a couple predicates that were not used previously at all.
1 parent 1e863ac commit 53d4a8e

15 files changed

Lines changed: 157 additions & 374 deletions

File tree

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ class Instruction extends Construction::TStageInstruction {
200200
* conversion.
201201
*/
202202
final Language::Expr getConvertedResultExpression() {
203-
result = Construction::getInstructionConvertedResultExpression(this)
203+
result = Raw::getInstructionConvertedResultExpression(this)
204204
}
205205

206206
/**
207207
* Gets the unconverted form of the `Expr` whose result is computed by this instruction, if any.
208208
*/
209209
final Language::Expr getUnconvertedResultExpression() {
210-
result = Construction::getInstructionUnconvertedResultExpression(this)
210+
result = Raw::getInstructionUnconvertedResultExpression(this)
211211
}
212212

213213
final Language::LanguageType getResultLanguageType() {
@@ -401,7 +401,7 @@ class Instruction extends Construction::TStageInstruction {
401401
class VariableInstruction extends Instruction {
402402
IRVariable var;
403403

404-
VariableInstruction() { var = Construction::getInstructionVariable(this) }
404+
VariableInstruction() { var = Raw::getInstructionVariable(this) }
405405

406406
override string getImmediateString() { result = var.toString() }
407407

@@ -416,7 +416,7 @@ class VariableInstruction extends Instruction {
416416
class FieldInstruction extends Instruction {
417417
Language::Field field;
418418

419-
FieldInstruction() { field = Construction::getInstructionField(this) }
419+
FieldInstruction() { field = Raw::getInstructionField(this) }
420420

421421
final override string getImmediateString() { result = field.toString() }
422422

@@ -426,7 +426,7 @@ class FieldInstruction extends Instruction {
426426
class FunctionInstruction extends Instruction {
427427
Language::Function funcSymbol;
428428

429-
FunctionInstruction() { funcSymbol = Construction::getInstructionFunction(this) }
429+
FunctionInstruction() { funcSymbol = Raw::getInstructionFunction(this) }
430430

431431
final override string getImmediateString() { result = funcSymbol.toString() }
432432

@@ -436,7 +436,7 @@ class FunctionInstruction extends Instruction {
436436
class ConstantValueInstruction extends Instruction {
437437
string value;
438438

439-
ConstantValueInstruction() { value = Construction::getInstructionConstantValue(this) }
439+
ConstantValueInstruction() { value = Raw::getInstructionConstantValue(this) }
440440

441441
final override string getImmediateString() { result = value }
442442

@@ -446,7 +446,7 @@ class ConstantValueInstruction extends Instruction {
446446
class IndexedInstruction extends Instruction {
447447
int index;
448448

449-
IndexedInstruction() { index = Construction::getInstructionIndex(this) }
449+
IndexedInstruction() { index = Raw::getInstructionIndex(this) }
450450

451451
final override string getImmediateString() { result = index.toString() }
452452

@@ -705,7 +705,7 @@ class PointerArithmeticInstruction extends BinaryInstruction {
705705

706706
PointerArithmeticInstruction() {
707707
getOpcode() instanceof PointerArithmeticOpcode and
708-
elementSize = Construction::getInstructionElementSize(this)
708+
elementSize = Raw::getInstructionElementSize(this)
709709
}
710710

711711
final override string getImmediateString() { result = elementSize.toString() }
@@ -754,7 +754,7 @@ class InheritanceConversionInstruction extends UnaryInstruction {
754754
Language::Class derivedClass;
755755

756756
InheritanceConversionInstruction() {
757-
Construction::getInstructionInheritance(this, baseClass, derivedClass)
757+
Raw::getInstructionInheritance(this, baseClass, derivedClass)
758758
}
759759

760760
final override string getImmediateString() {
@@ -1217,7 +1217,7 @@ class CatchByTypeInstruction extends CatchInstruction {
12171217

12181218
CatchByTypeInstruction() {
12191219
getOpcode() instanceof Opcode::CatchByType and
1220-
exceptionType = Construction::getInstructionExceptionType(this)
1220+
exceptionType = Raw::getInstructionExceptionType(this)
12211221
}
12221222

12231223
final override string getImmediateString() { result = exceptionType.toString() }
@@ -1363,7 +1363,7 @@ class BuiltInOperationInstruction extends Instruction {
13631363

13641364
BuiltInOperationInstruction() {
13651365
getOpcode() instanceof BuiltInOperationOpcode and
1366-
operation = Construction::getInstructionBuiltInOperation(this)
1366+
operation = Raw::getInstructionBuiltInOperation(this)
13671367
}
13681368

13691369
final Language::BuiltInOperation getBuiltInOperation() { result = operation }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import semmle.code.cpp.ir.internal.IRCppLanguage as Language
22
import SSAConstruction as Construction
33
import semmle.code.cpp.ir.implementation.IRConfiguration as IRConfiguration
4+
import semmle.code.cpp.ir.implementation.raw.internal.IRConstruction::Raw as Raw

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/SSAConstruction.qll

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,6 @@ private module Cached {
197197
)
198198
}
199199

200-
cached
201-
Language::Expr getInstructionConvertedResultExpression(Instruction instruction) {
202-
result = getOldInstruction(instruction).getConvertedResultExpression()
203-
}
204-
205-
cached
206-
Language::Expr getInstructionUnconvertedResultExpression(Instruction instruction) {
207-
result = getOldInstruction(instruction).getUnconvertedResultExpression()
208-
}
209-
210200
/*
211201
* This adds Chi nodes to the instruction successor relation; if an instruction has a Chi node,
212202
* that node is its successor in the new successor relation, and the Chi node's successors are
@@ -306,59 +296,6 @@ private module Cached {
306296
instr = unreachedInstruction(result)
307297
}
308298

309-
cached
310-
IRVariable getInstructionVariable(Instruction instruction) {
311-
result =
312-
getNewIRVariable(getOldInstruction(instruction).(OldIR::VariableInstruction).getIRVariable())
313-
}
314-
315-
cached
316-
Language::Field getInstructionField(Instruction instruction) {
317-
result = getOldInstruction(instruction).(OldIR::FieldInstruction).getField()
318-
}
319-
320-
cached
321-
int getInstructionIndex(Instruction instruction) {
322-
result = getOldInstruction(instruction).(OldIR::IndexedInstruction).getIndex()
323-
}
324-
325-
cached
326-
Language::Function getInstructionFunction(Instruction instruction) {
327-
result = getOldInstruction(instruction).(OldIR::FunctionInstruction).getFunctionSymbol()
328-
}
329-
330-
cached
331-
string getInstructionConstantValue(Instruction instruction) {
332-
result = getOldInstruction(instruction).(OldIR::ConstantValueInstruction).getValue()
333-
}
334-
335-
cached
336-
Language::BuiltInOperation getInstructionBuiltInOperation(Instruction instruction) {
337-
result =
338-
getOldInstruction(instruction).(OldIR::BuiltInOperationInstruction).getBuiltInOperation()
339-
}
340-
341-
cached
342-
Language::LanguageType getInstructionExceptionType(Instruction instruction) {
343-
result = getOldInstruction(instruction).(OldIR::CatchByTypeInstruction).getExceptionType()
344-
}
345-
346-
cached
347-
int getInstructionElementSize(Instruction instruction) {
348-
result = getOldInstruction(instruction).(OldIR::PointerArithmeticInstruction).getElementSize()
349-
}
350-
351-
cached
352-
predicate getInstructionInheritance(
353-
Instruction instruction, Language::Class baseClass, Language::Class derivedClass
354-
) {
355-
exists(OldIR::InheritanceConversionInstruction oldInstr |
356-
oldInstr = getOldInstruction(instruction) and
357-
baseClass = oldInstr.getBaseClass() and
358-
derivedClass = oldInstr.getDerivedClass()
359-
)
360-
}
361-
362299
cached
363300
Instruction getPrimaryInstructionForSideEffect(Instruction instruction) {
364301
exists(OldIR::SideEffectInstruction oldInstruction |

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ class Instruction extends Construction::TStageInstruction {
200200
* conversion.
201201
*/
202202
final Language::Expr getConvertedResultExpression() {
203-
result = Construction::getInstructionConvertedResultExpression(this)
203+
result = Raw::getInstructionConvertedResultExpression(this)
204204
}
205205

206206
/**
207207
* Gets the unconverted form of the `Expr` whose result is computed by this instruction, if any.
208208
*/
209209
final Language::Expr getUnconvertedResultExpression() {
210-
result = Construction::getInstructionUnconvertedResultExpression(this)
210+
result = Raw::getInstructionUnconvertedResultExpression(this)
211211
}
212212

213213
final Language::LanguageType getResultLanguageType() {
@@ -401,7 +401,7 @@ class Instruction extends Construction::TStageInstruction {
401401
class VariableInstruction extends Instruction {
402402
IRVariable var;
403403

404-
VariableInstruction() { var = Construction::getInstructionVariable(this) }
404+
VariableInstruction() { var = Raw::getInstructionVariable(this) }
405405

406406
override string getImmediateString() { result = var.toString() }
407407

@@ -416,7 +416,7 @@ class VariableInstruction extends Instruction {
416416
class FieldInstruction extends Instruction {
417417
Language::Field field;
418418

419-
FieldInstruction() { field = Construction::getInstructionField(this) }
419+
FieldInstruction() { field = Raw::getInstructionField(this) }
420420

421421
final override string getImmediateString() { result = field.toString() }
422422

@@ -426,7 +426,7 @@ class FieldInstruction extends Instruction {
426426
class FunctionInstruction extends Instruction {
427427
Language::Function funcSymbol;
428428

429-
FunctionInstruction() { funcSymbol = Construction::getInstructionFunction(this) }
429+
FunctionInstruction() { funcSymbol = Raw::getInstructionFunction(this) }
430430

431431
final override string getImmediateString() { result = funcSymbol.toString() }
432432

@@ -436,7 +436,7 @@ class FunctionInstruction extends Instruction {
436436
class ConstantValueInstruction extends Instruction {
437437
string value;
438438

439-
ConstantValueInstruction() { value = Construction::getInstructionConstantValue(this) }
439+
ConstantValueInstruction() { value = Raw::getInstructionConstantValue(this) }
440440

441441
final override string getImmediateString() { result = value }
442442

@@ -446,7 +446,7 @@ class ConstantValueInstruction extends Instruction {
446446
class IndexedInstruction extends Instruction {
447447
int index;
448448

449-
IndexedInstruction() { index = Construction::getInstructionIndex(this) }
449+
IndexedInstruction() { index = Raw::getInstructionIndex(this) }
450450

451451
final override string getImmediateString() { result = index.toString() }
452452

@@ -705,7 +705,7 @@ class PointerArithmeticInstruction extends BinaryInstruction {
705705

706706
PointerArithmeticInstruction() {
707707
getOpcode() instanceof PointerArithmeticOpcode and
708-
elementSize = Construction::getInstructionElementSize(this)
708+
elementSize = Raw::getInstructionElementSize(this)
709709
}
710710

711711
final override string getImmediateString() { result = elementSize.toString() }
@@ -754,7 +754,7 @@ class InheritanceConversionInstruction extends UnaryInstruction {
754754
Language::Class derivedClass;
755755

756756
InheritanceConversionInstruction() {
757-
Construction::getInstructionInheritance(this, baseClass, derivedClass)
757+
Raw::getInstructionInheritance(this, baseClass, derivedClass)
758758
}
759759

760760
final override string getImmediateString() {
@@ -1217,7 +1217,7 @@ class CatchByTypeInstruction extends CatchInstruction {
12171217

12181218
CatchByTypeInstruction() {
12191219
getOpcode() instanceof Opcode::CatchByType and
1220-
exceptionType = Construction::getInstructionExceptionType(this)
1220+
exceptionType = Raw::getInstructionExceptionType(this)
12211221
}
12221222

12231223
final override string getImmediateString() { result = exceptionType.toString() }
@@ -1363,7 +1363,7 @@ class BuiltInOperationInstruction extends Instruction {
13631363

13641364
BuiltInOperationInstruction() {
13651365
getOpcode() instanceof BuiltInOperationOpcode and
1366-
operation = Construction::getInstructionBuiltInOperation(this)
1366+
operation = Raw::getInstructionBuiltInOperation(this)
13671367
}
13681368

13691369
final Language::BuiltInOperation getBuiltInOperation() { result = operation }

0 commit comments

Comments
 (0)