4 # Common base class for code generators.
6 # Code generators are expected to implement the following methods:
16 class CommonCodeGenerator
18 @@gensym_mutex = Mutex.new
20 # Initializes the code generator.
21 # _params_ shall be a hash containing parameters to the code generator,
22 # and shall at least contain the keys <tt>:architecture</tt> and
23 # <tt>:format</tt>, specifying the target architecture and output
24 # format, respectively.
25 def initialize params = {}
26 @architecture = params[:architecture] || Config.default_architecture
27 @format = params[:format] || Config.default_format
28 @sections = { :code => '' }
31 @unused_temporaries = []
35 # Declare that a temporary variable is no longer in use
36 def free_temporary name
37 @unused_temporaries.unshift name
40 # Generate a new, unused symbol
42 @@gensym_mutex.synchronize do
43 @@gensym_counter = @@gensym_counter + 1
44 "_G#{@@gensym_counter}".to_sym
48 # Get a temporary variable name
50 if @unused_temporaries.empty?
54 # Reuse a temporary that wasn't in use anymore
55 name = @unused_temporaries.pop
57 @temporaries.push name
61 # Given an input file name, returns the canonical output file name
62 # for this code generator.
63 def output_file_name input_name
64 input_name.sub(/\.sb$/, '') + '.o'