Module: HDLRuby::High::High2C

Defined in:
lib/HDLRuby/hruby_rcsim.rb

Overview

Provides tools for converting HDLRuby::High objects to C.

Constant Summary collapse

@@hdrobj2c =
{}

Class Method Summary collapse

Class Method Details

.c_name(name) ⇒ Object

Converts a +name+ to a C-compatible name.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/HDLRuby/hruby_rcsim.rb', line 32

def self.c_name(name)
    name = name.to_s
    # Convert special characters.
    name = name.each_char.map do |c|
        if c=~ /[a-z0-9]/ then
            c
        elsif c == "_" then
            "__"
        else
            "_" + c.ord.to_s
        end
    end.join
    # First character: only letter is possible.
    unless name[0] =~ /[a-z_]/ then
        name = "_" + name
    end
    return name
end

.c_string(str) ⇒ Object

Converts string +str+ to a C-compatible string.



25
26
27
28
29
# File 'lib/HDLRuby/hruby_rcsim.rb', line 25

def self.c_string(str)
    str = str.gsub(/\n/,"\\n")
    str.gsub!(/\t/,"\\t")
    return str
end

.int_widthObject

Gives the width of an int in the current computer.



19
20
21
22
# File 'lib/HDLRuby/hruby_rcsim.rb', line 19

def self.int_width
    # puts "int_width=#{[1.to_i].pack("i").size*8}"
    return [1.to_i].pack("i").size*8
end

.obj_name(obj) ⇒ Object

Generates a uniq name for an object.



54
55
56
57
58
59
60
61
62
# File 'lib/HDLRuby/hruby_rcsim.rb', line 54

def self.obj_name(obj)
    id = obj.hierarchy.map! {|obj| obj.object_id}
    oname = @@hdrobj2c[id]
    unless oname then
        oname = "_" << @@hdrobj2c.size.to_s(36)
        @@hdrobj2c[id] = oname
    end
    return oname
end

.type_name(obj) ⇒ Object

Generates the name of a type.



65
66
67
# File 'lib/HDLRuby/hruby_rcsim.rb', line 65

def self.type_name(obj)
    return "type#{Low2C.obj_name(obj)}"
end

.unit_name(obj) ⇒ Object

Generates the name of a unit.



70
71
72
# File 'lib/HDLRuby/hruby_rcsim.rb', line 70

def self.unit_name(obj)
    return "#{obj.to_s.upcase}"
end