Class: HDLRuby::Low::Code

Inherits:
Base::Code
  • Object
show all
Includes:
Hparent, Low2Symbol
Defined in:
lib/HDLRuby/hruby_db.rb,
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2sym.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_mutable.rb,
lib/HDLRuby/hruby_low_skeleton.rb,
lib/HDLRuby/backend/hruby_c_allocator.rb,
lib/HDLRuby/hdrcc.rb

Overview

Extend the Code class with generation of file for the content.

Direct Known Subclasses

High::Code

Constant Summary

Constants included from Low2Symbol

Low2Symbol::Low2SymbolPrefix, Low2Symbol::Low2SymbolTable, Low2Symbol::Symbol2LowTable

Instance Attribute Summary

Attributes included from Hparent

#parent

Instance Method Summary collapse

Methods included from Low2Symbol

#to_sym

Methods included from Hparent

#scope

Constructor Details

#initializeCode

Creates a new chunk of code.



2489
2490
2491
2492
2493
2494
# File 'lib/HDLRuby/hruby_low.rb', line 2489

def initialize
    # Initialize the set of events.
    @events = []
    # Initialize the content.
    @chunks = HashName.new
end

Instance Method Details

#add_chunk(chunk) ⇒ Object

Adds a +chunk+ to the sensitivity list.



2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
# File 'lib/HDLRuby/hruby_low.rb', line 2497

def add_chunk(chunk)
    # Check and add the chunk.
    unless chunk.is_a?(Chunk)
        raise AnyError,
              "Invalid class for a code chunk: #{chunk.class}"
    end
    # if @chunks.has_key?(chunk.name) then
    if @chunks.include?(chunk) then
        raise AnyError, "Code chunk #{chunk.name} already present."
    end
    # Set its parent.
    chunk.parent = self
    # And add it
    @chunks.add(chunk)
end

#add_event(event) ⇒ Object

Adds an +event+ to the sensitivity list.



2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
# File 'lib/HDLRuby/hruby_low.rb', line 2524

def add_event(event)
    unless event.is_a?(Event)
        raise AnyError, "Invalid class for a event: #{event.class}"
    end
    # Set the event's parent.
    event.parent = self
    # And add the event.
    @events << event
    event
end

#c_code_allocate(allocator) ⇒ Object

Allocates signals within C code using +allocator+.



68
69
70
71
72
73
# File 'lib/HDLRuby/backend/hruby_c_allocator.rb', line 68

def c_code_allocate(allocator)
    # Apply the allocator on each C chunk.
    self.each_chunk do |chunk|
        chunk.c_code_allocate!(allocator) if chunk.name == :c
    end
end

#each_chunk(&ruby_block) ⇒ Object

Iterates over the code chunks.

Returns an enumerator if no ruby block is given.



2516
2517
2518
2519
2520
2521
# File 'lib/HDLRuby/hruby_low.rb', line 2516

def each_chunk(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_chunk) unless ruby_block
    # A ruby block? Apply it on each chunk.
    @chunks.each(&ruby_block)
end

#each_event(&ruby_block) ⇒ Object

Iterates over the events of the sensitivity list.

Returns an enumerator if no ruby block is given.



2538
2539
2540
2541
2542
2543
# File 'lib/HDLRuby/hruby_low.rb', line 2538

def each_event(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_event) unless ruby_block
    # A ruby block? Apply it on each event.
    @events.each(&ruby_block)
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
# File 'lib/HDLRuby/hruby_low.rb', line 2559

def eql?(obj)
    return false unless obj.is_a?(Code)
    idx = 0
    obj.each_event do |event|
        return false unless @events[idx].eql?(event)
        idx += 1
    end
    idx = 0
    obj.each_chunk do |chunk|
        return false unless @chunks[idx].eql?(chunk)
        idx += 1
    end
    return true
end

#has_event?Boolean

Tells if there is any event.

Returns:

  • (Boolean)


2546
2547
2548
# File 'lib/HDLRuby/hruby_low.rb', line 2546

def has_event?
    return !@events.empty?
end

#hashObject

Hash function.



2575
2576
2577
# File 'lib/HDLRuby/hruby_low.rb', line 2575

def hash
    return [@events,@chunk].hash
end

#on_edge?Boolean

Tells if there is a positive or negative edge event.

Returns:

  • (Boolean)


2551
2552
2553
2554
2555
2556
# File 'lib/HDLRuby/hruby_low.rb', line 2551

def on_edge?
    @events.each do |event|
        return true if event.on_edge?
    end
    return false
end

#set_content!(content) ⇒ Object

Sets the content.



1203
1204
1205
1206
1207
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1203

def set_content!(content)
    @content = content
    # Freeze it to avoid dynamic tempering of the hardware.
    content.freeze
end

#set_type!(type) ⇒ Object

Sets the type.



1197
1198
1199
1200
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 1197

def set_type!(type)
    # Check and set type.
    @type = type.to_sym
end

#to_c(level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object.



940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/HDLRuby/hruby_low2c.rb', line 940

def to_c(level = 0)
    # puts "For behavior: #{self}"
    # The resulting string.
    res = ""

    # Declare the global variable holding the behavior.
    res << "Code #{Low2C.obj_name(self)};\n\n"

    # Generate the code of the behavior.
    
    # The header of the behavior.
    res << " " * level*3
    res << "Code #{Low2C.make_name(self)}() {\n"
    res << " " * (level+1)*3

    # Allocate the code.
    res << "Code code = malloc(sizeof(CodeS));\n"
    res << " " * (level+1)*3
    res << "code->kind = CODE;\n";

    # Sets the global variable of the code.
    res << "\n"
    res << " " * (level+1)*3
    res << "#{Low2C.obj_name(self)} = code;\n"

    # Set the owner if any.
    if self.parent then
        res << " " * (level+1)*3
        res << "code->owner = (Object)" + 
               "#{Low2C.obj_name(self.parent)};\n"
    else
        res << "code->owner = NULL;\n"
    end

    # Set the code as inactive. */
    res << " " * (level+1)*3
    res << "code->activated = 0;\n"

    # Add the events and register the code as activable
    # on them.
    res << " " * (level+1)*3
    res << "code->num_events = #{self.each_event.to_a.size};\n"
    res << " " * (level+1)*3
    res << "code->events = calloc(sizeof(Event)," +
           "code->num_events);\n"
    # Process the events.
    events = self.each_event.to_a
    events.each_with_index do |event,i|
        # puts "for event=#{event}"
        # Add the event.
        res << " " * (level+1)*3
        res << "code->events[#{i}] = #{event.to_c};\n"
        
        # Register the behavior as activable on this event.
        # Select the active field.
        field = "any"
        field = "pos" if event.type == :posedge
        field = "neg" if event.type == :negedge
        # Get the target signal access
        sigad = event.ref.resolve.to_c_signal
        # Add the code to the relevant field.
        res << " " * (level+1)*3
        res << "#{sigad}->num_#{field} += 1;\n"
        res << " " * (level+1)*3
        res << "#{sigad}->#{field} = realloc(#{sigad}->#{field}," +
               "#{sigad}->num_#{field}*sizeof(Object));\n"
        res << "#{sigad}->#{field}[#{sigad}->num_#{field}-1] = " +
               "(Object)code;\n"
    end

    # Adds the function to execute.
    function = self.each_chunk.find { |chunk| chunk.name == :sim }
    res << " " * (level+1)*3
    res << "code->function = &#{function.to_c};\n"

    # Generate the Returns of the result.
    res << "\n"
    res << " " * (level+1)*3
    res << "return code;\n"

    # Close the behavior makeing.
    res << " " * level*3
    res << "}\n\n"
    return res
end

#to_chObject

Generates the content of the h file.



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/HDLRuby/hruby_low2c.rb', line 1027

def to_ch
    res = ""
    # Declare the global variable holding the signal.
    res << "extern Behavior #{Low2C.obj_name(self)};\n\n"

    # Generate the access to the function making the behavior.
    res << "extern Behavior #{Low2C.make_name(self)}();\n\n"

    # Generate the accesses to the block of the behavior.
    res << self.block.to_ch

    return res;
end

#to_file(path = "") ⇒ Object

Creates a file in +path+ containing the content of the code.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/HDLRuby/hdrcc.rb', line 205

def to_file(path = "")
    self.each_chunk do |chunk|
        # Process the lumps of the chunk.
        # NOTE: for now use the C code generation of Low2C
        content = chunk.to_c
        # Dump to a file.
        if chunk.name != :sim then 
            # The chunk is to be dumbed to a file.
            # puts "Outputing chunk:#{HDLRuby::Low::Low2C.obj_name(chunk)}"
            outfile = File.open(path + "/" +
                               HDLRuby::Low::Low2C.obj_name(chunk) + "." +
                               chunk.name.to_s,"w")
            outfile << content
            outfile.close
        end
    end
end

#to_high(level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object.



526
527
528
# File 'lib/HDLRuby/hruby_low2high.rb', line 526

def to_high(level = 0)
    return self.content.to_s
end

#to_vhdl(level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object.



1092
1093
1094
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 1092

def to_vhdl(level = 0)
    raise "Code constructs cannot be converted into VHDL."
end