Giter Site home page Giter Site logo

simplejit's Introduction

SimpleJIT

Not a very smart JIT compiler for C# instructions.

Not all OpCodes are supported.

Made for fun and for educational purposes (how to do it wrong).

Example

How to jit:

var jit = new SimpleJIT.SimpleJIT();
var MyMethod = (delegate* managed <void>)jit.JitForCalli(MyCoolMethod);
MyMethod(); // invoke

static void MyCoolMethod() {
  // some things
}

How to jit from Cecil:

// define your module and method
var module = Mono.Cecil.ModuleDefinition.CreateModule("jitfrommem", Mono.Cecil.ModuleKind.Dll);
var @long = module.ImportReference(typeof(long));
var method = new Mono.Cecil.MethodDefinition("MAX", Mono.Cecil.MethodAttributes.Static, @long);
method.Parameters.Add(new Mono.Cecil.ParameterDefinition("1", Mono.Cecil.ParameterAttributes.None, @long));
method.Parameters.Add(new Mono.Cecil.ParameterDefinition("2", Mono.Cecil.ParameterAttributes.None, @long));
method.Body = new MethodBody(method);
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
var ldarg0 = Instruction.Create(OpCodes.Ldarg_0);
method.Body.Instructions.Add(Instruction.Create(OpCodes.Bgt_S, ldarg0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
method.Body.Instructions.Add(ldarg0);
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
// create jit instance
var jit = new SimpleJIT.SimpleJIT();
// jit cecil method
var jitted = (delegate* managed <long, long, long>)myjit.JitForCalli(method);
// call
var result = jitted(400, 200); // result = 400

simplejit's People

Contributors

badryuner avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

lyf-is-coding

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.