Class: Lignite::Assembler

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

Overview

Assemble a complete RBF program file.

Constant Summary collapse

SIGNATURE =
"LEGO"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VariableDeclarer

#array8, #data16, #data32, #data8, #dataf, #datas

Methods included from Logger

default_logger, #logger

Methods included from Bytes

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

Instance Attribute Details

#globalsVariables (readonly)

Returns:



18
19
20
# File 'lib/lignite/assembler.rb', line 18

def globals
  @globals
end

#objectsArray<RbfObject> (readonly)

Returns:



16
17
18
# File 'lib/lignite/assembler.rb', line 16

def objects
  @objects
end

Instance Method Details

#assemble(rb_filename, rbf_filename, version: 109) ⇒ Object

Assemble a complete RBF program file. (it is OK to reuse an Assembler and call this several times in a sequence) TODO: redesign for Assembler.new(rb_filename).assemble(rbf_filename)?

Parameters:

  • rb_filename (String)

    input

  • rbf_filename (String)

    output



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lignite/assembler.rb', line 25

def assemble(rb_filename, rbf_filename, version: 109)
  rb_text = File.read(rb_filename)
  @objects = []
  @globals = Variables.new

  instance_eval(rb_text, rb_filename, 1) # 1 is the line number

  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: version,
                          object_count: @objects.size,
                          global_bytes: @globals.bytesize)
    f.write(header)
  end
end

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



10
11
12
13
# File 'lib/lignite/assembler.rb', line 10

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

#variablesObject



51
52
53
# File 'lib/lignite/assembler.rb', line 51

def variables
  globals
end

#vmthread(id, &body) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lignite/assembler.rb', line 56

def vmthread(id, &body)
  @locals = Variables.new
  bodyc = BodyCompiler.new(@globals, @locals)
  bodyc.instance_exec(&body)
  bodyc.instance_exec { object_end }
  # FIXME: id is not written?!
  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