1/*
2 *  Copyright(c) 2019-2022 rev.ng Labs Srl. All Rights Reserved.
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *  GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18/* Copy rules */
19#define fLSBOLD(VAL) (fGETBIT(0, VAL))
20#define fSATH(VAL) fSATN(16, VAL)
21#define fSATUH(VAL) fSATUN(16, VAL)
22#define fVSATH(VAL) fVSATN(16, VAL)
23#define fVSATUH(VAL) fVSATUN(16, VAL)
24#define fSATUB(VAL) fSATUN(8, VAL)
25#define fSATB(VAL) fSATN(8, VAL)
26#define fVSATUB(VAL) fVSATUN(8, VAL)
27#define fVSATB(VAL) fVSATN(8, VAL)
28#define fCALL(A) fWRITE_LR(fREAD_NPC()); fWRITE_NPC(A);
29#define fCALLR(A) fWRITE_LR(fREAD_NPC()); fWRITE_NPC(A);
30#define fCAST2_8s(A) fSXTN(16, 64, A)
31#define fCAST2_8u(A) fZXTN(16, 64, A)
32#define fVSATW(A) fVSATN(32, fCAST8_8s(A))
33#define fSATW(A) fSATN(32, fCAST8_8s(A))
34#define fVSAT(A) fVSATN(32, A)
35#define fSAT(A) fSATN(32, A)
36
37/* Ease parsing */
38#define f8BITSOF(VAL) ((VAL) ? 0xff : 0x00)
39#define fREAD_GP() (Constant_extended ? (0) : GP)
40#define fCLIP(DST, SRC, U) (DST = fMIN((1 << U) - 1, fMAX(SRC, -(1 << U))))
41#define fBIDIR_ASHIFTL(SRC, SHAMT, REGSTYPE)                            \
42    ((SHAMT > 0) ?                                                      \
43     (fCAST##REGSTYPE##s(SRC) << SHAMT) :                               \
44     (fCAST##REGSTYPE##s(SRC) >> -SHAMT))
45
46#define fBIDIR_LSHIFTL(SRC, SHAMT, REGSTYPE)    \
47    ((SHAMT > 0) ?                              \
48     (fCAST##REGSTYPE##u(SRC) << SHAMT) :       \
49     (fCAST##REGSTYPE##u(SRC) >>> -SHAMT))
50
51#define fBIDIR_ASHIFTR(SRC, SHAMT, REGSTYPE)    \
52    ((SHAMT > 0) ?                              \
53     (fCAST##REGSTYPE##s(SRC) >> SHAMT) :       \
54     (fCAST##REGSTYPE##s(SRC) << -SHAMT))
55
56#define fBIDIR_SHIFTR(SRC, SHAMT, REGSTYPE) \
57    (((SHAMT) < 0) ? ((fCAST##REGSTYPE(SRC) << ((-(SHAMT)) - 1)) << 1)  \
58                   : (fCAST##REGSTYPE(SRC) >> (SHAMT)))
59
60#define fBIDIR_LSHIFTR(SRC, SHAMT, REGSTYPE)                            \
61    fBIDIR_SHIFTR(SRC, SHAMT, REGSTYPE##u)
62
63#define fSATVALN(N, VAL)                                                \
64    fSET_OVERFLOW(                                                      \
65        ((VAL) < 0) ? (-(1LL << ((N) - 1))) : ((1LL << ((N) - 1)) - 1)  \
66    )
67
68#define fSAT_ORIG_SHL(A, ORIG_REG)                                      \
69    (((fCAST4s((fSAT(A)) ^ (fCAST4s(ORIG_REG)))) < 0)                   \
70        ? fSATVALN(32, (fCAST4s(ORIG_REG)))                             \
71        : ((((ORIG_REG) > 0) && ((A) == 0)) ? fSATVALN(32, (ORIG_REG))  \
72                                            : fSAT(A)))
73
74#define fBIDIR_ASHIFTR_SAT(SRC, SHAMT, REGSTYPE)                        \
75    (((SHAMT) < 0) ? fSAT_ORIG_SHL((fCAST##REGSTYPE##s(SRC)             \
76                        << ((-(SHAMT)) - 1)) << 1, (SRC))               \
77                   : (fCAST##REGSTYPE##s(SRC) >> (SHAMT)))
78
79#define fBIDIR_ASHIFTL_SAT(SRC, SHAMT, REGSTYPE)                        \
80    (((SHAMT) < 0)                                                      \
81     ? ((fCAST##REGSTYPE##s(SRC) >> ((-(SHAMT)) - 1)) >> 1)             \
82     : fSAT_ORIG_SHL(fCAST##REGSTYPE##s(SRC) << (SHAMT), (SRC)))
83
84#define fEXTRACTU_BIDIR(INREG, WIDTH, OFFSET)                           \
85    (fZXTN(WIDTH, 32, fBIDIR_LSHIFTR((INREG), (OFFSET), 4_8)))
86
87/* Least significant bit operations */
88#define fLSBNEW0 fLSBNEW(P0N)
89#define fLSBNEW1 fLSBNEW(P1N)
90#define fLSBOLDNOT(VAL) fGETBIT(0, ~VAL)
91#define fLSBNEWNOT(PRED) (fLSBNEW(~PRED))
92#define fLSBNEW0NOT fLSBNEW(~P0N)
93#define fLSBNEW1NOT fLSBNEW(~P1N)
94
95/* Assignments */
96#define fPCALIGN(IMM) (IMM = IMM & ~3)
97#define fWRITE_LR(A) (LR = A)
98#define fWRITE_FP(A) (FP = A)
99#define fWRITE_SP(A) (SP = A)
100/*
101 * Note: There is a rule in the parser that matches `PC = ...` and emits
102 * a call to `gen_write_new_pc`. We need to call `gen_write_new_pc` to
103 * get the correct semantics when there are multiple stores in a packet.
104 */
105#define fBRANCH(LOC, TYPE) (PC = LOC)
106#define fJUMPR(REGNO, TARGET, TYPE) (PC = TARGET)
107#define fWRITE_LOOP_REGS0(START, COUNT) SA0 = START; (LC0 = COUNT)
108#define fWRITE_LOOP_REGS1(START, COUNT) SA1 = START; (LC1 = COUNT)
109#define fWRITE_LC0(VAL) (LC0 = VAL)
110#define fWRITE_LC1(VAL) (LC1 = VAL)
111#define fSET_LPCFG(VAL) (USR.LPCFG = VAL)
112#define fWRITE_P0(VAL) P0 = VAL;
113#define fWRITE_P1(VAL) P1 = VAL;
114#define fWRITE_P3(VAL) P3 = VAL;
115#define fEA_RI(REG, IMM) (EA = REG + IMM)
116#define fEA_RRs(REG, REG2, SCALE) (EA = REG + (REG2 << SCALE))
117#define fEA_IRs(IMM, REG, SCALE) (EA = IMM + (REG << SCALE))
118#define fEA_IMM(IMM) (EA = IMM)
119#define fEA_REG(REG) (EA = REG)
120#define fEA_BREVR(REG) (EA = fbrev(REG))
121#define fEA_GPI(IMM) (EA = fREAD_GP() + IMM)
122#define fPM_I(REG, IMM) (REG = REG + IMM)
123#define fPM_M(REG, MVAL) (REG = REG + MVAL)
124#define fWRITE_NPC(VAL) (PC = VAL)
125
126/* Unary operators */
127#define fROUND(A) (A + 0x8000)
128
129/* Binary operators */
130#define fSCALE(N, A) (A << N)
131#define fASHIFTR(SRC, SHAMT, REGSTYPE) (fCAST##REGSTYPE##s(SRC) >> SHAMT)
132#define fLSHIFTR(SRC, SHAMT, REGSTYPE) (SRC >>> SHAMT)
133#define fROTL(SRC, SHAMT, REGSTYPE) fROTL(SRC, SHAMT)
134#define fASHIFTL(SRC, SHAMT, REGSTYPE) (fCAST##REGSTYPE##s(SRC) << SHAMT)
135
136/* Include fHIDE macros which hide type declarations */
137#define fHIDE(A) A
138
139/* Purge non-relavant parts */
140#define fBRANCH_SPECULATE_STALL(A, B, C, D, E)
141