-
Notifications
You must be signed in to change notification settings - Fork 11
/
script.cc
521 lines (478 loc) · 13.7 KB
/
script.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//---------------------------------------------------------------------------
#include "script.h"
#include "console.h"
#include "mem.h"
#include "varint_writer.h"
#include <assert.h>
//---------------------------------------------------------------------------
void Script::Reset() {
stackTop = stack;
Mem::Clear(globals);
}
bool Script::IsScriptEmpty(size_t offset) const {
return offset == 0 ||
((uint8_t *)byteCode)[offset] == StenoScriptByteCode::RETURN;
}
void Script::ExecuteScript(size_t offset) {
if (offset == 0) {
return;
}
intptr_t *const start = stackTop;
Run(offset);
assert(stackTop == start);
}
void Script::ExecuteScript(size_t offset, const intptr_t *parameters,
size_t parameterCount) {
if (offset == 0) {
return;
}
intptr_t *const start = stackTop;
for (size_t i = 0; i < parameterCount; ++i) {
Push(parameters[i]);
}
Run(offset);
assert(stackTop == start);
}
void Script::PrintScriptGlobals() const {
char buffer[256 * 5 + 4];
VarintWriter writer(buffer);
int i = 0;
while (i < 256) {
int j = i;
while (j < 256 && globals[j] == 0) {
++j;
}
writer.Write(j - i);
i = j;
while (j < 256 && globals[j] != 0) {
++j;
}
writer.Write(j - i);
for (; i < j; ++i) {
writer.Write(VarintWriter::ZigZagEncode((int32_t)globals[i]));
}
}
Console::Printf("%D\n\n", buffer, size_t(writer.p - buffer));
}
//---------------------------------------------------------------------------
// This local class optimizes access to the stack, and places the pointer
// in a register, which dramatically reduces the memory accesses when
// executing a script.
class Script::StackPointer {
public:
StackPointer(Script &script) { SetStackTop(script.stackTop); }
void WriteBack(Script &script) { script.stackTop = GetStackTop(); }
void Load(Script &script) { SetStackTop(script.stackTop); }
intptr_t *GetStackTop() const { return p; }
void SetStackTop(intptr_t *stackTop) { p = stackTop; }
intptr_t Pop() {
#if JAVELIN_CPU_CORTEX_M4
intptr_t r;
asm("ldr %0, [%1, #-4]!" : "=l"(r), "+l"(p));
return r;
#else
return *--p;
#endif
}
void Push(intptr_t v) {
#if JAVELIN_CPU_CORTEX_M4
asm("stmia %0!, {%1}" : "+l"(p) : "l"(v));
#else
*p++ = v;
#endif
}
void UnaryOp(intptr_t (*op)(intptr_t)) { Push(op(Pop())); }
template <typename T> void TwoParam(T op) {
#if JAVELIN_CPU_CORTEX_M4
intptr_t a, b;
asm("ldrd %0, %1, [%r2, #-8]!" : "=l"(a), "=l"(b), "+l"(p));
#elif JAVELIN_CPU_CORTEX_M0
intptr_t a, b;
asm("sub %2, #8\n\t"
"ldr %0, [%2]\n\t"
"ldr %1, [%2, #4]\n\t"
: "=l"(a), "=l"(b), "+l"(p));
#else
const intptr_t b = Pop();
const intptr_t a = Pop();
#endif
op(a, b);
}
template <typename T> void BinaryOp(T op) {
#if JAVELIN_CPU_CORTEX_M4
intptr_t a, b;
asm("ldrd %0, %1, [%r2, #-8]!" : "=l"(a), "=l"(b), "+l"(p));
#elif JAVELIN_CPU_CORTEX_M0
intptr_t a, b;
asm("sub %2, #8\n\t"
"ldr %0, [%2]\n\t"
"ldr %1, [%2, #4]\n\t"
: "=l"(a), "=l"(b), "+l"(p));
#else
const intptr_t b = Pop();
const intptr_t a = Pop();
#endif
Push(op(a, b));
}
private:
intptr_t *p;
};
class Script::ProgramCounter {
public:
ProgramCounter(const uint8_t *p) : p(p) {}
uint32_t ReadU8() { return *p++; }
void Advance(intptr_t offset) { p += offset; }
uint32_t GetU16() const { return p[0] + (p[1] << 8); }
uint32_t ReadU16() {
#if JAVELIN_CPU_CORTEX_M4
uint32_t r;
asm("ldrh %0, [%1], #2" : "=l"(r), "+l"(p));
return r;
#else
const uint32_t v = p[0] + (p[1] << 8);
p += 2;
return v;
#endif
}
int32_t ReadS16() {
#if JAVELIN_CPU_CORTEX_M4
int32_t r;
asm("ldrsh %0, [%1], #2" : "=l"(r), "+l"(p));
return r;
#else
int32_t value = p[0] + (p[1] << 8);
p += 2;
value <<= 16;
value >>= 16;
return value;
#endif
}
int32_t ReadS24() {
#if JAVELIN_CPU_CORTEX_M4
int32_t r;
asm("ldr %0, [%1], #3\n\t"
"sbfx %0, %0, #0, #24\n\t"
: "=l"(r), "+l"(p));
return r;
#else
int32_t value = p[0] + (p[1] << 8) + (p[2] << 16);
p += 3;
value <<= 8;
value >>= 8;
return value;
#endif
}
int32_t ReadS32() {
#if JAVELIN_CPU_CORTEX_M4
int32_t v;
asm("ldr %0, [%1], #4" : "=l"(v), "+l"(p));
#else
const int32_t v = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
p += 4;
#endif
return v;
}
private:
const uint8_t *p;
};
[[gnu::weak]] void Script::Run(size_t offset) {
using BC = StenoScriptByteCode;
using OP = StenoScriptOperator;
intptr_t *locals;
// The stack point that the code needs to pop to to exit.
intptr_t *base = stackTop;
// The working area for the current function
intptr_t *frame = stackTop;
StackPointer stack(*this);
const uint8_t *const byteCode = (const uint8_t *)this->byteCode;
void (*const *const functionTable)(Script &) = this->functionTable;
ProgramCounter p = byteCode + offset;
#define CONTINUE goto next1;
next1:
uint32_t c = p.ReadU8();
#if JAVELIN_CPU_CORTEX_M4
next2:
// This is the same effect as CONTINUE, but generates better code for M4.
#define CONTINUE2 \
c = p.ReadU8(); \
goto next2;
#else
#define CONTINUE2 goto next1;
#endif
switch (c) {
case BC::PUSH_CONSTANT_START... BC::PUSH_CONSTANT_END:
stack.Push(c);
CONTINUE;
case BC::PUSH_BYTES_1U: {
int value = p.ReadU8();
#if JAVELIN_CPU_CORTEX_M4
asm volatile("cmp %0, #0x3c\n\t"
"it lt\n\t"
"sublt %0, #0x3c\n\t"
: "+l"(value));
#else
if (value < 0x3c) {
value -= 0x3c;
}
#endif
stack.Push(value);
CONTINUE;
}
case BC::PUSH_BYTES_2S:
stack.Push(p.ReadS16());
CONTINUE;
case BC::PUSH_BYTES_3S:
stack.Push(p.ReadS24());
CONTINUE;
case BC::PUSH_BYTES_4:
stack.Push(p.ReadS32());
CONTINUE;
case BC::LOAD_GLOBAL_BEGIN... BC::LOAD_GLOBAL_END:
stack.Push(globals[c - BC::LOAD_GLOBAL_BEGIN]);
CONTINUE;
case BC::LOAD_GLOBAL_VALUE: {
const int globalIndex = p.ReadU8();
stack.Push(globals[globalIndex]);
CONTINUE;
}
case BC::LOAD_GLOBAL_INDEX: {
const intptr_t globalBaseIndex = p.ReadU8();
const intptr_t index = stack.Pop();
stack.Push(globals[globalBaseIndex + index]);
CONTINUE;
}
case BC::STORE_GLOBAL_BEGIN... BC::STORE_GLOBAL_END:
globals[c - BC::STORE_GLOBAL_BEGIN] = stack.Pop();
CONTINUE;
case BC::STORE_GLOBAL_VALUE: {
const int globalIndex = p.ReadU8();
globals[globalIndex] = stack.Pop();
CONTINUE;
}
case BC::STORE_GLOBAL_INDEX: {
const int globalBaseIndex = p.ReadU8();
stack.TwoParam([&, globalBaseIndex](intptr_t index, intptr_t value) {
globals[globalBaseIndex + index] = value;
});
CONTINUE;
}
case BC::LOAD_LOCAL_BEGIN... BC::LOAD_LOCAL_END:
stack.Push(locals[c - BC::LOAD_LOCAL_BEGIN]);
CONTINUE;
case BC::LOAD_LOCAL_VALUE: {
const int localIndex = p.ReadU8();
stack.Push(locals[localIndex]);
CONTINUE;
}
case BC::LOAD_LOCAL_INDEX: {
const int localBaseIndex = p.ReadU8();
const intptr_t index = stack.Pop();
stack.Push(locals[localBaseIndex + index]);
CONTINUE;
}
case BC::STORE_LOCAL_BEGIN... BC::STORE_LOCAL_END:
locals[c - BC::STORE_LOCAL_BEGIN] = stack.Pop();
CONTINUE;
case BC::STORE_LOCAL_VALUE: {
const int localIndex = p.ReadU8();
locals[localIndex] = stack.Pop();
CONTINUE;
}
case BC::STORE_LOCAL_INDEX: {
const int localBaseIndex = p.ReadU8();
stack.TwoParam([=](intptr_t index, intptr_t value) {
locals[localBaseIndex + index] = value;
});
CONTINUE;
}
case BC::OPERATOR_START + (int)OP::NOT:
stack.UnaryOp([](intptr_t a) { return (intptr_t)!a; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::NEGATIVE:
stack.UnaryOp([](intptr_t a) { return -a; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::MULTIPLY:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a * b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::QUOTIENT:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a / b; });
CONTINUE2;
case BC::OPERATOR_START + (int)OP::REMAINDER:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a % b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::ADD:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a + b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::SUBTRACT:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a - b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::EQUALS:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a == b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::NOT_EQUALS:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a != b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::LESS_THAN:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a < b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::GREATER_THAN:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a > b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::LESS_THAN_OR_EQUAL_TO:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a <= b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::GREATER_THAN_OR_EQUAL_TO:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a >= b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::BITWISE_AND:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a & b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::BITWISE_OR:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a | b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::BITWISE_XOR:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a ^ b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::AND:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a && b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::OR:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a || b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::SHIFT_LEFT:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a << b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::ARITHMETIC_SHIFT_RIGHT:
stack.BinaryOp([](intptr_t a, intptr_t b) { return a >> b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::LOGICAL_SHIFT_RIGHT:
stack.BinaryOp([](intptr_t a, intptr_t b) { return uintptr_t(a) >> b; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::BYTE_LOOKUP:
stack.BinaryOp([=](intptr_t offset, intptr_t index) {
const uint8_t *data = byteCode + offset;
return data[index];
});
CONTINUE;
case BC::OPERATOR_START + (int)OP::WORD_LOOKUP:
stack.BinaryOp([=](intptr_t offset, intptr_t index) {
const intptr_t *data = (const intptr_t *)(byteCode + offset);
return data[index];
});
CONTINUE;
case BC::OPERATOR_START + (int)OP::INCREMENT:
stack.UnaryOp([](intptr_t a) { return a + 1; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::DECREMENT:
stack.UnaryOp([](intptr_t a) { return a - 1; });
CONTINUE;
case BC::OPERATOR_START + (int)OP::HALF_WORD_LOOKUP:
stack.BinaryOp([=](intptr_t offset, intptr_t index) {
const uint16_t *data = (const uint16_t *)(byteCode + offset);
return data[index];
});
CONTINUE;
case BC::CALL_INTERNAL: {
stack.WriteBack(*this);
const uint8_t function = p.ReadU8();
(*functionTable[function])(*this);
stack.Load(*this);
CONTINUE;
}
case BC::CALL: {
const size_t offset = p.ReadU16();
stack.WriteBack(*this);
Run(offset);
stack.Load(*this);
CONTINUE;
}
case BC::RETURN:
exit: {
intptr_t *p = base;
if (frame != stack.GetStackTop()) {
*p++ = *frame;
}
stackTop = p;
return;
}
case BC::POP:
stack.Pop();
CONTINUE;
case BC::ENTER_FUNCTION: {
const size_t parameterCount = p.ReadU8();
const size_t localsCount = p.ReadU8();
locals = stack.GetStackTop() - parameterCount;
// This is necessary when a bytecode optimization changes a tail call
// to a jump -- in this case, the base should not be modified.
if (locals < base) {
base = locals;
}
frame = stack.GetStackTop() + localsCount;
stack.SetStackTop(frame);
CONTINUE;
}
case BC::CALL_VALUE: {
const size_t offset = stack.Pop();
if (offset != 0) {
stack.WriteBack(*this);
Run(offset);
stack.Load(*this);
}
CONTINUE;
}
case BC::JUMP_VALUE: {
const size_t offset = stack.Pop();
if (offset == 0) {
goto exit;
}
p = byteCode + offset;
CONTINUE;
}
case BC::RETURN_IF_ZERO:
if (!stack.Pop()) {
goto exit;
}
CONTINUE;
case BC::RETURN_IF_NOT_ZERO:
if (stack.Pop()) {
goto exit;
}
CONTINUE;
case BC::JUMP_SHORT_BEGIN... BC::JUMP_SHORT_END: {
const int offset = c + 1 - BC::JUMP_SHORT_BEGIN;
p.Advance(offset);
CONTINUE;
}
case BC::JUMP_LONG:
p = byteCode + p.GetU16();
CONTINUE;
case BC::JUMP_IF_ZERO_SHORT_BEGIN... BC::JUMP_IF_ZERO_SHORT_END:
if (!stack.Pop()) {
const int offset = c + 1 - BC::JUMP_IF_ZERO_SHORT_BEGIN;
p.Advance(offset);
}
CONTINUE;
case BC::JUMP_IF_ZERO_LONG: {
const size_t offset = p.ReadU16();
if (!stack.Pop()) {
p = byteCode + offset;
}
CONTINUE;
}
case BC::JUMP_IF_NOT_ZERO_SHORT_BEGIN... BC::JUMP_IF_NOT_ZERO_SHORT_END:
if (stack.Pop()) {
const int offset = c + 1 - BC::JUMP_IF_NOT_ZERO_SHORT_BEGIN;
p.Advance(offset);
}
CONTINUE;
case BC::JUMP_IF_NOT_ZERO_LONG: {
const size_t offset = p.ReadU16();
if (stack.Pop()) {
p = byteCode + offset;
}
CONTINUE;
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------