-
Notifications
You must be signed in to change notification settings - Fork 7
/
bytecode.hpp
822 lines (676 loc) · 16.7 KB
/
bytecode.hpp
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/// generates bytecode from AST
#ifndef OGMC_BYTECODE_HPP
#define OGMC_BYTECODE_HPP
#include "BytecodeTable.hpp"
#include "Library.hpp"
#include "ogm/ast/parse.h"
#include "ogm/bytecode/Namespace.hpp"
#include "ogm/asset/AssetTable.hpp"
#include "ogm/asset/Config.hpp"
#include "ogm/common/parallel.hpp"
#include "ogm/common/error.hpp"
#include <vector>
#include <set>
#include <string>
#include <map>
namespace ogm { namespace bytecode {
class Library;
namespace opcode
{
// TODO: replace with macro definition so that string list in dis.cpp stays in sync
enum opcode_t
{
// dsc: loads false.
// imm:
// pop:
// psh: bool
ldi_false,
// dsc: loads false.
// imm:
// pop:
// psh: bool
ldi_true,
// dsc: loads 0.0
// imm:
// pop:
// psh: float
ldi_zero,
// dsc: loads "self" id
// imm:
// pop:
// psh: id
ldi_self,
// dsc: loads "other" id
// imm:
// pop:
// psh: id
ldi_other,
// dsc: loads undefined.
// imm:
// pop:
// psh: undef
ldi_undef,
// dsc: loads a float from immediate
// imm: float32_t
// pop:
// psh: ogm::real
ldi_f32,
// dsc: loads a double from immediate
// imm: float64_t
// pop:
// psh: ogm::real
ldi_f64,
// dsc: loads an int from immediate
// imm: int32_t
// pop:
// psh: ogm::int
ldi_s32,
// dsc: loads an int from immediate
// imm: uint64_t
// pop:
// psh: ogm::int64
ldi_u64,
// dsc: loads a string from immediate
// imm: (length+1):int32_t string(nullterminated)
// pop:
// psh: string
ldi_string,
// dsc: loads a zero-length array.
// imm:
// pop:
// psh: array
ldi_arr,
// dsc: loads an unbound function literal
// imm: bytecode_index_t
// pop:
// psh: function
ldi_fn,
// dsc: loads an empty struct.
// imm:
// pop:
// psh: struct
ldi_struct,
// dsc: sets the current instance's struct-type
// imm:
// pop: function
// psh:
tstruct,
// dsc: increments number
// imm:
// pop: num
// psh: num
inc,
// dsc: decrements number
// imm:
// pop: num
// psh: num
dec,
// dsc: increments local variable
// imm: id32
// pop:
// psh:
incl,
// dsc: decrements local variable
// imm: id32
// pop:
// psh:
decl,
// dsc: sets value in stack array
// imm:
// pop: array index0 {index1} value
// psh: array
seti,
// dsc: adds 2 numbers together
// imm:
// pop: any any
// psh: any
add2,
// dsc: subtracts 2nd from first
// imm:
// pop: num num
// psh: num
sub2,
// dsc: multiplies 2 numbers
// imm:
// pop: num num
// psh: num
mult2,
// dsc: divides 2 numbers (floating point)
// imm:
// pop: num num
// psh: num
fdiv2,
// dsc: divides 2 numbers (integer)
// imm:
// pop: num num
// psh: num
idiv2,
// dsc: computes modulo
// imm:
// pop: num num
// psh: num
mod2,
// dsc: leftshift
// imm:
// pop: num num
// psh: num
lsh2,
// dsc: rightshift
// imm:
// pop: num num
// psh: num
rsh2,
// dsc: >
// imm:
// pop: num num
// psh: bool
gt,
// dsc: <
// imm:
// pop: num num
// psh: bool
lt,
// dsc: >=
// imm:
// pop: num num
// psh: bool
gte,
// dsc: <=
// imm:
// pop: num num
// psh: bool
lte,
// dsc: ==
// imm:
// pop: any any
// psh: bool
eq,
// dsc: !=
// imm:
// pop: any any
// psh: bool
neq,
// dsc: &&
// imm:
// pop: bool bool
// psh: bool
bland,
// dsc: ||
// imm:
// pop: bool bool
// psh: bool
blor,
// dsc: ^^
// imm:
// pop: bool bool
// psh: bool
blxor,
// dsc: &
// imm:
// pop: num num
// psh: num
band,
// dsc: |
// imm:
// pop: num num
// psh: num
bor,
// dsc: ^
// imm:
// pop: num num
// psh: num
bxor,
// dsc: ~
// imm:
// pop: num
// psh: num
bnot,
// dsc: computes condition
// imm:
// pop: any
// psh:
// ofg: C
cond,
// dsc: computes condition (negative)
// imm:
// pop: any
// psh:
// ifg:
// ofg: C
ncond,
// dsc: pushes condition flag as bool
// imm:
// pop:
// psh: bool
// ifg: C
// ofg:
pcond,
// dsc: denotes that store commands should be no-copy
// imm:
// pop:
// psh:
// ofg: X
sfx,
// dsc: denotes that store commands should copy-on-write (default)
// imm:
// pop:
// psh:
// ofg: X
ufx,
// dsc: allocates local variables
// imm: uint32
// pop:
// psh:
all,
// dsc: stores local variable
// imm: id32
// pop: num
// psh:
stl,
// dsc: retrieves local variable
// imm: id32
// pop:
// psh: num
ldl,
// dsc: store instance variable self
// imm: id32
// pop: any
// psh:
sts,
// dsc: store instance variable self
// imm: id32
// pop:
// psh: any
lds,
// dsc: store instance variable other
// imm: id32
// pop: id any
// psh:
sto,
// dsc: store instance variable self
// imm: id32
// pop: id
// psh: any
ldo,
// dsc: store global variable
// imm: id32
// pop: any
// psh:
stg,
// dsc: load global variable
// imm: id32
// pop:
// psh: any
ldg,
// dsc: check if global variable is set
// imm: id32
// pop:
// psh:
// ifg:
// ofg: C
okg,
// dsc: store built-in instance variable
// imm: id32
// pop: any
// psh:
stt,
// dsc: load built-in instance variable
// imm: id32
// pop:
// psh: any
ldt,
// dsc: store built-in instance variable
// imm: id32
// pop: id any
// psh:
stp,
// dsc: load built-in instance variable
// imm: id32
// pop: id
// psh: any
ldp,
// dsc: stores local variable array
// imm: id8
// pop: num {num} any
// psh:
stla,
// dsc: retrieves local variable array
// imm: id8
// pop: num {num}
// psh: num
ldla,
// dsc: store local nested array
// (requires garbage collector)
// imm: id32 nest-depth32
// pop: (num {num})* any
// psh:
stlax,
// dsc: load local nested array
// (requires garbage collector)
// imm: id32 nest-depth32
// pop: (num {num})*
// psh: any
ldlax,
// dsc: store instance variable self array
// imm: id32
// pop: num {num} any
// psh:
stsa,
// dsc: load instance variable self array
// imm: id32
// pop: num {num}
// psh: any
ldsa,
// dsc: store instance variable other array
// imm: id32
// pop: num {num} id any
// psh:
stoa,
// dsc: extract instance variable other array
// imm: id32
// pop: num {num} id
// psh: any
ldoa,
// dsc: store other nested array
// (requires garbage collector)
// imm: id32 nest-depth32
// pop: (num {num})* id any
// psh:
stoax,
// dsc: load other nested array
// (requires garbage collector)
// imm: id32 nest-depth32
// pop: (num {num})* id
// psh: any
ldoax,
// dsc: store global variable array
// imm: id32
// pop: num {num} any
// psh:
stga,
// dsc: load global variable array
// imm: id32
// pop: num {num}
// psh: any
ldga,
// dsc: store built-in instance variable array
// imm: id32
// pop: num {num} id any
// psh:
stpa,
// dsc: load built-in instance variable array
// imm: id32
// pop: num {num} id
// psh: any
ldpa,
// dsc: pops value (ignore it)
// imm:
// pop: any
// psh:
pop,
// dsc: duplicates value
// imm:
// pop: any
// psh: any any
dup,
// dsc: duplicates 2 values from the stack.
// imm:
// pop: any any
// psh: any any any any
dup2,
// dsc: duplicates 3 values from the stack.
// imm:
// pop: any any any
// psh: any any any any any any
dup3,
// dsc: duplicates n values from the stack.
// imm: u8
// pop: any*
// psh: any*
dupn,
// dsc: duplicates 1 value from i positions earlier in the stack.
// imm: u8
// pop: any
// psh: any
dupi,
// dsc: deletes 1 value from i positions earlier in the stack.
// imm: u8
// pop: any
// psh: any
deli,
// dsc: swaps top 2 values on stack.
// imm:
// pop: any any
// psh: any any
swap,
// dsc: call native function
// imm: fptr(32/64) byte (number of arguments)
// pop: any*
// psh: any
nat,
// dsc: obtain 'with' iterator. Pushes current id on stack.
// imm:
// pop: id
// psh: id32 iter32
wti,
// dsc: 'with' yield. Sets condition flag to true when complete and restores id from stack
// If not complete, pushes the iterator id onto the stack and sets the current id.
// imm:
// pop: id32? iter32
// psh: iter32?
// ifg:
// ofg: C
wty,
// dsc: 'with' drop. Frees with iterator prematurely and restores id from stack
// imm:
// pop: id iter32
// psh:
// ifg:
// ofg: C
wtd,
// jumps to given bytecode pointer
// imm: bytecode_address_t
jmp,
// conditionally jumps to given bytecode pointer
// imm: bytecode_address_t
// ifg: C
// ofg:
bcond,
// dsc: calls the bytecode at the given bytecode section index
// imm: bytecode_index_t argc
// pop: any*
// psh: any
call,
// dsc: calls the bytecode which is on the stack
// imm: argc
// pop: any* bytecode_index_t
// psh: any
calls,
// dsc: returns from subroutine
// imm: number of return values
// pop: (up to last all)
// psh:
ret,
// dsc: suspends execution (used by emscripten)
// imm:
// pop:
// psh:
sus,
// dsc: does nothing
// imm:
// pos:
// psh:
nop,
eof
};
extern const char* opcode_string[];
// retrieves opcode string, or "?" for unknown opcodes.
const char* get_opcode_string(opcode_t);
}
static_assert((int)opcode::eof <= 0xff, "Opcode must be 1 byte.");
// TODO: extract these struct definitions into another header.
struct EnumTable;
/**
* the Reflection Accumulator contains information which is needed
* only at compile time and not at runtime, such as the mapping between
* variables and their IDs, or the list of macros.
*
* Unless otherwise specified, it is retained during runtime anyway,
* so that functions like string_execute() can work.
*/
class ReflectionAccumulator
{
public:
Namespace m_namespace_instance;
// globals declared as globalvar.
std::set<std::string> m_bare_globals;
std::map<std::string, ogm_ast_t*> m_ast_macros;
// pointer set in constructor and guaranteed to exist.
EnumTable* const m_enums;
public:
// member functions
ReflectionAccumulator();
~ReflectionAccumulator();
inline bool has_bare_global(const std::string& s) const
{
READ_LOCK(m_mutex_bare_globals)
return m_bare_globals.find(s) != m_bare_globals.end();
}
// checks if macro exists without attaining write lock.
// (by compilation time, macros should be fully set.)
inline bool has_macro_NOMUTEX(const std::string& s) const
{
return m_ast_macros.find(s) != m_ast_macros.end();
}
// compiles and writes macro. (Flags should come from config.m_parse_flags)
// TODO: (should ReflectionAccumulator gain a reference to config...?)
void set_macro(const char* name, const char* value, int flags);
#ifdef PARALLEL_COMPILE
// TODO: make these private.
mutable std::mutex m_mutex_bare_globals;
mutable std::mutex m_mutex_macros;
mutable std::mutex m_mutex_enums;
#endif
};
struct DisassembledBytecodeInstruction
{
DisassembledBytecodeInstruction()
: m_op(opcode::eof)
, m_address(0)
, m_immediate()
{ }
DisassembledBytecodeInstruction(opcode::opcode_t op, size_t address, std::string immediate, ogm_location start = ogm_location())
: m_op(op)
, m_address(address)
, m_immediate(immediate)
{ }
opcode::opcode_t m_op;
size_t m_address;
std::string m_immediate;
};
// describes a block of code that can be compiled into bytecode
// an AST coupled with number of arguments, return values, and optional
// source information (for the debug symbols and error reporting).
struct DecoratedAST
{
ogm_ast_t* m_ast;
uint8_t m_retc;
uint8_t m_argc;
std::string* m_named_args = nullptr; // optional array of length m_argc, or nullptr.
std::string m_name;
std::string m_source;
DecoratedAST(ogm_ast_t* ast, std::string name = "", std::string source = "", uint8_t retc=0, uint8_t argc=0)
: m_ast(ast)
, m_name(std::move(name))
, m_source(std::move(source))
, m_retc(retc)
, m_argc(argc)
{ }
~DecoratedAST()
{
if (m_named_args) delete[] m_named_args;
}
};
// extra information required to compile bytecode within a larger project.
struct ProjectAccumulator
{
const Library* m_library = nullptr;
// accumulates variable indices, globalvar settings, and enums
ReflectionAccumulator* m_reflection = nullptr;
// assets
asset::AssetTable* m_assets = nullptr;
// list of bytecode sections
bytecode::BytecodeTable* m_bytecode = nullptr;
// runtime and project configuration
asset::Config* m_config = nullptr;
//// the following are not used by bytecode_generate but are used in project/ ////
// datafiles
std::string m_included_directory = "";
// project root
std::string m_project_base_directory = "";
// v2 hash id-to-resource-name mapping
std::map<std::string, std::string> m_id_map;
// threadsafe
bytecode_index_t next_bytecode_index()
{
#ifdef PARALLEL_COMPILE
WRITE_LOCK(m_mutex);
#endif
return m_next_bytecode_index++;
}
private:
// next bytecode index
bytecode_index_t m_next_bytecode_index = 0;
#ifdef PARALLEL_COMPILE
std::mutex m_mutex;
#endif
public:
// TODO: most of these members should be created by the constructor, not taken as arguments.
ProjectAccumulator(const Library* library, ReflectionAccumulator* reflection, asset::AssetTable* assets, bytecode::BytecodeTable* bytecode, asset::Config* config=nullptr)
: m_library(library)
, m_reflection(reflection)
, m_assets(assets)
, m_bytecode(bytecode)
, m_config(config)
{
if (reflection)
{
library->reflection_add_instance_variables(*reflection);
}
// null id
m_id_map["00000000-0000-0000-0000-000000000000"] = "";
}
// no copying
ProjectAccumulator(const ProjectAccumulator&)=delete;
// moving is okay
ProjectAccumulator(ProjectAccumulator&&)=default;
};
// configuration options for bytecode generation.
struct GenerateConfig
{
// allow some advanced features
bool m_permit_array_literals = true;
bool m_permit_ternary_literals = true;
// instead of return statements, use suspends.
// (this is used by the debugger.)
bool m_return_is_suspend = false;
// do not allow local variables
// (the debugger needs this)
bool m_no_locals = false;
// this is not usually used
// (but the debugger needs it to associate local variables)
const Namespace* m_existing_locals_namespace = nullptr;
};
// scans te ast to determine the number of arguments and return vales associated with the function.
// some other preprocess steps occur as well.
void bytecode_preprocess(DecoratedAST& in_out_decorated_ast, ReflectionAccumulator& in_out_reflection_accumulator, asset::Config* config);
// compiles bytecode from the given abstract syntax tree.
// if the ast is an ogm_ast_st_imp_body_list, then there must be at most one body in that list.
bytecode_index_t bytecode_generate(const DecoratedAST& in, ProjectAccumulator& accumulator, GenerateConfig* config = nullptr, bytecode_index_t destination_index = k_no_bytecode);
// disassembles bytecode to vector of instructions
void bytecode_dis(bytecode::BytecodeStream inBytecode, std::vector<struct DisassembledBytecodeInstruction>& outInstructions, const Library* library = &defaultLibrary, bool porcelain=false);
// disassembles bytecode to string
void bytecode_dis(bytecode::BytecodeStream inBytecode, std::ostream& outDis, const Library* library = &defaultLibrary, const ReflectionAccumulator* accumulator = nullptr, bool show_source_inline = false, size_t end_pos = std::numeric_limits<size_t>::max());
}
}
#endif