EN
games/alis / src / Alis / Lang / Grammar.php · commit 9c722cf
<?phpnamespace App\Alis\Lang;/** * The operands of every ALIS instruction (VM v2x / v3x, the PC / Atari / Amiga games: Robinson's Requiem, * Ishar, Transarctica...), read from the handlers of the VM (fn/opcodes_*.php): what each one reads from the * byte code after its opcode, in order. * * Operand tokens: *   E  an expression (opername)         S  a store target (storename)    A  an add target (addname) *   b  byte   w  word (s16)   W  word (u16)   l  long (s32) *   j8 j16 j24  relative jump (from the end of the operand; j24 = s16 + a pad byte, little endian) *   z  zero-terminated string   v  variable address (s16, a second s16 when the first is 0) *   rN N raw bytes *   @name  a special decoder (Disassembler::special*) */final class Grammar{    /** @var array<int, array{0: string, 1: string}> opcode => [name, operands] */    public const OPCODES = [        0x00 => ['cnul', ''], 0x01 => ['cesc1', '@esc1'], 0x02 => ['cesc2', 'b'], 0x03 => ['cesc3', 'b'],        0x04 => ['cbreakpt', ''], 0x05 => ['cjsr8', 'j8'], 0x06 => ['cjsr16', 'j16'], 0x07 => ['cjsr24', 'j24'],        0x08 => ['cjmp8', 'j8'], 0x09 => ['cjmp16', 'j16'], 0x0a => ['cjmp24', 'j24'], 0x0b => ['cjsrabs', ''],        0x0c => ['cjmpabs', ''], 0x0d => ['cjsrind16', ''], 0x0e => ['cjsrind24', ''], 0x0f => ['cjmpind16', ''],        0x10 => ['cjmpind24', ''], 0x11 => ['cret', ''], 0x12 => ['cbz8', 'j8'], 0x13 => ['cbz16', 'j16'],        0x14 => ['cbz24', 'j24'], 0x15 => ['cbnz8', 'j8'], 0x16 => ['cbnz16', 'j16'], 0x17 => ['cbnz24', 'j24'],        0x18 => ['cbeq8', 'j8'], 0x19 => ['cbeq16', 'j16'], 0x1a => ['cbeq24', 'j24'], 0x1b => ['cbne8', 'j8'],        0x1c => ['cbne16', 'j16'], 0x1d => ['cbne24', 'j24'], 0x1e => ['cstore', 'E S'], 0x1f => ['ceval', 'E'],        0x20 => ['cadd', 'E A'], 0x21 => ['csub', 'E A'], 0x22 => ['cmul', ''], 0x23 => ['cdiv', ''],        0x24 => ['cvprint', 'E'], 0x25 => ['csprinti', 'z'], 0x26 => ['csprinta', 'E'], 0x27 => ['clocate', 'E E'],        0x28 => ['ctab', 'E'], 0x29 => ['cdim', '@dim'], 0x2a => ['crandom', 'E'], 0x2b => ['cloop8', 'j8 A'],        0x2c => ['cloop16', 'j16 A'], 0x2d => ['cloop24', 'j24 A'], 0x2e => ['cswitch1', '@switch1'], 0x2f => ['cswitch2', '@switch2'],        0x30 => ['cstart8', 'j8'], 0x31 => ['cstart16', 'j16'], 0x32 => ['cstart24', 'j24'], 0x33 => ['cleave', ''],        0x34 => ['cprotect', 'E'], 0x35 => ['casleep', 'E'], 0x36 => ['cclock', 'E'], 0x37 => ['canim', ''],        0x38 => ['cscmov', 'E E E'], 0x39 => ['cscset', 'E E E'], 0x3a => ['cclipping', ''], 0x3b => ['cswitching', ''],        0x3c => ['cwlive', 'E w S'], 0x3d => ['cunload', 'w'], 0x3e => ['cwakeup', 'E'], 0x3f => ['csleep', ''],        0x40 => ['clive', 'w S'], 0x41 => ['ckill', 'E'], 0x42 => ['cstop', ''], 0x43 => ['cstopret', ''],        0x44 => ['cexit', ''], 0x45 => ['cload', '@load'], 0x46 => ['cdefsc', 'w b r32'], 0x47 => ['cscreen', 'w'],        0x48 => ['cput', 'E'], 0x49 => ['cputnat', 'E E E E E'], 0x4a => ['cerase', ''], 0x4b => ['cerasen', 'E'],        0x4c => ['cset', 'E E E'], 0x4d => ['cmov', 'E E E'], 0x4e => ['copensc', 'w'], 0x4f => ['cclosesc', 'w'],        0x50 => ['cerasall', ''], 0x51 => ['cforme', 'E'], 0x52 => ['cdelforme', ''], 0x53 => ['ctstmov', 'E E E E S'],        0x54 => ['ctstset', ''], 0x55 => ['cftstmov', 'E E E E E S'], 0x56 => ['cftstset', 'E E E E E S'], 0x57 => ['csuccent', 'S'],        0x58 => ['cpredent', 'S'], 0x59 => ['cnearent', 'E E S'], 0x5a => ['cneartyp', 'E w S'], 0x5b => ['cnearmat', 'E E S'],        0x5c => ['cviewent', 'E E E S'], 0x5d => ['cviewtyp', 'E E w S'], 0x5e => ['cviewmat', 'E E E S'], 0x5f => ['corient', 'E'],        0x60 => ['crstent', 'S'], 0x61 => ['csend', '@send'], 0x62 => ['cscanon', ''], 0x63 => ['cscanoff', ''],        0x64 => ['cinteron', ''], 0x65 => ['cinteroff', ''], 0x66 => ['cscanclr', ''], 0x67 => ['callentity', ''],        0x68 => ['cpalette', 'E'], 0x69 => ['cdefcolor', 'E E'], 0x6a => ['ctiming', 'E'], 0x6b => ['czap', '@zap'],        0x6c => ['cexplode', 'E E E E'], 0x6d => ['cding', 'E E E E'], 0x6e => ['cnoise', 'E E E E'], 0x6f => ['cinitab', ''],        0x70 => ['cfopen', '@fopen'], 0x71 => ['cfclose', ''], 0x72 => ['cfcreat', ''], 0x73 => ['cfdel', ''],        0x74 => ['cfreadv', 'S'], 0x75 => ['cfwritev', 'E'], 0x76 => ['cfwritei', ''], 0x77 => ['cfreadb', 'v l'],        0x78 => ['cfwriteb', 'v l'], 0x79 => ['cplot', 'E E'], 0x7a => ['cdraw', 'E E'], 0x7b => ['cbox', 'E E'],        0x7c => ['cboxf', 'E E'], 0x7d => ['cink', 'E'], 0x7e => ['cpset', 'E E'], 0x7f => ['cpmove', 'E E'],        0x80 => ['cpmode', 'E'], 0x81 => ['cpicture', ''], 0x82 => ['cxyscroll', 'E E'], 0x83 => ['clinking', 'E'],        0x84 => ['cmouson', ''], 0x85 => ['cmousoff', ''], 0x86 => ['cmouse', 'S S S'], 0x87 => ['cdefmouse', 'E'],        0x88 => ['csetmouse', 'E E'], 0x89 => ['cdefvect', '@defvect'], 0x8a => ['csetvect', 'w'], 0x8b => ['cnul2', ''],        0x8c => ['capproach', 'E'], 0x8d => ['cescape', 'E'], 0x8e => ['cvtstmov', 'E S'], 0x8f => ['cvftstmov', 'E E S'],        0x90 => ['cvmov', ''], 0x91 => ['cdefworld', 'w r6'], 0x92 => ['cworld', 'b b'], 0x93 => ['cfindmat', ''],        0x94 => ['cfindtyp', 'w S'], 0x95 => ['cmusic', 'E E E E E E'], 0x96 => ['cdelmusic', 'E'], 0x97 => ['ccadence', 'E'],        0x98 => ['csetvolum', 'E'], 0x99 => ['cxinv', ''], 0x9a => ['cxinvon', ''], 0x9b => ['cxinvoff', ''],        0x9c => ['clistent', 'S'], 0x9d => ['csound', 'E E E E E'], 0x9e => ['cmsound', 'E E E E E'], 0x9f => ['credon', ''],        0xa0 => ['credoff', ''], 0xa1 => ['cdelsound', ''], 0xa2 => ['cwmov', 'E'], 0xa3 => ['cwtstmov', ''],        0xa4 => ['cwftstmov', ''], 0xa5 => ['ctstform', 'E E S'], 0xa6 => ['cxput', 'E'], 0xa7 => ['cxputat', 'E E E E E'],        0xa8 => ['cmput', 'E'], 0xa9 => ['cmputat', 'E E E E E'], 0xaa => ['cmxput', 'E'], 0xab => ['cmxputat', 'E E E E E'],        0xac => ['cmmusic', 'E E E E E E'], 0xad => ['cmforme', 'E'], 0xae => ['csettime', 'E E E'], 0xaf => ['cgettime', 'S S S'],        0xb0 => ['cvinput', 'S'], 0xb1 => ['csinput', 'S'], 0xb2 => ['casleepfar', 'E'], 0xb3 => ['casleepon', ''],        0xb4 => ['casleepoff', ''], 0xb5 => ['crunfilm', 'E S S'], 0xb6 => ['cvpicprint', 'E'], 0xb7 => ['cspicprint', 'E'],        0xb8 => ['cvputprint', 'E'], 0xb9 => ['csputprint', 'E'], 0xba => ['cfont', 'E E E E E'], 0xbb => ['cpaper', 'E'],        0xbc => ['ctoblack', 'E'], 0xbd => ['cmovcolor', 'E E'], 0xbe => ['ctopalet', 'E E'], 0xbf => ['cnumput', 'E E E E'],        0xc0 => ['cscheart', 'E E'], 0xc1 => ['cscpos', 'E E'], 0xc2 => ['cscsize', 'E E'], 0xc3 => ['cschoriz', 'E E E'],        0xc4 => ['cscvertic', 'E E E'], 0xc5 => ['cscreduce', 'E E E'], 0xc6 => ['cscscale', 'E'], 0xc7 => ['creducing', 'E E'],        0xc8 => ['cscmap', 'E v'], 0xc9 => ['cscdump', ''], 0xca => ['cfindcla', 'E S'], 0xcb => ['cnearcla', ''],        0xcc => ['cviewcla', ''], 0xcd => ['cinstru', 'E E E'], 0xce => ['cminstru', 'E E E'], 0xcf => ['cordspr', 'E'],        0xd0 => ['calign', 'E E'], 0xd1 => ['cbackstar', 'E E E E E E'], 0xd2 => ['cstarring', 'E E E E E E E E'], 0xd3 => ['cengine', ''],        0xd4 => ['cautobase', ''], 0xd5 => ['cquality', 'E'], 0xd6 => ['chsprite', 'E E E E'], 0xd7 => ['cselpalet', 'E'],        0xd8 => ['clinepalet', 'E E'], 0xd9 => ['cautomode', ''], 0xda => ['cautofile', 'E'], 0xdb => ['ccancel', ''],        0xdc => ['ccancall', ''], 0xdd => ['ccancen', 'E'], 0xde => ['cblast', 'E'], 0xdf => ['cscback', 'E E E E E E'],        0xe0 => ['cscrolpage', 'E E E E'], 0xe1 => ['cmatent', 'S'], 0xe2 => ['cshrink', 'E E'], 0xe3 => ['cdefmap', 'v E E E E E E E'],        0xe4 => ['csetmap', 'v E E E E E E'], 0xe5 => ['cputmap', 'v E E E E'], 0xe6 => ['csavepal', 'E'], 0xe7 => ['csczoom', ''],        0xe8 => ['ctexmap', 'v E E E E E E E E E E E'], 0xe9 => ['calloctab', 'v'], 0xea => ['cfreetab', 'v'], 0xeb => ['cscantab', ''],        0xec => ['cneartab', ''], 0xed => ['cscsun', ''], 0xee => ['cdarkpal', 'E E E E E'], 0xef => ['cscdark', 'E E E'],        0xf0 => ['caset', 'E E E'], 0xf1 => ['camov', 'E E E'], 0xf2 => ['cscaset', ''], 0xf3 => ['cscamov', ''],        0xf4 => ['cscfollow', 'E E E E E E E E'], 0xf5 => ['cscview', 'E E E E E E'], 0xf6 => ['cfilm', 'E E E S'], 0xf7 => ['cwalkmap', 'v E E E S'],        0xf8 => ['catstmap', 'v E E E S'], 0xf9 => ['cavtstmov', 'E S'], 0xfa => ['cavmov', ''], 0xfb => ['caim', 'E E E E S S S'],        0xfc => ['cpointpix', 'E E S S S S S S'], 0xfd => ['cchartmap', '@chartmap'], 0xfe => ['cscsky', 'E E E'], 0xff => ['czoom', 'E E'],    ];    /** escape 1 (cesc1 + byte) */    public const ESC1 = [        0 => ['cnul', ''], 1 => ['csoundon', ''], 2 => ['csoundoff', ''], 3 => ['cmusicon', ''], 4 => ['cmusicoff', ''],        5 => ['cdelfilm', ''], 6 => ['copenfilm', 'E E E E S'],    ];    /**     * Expression items (opernames, even codes): [name, operands, kind].     * kind: v value (loads d7), t table (index = d7, more indices popped), b binary (reads its right operand),     * f function (no operand: works on d7 / the accumulator), e oeval (until ofin), x ofin.     * Operands: b u8 offset, w s16 offset, h two s16 (entity variable, offset), i8 / i16 / i32 immediates, z string.     */    public const OPERS = [        0x00 => ['oimmb', 'i8', 'v'], 0x02 => ['oimmw', 'i16', 'v'], 0x04 => ['oimmp', 'z', 'v'],        0x06 => ['olocb', 'w', 'v'], 0x08 => ['olocw', 'w', 'v'], 0x0a => ['olocp', 'w', 'v'],        0x0c => ['oloctp', 'w', 't'], 0x0e => ['oloctc', 'w', 't'], 0x10 => ['olocti', 'w', 't'],        0x12 => ['odirb', 'b', 'v'], 0x14 => ['odirw', 'b', 'v'], 0x16 => ['odirp', 'b', 'v'],        0x18 => ['odirtp', 'b', 't'], 0x1a => ['odirtc', 'b', 't'], 0x1c => ['odirti', 'b', 't'],        0x1e => ['omainb', 'w', 'v'], 0x20 => ['omainw', 'w', 'v'], 0x22 => ['omainp', 'w', 'v'],        0x24 => ['omaintp', 'w', 't'], 0x26 => ['omaintc', 'w', 't'], 0x28 => ['omainti', 'w', 't'],        0x2a => ['ohimb', 'h', 'v'], 0x2c => ['ohimw', 'h', 'v'], 0x2e => ['ohimp', 'h', 'v'],        0x30 => ['ohimtp', '', 'f'], 0x32 => ['ohimtc', '', 'f'], 0x34 => ['ohimti', '', 'f'],        0x36 => ['opile', '', 'f'], 0x38 => ['oeval', '', 'e'], 0x3a => ['ofin', '', 'x'],        0x3c => ['oimml', 'i32', 'v'], 0x3e => ['olocto', 'w', 't'], 0x40 => ['opushacc', '', 'f'],        0x42 => ['oand', '', 'b'], 0x44 => ['oor', '', 'b'], 0x46 => ['oxor', '', 'b'], 0x48 => ['oeqv', '', 'b'],        0x4a => ['oegal', '', 'b'], 0x4c => ['odiff', '', 'b'], 0x4e => ['oinfeg', '', 'b'], 0x50 => ['osupeg', '', 'b'],        0x52 => ['oinf', '', 'b'], 0x54 => ['osup', '', 'b'], 0x56 => ['oadd', '', 'b'], 0x58 => ['osub', '', 'b'],        0x5a => ['omod', '', 'b'], 0x5c => ['odiv', '', 'b'], 0x5e => ['omul', '', 'b'],        0x60 => ['oneg', '', 'f'], 0x62 => ['oabs', '', 'f'], 0x64 => ['ornd', '', 'f'], 0x66 => ['osgn', '', 'f'],        0x68 => ['onot', '', 'f'], 0x6a => ['oinkey', '', 'f'], 0x6c => ['okeyon', '', 'f'], 0x6e => ['ojoy', '', 'f'],        0x70 => ['oprnd', '', 'f'], 0x72 => ['oscan', '', 'f'], 0x74 => ['oshiftkey', '', 'f'], 0x76 => ['ofree', '', 'f'],        0x78 => ['omodel', '', 'f'], 0x7a => ['ogetkey', '', 'f'], 0x7c => ['oleft', '', 'f'], 0x7e => ['oright', '', 'f'],        0x80 => ['omid', '', 'f'], 0x82 => ['olen', '', 'f'], 0x84 => ['oasc', '', 'f'], 0x86 => ['ostr', '', 'f'],        0x88 => ['osadd', '', 'b'], 0x8a => ['osequal', '', 'b'], 0x8c => ['osdiff', '', 'b'], 0x8e => ['osinfeg', '', 'b'],        0x90 => ['ossupeg', '', 'b'], 0x92 => ['osinf', '', 'b'], 0x94 => ['ossup', '', 'b'], 0x96 => ['ospushacc', '', 'f'],        0x98 => ['ospile', '', 'f'], 0x9a => ['oval', '', 'f'], 0x9c => ['oexistf', '', 'f'], 0x9e => ['ochr', '', 'f'],        0xa0 => ['ochange', '', 'f'], 0xa2 => ['ocountry', '', 'f'], 0xa4 => ['omip', '', 'f'], 0xa6 => ['ojoykey', '', 'f'],        0xa8 => ['oconfig', '', 'f'], 0xaa => ['ogettext', '', 'f'], 0xac => ['oips', '', 'f'],    ];    /** Store targets (storenames): [name, operands, kind]; kind as OPERS (s = seval: push d7, an oeval, then a store). */    public const STORES = [        0x00 => ['pnul', '', 'f'], 0x02 => ['pnul', '', 'f'], 0x04 => ['pnul', '', 'f'],        0x06 => ['slocb', 'w', 'v'], 0x08 => ['slocw', 'w', 'v'], 0x0a => ['slocp', 'w', 'v'],        0x0c => ['sloctp', 'w', 't'], 0x0e => ['sloctc', 'w', 't'], 0x10 => ['slocti', 'w', 't'],        0x12 => ['sdirb', 'b', 'v'], 0x14 => ['sdirw', 'b', 'v'], 0x16 => ['sdirp', 'b', 'v'],        0x18 => ['sdirtp', 'b', 't'], 0x1a => ['sdirtc', 'b', 't'], 0x1c => ['sdirti', 'b', 't'],        0x1e => ['smainb', 'w', 'v'], 0x20 => ['smainw', 'w', 'v'], 0x22 => ['smainp', 'w', 'v'],        0x24 => ['smaintp', 'w', 't'], 0x26 => ['smaintc', 'w', 't'], 0x28 => ['smainti', 'w', 't'],        0x2a => ['shimb', 'h', 'v'], 0x2c => ['shimw', 'h', 'v'], 0x2e => ['shimp', '', 'f'],        0x30 => ['shimtp', '', 'f'], 0x32 => ['shimtc', '', 'f'], 0x34 => ['shimti', '', 'f'],        0x36 => ['spile', '', 'f'], 0x38 => ['seval', '', 's'], 0x3a => ['ofin', '', 'x'],    ];    /** Add targets (addnames). */    public const ADDS = [        0x00 => ['pnul', '', 'f'], 0x02 => ['pnul', '', 'f'], 0x04 => ['pnul', '', 'f'],        0x06 => ['alocb', 'w', 'v'], 0x08 => ['alocw', 'w', 'v'], 0x0a => ['alocp', 'w', 'v'],        0x0c => ['aloctp', 'w', 't'], 0x0e => ['aloctc', 'w', 't'], 0x10 => ['alocti', 'w', 't'],        0x12 => ['adirb', 'b', 'v'], 0x14 => ['adirw', 'b', 'v'], 0x16 => ['adirp', 'b', 'v'],        0x18 => ['adirtp', 'b', 't'], 0x1a => ['adirtc', 'b', 't'], 0x1c => ['adirti', 'b', 't'],        0x1e => ['amainb', 'w', 'v'], 0x20 => ['amainw', 'w', 'v'], 0x22 => ['amainp', 'w', 'v'],        0x24 => ['amaintp', 'w', 't'], 0x26 => ['amaintc', 'w', 't'], 0x28 => ['amainti', 'w', 't'],        0x2a => ['ahimb', '', 'f'], 0x2c => ['ahimw', 'h', 'v'], 0x2e => ['ahimp', '', 'f'],        0x30 => ['ahimtp', 'h', 't'], 0x32 => ['ahimtc', '', 'f'], 0x34 => ['ahimti', '', 'f'],        0x36 => ['spile', '', 'f'], 0x38 => ['aeval', '', 's'], 0x3a => ['ofin', '', 'x'],    ];}