-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpropane_generator.hpp
More file actions
437 lines (375 loc) · 15.6 KB
/
propane_generator.hpp
File metadata and controls
437 lines (375 loc) · 15.6 KB
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
#ifndef _HEADER_PROPANE_GENERATOR
#define _HEADER_PROPANE_GENERATOR
#include "propane_intermediate.hpp"
#include "propane_runtime.hpp"
namespace propane
{
struct address
{
address(uint32_t index, address_type type,
address_modifier modifier = address_modifier::none,
address_prefix prefix = address_prefix::none) :
header(type, prefix, modifier, index),
payload(0) {}
address_header header;
union address_payload
{
address_payload() = default;
address_payload(uint64_t init) : u64(init) {}
int8_t i8;
uint8_t u8;
int16_t i16;
uint16_t u16;
int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
float f32;
double f64;
void* vptr;
name_idx global;
offset_idx field;
offset_t offset;
} payload;
inline bool operator==(const address& other) const noexcept
{
return header == other.header && payload.u64 == other.payload.u64;
}
inline bool operator!=(const address& other) const noexcept
{
return header != other.header || payload.u64 != other.payload.u64;
}
};
struct constant : public address
{
private:
constant(type_idx type) : address(static_cast<uint32_t>(type), address_type::constant) {}
public:
constant(int8_t val) : constant(type_idx::i8) { payload.i8 = val; }
constant(uint8_t val) : constant(type_idx::u8) { payload.u8 = val; }
constant(int16_t val) : constant(type_idx::i16) { payload.i16 = val; }
constant(uint16_t val) : constant(type_idx::u16) { payload.u16 = val; }
constant(int32_t val) : constant(type_idx::i32) { payload.i32 = val; }
constant(uint32_t val) : constant(type_idx::u32) { payload.u32 = val; }
constant(int64_t val) : constant(type_idx::i64) { payload.i64 = val; }
constant(uint64_t val) : constant(type_idx::u64) { payload.u64 = val; }
constant(float val) : constant(type_idx::f32) { payload.f32 = val; }
constant(double val) : constant(type_idx::f64) { payload.f64 = val; }
constant(std::nullptr_t) : constant(type_idx::vptr) { payload.vptr = nullptr; }
constant(name_idx val) : constant(type_idx::voidtype) { payload.global = val; }
inline type_idx type() const noexcept
{
return type_idx(header.index());
}
};
struct invalid_address : public constant
{
public:
invalid_address() : constant(name_idx::invalid) {}
};
struct prefixable_address : public address
{
protected:
prefixable_address(uint32_t index, address_type type) :
address(index, type) {}
public:
address operator*() const
{
address result = *this;
result.header.set_prefix(address_prefix::indirection);
return result;
}
address operator&() const
{
address result = *this;
result.header.set_prefix(address_prefix::address_of);
return result;
}
address operator!() const
{
address result = *this;
result.header.set_prefix(address_prefix::size_of);
return result;
}
};
struct indirect_address : public prefixable_address
{
prefixable_address field(offset_idx field) const
{
prefixable_address result = *this;
result.header.set_modifier(address_modifier::indirect_field);
result.payload.field = field;
return result;
}
};
struct modifyable_address : public prefixable_address
{
protected:
modifyable_address(uint32_t index, address_type type) :
prefixable_address(index, type) {}
public:
prefixable_address field(offset_idx field) const
{
prefixable_address result = *this;
result.header.set_modifier(address_modifier::direct_field);
result.payload.field = field;
return result;
}
const indirect_address* operator->() const
{
return reinterpret_cast<const indirect_address*>(this);
}
prefixable_address operator[](offset_t offset) const
{
prefixable_address result = *this;
result.header.set_modifier(address_modifier::offset);
result.payload.offset = offset;
return result;
}
};
struct stack : public modifyable_address
{
stack(uint32_t index) :
modifyable_address(index, address_type::stackvar) {}
inline uint32_t index() const noexcept
{
return header.index();
}
};
struct param : public modifyable_address
{
param(uint32_t index) :
modifyable_address(index, address_type::parameter) {}
inline uint32_t index() const noexcept
{
return header.index();
}
};
struct retval : public modifyable_address
{
retval() :
modifyable_address(address_header_constants::index_max, address_type::stackvar) {}
};
struct global : public modifyable_address
{
global(name_idx name) :
modifyable_address(static_cast<uint32_t>(name), address_type::global) {}
inline name_idx name() const noexcept
{
return name_idx(header.index());
}
};
// Experimental Propane bytecode generator
// Inherit from this to implement a parser
class generator : public handle<class generator_impl, sizeof(size_t) * 128>
{
public:
generator();
// String name of the file (will be included in type/method metadata)
generator(std::string_view name);
~generator();
generator(const generator&) = delete;
generator& operator=(const generator&) = delete;
class type_writer final : public handle<class type_writer_impl, sizeof(size_t) * 20>
{
public:
type_writer(const type_writer&) = delete;
type_writer& operator=(const type_writer&) = delete;
name_idx name() const;
type_idx index() const;
// Field declaration for structs
void declare_field(type_idx type, name_idx name);
name_idx declare_field(type_idx type, std::string_view name);
span<const field> fields() const;
// Finalize
void finalize();
private:
friend class generator_impl;
friend class generator;
friend class type_writer_impl;
type_writer(class generator_impl&, name_idx, type_idx, bool);
~type_writer();
file_meta get_meta() const;
};
class method_writer final : public handle<class method_writer_impl, sizeof(size_t) * 128>
{
public:
method_writer(const method_writer&) = delete;
method_writer& operator=(const method_writer&) = delete;
name_idx name() const;
method_idx index() const;
// Variable stack
void push(span<const type_idx> types);
inline void push(std::initializer_list<type_idx> types)
{
push(init_span(types));
}
inline void push(type_idx type)
{
push(init_span(type));
}
span<const stackvar> stack() const;
// Declare label for later use (name is optional)
label_idx declare_label(std::string_view label_name);
label_idx declare_label();
// Write label (this should be called only once per label)
void write_label(label_idx label);
// Instruction writer methods
void write_noop();
void write_set(address lhs, address rhs);
void write_conv(address lhs, address rhs);
void write_not(address lhs);
void write_neg(address lhs);
void write_mul(address lhs, address rhs);
void write_div(address lhs, address rhs);
void write_mod(address lhs, address rhs);
void write_add(address lhs, address rhs);
void write_sub(address lhs, address rhs);
void write_lsh(address lhs, address rhs);
void write_rsh(address lhs, address rhs);
void write_and(address lhs, address rhs);
void write_xor(address lhs, address rhs);
void write_or(address lhs, address rhs);
void write_padd(address lhs, address rhs);
void write_psub(address lhs, address rhs);
void write_pdif(address lhs, address rhs);
void write_cmp(address lhs, address rhs);
void write_ceq(address lhs, address rhs);
void write_cne(address lhs, address rhs);
void write_cgt(address lhs, address rhs);
void write_cge(address lhs, address rhs);
void write_clt(address lhs, address rhs);
void write_cle(address lhs, address rhs);
void write_cze(address lhs);
void write_cnz(address lhs);
void write_br(label_idx label);
void write_beq(label_idx label, address lhs, address rhs);
void write_bne(label_idx label, address lhs, address rhs);
void write_bgt(label_idx label, address lhs, address rhs);
void write_bge(label_idx label, address lhs, address rhs);
void write_blt(label_idx label, address lhs, address rhs);
void write_ble(label_idx label, address lhs, address rhs);
void write_bze(label_idx label, address lhs);
void write_bnz(label_idx label, address lhs);
void write_sw(address addr, span<const label_idx> switch_labels);
inline void write_sw(address addr, std::initializer_list<label_idx> switch_labels)
{
write_sw(addr, init_span(switch_labels));
}
void write_call(method_idx method, span<const address> args = span<const address>());
inline void write_call(method_idx method, std::initializer_list<address> args)
{
write_call(method, init_span(args));
}
void write_callv(address addr, span<const address> args = span<const address>());
inline void write_callv(address addr, std::initializer_list<address> args)
{
write_callv(addr, init_span(args));
}
void write_ret();
void write_retv(address addr);
void write_dump(address addr);
// Finalize
void finalize();
private:
friend class generator_impl;
friend class generator;
friend class method_writer_impl;
method_writer(class generator_impl&, name_idx, method_idx, signature_idx);
~method_writer();
file_meta get_meta() const;
};
// Declare a unique identifier. If 'name' has already been used,
// this method will return the same index
name_idx make_identifier(std::string_view name);
// Declare signatures
// Signatures can be used for method declaration or signature type declaration
signature_idx make_signature(type_idx return_type, span<const type_idx> parameter_types = span<const type_idx>());
inline signature_idx make_signature(type_idx return_type, std::initializer_list<type_idx> parameter_types)
{
return make_signature(return_type, init_span(parameter_types));
}
// Offsets
// Data views into structs relative to the root type.
// Modifying field values is only possible through offsets.
offset_idx make_offset(type_idx type, span<const name_idx> fields);
inline offset_idx make_offset(type_idx type, std::initializer_list<name_idx> fields)
{
return make_offset(type, init_span(fields));
}
inline offset_idx make_offset(type_idx type, name_idx field)
{
return make_offset(type, init_span(field));
}
// Append more fields to existing offsets (creates a new offset)
offset_idx append_offset(offset_idx offset, span<const name_idx> fields);
inline offset_idx append_offset(offset_idx offset, std::initializer_list<name_idx> fields)
{
return append_offset(offset, init_span(fields));
}
inline offset_idx append_offset(offset_idx offset, name_idx field)
{
return append_offset(offset, init_span(field));
}
// Global and constant definition
void define_global(name_idx name, bool is_constant, type_idx type, span<const constant> values = span<const constant>());
inline void define_global(name_idx name, bool is_constant, type_idx type, std::initializer_list<constant> values)
{
define_global(name, is_constant, type, init_span(values));
}
inline name_idx define_global(std::string_view name, bool is_constant, type_idx type, span<const constant> values = span<const constant>())
{
const name_idx id = make_identifier(name);
define_global(id, is_constant, type, values);
return id;
}
inline name_idx define_global(std::string_view name, bool is_constant, type_idx type, std::initializer_list<constant> values)
{
const name_idx id = make_identifier(name);
define_global(id, is_constant, type, init_span(values));
return id;
}
// Type declaration
// Can be called multiple times, but will always
// return the same index for identifier 'name'
type_idx declare_type(name_idx name);
inline type_idx declare_type(std::string_view name)
{
return declare_type(make_identifier(name));
}
// Define type can only be called once
type_writer& define_type(type_idx type, bool is_union = false);
inline type_writer& define_type(std::string_view name, bool is_union = false)
{
return define_type(declare_type(make_identifier(name)), is_union);
}
// Creation of generated types
type_idx declare_pointer_type(type_idx base_type);
type_idx declare_array_type(type_idx base_type, size_t array_size);
type_idx declare_signature_type(signature_idx signature);
// Method declaration
// Can be called multiple times, but will always
// return the same index for identifier 'name'
method_idx declare_method(name_idx name);
inline method_idx declare_method(std::string_view name)
{
return declare_method(make_identifier(name));
}
// Define method can only be called once
method_writer& define_method(method_idx method, signature_idx signature);
inline method_writer& define_method(std::string_view name, signature_idx signature)
{
return define_method(declare_method(make_identifier(name)), signature);
}
// Set a line number to be included in type/method metadata
void set_line_number(uint32_t line_number) noexcept;
// Finalize
// This method finishes up all the writers and releases all the resources.
// The returned intermediate can be merged with other intermediates or linked and executed.
intermediate finalize();
file_meta get_meta() const;
private:
friend class generator_impl;
};
}
#endif