contur2
Loading...
Searching...
No Matches
isa.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <cstdint>
11#include <string_view>
12
15
16namespace contur {
17
19 constexpr std::uint8_t INSTRUCTION_COUNT = 25;
20
22 [[nodiscard]] constexpr std::string_view instructionName(Instruction instr) noexcept
23 {
24 switch (instr)
25 {
27 return "NOP";
29 return "MOV";
31 return "ADD";
33 return "SUB";
35 return "MUL";
37 return "DIV";
39 return "AND";
40 case Instruction::Or:
41 return "OR";
43 return "XOR";
45 return "SHL";
47 return "SHR";
49 return "CMP";
51 return "JE";
53 return "JNE";
55 return "JG";
57 return "JL";
59 return "JGE";
61 return "JLE";
63 return "PUSH";
65 return "POP";
67 return "CALL";
69 return "RET";
71 return "RMEM";
73 return "WMEM";
75 return "INT";
77 return "HALT";
78 }
79 return "???";
80 }
81
83 [[nodiscard]] constexpr std::string_view interruptName(Interrupt intr) noexcept
84 {
85 switch (intr)
86 {
87 case Interrupt::Ok:
88 return "OK";
90 return "ERROR";
92 return "DIV_BY_ZERO";
94 return "SYSCALL";
96 return "PAGE_FAULT";
97 case Interrupt::Exit:
98 return "EXIT";
100 return "NETWORK_IO";
101 case Interrupt::Timer:
102 return "TIMER";
104 return "DEVICE_IO";
105 }
106 return "UNKNOWN";
107 }
108
110 [[nodiscard]] constexpr bool isArithmetic(Instruction instr) noexcept
111 {
112 return instr == Instruction::Add || instr == Instruction::Sub || instr == Instruction::Mul ||
113 instr == Instruction::Div;
114 }
115
117 [[nodiscard]] constexpr bool isLogic(Instruction instr) noexcept
118 {
119 return instr == Instruction::And || instr == Instruction::Or || instr == Instruction::Xor ||
121 }
122
124 [[nodiscard]] constexpr bool isJump(Instruction instr) noexcept
125 {
126 return instr == Instruction::JumpEqual || instr == Instruction::JumpNotEqual ||
127 instr == Instruction::JumpGreater || instr == Instruction::JumpLess ||
129 }
130
132 [[nodiscard]] constexpr bool isStackOp(Instruction instr) noexcept
133 {
134 return instr == Instruction::Push || instr == Instruction::Pop || instr == Instruction::Call ||
135 instr == Instruction::Return;
136 }
137
139 [[nodiscard]] constexpr bool isMemoryOp(Instruction instr) noexcept
140 {
141 return instr == Instruction::ReadMemory || instr == Instruction::WriteMemory;
142 }
143
145 [[nodiscard]] constexpr bool isHalt(Instruction instr) noexcept
146 {
147 return instr == Instruction::Halt;
148 }
149
150} // namespace contur
Instruction enum class — all opcodes for the bytecode interpreter.
Interrupt enum class — hardware and software interrupt codes.
Definition block.h:15
Interrupt
Interrupt codes used by the CPU and kernel.
Definition interrupt.h:17
@ PageFault
Page not present in physical memory.
Definition interrupt.h:22
@ SystemCall
Software interrupt — syscall entry.
Definition interrupt.h:21
@ Error
Generic error.
Definition interrupt.h:19
@ DeviceIO
Generic device I/O interrupt.
Definition interrupt.h:26
@ NetworkIO
Network I/O event.
Definition interrupt.h:24
@ Ok
No interrupt — normal execution.
Definition interrupt.h:18
@ DivByZero
Division by zero fault.
Definition interrupt.h:20
@ Timer
Timer tick interrupt (preemption).
Definition interrupt.h:25
@ Exit
Process requests termination.
Definition interrupt.h:23
Instruction
CPU instruction opcodes for the simulated architecture.
Definition instruction.h:16
@ Pop
Pop top of stack into register.
Definition instruction.h:36
@ Mov
Move immediate/register → register.
Definition instruction.h:18
@ Interrupt
Software interrupt (syscall, exit, etc.).
Definition instruction.h:41
@ JumpGreaterEqual
Jump if greater or equal.
Definition instruction.h:33
@ JumpNotEqual
Jump if not equal (ZF clear).
Definition instruction.h:30
@ JumpLess
Jump if less.
Definition instruction.h:32
@ JumpLessEqual
Jump if less or equal.
Definition instruction.h:34
@ Or
Bitwise OR: dst = dst | src.
Definition instruction.h:24
@ Div
Divide: dst = dst / src (may raise DivByZero).
Definition instruction.h:22
@ Halt
Halt execution.
Definition instruction.h:42
@ Mul
Multiply: dst = dst * src.
Definition instruction.h:21
@ ReadMemory
Read value from memory address into register.
Definition instruction.h:39
@ Xor
Bitwise XOR: dst = dst ^ src.
Definition instruction.h:25
@ Compare
Compare two registers (sets flags).
Definition instruction.h:28
@ Return
Return from subroutine (pop PC).
Definition instruction.h:38
@ Push
Push register onto stack.
Definition instruction.h:35
@ JumpEqual
Jump if equal (ZF set).
Definition instruction.h:29
@ Nop
No operation.
Definition instruction.h:17
@ WriteMemory
Write register value to memory address.
Definition instruction.h:40
@ And
Bitwise AND: dst = dst & src.
Definition instruction.h:23
@ Call
Call subroutine (push PC, jump).
Definition instruction.h:37
@ JumpGreater
Jump if greater.
Definition instruction.h:31
@ ShiftLeft
Left shift: dst = dst << src.
Definition instruction.h:26
@ Sub
Subtract: dst = dst - src.
Definition instruction.h:20
@ Add
Add: dst = dst + src.
Definition instruction.h:19
@ ShiftRight
Right shift: dst = dst >> src.
Definition instruction.h:27
constexpr std::string_view interruptName(Interrupt intr) noexcept
Returns the human-readable name for an interrupt code.
Definition isa.h:83
constexpr bool isStackOp(Instruction instr) noexcept
Returns true if the instruction affects the stack (Push, Pop, Call, Return).
Definition isa.h:132
constexpr std::uint8_t INSTRUCTION_COUNT
Total number of defined instructions.
Definition isa.h:19
constexpr bool isLogic(Instruction instr) noexcept
Returns true if the instruction is a logic/bitwise operation (And, Or, Xor, Shift).
Definition isa.h:117
constexpr bool isJump(Instruction instr) noexcept
Returns true if the instruction is a conditional or unconditional jump.
Definition isa.h:124
constexpr bool isMemoryOp(Instruction instr) noexcept
Returns true if the instruction involves memory access (ReadMemory, WriteMemory).
Definition isa.h:139
constexpr bool isArithmetic(Instruction instr) noexcept
Returns true if the instruction is an arithmetic operation (Add, Sub, Mul, Div).
Definition isa.h:110
constexpr std::string_view instructionName(Instruction instr) noexcept
Returns the human-readable mnemonic for an instruction.
Definition isa.h:22
constexpr bool isHalt(Instruction instr) noexcept
Returns true if the instruction terminates execution flow (Halt, Interrupt with Exit).
Definition isa.h:145