
/* Hive Spin Compiler token names and values */

/* token classes */
#define TK_ID               0x80000000
#define TK_INTLITERAL       0x40000000
#define TK_FLOATLITERAL     0x20000000
#define TK_UNARY            0x08000000
#define TK_BINARY           0x04000000
#define TK_POSTFIX          0x04000000
#define TK_PASM             0x02000000
#define TK_COND             0x01000000
#define TK_EFFECT           0x00800000
#define TK_STMTFN           0x00400000
#define TK_ASSIGNMENT       0x00200000
#define TK_INTRINSIC        0x00100000

/* spin operators; 0x08 is unary, 0x04 is binary, 0x0c is unary that can be used postfix */
#define TK_BANG             0x080100e7
#define TK_HASHGT           0x040700e4
#define TK_AMPERSAND        0x040300e8
#define TK_STAR             0x040500f4
#define TK_STARSTAR         0x040500f5
#define TK_PLUS             0x040600ec
#define TK_PLUSPLUS         0x0c002820
#define TK_MINUS            0x040600ed
#define TK_MINUSMINUS       0x0c003830
#define TK_MINUSGT          0x040200e0
#define TK_SLASH            0x040500f6
#define TK_SLASHSLASH       0x040500f7
#define TK_LT               0x040800f9
#define TK_LTHASH           0x040700e5
#define TK_LTMINUS          0x040200e1
#define TK_LTLT             0x040200e3
#define TK_LTGT             0x040800fb
#define TK_EQUALLT          0x040800fd
#define TK_EQUALEQUAL       0x040800fc
#define TK_EQUALGT          0x040800fe
#define TK_GT               0x040800fa
#define TK_GTLT             0x040200ef
#define TK_GTGT             0x040200e2
#define TK_GTBAR            0x080100f1
#define TK_QUESTION         0x0c000c08
#define TK_AT               0x08000000
#define TK_ATAT             0x08000001
#define TK_CARET            0x040400eb
#define TK_CARETCARET       0x080100f8
#define TK_BAR              0x040400ea
#define TK_BARLT            0x080100f3
#define TK_BARBAR           0x080100e9
#define TK_TILDE            0x0c001810
#define TK_TILDEGT          0x040200ee
#define TK_TILDETILDE       0x0c001c14
#define TK_NOT              0x080900ff

/* both spin operators and PASM mnemonics */
#define TK_AND              0x060a00f0
#define TK_OR               0x060b00f2

/* PASM mnemonics */
#define TK_ABS              0x0203a8bc
#define TK_ABSNEG           0x0203acbc
#define TK_ADD              0x020380bc
#define TK_ADDABS           0x020388bc
#define TK_ADDS             0x0203d0bc
#define TK_ADDSX            0x0203d8bc
#define TK_ADDX             0x0203c8bc
#define TK_ANDN             0x020364bc
#define TK_CALL             0x02015cfc
#define TK_CMP              0x0203843c
#define TK_CMPS             0x0203c03c
#define TK_CMPSUB           0x0203e0bc
#define TK_CMPSX            0x0203c43c
#define TK_CMPX             0x0203cc3c
#define TK_DJNZ             0x0203e4bc
#define TK_HUBOP            0x02030c3c
#define TK_JMP              0x02015c3c
#define TK_JMPRET           0x02035cbc
#define TK_MAX              0x02034cbc
#define TK_MAXS             0x020344bc
#define TK_MIN              0x020348bc
#define TK_MINS             0x020340bc
#define TK_MOV              0x0203a0bc
#define TK_MOVD             0x020354bc
#define TK_MOVI             0x020358bc
#define TK_MOVS             0x020350bc
#define TK_MUXC             0x020370bc
#define TK_MUXNC            0x020374bc
#define TK_MUXNZ            0x02037cbc
#define TK_MUXZ             0x020378bc
#define TK_NEG              0x0203a4bc
#define TK_NEGC             0x0203b0bc
#define TK_NEGNC            0x0203b4bc
#define TK_NEGNZ            0x0203bcbc
#define TK_NEGZ             0x0203b8bc
#define TK_NOP              0x02000000
#define TK_RCL              0x020334bc
#define TK_RCR              0x020330bc
#define TK_RDBYTE           0x020300bc
#define TK_RDLONG           0x020308bc
#define TK_RDWORD           0x020304bc
#define TK_RET              0x02005c7c
#define TK_REV              0x02033cbc
#define TK_ROL              0x020324bc
#define TK_ROR              0x020320bc
#define TK_SAR              0x020338bc
#define TK_SHL              0x02032cbc
#define TK_SHR              0x020328bc
#define TK_SUB              0x020384bc
#define TK_SUBABS           0x02038cbc
#define TK_SUBS             0x0203d4bc
#define TK_SUBSX            0x0203dcbc
#define TK_SUBX             0x0203ccbc
#define TK_SUMC             0x020390bc
#define TK_SUMNC            0x020394bc
#define TK_SUMNZ            0x02039cbc
#define TK_SUMZ             0x020398bc
#define TK_TEST             0x0203603c
#define TK_TJNZ             0x0203e83c
#define TK_TJZ              0x0203ec3c
#define TK_WRBYTE           0x0203003c
#define TK_WRLONG           0x0203083c
#define TK_WRWORD           0x0203043c
#define TK_XOR              0x02036cbc

/* PASM condition codes */
#define TK_IF_A             0x01000001
#define TK_IF_AE            0x01000003
#define TK_IF_ALWAYS        0x0100000f
#define TK_IF_B             0x0100000c
#define TK_IF_BE            0x0100000e
#define TK_IF_C             0x0100000c
#define TK_IF_C_AND_NZ      0x01000004
#define TK_IF_C_AND_Z       0x01000008
#define TK_IF_C_EQ_Z        0x01000009
#define TK_IF_C_NE_Z        0x01000006
#define TK_IF_C_OR_NZ       0x0100000d
#define TK_IF_C_OR_Z        0x0100000e
#define TK_IF_E             0x0100000a
#define TK_IF_NC            0x01000003
#define TK_IF_NC_AND_NZ     0x01000001
#define TK_IF_NC_AND_Z      0x01000002
#define TK_IF_NC_OR_NZ      0x01000007
#define TK_IF_NC_OR_Z       0x0100000b
#define TK_IF_NE            0x01000005
#define TK_IF_NEVER         0x01000000
#define TK_IF_NZ            0x01000005
#define TK_IF_NZ_AND_C      0x01000004
#define TK_IF_NZ_AND_NC     0x01000001
#define TK_IF_NZ_OR_C       0x0100000e
#define TK_IF_NZ_OR_NC      0x0100000b
#define TK_IF_Z             0x0100000a
#define TK_IF_Z_AND_C       0x01000008
#define TK_IF_Z_AND_NC      0x01000002
#define TK_IF_Z_EQ_C        0x01000009
#define TK_IF_Z_NE_C        0x01000006
#define TK_IF_Z_OR_C        0x0100000e
#define TK_IF_Z_OR_NC       0x0100000b

/* PASM result codes */
#define TK_WR               0x00800001
#define TK_WC               0x00800002
#define TK_WZ               0x00800004
#define TK_NR               0x00800008

/* statement functions */
#define TK_BYTEFILL         0x00400318
#define TK_WORDFILL         0x00400319
#define TK_LONGFILL         0x0040031a
#define TK_WAITPEQ          0x0240031b
#define TK_BYTEMOVE         0x0040031c
#define TK_WORDMOVE         0x0040031d
#define TK_LONGMOVE         0x0040031e
#define TK_WAITPNE          0x0240031f
#define TK_CLKSET           0x02400220
#define TK_COGSTOP          0x02400121
#define TK_LOCKRET          0x00400122
#define TK_WAITCNT          0x02400123
#define TK_WAITVID          0x02400227

/* intrinsics */
#define TK_CHIPVER          0x00100000
#define TK_CLKFREQ          0x00100010
#define TK_CLKMODE          0x00100020
#define TK_COGID            0x02100003
#define TK_COGINIT          0x02100004
#define TK_COGNEW           0x00100005
#define TK_REBOOT           0x00100006
#define TK_STRCOMP          0x00100172
#define TK_STRSIZE          0x00100161
#define TK_LOCKCLR          0x0012b2f1
#define TK_LOCKNEW          0x001292d0
#define TK_LOCKSET          0x0012a2e1
#define TK_LOOKDOWN         0x00113111
#define TK_LOOKDOWNZ        0x00113110
#define TK_LOOKUP           0x00112101
#define TK_LOOKUPZ          0x00112100

/* keywords and related tokens */
#define TK_PAR              0x00000001
#define TK_CNT              0x00000002
#define TK_INA              0x00000003
#define TK_INB              0x00000004
#define TK_OUTA             0x00000005
#define TK_OUTB             0x00000006
#define TK_DIRA             0x00000007
#define TK_DIRB             0x00000008
#define TK_CTRA             0x00000009
#define TK_CTRB             0x0000000a
#define TK_FRQA             0x0000000b
#define TK_FRQB             0x0000000c
#define TK_PHSA             0x0000000d
#define TK_PHSB             0x0000000e
#define TK_VCFG             0x0000000f
#define TK_VSCL             0x00000010
#define TK_CON              0x00000011
#define TK_DAT              0x00000012
#define TK_OBJ              0x00000013
#define TK_PRI              0x00000014
#define TK_PUB              0x00000015
#define TK_VAR              0x00000016
#define TK_BYTE             0x00000017
#define TK_WORD             0x00000018
#define TK_LONG             0x00000019
#define TK_HASH             0x0000001a
#define TK_DOLLAR           0x0000001b
#define TK_LPAREN           0x0000001c
#define TK_RPAREN           0x0000001d
#define TK_COMMA            0x0000001e
#define TK_DOT              0x0000001f
#define TK_DOTDOT           0x00000020
#define TK_COLON            0x00000021
#define TK_COLONEQUAL       0x00200022
#define TK_EQUAL            0x00000023
#define TK_ABORT            0x00000024
#define TK_CASE             0x00000025
#define TK_CONSTANT         0x00000026
#define TK_ELSE             0x00000027
#define TK_ELSEIF           0x00000028
#define TK_ELSEIFNOT        0x00000029
#define TK_FILE             0x0000002a
#define TK_FIT              0x0000002b
#define TK_FLOAT            0x0000002c
#define TK_FROM             0x0000002d
#define TK_IF               0x0000002e
#define TK_IFNOT            0x0000002f
#define TK_NEXT             0x00000030
#define TK_ORG              0x00000031
#define TK_ORGX             0x00000032
#define TK_OTHER            0x00000033
#define TK_QUIT             0x00000034
#define TK_REPEAT           0x00000035
#define TK_RES              0x00000036
#define TK_RESULT           0x00000037
#define TK_RETURN           0x00000038
#define TK_ROUND            0x00000039
#define TK_SPR              0x0000003a
#define TK_STEP             0x0000003b
#define TK_STRING           0x0000003c
#define TK_TO               0x0000003d
#define TK_TRUNC            0x0000003e
#define TK_UNTIL            0x0000003f
#define TK_WHILE            0x00000040
#define TK_LBRACKET         0x00000041
#define TK_BACKSLASH        0x00000042
#define TK_RBRACKET         0x00000043
#define TK_LBRACE           0x00000044
#define TK_RBRACE           0x00000045

/* pseudo tokens */
#define TK_EOL              0x000000fd
#define TK_EOF              0x000000fe
#define TK_UNKNOWN          0x000000ff

/* The following macros go a long way to documenting the above token values. */

#define IsId(t)           (t & TK_ID)             /* token is identifier */
#define IsIntLiteral(t)   (t & TK_INTLITERAL)     /* token is integer literal */
#define IsFloatLiteral(t) (t & TK_FLOATLITERAL)   /* token is float literal */

#define IsUnaryOp(t)      (t & TK_UNARY)                                     /* token is unary operator */
#define IsBinaryOp(t)     ((t & TK_UNARY|TK_BINARY)==TK_BINARY)              /* token is binary operator; note that binary and postfix share the same bit */
#define IsPostfixOp(t)    ((t & TK_UNARY|TK_POSTFIX)==(TK_UNARY|TK_POSTFIX)) /* token can be used postfix; see note above */
#define IsAssignmentOp(t) (t & TK_ASSIGNMENT)                                /* token is assignment operator */
#define GetPrecedence(t)  ((t & TK_BINARY|TK_UNARY) ? (t>>16) & 0x0f : 13)   /* get operator precedence, or 13 if token is not an operator */
#define GetSpinOpcode(t)  (t & 0xff)                                          /* return spin opcode for operator */
#define GetSpinOpcode2(t) ((t>>8) & 0xff)                                     /* return alternate spin opcode for operator used in postfix role*/

#define IsPasm(t)         (t & TK_PASM)            /* token is a PASM instruction mnemonic */
#define GetPasmOp(t)      (t<<16)                  /* return opcode for PASM instruction */
#define GetPasmDS(t)      ((t>>16) & 3)            /* return DS information for PASM instruction */

#define IsCond(t)         (t & TK_COND)            /* token is a PASM condition code */
#define GetCond(t)        (t & 0x0f)               /* return PASM condition code */

#define IsEffect(t)       (t & TK_EFFECT)          /* token is a PASM condition code */
#define GetEffect(t)      (t & 0x0f)               /* return PASM condition code */

#define IsReg(t)          (TK_PAR<=t && t<=TKVSCL) /* token is a special register name */
#define GetReg(t)         (t - TK_PAR)             /* return special register number (par == 0) */

#define IsBlockDesignator(t) (TK_CON<=t && t<=TK_VAR) /* token is a section designator */

#define IsSize(t)         (TK_BYTE<=t && t<=TK_LONG)  /* token is a size specifier */

#define IsStmtFn(t)       (t & TK_STMTFN)          /* token is a "statement function" (e.g. BYTEMOVE, WAITCNT) */
#define GetNumArgs(t)     ((t>>8) & 3)             /* return number of arguments of statement function */

#define IsIntrinsic(t)    (t & TK_INSTRINSIC)      /* token is an intrinsic (e.g. COGNEW, LOOKUP) */
