Sha256: ee582bc41552fd075261f0458d2ec754fc9488fc054591a17318a835333bf437

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

#    This file is part of Metasm, the Ruby assembly manipulation suite
#    Copyright (C) 2006-2009 Yoann GUILLOT
#
#    Licence is LGPL, see LICENCE in the top-level directory


require 'metasm/ppc/opcodes'
require 'metasm/encode'

module Metasm
class PowerPC
	private
	def encode_instr_op(exe, instr, op)
		base = op.bin
		set_field = lambda { |f, v|
			base |= (v & @fields_mask[f]) << @fields_shift[f]
		}

		val, mask, shift = 0, 0, 0

# TODO
		# convert label name for jmp/call/loop to relative offset
		if op.props[:setip] and op.name[0] != ?t and instr.args.last.kind_of? Expression
			postlabel = exe.new_label('jmp_offset')
			instr = instr.dup
			instr.args[-1] = Expression[[instr.args[-1], :-, postlabel], :>>, 2]
			postdata = EncodedData.new '', :export => {postlabel => 0}
		else
			postdata = ''
		end

		op.args.zip(instr.args).each { |sym, arg|
			case sym
			when :rs, :rt, :rd
				set_field[sym, arg.i]
			when :ft
				set_field[sym, arg.i]
			when :rs_i16
				set_field[:rs, arg.base.i]
				val, mask, shift = arg.offset, @fields_mask[:i16], @fields_shift[:i16]
			when :sa, :i16, :i20
				val, mask, shift = arg, @fields_mask[sym], @fields_shift[sym]
			when :i26
				val, mask, shift = Expression[arg, :>>, 2], @fields_mask[sym], @fields_shift[sym]
			end
		}

		Expression[base, :+, [[val, :&, mask], :<<, shift]].encode(:u32, @endianness) << postdata
	end
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metasm-1.0.1 lib/metasm/ppc/encode.rb
metasm-1.0.0 lib/metasm/ppc/encode.rb