vm/codegen/instructions_gen.rb
# Wraps instruction implementation code provided by instructions.rb.
# It also captures some meta info using parsetree to generate
# better output.
#
Wraps instruction implementation code provided by instructions.rb.
It also captures some meta info using parsetree to generate
better output.
Wraps instruction implementation code provided by instructions . rb . It also captures some meta info using parsetree to generate better output .
def generate_superinst_finder
    create = proc { |h,k| h[k] = Hash.new(&create) }
    tree = Hash.new(&create)
    @superinsns.each_with_index do |combo, superop|
      node = tree[combo.first]
      1.upto(combo.size - 1) do |idx|
        node = node[combo[idx]]
      end
      node[:__superop__] = superop
    end
    code = ""
    code << "int find_superop(opcode* stream) {\n"
    code << "  switch(stream[0]) {\n"
    tree.each do |name, node|
      insn = InstructionSet[name]
      code << "  case #{insn.bytecode}: // #{name}\n"
      output_node 4, code, node, insn.width
    end
    code << "  }\n"
    code << "  return -1;\n"
    code << "}\n"
    regular = InstructionSet::OpCodes.size
    code << "int reverse_superop(opcode superop) {\n"
    code << "  static int superops[] = {"
    @superinsns.each_with_index do |combo, superop|
      insn = InstructionSet[combo.first]
      code << " #{insn.bytecode},"
    end
    code << "};\n"
    code << "  if(superop < #{regular}) return superop;\n"
    code << "  return superops[superop - #{regular}];\n"
    code << "}\n"
    return code
  end
def generate_superinst_finder create = proc { | h , k | h [ k ] = Hash . new ( & create ) } tree = Hash . new ( & create ) @superinsns . each_with_index do | combo , superop | node = tree [ combo . first ] 1 . upto ( combo . size - 1 ) do | idx | node = node [ combo [ idx ] ] end node [ :__superop__ ] = superop end code = "" code << "int find_superop(opcode* stream) {\n" code << "  switch(stream[0]) {\n" tree . each do | name , node | insn = InstructionSet [ name ] code << "  case #{insn.bytecode}: // #{name}\n" output_node 4 , code , node , insn . width end code << "  }\n" code << "  return -1;\n" code << "}\n" regular = InstructionSet :: OpCodes . size code << "int reverse_superop(opcode superop) {\n" code << "  static int superops[] = {" @superinsns . each_with_index do | combo , superop | insn = InstructionSet [ combo . first ] code << " #{insn.bytecode}," end code << "};\n" code << "  if(superop < #{regular}) return superop;\n" code << "  return superops[superop - #{regular}];\n" code << "}\n" return code end
Wraps instruction implementation code provided by instructions.rb.
Wraps instruction implementation code provided by instructions . rb .
=begin
    code << "int find_superop(opcode* stream) {\n"
    code << "  opcode one = stream[0];\n"
    code << "  opcode two = 0;\n"
    code << "  switch(one) {\n"
    tree.each do |name, node|
      inst = InstructionSet[name]
      code << "  case #{inst.bytecode}: // #{insn}\n"
      code << "    two = stream[#{inst.width}];\n"
      code << "    switch(two) {\n"
      v.each do |sub, op|
        inst2 = InstructionSet[sub].bytecode
        code << "    case #{inst2}: // #{sub}\n"
        code << "      return #{op + InstructionSet::OpCodes.size};\n"
      end
      code << "    default: return -1;\n"
      code << "    }\n"
    end
    code << "  default: return -1;\n"
    code << "  }\n"
    code << "}\n"
=end