Skip to content

Commit

Permalink
Merge pull request #235 from koto-lang/runtime-cleanups
Browse files Browse the repository at this point in the history
Runtime cleanups
  • Loading branch information
irh authored Aug 9, 2023
2 parents 4bd1ae2 + cedc46f commit 956bc2c
Show file tree
Hide file tree
Showing 48 changed files with 466 additions and 565 deletions.
54 changes: 24 additions & 30 deletions core/bytecode/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,9 @@ impl Compiler {
.compile_node(ResultRegister::Any, rhs_node, ast)?
.unwrap();

// If the result is needed then prepare a tuple in the result register
if let Some(result) = result {
self.push_op(SequenceStart, &[result.register, targets.len() as u8]);
// If the result is needed then prepare the creation of a tuple
if result.is_some() {
self.push_op(SequenceStart, &[targets.len() as u8]);
}

// If the RHS is a single value then convert it into an iterator
Expand Down Expand Up @@ -1214,8 +1214,8 @@ impl Compiler {
// needs to be committed.
self.commit_local_register(*target_register)?;

if let Some(result) = result {
self.push_op(SequencePush, &[result.register, *target_register]);
if result.is_some() {
self.push_op(SequencePush, &[*target_register]);
}
}
(None, Scope::Export) => {
Expand All @@ -1229,8 +1229,8 @@ impl Compiler {

self.compile_value_export(*id_index, value_register)?;

if let Some(result) = result {
self.push_op(SequencePush, &[result.register, value_register]);
if result.is_some() {
self.push_op(SequencePush, &[value_register]);
}

self.pop_register()?; // value_register
Expand Down Expand Up @@ -1261,14 +1261,14 @@ impl Compiler {
ast,
)?;

if let Some(result) = result {
self.push_op(SequencePush, &[result.register, value_register]);
if result.is_some() {
self.push_op(SequencePush, &[value_register]);
}

self.pop_register()?; // value_register
}
Node::Wildcard(_) => {
if let Some(result) = result {
if result.is_some() {
let value_register = self.push_register()?;

if rhs_is_temp_tuple {
Expand All @@ -1277,7 +1277,7 @@ impl Compiler {
self.push_op(IterUnpack, &[value_register, iter_register]);
}

self.push_op(SequencePush, &[result.register, value_register]);
self.push_op(SequencePush, &[value_register]);

self.pop_register()?; // value_register
} else if !rhs_is_temp_tuple {
Expand Down Expand Up @@ -1531,9 +1531,9 @@ impl Compiler {
[] => return compiler_error!(self, "Missing item to import"),
[single_item] => self.push_op(Copy, &[result.register, *single_item]),
_ => {
self.push_op(SequenceStart, &[result.register, imported.len() as u8]);
self.push_op(SequenceStart, &[imported.len() as u8]);
for item in imported.iter() {
self.push_op(SequencePush, &[result.register, *item]);
self.push_op(SequencePush, &[*item]);
}
self.push_op(SequenceToTuple, &[result.register]);
}
Expand Down Expand Up @@ -2037,35 +2037,32 @@ impl Compiler {
}
}
_ => {
if let Some(result) = result {
if result.is_some() {
if size_hint <= u8::MAX as usize {
self.push_op(Op::StringStart, &[result.register, size_hint as u8]);
self.push_op(Op::StringStart, &[size_hint as u8]);
} else {
// Limit the size hint to u32::MAX, u64 size hinting can be added later if
// it would be useful in practice.
let size_hint = size_hint.min(u32::MAX as usize) as u32;
self.push_op(Op::StringStart32, &[result.register]);
self.push_op(Op::StringStart32, &[]);
self.push_bytes(&size_hint.to_le_bytes());
}
}

for node in nodes.iter() {
match node {
StringNode::Literal(constant_index) => {
if let Some(result) = result {
if result.is_some() {
let node_register = self.push_register()?;

self.compile_load_string_constant(node_register, *constant_index);
self.push_op_without_span(
Op::StringPush,
&[result.register, node_register],
);
self.push_op_without_span(Op::StringPush, &[node_register]);

self.pop_register()?;
}
}
StringNode::Expr(expression_node) => {
if let Some(result) = result {
if result.is_some() {
let expression_result = self
.compile_node(
ResultRegister::Any,
Expand All @@ -2076,7 +2073,7 @@ impl Compiler {

self.push_op_without_span(
Op::StringPush,
&[result.register, expression_result.register],
&[expression_result.register],
);

if expression_result.is_temporary {
Expand Down Expand Up @@ -2159,10 +2156,10 @@ impl Compiler {
Some(result) => {
match elements.len() {
size_hint if size_hint <= u8::MAX as usize => {
self.push_op(SequenceStart, &[result.register, size_hint as u8]);
self.push_op(SequenceStart, &[size_hint as u8]);
}
size_hint if size_hint <= u32::MAX as usize => {
self.push_op(SequenceStart32, &[result.register]);
self.push_op(SequenceStart32, &[]);
self.push_bytes(&(size_hint as u32).to_le_bytes());
}
overflow => {
Expand All @@ -2181,10 +2178,7 @@ impl Compiler {
let element = self
.compile_node(ResultRegister::Any, ast.node(*single_element), ast)?
.unwrap();
self.push_op_without_span(
SequencePush,
&[result.register, element.register],
);
self.push_op_without_span(SequencePush, &[element.register]);
if element.is_temporary {
self.pop_register()?;
}
Expand All @@ -2206,7 +2200,7 @@ impl Compiler {

self.push_op_without_span(
SequencePushN,
&[result.register, start_register, elements_batch.len() as u8],
&[start_register, elements_batch.len() as u8],
);

self.truncate_register_stack(stack_count)?;
Expand Down
69 changes: 18 additions & 51 deletions core/bytecode/src/instruction_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,20 @@ pub enum Instruction {
size_hint: usize,
},
SequenceStart {
register: u8,
size_hint: usize,
},
SequencePush {
sequence: u8,
value: u8,
},
SequencePushN {
sequence: u8,
start: u8,
count: u8,
},
SequenceToList {
sequence: u8,
register: u8,
},
SequenceToTuple {
sequence: u8,
register: u8,
},
Range {
register: u8,
Expand Down Expand Up @@ -411,11 +408,9 @@ pub enum Instruction {
size: usize,
},
StringStart {
register: u8,
size_hint: usize,
},
StringPush {
register: u8,
value: u8,
},
StringFinish {
Expand Down Expand Up @@ -467,26 +462,15 @@ impl fmt::Debug for Instruction {
register,
size_hint,
} => write!(f, "MakeMap\t\tresult: {register}\tsize_hint: {size_hint}"),
SequenceStart {
register,
size_hint,
} => write!(
f,
"SequenceStart\tresult: {register}\tsize_hint: {size_hint}"
),
SequencePush { sequence, value } => {
write!(f, "SequencePush\tsequence: {sequence}\tvalue: {value}")
SequenceStart { size_hint } => write!(f, "SequenceStart\tsize_hint: {size_hint}"),
SequencePush { value } => {
write!(f, "SequencePush\tvalue: {value}")
}
SequencePushN {
sequence,
start,
count,
} => write!(
f,
"SequencePushN\tsequence: {sequence}\tstart: {start}\tcount: {count}",
),
SequenceToList { sequence } => write!(f, "SequenceToList\tsequence: {sequence}"),
SequenceToTuple { sequence } => write!(f, "SequenceToTuple\tsequence: {sequence}"),
SequencePushN { start, count } => {
write!(f, "SequencePushN\tstart: {start}\tcount: {count}",)
}
SequenceToList { register } => write!(f, "SequenceToList\tregister: {register}"),
SequenceToTuple { register } => write!(f, "SequenceToTuple\tregister: {register}"),
Range {
register,
start,
Expand Down Expand Up @@ -754,17 +738,11 @@ impl fmt::Debug for Instruction {
CheckSizeMin { register, size } => {
write!(f, "CheckSizeMin\tregister: {register}\tsize: {size}")
}
StringStart {
register,
size_hint,
} => {
write!(
f,
"StringStart\tregister: {register}\tsize hint: {size_hint}"
)
StringStart { size_hint } => {
write!(f, "StringStart\t\tsize hint: {size_hint}")
}
StringPush { register, value } => {
write!(f, "StringPush\tregister: {register}\tvalue: {value}")
StringPush { value } => {
write!(f, "StringPush\tvalue: {value}")
}
StringFinish { register } => {
write!(f, "StringFinish\tregister: {register}")
Expand Down Expand Up @@ -1040,27 +1018,21 @@ impl Iterator for InstructionReader {
size_hint: get_u32!() as usize,
}),
Op::SequenceStart => Some(SequenceStart {
register: get_u8!(),
size_hint: get_u8!() as usize,
}),
Op::SequenceStart32 => Some(SequenceStart {
register: get_u8!(),
size_hint: get_u32!() as usize,
}),
Op::SequencePush => Some(SequencePush {
sequence: get_u8!(),
value: get_u8!(),
}),
Op::SequencePush => Some(SequencePush { value: get_u8!() }),
Op::SequencePushN => Some(SequencePushN {
sequence: get_u8!(),
start: get_u8!(),
count: get_u8!(),
}),
Op::SequenceToList => Some(SequenceToList {
sequence: get_u8!(),
register: get_u8!(),
}),
Op::SequenceToTuple => Some(SequenceToTuple {
sequence: get_u8!(),
register: get_u8!(),
}),
Op::Range => Some(Range {
register: get_u8!(),
Expand Down Expand Up @@ -1421,17 +1393,12 @@ impl Iterator for InstructionReader {
size: get_u8!() as usize,
}),
Op::StringStart => Some(StringStart {
register: get_u8!(),
size_hint: get_u8!() as usize,
}),
Op::StringStart32 => Some(StringStart {
register: get_u8!(),
size_hint: get_u32!() as usize,
}),
Op::StringPush => Some(StringPush {
register: get_u8!(),
value: get_u8!(),
}),
Op::StringPush => Some(StringPush { value: get_u8!() }),
Op::StringFinish => Some(StringFinish {
register: get_u8!(),
}),
Expand Down
38 changes: 19 additions & 19 deletions core/bytecode/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,55 +149,55 @@ pub enum Op {
/// `[*target, *iterable]`
MakeIterator,

/// Makes a SequenceBuilder with a u8 size hint
/// Starts a new sequence with a u8 size hint
///
/// `[*target, size hint]`
/// `[size hint]`
SequenceStart,

/// Makes a SequenceBuilder with a u32 size hint
/// Starts a new sequence with a u32 size hint
///
/// `[*target, size hint[4]]`
/// `[size hint[4]]`
SequenceStart32,

/// Pushes a single value to the end of a SequenceBuilder
/// Pushes a single value to the end of the current sequence
///
/// `[*target, *value]`
/// `[*value]`
SequencePush,

/// Pushes values from consecutive registers to the end of a SequenceBuilder
/// Pushes values from consecutive registers to the end of the current sequence
///
/// `[*target, *start, value count]`
/// `[*start, value count]`
SequencePushN,

/// Converts a SequenceBuilder into a List
/// Converts the current sequence into a List
///
/// `[*register]`
SequenceToList,

/// Converts a SequenceBuilder into a Tuple
/// Converts the current sequence into a Tuple
///
/// `[*register]`
SequenceToTuple,

/// Makes a StringBuilder with a u8 size hint
/// Starts the construction of a new string with a u8 size hint
///
/// `[*target, size hint]`
/// `[size hint]`
StringStart,

/// Makes a StringBuilder with a u32 size hint
/// Starts the construction of a new string with a u32 size hint
///
/// `[*target, size hint[4]]`
/// `[size hint[4]]`
StringStart32,

/// Pushes a value to the end of a StringBuilder
/// Pushes a value to the end of the current string
///
/// Strings will have their contents added directly to the StringBuilder
/// Other values will be formatted to a string and then added to the StrignBuilder.
/// Strings will have their contents added directly to the string being built.
/// Other values will be formatted and then added to the string.
///
/// `[*target, *value]`
/// `[*value]`
StringPush,

/// Replaces a StringBuilder with a String containing the builder's contents
/// Places the finished string in the target register
///
/// `[*target]`
StringFinish,
Expand Down
Loading

0 comments on commit 956bc2c

Please sign in to comment.