1
0
infernal-interpreter/infernal_interpreter/opcodes.py

110 lines
2.0 KiB
Python
Raw Normal View History

OPCODES = {}
2024-07-09 23:15:47 +00:00
def add_opcode(*opcode_names):
def wrapped(func):
2018-02-06 19:56:33 +00:00
for name in opcode_names:
OPCODES[name] = func
2024-07-09 23:15:47 +00:00
2018-02-06 19:56:33 +00:00
return wrapped
2024-07-09 23:15:47 +00:00
################################################################################
# Stack
2024-07-09 23:15:47 +00:00
@add_opcode('push', 'pushq')
def push(emu, value):
2018-02-06 19:56:33 +00:00
emu.pushToStack(emu.getVal(value))
2024-07-09 23:15:47 +00:00
@add_opcode('pop', 'popq')
def pop(emu, target_reg):
2018-02-06 19:56:33 +00:00
emu.registers[target_reg] = emu.popFromStack()
2024-07-09 23:15:47 +00:00
emu.changedRegisters((target_reg, 'change'))
# Arithmetic
2024-07-09 23:15:47 +00:00
@add_opcode('mov', 'movq')
def mov(emu, from_reg, target_reg):
2018-02-06 19:56:33 +00:00
emu.registers[target_reg] = emu.getVal(from_reg)
2024-07-09 23:15:47 +00:00
emu.changedRegisters((target_reg, 'change'))
2024-07-09 23:15:47 +00:00
@add_opcode('add', 'addq')
def add(emu, add_val, target_reg):
2018-02-06 19:56:33 +00:00
emu.registers[target_reg] += emu.getVal(add_val)
2024-07-09 23:15:47 +00:00
emu.changedRegisters((target_reg, 'change'))
2024-07-09 23:15:47 +00:00
@add_opcode('sub', 'subq')
def sub(emu, sub_val, target_reg):
2018-02-06 19:56:33 +00:00
emu.registers[target_reg] -= emu.getVal(sub_val)
2024-07-09 23:15:47 +00:00
emu.changedRegisters((target_reg, 'change'))
# Comparison and conditional jumping
2024-07-09 23:15:47 +00:00
@add_opcode('cmp', 'cmpq')
def comp(emu, val1, val2):
2018-02-06 19:56:33 +00:00
emu.compareVal(val1, val2)
2024-07-09 23:15:47 +00:00
@add_opcode('jg')
def jg(emu, label):
2018-02-06 19:56:33 +00:00
emu.jump(label, g=True)
2024-07-09 23:15:47 +00:00
@add_opcode('jl')
def jl(emu, label):
2018-02-06 19:56:33 +00:00
emu.jump(label, l=True)
2024-07-09 23:15:47 +00:00
@add_opcode('je')
def je(emu, label):
2018-02-06 19:56:33 +00:00
emu.jump(label, e=True)
2024-07-09 23:15:47 +00:00
@add_opcode('jge')
def jge(emu, label):
2024-07-09 23:15:47 +00:00
emu.jump(label, 'or', g=True, e=True)
2024-07-09 23:15:47 +00:00
@add_opcode('jle')
def jle(emu, label):
2024-07-09 23:15:47 +00:00
emu.jump(label, 'or', l=True, e=True)
2024-07-09 23:15:47 +00:00
@add_opcode('jne')
def jne(emu, label):
2018-02-06 19:56:33 +00:00
emu.jump(label, e=False)
2024-07-09 23:15:47 +00:00
# Unconditional Jumping
2024-07-09 23:15:47 +00:00
@add_opcode('jmp')
def jmp(emu, label):
2018-02-06 19:56:33 +00:00
emu.registers['%rip'] = emu.labels[label]
2024-07-09 23:15:47 +00:00
@add_opcode('call')
def call(emu, label):
2024-07-09 23:15:47 +00:00
emu.pushToStack(emu.registers['%rip'] + 1)
2018-02-06 19:56:33 +00:00
emu.registers['%rip'] = emu.labels[label]
2024-07-09 23:15:47 +00:00
@add_opcode('leave')
def leave(emu):
2024-07-09 23:15:47 +00:00
emu.registers['%rsp'] = emu.registers['%rbp']
emu.registers['%rbp'] = emu.popFromStack()
2024-07-09 23:15:47 +00:00
@add_opcode('ret')
def ret(emu):
2018-02-06 19:56:33 +00:00
emu.registers['%rip'] = emu.popFromStack()