Class: RubyRTL::TypeChecker

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

Instance Method Summary collapse

Methods inherited from Visitor

#visit, #visitBitLit, #visitBitType, #visitBitVectorType, #visitBody, #visitCase, #visitCircuitPart, #visitCombinatorial, #visitComment, #visitCompDecl, #visitElse, #visitElsif, #visitFsm, #visitIf, #visitIntType, #visitInteger, #visitLiteral, #visitNext, #visitPort, #visitRIntType, #visitRUintType, #visitRecordType, #visitSequential, #visitSigDecl, #visitState, #visitType, #visitTypeDecl, #visitUIntType, #visitUnary, #visitWhen

Constructor Details

#initializeTypeChecker

Returns a new instance of TypeChecker.



6
7
8
# File 'lib/ruby_rtl/type_checker.rb', line 6

def initialize
  @ppr=DSLPrinter.new
end

Instance Method Details

#cast(t1, t2) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_rtl/type_checker.rb', line 20

def cast t1,t2
  pair=[t1.class,t2.class]
  cast=nil
  case pair
  when [BitType,BitType]
  when [BitType,UIntType]
    cast=[:to_bit,1]
  when [BitType,IntType]
    cast=[:to_bit,1]

  when [BitType,RUIntType]
    cast=[:to_bit,1]

  when [IntType,BitType]
    cast=[:to_bit,1]
  when [IntType,IntType]
    if t1.bitwidth==t2.bitwidth
      return #nothing to do
    else
      cast=[:to_int,t1.bitwidth]
    end
  when [IntType,UIntType]
    unless t1.bitwidth==t2.bitwidth
      cast=[:to_int,t1.bitwidth]
    end
  when [IntType,RUIntType]
    unless t1.bitwidth == t2.bitwidth
      cast=[:to_int,t1.bitwidth]
    end
  when [UIntType,BitType]
    cast=[:to_uint,t1.bitwidth]
  when [UIntType,RIntType]
    cast=[:to_uint,t1.bitwidth]
  when [UIntType,RUIntType]
    cast=[:to_uint,t1.bitwidth]

  when [UIntType,IntType]
    u_bw,s_bw=t1.bitwidth,t2.bitwidth
    cast=[:to_uint,u_bw]
  when [UIntType,UIntType]
    if t1.bitwidth==t2.bitwidth
      return #nothing to do
    else
      cast=[:to_uint,t1.bitwidth]
    end
  #============ bit vector =============
  when [BitVectorType,UIntType]
    cast=[:to_bv,t1.bitwidth]
  when [BitVectorType,RUIntType]
    cast=[:to_bv,t1.bitwidth]
  when [RecordType,RecordType]
  else
    raise "NIY cast #{pair}"
  end
  return cast
end

#check(circuit) ⇒ Object



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

def check circuit
  puts "[+] type checking"
  root=circuit.ast
  if root=circuit.ast
    #root.ios.each{|io| io.accept(self)}
    root.decls.each{|decl| decl.accept(self)}
    root.body.accept(self)
  end
end

#homogenize(t1, op, t2) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ruby_rtl/type_checker.rb', line 89

def homogenize t1,op,t2
  pair=[t1.class,t2.class]
  conv=[nil,nil,t1]
  case pair
  when [BitType,BitType]
    unless ["&","|","^"].include?(op)
      raise "illegal operation '#{op}' between 2 bits"
    end
  when [BitType,UIntType]
    conv=[["to_uint",t2.bitwidth],nil,t2]
  when [BitType,IntType]
    conv=[["to_int",t2.bitwidth],nil,t2]
  when [BitType,RIntType]
    conv=[["to_int",t2.bitwidth],nil,t2]
  when [BitType,RUIntType]
    conv=[["to_uint",t2.bitwidth],nil,t2]

  when [IntType,BitType]
    conv=[nil,["to_int",t1.bitwidth],t1]
  when [IntType,IntType]
    unless t1.bitwidth==t2.bitwidth
      max=[t1.bitwidth,t2.bitwidth].max
      if t1==max
        conv=[nil,["to_int",max],t1]
      else
        conv=[["to_int",max],nil,t2]
      end
    end
  when [IntType,UIntType]
    max=[t1.bitwidth,t2.bitwidth].max
    if t1==max
      conv=[nil,["to_int",max],t1]
    else
      conv=[["to_uint",max],nil,t2]
    end
  when [IntType,RUIntType]
    unless t1.bitwidth >= t2.bitwidth # note >=
      max=[t1.bitwidth,t2.bitwidth].max
      if t1==max
        conv=[nil,["to_int",max],t1]
      else
        conv=[["to_int",max],nil,t2]
      end
    end
  when [UIntType,BitType]
    conv=[nil,["to_uint",t1.bitwidth],t1]
  when [UIntType,IntType]
    u_bw,s_bw=t1.bitwidth,t2.bitwidth
    max=[u_bw,s_bw].max
    type=IntType.new(max)
    conv=[["to_int",max],["to_int",max],type]
  when [UIntType,UIntType]
    unless t1.bitwidth==t2.bitwidth
      max=[t1.bitwidth,t2.bitwidth].max
      if t1==max
        conv=[nil,["to_uint",max],t1]
      else
        conv=[["to_uint",max],nil,t2]
      end
    end
  when [RecordType,RecordType]
  else
    raise "NIY homogenize #{pair}"
  end
  pp conv
  return conv
end

#visitAssign(assign, args = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby_rtl/type_checker.rb', line 77

def visitAssign assign,args=nil
  puts "- assign : #{assign.accept(@ppr)}"
  lhs=assign.lhs.accept(self)
  rhs=assign.rhs.accept(self)
  puts "\ttypes : #{lhs.to_s} <= #{rhs.to_s}"
  cast_func=cast(lhs,rhs)
  if cast_func
    name,bw=*cast_func
    assign.rhs=FuncCall.new(name,[assign.rhs,bw])
  end
end

#visitBinary(bin, args = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ruby_rtl/type_checker.rb', line 157

def visitBinary bin,args=nil
  puts "- binary #{bin.accept(@ppr)}"
  lhs_t=bin.lhs.accept(self)
  rhs_t=bin.rhs.accept(self)
  puts "\ttypes : #{lhs_t} #{bin.op} #{rhs_t}"
  conv_funcs=homogenize(lhs_t,bin.op,rhs_t)
  pp conv_funcs
  if conv=conv_funcs[0]
    name,bitwidth=*conv
    bin.lhs=FuncCall.new(name,[bin.lhs,bitwidth])
  end
  if conv=conv_funcs[1]
    name,bitwidth=*conv
    bin.rhs=FuncCall.new(name,[bin.rhs,bitwidth])
  end
  bin.type=conv_funcs[2]
end

#visitIndexed(indexed, args = nil) ⇒ Object



179
180
181
# File 'lib/ruby_rtl/type_checker.rb', line 179

def visitIndexed indexed, args=nil
  indexed.type
end

#visitInput(input, args = nil) ⇒ Object



183
184
185
# File 'lib/ruby_rtl/type_checker.rb', line 183

def visitInput input,args=nil
  input.type
end

#visitIntLit(lit, args = nil) ⇒ Object

literals



192
193
194
# File 'lib/ruby_rtl/type_checker.rb', line 192

def visitIntLit lit,args=nil
  lit.type
end

#visitOutput(output, args = nil) ⇒ Object



187
188
189
# File 'lib/ruby_rtl/type_checker.rb', line 187

def visitOutput output,args=nil
  output.type
end

#visitRIntLit(lit, args = nil) ⇒ Object



200
201
202
# File 'lib/ruby_rtl/type_checker.rb', line 200

def visitRIntLit lit,args=nil
  lit.type
end

#visitRUIntLit(lit, args = nil) ⇒ Object



204
205
206
# File 'lib/ruby_rtl/type_checker.rb', line 204

def visitRUIntLit lit,args=nil
  lit.type
end

#visitSig(sig, args = nil) ⇒ Object



175
176
177
# File 'lib/ruby_rtl/type_checker.rb', line 175

def visitSig sig,args=nil
  sig.type
end

#visitUIntLit(lit, args = nil) ⇒ Object



196
197
198
# File 'lib/ruby_rtl/type_checker.rb', line 196

def visitUIntLit lit,args=nil
  lit.type
end