refactor: 拆分编译流水线过程以避免逻辑漂移
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(super) fn count_break_statement(&self, ctx: &mut CompileCtx) {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
|
||||
pub(super) fn count_continue_statement(&self, ctx: &mut CompileCtx) {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
|
||||
pub(crate) fn emit_break_statement(
|
||||
&self, stmt: &oxide_parser::BreakStatement, ctx: &mut CompileCtx,
|
||||
) -> Result<Option<u8>, String> {
|
||||
let break_label = if let Some(label) = &stmt.label {
|
||||
let name = label.name.as_str();
|
||||
let scope = ctx
|
||||
.find_label(name)
|
||||
.ok_or_else(|| format!("SyntaxError: Undefined label '{name}'"))?;
|
||||
scope.break_label
|
||||
} else if let Some(sw_label) = ctx.current_switch() {
|
||||
*sw_label
|
||||
} else {
|
||||
let (bl, _) = ctx.current_loop().ok_or("break outside switch or loop".to_string())?;
|
||||
*bl
|
||||
};
|
||||
let break_pos = ctx.resolve_label(break_label)?;
|
||||
let offset = (break_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_continue_statement(
|
||||
&self, stmt: &oxide_parser::ContinueStatement, ctx: &mut CompileCtx,
|
||||
) -> Result<Option<u8>, String> {
|
||||
let continue_label = if let Some(label) = &stmt.label {
|
||||
let name = label.name.as_str();
|
||||
let scope = ctx
|
||||
.find_label(name)
|
||||
.ok_or_else(|| format!("SyntaxError: Undefined label '{name}'"))?;
|
||||
scope.continue_label.ok_or_else(|| {
|
||||
format!("SyntaxError: Illegal continue statement: '{name}' does not denote an iteration statement")
|
||||
})?
|
||||
} else {
|
||||
let (_, cl) = ctx.current_loop().ok_or("continue outside loop".to_string())?;
|
||||
*cl
|
||||
};
|
||||
let continue_pos = ctx.resolve_label(continue_label)?;
|
||||
let offset = (continue_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(super) fn count_expression_statement(
|
||||
&self, stmt: &oxide_parser::ExpressionStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
self.count_expression(&stmt.expression, ctx);
|
||||
}
|
||||
|
||||
fn emit_expression_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::ExpressionStatement(es) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(self.emit_expression(&es.expression, ctx)?))
|
||||
}
|
||||
|
||||
pub(crate) fn emit_basic_expression(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
self.emit_expression_statement(stmt, ctx)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(super) fn count_labeled_statement(&self, stmt: &oxide_parser::LabeledStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let body_is_loop = matches!(
|
||||
stmt.body,
|
||||
Statement::WhileStatement(_)
|
||||
| Statement::DoWhileStatement(_)
|
||||
| Statement::ForStatement(_)
|
||||
| Statement::ForInStatement(_)
|
||||
| Statement::ForOfStatement(_)
|
||||
);
|
||||
if body_is_loop {
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
} else {
|
||||
let id = ctx.next_label_id();
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
ctx.labels.label_map.insert(Label::LabeledEnd(id), ctx.projected_pc);
|
||||
}
|
||||
}
|
||||
|
||||
fn is_iteration_statement(stmt: &Statement) -> bool {
|
||||
matches!(
|
||||
stmt,
|
||||
Statement::WhileStatement(_)
|
||||
| Statement::DoWhileStatement(_)
|
||||
| Statement::ForStatement(_)
|
||||
| Statement::ForInStatement(_)
|
||||
| Statement::ForOfStatement(_)
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_labeled_statement(
|
||||
&self, stmt: &oxide_parser::LabeledStatement, ctx: &mut CompileCtx,
|
||||
) -> Result<Option<u8>, String> {
|
||||
let name = stmt.label.name.as_str();
|
||||
if Self::is_iteration_statement(&stmt.body) {
|
||||
ctx.queue_loop_label(name)?;
|
||||
self.emit_statement(&stmt.body, ctx)?;
|
||||
} else {
|
||||
let id = ctx.next_label_id();
|
||||
ctx.push_label_scope(name, Label::LabeledEnd(id), None)?;
|
||||
self.emit_statement(&stmt.body, ctx)?;
|
||||
ctx.pop_label_scope();
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
pub use super::super::*;
|
||||
|
||||
mod break_continue;
|
||||
mod expression;
|
||||
mod labeled;
|
||||
mod return_stmt;
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn emit_basic_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
match stmt {
|
||||
Statement::ExpressionStatement(_) => self.emit_basic_expression(stmt, ctx),
|
||||
Statement::ReturnStatement(_) => self.emit_basic_return(stmt, ctx),
|
||||
Statement::EmptyStatement(_) => Ok(None),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn count_basic(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::ExpressionStatement(es) => self.count_expression_statement(es, ctx),
|
||||
Statement::ReturnStatement(ret) => self.count_return_statement(ret, ctx),
|
||||
Statement::BreakStatement(_) => self.count_break_statement(ctx),
|
||||
Statement::ContinueStatement(_) => self.count_continue_statement(ctx),
|
||||
Statement::LabeledStatement(ls) => self.count_labeled_statement(ls, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(super) fn count_return_statement(&self, stmt: &oxide_parser::ReturnStatement<'_>, ctx: &mut CompileCtx) {
|
||||
if let Some(arg) = &stmt.argument {
|
||||
self.count_expression(arg, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
|
||||
fn emit_return_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::ReturnStatement(ret) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
match &ret.argument {
|
||||
Some(expr) => {
|
||||
let r = self.emit_expression(expr, ctx)?;
|
||||
ctx.emit(opcode::encode(OpCode::RETURN, r, 0, 0));
|
||||
}
|
||||
None => {
|
||||
ctx.emit(opcode::encode(OpCode::RETURN, 0, 0, 0));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_basic_return(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
self.emit_return_statement(stmt, ctx)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
fn count_block_statement(&self, block: &oxide_parser::BlockStatement<'_>, ctx: &mut CompileCtx) {
|
||||
for s in &block.body {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_block_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::BlockStatement(block) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
ctx.push_scope();
|
||||
let mut r = None;
|
||||
for s in &block.body {
|
||||
if let Some(rr) = self.emit_statement(s, ctx)? {
|
||||
r = Some(rr);
|
||||
}
|
||||
}
|
||||
ctx.pop_scope();
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
pub(crate) fn count_block_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
if let Statement::BlockStatement(block) = stmt {
|
||||
self.count_block_statement(block, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_block_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
match stmt {
|
||||
Statement::BlockStatement(_) => self.emit_block_statement(stmt, ctx),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
fn count_if_statement(&self, stmt: &oxide_parser::IfStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let else_label = Label::IfElse(id);
|
||||
let end_label = Label::IfEnd(id);
|
||||
|
||||
self.count_expression(&stmt.test, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
self.count_statement(&stmt.consequent, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
if stmt.alternate.is_some() {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.labels.label_map.insert(else_label, ctx.projected_pc);
|
||||
if let Some(alt_stmt) = &stmt.alternate {
|
||||
self.count_statement(alt_stmt, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
fn emit_if_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::IfStatement(ifs) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let else_label = Label::IfElse(id);
|
||||
let end_label = Label::IfEnd(id);
|
||||
|
||||
let test_reg = self.emit_expression(&ifs.test, ctx)?;
|
||||
|
||||
let else_pos = ctx.resolve_label(else_label)?;
|
||||
let offset = (else_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
|
||||
|
||||
let cons_reg = self.emit_statement(&ifs.consequent, ctx)?;
|
||||
let result_reg = ctx.alloc_reg();
|
||||
if let Some(r) = cons_reg {
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, r, 0));
|
||||
} else {
|
||||
let undef_idx = ctx.add_constant(Constant::Undefined);
|
||||
ctx.emit_load_const(result_reg, undef_idx);
|
||||
}
|
||||
|
||||
if ifs.alternate.is_some() {
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
}
|
||||
|
||||
if let Some(alt) = &ifs.alternate {
|
||||
let alt_reg = self.emit_statement(alt, ctx)?;
|
||||
if let Some(r) = alt_reg {
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, r, 0));
|
||||
} else {
|
||||
let undef_idx = ctx.add_constant(Constant::Undefined);
|
||||
ctx.emit_load_const(result_reg, undef_idx);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Some(result_reg))
|
||||
}
|
||||
|
||||
pub(crate) fn count_control_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
if let Statement::IfStatement(ifs) = stmt {
|
||||
self.count_if_statement(ifs, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_control_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
match stmt {
|
||||
Statement::IfStatement(_) => self.emit_if_statement(stmt, ctx),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub use super::super::*;
|
||||
|
||||
pub mod var;
|
||||
@@ -0,0 +1,162 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
fn count_variable_declaration(&self, decl: &oxide_parser::VariableDeclaration<'_>, ctx: &mut CompileCtx) {
|
||||
let is_var = matches!(decl.kind, VariableDeclarationKind::Var);
|
||||
for d in &decl.declarations {
|
||||
if let Some(init) = &d.init {
|
||||
self.count_expression(init, ctx);
|
||||
if is_var {
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_initialized(bi.name.as_str(), reg, VariableDeclarationKind::Var, false);
|
||||
ctx.projected_pc += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
self.count_binding_pattern(&d.id, ctx);
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_words(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn count_function_declaration(&self, decl: &oxide_parser::Function<'_>, ctx: &mut CompileCtx) {
|
||||
let name = if let Some(id) = &decl.id {
|
||||
id.name.to_string()
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
let func_reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_initialized(&name, func_reg, VariableDeclarationKind::Var, false);
|
||||
ctx.count_words(2);
|
||||
}
|
||||
|
||||
fn count_class_declaration(&self, class: &oxide_parser::Class<'_>, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg();
|
||||
self.count_class(class, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
|
||||
pub(crate) fn count_declaration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::VariableDeclaration(decl) => self.count_variable_declaration(decl, ctx),
|
||||
Statement::FunctionDeclaration(fd) => self.count_function_declaration(fd, ctx),
|
||||
Statement::ClassDeclaration(class) => self.count_class_declaration(class, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn emit_variable_declaration_statement(
|
||||
&self, stmt: &Statement, ctx: &mut CompileCtx,
|
||||
) -> Result<Option<u8>, String> {
|
||||
let Statement::VariableDeclaration(decl) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let mut r = None;
|
||||
for d in &decl.declarations {
|
||||
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);
|
||||
if is_const && d.init.is_none() {
|
||||
return Err("const declaration must have an initializer".into());
|
||||
}
|
||||
if let Some(init) = &d.init {
|
||||
let val_reg = self.emit_expression(init, ctx)?;
|
||||
self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?;
|
||||
if let BindingPattern::BindingIdentifier(bi) = &d.id {
|
||||
if matches!(*init, Expression::ArrowFunctionExpression(_)) {
|
||||
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
|
||||
sub_mod.function_name = Some(bi.name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
r = Some(val_reg);
|
||||
} else {
|
||||
let BindingPattern::BindingIdentifier(bi) = &d.id else {
|
||||
return Err("destructuring declaration requires an initializer".into());
|
||||
};
|
||||
let idx = ctx.add_constant(Constant::Undefined);
|
||||
let tmp = ctx.alloc_reg();
|
||||
ctx.emit_load_const(tmp, idx);
|
||||
let var_reg = ctx.alloc_reg();
|
||||
ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const)?;
|
||||
let is_captured = ctx.scopes.symbols.lookup_is_captured(bi.name.as_str());
|
||||
if is_captured {
|
||||
let cell_idx = ctx.scopes.cell_registry.len() as u8;
|
||||
ctx.scopes.cell_registry.push((bi.name.to_string(), cell_idx));
|
||||
ctx.emit(opcode::encode(OpCode::MAKE_CELL, tmp, cell_idx, 0));
|
||||
} else {
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, tmp, 0));
|
||||
}
|
||||
ctx.init_var(bi.name.as_str());
|
||||
r = Some(var_reg);
|
||||
}
|
||||
}
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_function_declaration_statement(
|
||||
&self, stmt: &Statement, ctx: &mut CompileCtx,
|
||||
) -> Result<Option<u8>, String> {
|
||||
let Statement::FunctionDeclaration(fd) = stmt else {
|
||||
return Err("FunctionDeclaration without name".into());
|
||||
};
|
||||
let name = if let Some(id) = &fd.id {
|
||||
id.name.to_string()
|
||||
} else {
|
||||
return Err("FunctionDeclaration without name".into());
|
||||
};
|
||||
let mut param_names = Vec::new();
|
||||
for (idx, param) in fd.params.items.iter().enumerate() {
|
||||
match ¶m.pattern {
|
||||
oxide_parser::BindingPattern::BindingIdentifier(bi) => {
|
||||
param_names.push(ParamSpec::Identifier(bi.name.to_string()))
|
||||
}
|
||||
pattern => param_names.push(ParamSpec::Pattern {
|
||||
synthetic_name: format!("@@param_{idx}"),
|
||||
pattern,
|
||||
}),
|
||||
}
|
||||
}
|
||||
let body_stmts: &[Statement] = if let Some(body) = &fd.body { &body.statements } else { &[] };
|
||||
let mut sub_module = self.compile_function_body(¶m_names, body_stmts, ctx, false, false)?;
|
||||
sub_module.function_name = Some(name.clone());
|
||||
ctx.sub_modules.push(sub_module);
|
||||
let sub_idx = ctx.sub_modules.len() as u32;
|
||||
let var_reg = ctx.lookup(&name)?;
|
||||
ctx.reserve_reg(var_reg);
|
||||
ctx.emit_create_closure(var_reg, sub_idx);
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, var_reg, 0));
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_class_declaration_statement(
|
||||
&self, stmt: &Statement, ctx: &mut CompileCtx,
|
||||
) -> Result<Option<u8>, String> {
|
||||
let Statement::ClassDeclaration(class) = stmt else {
|
||||
return Err("ClassDeclaration without name".into());
|
||||
};
|
||||
let name = class
|
||||
.id
|
||||
.as_ref()
|
||||
.map(|id| id.name.to_string())
|
||||
.ok_or_else(|| "ClassDeclaration without name".to_string())?;
|
||||
let var_reg = ctx.alloc_reg();
|
||||
ctx.declare(&name, var_reg, VariableDeclarationKind::Let, false)?;
|
||||
ctx.init_var(&name);
|
||||
let ctor_reg = self.emit_class(class, ctx)?;
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, ctor_reg, 0));
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_declaration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
match stmt {
|
||||
Statement::VariableDeclaration(_) => self.emit_variable_declaration_statement(stmt, ctx),
|
||||
Statement::FunctionDeclaration(_) => self.emit_function_declaration_statement(stmt, ctx),
|
||||
Statement::ClassDeclaration(_) => self.emit_class_declaration_statement(stmt, ctx),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
fn count_throw_statement(&self, stmt: &oxide_parser::ThrowStatement<'_>, ctx: &mut CompileCtx) {
|
||||
self.count_expression(&stmt.argument, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
|
||||
fn count_try_statement(&self, stmt: &oxide_parser::TryStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let catch_label = Label::CatchBody(id);
|
||||
let try_end_label = Label::TryEnd(id);
|
||||
let has_catch = stmt.handler.is_some();
|
||||
let has_finally = stmt.finalizer.is_some();
|
||||
ctx.alloc_reg();
|
||||
if has_finally {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
if has_catch {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
for s in &stmt.block.body {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
if has_catch {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
let jmp_needed = has_catch || has_finally;
|
||||
if jmp_needed {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.labels.label_map.insert(catch_label, ctx.projected_pc);
|
||||
if let Some(catch) = &stmt.handler {
|
||||
ctx.push_scope();
|
||||
if let Some(param) = &catch.param {
|
||||
ctx.alloc_reg();
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(_) = ¶m.pattern {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
}
|
||||
for cs in &catch.body.body {
|
||||
self.count_statement(cs, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
ctx.pop_scope();
|
||||
}
|
||||
if let Some(finally) = &stmt.finalizer {
|
||||
let finally_label = Label::FinallyBody(id);
|
||||
ctx.labels.label_map.insert(finally_label, ctx.projected_pc);
|
||||
for fs in &finally.body {
|
||||
self.count_statement(fs, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.labels.label_map.insert(try_end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(crate) fn count_exception_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::ThrowStatement(ts) => self.count_throw_statement(ts, ctx),
|
||||
Statement::TryStatement(ts) => self.count_try_statement(ts, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn emit_throw_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::ThrowStatement(ts) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let exc_reg = self.emit_expression(&ts.argument, ctx)?;
|
||||
ctx.emit(opcode::encode(OpCode::THROW, exc_reg, 0, 0));
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_try_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::TryStatement(ts) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let catch_label = Label::CatchBody(id);
|
||||
let try_end_label = Label::TryEnd(id);
|
||||
let has_catch = ts.handler.is_some();
|
||||
let has_finally = ts.finalizer.is_some();
|
||||
let result_reg = ctx.alloc_reg();
|
||||
let mut try_finally_begin_pos: Option<usize> = None;
|
||||
let mut try_begin_pos: Option<usize> = None;
|
||||
if has_finally {
|
||||
try_finally_begin_pos = Some(ctx.bytecode.len());
|
||||
ctx.emit(opcode::encode_try_finally_begin(0));
|
||||
}
|
||||
if has_catch {
|
||||
try_begin_pos = Some(ctx.bytecode.len());
|
||||
ctx.emit(opcode::encode_try_begin(0));
|
||||
}
|
||||
let mut last_try_result: Option<u8> = None;
|
||||
for s in &ts.block.body {
|
||||
if let Some(r) = self.emit_statement(s, ctx)? {
|
||||
last_try_result = Some(r);
|
||||
}
|
||||
}
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, last_try_result.unwrap_or(result_reg), 0));
|
||||
if has_catch {
|
||||
ctx.emit(opcode::encode(OpCode::TRY_END, 0, 0, 0));
|
||||
}
|
||||
let jmp_needed = has_catch || has_finally;
|
||||
let jmp_skip_pos = if jmp_needed {
|
||||
let pos = ctx.bytecode.len();
|
||||
ctx.emit(opcode::encode_jmp(0));
|
||||
Some(pos)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let catch_label_pc = ctx.bytecode.len();
|
||||
ctx.labels.label_map.insert(catch_label, catch_label_pc);
|
||||
if let Some(try_begin_pc) = try_begin_pos {
|
||||
let offset = catch_label_pc as isize - (try_begin_pc as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.bytecode[try_begin_pc] = opcode::encode_try_begin(offset);
|
||||
}
|
||||
if let Some(catch) = &ts.handler {
|
||||
ctx.push_scope();
|
||||
if let Some(param) = &catch.param {
|
||||
let catch_reg = ctx.alloc_reg();
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = ¶m.pattern {
|
||||
ctx.declare_initialized(bi.name.as_str(), catch_reg, VariableDeclarationKind::Let, false)?;
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, catch_reg, 0, 0));
|
||||
}
|
||||
}
|
||||
let mut last_catch_result: Option<u8> = None;
|
||||
for s in &catch.body.body {
|
||||
if let Some(r) = self.emit_statement(s, ctx)? {
|
||||
last_catch_result = Some(r);
|
||||
}
|
||||
}
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, last_catch_result.unwrap_or(result_reg), 0));
|
||||
ctx.pop_scope();
|
||||
}
|
||||
if has_finally {
|
||||
let finally_label = Label::FinallyBody(id);
|
||||
let finally_label_pc = ctx.bytecode.len();
|
||||
ctx.labels.label_map.insert(finally_label, finally_label_pc);
|
||||
if let Some(fb_pos) = try_finally_begin_pos {
|
||||
let offset = finally_label_pc as isize - (fb_pos as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.bytecode[fb_pos] = opcode::encode_try_finally_begin(offset);
|
||||
}
|
||||
if let Some(jmp_pos) = jmp_skip_pos {
|
||||
let offset = finally_label_pc as isize - (jmp_pos as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.bytecode[jmp_pos] = opcode::encode_jmp(offset);
|
||||
}
|
||||
let mut last_finally_result: Option<u8> = None;
|
||||
for s in &ts.finalizer.as_ref().unwrap().body {
|
||||
if let Some(r) = self.emit_statement(s, ctx)? {
|
||||
last_finally_result = Some(r);
|
||||
}
|
||||
}
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, last_finally_result.unwrap_or(result_reg), 0));
|
||||
ctx.emit(opcode::encode(OpCode::TRY_FINALLY_END, 0, 0, 0));
|
||||
} else if let Some(jmp_pos) = jmp_skip_pos {
|
||||
let offset = ctx.bytecode.len() as isize - (jmp_pos as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.bytecode[jmp_pos] = opcode::encode_jmp(offset);
|
||||
}
|
||||
let try_end_pc = ctx.bytecode.len();
|
||||
ctx.labels.label_map.insert(try_end_label, try_end_pc);
|
||||
Ok(Some(result_reg))
|
||||
}
|
||||
|
||||
pub(crate) fn emit_exception_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
match stmt {
|
||||
Statement::ThrowStatement(_) => self.emit_throw_statement(stmt, ctx),
|
||||
Statement::TryStatement(_) => self.emit_try_statement(stmt, ctx),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub use super::super::*;
|
||||
|
||||
pub mod count;
|
||||
pub mod emit;
|
||||
@@ -0,0 +1,138 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
fn count_while_statement(&self, stmt: &oxide_parser::WhileStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::WhileStart(id);
|
||||
let end_label = Label::WhileEnd(id);
|
||||
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
|
||||
self.count_expression(&stmt.test, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
fn count_do_while_statement(&self, stmt: &oxide_parser::DoWhileStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::DoWhileStart(id);
|
||||
let end_label = Label::DoWhileEnd(id);
|
||||
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
self.count_expression(&stmt.test, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
fn count_for_statement(&self, stmt: &oxide_parser::ForStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForStart(id);
|
||||
let update_label = Label::ForUpdate(id);
|
||||
let end_label = Label::ForEnd(id);
|
||||
if let Some(init) = &stmt.init {
|
||||
if let Some(expr) = init.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
} else if let ForStatementInit::VariableDeclaration(decl) = init {
|
||||
for d in &decl.declarations {
|
||||
if let Some(init_expr) = &d.init {
|
||||
self.count_expression(init_expr, ctx);
|
||||
self.count_binding_pattern(&d.id, ctx);
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.alloc_reg();
|
||||
ctx.count_words(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
|
||||
if let Some(test) = &stmt.test {
|
||||
self.count_expression(test, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
ctx.labels.label_map.insert(update_label, ctx.projected_pc);
|
||||
if let Some(update) = &stmt.update {
|
||||
self.count_expression(update, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
fn count_for_in_statement(&self, stmt: &oxide_parser::ForInStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForInStart(id);
|
||||
let end_label = Label::ForInEnd(id);
|
||||
self.count_expression(&stmt.right, ctx);
|
||||
ctx.count_instr();
|
||||
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
|
||||
ctx.count_instr();
|
||||
ctx.count_jump();
|
||||
ctx.count_jump();
|
||||
ctx.count_instr();
|
||||
match &stmt.left {
|
||||
oxide_parser::ForStatementLeft::VariableDeclaration(decl) => {
|
||||
for _d in &decl.declarations {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr();
|
||||
}
|
||||
}
|
||||
oxide_parser::ForStatementLeft::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
ctx.count_jump();
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.count_instr();
|
||||
}
|
||||
|
||||
fn count_for_of_statement(&self, stmt: &oxide_parser::ForOfStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForOfStart(id);
|
||||
let end_label = Label::ForOfEnd(id);
|
||||
self.count_expression(&stmt.right, ctx);
|
||||
ctx.count_instr();
|
||||
ctx.labels.label_map.insert(start_label, ctx.projected_pc);
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr();
|
||||
ctx.count_jump();
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr();
|
||||
match &stmt.left {
|
||||
oxide_parser::ForStatementLeft::VariableDeclaration(decl) => {
|
||||
for d in &decl.declarations {
|
||||
self.count_binding_pattern(&d.id, ctx);
|
||||
}
|
||||
}
|
||||
oxide_parser::ForStatementLeft::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr();
|
||||
}
|
||||
oxide_parser::ForStatementLeft::ArrayAssignmentTarget(ap) => {
|
||||
self.count_array_assignment(ap, ctx);
|
||||
}
|
||||
oxide_parser::ForStatementLeft::ObjectAssignmentTarget(op) => {
|
||||
self.count_object_assignment(op, ctx);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
ctx.count_jump();
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.count_instr();
|
||||
}
|
||||
|
||||
pub(crate) fn count_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::WhileStatement(wh) => self.count_while_statement(wh, ctx),
|
||||
Statement::DoWhileStatement(dw) => self.count_do_while_statement(dw, ctx),
|
||||
Statement::ForStatement(fr) => self.count_for_statement(fr, ctx),
|
||||
Statement::ForInStatement(fi) => self.count_for_in_statement(fi, ctx),
|
||||
Statement::ForOfStatement(fo) => self.count_for_of_statement(fo, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn emit_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
match stmt {
|
||||
Statement::WhileStatement(_) => self.emit_while_statement(stmt, ctx),
|
||||
Statement::DoWhileStatement(_) => self.emit_do_while_statement(stmt, ctx),
|
||||
Statement::ForStatement(_) => self.emit_for_statement(stmt, ctx),
|
||||
Statement::ForInStatement(_) => self.emit_for_in_statement(stmt, ctx),
|
||||
Statement::ForOfStatement(_) => self.emit_for_of_statement(stmt, ctx),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::WhileStatement(wh) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::WhileStart(id);
|
||||
let end_label = Label::WhileEnd(id);
|
||||
ctx.push_loop(end_label, start_label);
|
||||
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
|
||||
let test_reg = self.emit_expression(&wh.test, ctx)?;
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
|
||||
self.emit_statement(&wh.body, ctx)?;
|
||||
let start_pos = ctx.resolve_label(start_label)?;
|
||||
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
ctx.pop_label_scopes(n_labeled);
|
||||
ctx.pop_loop();
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_do_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::DoWhileStatement(dw) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::DoWhileStart(id);
|
||||
let end_label = Label::DoWhileEnd(id);
|
||||
ctx.push_loop(end_label, start_label);
|
||||
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
|
||||
self.emit_statement(&dw.body, ctx)?;
|
||||
let test_reg = self.emit_expression(&dw.test, ctx)?;
|
||||
let start_pos = ctx.resolve_label(start_label)?;
|
||||
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_true(test_reg, offset));
|
||||
ctx.pop_label_scopes(n_labeled);
|
||||
ctx.pop_loop();
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_for_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::ForStatement(fr) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForStart(id);
|
||||
let update_label = Label::ForUpdate(id);
|
||||
let end_label = Label::ForEnd(id);
|
||||
ctx.push_loop(end_label, update_label);
|
||||
let n_labeled = ctx.take_pending_loop_labels(end_label, update_label);
|
||||
if let Some(init) = &fr.init {
|
||||
if let Some(expr) = init.as_expression() {
|
||||
self.emit_expression(expr, ctx)?;
|
||||
} else if let ForStatementInit::VariableDeclaration(decl) = init {
|
||||
for d in &decl.declarations {
|
||||
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);
|
||||
if let Some(init_expr) = &d.init {
|
||||
let val_reg = self.emit_expression(init_expr, ctx)?;
|
||||
self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?;
|
||||
} else if let BindingPattern::BindingIdentifier(bi) = &d.id {
|
||||
let idx = ctx.add_constant(Constant::Undefined);
|
||||
let tmp = ctx.alloc_reg();
|
||||
ctx.emit_load_const(tmp, idx);
|
||||
let var_reg = ctx.alloc_reg();
|
||||
ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const)?;
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, tmp, 0));
|
||||
ctx.init_var(bi.name.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(test) = &fr.test {
|
||||
let test_reg = self.emit_expression(test, ctx)?;
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
|
||||
}
|
||||
self.emit_statement(&fr.body, ctx)?;
|
||||
if let Some(update) = &fr.update {
|
||||
self.emit_expression(update, ctx)?;
|
||||
}
|
||||
let start_pos = ctx.resolve_label(start_label)?;
|
||||
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
ctx.pop_label_scopes(n_labeled);
|
||||
ctx.pop_loop();
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_for_in_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::ForInStatement(fi) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForInStart(id);
|
||||
let end_label = Label::ForInEnd(id);
|
||||
let obj_reg = self.emit_expression(&fi.right, ctx)?;
|
||||
ctx.emit(opcode::encode(OpCode::FOR_IN_INIT, 0, obj_reg, 0));
|
||||
ctx.push_loop(end_label, start_label);
|
||||
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
|
||||
let done_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_IN_DONE, done_reg, 0, 0));
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let cleanup_jmp_offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
ctx.emit(opcode::encode_jmp_if_false(done_reg, 2));
|
||||
let cleanup_jmp_offset = ctx.checked_jump_offset(cleanup_jmp_offset);
|
||||
ctx.emit(opcode::encode_jmp(cleanup_jmp_offset));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_IN_NEXT, key_reg, 0, 0));
|
||||
match &fi.left {
|
||||
ForStatementLeft::VariableDeclaration(decl) => {
|
||||
for d in &decl.declarations {
|
||||
let name = match &d.id {
|
||||
oxide_parser::BindingPattern::BindingIdentifier(bi) => bi.name.as_str(),
|
||||
_ => return Err("destructuring not supported".into()),
|
||||
};
|
||||
let var_reg = ctx.alloc_reg();
|
||||
ctx.declare(name, var_reg, decl.kind, matches!(decl.kind, VariableDeclarationKind::Const))?;
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, key_reg, 0));
|
||||
ctx.init_var(name);
|
||||
}
|
||||
}
|
||||
ForStatementLeft::AssignmentTargetIdentifier(id_ref) => {
|
||||
let name = id_ref.name.as_str();
|
||||
let var_reg = ctx.lookup_or_global(name);
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, key_reg, 0));
|
||||
}
|
||||
_ => return Err("unsupported for-in left-hand side".into()),
|
||||
}
|
||||
self.emit_statement(&fi.body, ctx)?;
|
||||
let start_pos = ctx.resolve_label(start_label)?;
|
||||
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
ctx.emit(opcode::encode(OpCode::FOR_IN_CLEANUP, 0, 0, 0));
|
||||
ctx.pop_label_scopes(n_labeled);
|
||||
ctx.pop_loop();
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_for_of_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::ForOfStatement(fo) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForOfStart(id);
|
||||
let end_label = Label::ForOfEnd(id);
|
||||
let iter_src_reg = self.emit_expression(&fo.right, ctx)?;
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, iter_src_reg, 0));
|
||||
ctx.push_loop(end_label, start_label);
|
||||
let n_labeled = ctx.take_pending_loop_labels(end_label, start_label);
|
||||
let has_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_false(has_reg, offset));
|
||||
let val_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
|
||||
match &fo.left {
|
||||
ForStatementLeft::VariableDeclaration(decl) => {
|
||||
for d in &decl.declarations {
|
||||
self.emit_binding_pattern(&d.id, val_reg, decl.kind, false, ctx)?;
|
||||
}
|
||||
}
|
||||
ForStatementLeft::AssignmentTargetIdentifier(id_ref) => {
|
||||
let name = id_ref.name.as_str();
|
||||
let var_reg = ctx.lookup_or_global(name);
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, val_reg, 0));
|
||||
}
|
||||
ForStatementLeft::ArrayAssignmentTarget(ap) => {
|
||||
self.emit_array_assignment(ap, val_reg, ctx)?;
|
||||
}
|
||||
ForStatementLeft::ObjectAssignmentTarget(op) => {
|
||||
self.emit_object_assignment(op, val_reg, ctx)?;
|
||||
}
|
||||
_ => return Err("unsupported for-of left-hand side".into()),
|
||||
}
|
||||
self.emit_statement(&fo.body, ctx)?;
|
||||
let start_pos = ctx.resolve_label(start_label)?;
|
||||
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
ctx.emit(opcode::encode(OpCode::FOR_OF_CLOSE, 0, 0, 0));
|
||||
ctx.pop_label_scopes(n_labeled);
|
||||
ctx.pop_loop();
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub use super::super::*;
|
||||
|
||||
pub mod count;
|
||||
pub mod emit;
|
||||
@@ -0,0 +1,9 @@
|
||||
pub use super::*;
|
||||
|
||||
pub mod basic;
|
||||
pub mod block;
|
||||
pub mod control;
|
||||
pub mod declaration;
|
||||
pub mod exception;
|
||||
pub mod iteration;
|
||||
pub mod switch;
|
||||
@@ -0,0 +1,86 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
fn count_switch_statement(&self, stmt: &oxide_parser::SwitchStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let end_label = Label::SwitchEnd(id);
|
||||
ctx.push_switch(end_label);
|
||||
self.count_expression(&stmt.discriminant, ctx);
|
||||
let compare_reg_checkpoint = ctx.reg_checkpoint();
|
||||
let cases = &stmt.cases;
|
||||
for case in cases.iter() {
|
||||
if let Some(test) = &case.test {
|
||||
self.count_expression(test, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
ctx.restore_reg_checkpoint(compare_reg_checkpoint);
|
||||
}
|
||||
}
|
||||
let has_default = cases.iter().any(|c| c.test.is_none());
|
||||
if !has_default {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
for (case_idx, case) in cases.iter().enumerate() {
|
||||
let case_label = Label::SwitchCase(id, case_idx as u32);
|
||||
ctx.labels.label_map.insert(case_label, ctx.projected_pc);
|
||||
for s in &case.consequent {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
}
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.pop_switch();
|
||||
}
|
||||
|
||||
fn emit_switch_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
let Statement::SwitchStatement(sw) = stmt else {
|
||||
return Ok(None);
|
||||
};
|
||||
let id = ctx.next_label_id();
|
||||
let end_label = Label::SwitchEnd(id);
|
||||
ctx.push_switch(end_label);
|
||||
let disc_reg = self.emit_expression(&sw.discriminant, ctx)?;
|
||||
let compare_reg_checkpoint = ctx.reg_checkpoint();
|
||||
let cases = &sw.cases;
|
||||
for (case_idx, case) in cases.iter().enumerate() {
|
||||
if let Some(test) = &case.test {
|
||||
let test_reg = self.emit_expression(test, ctx)?;
|
||||
let eq_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::EQ, eq_reg, disc_reg, test_reg));
|
||||
let case_label = Label::SwitchCase(id, case_idx as u32);
|
||||
let body_pos = ctx.resolve_label(case_label)?;
|
||||
let offset = (body_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_true(eq_reg, offset));
|
||||
ctx.restore_reg_checkpoint(compare_reg_checkpoint);
|
||||
}
|
||||
}
|
||||
let has_default = cases.iter().any(|c| c.test.is_none());
|
||||
if !has_default {
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
}
|
||||
for case in cases.iter() {
|
||||
for s in &case.consequent {
|
||||
self.emit_statement(s, ctx)?;
|
||||
}
|
||||
}
|
||||
ctx.pop_switch();
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn count_switch_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
if let Statement::SwitchStatement(sw) = stmt {
|
||||
self.count_switch_statement(sw, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_switch_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||
match stmt {
|
||||
Statement::SwitchStatement(_) => self.emit_switch_statement(stmt, ctx),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user