Skip to content
This repository has been archived by the owner. It is now read-only.
Shivam Balikondwar edited this page May 3, 2020 · 2 revisions

Welcome to the moonjit wiki!

This wiki is primarily to document plans for development and also to document internal design.

Project work for adding JIT support on s390x arch for Moonjit.

JIT support for s390 is a long-awaited request from users and developers alike. We currently have an interpreter for LuaJIT in moonjit, but there is no backend for emitting machine code. discussion. This conveys the urge to write a functional assembler for s390 so that system admins or any developer in general can use Lua. Thus fully utilizing the power of the mainframe available at hand. This implementation will be achieved in the following four-phases:

1>ABI( Application binary interface). The first phase to write the backend for any arch begins with properly abstracting the underlying hardware. Part of this is already implemented as seen in "src/lj_target_s390.h". The code to initialise instructions set is remaining. I have referred the official documentation as mentioned here: manual1, manual2

The instructions and opcodes will be stored as a key-value pair. At the time to emit code, these instructions will be substituted with their respective opcodes. Eg: typedef enum{ OR = 0x16; // OR operation XR = 0x17; // XOR operation SEB = 0xED0B; // Substract DD = 0x6D; //Divide LH DDB = 0xED1D; //Divide LB } s390op;

To categorise the instructions properly they can be further arranged in their respective type enum. Eg: typedef enum{ LA, LAA, LAAG, LAALG, LAM, LAMY // load address instructions } s390op_load; This implementation will be enough but if there is any extra requirement I will add it eventually.

2>Implement functions to emit machine code from IR. After having received IR from the parser each of the IR instruction needs to be mapped and converted to its respective series of opcodes. The functions to emit machine code from the IR will be implemented in "src/lj_emit_s390x". Eg: For loading registers, we can use a single function "emit_loadk64" to pass the respective opcodes to the assembler. static void emit_loadk64(ASMState as, Reg r, IRIns ir){ / Perform necessary checks/ instr = ir->search(); // search opcode from table as->mcp =emit_s390opcode( r, ir ); //opcode reference }

3>Assembler to emit machine code. The assembler is where all the magic of converting the IR to machine code happens. I will first explain the execution flow of the assembler followed by the sample implementation. The JIT consists of profiler, recorder and tracer. The IR received from the parsing engine is first sent to the profiler. The profiler monitors the execution of each function which the interpreter executes from the code. When there is an operation which is very frequently used it is sent to the tracer. The tracer executes each received operation in a new trace(produces a compiled code). The tracer applies necessary guards while tracing the flow which will be used to execute this traced code more efficiently. When we hit the same code again, it will be replaced with its compiled version. This process continues until the code is completely compiled. The above process will be realised as functions. Eg: static void asm_setup_target(ASMState *as); //setup all needed exit stubs static void asm_guardcc(ASMState *as, int cc); //apply guards while tracing static void asm_genresult(ASMState *as, IRIns *ir); //gen result for callbacks static void *asm_gencall(as, args); // gen callback

4>Conformance for all tests for s390 as opposed to x86_64. Currently, there are around 587 tests for Moonjit for x86_64 platform. The x86 test output (here) and s390x test (experimental implementation: here) show the state of conformance with the standards. From what I have looked up till now as the assembler wasn't implemented the tests for trace and optimizations were not tested. There are a total of 127 tests. These include tests for slot_revival, phi function, snap, stitch for trace and DSE, FOLD, FWD, FUSE, LOOP and SINK in optimisation. I will debug and re-write all the failed tests for the assembler.

I have tried to write down what I think can be the way to realise this project. To better understand the codebase I am working to fix bugs currently present on the active fork of moonjit.

Clone this wiki locally