asm/AJx86JumpInstruction.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 15 Jun 2016 23:46:29 +0100
changeset 23 d2d9a2d4d6bf
parent 17 54798ae989cc
permissions -rw-r--r--
Added README, licenses and copyright notices.

"
    Copyright (c) 2012-2016 Igor Stasenko
                            Martin McClure
                            Damien Pollet
                            Camillo Bruni
                            Guido Chari
                   2016-now Jan Vrany <jan.vrany [at] fit . cvut . cz>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the 'Software'), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
"
"{ Package: 'jv:dragonfly/asm' }"

"{ NameSpace: Smalltalk }"

AJJumpInstruction subclass:#AJx86JumpInstruction
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'AsmJit-x86-Instructions'
!

!AJx86JumpInstruction class methodsFor:'documentation'!

copyright
"
    Copyright (c) 2012-2016 Igor Stasenko
                            Martin McClure
                            Damien Pollet
                            Camillo Bruni
                            Guido Chari
                   2016-now Jan Vrany <jan.vrany [at] fit . cvut . cz>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the 'Software'), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.

"
! !

!AJx86JumpInstruction methodsFor:'accessing'!

codeSize 
    machineCode ifNil: [  ^ 2 ].
    ^ machineCode size
!

instructionDesciptions
    ^ AJx86InstructionDescription instructions
!

machineCodeSize

    machineCode ifNil: [ ^ 2 ].
    
    ^ machineCode size
! !

!AJx86JumpInstruction methodsFor:'convenience'!

errorUndefinedLabel: aLabel 

    ^ self error: 'undefined label: ', aLabel name
! !

!AJx86JumpInstruction methodsFor:'emitting code'!

emitCode: asm
    "generate opcodes"

    | delta code nextInstruction target desc |
    
    target := label position.
    target ifNil: [ ^ machineCode := nil ].
    
    nextInstruction := position + 2.
    delta := (target - nextInstruction) asImm.
    desc := self instructionDesciptions at: name.	"can we use 8bit offset?"
    machineCode := delta isInt8
        ifTrue: [ 	self emitShortJump: desc offset: delta ]
        ifFalse: [ self emitLongJump: desc target: target ]
!

emitCodeAtOffset: offset assembler: asm
    
    position := offset.
    [ | labelPos | 
        labelPos := label position.
        labelPos ifNotNil: [ self emitCode: asm ].
        next ifNotNil: [ 
            next emitCodeAtOffset: offset + self machineCodeSize assembler: asm ].
        label position ~= labelPos ] whileTrue.
    
    label position ifNil: [ self errorUndefinedLabel: label  ]
!

emitConditionalJump: addr to: desc
    ^ {16r0F.
    (16r80 + desc opCode1).
    (addr bitAnd: 255).
    (addr >> 8 bitAnd: 255).
    (addr >> 16 bitAnd: 255).
    (addr >> 24 bitAnd: 255)} asByteArray
!

emitLongJump: desc target: target
    | addr sz nextInstruction |
    
    sz := self isConditional
        ifTrue: [ 2 ]
        ifFalse: [ 1 ].
        
    nextInstruction := position + 4 + sz.
    addr := (AJImmediate ivalue: target - nextInstruction) asDWord.
    
    ^ self isConditional
        ifFalse: [ self emitUnconditionalJumpTo: addr ]
        ifTrue: [ 	self emitConditionalJump: addr to: desc ]
!

emitShortJump: desc offset: delta
    "short jump"
    ^ self isConditional
        ifTrue: [ {(16r70 + desc opCode1). (delta asByte)} asByteArray ]
        ifFalse: [ {16rEB. (delta asByte)} asByteArray ]
!

emitUnconditionalJumpTo: addr
    ^ {
    16rE9.
    (addr bitAnd: 255).
    (addr >> 8 bitAnd: 255).
    (addr >> 16 bitAnd: 255).
    (addr >> 24 bitAnd: 255)} asByteArray
! !

!AJx86JumpInstruction methodsFor:'testing'!

isConditional
    ^ name ~~ #jmp
! !

!AJx86JumpInstruction class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !