diff --git a/lib/asm/block.rb b/lib/asm/block.rb index ab783b6a..99f2f6ba 100644 --- a/lib/asm/block.rb +++ b/lib/asm/block.rb @@ -2,11 +2,10 @@ require_relative 'call_instruction' require_relative 'stack_instruction' require_relative 'logic_instruction' require_relative 'memory_instruction' +require_relative "code" module Asm - - class Code ; end - + # A Block is the smalles unit of code, a list of instructions as it were # It is also a point to jump/branch to. An address in the final stream. # To allow for forward branches creation does not fix the position. diff --git a/lib/asm/code.rb b/lib/asm/code.rb new file mode 100644 index 00000000..fa50c374 --- /dev/null +++ b/lib/asm/code.rb @@ -0,0 +1,45 @@ +module Asm + # Base class for anything that we can assemble + + # Derived classes include instructions and data(strings) + + # The commonality abstracted here is the length and position + # and the ability to assemble itself into the stream + + # All code is position independant once assembled. + # But for jumps and calls two passes are neccessary. + # The first setting the position, the second assembling + class Code + + # just sets position to nil, so we can sell that it has not been set + def initialize + @position = nil + end + + # the position in the stream. Think of it as an address if you want. The difference is small. + # Especially since we produce _only_ position independant code + # in other words, during assembly the position _must_ be resolved into a pc relative address + # and not used as is + def position + throw "Not set" unless @address + @address + end + + # The containing class (assembler/function) call this to tell the instruction/data where it is in the + # stream. During assembly the position is then used to calculate pc relative addresses. + def at address + @address = address + end + + # length for this code in bytes + def length + throw "Not implemented #{self}" + end + + # so currently the interface passes the io (usually string_io) in for the code to assemble itself. + # this may change as the writing is still done externally (or that will change) + def assemble(io) + throw "Not implemented #{self}" + end + end +end \ No newline at end of file diff --git a/test/test_all.rb b/test/test_all.rb index 35a27627..be6f73a0 100644 --- a/test/test_all.rb +++ b/test/test_all.rb @@ -1,4 +1,4 @@ require_relative "test_crystal" require_relative "test_small_program" -require_relative "test_runner" +#require_relative "test_runner" require_relative "parser/test_all"