Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Mermaid #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Control flow graphs to mermaid
  • Loading branch information
kddnewton committed Feb 6, 2023
commit e7c5adf1de9fcac198fdbbdc1350515c3bf02210
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Style/CaseLikeIf:
Style/ClassVars:
Enabled: false

Style/CombinableLoops:
Enabled: false

Style/DocumentDynamicEvalDefinition:
Enabled: false

Expand Down
8 changes: 4 additions & 4 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ def to_json(*opts)
accept(Visitor::JSONVisitor.new).to_json(*opts)
end

def construct_keys
PrettierPrint.format(+"") { |q| accept(Visitor::MatchVisitor.new(q)) }
def to_mermaid
accept(Visitor::MermaidVisitor.new)
end

def mermaid
accept(Visitor::MermaidVisitor.new)
def construct_keys
PrettierPrint.format(+"") { |q| accept(Visitor::MatchVisitor.new(q)) }
end
end

Expand Down
34 changes: 34 additions & 0 deletions lib/syntax_tree/yarv/control_flow_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ def disasm
fmt.string
end

def to_mermaid
output = StringIO.new
output.puts("flowchart TD")

fmt = Disassembler::Mermaid.new
blocks.each do |block|
output.puts(" subgraph #{block.id}")
previous = nil

block.each_with_length do |insn, length|
node_id = "node_#{length}"
label = "%04d %s" % [length, insn.disasm(fmt)]

output.puts(" #{node_id}(\"#{CGI.escapeHTML(label)}\")")
output.puts(" #{previous} --> #{node_id}") if previous

previous = node_id
end

output.puts(" end")
end

blocks.each do |block|
block.outgoing_blocks.each do |outgoing|
offset =
block.block_start + block.insns.sum(&:length) -
block.insns.last.length
output.puts(" node_#{offset} --> node_#{outgoing.block_start}")
end
end

output.string
end

# This method is used to verify that the control flow graph is well
# formed. It does this by checking that each basic block is itself well
# formed.
Expand Down
35 changes: 35 additions & 0 deletions lib/syntax_tree/yarv/disassembler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@
module SyntaxTree
module YARV
class Disassembler
# This class is another object that handles disassembling a YARV
# instruction sequence but it does so in order to provide a label for a
# mermaid diagram.
class Mermaid
def calldata(value)
value.inspect
end

def enqueue(iseq)
end

def event(name)
end

def inline_storage(cache)
"<is:#{cache}>"
end

def instruction(name, operands = [])
operands.empty? ? name : "#{name} #{operands.join(", ")}"
end

def label(value)
"%04d" % value.name["label_".length..]
end

def local(index, **)
index.inspect
end

def object(value)
value.inspect
end
end

attr_reader :output, :queue

attr_reader :current_prefix
Expand Down