repeat until no more exception

move class to where it is used
This commit is contained in:
Torsten Ruger 2018-03-28 19:49:16 +03:00
parent 5eee79719d
commit ce58de2671
2 changed files with 31 additions and 7 deletions

View File

@ -1,6 +1,4 @@
module Risc module Risc
class LinkException < Exception
end
# Assemble the object machine into a binary. # Assemble the object machine into a binary.
# Assemble first to get positions, then write # Assemble first to get positions, then write

View File

@ -12,14 +12,25 @@ module Risc
end end
# write into the given BinaryCode instance # write into the given BinaryCode instance
# LinkException may be thrown, possibly several times
# So repeat until it works
def assemble( instruction ) def assemble( instruction )
ok = false
until(ok)
begin
assemble_all(instruction)
ok = true
rescue LinkException
end
end
end
# Go through and assemble all instructions.
# Assembly may cause LinkException, which is caught by caller
def assemble_all( instruction )
@index = 1 @index = 1
while(instruction) while(instruction)
begin instruction.assemble(self)
instruction.assemble(self)
rescue LinkException
instruction.assemble(self)
end
instruction = instruction.next instruction = instruction.next
end end
end end
@ -30,4 +41,19 @@ module Risc
end end
end end
# A LinkException is raised when the arm code can't fit a constant into _one_
# instruction. This is kind of unavoidable with arm.
#
# Off course the problem could be fixed without the exception, but the exception
# means all subsequent Instructions, including labels/jump targets move.
# Thus changing jump instructions to those labels.
# So the whole method has to be reassembled and (at least) the instructions beyond
# repositioned. Ie a non-local problem, and so the Exception.
#
# Note: In the future i hope to have a more flexible system, possibly with position
# listeners and change events. Because positions chaning is normal, not exceptional.
#
class LinkException < Exception
end
end end