Class: RubyRTL::DSLPrinter

Inherits:
Visitor
  • Object
show all
Defined in:
lib/ruby_rtl/dsl_printer.rb

Direct Known Subclasses

SexpGenerator

Instance Method Summary collapse

Methods inherited from Visitor

#visit, #visitCircuitPart, #visitInteger, #visitPort, #visitType, #visitWhen

Instance Method Details



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby_rtl/dsl_printer.rb', line 8

def print circuit
  code=Code.new
  code << "# code generated by DSL printer"
  if root=circuit.ast
    code << "circuit #{circuit.name}"
    #root.ios.each{|io|     code << io.accept(self)}
    root.decls.each{|decl| code << decl.accept(self)}
    code << root.body.accept(self)
    puts code.finalize
  end
end

#visitAssign(node, args = nil) ⇒ Object



91
92
93
94
95
# File 'lib/ruby_rtl/dsl_printer.rb', line 91

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

#visitBinary(node, args = nil) ⇒ Object

expr ===



166
167
168
169
170
171
# File 'lib/ruby_rtl/dsl_printer.rb', line 166

def visitBinary node,args=nil
  lhs=node.lhs.accept(self)
  op=node.op
  rhs=node.rhs.accept(self)
  "(#{lhs} #{op} #{rhs})"
end

#visitBitLit(node, args = nil) ⇒ Object



195
196
197
# File 'lib/ruby_rtl/dsl_printer.rb', line 195

def visitBitLit node,args=nil
  node.super.accept(self)
end

#visitBitType(node, args = nil) ⇒ Object

types ===



216
217
218
# File 'lib/ruby_rtl/dsl_printer.rb', line 216

def visitBitType node,args=nil
  "bit"
end

#visitBitVectorType(bvt, args = nil) ⇒ Object



220
221
222
# File 'lib/ruby_rtl/dsl_printer.rb', line 220

def visitBitVectorType bvt,args=nil
  "bv#{bvt.bitwidth}"
end

#visitBody(body, args = nil) ⇒ Object

statements



85
86
87
88
89
# File 'lib/ruby_rtl/dsl_printer.rb', line 85

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

#visitCase(case_, args = nil) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/ruby_rtl/dsl_printer.rb', line 257

def visitCase case_,args=nil
  code=Code.new
  code << "Case(){"
  code.indent=2
  code << case_.body.accept(self)
  code.indent=0
  code << "}"
  code
end

#visitCombinatorial(comb, args = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_rtl/dsl_printer.rb', line 62

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

#visitComment(node, args = nil) ⇒ Object



20
21
# File 'lib/ruby_rtl/dsl_printer.rb', line 20

def visitComment node,args=nil
end

#visitCompDecl(decl, args = nil) ⇒ Object



58
59
60
# File 'lib/ruby_rtl/dsl_printer.rb', line 58

def visitCompDecl decl,args=nil
  "component #{decl.name} => #{decl.comp}"
end

#visitElse(else_, args = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_rtl/dsl_printer.rb', line 117

def visitElse else_,args=nil
  body=else_.body.accept(self)
  code=Code.new
  code << "else {"
  code.indent=2
  code << body
  code.indent=0
  code << "}"
  code
end

#visitElsif(elsif_, args = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_rtl/dsl_printer.rb', line 128

def visitElsif elsif_,args=nil
  cond=elsif_.cond.accept(self)
  body=elsif_.body.accept(self)
  code=Code.new
  code << "elsif #{cond}{"
  code.indent=2
  code << body
  code.indent=0
  code << "}"
  code
end

#visitEnumType(enum_type, args = nil) ⇒ Object



248
249
250
# File 'lib/ruby_rtl/dsl_printer.rb', line 248

def visitEnumType enum_type,args=nil
  "Enum(#{enum_type.items.join(",")})"
end

#visitFsm(fsm, args = nil) ⇒ Object

fsm



141
142
143
144
145
146
147
148
149
# File 'lib/ruby_rtl/dsl_printer.rb', line 141

def visitFsm fsm,args=nil
  code=Code.new
  code << "fsm(#{fsm.name}){"
  code.indent=2
  code << fsm.body.accept(self)
  code.indent=0
  code << "}"
  code
end

#visitFuncCall(func, args = nil) ⇒ Object



178
179
180
181
182
# File 'lib/ruby_rtl/dsl_printer.rb', line 178

def visitFuncCall func,args=nil
  name=func.name
  argus=func.args.collect{|arg| arg.accept(self)}.join(',')
  "#{name}(#{argus})"
end

#visitIf(node, args = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ruby_rtl/dsl_printer.rb', line 97

def visitIf node,args=nil
  cond=node.cond.accept(self)
  body=node.body.accept(self)
  elsifs_=Code.new
  node.elsifs.collect{|elsif_| elsifs_ << elsif_.accept(self)}
  else_=node.else.accept(self) if node.else
  code=Code.new
  code << "if #{cond}{"
  code.indent=2
  code << body
  code.indent=0
  if elsifs_.size>0 or node.else
    code << "}"
  end
  code << elsifs_
  code << else_ if node.else
  code << "}" unless node.else or elsifs_.size>0
  code
end

#visitIndexed(indexed, args = nil) ⇒ Object



184
185
186
187
188
# File 'lib/ruby_rtl/dsl_printer.rb', line 184

def visitIndexed indexed,args=nil
  lhs=indexed.lhs.accept(self)
  rhs=indexed.rhs.accept(self)
  "#{lhs}[#{rhs}]"
end

#visitInput(input, args = nil) ⇒ Object



27
28
29
# File 'lib/ruby_rtl/dsl_printer.rb', line 27

def visitInput input,args=nil
  input.name
end

#visitIntLit(lit, args = nil) ⇒ Object



199
200
201
# File 'lib/ruby_rtl/dsl_printer.rb', line 199

def visitIntLit lit,args=nil
  lit.val
end

#visitIntType(node, args = nil) ⇒ Object



224
225
226
# File 'lib/ruby_rtl/dsl_printer.rb', line 224

def visitIntType node,args=nil
  "int#{node.bitwidth}"
end

#visitLiteral(node, args = nil) ⇒ Object

literals ===



191
192
193
# File 'lib/ruby_rtl/dsl_printer.rb', line 191

def visitLiteral node,args=nil
  node.val
end

#visitMemoryType(mem_type, args = nil) ⇒ Object



252
253
254
255
# File 'lib/ruby_rtl/dsl_printer.rb', line 252

def visitMemoryType mem_type,args=nil
  typename=mem_type.type
  "Memory(#{mem_type.size},#{typename})"
end

#visitNext(node, args = nil) ⇒ Object



161
162
163
# File 'lib/ruby_rtl/dsl_printer.rb', line 161

def visitNext node,args=nil
  "next_state #{node.name}"
end

#visitOutput(output, args = nil) ⇒ Object



31
32
33
# File 'lib/ruby_rtl/dsl_printer.rb', line 31

def visitOutput output,args=nil
  output.name
end

#visitRecordType(rec_type, args = nil) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/ruby_rtl/dsl_printer.rb', line 240

def visitRecordType rec_type,args=nil
  items=[]
  rec_type.hash.each{|item,type|
    items << "#{item} => #{type}"
  }
  "Record(#{items.join(",")})"
end

#visitRIntLit(lit, args = nil) ⇒ Object



207
208
209
# File 'lib/ruby_rtl/dsl_printer.rb', line 207

def visitRIntLit lit,args=nil
  lit.val
end

#visitRIntType(node, args = nil) ⇒ Object



228
229
230
# File 'lib/ruby_rtl/dsl_printer.rb', line 228

def visitRIntType node,args=nil
  "rint#{node.bitwidth}"
end

#visitRUIntLit(lit, args = nil) ⇒ Object



211
212
213
# File 'lib/ruby_rtl/dsl_printer.rb', line 211

def visitRUIntLit lit,args=nil
  lit.val
end

#visitRUintType(node, args = nil) ⇒ Object



232
233
234
# File 'lib/ruby_rtl/dsl_printer.rb', line 232

def visitRUintType node,args=nil
  "ruint#{node.bitwidth}"
end

#visitSequential(seq, args = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby_rtl/dsl_printer.rb', line 73

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

#visitSig(sig, args = nil) ⇒ Object



23
24
25
# File 'lib/ruby_rtl/dsl_printer.rb', line 23

def visitSig sig,args=nil
  sig.name
end

#visitSigDecl(decl, args = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_rtl/dsl_printer.rb', line 40

def visitSigDecl decl,args=nil
  name=(sig=decl.sig).accept(self)
  type=sig.type.accept(self)
  if (values=$typedefs.values).include?(sig.type)
    idx=values.index(sig.type)
    type=$typedefs.keys[idx]
  end
  case sig
  when Input
    kind="input "
  when Output
    kind="output"
  when Sig
    kind="wire"
  end
  "#{kind} #{name} => #{type}"
end

#visitState(state, args = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/ruby_rtl/dsl_printer.rb', line 151

def visitState state,args=nil
  code=Code.new
  code << "state(#{state.name}){"
  code.indent=2
  code << state.body.accept(self)
  code.indent=0
  code << "}"
  code
end

#visitTypeDecl(decl, args = nil) ⇒ Object



35
36
37
38
# File 'lib/ruby_rtl/dsl_printer.rb', line 35

def visitTypeDecl decl,args=nil
  definition=decl.definition.accept(self)
  return "typedef #{decl.name} => #{definition}"
end

#visitUIntLit(lit, args = nil) ⇒ Object



203
204
205
# File 'lib/ruby_rtl/dsl_printer.rb', line 203

def visitUIntLit lit,args=nil
  lit.val
end

#visitUIntType(node, args = nil) ⇒ Object



236
237
238
# File 'lib/ruby_rtl/dsl_printer.rb', line 236

def visitUIntType node,args=nil
  "uint#{node.bitwidth}"
end

#visitUnary(node, args = nil) ⇒ Object



173
174
175
176
# File 'lib/ruby_rtl/dsl_printer.rb', line 173

def visitUnary node,args=nil
  expr=node.expr.accept(self)
  "(#{node.op} #{expr})"
end