Class: Bytecode

Inherits:
Object
  • Object
show all
Defined in:
lib/microruby/bytecode.rb,
lib/microruby/instruction_set.rb

Constant Summary collapse

TYPES =
{
    :null     => 0,
    :int8     => 1,
    :int16    => 2,
    :int32    => 3,
    :uint8    => 4,
    :uint16   => 5,
    :uint32   => 6,
    :float    => 7,
    :double   => 8,
    :bool     => 9,
    :string   => 10,
    :array    => 11
}
INSTRUCTIONS =
{
    # opcode key => [opcode value, operand count]
    :nop          => [0, 0],
    :getlocal     => [1, 1],
    :setlocal     => [2, 1],
    :putobject    => [17, 1],
    :putstring    => [18, 1],
    :dup          => [31, 0],
    :trace        => [43, 1],
    :leave        => [48, 0],
    :opt_plus     => [59, 1],
    :opt_minus    => [60, 1],
    :opt_mult     => [61, 1],
    :opt_div      => [62, 1],
    :newarray     => [22, 1],
    :expandarray  => [24, 1]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBytecode

Returns a new instance of Bytecode.



28
29
30
# File 'lib/microruby/bytecode.rb', line 28

def initialize
  @bytes = []
end

Instance Attribute Details

#arg_sizeObject

Returns the value of attribute arg_size.



21
22
23
# File 'lib/microruby/bytecode.rb', line 21

def arg_size
  @arg_size
end

#bytesObject

Returns the value of attribute bytes.



25
26
27
# File 'lib/microruby/bytecode.rb', line 25

def bytes
  @bytes
end

#inst_seqObject

Returns the value of attribute inst_seq.



24
25
26
# File 'lib/microruby/bytecode.rb', line 24

def inst_seq
  @inst_seq
end

#local_sizeObject

Returns the value of attribute local_size.



22
23
24
# File 'lib/microruby/bytecode.rb', line 22

def local_size
  @local_size
end

#stack_maxObject

Returns the value of attribute stack_max.



23
24
25
# File 'lib/microruby/bytecode.rb', line 23

def stack_max
  @stack_max
end

Class Method Details

.encode(sequence) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/microruby/bytecode.rb', line 33

def self.encode(sequence)
  bc = Bytecode.new
  bc.arg_size = sequence[4][:arg_size]
  bc.local_size = sequence[4][:local_size]
  bc.stack_max = sequence[4][:stack_max]
  # TODO: preserve line numbers (optionally)
  instructions = sequence[13].find_all {|i| i.is_a?(Array)}
  instructions.each do |i|
    bc.bytes += encode_instruction(i)
  end
  bc
end

Instance Method Details

#to_binObject



47
48
49
# File 'lib/microruby/bytecode.rb', line 47

def to_bin
  self.bytes.pack("C*")
end