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
//! Writing byte code: the code stream, and the writer that every
//! instruction goes through.
//!
//! The instruction writer does the reference compiler's peephole
//! optimizations, in the reference's order, because they decide the
//! exact bytes that come out: a comparison followed by a conditional jump
//! becomes a compare-and-jump, pushing a local and then getting a
//! property becomes one instruction that does both, and so on. Each rule
//! looks only at the last instruction or two written.
use hexicon_bytecode::Opcode as Op;
/// A place in the code that jumps go to. It may not be known yet where
/// it is when a jump to it is written.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Label(usize);
#[derive(Debug, Default)]
struct LabelInfo {
/// Where the label is, once it has been placed.
pos: Option<usize>,
/// The jump operands waiting to be filled in when it is.
fixups: Vec<usize>,
}
/// The code being generated, and what is known about the last
/// instructions written to it.
#[derive(Debug)]
pub(crate) struct CodeGen {
/// The byte code.
pub code: Vec<u8>,
last_op: u8,
second_last_op: u8,
labels: Vec<LabelInfo>,
/// How many values are on the stack at this point, as far as the
/// compiler can tell, and the most there have been in this method.
sp_depth: i32,
max_sp_depth: i32,
}
const NOP: u8 = Op::Nop as u8;
impl CodeGen {
pub(crate) fn new() -> Self {
Self {
code: Vec::new(),
last_op: NOP,
second_last_op: NOP,
labels: Vec::new(),
sp_depth: 0,
max_sp_depth: 0,
}
}
/// Where the next byte goes.
pub(crate) fn ofs(&self) -> usize {
self.code.len()
}
pub(crate) fn write(&mut self, byte: u8) {
self.code.push(byte);
}
pub(crate) fn write2(&mut self, value: u16) {
self.code.extend_from_slice(&value.to_le_bytes());
}
pub(crate) fn write4(&mut self, value: u32) {
self.code.extend_from_slice(&value.to_le_bytes());
}
pub(crate) fn write2_at(&mut self, at: usize, value: u16) {
self.code[at..at + 2].copy_from_slice(&value.to_le_bytes());
}
pub(crate) fn write4_at(&mut self, at: usize, value: u32) {
self.code[at..at + 4].copy_from_slice(&value.to_le_bytes());
}
fn byte_at(&self, at: usize) -> u8 {
self.code[at]
}
fn dec_ofs(&mut self, by: usize) {
self.code.truncate(self.code.len() - by);
}
// ---- the stack ----
pub(crate) fn note_push(&mut self) {
self.note_pushes(1);
}
pub(crate) fn note_pushes(&mut self, count: i32) {
self.sp_depth += count;
self.max_sp_depth = self.max_sp_depth.max(self.sp_depth);
}
pub(crate) fn note_pop(&mut self) {
self.sp_depth -= 1;
}
pub(crate) fn note_pops(&mut self, count: i32) {
self.sp_depth -= count;
}
pub(crate) fn sp_depth(&self) -> i32 {
self.sp_depth
}
pub(crate) fn max_sp_depth(&self) -> i32 {
self.max_sp_depth
}
/// Start a new method: nothing on the stack, and no instruction
/// before the first.
pub(crate) fn start_method(&mut self) {
self.sp_depth = 0;
self.max_sp_depth = 0;
self.clear_peephole();
}
/// Forget the last instructions, so that the next one is not
/// combined with them. This is needed wherever a jump can arrive.
pub(crate) fn clear_peephole(&mut self) {
self.last_op = NOP;
self.second_last_op = NOP;
}
/// Whether the next instruction can never be reached, because the
/// last one always goes somewhere else.
pub(crate) fn can_skip_op(&self) -> bool {
[
Op::Ret,
Op::RetVal,
Op::RetTrue,
Op::RetNil,
Op::Throw,
Op::Jmp,
Op::LRet,
]
.iter()
.any(|&op| op as u8 == self.last_op)
}
/// The last instruction written, as the peephole rules left it.
pub(crate) fn last_op(&self) -> u8 {
self.last_op
}
// ---- labels ----
pub(crate) fn new_label(&mut self) -> Label {
self.labels.push(LabelInfo::default());
Label(self.labels.len() - 1)
}
/// A label at the current position.
pub(crate) fn new_label_here(&mut self) -> Label {
let label = self.new_label();
self.def_label(label);
label
}
/// Put a label at the current position. Jumps can arrive here, so
/// what came before is no guide to what the next instruction follows.
pub(crate) fn def_label(&mut self, label: Label) {
// A jump to the very next instruction does nothing: if the last
// instruction is a jump to this label, take it back out.
if self.last_op == Op::Jmp as u8 && self.ofs() >= 3 {
let operand = self.ofs() - 2;
let fixups = &mut self.labels[label.0].fixups;
if let Some(index) = fixups.iter().position(|&at| at == operand) {
fixups.remove(index);
self.dec_ofs(3);
}
}
let pos = self.ofs();
let info = &mut self.labels[label.0];
info.pos = Some(pos);
for at in std::mem::take(&mut info.fixups) {
let offset = pos as i64 - at as i64;
self.code[at..at + 2].copy_from_slice(&(offset as i16).to_le_bytes());
}
self.clear_peephole();
}
/// How many jumps have been written to labels that were never
/// placed. They are forgotten, so that each is counted once.
pub(crate) fn take_unresolved(&mut self) -> usize {
self.labels
.iter_mut()
.filter(|label| label.pos.is_none())
.map(|label| std::mem::take(&mut label.fixups).len())
.sum()
}
/// Write a jump's operand: the distance from the operand to the
/// label.
pub(crate) fn write_label_ofs(&mut self, label: Label) {
let at = self.ofs();
match self.labels[label.0].pos {
Some(pos) => {
let offset = pos as i64 - at as i64;
self.write2(offset as i16 as u16);
}
None => {
self.labels[label.0].fixups.push(at);
self.write2(0);
}
}
}
/// Write a jump instruction to a label.
pub(crate) fn write_jump(&mut self, op: Op, label: Label) {
self.write_op(op);
self.write_label_ofs(label);
}
// ---- instructions ----
/// Write an instruction's opcode, combining it with the instruction
/// before where the two together have a shorter form.
/// Write a call to a property of the value on the stack, but for
/// the property's number, which the caller writes next. If the
/// value was just pushed from a local, the push and the call are
/// one instruction.
pub(crate) fn write_callprop(&mut self, argc: usize, varargs: bool) {
let last = self.last_op;
let short = (Op::GetLclN0 as u8..=Op::GetLclN5 as u8).contains(&last);
if last == Op::GetLcl1 as u8 || short {
let local = if short {
self.dec_ofs(1);
last - Op::GetLclN0 as u8
} else {
let local = self.code[self.ofs() - 1];
self.dec_ofs(2);
local
};
self.last_op = self.second_last_op;
self.second_last_op = NOP;
if varargs {
self.write_op(Op::VarArgc);
}
self.write_op(Op::CallPropLcl1);
self.write(argc as u8);
self.write(local);
} else {
if varargs {
self.write_op(Op::VarArgc);
}
self.write_op(Op::CallProp);
self.write(argc as u8);
}
self.note_pops(argc as i32 + 1);
}
pub(crate) fn write_op(&mut self, op: Op) {
let mut opc = op as u8;
self.write(opc);
let mut op_len = 1usize;
let mut prv_len = 1usize;
let is = |byte: u8, op: Op| byte == op as u8;
let lcln = |byte: u8| (Op::GetLclN0 as u8..=Op::GetLclN5 as u8).contains(&byte);
loop {
let last = self.last_op;
let combined: Option<u8> = match Op::from_byte(opc) {
Some(Op::Dup) if is(last, Op::GetR0) => Some(Op::DupR0 as u8),
Some(Op::Disc) if is(last, Op::Disc) => {
// Two discards are one that discards two.
op_len = 0;
prv_len = 2;
let at = self.ofs() - 1;
self.code[at] = 2;
Some(Op::Disc1 as u8)
}
Some(Op::Disc1) if is(last, Op::Disc) && self.byte_at(self.ofs() - 2) != 0xff => {
let at = self.ofs() - 2;
self.code[at] += 1;
prv_len = 2;
Some(Op::Disc1 as u8)
}
Some(Op::Jf) => match Op::from_byte(last) {
Some(Op::Not) => Some(Op::Jt as u8),
Some(Op::Eq) => Some(Op::Jne as u8),
Some(Op::Ne) => Some(Op::Je as u8),
Some(Op::Lt) => Some(Op::Jge as u8),
Some(Op::Le) => Some(Op::Jgt as u8),
Some(Op::Gt) => Some(Op::Jle as u8),
Some(Op::Ge) => Some(Op::Jlt as u8),
Some(Op::GetR0) => Some(Op::JR0F as u8),
_ => None,
},
Some(Op::Je) if is(last, Op::PushNil) => Some(Op::JNil as u8),
Some(Op::Jne) if is(last, Op::PushNil) => Some(Op::JNotNil as u8),
Some(Op::Jt) => match Op::from_byte(last) {
Some(Op::Not) => Some(Op::Jf as u8),
Some(Op::Eq) => Some(Op::Je as u8),
Some(Op::Ne) => Some(Op::Jne as u8),
Some(Op::Lt) => Some(Op::Jlt as u8),
Some(Op::Le) => Some(Op::Jle as u8),
Some(Op::Gt) => Some(Op::Jgt as u8),
Some(Op::Ge) => Some(Op::Jge as u8),
Some(Op::GetR0) => Some(Op::JR0T as u8),
_ => None,
},
Some(Op::Not) => match Op::from_byte(last) {
Some(Op::Eq) => Some(Op::Ne as u8),
Some(Op::Ne) => Some(Op::Eq as u8),
Some(Op::Gt) => Some(Op::Le as u8),
Some(Op::Ge) => Some(Op::Lt as u8),
Some(Op::Lt) => Some(Op::Ge as u8),
Some(Op::Le) => Some(Op::Gt as u8),
Some(Op::Boolize) => Some(Op::Not as u8),
Some(Op::Not) => Some(Op::Boolize as u8),
_ => None,
},
// A return right after a return is left as it is and not
// looked at again. The reference stops here with the
// opcode already written, so the second return stays in
// the code; it is never reached.
Some(Op::Ret)
if is(last, Op::Ret)
|| is(last, Op::RetVal)
|| is(last, Op::RetNil)
|| is(last, Op::RetTrue) =>
{
return;
}
Some(Op::RetNil) if is(last, Op::RetNil) => return,
Some(Op::RetTrue) if is(last, Op::RetTrue) => return,
Some(Op::RetVal) => match Op::from_byte(last) {
Some(Op::GetR0) => Some(Op::Ret as u8),
Some(Op::PushNil) => Some(Op::RetNil as u8),
Some(Op::PushTrue) => Some(Op::RetTrue as u8),
_ => None,
},
Some(Op::SetLcl1) => match Op::from_byte(last) {
Some(Op::GetR0) => Some(Op::SetLcl1R0 as u8),
Some(Op::Dup) => Some(Op::GetSetLcl1 as u8),
Some(Op::DupR0) => Some(Op::GetSetLcl1R0 as u8),
_ => None,
},
Some(Op::GetProp) => {
if is(last, Op::GetLcl1) {
prv_len = 2;
Some(Op::GetPropLcl1 as u8)
} else if lcln(last) {
// The short form of the push has no operand, so
// make room for the local's number.
self.write(0);
let at = self.ofs() - 2;
self.code[at] = last - Op::GetLclN0 as u8;
prv_len = 2;
Some(Op::GetPropLcl1 as u8)
} else if is(last, Op::GetR0) {
Some(Op::GetPropR0 as u8)
} else {
None
}
}
Some(Op::CallProp) if is(last, Op::GetR0) => Some(Op::CallPropR0 as u8),
Some(Op::Index) => {
if is(last, Op::Push0) || is(last, Op::Push1) {
let at = self.ofs() - 1;
self.code[at] = u8::from(is(last, Op::Push1));
self.write(0);
prv_len = 2;
Some(Op::IdxInt8 as u8)
} else if is(last, Op::PushInt8) {
prv_len = 2;
Some(Op::IdxInt8 as u8)
} else {
None
}
}
Some(Op::IdxInt8) => {
if is(last, Op::GetLcl1) {
let index = self.byte_at(self.ofs() - op_len + 1);
let at = self.ofs() - op_len;
self.code[at] = index;
op_len -= 1;
prv_len = 3;
Some(Op::IdxLcl1Int8 as u8)
} else if lcln(last) {
let at = self.ofs() - op_len;
self.code[at] = last - Op::GetLclN0 as u8;
if op_len == 1 {
self.write(0);
}
op_len = 0;
prv_len = 3;
Some(Op::IdxLcl1Int8 as u8)
} else {
None
}
}
Some(Op::SetInd) => self.combine_setind(&mut op_len, &mut prv_len),
_ => None,
};
match combined {
Some(new_opc) => {
// Take the new instruction back out, and put the
// combined one where the one before it was.
opc = new_opc;
self.dec_ofs(op_len);
let at = self.ofs() - prv_len;
self.code[at] = opc;
self.last_op = self.second_last_op;
self.second_last_op = NOP;
op_len = prv_len;
prv_len = 1;
}
None => break,
}
}
self.second_last_op = self.last_op;
self.last_op = opc;
}
/// Assigning to an element of a local, where the index is a small
/// constant: `lcl[i] = value` as one instruction.
fn combine_setind(&mut self, op_len: &mut usize, prv_len: &mut usize) -> Option<u8> {
let second = self.second_last_op;
let last = self.last_op;
let short_local = (Op::GetLclN0 as u8..=Op::GetLclN5 as u8).contains(&second);
if second != Op::GetLcl1 as u8 && !short_local {
return None;
}
let getlcl_len = if short_local { 1 } else { 2 };
let ofs = self.ofs();
let (index, push_len): (i64, usize) = match Op::from_byte(last) {
Some(Op::PushInt8) => {
let index = i64::from(self.byte_at(ofs - 2));
if index >= 127 {
return None;
}
(index, 2)
}
Some(Op::PushInt) => {
let bytes = [
self.byte_at(ofs - 5),
self.byte_at(ofs - 4),
self.byte_at(ofs - 3),
self.byte_at(ofs - 2),
];
let index = i64::from(i32::from_le_bytes(bytes));
if !(0..=255).contains(&index) {
return None;
}
(index, 5)
}
Some(Op::Push0) => (0, 1),
Some(Op::Push1) => (1, 1),
_ => return None,
};
let local = if short_local {
second - Op::GetLclN0 as u8
} else {
self.byte_at(ofs - push_len - 2)
};
let at = ofs - getlcl_len - push_len;
self.code[at] = local;
self.code[at + 1] = index as u8;
self.second_last_op = NOP;
*op_len = getlcl_len + push_len - 2;
*prv_len = 3;
Some(Op::SetIndLcl1I8 as u8)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn written(build: impl FnOnce(&mut CodeGen)) -> Vec<u8> {
let mut cg = CodeGen::new();
build(&mut cg);
cg.code
}
#[test]
fn a_comparison_and_a_jump_become_one_instruction() {
let code = written(|cg| {
let label = cg.new_label();
cg.write_op(Op::Gt);
cg.write_jump(Op::Jf, label);
cg.def_label(label);
});
assert_eq!(code, [Op::Jle as u8, 2, 0]);
}
#[test]
fn negations_cancel() {
let code = written(|cg| {
cg.write_op(Op::Eq);
cg.write_op(Op::Not);
cg.write_op(Op::Not);
});
assert_eq!(code, [Op::Eq as u8]);
}
#[test]
fn returns() {
// Returning nil is one instruction, and a second return after it
// is written but changes nothing.
assert_eq!(
written(|cg| {
cg.write_op(Op::PushNil);
cg.write_op(Op::RetVal);
cg.write_op(Op::RetNil);
}),
[Op::RetNil as u8, Op::RetNil as u8]
);
assert_eq!(
written(|cg| {
cg.write_op(Op::GetR0);
cg.write_op(Op::RetVal);
}),
[Op::Ret as u8]
);
}
#[test]
fn two_discards_become_one() {
// Only in pairs: a third is not added to the two before it.
let code = written(|cg| {
cg.write_op(Op::Disc);
cg.write_op(Op::Disc);
cg.write_op(Op::Disc);
});
assert_eq!(code, [Op::Disc1 as u8, 2, Op::Disc as u8]);
}
#[test]
fn a_local_and_what_is_done_with_it_become_one_instruction() {
// local.prop
let code = written(|cg| {
cg.write_op(Op::GetLclN2);
cg.write_op(Op::GetProp);
cg.write2(0x1234);
});
assert_eq!(code, [Op::GetPropLcl1 as u8, 2, 0x34, 0x12]);
// local[3]
let code = written(|cg| {
cg.write_op(Op::GetLclN1);
cg.write_op(Op::PushInt8);
cg.write(3);
cg.write_op(Op::Index);
});
assert_eq!(code, [Op::IdxLcl1Int8 as u8, 1, 3]);
// local[1] = value
let code = written(|cg| {
cg.write_op(Op::GetLcl1);
cg.write(9);
cg.write_op(Op::Push1);
cg.write_op(Op::SetInd);
});
assert_eq!(code, [Op::SetIndLcl1I8 as u8, 9, 1]);
}
}