| 1 | <?php |
| 2 | namespace Std\Lib\Syntax; |
| 3 | |
| 4 | /** |
| 5 | * MIPS(MIPS32/MIPS64、GAS) |
| 6 | */ |
| 7 | class AsmMips extends Assembly { |
| 8 | protected array $comment = ['#']; |
| 9 | protected ?array $multiline = ['/*', '*/']; |
| 10 | // 「$」はレジスター($t0、$31)なので16進数ではない |
| 11 | protected bool $dollarHex = false; |
| 12 | protected bool $percentBinary = false; |
| 13 | protected string $immediatePrefix = ''; |
| 14 | protected string $identifier = '\$\d+|[%$]?[.A-Za-z_][\w.$]*'; |
| 15 | // add.s → add、c.eq.d → c.eq → c |
| 16 | protected array $suffixes = ['/\.[a-z0-9]+$/', '/\.[a-z0-9]+$/']; |
| 17 | |
| 18 | protected array $tokens = [ |
| 19 | 'register' => [ |
| 20 | 'zero', 'at', 'v0', 'v1', 'a0', 'a1', 'a2', 'a3', 'k0', 'k1', 'gp', 'sp', 'fp', 'ra', 'hi', 'lo', 'pc', |
| 21 | ], |
| 22 | 'instruction' => [ |
| 23 | 'add', 'addu', 'addi', 'addiu', 'sub', 'subu', 'mult', 'multu', 'mul', 'madd', 'div', 'divu', 'mfhi', 'mflo', 'mthi', 'mtlo', |
| 24 | 'dadd', 'daddu', 'daddi', 'daddiu', 'dsub', 'dsubu', 'dmult', 'ddiv', |
| 25 | 'and', 'andi', 'or', 'ori', 'xor', 'xori', 'nor', 'sll', 'srl', 'sra', 'sllv', 'srlv', 'srav', 'dsll', 'dsrl', 'dsra', 'slt', 'sltu', 'slti', 'sltiu', |
| 26 | 'lui', 'lb', 'lbu', 'lh', 'lhu', 'lw', 'lwu', 'ld', 'lwl', 'lwr', 'll', 'sb', 'sh', 'sw', 'sd', 'swl', 'swr', 'sc', |
| 27 | 'beq', 'bne', 'bgez', 'bgtz', 'blez', 'bltz', 'bgezal', 'bltzal', 'j', 'jal', 'jr', 'jalr', 'syscall', 'break', 'eret', 'sync', 'nop', |
| 28 | 'mfc0', 'mtc0', 'mfc1', 'mtc1', 'lwc1', 'swc1', 'ldc1', 'sdc1', 'movn', 'movz', 'teq', 'ext', 'ins', 'seb', 'seh', 'clz', 'clo', |
| 29 | 'abs', 'cvt', 'c', 'bc1t', 'bc1f', 'mov', 'neg', 'sqrt', 'trunc', 'round', 'floor', 'ceil', |
| 30 | // 疑似命令 |
| 31 | 'li', 'la', 'move', 'b', 'bal', 'beqz', 'bnez', 'bge', 'bgt', 'ble', 'blt', 'not', 'negu', |
| 32 | ], |
| 33 | ]; |
| 34 | |
| 35 | public function prepare(string $code): void { |
| 36 | $tokens = $this->tokens; |
| 37 | $names = array_merge(self::numbered('t', 0, 9), self::numbered('s', 0, 8), self::numbered('f', 0, 31), self::numbered('', 0, 31)); |
| 38 | foreach ($names as $r) $tokens['register'][] = $r; |
| 39 | $this->words = $this->buildWords($tokens); |
| 40 | } |
| 41 | } |