Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/src/CodeGenA64.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "CodeGenA64.h"
3
4
#include "Luau/AssemblyBuilderA64.h"
5
#include "Luau/UnwindBuilder.h"
6
7
#include "BitUtils.h"
8
#include "CodeGenContext.h"
9
#include "CodeGenUtils.h"
10
#include "NativeState.h"
11
#include "EmitCommonA64.h"
12
13
#include "lstate.h"
14
15
LUAU_DYNAMIC_FASTFLAG(AddReturnExectargetCheck)
16
LUAU_FASTFLAG(LuauCodegenFreeBlocks)
17
18
namespace Luau
19
{
20
namespace CodeGen
21
{
22
namespace A64
23
{
24
25
struct EntryLocations
26
{
27
Label start;
28
Label prologueEnd;
29
Label epilogueStart;
30
};
31
32
static void emitExit(AssemblyBuilderA64& build, bool continueInVm)
33
{
34
build.mov(x0, continueInVm);
35
build.ldr(x1, mem(rNativeContext, offsetof(NativeContext, gateExit)));
36
build.br(x1);
37
}
38
39
static void emitUpdatePcForExit(AssemblyBuilderA64& build)
40
{
41
// x0 = pcpos * sizeof(Instruction)
42
build.add(x0, rCode, x0);
43
build.ldr(x1, mem(rState, offsetof(lua_State, ci)));
44
build.str(x0, mem(x1, offsetof(CallInfo, savedpc)));
45
}
46
47
static void emitClearNativeFlag(AssemblyBuilderA64& build)
48
{
49
build.ldr(x0, mem(rState, offsetof(lua_State, ci)));
50
build.ldr(w1, mem(x0, offsetof(CallInfo, flags)));
51
build.mov(w2, ~LUA_CALLINFO_NATIVE);
52
build.and_(w1, w1, w2);
53
build.str(w1, mem(x0, offsetof(CallInfo, flags)));
54
}
55
56
static void emitInterrupt(AssemblyBuilderA64& build)
57
{
58
// x0 = pc offset
59
// x1 = return address in native code
60
61
Label skip;
62
63
// Stash return address in rBase; we need to reload rBase anyway
64
build.mov(rBase, x1);
65
66
// Load interrupt handler; it may be nullptr in case the update raced with the check before we got here
67
build.ldr(x2, mem(rState, offsetof(lua_State, global)));
68
build.ldr(x2, mem(x2, offsetof(global_State, cb.interrupt)));
69
build.cbz(x2, skip);
70
71
// Update savedpc; required in case interrupt errors
72
build.add(x0, rCode, x0);
73
build.ldr(x1, mem(rState, offsetof(lua_State, ci)));
74
build.str(x0, mem(x1, offsetof(CallInfo, savedpc)));
75
76
// Call interrupt
77
build.mov(x0, rState);
78
build.mov(w1, -1);
79
build.blr(x2);
80
81
// Check if we need to exit
82
build.ldrb(w0, mem(rState, offsetof(lua_State, status)));
83
build.cbz(w0, skip);
84
85
// L->ci->savedpc--
86
// note: recomputing this avoids having to stash x0
87
build.ldr(x1, mem(rState, offsetof(lua_State, ci)));
88
build.ldr(x0, mem(x1, offsetof(CallInfo, savedpc)));
89
build.sub(x0, x0, uint16_t(sizeof(Instruction)));
90
build.str(x0, mem(x1, offsetof(CallInfo, savedpc)));
91
92
emitExit(build, /* continueInVm */ false);
93
94
build.setLabel(skip);
95
96
// Return back to caller; rBase has stashed return address
97
build.mov(x0, rBase);
98
99
emitUpdateBase(build); // interrupt may have reallocated stack
100
101
build.br(x0);
102
}
103
104
static void emitContinueCall(AssemblyBuilderA64& build, ModuleHelpers& helpers)
105
{
106
// x0 = closure object to reentry (equal to clvalue(L->ci->func))
107
108
// If the fallback yielded, we need to do this right away
109
// note: it's slightly cheaper to check x0 LSB; a valid Closure pointer must be aligned to 8 bytes
110
CODEGEN_ASSERT(CALL_FALLBACK_YIELD == 1);
111
build.tbnz(x0, 0, helpers.exitNoContinueVm);
112
113
// Need to update state of the current function before we jump away
114
build.ldr(x1, mem(x0, offsetof(Closure, l.p))); // cl->l.p aka proto
115
116
build.ldr(x2, mem(x1, offsetof(Proto, exectarget)));
117
build.cbz(x2, helpers.exitContinueVm);
118
119
build.mov(rClosure, x0);
120
121
static_assert(offsetof(Proto, code) == offsetof(Proto, k) + sizeof(Proto::k));
122
build.ldp(rConstants, rCode, mem(x1, offsetof(Proto, k))); // proto->k, proto->code
123
124
build.br(x2);
125
}
126
127
void emitReturn(AssemblyBuilderA64& build, ModuleHelpers& helpers)
128
{
129
// x1 = res
130
// w2 = number of written values
131
132
// x0 = ci
133
build.ldr(x0, mem(rState, offsetof(lua_State, ci)));
134
// w3 = ci->nresults
135
build.ldr(w3, mem(x0, offsetof(CallInfo, nresults)));
136
137
Label skipResultCopy;
138
139
// Fill the rest of the expected results (nresults - written) with 'nil'
140
build.cmp(w2, w3);
141
build.b(ConditionA64::GreaterEqual, skipResultCopy);
142
143
// TODO: cmp above could compute this and flags using subs
144
build.sub(w2, w3, w2); // counter = nresults - written
145
build.mov(w4, LUA_TNIL);
146
147
Label repeatNilLoop = build.setLabel();
148
build.str(w4, mem(x1, offsetof(TValue, tt)));
149
build.add(x1, x1, uint16_t(sizeof(TValue)));
150
build.sub(w2, w2, uint16_t(1));
151
build.cbnz(w2, repeatNilLoop);
152
153
build.setLabel(skipResultCopy);
154
155
// x2 = cip = ci - 1
156
build.sub(x2, x0, uint16_t(sizeof(CallInfo)));
157
158
// res = cip->top when nresults >= 0
159
Label skipFixedRetTop;
160
build.tbnz(w3, 31, skipFixedRetTop);
161
build.ldr(x1, mem(x2, offsetof(CallInfo, top))); // res = cip->top
162
build.setLabel(skipFixedRetTop);
163
164
// Update VM state (ci, base, top)
165
build.str(x2, mem(rState, offsetof(lua_State, ci))); // L->ci = cip
166
build.ldr(rBase, mem(x2, offsetof(CallInfo, base))); // sync base = L->base while we have a chance
167
build.str(rBase, mem(rState, offsetof(lua_State, base))); // L->base = cip->base
168
169
build.str(x1, mem(rState, offsetof(lua_State, top))); // L->top = res
170
171
// Unlikely, but this might be the last return from VM
172
build.ldr(w4, mem(x0, offsetof(CallInfo, flags)));
173
build.tbnz(w4, countrz(uint32_t(LUA_CALLINFO_RETURN)), helpers.exitNoContinueVm);
174
175
// Continue in interpreter if function has no native data
176
build.ldr(w4, mem(x2, offsetof(CallInfo, flags)));
177
build.tbz(w4, countrz(uint32_t(LUA_CALLINFO_NATIVE)), helpers.exitContinueVm);
178
179
// Need to update state of the current function before we jump away
180
build.ldr(rClosure, mem(x2, offsetof(CallInfo, func)));
181
build.ldr(rClosure, mem(rClosure, offsetof(TValue, value.gc)));
182
183
build.ldr(x1, mem(rClosure, offsetof(Closure, l.p))); // cl->l.p aka proto
184
185
if (DFFlag::AddReturnExectargetCheck)
186
{
187
// Get new instruction location
188
static_assert(offsetof(Proto, exectarget) == offsetof(Proto, execdata) + sizeof(Proto::execdata));
189
build.ldp(x3, x4, mem(x1, offsetof(Proto, execdata)));
190
build.cbz(x4, helpers.exitContinueVmClearNativeFlag);
191
}
192
193
static_assert(offsetof(Proto, code) == offsetof(Proto, k) + sizeof(Proto::k));
194
build.ldp(rConstants, rCode, mem(x1, offsetof(Proto, k))); // proto->k, proto->code
195
196
// Get instruction index from instruction pointer
197
// To get instruction index from instruction pointer, we need to divide byte offset by 4
198
// But we will actually need to scale instruction index by 4 back to byte offset later so it cancels out
199
build.ldr(x2, mem(x2, offsetof(CallInfo, savedpc))); // cip->savedpc
200
build.sub(x2, x2, rCode);
201
202
if (!DFFlag::AddReturnExectargetCheck)
203
{
204
// Get new instruction location and jump to it
205
static_assert(offsetof(Proto, exectarget) == offsetof(Proto, execdata) + sizeof(Proto::execdata));
206
build.ldp(x3, x4, mem(x1, offsetof(Proto, execdata)));
207
}
208
build.ldr(w2, mem(x3, x2));
209
build.add(x4, x4, x2);
210
build.br(x4);
211
}
212
213
static EntryLocations buildEntryFunction(AssemblyBuilderA64& build, UnwindBuilder& unwind)
214
{
215
EntryLocations locations;
216
217
// Arguments: x0 = lua_State*, x1 = Proto*, x2 = native code pointer to jump to, x3 = NativeContext*
218
219
locations.start = build.setLabel();
220
221
// prologue
222
build.sub(sp, sp, uint16_t(kStackSize));
223
build.stp(x29, x30, mem(sp)); // fp, lr
224
225
// stash non-volatile registers used for execution environment
226
build.stp(x19, x20, mem(sp, 16));
227
build.stp(x21, x22, mem(sp, 32));
228
build.stp(x23, x24, mem(sp, 48));
229
build.str(x25, mem(sp, 64));
230
231
build.mov(x29, sp); // this is only necessary if we maintain frame pointers, which we do in the JIT for now
232
233
locations.prologueEnd = build.setLabel();
234
235
uint32_t prologueSize = build.getLabelOffset(locations.prologueEnd) - build.getLabelOffset(locations.start);
236
237
// Setup native execution environment
238
build.mov(rState, x0);
239
build.mov(rNativeContext, x3);
240
build.ldr(rGlobalState, mem(x0, offsetof(lua_State, global)));
241
242
build.ldr(rBase, mem(x0, offsetof(lua_State, base))); // L->base
243
244
static_assert(offsetof(Proto, code) == offsetof(Proto, k) + sizeof(Proto::k));
245
build.ldp(rConstants, rCode, mem(x1, offsetof(Proto, k))); // proto->k, proto->code
246
247
build.ldr(x9, mem(x0, offsetof(lua_State, ci))); // L->ci
248
build.ldr(x9, mem(x9, offsetof(CallInfo, func))); // L->ci->func
249
build.ldr(rClosure, mem(x9, offsetof(TValue, value.gc))); // L->ci->func->value.gc aka cl
250
251
// Jump to the specified instruction; further control flow will be handled with custom ABI with register setup from EmitCommonA64.h
252
build.br(x2);
253
254
// Even though we jumped away, we will return here in the end
255
locations.epilogueStart = build.setLabel();
256
257
// Cleanup and exit
258
build.ldr(x25, mem(sp, 64));
259
build.ldp(x23, x24, mem(sp, 48));
260
build.ldp(x21, x22, mem(sp, 32));
261
build.ldp(x19, x20, mem(sp, 16));
262
build.ldp(x29, x30, mem(sp)); // fp, lr
263
build.add(sp, sp, uint16_t(kStackSize));
264
265
build.ret();
266
267
// Our entry function is special, it spans the whole remaining code area
268
unwind.startFunction();
269
unwind.prologueA64(prologueSize, kStackSize, {x29, x30, x19, x20, x21, x22, x23, x24, x25});
270
unwind.finishFunction(build.getLabelOffset(locations.start), kFullBlockFunction);
271
272
return locations;
273
}
274
275
bool initHeaderFunctions(BaseCodeGenContext& codeGenContext)
276
{
277
AssemblyBuilderA64 build(/* logText= */ false);
278
UnwindBuilder& unwind = *codeGenContext.unwindBuilder.get();
279
280
unwind.startInfo(UnwindBuilder::A64);
281
282
EntryLocations entryLocations = buildEntryFunction(build, unwind);
283
284
build.finalize();
285
286
unwind.finishInfo();
287
288
CODEGEN_ASSERT(build.data.empty());
289
290
uint8_t* codeStart = nullptr;
291
292
if (FFlag::LuauCodegenFreeBlocks)
293
{
294
codeGenContext.gateAllocationData = codeGenContext.codeAllocator.allocate(
295
build.data.data(),
296
int(build.data.size()),
297
reinterpret_cast<const uint8_t*>(build.code.data()),
298
int(build.code.size() * sizeof(build.code[0]))
299
);
300
301
if (!codeGenContext.gateAllocationData.start)
302
return false;
303
304
codeStart = codeGenContext.gateAllocationData.codeStart;
305
}
306
else
307
{
308
if (!codeGenContext.codeAllocator.allocate_DEPRECATED(
309
build.data.data(),
310
int(build.data.size()),
311
reinterpret_cast<const uint8_t*>(build.code.data()),
312
int(build.code.size() * sizeof(build.code[0])),
313
codeGenContext.gateData_DEPRECATED,
314
codeGenContext.gateDataSize_DEPRECATED,
315
codeStart
316
))
317
{
318
return false;
319
}
320
}
321
322
// Set the offset at the beginning so that functions in new blocks will not overlay the locations
323
// specified by the unwind information of the entry function
324
unwind.setBeginOffset(build.getLabelOffset(entryLocations.prologueEnd));
325
326
codeGenContext.context.gateEntry = codeStart + build.getLabelOffset(entryLocations.start);
327
codeGenContext.context.gateExit = codeStart + build.getLabelOffset(entryLocations.epilogueStart);
328
329
return true;
330
}
331
332
void assembleHelpers(AssemblyBuilderA64& build, ModuleHelpers& helpers)
333
{
334
if (build.logText)
335
build.logAppend("; updatePcAndContinueInVm\n");
336
build.setLabel(helpers.updatePcAndContinueInVm);
337
emitUpdatePcForExit(build);
338
339
if (build.logText)
340
build.logAppend("; exitContinueVmClearNativeFlag\n");
341
build.setLabel(helpers.exitContinueVmClearNativeFlag);
342
emitClearNativeFlag(build);
343
344
if (build.logText)
345
build.logAppend("; exitContinueVm\n");
346
build.setLabel(helpers.exitContinueVm);
347
emitExit(build, /* continueInVm */ true);
348
349
if (build.logText)
350
build.logAppend("; exitNoContinueVm\n");
351
build.setLabel(helpers.exitNoContinueVm);
352
emitExit(build, /* continueInVm */ false);
353
354
if (build.logText)
355
build.logAppend("; interrupt\n");
356
build.setLabel(helpers.interrupt);
357
emitInterrupt(build);
358
359
if (build.logText)
360
build.logAppend("; return\n");
361
build.setLabel(helpers.return_);
362
emitReturn(build, helpers);
363
364
if (build.logText)
365
build.logAppend("; continueCall\n");
366
build.setLabel(helpers.continueCall);
367
emitContinueCall(build, helpers);
368
}
369
370
} // namespace A64
371
} // namespace CodeGen
372
} // namespace Luau
373
374