浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

http://www.lingcc.com/2009/12/24/10451/

http://www.bluishcoder.co.nz/2009/05/simple-nanojit-example.html

https://developer.mozilla.org/En/Nanojit

这位是搞编译的,厉害

nanojit是tracemonkey中的技术,从spidermonkey中加入此功能就升级到后者了

 

 

 

 

Mozilla Developer Network

powered by

Nanojit

Overview

Nanojit is a small, cross-platform C++ library that emits machine code. Both the Tamarin JIT and the SpiderMonkey JIT (a.k.a. TraceMonkey) use Nanojit as their back end.

You can get Nanojit by cloning the tamarin-redux Mercurial repository at http://hg.mozilla.org/tamarin-redux. It's in the nanojit directory.

The input for Nanojit is a stream of Nanojit LIR instructions. The term LIR is compiler jargon for a language used internally in a compiler that is usually cross-platform but very close to machine language. It is an acronym for "low-level intermediate representation". A compiler's LIR is typically one of several partly-compiled representations of a program that a compiler produces on the way from raw source code to machine code.

An application using Nanojit creates a nanojit::LirBuffer object to hold LIR instructions.  It creates a nanojit::LirBufWriter object to write instructions to the buffer.  Then it wraps the LirBufWriter in zero or more other LirWriter objects, all of which implement the same interface asLirBufWriter. This chain of LirWriter objects forms a pipeline for the instructions to pass through.  Each LirWriter can perform an optimization or other task on the program as it passes through the system and into the LirBuffer.

Once the instructions are in the LirBuffer, the application calls nanojit::compile() to produce machine code, which is stored in ananojit::Fragment. Internally to Nanojit, another set of filters operates on the LIR as it passes from the LirBuffer toward the assembler. The result of compilation is a function that the application can call from C via a pointer to the first instruction.

Example

The following code works with SpiderMonkey's hacked version of Nanojit.  Figuring out how to compile it is left as an exercise for the reader; the following works when run in the object directory of an --enable-debug SpiderMonkey shell:

g++ -DDEBUG -g3 -Wno-invalid-offsetof -fno-rtti -include js-confdefs.h -I dist/include/ -I.. -I ../nanojit -o jittest ../jittest.cpp libjs_static.a

(Remove the -DDEBUG if you have not compiled SpiderMonkey with --enable-debug, and use whatever you called the sample source file in place ofjittest.cpp.)

  1. #include <stdio.h>  
  2. #include <stdint.h>  
  3. #include "jsapi.h"  
  4. #include "jstracer.h"  
  5. #include "nanojit.h"  
  6.   
  7. using namespace nanojit;  
  8.   
  9. const uint32_t CACHE_SIZE_LOG2 = 20;  
  10.   
  11. static avmplus::GC gc = avmplus::GC();  
  12. static avmplus::AvmCore core = avmplus::AvmCore();  
  13.   
  14. int main()  
  15. {  
  16.     LogControl lc;  
  17. #ifdef DEBUG  
  18.     lc.lcbits = LC_ReadLIR | LC_Assembly;  
  19. #else  
  20.     lc.lcbits = 0;  
  21. #endif  
  22.   
  23.     // Set up the basic Nanojit objects.  
  24.     Allocator *alloc = new VMAllocator();  
  25.     CodeAlloc *codeAlloc = new CodeAlloc();  
  26.     Assembler *assm = new (&gc) Assembler(*codeAlloc, *alloc, &core, &lc);  
  27.     Fragmento *fragmento =  
  28.         new (&gc) Fragmento(&core, &lc, CACHE_SIZE_LOG2, codeAlloc);  
  29.     LirBuffer *buf = new (*alloc) LirBuffer(*alloc);  
  30.   
  31.     #ifdef DEBUG  
  32.     fragmento->labels = new (*alloc) LabelMap(*alloc, &lc);  
  33.     buf->names = new (*alloc) LirNameMap(*alloc, fragmento->labels);  
  34.     #endif  
  35.   
  36.     // Create a Fragment to hold some native code.  
  37.     Fragment *f = fragmento->getAnchor((void *)0xdeadbeef);  
  38.     f->lirbuf = buf;  
  39.     f->root = f;  
  40.   
  41.     // Create a LIR writer  
  42.     LirBufWriter out(buf);  
  43.   
  44.     // Write a few LIR instructions to the buffer: add the first parameter  
  45.     // to the constant 2.  
  46.     out.ins0(LIR_start);  
  47.     LIns *two = out.insImm(2);  
  48.     LIns *firstParam = out.insParam(0, 0);  
  49.     LIns *result = out.ins2(LIR_add, firstParam, two);  
  50.     out.ins1(LIR_ret, result);  
  51.   
  52.     // Emit a LIR_loop instruction.  It won't be reached, but there's  
  53.     // an assertion in Nanojit that trips if a fragment doesn't end with  
  54.     // a guard (a bug in Nanojit).  
  55.     LIns *rec_ins = out.insSkip(sizeof(GuardRecord) + sizeof(SideExit));  
  56.     GuardRecord *guard = (GuardRecord *) rec_ins->payload();  
  57.     memset(guard, 0, sizeof(*guard));  
  58.     SideExit *exit = (SideExit *)(guard + 1);  
  59.     guard->exit = exit;  
  60.     guard->exit->target = f;  
  61.     f->lastIns = out.insGuard(LIR_loop, out.insImm(1), rec_ins);  
  62.   
  63.     // Compile the fragment.  
  64.     compile(assm, f, *alloc verbose_only(, fragmento->labels));  
  65.     if (assm->error() != None) {  
  66.         fprintf(stderr, "error compiling fragment\n");  
  67.         return 1;  
  68.     }  
  69.     printf("Compilation successful.\n");  
  70.   
  71.     // Call the compiled function.  
  72.     typedef JS_FASTCALL int32_t (*AddTwoFn)(int32_t);  
  73.     AddTwoFn fn = reinterpret_cast<AddTwoFn>(f->code());  
  74.     printf("2 + 5 = %d\n", fn(5));  
  75.     return 0;  
  76. }  

Code Explanation


Interesting part are the lines 38-44:

 // Write a few LIR instructions to the buffer: add the first parameter
// to the constant 2.
out.ins0(LIR_start);
LIns *two = out.insImm(2);
LIns *firstParam = out.insParam(0, 0); 
LIns *result = out.ins2(LIR_add, firstParam, two);
out.ins1(LIR_ret, result);

Basically, what the code provided above is doing is feeding raw LIR into the LIR Buffer, using the LIRWriter's writer object. From an operational point of view, it is creating a function, which takes an integer input, and adds it to two, and outputs the result. The function is created here on lines 57-69:

 // Compile the fragment.
compile(assm, f, *alloc verbose_only(, fragmento->labels));             
if (assm->error() != None) {                                            
    fprintf(stderr, "error compiling fragment\n");                       
    return 1;                                            
}
printf("Compilation successful.\n");

// Call the compiled function.
typedef JS_FASTCALL int32_t (*AddTwoFn)(int32_t);
AddTwoFn fn = reinterpret_cast<addtwofn>(f->code());
printf("2 + 5 = %d\n", fn(5));
return 0;</addtwofn>

This upper half of this snippet includes code where the raw LIR is first converted into machine code.(where compile(fragmento->assm(), f); is called basically).
Then a pointer to a function is used, which takes an int as input and returns the sum of that parameter with two. (typedef JS_FASTCALL int32_t (*AddTwoFn)(int32_t); )
Then, printf is hardcoded to call it with a parameter 5, and on linking with nanojit library, the following program will display

2+5=7

Now, what I need to do is generate output for this:

start
two = int 2
twoPlusTwo = add two, two
ret twoPlusTwo

This adds two and two in the most hardcoded way possible. The conversion from LIR to a program like one shown above is the task of the parser.

Guards

Guards are special LIR instructions, similar to conditional branches, with the difference that when they are called, instead of going to a particular address, they leave the JIT code entirely, and stop the trace.

Need

Guards are required in a cross platform dynamic language like Javascript. Certain assumptions are made when a particular JIT code is generated.

For example, in an instruction INR x, a guard would check that x doesn't overflow the range for a 32 bit integer. The JIT code would have a guard checking this condition(an xt guard), and would return to the interpreter if the condition turns out to be true. The interpreter is then equipped to handle the overflow.

Hence, guards are needed to prevent certain erroneous behaviour that might result from the assumptions that are generally made while JIT is generated.

 

TODO: Explain guards, guard records, VMSideExitFragmentoVerboseWriter::formatGuard...

TAGS (0)

EDIT TAGS
  • No tags

ATTACHMENTS (0)

ATTACH FILE

Page last modified 03:31, 13 Aug 2009 by msullivan

 | Join MDN

posted on 2011-10-13 02:06  lexus  阅读(361)  评论(0编辑  收藏  举报