module Voodoo
  # This module implements the old code generation API, consisting of
  # the following methods:
  #
  # #add_code
  # #add_code_label
  # #add_data
  # #add_data_label
  # #add_function
  # #add_function_label
  # #align_code
  # #align_data
  # #align_function
  #
  module GeneratorApi1
    # Add code.
    #
    # Parameters:
    # [actions] an Array of actions to be added
    #
    # Example:
    #   add_code [:call, :exit, 0]
    def add_code *actions
      add :code, *actions
    end

    # Add a label at the current address in the code section.
    #
    # Example:
    #   add_code_label :mylabel
    def add_code_label name
      in_section(:code) { label name }
    end

    # Add data.
    #
    # Parameters:
    # [defs] an Array of data definitions
    #
    # Example:
    #   add_data [:word, 42]
    def add_data *defs
      add :data, *defs
    end

    # Add a label at the current address in the data section.
    #
    # Example:
    #   add_data_label :msg
    def add_data_label name
      add :data, [:label, name]
    end

    # Add a label at the address where the next function will be generated.
    #
    # Example:
    #   add_function_label :add_one
    def add_function_label name
      add :functions, [:label, name]
    end

    # Align code on the next _alignment_-byte boundary.
    # If _alignment_ is not specified, the default code alignment
    # is used.
    def align_code alignment = nil
      if alignment
        add :code, [:align, alignment]
      else
        add :code, [:align]
      end
    end

    # Align data on the next _alignment_-byte boundary.
    # If _alignment_ is not specified, the default data alignment
    # is used.
    def align_data alignment = nil
      if alignment
        add :data, [:align, alignment]
      else
        add :data, [:align]
      end
    end

    # Align function on the next _alignment_-byte boundary.
    # If _alignment_ is not specified, the default function alignment
    # is used.
    def align_function alignment = nil
      if alignment
        add :functions, [:align, alignment]
      else
        add :functions, [:align]
      end
    end
  end
end
