Class: MLIR::Dialect::Ruby::PrismVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/mlir/dialect/ruby/prism_loader.rb

Overview

rubocop:disable Metrics/ClassLength visit prism ast

Constant Summary collapse

CALL_STMT_TPL_STR =
<<~CALL_STMT_TPL.strip
  <%= ssa_var %> = ruby.call <%= receiver_info %>\
  -> "<%= name %>"(<%= args_ssa_values %>) \
  : (<%= arg_types %>) -> <%= ret_type %>
CALL_STMT_TPL
CALL_STMT_TPL =
ERB.new(CALL_STMT_TPL_STR)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ PrismVisitor

Returns a new instance of PrismVisitor.



18
19
20
21
22
23
24
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 18

def initialize(context = nil)
  @context = context || MLIR::CAPI.mlirContextCreate
  @ssa_counter = 0
  @stmts = []
  MLIR::CAPI.register_all_upstream_dialects(@context)
  MLIR::CAPI.mlirDialectHandleRegisterDialect(MLIR::Dialect::Ruby::CAPI.mlirGetDialectHandle__ruby__, @context)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



16
17
18
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 16

def context
  @context
end

#stmtsObject (readonly)

Returns the value of attribute stmts.



16
17
18
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 16

def stmts
  @stmts
end

Instance Method Details

#build_call_stmt(receiver, name, args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 97

def build_call_stmt(receiver, name, args)
  with_new_ssa_var do |ssa_var|
    receiver_info = receiver ? "#{receiver.ssa_var} : #{receiver.type} " : ""
    args_ssa_values = args.map(&:ssa_var).join(", ")
    arg_types = args.map(&:type).join(", ")
    ret_type = "!ruby.opaque_object"
    @stmts << CALL_STMT_TPL.result(binding)
    ret_type
  end
end

#build_int_stmt(value) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 123

def build_int_stmt(value)
  # MLIR::CAPI.mlirBuildIntLit(@context, MLIR::CAPI.mlirIntegerTypeGet(@context, 64), value)
  with_new_ssa_var do |ssa_var|
    ret_type = "!ruby.int"
    @stmts << "  #{ssa_var} = ruby.constant_int \"#{value}\" : #{ret_type}"
    ret_type
  end
end

#build_local_variable_read_stmt(name) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 115

def build_local_variable_read_stmt(name)
  with_new_ssa_var do |ssa_var|
    ret_type = "!ruby.opaque_object"
    @stmts << "  #{ssa_var} = ruby.local_variable_read \"#{name}\" : #{ret_type}"
    ret_type
  end
end

#build_local_variable_write_stmt(name, value) ⇒ Object



108
109
110
111
112
113
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 108

def build_local_variable_write_stmt(name, value)
  with_new_ssa_var do |ssa_var|
    @stmts << "  #{ssa_var} = ruby.local_variable_write \"#{name}\" = #{value.ssa_var} : #{value.type} "
    value.type
  end
end

#build_string_stmt(value) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 132

def build_string_stmt(value)
  with_new_ssa_var do |ssa_var|
    ret_type = "!ruby.string"
    @stmts << "  #{ssa_var} = ruby.constant_str \"#{value}\" : #{ret_type}"
    ret_type
  end
end

#visit(node) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 73

def visit(node)
  type = node.type.to_s
  method_name = "visit_#{type.split("_")[..-2].join("_")}"
  raise "not implemented: #{method_name}" unless respond_to?(method_name)

  send(method_name, node)
end

#visit_arguments(node) ⇒ Object



46
47
48
49
50
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 46

def visit_arguments(node)
  node.arguments.map do |arg|
    visit(arg)
  end
end

#visit_call(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 39

def visit_call(node)
  receiver = node.receiver ? visit(node.receiver) : nil
  name = node.name
  args = visit_arguments(node.arguments)
  build_call_stmt(receiver, name, args)
end

#visit_integer(node) ⇒ Object



56
57
58
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 56

def visit_integer(node)
  build_int_stmt(node.value)
end

#visit_local_variable_read(node) ⇒ Object



65
66
67
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 65

def visit_local_variable_read(node)
  build_local_variable_read_stmt(node.name)
end

#visit_local_variable_write(node) ⇒ Object



60
61
62
63
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 60

def visit_local_variable_write(node)
  value = visit(node.value)
  build_local_variable_write_stmt(node.name, value)
end

#visit_parentheses(node) ⇒ Object



52
53
54
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 52

def visit_parentheses(node)
  visit(node.body)
end

#visit_program(node) ⇒ Object



26
27
28
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 26

def visit_program(node)
  visit_statements(node.statements)
end

#visit_statements(node) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 30

def visit_statements(node)
  ret = nil
  node.body.each do |stmt|
    # pp stmt
    ret = visit(stmt)
  end
  ret
end

#visit_string(node) ⇒ Object



69
70
71
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 69

def visit_string(node)
  build_string_stmt(node.unescaped)
end

#with_new_ssa_varObject



81
82
83
84
85
86
87
88
# File 'lib/mlir/dialect/ruby/prism_loader.rb', line 81

def with_new_ssa_var
  ret = "%#{@ssa_counter}"
  raise "must have a block" unless block_given?

  type = yield ret
  @ssa_counter += 1
  SSARetValue.new(ret, type)
end