class Sexpir::RubyRTLGenerator

Constants

SEXPIR_TO_RUBY_OP

Public Instance Methods

gen_type(type) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

statements ===

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

component stuff ====

# 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) click to toggle source
# 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) click to toggle source
# File lib/sexpir/ruby_rtl_generator.rb, line 209
def visitConst const,args=nil
  const.value
end
visitDefault(default_,args=nil) click to toggle source
# 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) click to toggle source
# File lib/sexpir/ruby_rtl_generator.rb, line 185
def visitExpression expr,args=nil
end
visitIf(if_,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/sexpir/ruby_rtl_generator.rb, line 61
def visitIo io,args=nil
  io.name
  io.type
end
visitOutput(output,args=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/sexpir/ruby_rtl_generator.rb, line 201
def visitTerm term,args=nil
  term
end
visitVar(var,args=nil) click to toggle source
# File lib/sexpir/ruby_rtl_generator.rb, line 205
def visitVar var,args=nil
  var.name
end
visitWhen(when_,args=nil) click to toggle source
# 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