Class: Sexpir::RubyRTLGenerator

Inherits:
Visitor
  • Object
show all
Includes:
Log
Defined in:
lib/sexpir/ruby_rtl_generator.rb

Constant Summary collapse

SEXPIR_TO_RUBY_OP =
{
  "and" => "&",
  "or"  => "|",
  "xor" => "^",
  "not" => "!",
}

Instance Method Summary collapse

Methods included from Log

#indent, #log, #log_dbg, #step

Instance Method Details

#gen_type(type) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sexpir/ruby_rtl_generator.rb', line 41

def gen_type(type)
  case num=type
  when Integer
    case num
    when 1
      return "bit"
    else
      return "bv#{num}"
    end
  else
    return type
  end
end

#generate(circuit) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/sexpir/ruby_rtl_generator.rb', line 11

def generate circuit
  log "[+] generating RubyRTL '#{circuit.name}'"
  code=circuit.accept(self)
  puts code.finalize
  filename=circuit.name.to_s+".rb"
  code.save_as filename
end

#visitAssign(assign, args = nil) ⇒ Object



110
111
112
113
114
# File 'lib/sexpir/ruby_rtl_generator.rb', line 110

def visitAssign assign,args=nil
  lhs=assign.lhs.accept(self)
  rhs=assign.rhs.accept(self)
  "assign(#{lhs} <= #{rhs})"
end

#visitBinary(binary, args = nil) ⇒ Object



194
195
196
197
198
199
# File 'lib/sexpir/ruby_rtl_generator.rb', line 194

def visitBinary binary,args=nil
  lhs=binary.lhs.accept(self)
  rhs=binary.rhs.accept(self)
  op=SEXPIR_TO_RUBY_OP[binary.op] || binary.op
  "(#{lhs} #{op} #{rhs})"
end

#visitBody(body, args = nil) ⇒ Object

statements ===



85
86
87
88
89
# File 'lib/sexpir/ruby_rtl_generator.rb', line 85

def visitBody body,args=nil
  code=Code.new
  body.stmts.each{|stmt| code << stmt.accept(self)}
  code
end

#visitCase(case_, args = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sexpir/ruby_rtl_generator.rb', line 136

def visitCase case_,args=nil
  expr=case_.expr.accept(self)
  code=Code.new
  code << "Case(#{expr}){"
  code.indent=2
  case_.whens.each do |when_|
    code << when_.accept(self)
  end
  code << case_.default.accept(self)
  code.indent=0
  code << "}"
  code
end

#visitCircuit(circuit, args = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sexpir/ruby_rtl_generator.rb', line 19

def visitCircuit circuit,args=nil
  code=Code.new
  code << "require 'ruby_rtl'"
  code.newline
  code << "include RubyRTL"
  code.newline
  code << "class #{circuit.name.capitalize} < Circuit"
  code.indent=2
  code << "def initialize"
  code.indent=4
  circuit.inputs.each{|input| code << input.accept(self)}
  circuit.outputs.each{|output| code << output.accept(self)}
  circuit.signals.each{|signal| code << signal.accept(self)}
  code.newline
  code << circuit.body.accept(self)
  code.indent=2
  code << "end"
  code.indent=0
  code << "end"
  code
end

#visitCombinatorial(comb, args = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/sexpir/ruby_rtl_generator.rb', line 91

def visitCombinatorial comb,args=nil
  code=Code.new
  code << "combinatorial(#{comb.label}){"
  code.indent=2
  code << comb.body.accept(self)
  code.indent=0
  code << "}"
  code
end

#visitComponent(component, args = nil) ⇒ Object

component stuff ====



172
173
174
175
176
# File 'lib/sexpir/ruby_rtl_generator.rb', line 172

def visitComponent component,args=nil
  name=component.name
  type=component.type
  "component :#{name} => #{type.capitalize}"
end

#visitConnect(connect, args = nil) ⇒ Object



178
179
180
181
182
# File 'lib/sexpir/ruby_rtl_generator.rb', line 178

def visitConnect connect,args=nil
  source=connect.source.accept(self)
  sink=connect.sink.accept(self)
  "connect #{source} => #{sink}"
end

#visitConst(const, args = nil) ⇒ Object



209
210
211
# File 'lib/sexpir/ruby_rtl_generator.rb', line 209

def visitConst const,args=nil
  const.value
end

#visitDefault(default_, args = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/sexpir/ruby_rtl_generator.rb', line 161

def visitDefault default_,args=nil
  code=Code.new
  code << "Else{"
  code.indent=2
  code << default_.body.accept(self)
  code.indent=0
  code << "}"
  code
end

#visitExpression(expr, args = nil) ⇒ Object



185
186
# File 'lib/sexpir/ruby_rtl_generator.rb', line 185

def visitExpression expr,args=nil
end

#visitIf(if_, args = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sexpir/ruby_rtl_generator.rb', line 116

def visitIf if_,args=nil
  cond=if_.cond.accept(self)
  code=Code.new
  code << "If(#{cond}){"
  code.indent=2
  code << if_.then.accept(self)
  if if_.else
    code.indent=0
    code << "Else{"
    code.indent=2
    code << if_.else.accept(self)
    code.indent=0
    code << "}"
  else
    code.indent=0
    code << "}"
  end
  code
end

#visitInput(input, args = nil) ⇒ Object



72
73
74
75
76
# File 'lib/sexpir/ruby_rtl_generator.rb', line 72

def visitInput input,args=nil
  input.name
  input.type
  "input :#{input.name} => :#{input.type}"
end

#visitIo(io, args = nil) ⇒ Object



61
62
63
64
# File 'lib/sexpir/ruby_rtl_generator.rb', line 61

def visitIo io,args=nil
  io.name
  io.type
end

#visitOutput(output, args = nil) ⇒ Object



78
79
80
81
82
# File 'lib/sexpir/ruby_rtl_generator.rb', line 78

def visitOutput output,args=nil
  output.name
  output.type
  "output :#{output.name} => :#{output.type}"
end

#visitPort(port, args = nil) ⇒ Object



66
67
68
69
70
# File 'lib/sexpir/ruby_rtl_generator.rb', line 66

def visitPort port,args=nil
  comp=port.component_name
  name=port.name
  "#{comp}.#{name}"
end

#visitSequential(seq, args = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/sexpir/ruby_rtl_generator.rb', line 100

def visitSequential seq,args=nil
  code=Code.new
  code << "sequential(#{seq.label}){"
  code.indent=2
  code << seq.body.accept(self)
  code.indent=0
  code << "}"
  code
end

#visitSignal(sig, args = nil) ⇒ Object



55
56
57
58
59
# File 'lib/sexpir/ruby_rtl_generator.rb', line 55

def visitSignal sig,args=nil
  sig.name
  type=gen_type(sig.type)
  "wire :#{sig.name} => :#{type}"
end

#visitSlice(slice, args = nil) ⇒ Object



213
214
215
216
217
218
# File 'lib/sexpir/ruby_rtl_generator.rb', line 213

def visitSlice slice,args=nil
  expr=slice.expr.accept(self)
  msb=slice.msb.accept(self)
  lsb=slice.lsb.accept(self)
  "#{expr}[#{msb}..#{lsb}]"
end

#visitTerm(term, args = nil) ⇒ Object



201
202
203
# File 'lib/sexpir/ruby_rtl_generator.rb', line 201

def visitTerm term,args=nil
  term
end

#visitVar(var, args = nil) ⇒ Object



205
206
207
# File 'lib/sexpir/ruby_rtl_generator.rb', line 205

def visitVar var,args=nil
  var.name
end

#visitWhen(when_, args = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/sexpir/ruby_rtl_generator.rb', line 150

def visitWhen when_,args=nil
  expr=when_.expr.accept(self)
  code=Code.new
  code << "When(#{expr}){"
  code.indent=2
  code << when_.body.accept(self)
  code.indent=0
  code << "}"
  code
end