machine.go (3606B)
1 // Package egg/machine has the interface and syscall struct used by EGG backends. 2 package machine 3 4 import ( 5 "github.com/gboncoffee/egg/assembler" 6 "github.com/gboncoffee/intergo" 7 ) 8 9 type ArchitectureInfo struct { 10 Name string 11 RegistersNames []string 12 WordWidth uint16 13 } 14 15 // Interface that machine structs are required to implement to work with EGG. 16 // 17 // It always use uint64 because if we used variable-lengh []bytes, they would 18 // need to be stored on the heap. Converting from any size less than and to 64 19 // bits can be done with a single x86 instruction. 20 type Machine interface { 21 // Generic function for loading the program into memory and initializing 22 // anything the machine may need. After this, the machine should be 23 // ready to receive sequences of NextInstruction(). 24 LoadProgram([]uint8) error 25 // Executes the next instruction. The execution should be handled in a 26 // way that the instruction is executed and only them the instruction 27 // pointer is incremented, thus the pointer always points to the 28 // instruction to be performed next. 29 NextInstruction() (*Call, error) 30 // Retrieves a single byte from memory. 31 GetMemory(uint64) (uint8, error) 32 // Sets a single byte of memory. 33 SetMemory(uint64, uint8) error 34 // Retrieves multiple bytes from memory. First argument is the address 35 // and second argument is the size. Should return an error if 36 // address+size is not addressable. 37 GetMemoryChunk(uint64, uint64) ([]uint8, error) 38 // Same as GetMemoryChunk but for setting. 39 SetMemoryChunk(uint64, []uint8) error 40 // Gets the content of a register. When the ISA has registers with 41 // ambiguous numbers, the backend should define unambiguous numbers for 42 // them and only translate these numbers when needed on the instruction 43 // execution. Example: a RISC-V backend with the floating-point 44 // extension may define the floating-point registers as registers 32-63. 45 GetRegister(uint64) (uint64, error) 46 // Same as GetRegister but for setting. First argument is the register 47 // number, and second is the content. This may fail silently if trying 48 // to write to a forbidden register (as with x0 in RISC-V), so the 49 // caller should not expected it to work always. 50 SetRegister(uint64, uint64) error 51 // Translates a register name (as used in Assembly) to it's number. 52 // Registers may have more than a name (example: in the RISC-V backend, 53 // both "zero" and "x0" translates to the register number 0) 54 GetRegisterNumber(string) (uint64, error) 55 // Assembles a program. Usually a bunch of calls to functions on the 56 // egg/assembler package. If the second return value is nil, debugger 57 // support is disabled. 58 Assemble(string) ([]uint8, []assembler.DebuggerToken, error) 59 // Self-explanatory. Usually just a "return m.pc" or something like 60 // that. 61 GetCurrentInstructionAddress() uint64 62 // Self-explanatory. 63 ArchitectureInfo() ArchitectureInfo 64 } 65 66 // Syscalls numbers. ISAs with specific calls for BREAK should send a BREAK on them. 67 // 68 // BREAK - 1 - Transfer control to debugger or stop machine. 69 // - No arguments. 70 // 71 // READ - 2 - Read input. 72 // - Arg1: Buffer address (will be put into SetMemoryChunk()). 73 // - Arg2: Size in bytes of input. 74 // 75 // WRITE - 3 - Write output. 76 // - Arg1: Buffer address (will be put into GetMemoryChunk()). 77 // - Arg2: Size in bytes of output. 78 const ( 79 SYS_BREAK = 1 80 SYS_READ = 2 81 SYS_WRITE = 3 82 ) 83 84 // Struct returned from NextInstruction() when a call is performed. 85 type Call struct { 86 Number uint64 87 Arg1 uint64 88 Arg2 uint64 89 } 90 91 // Internationalization context available for architectures. 92 var InterCtx intergo.InterContext