Class: Bytecode
- Inherits:
-
Object
- Object
- Bytecode
- 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
-
#arg_size ⇒ Object
Returns the value of attribute arg_size.
-
#bytes ⇒ Object
Returns the value of attribute bytes.
-
#inst_seq ⇒ Object
Returns the value of attribute inst_seq.
-
#local_size ⇒ Object
Returns the value of attribute local_size.
-
#stack_max ⇒ Object
Returns the value of attribute stack_max.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Bytecode
constructor
A new instance of Bytecode.
- #to_bin ⇒ Object
Constructor Details
#initialize ⇒ Bytecode
Returns a new instance of Bytecode.
28 29 30 |
# File 'lib/microruby/bytecode.rb', line 28 def initialize @bytes = [] end |
Instance Attribute Details
#arg_size ⇒ Object
Returns the value of attribute arg_size.
21 22 23 |
# File 'lib/microruby/bytecode.rb', line 21 def arg_size @arg_size end |
#bytes ⇒ Object
Returns the value of attribute bytes.
25 26 27 |
# File 'lib/microruby/bytecode.rb', line 25 def bytes @bytes end |
#inst_seq ⇒ Object
Returns the value of attribute inst_seq.
24 25 26 |
# File 'lib/microruby/bytecode.rb', line 24 def inst_seq @inst_seq end |
#local_size ⇒ Object
Returns the value of attribute local_size.
22 23 24 |
# File 'lib/microruby/bytecode.rb', line 22 def local_size @local_size end |
#stack_max ⇒ Object
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_bin ⇒ Object
47 48 49 |
# File 'lib/microruby/bytecode.rb', line 47 def to_bin self.bytes.pack("C*") end |