| 1 | <?php |
| 2 | namespace Std\Lib\Syntax; |
| 3 | |
| 4 | /** |
| 5 | * Motorola 68000(68k) |
| 6 | */ |
| 7 | class Asm68000 extends Assembly { |
| 8 | protected string $identifier = '[.A-Za-z_][\w.$]*'; |
| 9 | // move.l → move、dc.w → dc |
| 10 | protected array $suffixes = ['/\.[bwlsdxp]$/']; |
| 11 | |
| 12 | protected array $rules = [ |
| 13 | // 行頭の「*」もコメント |
| 14 | ['re' => '\*[^\n]*', 'class' => 'comment', 'bol' => true], |
| 15 | ]; |
| 16 | |
| 17 | protected array $tokens = [ |
| 18 | 'register' => [ |
| 19 | 'sp', 'usp', 'ssp', 'pc', 'sr', 'ccr', 'vbr', 'sfc', 'dfc', |
| 20 | ], |
| 21 | 'instruction' => [ |
| 22 | 'move', 'movea', 'moveq', 'movem', 'movep', 'lea', 'pea', 'exg', 'swap', 'ext', 'extb', 'clr', 'tst', 'tas', |
| 23 | 'add', 'adda', 'addi', 'addq', 'addx', 'sub', 'suba', 'subi', 'subq', 'subx', 'muls', 'mulu', 'divs', 'divu', 'neg', 'negx', |
| 24 | 'cmp', 'cmpa', 'cmpi', 'cmpm', 'chk', 'abcd', 'sbcd', 'nbcd', |
| 25 | 'and', 'andi', 'or', 'ori', 'eor', 'eori', 'not', 'lsl', 'lsr', 'asl', 'asr', 'rol', 'ror', 'roxl', 'roxr', |
| 26 | 'btst', 'bset', 'bclr', 'bchg', 'bra', 'bsr', 'jmp', 'jsr', 'rts', 'rte', 'rtr', 'trap', 'trapv', 'link', 'unlk', |
| 27 | 'nop', 'reset', 'stop', 'illegal', 'dbra', |
| 28 | ], |
| 29 | 'directive' => [ |
| 30 | 'dc', 'ds', 'dcb', 'even', 'odd', 'cnop', 'org', 'equ', 'set', 'rs', 'rsreset', 'rsset', 'section', 'text', 'data', 'bss', |
| 31 | 'xdef', 'xref', 'public', 'include', 'incbin', 'end', 'macro', 'endm', 'rept', 'endr', 'if', 'else', 'endc', 'endif', |
| 32 | 'ifd', 'ifnd', 'ifeq', 'ifne', 'opt', 'align', |
| 33 | ], |
| 34 | ]; |
| 35 | |
| 36 | public function prepare(string $code): void { |
| 37 | $tokens = $this->tokens; |
| 38 | foreach (['d', 'a', 'fp'] as $p) foreach (self::numbered($p, 0, 7) as $r) $tokens['register'][] = $r; |
| 39 | // 条件付き命令(beq, dbne, sne, trapcc等) |
| 40 | $cc = ['t', 'f', 'hi', 'ls', 'cc', 'hs', 'cs', 'lo', 'ne', 'eq', 'vc', 'vs', 'pl', 'mi', 'ge', 'lt', 'gt', 'le']; |
| 41 | foreach ($cc as $c) array_push($tokens['instruction'], "b{$c}", "db{$c}", "s{$c}", "trap{$c}"); |
| 42 | $this->words = $this->buildWords($tokens); |
| 43 | } |
| 44 | } |