Skip to content
chylaaPublic

About

W-machine minimal JIT compiler

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

W-machine Just-In-Time compiler


Toy project allowing to compile and run (on x86 machines) programs that are written in custom assembly of 'W-machine' computer (example implementation) - simplified computer model, designed in Computer Science Department of Silesian University of Technology for teaching basics of Computer Architecture.

W-machine is based on von Neumann architecture. All operations are performed using single accumulator register, for which in this project x86 register %eax is used as the virtual 32-bit ACC.


Quick build
gcc wmjit.c -m32 -I utilis tokenizer.c compiler.c utilis/utilis.c utilis/strlist.c utilis/strbuilder.c utilis/exmem.c -o bin/wmjit

Supports Windows and Linux systems (see exmem.c file).

Regarding 64-bit systems:

  • On Windows machines this should work out of the box for x86_64 GCC.
  • On Linux, one might be required to install gcc-multilib to supprot building 32-bit application. Kernels like Ubuntu are compiled with CONFIG_IA32_EMULATION=y flags, so we are able to run 32-bit programs directly on 64-bit system.
Usage

Program accepts input assembly code only from .prg files (see examples). When running on linux, make sure that input file lines are not terminated with CRLF.

Following options are avaliable:

Argument Description
-b BINFILE Write memory dump of .prg to BINFILE after it is compiled.
-d BINFILE Write memory dump of .prg to BINFILE after it finishes running.
--out=ADDR:FILE,... Set output device of ADDR to specific file (max 8 devs).
--in=ADDR:FILE,... Set input device of ADDR to specific file (max 8 devs).
--nr Do not run, only parse and compile.
--help Prints this message.
Instruction set
Instrucion Description
NOP No operation
STP Stop execution
ADD [address] Add value at memory address to accumulator
SUB [address] Subtract value at memory address from accumulator
LOAD [address] Load value from memory into accumulator
STOR [address] Store value from accumulator into memory address
JMP [address] Unconditional jump to address
BLZ [address] Branch if ACC less than zero
BEZ [address] Branch if ACC equal to zero
BGZ [address] Branch if ACC greater than zero
PSH Push an accumulator onto stack
POP Pop value from stack into accumulator
DIV [address] Divide accumulator by memory value
MUL [address] Multiply accumulator by memory value
IORD [device] Read value from input device into accumulator
IOWR [device] Write character from accumulator into output device
DEF [value] Define value data word
RES Reserve data word

Input and output

I/O operations using IORD and IOWR instructions are implemented as a call to specific function:

// Note: if 'device' < 0, 'c' is printed to |device| as integer not a character
__attribute__((__cdecl__))
static void __iowr(WORD_t device, int c); 

// Uses fgetc to read from mapped 'device' - potential EOF should be handled within jitted code
__attribute__((__cdecl__))
static int __iord(WORD_t device);

Following device mapping is created by default (can be overwritten using --out= and --in= options):

TYPE DEVICE FILE
OUT 1 stdout
OUT 2 stderr
IN 1 stdin

About

W-machine minimal JIT compiler

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages