/src/mozilla-central/js/src/frontend/JumpList.h
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 | | * vim: set ts=8 sts=4 et sw=4 tw=99: |
3 | | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef frontend_JumpList_h |
8 | | #define frontend_JumpList_h |
9 | | |
10 | | #include <stddef.h> |
11 | | |
12 | | #include "js/TypeDecls.h" |
13 | | |
14 | | namespace js { |
15 | | namespace frontend { |
16 | | |
17 | | // Linked list of jump instructions that need to be patched. The linked list is |
18 | | // stored in the bytes of the incomplete bytecode that will be patched, so no |
19 | | // extra memory is needed, and patching the instructions destroys the list. |
20 | | // |
21 | | // Example: |
22 | | // |
23 | | // JumpList brList; |
24 | | // if (!emitJump(JSOP_IFEQ, &brList)) |
25 | | // return false; |
26 | | // ... |
27 | | // JumpTarget label; |
28 | | // if (!emitJumpTarget(&label)) |
29 | | // return false; |
30 | | // ... |
31 | | // if (!emitJump(JSOP_GOTO, &brList)) |
32 | | // return false; |
33 | | // ... |
34 | | // patchJumpsToTarget(brList, label); |
35 | | // |
36 | | // +-> -1 |
37 | | // | |
38 | | // | |
39 | | // ifeq .. <+ + +-+ ifeq .. |
40 | | // .. | | .. |
41 | | // label: | +-> label: |
42 | | // jumptarget | | jumptarget |
43 | | // .. | | .. |
44 | | // goto .. <+ + +-+ goto .. <+ |
45 | | // | | |
46 | | // | | |
47 | | // + + |
48 | | // brList brList |
49 | | // |
50 | | // | ^ |
51 | | // +------- patchJumpsToTarget -------+ |
52 | | // |
53 | | |
54 | | // Offset of a jump target instruction, used for patching jump instructions. |
55 | | struct JumpTarget { |
56 | | ptrdiff_t offset; |
57 | | }; |
58 | | |
59 | | struct JumpList { |
60 | 11.4k | JumpList() {} |
61 | | // -1 is used to mark the end of jump lists. |
62 | | ptrdiff_t offset = -1; |
63 | | |
64 | | // Add a jump instruction to the list. |
65 | | void push(jsbytecode* code, ptrdiff_t jumpOffset); |
66 | | |
67 | | // Patch all jump instructions in this list to jump to `target`. This |
68 | | // clobbers the list. |
69 | | void patchAll(jsbytecode* code, JumpTarget target); |
70 | | }; |
71 | | |
72 | | } /* namespace frontend */ |
73 | | } /* namespace js */ |
74 | | |
75 | | #endif /* frontend_JumpList_h */ |