简体   繁体   English

罗斯林:从basicblock获得说明

[英]Roslyn : get instructions from a basicblock

Control flow graph generated in roslyn contains blocks (basicblock) as nodes, each basicblock contains one or more instructions. 在roslyn中生成的控制流程图包含块(basicblock)作为节点,每个basicblock包含一个或多个指令。 for the basicblocks that contains more than one instruction, i try to get all the instructions and their types this is what i did : 对于包含多个指令的basicblock,我尝试获取所有指令及其类型,这就是我所做的:

var cfg = ControlFlowGraph.Create(method);
foreach(var block in cfg.Blocks)
{
    foreach(var operation in block.Operations)
    {
        var syntax = operation.Syntax;
        Console.WriteLine(syntax.Kind());
    }
}

for the following method : 对于以下方法:

public int method(int x, int y)
{
y = 10;
x = y;
return x + y;
}

i get the result : 我得到结果:

ExpressionStatement
ExpressionStatement

but i wan to get the exacte instruction and it's type for example for the instruction x = y; 但是我想得到严格的指令,例如指令x = y;的类型x = y; i want to get AssignmentExpressionSyntax . 我想获得AssignmentExpressionSyntax Also i want to performe some opeartion on each instruction depending on it's type. 我也想根据它的类型对每条指令执行一些操作。

Since you are looking at the syntax kind, the ExpressionStatement is the correct kind for the statement. 由于您正在查看语法类型,因此ExpressionStatement是该语句的正确类型。 You can find the expressions's kind by looking at the kind of the expression within the ExpressionStatement. 您可以通过查看ExpressionStatement中的表达式种类来找到表达式的种类。

if (operation.Syntax is ExpressionStatement es)
{
    var kind = es.Expression.Kind();
}

However, if you are using operations (IOperation) then you can probably get better info by skipping the syntax and using the OperationKind. 但是,如果您正在使用操作(IOperation),则可以通过跳过语法并使用OperationKind获得更好的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM