2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "LowLevelInterpreter.h"
31 #include "LLIntOfflineAsmConfig.h"
32 #include <wtf/InlineASM.h>
34 #if ENABLE(LLINT_C_LOOP)
35 #include "CodeBlock.h"
36 #include "LLIntCLoop.h"
37 #include "LLIntSlowPaths.h"
38 #include "Operations.h"
39 #include "VMInspector.h"
40 #include <wtf/Assertions.h>
41 #include <wtf/MathExtras.h>
43 using namespace JSC::LLInt;
45 // LLInt C Loop opcodes
46 // ====================
47 // In the implementation of the C loop, the LLint trampoline glue functions
48 // (e.g. llint_program_prologue, llint_eval_prologue, etc) are addressed as
49 // if they are bytecode handlers. That means the names of the trampoline
50 // functions will be added to the OpcodeID list via the
51 // FOR_EACH_LLINT_OPCODE_EXTENSION() macro that FOR_EACH_OPCODE_ID()
54 // In addition, some JIT trampoline functions which are needed by LLInt
55 // (e.g. getHostCallReturnValue, ctiOpThrowNotCaught) are also added as
56 // bytecodes, and the CLoop will provide bytecode handlers for them.
58 // In the CLoop, we can only dispatch indirectly to these bytecodes
59 // (including the LLInt and JIT extensions). All other dispatches
60 // (i.e. goto's) must be to a known label (i.e. local / global labels).
63 // How are the opcodes named?
64 // ==========================
65 // Here is a table to show examples of how each of the manifestation of the
68 // Type: Opcode Trampoline Glue
69 // ====== ===============
70 // [In the llint .asm files]
71 // llint labels: llint_op_enter llint_program_prologue
73 // OpcodeID: op_enter llint_program
74 // [in Opcode.h] [in LLIntOpcode.h]
76 // When using a switch statement dispatch in the CLoop, each "opcode" is
78 // Opcode: case op_enter: case llint_program_prologue:
80 // When using a computed goto dispatch in the CLoop, each opcode is a label:
81 // Opcode: op_enter: llint_program_prologue:
84 //============================================================================
85 // Define the opcode dispatch mechanism when using the C loop:
88 // These are for building a C Loop interpreter:
89 #define OFFLINE_ASM_BEGIN
90 #define OFFLINE_ASM_END
93 #define OFFLINE_ASM_OPCODE_LABEL(opcode) DEFINE_OPCODE(opcode)
94 #if ENABLE(COMPUTED_GOTO_OPCODES)
95 #define OFFLINE_ASM_GLUE_LABEL(label) label:
97 #define OFFLINE_ASM_GLUE_LABEL(label) case label: label:
100 #define OFFLINE_ASM_LOCAL_LABEL(label) label:
103 //============================================================================
110 #if USE(JSVALUE32_64)
111 static double Ints2Double(uint32_t lo, uint32_t hi)
117 u.ival64 = (static_cast<uint64_t>(hi) << 32) | lo;
120 #endif // USE(JSVALUE32_64)
125 //============================================================================
126 // CLoopRegister is the storage for an emulated CPU register.
127 // It defines the policy of how ints smaller than intptr_t are packed into the
128 // pseudo register, as well as hides endianness differences.
130 struct CLoopRegister {
149 uint8_t u8padding[7];
152 #else // !CPU(BIG_ENDIAN)
167 uint8_t u8padding[7];
169 #endif // !CPU(BIG_ENDIAN)
170 #else // !USE(JSVALUE64)
180 uint8_t u8padding[3];
184 #else // !CPU(BIG_ENDIAN)
191 uint8_t u8padding[3];
193 #endif // !CPU(BIG_ENDIAN)
194 #endif // !USE(JSVALUE64)
198 ExecState* execState;
200 NativeFunction nativeFunc;
204 EncodedJSValue encodedJSValue;
211 inline void clearHighWord() { i32padding = 0; }
213 inline void clearHighWord() { }
217 //============================================================================
218 // The llint C++ interpreter loop:
221 JSValue CLoop::execute(CallFrame* callFrame, OpcodeID bootstrapOpcodeId,
222 bool isInitializationPass)
224 #define CAST reinterpret_cast
225 #define SIGN_BIT32(x) ((x) & 0x80000000)
227 // One-time initialization of our address tables. We have to put this code
228 // here because our labels are only in scope inside this function. The
229 // caller (or one of its ancestors) is responsible for ensuring that this
230 // is only called once during the initialization of the VM before threads
232 if (UNLIKELY(isInitializationPass)) {
233 #if ENABLE(COMPUTED_GOTO_OPCODES)
234 Opcode* opcodeMap = LLInt::opcodeMap();
235 #define OPCODE_ENTRY(__opcode, length) \
236 opcodeMap[__opcode] = bitwise_cast<void*>(&&__opcode);
237 FOR_EACH_OPCODE_ID(OPCODE_ENTRY)
240 #define LLINT_OPCODE_ENTRY(__opcode, length) \
241 opcodeMap[__opcode] = bitwise_cast<void*>(&&__opcode);
243 FOR_EACH_LLINT_NATIVE_HELPER(LLINT_OPCODE_ENTRY)
244 #undef LLINT_OPCODE_ENTRY
246 // Note: we can only set the exceptionInstructions after we have
247 // initialized the opcodeMap above. This is because getCodePtr()
248 // can depend on the opcodeMap.
249 Instruction* exceptionInstructions = LLInt::exceptionInstructions();
250 for (int i = 0; i < maxOpcodeLength + 1; ++i)
251 exceptionInstructions[i].u.pointer =
252 LLInt::getCodePtr(llint_throw_from_slow_path_trampoline);
257 ASSERT(callFrame->globalData().topCallFrame == callFrame);
259 // Define the pseudo registers used by the LLINT C Loop backend:
260 ASSERT(sizeof(CLoopRegister) == sizeof(intptr_t));
262 union CLoopDoubleRegister {
269 // The CLoop llint backend is initially based on the ARMv7 backend, and
270 // then further enhanced with a few instructions from the x86 backend to
271 // support building for X64 targets. Hence, the shape of the generated
272 // code and the usage convention of registers will look a lot like the
275 // For example, on a 32-bit build:
276 // 1. Outgoing args will be set up as follows:
277 // arg1 in t0 (r0 on ARM)
278 // arg2 in t1 (r1 on ARM)
279 // 2. 32 bit return values will be in t0 (r0 on ARM).
280 // 3. 64 bit return values (e.g. doubles) will be in t0,t1 (r0,r1 on ARM).
282 // But instead of naming these simulator registers based on their ARM
283 // counterparts, we'll name them based on their original llint asm names.
284 // This will make it easier to correlate the generated code with the
285 // original llint asm code.
287 // On a 64-bit build, it more like x64 in that the registers are 64 bit.
289 // 1. Outgoing args are still the same: arg1 in t0, arg2 in t1, etc.
290 // 2. 32 bit result values will be in the low 32-bit of t0.
291 // 3. 64 bit result values will be in t0.
293 CLoopRegister t0, t1, t2, t3;
295 CLoopRegister rBasePC, tagTypeNumber, tagMask;
297 CLoopRegister rRetVPC;
298 CLoopDoubleRegister d0, d1;
301 // Keep the compiler happy. We don't really need this, but the compiler
302 // will complain. This makes the warning go away.
307 // Instantiate the pseudo JIT stack frame used by the LLINT C Loop backend:
308 JITStackFrame jitStackFrame;
310 // The llint expects the native stack pointer, sp, to be pointing to the
311 // jitStackFrame (which is the simulation of the native stack frame):
312 JITStackFrame* const sp = &jitStackFrame;
313 sp->globalData = &callFrame->globalData();
315 // Set up an alias for the globalData ptr in the JITStackFrame:
316 JSGlobalData* &globalData = sp->globalData;
318 CodeBlock* codeBlock = callFrame->codeBlock();
321 // rPC is an alias for vPC. Set up the alias:
322 CLoopRegister& rPC = *CAST<CLoopRegister*>(&vPC);
324 #if USE(JSVALUE32_64)
325 vPC = codeBlock->instructions().begin();
326 #else // USE(JSVALUE64)
328 rBasePC.vp = codeBlock->instructions().begin();
330 // For the ASM llint, JITStubs takes care of this initialization. We do
331 // it explicitly here for the C loop:
332 tagTypeNumber.i = 0xFFFF000000000000;
333 tagMask.i = 0xFFFF000000000002;
334 #endif // USE(JSVALUE64)
336 // cfr is an alias for callFrame. Set up this alias:
337 CLoopRegister& cfr = *CAST<CLoopRegister*>(&callFrame);
339 // Simulate a native return PC which should never be used:
340 rRetVPC.i = 0xbbadbeef;
342 // Interpreter variables for value passing between opcodes and/or helpers:
343 NativeFunction nativeFunc = 0;
344 JSValue functionReturnValue;
347 opcode = LLInt::getOpcode(bootstrapOpcodeId);
349 #if ENABLE(OPCODE_STATS)
350 #define RECORD_OPCODE_STATS(__opcode) \
351 OpcodeStats::recordInstruction(__opcode)
353 #define RECORD_OPCODE_STATS(__opcode)
356 #if USE(JSVALUE32_64)
357 #define FETCH_OPCODE() vPC->u.opcode
358 #else // USE(JSVALUE64)
359 #define FETCH_OPCODE() *bitwise_cast<Opcode*>(rBasePC.i8p + rPC.i * 8)
360 #endif // USE(JSVALUE64)
362 #define NEXT_INSTRUCTION() \
364 opcode = FETCH_OPCODE(); \
368 #if ENABLE(COMPUTED_GOTO_OPCODES)
370 //========================================================================
371 // Loop dispatch mechanism using computed goto statements:
373 #define DISPATCH_OPCODE() goto *opcode
375 #define DEFINE_OPCODE(__opcode) \
377 RECORD_OPCODE_STATS(__opcode);
379 // Dispatch to the current PC's bytecode:
382 #else // !ENABLE(COMPUTED_GOTO_OPCODES)
383 //========================================================================
384 // Loop dispatch mechanism using a C switch statement:
386 #define DISPATCH_OPCODE() goto dispatchOpcode
388 #define DEFINE_OPCODE(__opcode) \
391 RECORD_OPCODE_STATS(__opcode);
393 // Dispatch to the current PC's bytecode:
397 #endif // !ENABLE(COMPUTED_GOTO_OPCODES)
399 //========================================================================
400 // Bytecode handlers:
402 // This is the file generated by offlineasm, which contains all of the
403 // bytecode handlers for the interpreter, as compiled from
404 // LowLevelInterpreter.asm and its peers.
406 #include "LLIntAssembly.h"
408 // In the ASM llint, getHostCallReturnValue() is a piece of glue
409 // function provided by the JIT (see dfg/DFGOperations.cpp).
410 // We simulate it here with a pseduo-opcode handler.
411 OFFLINE_ASM_GLUE_LABEL(getHostCallReturnValue)
413 // The ASM part pops the frame:
414 callFrame = callFrame->callerFrame();
416 // The part in getHostCallReturnValueWithExecState():
417 JSValue result = globalData->hostCallReturnValue;
418 #if USE(JSVALUE32_64)
420 t0.i = result.payload();
422 t0.encodedJSValue = JSValue::encode(result);
427 OFFLINE_ASM_GLUE_LABEL(ctiOpThrowNotCaught)
429 return globalData->exception;
432 #if !ENABLE(COMPUTED_GOTO_OPCODES)
437 } // END bytecode handler cases.
439 //========================================================================
444 if (callFrame->hasHostCallFrameFlag()) {
445 #if USE(JSVALUE32_64)
446 return JSValue(t1.i, t0.i); // returning JSValue(tag, payload);
448 return JSValue::decode(t0.encodedJSValue);
452 // The normal ASM llint call implementation returns to the caller as
453 // recorded in rRetVPC, and the caller would fetch the return address
454 // from ArgumentCount.tag() (see the dispatchAfterCall() macro used in
455 // the callTargetFunction() macro in the llint asm files).
457 // For the C loop, we don't have the JIT stub to this work for us.
458 // So, we need to implement the equivalent of dispatchAfterCall() here
459 // before dispatching to the PC.
461 vPC = callFrame->currentVPC();
464 // Based on LowLevelInterpreter64.asm's dispatchAfterCall():
466 // When returning from a native trampoline call, unlike the assembly
467 // LLInt, we can't simply return to the caller. In our case, we grab
468 // the caller's VPC and resume execution there. However, the caller's
469 // VPC returned by callFrame->currentVPC() is in the form of the real
470 // address of the target bytecode, but the 64-bit llint expects the
471 // VPC to be a bytecode offset. Hence, we need to map it back to a
472 // bytecode offset before we dispatch via the usual dispatch mechanism
473 // i.e. NEXT_INSTRUCTION():
475 codeBlock = callFrame->codeBlock();
477 rPC.vp = callFrame->currentVPC();
478 rPC.i = rPC.i8p - reinterpret_cast<int8_t*>(codeBlock->instructions().begin());
481 rBasePC.vp = codeBlock->instructions().begin();
482 #endif // USE(JSVALUE64)
486 } // END doReturnHelper.
489 // Keep the compiler happy so that it doesn't complain about unused
490 // labels for the LLInt trampoline glue. The labels are automatically
491 // emitted by label macros above, and some of them are referenced by
492 // the llint generated code. Since we can't tell ahead of time which
493 // will be referenced and which will be not, we'll just passify the
494 // compiler on all such labels:
495 #define LLINT_OPCODE_ENTRY(__opcode, length) \
496 UNUSED_LABEL(__opcode);
497 FOR_EACH_OPCODE_ID(LLINT_OPCODE_ENTRY);
498 #undef LLINT_OPCODE_ENTRY
501 #undef NEXT_INSTRUCTION
503 #undef CHECK_FOR_TIMEOUT
507 } // Interpreter::llintCLoopExecute()
511 #else // !ENABLE(LLINT_C_LOOP)
513 //============================================================================
514 // Define the opcode dispatch mechanism when using an ASM loop:
517 // These are for building an interpreter from generated assembly code:
518 #define OFFLINE_ASM_BEGIN asm (
519 #define OFFLINE_ASM_END );
521 #define OFFLINE_ASM_OPCODE_LABEL(__opcode) OFFLINE_ASM_GLOBAL_LABEL(llint_##__opcode)
522 #define OFFLINE_ASM_GLUE_LABEL(__opcode) OFFLINE_ASM_GLOBAL_LABEL(__opcode)
525 #define OFFLINE_ASM_GLOBAL_LABEL(label) \
526 ".globl " SYMBOL_STRING(label) "\n" \
527 HIDE_SYMBOL(label) "\n" \
529 ".thumb_func " THUMB_FUNC_PARAM(label) "\n" \
530 SYMBOL_STRING(label) ":\n"
532 #define OFFLINE_ASM_GLOBAL_LABEL(label) \
533 ".globl " SYMBOL_STRING(label) "\n" \
534 HIDE_SYMBOL(label) "\n" \
535 SYMBOL_STRING(label) ":\n"
538 #define OFFLINE_ASM_LOCAL_LABEL(label) LOCAL_LABEL_STRING(label) ":\n"
540 // This is a file generated by offlineasm, which contains all of the assembly code
541 // for the interpreter, as compiled from LowLevelInterpreter.asm.
542 #include "LLIntAssembly.h"
544 #endif // !ENABLE(LLINT_C_LOOP)
546 #endif // ENABLE(LLINT)