asm/AJx86GPRegister.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 17 Jun 2016 17:25:15 +0100
changeset 26 8eb6716029aa
parent 23 d2d9a2d4d6bf
permissions -rw-r--r--
Merge

"
    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 }"

AJRegister subclass:#AJx86GPRegister
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:'AJx86Registers'
	category:'AsmJit-x86-Operands'
!

AJx86GPRegister comment:'A general purpose x86 & x64 registers'
!

!AJx86GPRegister 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.

"
! !

!AJx86GPRegister methodsFor:'accessing'!

descriptionOn: s
    s nextPutAll: 'A '; print: self size * 8; nextPutAll: 'bit general purpose register'.
!

registerName 
    ^ name asString.
!

stackSize
    ^ self size
! !

!AJx86GPRegister methodsFor:'converting'!

as16
    ^ self isHighByte
        ifTrue: [ self asLowByte as16 ]
        ifFalse: [ 
            AJx86Registers
                generalPurposeWithIndex: self index
                size: 2
                requiresRex: self index > 7
                prohibitsRex: false ]
!

as32
    ^ self isHighByte
        ifTrue: [ self asLowByte as32 ]
        ifFalse: [ 
            AJx86Registers
                generalPurposeWithIndex: self index
                size: 4
                requiresRex: self index > 7
                prohibitsRex: false ]
!

as64
    ^ self isHighByte
        ifTrue: [ self asLowByte as64 ]
        ifFalse: [ 
            AJx86Registers
                generalPurposeWithIndex: self index
                size: 8
                requiresRex: self index > 7
                prohibitsRex: false ]
!

as8
    "8-bit low-byte registers require REX if they have a high index (>7), or if they are one of BPL, SIL, DIL, SPL (indices 4-7).
    The only way to get AH, BH, CH, or DH out of this method is to send it to one of those registers."

    ^ self is8
        ifTrue: [ self ]
        ifFalse: [ 
            AJx86Registers
                generalPurposeWithIndex: self index
                size: 1
                requiresRex: self index > 3
                prohibitsRex: false ]
!

asHighByte
    self isHighByte
        ifTrue: [ ^ self ].
    self isLowByte
        ifFalse: [ Error signal: 'Can only convert AH, BH, CH, or DH to high byte' ].
    ^ AJx86Registers
        generalPurposeWithIndex: self index + 2r100
        size: 1
        requiresRex: false
        prohibitsRex: true
!

asLowByte
    self isLowByte
        ifTrue: [ ^ self ].
    self isHighByte
        ifFalse: [ Error signal: 'Can only convert high byte 8bit register to low byte' ].
    ^ AJx86Registers
        generalPurposeWithIndex: self index - 2r100
        size: 1
        requiresRex: false
        prohibitsRex: false
!

ptr

    "turn receiver into a memory operand with receiver as base"
    
    ^ AJMem new base: self
! !

!AJx86GPRegister methodsFor:'emitting'!

emitModRM: emitter code: rCode immSize: immSize

    "Receiver is register, hence mod = 3 
    immSize is ignored"

    ^ emitter emitMod: 3 reg: rCode rm: self code
! !

!AJx86GPRegister methodsFor:'printing'!

printAsMemBaseOn: aStream

    aStream nextPutAll: self registerName 
!

printOn: aStream
    
    aStream nextPutAll: self registerName 
! !

!AJx86GPRegister methodsFor:'testing'!

isGeneralPurpose
    ^ true
!

isHighByte
    "return true for 8bit high-byte registers (AH - DH)"

    ^ self prohibitsRex
!

isLowByte
    "return true for 8bit low-byte register (AL - DL)"

    "Note that this does *not* answer true for all byte registers -- send #is8 for that."

    ^ self code <= 3
! !