remove the old asm (that is now arm)

This commit is contained in:
Torsten Ruger 2014-05-10 15:27:10 +03:00
parent 79a28ac5fa
commit cba171cc7d
13 changed files with 0 additions and 812 deletions

View File

@ -1,13 +0,0 @@
Assembler in Ruby
=================
Supporting arm, but aimed quite specifically at raspberry pi, arm v7, floating point included (later)
Supported (pseudo)instructions:
- adc, add, and, bic, eor, orr, rsb, rsc, sbc, sub, cmn, cmp, teq, tst,
mov, mvn, strb, str, ldrb, ldr, push, pop, b, bl, bx, swi
- Conditional versions of above
Thanks to Mikko for starting this arm/elf project in the first place: https://github.com/cyndis/as

View File

@ -1,82 +0,0 @@
module Asm
module ArmMachine
OPCODES = {
:adc => 0b0101, :add => 0b0100,
:and => 0b0000, :bic => 0b1110,
:eor => 0b0001, :orr => 0b1100,
:rsb => 0b0011, :rsc => 0b0111,
:sbc => 0b0110, :sub => 0b0010,
# for these Rn is sbz (should be zero)
:mov => 0b1101,
:mvn => 0b1111,
# for these Rd is sbz and S=1
:cmn => 0b1011,
:cmp => 0b1010,
:teq => 0b1001,
:tst => 0b1000,
:b => 0b1010,
:bl => 0b1011,
:bx => 0b00010010
}
#return the bit patter that the cpu uses for the current instruction @opcode
def op_bit_code
OPCODES[@opcode] or throw "no code found for #{@opcode.inspect}"
end
#codition codes can be applied to many instructions and thus save branches
# :al => always , :eq => equal and so on
# eq mov if equal :moveq r1 r2 (also exists as function) will only execute if the last operation was 0
COND_CODES = {
:al => 0b1110, :eq => 0b0000,
:ne => 0b0001, :cs => 0b0010,
:mi => 0b0100, :hi => 0b1000,
:cc => 0b0011, :pl => 0b0101,
:ls => 0b1001, :vc => 0b0111,
:lt => 0b1011, :le => 0b1101,
:ge => 0b1010, :gt => 0b1100,
:vs => 0b0110
}
#return the bit pattern for the @condition_code variable, which signals the conditional code
def cond_bit_code
COND_CODES[@condition_code] or throw "no code found for #{@condition_code}"
end
REGISTERS = { 'r0' => 0, 'r1' => 1, 'r2' => 2, 'r3' => 3, 'r4' => 4, 'r5' => 5,
'r6' => 6, 'r7' => 7, 'r8' => 8, 'r9' => 9, 'r10' => 10, 'r11' => 11,
'r12' => 12, 'r13' => 13, 'r14' => 14, 'r15' => 15, 'a1' => 0, 'a2' => 1,
'a3' => 2, 'a4' => 3, 'v1' => 4, 'v2' => 5, 'v3' => 6, 'v4' => 7, 'v5' => 8,
'v6' => 9, 'rfp' => 9, 'sl' => 10, 'fp' => 11, 'ip' => 12, 'sp' => 13,
'lr' => 14, 'pc' => 15 }
def reg name
raise "no such register #{reg}" unless REGISTERS[name]
Asm::Register.new(name , REGISTERS[name])
end
def calculate_u8_with_rr(arg)
parts = arg.value.to_s(2).rjust(32,'0').scan(/^(0*)(.+?)0*$/).flatten
pre_zeros = parts[0].length
imm_len = parts[1].length
if ((pre_zeros+imm_len) % 2 == 1)
u8_imm = (parts[1]+'0').to_i(2)
imm_len += 1
else
u8_imm = parts[1].to_i(2)
end
if (u8_imm.fits_u8?)
# can do!
rot_imm = (pre_zeros+imm_len) / 2
if (rot_imm > 15)
return nil
end
return u8_imm | (rot_imm << 8)
else
return nil
end
end
end
end

View File

@ -1,98 +0,0 @@
require 'asm/nodes'
require 'asm/block'
require 'stream_reader'
require 'stringio'
require "asm/string_literal"
module Asm
# Assembler is the the top-level of the code hierachy, except it is not derived from code
# instead a Assembler is a list of blocks (and string constants)
# All code is created in blocks (see there) and there are two styles for that, for forward of backward
# referencing. Read function block and add_block and Block.set
class Assembler
def initialize
@blocks = []
@string_table = {}
end
attr_reader :blocks
# Assembling to string will return a binary string of the whole program, ie all blocks and the
# strings they use
# As a memory reference this would be callable, but more likely you will hand it over to
# an ObjectWriter as the .text section and then link it. And then execute it :-)
def assemble_to_string
#put the strings at the end of the assembled code.
# adding them will fix their position and make them assemble after
@string_table.values.each do |data|
add_block data
end
io = StringIO.new
assemble(io)
io.string
end
# Add a string to the string table. Strings are global and constant. So only one copy of each
# string exists
# Internally StringLiterals are created and stored and during assembly written after the blocks
def add_string str
code = @string_table[str]
return code if code
data = Asm::StringLiteral.new(str)
@string_table[str] = data
end
# Length of all blocks. Does not take strings into account as they are added after all blocks.
# This is used to determine where a block when it is added after creation (see add_block)
def length
@blocks.inject(0) {| sum , item | sum + item.length}
end
# This is how you add a forward declared block. This is called automatically when you
# call block with ruby block, but has to be done manually if not
def add_block block
block.at self.length
@blocks << block
end
# return the block of the given name
# or raise an exception, as this is meant to be called when the block is available
def get_block name
block = @blocks.find {|b| b.name == name}
raise "No block found for #{name} (in #{blocks.collect{|b|b.name}.join(':')})" unless block
block
end
# this is used to create blocks.
# All functions that have no args are interpreted as block names
# and if a block is provided, it is evaluated in the (ruby)blocks scope and the block added to the
# program immediately.
# If no block is provided (forward declaration), you must call code on it later
def method_missing(meth, *args, &block)
if args.length == 0
code = Block.new(meth.to_s , self )
if block_given?
add_block code
code.instance_eval(&block)
end
return code
else
super
end
end
private
def assemble(io)
@blocks.each do |obj|
obj.assemble io
end
end
end
end

View File

@ -1,8 +0,0 @@
module Asm
class AssemblyError < StandardError
def initialize(message)
super(message)
end
end
end

View File

@ -1,152 +0,0 @@
require_relative 'call_instruction'
require_relative 'stack_instruction'
require_relative 'logic_instruction'
require_relative 'memory_instruction'
require_relative "code"
module Asm
# 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.
# Thee position is fixed in one of three ways
# - create the block with ruby block, signalling that the instantiation poin is the position
# - call block.code with the code or if you wish program.add_block (and add you code with calls)
# - the assmebly process will pin it if it wasn't set
# creating blocks is done by calling the blocks name/label on either a program or a block
# (method missing will cathc the call and create the block)
# and the easiest way is to go into a ruby block and start writing instructions
# Example (backward jump):
# program.loop do create a new block with label loop
# sub r1 , r1 , 1 count the r1 register down
# bne :loop jump back to loop when the counter is not zero
# end (initialization and actual code missing off course)
# Example (forward jump)
# else_block = program.else
# program.if do
# test r1 , 0 test some condition
# beq :else_block
# mov . . .. .. do whatever the if block does
# end
# else_block.code do
# ldr .... do whatever else does
# end
# Blocks are also used to create instructions, and so Block has functions for every cpu instruction
# and to make using the apu function easier, there are functions that create registers as well
class Block < Code
def initialize(name , prog)
super()
@name = name.to_sym
@codes = []
@position = 0
@program = prog
end
attr_reader :name
ArmMachine::REGISTERS.each do |reg , number|
define_method(reg) { Asm::Register.new(reg , number) }
end
def instruction(clazz, opcode , condition_code , update_status , *args)
arg_nodes = []
args.each do |arg|
if (arg.is_a?(Asm::Register))
arg_nodes << arg
elsif (arg.is_a?(Integer))
arg_nodes << Asm::NumLiteral.new(arg)
elsif (arg.is_a?(String))
arg_nodes << @program.add_string(arg)
elsif (arg.is_a?(Asm::Block))
arg_nodes << arg
elsif (arg.is_a?(Symbol))
block = @program.get_block arg
arg_nodes << block
else
raise "Invalid argument #{arg.inspect} for instruction"
end
end
add_code clazz.new(opcode , condition_code , update_status , arg_nodes)
end
def self.define_instruction(inst , clazz )
define_method(inst) do |*args|
instruction clazz , inst , :al , 0 , *args
end
define_method("#{inst}s") do |*args|
instruction clazz , inst , :al , 1 , *args
end
ArmMachine::COND_CODES.keys.each do |suffix|
define_method("#{inst}#{suffix}") do |*args|
instruction clazz , inst , suffix , 0 , *args
end
define_method("#{inst}s#{suffix}") do |*args|
instruction clazz , inst , suffix , 1 , *args
end
end
end
[:push, :pop].each do |inst|
define_instruction(inst , StackInstruction)
end
[:adc, :add, :and, :bic, :eor, :orr, :rsb, :rsc, :sbc, :sub].each do |inst|
define_instruction(inst , LogicInstruction)
end
[:mov, :mvn].each do |inst|
define_instruction(inst , MoveInstruction)
end
[:cmn, :cmp, :teq, :tst].each do |inst|
define_instruction(inst , CompareInstruction)
end
[:strb, :str , :ldrb, :ldr].each do |inst|
define_instruction(inst , MemoryInstruction)
end
[:b, :bl , :swi].each do |inst|
define_instruction(inst , CallInstruction)
end
# codeing a block fixes it's position in the stream.
# You must call with a block, which is instance_eval'd and provides the actual code for the block
def code &block
@program.add_block self
self.instance_eval block
end
# length of the codes. In arm it would be the length * 4
# (strings are stored globally in the Assembler)
def length
@codes.inject(0) {| sum , item | sum + item.length}
end
def add_code(kode)
kode.at(@position)
length = kode.length
@position += length
@codes << kode
end
def assemble(io)
@codes.each do |obj|
obj.assemble io
end
end
# this is used to create blocks.
# All functions that have no args are interpreted as block names
# In fact the block calls are delegated to the program which then instantiates the blocks
def method_missing(meth, *args, &block)
if args.length == 0
@program.send(meth , *args , &block)
else
super
end
end
end
end

View File

@ -1,49 +0,0 @@
require_relative "instruction"
module Asm
# There are only three call instructions in arm branch (b), call (bl) and syscall (swi)
# A branch could be called a jump as it has no notion of returning
# A call has the bl code as someone thought "branch with link" is a useful name.
# The pc is put into the link register to make a return possible
# a return is affected by moving the stored link register into the pc, effectively a branch
# swi (SoftWareInterrupt) or system call is how we call the kernel.
# in Arm the register layout is different and so we have to place the syscall code into register 7
# Registers 0-6 hold the call values as for a normal c call
class CallInstruction < Instruction
def assemble(io)
case opcode
when :b, :bl
arg = args[0]
if arg.is_a? Block
diff = arg.position - self.position - 8
arg = NumLiteral.new(diff)
end
if (arg.is_a?(Asm::NumLiteral))
jmp_val = arg.value >> 2
packed = [jmp_val].pack('l')
# signed 32-bit, condense to 24-bit
# TODO add check that the value fits into 24 bits
io << packed[0,3]
else
raise "else not coded #{arg.inspect}"
end
io.write_uint8 OPCODES[opcode] | (COND_CODES[@condition_code] << 4)
when :swi
arg = args[0]
if (arg.is_a?(Asm::NumLiteral))
packed = [arg.value].pack('L')[0,3]
io << packed
io.write_uint8 0b1111 | (COND_CODES[@condition_code] << 4)
else
raise Asm::AssemblyError.new("invalid operand argument expected literal not #{arg}")
end
end
end
end#class
end

View File

@ -1,45 +0,0 @@
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

View File

@ -1,46 +0,0 @@
require_relative "assembly_error"
require_relative "arm_machine"
module Asm
class Code ; end
# Not surprisingly represents an cpu instruction.
# This is an abstract base class, with derived classes
# Logic / Move / Compare / Stack / Memory (see there)
#
# Opcode is a (<= three) letter accronym (same as in assembly code). Though in arm, suffixes can
# make the opcode longer, we chop those off in the constructor
# Argurments are registers or labels or string/num Literals
class Instruction < Code
include ArmMachine
COND_POSTFIXES = Regexp.union( COND_CODES.keys.collect{|k|k.to_s} ).source
def initialize(opcode , condition_code , update_status , args)
@update_status_flag = update_status
@condition_code = condition_code.to_sym
@opcode = opcode
@args = args
@operand = 0
end
attr_reader :opcode, :args
# Many arm instructions may be conditional, where the default condition is always (al)
# ArmMachine::COND_CODES names them, and this attribute reflects it
attr_reader :condition_code
attr_reader :operand
# Logic instructions may be executed with or without affecting the status register
# Only when an instruction affects the status is a subsequent compare instruction effective
# But to make the conditional execution (see cond) work for more than one instruction, one needs to
# be able to execute without changing the status
attr_reader :update_status_flag
# arm intrucioons are pretty sensible, and always 4 bytes (thumb not supported)
def length
4
end
end
end

View File

@ -1,107 +0,0 @@
require_relative "instruction"
module Asm
# ADDRESSING MODE 1
# Logic ,Maths, Move and compare instructions (last three below)
class LogicInstruction < Instruction
def initialize(opcode , condition_code , update_status , args)
super(opcode , condition_code , update_status , args)
@rn = nil
@i = 0
@rd = args[0]
end
attr_accessor :i, :rn, :rd
# Build representation for source value
def build
@rn = args[1]
do_build args[2]
end
#(stays in subclases, while build is overriden to provide different arguments)
def do_build(arg)
if arg.is_a?(Asm::StringLiteral)
# do pc relative addressing with the difference to the instuction
# 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute)
arg = Asm::NumLiteral.new( arg.position - self.position - 8 )
end
if (arg.is_a?(Asm::NumLiteral))
if (arg.value.fits_u8?)
# no shifting needed
@operand = arg.value
@i = 1
elsif (op_with_rot = calculate_u8_with_rr(arg))
@operand = op_with_rot
@i = 1
else
raise Asm::AssemblyError.new("cannot fit numeric literal argument in operand #{arg}")
end
elsif (arg.is_a?(Asm::Register))
@operand = arg
@i = 0
elsif (arg.is_a?(Asm::Shift))
rm_ref = arg.argument
@i = 0
shift_op = {'lsl' => 0b000, 'lsr' => 0b010, 'asr' => 0b100,
'ror' => 0b110, 'rrx' => 0b110}[arg.type]
if (arg.type == 'ror' and arg.value.nil?)
# ror #0 == rrx
raise Asm::AssemblyError.new('cannot rotate by zero', arg)
end
arg1 = arg.value
if (arg1.is_a?(Asm::NumLiteral))
if (arg1.value >= 32)
raise Asm::AssemblyError.new('cannot shift by more than 31', arg1)
end
shift_imm = arg1.value
elsif (arg1.is_a?(Asm::Register))
shift_op val |= 0x1;
shift_imm = arg1.number << 1
elsif (arg.type == 'rrx')
shift_imm = 0
end
@operand = rm_ref | (shift_op << 4) | (shift_imm << 4+3)
else
raise Asm::AssemblyError.new("invalid operand argument #{arg.inspect}")
end
end
def assemble(io)
build
instuction_class = 0b00 # OPC_DATA_PROCESSING
val = operand.is_a?(Register) ? operand.bits : operand
val |= (rd.bits << 12)
val |= (rn.bits << 12+4)
val |= (update_status_flag << 12+4+4)#20
val |= (op_bit_code << 12+4+4 +1)
val |= (i << 12+4+4 +1+4)
val |= (instuction_class << 12+4+4 +1+4+1)
val |= (cond_bit_code << 12+4+4 +1+4+1+2)
io.write_uint32 val
end
end
class CompareInstruction < LogicInstruction
def initialize(opcode , condition_code , update_status , args)
super(opcode , condition_code , update_status , args)
@update_status_flag = 1
@rn = args[0]
@rd = reg "r0"
end
def build
do_build args[1]
end
end
class MoveInstruction < LogicInstruction
def initialize(opcode , condition_code , update_status , args)
super(opcode , condition_code , update_status , args)
@rn = reg "r0" # register zero = zero bit pattern
end
def build
do_build args[1]
end
end
end

View File

@ -1,80 +0,0 @@
require "asm/nodes"
require_relative "instruction"
module Asm
# ADDRESSING MODE 2
# Implemented: immediate offset with offset=0
class MemoryInstruction < Instruction
def initialize(opcode , condition_code , update_status , args)
super(opcode , condition_code , update_status , args)
@i = 0 #I flag (third bit)
@pre_post_index = 0 #P flag
@add_offset = 0 #U flag
@byte_access = opcode.to_s[-1] == "b" ? 1 : 0 #B (byte) flag
@w = 0 #W flag
@is_load = opcode.to_s[0] == "l" ? 1 : 0 #L (load) flag
@rn = reg "r0" # register zero = zero bit pattern
@rd = reg "r0" # register zero = zero bit pattern
end
attr_accessor :i, :pre_post_index, :add_offset,
:byte_access, :w, :is_load, :rn, :rd
# Build representation for target address
def build
if( @is_load )
@rd = args[0]
arg = args[1]
else #store
@rd = args[1]
arg = args[0]
end
#str / ldr are _serious instructions. With BIG possibilities not half are implemented
if (arg.is_a?(Asm::Register))
@rn = arg
if(arg.offset != 0)
@operand = arg.offset
if (@operand < 0)
@add_offset = 0
#TODO test/check/understand
@operand *= -1
else
@add_offset = 1
end
if (@operand.abs > 4095)
raise Asm::AssemblyError.new("reference offset too large/small (max 4095) #{argr.right}" )
end
end
elsif (arg.is_a?(Asm::Label) or arg.is_a?(Asm::NumLiteral))
@pre_post_index = 1
@rn = pc
@use_addrtable_reloc = true
@addrtable_reloc_target = arg
else
raise Asm::AssemblyError.new("invalid operand argument #{arg.inspect}")
end
end
def assemble(io)
build
#not sure about these 2 constants. They produce the correct output for str r0 , r1
# but i can't help thinking that that is because they are not used in that instruction and
# so it doesn't matter. Will see
@add_offset = 1
@pre_post_index = 1
instuction_class = 0b01 # OPC_MEMORY_ACCESS
val = operand
val |= (rd.bits << 12 )
val |= (rn.bits << 12+4) #16
val |= (is_load << 12+4 +4)
val |= (w << 12+4 +4+1)
val |= (byte_access << 12+4 +4+1+1)
val |= (add_offset << 12+4 +4+1+1+1)
val |= (pre_post_index << 12+4 +4+1+1+1+1)#24
val |= (i << 12+4 +4+1+1+1+1 +1)
val |= (instuction_class<<12+4 +4+1+1+1+1 +1+1)
val |= (cond_bit_code << 12+4 +4+1+1+1+1 +1+1+2)
io.write_uint32 val
end
end
end

View File

@ -1,44 +0,0 @@
module Asm
class Shift
attr_accessor :type, :value, :argument
end
# Registers have off course a name (r1-16 for arm)
# but also refer to an address. In other words they can be an operand for instructions.
# Arm has addressing modes abound, and so can add to a register before actually using it
# If can actually shift or indeed shift what it adds, but not implemented
class Register
attr_accessor :name , :offset , :bits
def initialize name , bits
@name = name
@bits = bits
@offset = 0
end
# this is for the dsl, so we can write pretty code like r1 + 4
# when we want to access the next word (4) after r1
def + number
@offset = number
self
end
end
# maybe not used at all as code_gen::instruction raises if used.
# instead now using Arrays
class RegisterList
attr_accessor :registers
def initialize regs
@registers = regs
regs.each{ |reg| raise "not a reg #{sym} , #{reg}" unless reg.is_a?(Asm::Register) }
end
end
class NumLiteral
attr_accessor :value
def initialize val
@value = val
end
end
end

View File

@ -1,58 +0,0 @@
require_relative "instruction"
module Asm
# ADDRESSING MODE 4
class StackInstruction < Instruction
def initialize(opcode , condition_code , update_status , args)
super(opcode , condition_code , update_status , args)
@update_status_flag= 0
@rn = reg "r0" # register zero = zero bit pattern
# downward growing, decrement before memory access
# official ARM style stack as used by gas
@write_base = 1
if (opcode == :push)
@pre_post_index = 1
@up_down = 0
@is_pop = 0
else #pop
@pre_post_index = 0
@up_down = 1
@is_pop = 1
end
end
attr_accessor :pre_post_index, :up_down,
:update_status_flag, :write_base, :is_pop, :rn
def assemble(io)
build
instuction_class = 0b10 # OPC_STACK
cond = @condition_code.is_a?(Symbol) ? COND_CODES[@condition_code] : @condition_code
rn = reg "sp" # sp register
#assemble of old
val = operand
val |= (rn.bits << 16)
val |= (is_pop << 16+4) #20
val |= (write_base << 16+4+ 1)
val |= (update_status_flag << 16+4+ 1+1)
val |= (up_down << 16+4+ 1+1+1)
val |= (pre_post_index << 16+4+ 1+1+1+1)#24
val |= (instuction_class << 16+4+ 1+1+1+1 +2)
val |= (cond << 16+4+ 1+1+1+1 +2+2)
io.write_uint32 val
end
private
# Build representation for source value
def build
if (args.is_a?(Array))
@operand = 0
args.each do |reg |
@operand |= (1 << reg.bits)
end
else
raise Asm::AssemblyError.new("invalid operand argument #{args.inspect}")
end
end
end
end

View File

@ -1,30 +0,0 @@
require_relative "../vm/code"
module Asm
# The name really says it all.
# The only interesting thing is storage.
# Currently string are stored "inline" , ie in the code segment.
# Mainly because that works an i aint no elf expert.
class StringLiteral < Vm::Code
# currently aligned to 4 (ie padded with 0) and off course 0 at the end
def initialize(str)
length = str.length
# rounding up to the next 4 (always adding one for zero pad)
pad = ((length / 4 ) + 1 ) * 4 - length
raise "#{pad} #{self}" unless pad >= 1
@string = str + "\x00" * pad
end
# the strings length plus padding
def length
@string.length
end
# just writing the string
def assemble(io)
io << @string
end
end
end