Class: ClassHDL::OpertorChain

Inherits:
Object
  • Object
show all
Defined in:
lib/tdl/class_hdl/hdl_redefine_opertor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) ⇒ OpertorChain

Returns a new instance of OpertorChain.



43
44
45
46
47
48
49
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 43

def initialize(arg=nil)
    @tree = [] #[[inst0,symb0],[inst1,symb1],[other_chain,symb2],[other_chain,symb3]]
               # self <symb0> inst0 <symb1> inst1 <symb2> ( other_chain ) <symb3> ( other_chain )
    if arg 
        @tree << arg 
    end  
end

Instance Attribute Details

#instance_add_bracketsObject

Returns the value of attribute instance_add_brackets.



41
42
43
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 41

def instance_add_brackets
  @instance_add_brackets
end

#slaverObject

Returns the value of attribute slaver.



41
42
43
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 41

def slaver
  @slaver
end

#treeObject

Returns the value of attribute tree.



41
42
43
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 41

def tree
  @tree
end

Class Method Details

.define_op_flag(ruby_op, hdl_op) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 106

def self.define_op_flag(ruby_op,hdl_op)
    define_method(ruby_op) do |b|
        if b.is_a? OpertorChain
            b.slaver = true
            unless b.simple_op?
                b.instance_add_brackets = true 
            end
        end   
        # 计算生成新的OpertorChain 是 self 也需要抛弃
        self.slaver = true
        # return self
        new_op =  OpertorChain.new 
        new_op.tree = new_op.tree + self.tree
        new_op.tree.push [b,hdl_op] 
    
        if ClassHDL::AssignDefOpertor.curr_assign_block
            ClassHDL::AssignDefOpertor.curr_assign_block.opertor_chains.push(new_op)
        end
        new_op
    end
end

Instance Method Details

#bracketsObject



96
97
98
99
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 96

def brackets
    self.slaver = true
    new_op = OpertorChain.new(["(#{self.instance})".to_nq])
end

#clog2Object



101
102
103
104
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 101

def clog2
    self.slaver = true
    new_op = OpertorChain.new(["$clog2(#{self.instance})".to_nq])
end

#instance(type = :assign) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 136

def instance(type=:assign)
    AssignDefOpertor.with_rollback_opertors(:old) do 
        str = ''
        # both_symb_used = false
        cnt = 0
        @tree.each do |node|
            if cnt==1
                if node[1].to_s=="<="
                    if type==:always_ff || type==:cond
                        sb = " <= "
                    else 
                        sb = " = "
                    end 
                else 
                    sb = "#{node[1].to_s}"
                end
            else
                sb = "#{node[1].to_s} "
            end
            
            unless node[0].is_a? OpertorChain
                ## 判断是不是属于 Var <= "String" 形式
                if (@tree.length == 2) && node[0].instance_of?(String) && !@slaver
                    str += (sb + '"' + node[0].to_s + '"')
                elsif node[0].instance_of?(String)
                    # "如果是字符串 则原始输出"
                    str += (sb + '"' + node[0].to_s + '"')
                else 
                    str += (sb + node[0].to_s)
                end
            else 
                node[0].slaver = true
                # puts "--------"
                # p node[0].tree
                # puts "========"
                # if node[0].tree.length>2 && ["&","|","<",">"].include?(node[0].tree[1][1])

                # else
                if sb =~/(\||&){2,2}/
                    str += " #{sb}#{node[0].instance(:slaver).to_s}"
                else
                    str += "#{sb}(#{node[0].instance(:slaver).to_s})"
                end
                # end
                # str += "#{sb}(#{"Node"})"
            end
            cnt += 1
        end

        ## 修饰

        # if nege 
        #     str = "~(#{str})"
        # else
        #     str
        # end
        if instance_add_brackets 
            "(#{str})"
        else 
            str
        end
    end
end

#simple_op?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 74

def simple_op?
    ## 判断是不是简单的运算 如 X > 0 ,x & y
    ##[[tmp0[0]], ["FALSE", "&"], [#<ClassHDL::OpertorChain:0x00005557046de088 @tree=[[a_inf.valid], ["1'b0", "|"]], @slaver=true>, "&"]]
    ##[[a_inf.valid], ["1'b0", "|"]
    rel = false
    AssignDefOpertor.with_rollback_opertors(:old) do 
        if tree.size==2 && tree[0].size==1 && !(tree[0][0].is_a?(ClassHDL::OpertorChain)) && !(tree[1][0].is_a?(ClassHDL::OpertorChain))
            rel = true 
        else
            rel = false 
        end
    end
    return rel
end

#to_sObject



132
133
134
# File 'lib/tdl/class_hdl/hdl_redefine_opertor.rb', line 132

def to_s 
    instance(type=:cond)
end