#BEGIN_LEGAL
#
#Copyright (c) 2024 Intel Corporation
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#  
#END_LEGAL

########################## REX2 ########################## 
# Check MODRM.mod=0, [rm] addressing form instructions
APX DEC     ; BUILDDIR/xed -64 -ih TESTDIR/../rex2-mod0-dec-in.txt

# Check MODRM.mod=1 instructions (All register combinations)
APX DEC     ; BUILDDIR/xed -64 -ih TESTDIR/../rex2-mod1-dec-in.txt

APX DEC ENC ; BUILDDIR/xed -64 -de 66d580124C8300   # REX2 MAP1
APX DEC ENC ; BUILDDIR/xed -64 -de 66d5b3124C8301   # REX2 MAP1 w/ EGPRs
APX DEC ENC ; BUILDDIR/xed -64 -de d50080C401       # No [A,C,D,B]h on REX2 (tests SPL)
APX DEC ENC ; BUILDDIR/xed -64 -de 66d5C0124C8300   # Ignore non-reg rex2.r4 bit
APX DEC ENC ; BUILDDIR/xed -64 -de 440f2203         # mov_cr cr8, rbx (REX Prefix)
APX DEC ENC ; BUILDDIR/xed -64 -de d5842203         # mov_cr cr8, rbx (REX2 Prefix)
APX DEC     ; BUILDDIR/xed -64 -d d5c42203          # mov_cr cr8, rbx (illegal REX2.R4=1)
APX DEC ENC ; BUILDDIR/xed -64 -de f36764d598aef7   # UMONITOR A_GPR_B()
APX DEC ENC ; BUILDDIR/xed -64 -de 66d5cdc5db08     # PEXTRW    (0xC5 opcode)
APX DEC ENC ; BUILDDIR/xed -64 -de d598c4fe77       # PINSRW    (0xC4 opcode)
APX DEC ENC ; BUILDDIR/xed -64 -de d5986212         # PUNPCKLDQ (0x62 opcode) 
APX DEC ENC ; BUILDDIR/xed -64 -de d51890           # REX2 (B4=1,B3=0) | 0x90 -> XCHG
APX DEC ENC ; BUILDDIR/xed -64 -de d50190           # REX2 (B4=0,B3=1) | 0x90 -> XCHG
APX DEC ENC ; BUILDDIR/xed -64 -de d50890           # REX2 (B4=0,B3=0) | 0x90 -> NOP

# Test XSAVE EGPRs limitations
APX DEC     ; BUILDDIR/xed -64 -d 0fae2411          # No prefix
APX DEC     ; BUILDDIR/xed -64 -d 4b0fae2411        # REX prefix
APX DEC     ; BUILDDIR/xed -64 -d d588ae2411        # REX2 prefix Error

# Test REX2 opcode applicability restriction
APX DEC     ; BUILDDIR/xed -64 -d d5 00 a4

# Disable APX decode support
DEC         ; BUILDDIR/xed -64 -set NO_APX 1 -d d5 84 67 0f  # REX2 prefix


########################## EVEX EGPRs ########################## 
# Test EGPRs accessability using EVEX.R4 and reinterpreted EVEX.B4 and EVEX.X4 bits

APX DEC     ; BUILDDIR/xed -64 -d 62B1E10A580C38  # MODRM()   using X4 
APX DEC     ; BUILDDIR/xed -64 -d 62D9742854041f  # MODRM()   using B4
APX DEC     ; BUILDDIR/xed -64 -d 62DB7D0814C706  # GPR32_B() using B4
APX DEC     ; BUILDDIR/xed -64 -d 62B9E10A580C38  # MODRM()   using B4 and X4
APX DEC     ; BUILDDIR/xed -64 -d 6265FE082DC0    # GPR64_R() using R4 bit

APX DEC     ; BUILDDIR/xed -64 -d 62fa7d49a02c00  # VSCATTER VSIB memory pattern

DEC         ; BUILDDIR/xed -64 -set NO_APX 1 -d 6265FE082DC0        # R4 | no-APX + EGPR -> error
DEC         ; BUILDDIR/xed -64 -set NO_APX 1 -d 62 61 fa 48 7a f7   # Check Ubit error w/o APX

########################## EVEX Promoted ISA ###################
APX DEC     ; BUILDDIR/xed -64 -isa-set -ih TESTDIR/../gen-enc-layer-apx-f.txt    # Decode all iforms

# Test scalable operand size NT e.g GPRv()/SIMMz().  (pp=1 -> OSZ 66 prefix)
APX DEC     ; BUILDDIR/xed -64 -d 62D4 FC 0881EC33C00003  # EOSZ=3 (w=1, pp=0)
APX DEC     ; BUILDDIR/xed -64 -d 62D4 FD 0881EC33C00003  # EOSZ=3 (w=1, pp=1)
APX DEC     ; BUILDDIR/xed -64 -d 62D4 7C 0881EC33C00003  # EOSZ=2 (w=0, pp=0)
APX DEC     ; BUILDDIR/xed -64 -d 62D4 7D 0881EC33C0      # EOSZ=1 (w=0, pp=1)

DEC         ; BUILDDIR/xed -64 -set NO_APX 1 -d 62F2E408F2C2 # NO_APX + promoted-from-vex -> error

########################## JMPABS ################### 
APX DEC     ; BUILDDIR/xed -64 -d  67 D500 A10123456789012345  # JMPABS, EASZ=2 -> illegal
APX DEC     ; BUILDDIR/xed-ex1 -64    D500 A10123456789012345  # JMPABS, EASZ=3
APX DEC     ; BUILDDIR/xed -64 -d          A10123456789012345  # MOV,    EASZ=3
APX DEC     ; BUILDDIR/xed -64 -d  67      A101234567          # MOV,    EASZ=2

################### APX chip-check ################### 
APX DEC     ; BUILDDIR/xed-ex1 -v -64 -chip FUTURE 62D2FC00F3D9       # EGPR
APX DEC     ; BUILDDIR/xed-ex1 -v -64 -chip FUTURE 66d580124C8300     # REX2
APX DEC     ; BUILDDIR/xed-ex1 -v -64 -chip ICE_LAKE 62B9E10A580C38   # using B4 and X4
APX DEC     ; BUILDDIR/xed-ex1 -v -64 -chip FUTURE 62B9E10A580C38     # using B4 and X4

################## miscellaneous Example1 Tests ################
APX DEC     ; BUILDDIR/xed-ex1 -v -64 6254F50239C8          # DFV
APX DEC     ; BUILDDIR/xed-ex1 -v -64 62D4BC1883D001        # NDD + ZU
APX DEC     ; BUILDDIR/xed-ex1 -v -64 6254FC0CF4C0          # NF

################## Instruction Restrictions Tests ################
APX DEC     ; BUILDDIR/xed-ex1 -v -64 62D434188FC1             # POP2 has dest1!=dest2 restriction

######################## Encoder / REX2 #########################
APX ENC     ; BUILDDIR/xed -64 -e add r16 r31               # REX2 MAP0 w/ EGPR
APX ENC     ; BUILDDIR/xed -64 -e movlpd xmm1 mem8:r27      # REX2 MAP1 w/ EGPR
APX ENC     ; BUILDDIR/xed -64 -e jmpabs ABSBR:4523018967452301
APX ENC     ; BUILDDIR/xed -64 -e MOVBE r16 mem8:rbx 
APX ENC     ; BUILDDIR/xed -64 -e VPGATHERDD zmm0 k1 MEM4:r30,zmm1,1

# NF should not be an encoder input for CFCMOV*:
APX ENC     ; BUILDDIR/xed -64 -e cfcmovb r16 mem8:rax,r30
APX ENC     ; BUILDDIR/xed -64 -e cfcmovb  mem8:rax,r30 r29
APX ENC     ; BUILDDIR/xed -64 -e cfcmovb r16 rax
APX ENC     ; BUILDDIR/xed -64 -e cmovb r16 rax r30
APX ENC     ; BUILDDIR/xed -64 -e cfcmovb r16 rax r30
# APX non zero-upper instructions (ZU encoder support is TBD):
APX ENC     ; BUILDDIR/xed -64 -set MUST_USE_EVEX 1 -e setb r27b
APX ENC     ; BUILDDIR/xed -64 -set MUST_USE_EVEX 1 -e imul rax rbx imm:00

# memory EGPRs w/ SIB byte
APX ENC     ; BUILDDIR/xed -64 -e vaddpd ymm20 k4 ymm21 mem64:r20
APX ENC     ; BUILDDIR/xed -64 -e vaddpd ymm20 k4 ymm21 mem64:r21
APX ENC     ; BUILDDIR/xed -64 -e vaddpd ymm20 k4 ymm21 mem64:r28
APX ENC     ; BUILDDIR/xed -64 -e vaddpd ymm20 k4 ymm21 mem64:r29
APX ENC     ; BUILDDIR/xed -64 -e vaddpd ymm20 k4 ymm21 mem64:r29,r14,8,-

