2022-09-04 17:53:17 +02:00
|
|
|
use std::io::Write;
|
2022-10-16 01:10:10 +02:00
|
|
|
use std::ops::{Index, IndexMut};
|
2022-09-04 15:49:42 +02:00
|
|
|
|
2022-08-07 22:41:07 +02:00
|
|
|
use crate::opcode::{CodeIdx, ConstantIdx, OpCode};
|
|
|
|
use crate::value::Value;
|
2022-10-04 16:05:34 +02:00
|
|
|
use crate::SourceCode;
|
2022-08-07 22:41:07 +02:00
|
|
|
|
2022-09-01 15:18:02 +02:00
|
|
|
/// Represents a source location from which one or more operations
|
|
|
|
/// were compiled.
|
|
|
|
///
|
2023-09-21 23:57:20 +02:00
|
|
|
/// The span itself is an index into a [codemap::CodeMap], and the
|
2022-09-01 15:18:02 +02:00
|
|
|
/// structure tracks the number of operations that were yielded from
|
|
|
|
/// the same span.
|
|
|
|
///
|
|
|
|
/// At error reporting time, it becomes possible to either just fetch
|
|
|
|
/// the textual representation of that span from the codemap, or to
|
|
|
|
/// even re-parse the AST using rnix to create more semantically
|
|
|
|
/// interesting errors.
|
2022-09-18 20:04:40 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2022-09-01 15:18:02 +02:00
|
|
|
struct SourceSpan {
|
2023-09-21 23:57:20 +02:00
|
|
|
/// Span into the [codemap::CodeMap].
|
2022-09-01 15:18:02 +02:00
|
|
|
span: codemap::Span,
|
|
|
|
|
2023-03-31 00:19:01 +02:00
|
|
|
/// Index of the first operation covered by this span.
|
|
|
|
start: usize,
|
2022-09-01 15:18:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// A chunk is a representation of a sequence of bytecode
|
|
|
|
/// instructions, associated constants and additional metadata as
|
|
|
|
/// emitted by the compiler.
|
2022-11-01 01:13:38 +01:00
|
|
|
#[derive(Debug, Default)]
|
2022-08-07 22:41:07 +02:00
|
|
|
pub struct Chunk {
|
|
|
|
pub code: Vec<OpCode>,
|
2022-08-13 21:14:53 +02:00
|
|
|
pub constants: Vec<Value>,
|
2022-09-01 15:18:02 +02:00
|
|
|
spans: Vec<SourceSpan>,
|
2022-08-07 22:41:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 15:49:42 +02:00
|
|
|
impl Index<ConstantIdx> for Chunk {
|
|
|
|
type Output = Value;
|
|
|
|
|
|
|
|
fn index(&self, index: ConstantIdx) -> &Self::Output {
|
|
|
|
&self.constants[index.0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<CodeIdx> for Chunk {
|
|
|
|
type Output = OpCode;
|
|
|
|
|
|
|
|
fn index(&self, index: CodeIdx) -> &Self::Output {
|
|
|
|
&self.code[index.0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 01:10:10 +02:00
|
|
|
impl IndexMut<CodeIdx> for Chunk {
|
|
|
|
fn index_mut(&mut self, index: CodeIdx) -> &mut Self::Output {
|
|
|
|
&mut self.code[index.0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:41:07 +02:00
|
|
|
impl Chunk {
|
2022-09-01 15:54:30 +02:00
|
|
|
pub fn push_op(&mut self, data: OpCode, span: codemap::Span) -> CodeIdx {
|
|
|
|
let idx = self.code.len();
|
|
|
|
self.code.push(data);
|
2023-03-31 00:19:01 +02:00
|
|
|
self.push_span(span, idx);
|
2022-09-01 15:54:30 +02:00
|
|
|
CodeIdx(idx)
|
|
|
|
}
|
|
|
|
|
2023-03-12 22:56:03 +01:00
|
|
|
/// Get the first span of a chunk, no questions asked.
|
|
|
|
pub fn first_span(&self) -> codemap::Span {
|
|
|
|
self.spans[0].span
|
|
|
|
}
|
|
|
|
|
2022-09-09 09:34:29 +02:00
|
|
|
/// Pop the last operation from the chunk and clean up its tracked
|
|
|
|
/// span. Used when the compiler backtracks.
|
|
|
|
pub fn pop_op(&mut self) {
|
|
|
|
// Simply drop the last op.
|
|
|
|
self.code.pop();
|
|
|
|
|
2023-03-31 00:19:01 +02:00
|
|
|
if let Some(span) = self.spans.last() {
|
|
|
|
// If the last span started at this op, drop it.
|
|
|
|
if span.start == self.code.len() {
|
2022-09-09 09:34:29 +02:00
|
|
|
self.spans.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 16:26:45 +02:00
|
|
|
pub fn push_constant(&mut self, data: Value) -> ConstantIdx {
|
2022-08-07 22:41:07 +02:00
|
|
|
let idx = self.constants.len();
|
|
|
|
self.constants.push(data);
|
|
|
|
ConstantIdx(idx)
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:18:02 +02:00
|
|
|
// Span tracking implementation
|
|
|
|
|
2023-03-31 00:19:01 +02:00
|
|
|
fn push_span(&mut self, span: codemap::Span, start: usize) {
|
2022-09-01 15:18:02 +02:00
|
|
|
match self.spans.last_mut() {
|
|
|
|
// We do not need to insert the same span again, as this
|
|
|
|
// instruction was compiled from the same span as the last
|
|
|
|
// one.
|
2023-03-31 00:19:01 +02:00
|
|
|
Some(last) if last.span == span => {}
|
2022-09-01 15:18:02 +02:00
|
|
|
|
|
|
|
// In all other cases, this is a new source span.
|
2023-03-31 00:19:01 +02:00
|
|
|
_ => self.spans.push(SourceSpan { span, start }),
|
2022-09-01 15:18:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the [codemap::Span] from which the instruction at
|
|
|
|
/// `offset` was compiled.
|
|
|
|
pub fn get_span(&self, offset: CodeIdx) -> codemap::Span {
|
2023-03-31 00:19:01 +02:00
|
|
|
let position = self
|
|
|
|
.spans
|
|
|
|
.binary_search_by(|span| span.start.cmp(&offset.0));
|
|
|
|
|
|
|
|
let span = match position {
|
|
|
|
Ok(index) => &self.spans[index],
|
|
|
|
Err(index) => {
|
|
|
|
if index == 0 {
|
|
|
|
&self.spans[0]
|
|
|
|
} else {
|
|
|
|
&self.spans[index - 1]
|
|
|
|
}
|
2022-09-01 15:18:02 +02:00
|
|
|
}
|
2023-03-31 00:19:01 +02:00
|
|
|
};
|
2022-09-01 15:18:02 +02:00
|
|
|
|
2023-03-31 00:19:01 +02:00
|
|
|
span.span
|
2022-09-01 15:18:02 +02:00
|
|
|
}
|
2022-09-03 13:50:50 +02:00
|
|
|
|
2022-09-04 17:53:17 +02:00
|
|
|
/// Write the disassembler representation of the operation at
|
|
|
|
/// `idx` to the specified writer.
|
|
|
|
pub fn disassemble_op<W: Write>(
|
|
|
|
&self,
|
|
|
|
writer: &mut W,
|
2022-10-04 16:05:34 +02:00
|
|
|
source: &SourceCode,
|
2022-09-04 17:53:17 +02:00
|
|
|
width: usize,
|
|
|
|
idx: CodeIdx,
|
|
|
|
) -> Result<(), std::io::Error> {
|
|
|
|
write!(writer, "{:#width$x}\t ", idx.0, width = width)?;
|
|
|
|
|
|
|
|
// Print continuation character if the previous operation was at
|
|
|
|
// the same line, otherwise print the line.
|
2022-10-04 16:05:34 +02:00
|
|
|
let line = source.get_line(self.get_span(idx));
|
|
|
|
if idx.0 > 0 && source.get_line(self.get_span(CodeIdx(idx.0 - 1))) == line {
|
2022-09-04 17:53:17 +02:00
|
|
|
write!(writer, " |\t")?;
|
|
|
|
} else {
|
|
|
|
write!(writer, "{:4}\t", line)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self[idx] {
|
2023-02-22 12:18:09 +01:00
|
|
|
OpCode::OpConstant(idx) => {
|
|
|
|
let val_str = match &self[idx] {
|
|
|
|
Value::Thunk(t) => t.debug_repr(),
|
|
|
|
Value::Closure(c) => format!("closure({:p})", c.lambda),
|
|
|
|
val => format!("{}", val),
|
|
|
|
};
|
|
|
|
|
|
|
|
writeln!(writer, "OpConstant({}@{})", val_str, idx.0)
|
|
|
|
}
|
2022-09-04 17:53:17 +02:00
|
|
|
op => writeln!(writer, "{:?}", op),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-29 22:35:59 +01:00
|
|
|
|
|
|
|
/// Extend this chunk with the content of another, moving out of the other
|
|
|
|
/// in the process.
|
|
|
|
///
|
|
|
|
/// This is used by the compiler when it detects that it unnecessarily
|
|
|
|
/// thunked a nested expression.
|
|
|
|
pub fn extend(&mut self, other: Self) {
|
|
|
|
// Some operations need to be modified in certain ways before being
|
|
|
|
// valid as part of the new chunk.
|
|
|
|
let const_count = self.constants.len();
|
|
|
|
for (idx, op) in other.code.iter().enumerate() {
|
|
|
|
let span = other.get_span(CodeIdx(idx));
|
|
|
|
match op {
|
|
|
|
// As the constants shift, the index needs to be moved relatively.
|
|
|
|
OpCode::OpConstant(ConstantIdx(idx)) => {
|
|
|
|
self.push_op(OpCode::OpConstant(ConstantIdx(idx + const_count)), span)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other operations either operate on relative offsets, or no
|
|
|
|
// offsets, and are safe to keep as-is.
|
|
|
|
_ => self.push_op(*op, span),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
self.constants.extend(other.constants);
|
|
|
|
self.spans.extend(other.spans);
|
|
|
|
}
|
2022-08-07 22:41:07 +02:00
|
|
|
}
|
2022-09-18 18:38:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-01-29 22:35:59 +01:00
|
|
|
use super::*;
|
2022-09-18 18:38:53 +02:00
|
|
|
use crate::test_utils::dummy_span;
|
|
|
|
|
2023-01-29 22:35:59 +01:00
|
|
|
// Note: These tests are about the functionality of the `Chunk` type, the
|
|
|
|
// opcodes used below do *not* represent valid, executable Tvix code (and
|
|
|
|
// don't need to).
|
|
|
|
|
2022-09-18 18:38:53 +02:00
|
|
|
#[test]
|
|
|
|
fn push_op() {
|
|
|
|
let mut chunk = Chunk::default();
|
2022-10-25 11:23:22 +02:00
|
|
|
chunk.push_op(OpCode::OpAdd, dummy_span());
|
|
|
|
assert_eq!(chunk.code.last().unwrap(), &OpCode::OpAdd);
|
2022-09-18 18:38:53 +02:00
|
|
|
}
|
2023-01-29 22:35:59 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extend_empty() {
|
|
|
|
let mut chunk = Chunk::default();
|
|
|
|
chunk.push_op(OpCode::OpAdd, dummy_span());
|
|
|
|
|
|
|
|
let other = Chunk::default();
|
|
|
|
chunk.extend(other);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
chunk.code,
|
|
|
|
vec![OpCode::OpAdd],
|
|
|
|
"code should not have changed"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extend_simple() {
|
|
|
|
let span = dummy_span();
|
|
|
|
let mut chunk = Chunk::default();
|
|
|
|
chunk.push_op(OpCode::OpAdd, span);
|
|
|
|
|
|
|
|
let mut other = Chunk::default();
|
|
|
|
other.push_op(OpCode::OpSub, span);
|
|
|
|
other.push_op(OpCode::OpMul, span);
|
|
|
|
|
|
|
|
let expected_code = vec![OpCode::OpAdd, OpCode::OpSub, OpCode::OpMul];
|
|
|
|
|
|
|
|
chunk.extend(other);
|
|
|
|
|
|
|
|
assert_eq!(chunk.code, expected_code, "code should have been extended");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extend_with_constant() {
|
|
|
|
let span = dummy_span();
|
|
|
|
let mut chunk = Chunk::default();
|
|
|
|
chunk.push_op(OpCode::OpAdd, span);
|
|
|
|
let cidx = chunk.push_constant(Value::Integer(0));
|
|
|
|
assert_eq!(
|
|
|
|
cidx.0, 0,
|
|
|
|
"first constant in main chunk should have index 0"
|
|
|
|
);
|
|
|
|
chunk.push_op(OpCode::OpConstant(cidx), span);
|
|
|
|
|
|
|
|
let mut other = Chunk::default();
|
|
|
|
other.push_op(OpCode::OpSub, span);
|
|
|
|
let other_cidx = other.push_constant(Value::Integer(1));
|
|
|
|
assert_eq!(
|
|
|
|
other_cidx.0, 0,
|
|
|
|
"first constant in other chunk should have index 0"
|
|
|
|
);
|
|
|
|
other.push_op(OpCode::OpConstant(other_cidx), span);
|
|
|
|
|
|
|
|
chunk.extend(other);
|
|
|
|
|
|
|
|
let expected_code = vec![
|
|
|
|
OpCode::OpAdd,
|
|
|
|
OpCode::OpConstant(ConstantIdx(0)),
|
|
|
|
OpCode::OpSub,
|
|
|
|
OpCode::OpConstant(ConstantIdx(1)), // <- note: this was rewritten
|
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
chunk.code, expected_code,
|
|
|
|
"code should have been extended and rewritten"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(chunk.constants.len(), 2);
|
|
|
|
assert!(matches!(chunk.constants[0], Value::Integer(0)));
|
|
|
|
assert!(matches!(chunk.constants[1], Value::Integer(1)));
|
|
|
|
}
|
2022-09-18 18:38:53 +02:00
|
|
|
}
|