Class: Lignite::Assembler

Inherits:
Object
  • Object
show all
Includes:
Bytes, Logger
Defined in:
lib/lignite/assembler.rb

Constant Summary collapse

SIGNATURE =
"LEGO"

Instance Method Summary collapse

Methods included from Logger

default_logger, #logger

Methods included from Bytes

#f32, #hexdump, #u16, #u32, #u8, #unpack_u16, #unpack_u32, #unpack_u8

Instance Method Details

#assemble(rb_filename, rbf_filename) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lignite/assembler.rb', line 12

def assemble(rb_filename, rbf_filename)
  rb_text = File.read(rb_filename)
  @objects = []
  @global_bytes = 0

  instance_eval(rb_text, rb_filename, 1)

  File.open(rbf_filename, "w") do |f|
    dummy_header = image_header(image_size:0, version: 0, object_count: 0, global_bytes: 0)
    f.write(dummy_header)
    @objects.each do |obj|
      h = obj.header(f.tell)
      f.write(h)
      f.write(obj.body)
      # align??
    end
    size = f.tell
    f.pos = 0
    header = image_header(image_size: size,
                          version: 109,
                          object_count: @objects.size,
                          global_bytes: @global_bytes)
    f.write(header)
  end
end

#image_header(image_size:, version:, object_count:, global_bytes:) ⇒ Object



7
8
9
10
# File 'lib/lignite/assembler.rb', line 7

def image_header(image_size:, version:, object_count:, global_bytes:)
  SIGNATURE + u32(image_size) + u16(version) + u16(object_count) +
    u32(global_bytes)
end

#vmthread(id, &body) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/lignite/assembler.rb', line 38

def vmthread(id, &body)
  @locals = Variables.new
  bodyc = BodyCompiler.new(@locals)
  bodyc.instance_exec(&body)
  bodyc.instance_exec { object_end }
  logger.debug "VMTHREAD #{id}"
  logger.debug "  size #{bodyc.bytes.bytesize}"
  logger.debug "  " + hexdump(bodyc.bytes)
  @objects << RbfObject.vmthread(body: bodyc.bytes, local_bytes: @locals.bytesize)
end