refactor: 移除简单表达式计数辅助

This commit is contained in:
2026-08-05 01:10:03 +08:00
parent 198bb7d428
commit dfc14cfce9
11 changed files with 1 additions and 193 deletions
+1 -18
View File
@@ -383,11 +383,6 @@ impl CompileCtx {
self.count_instr();
}
pub(crate) fn count_template_str(&mut self, segment_count: usize) {
self.alloc_reg();
self.count_instr_with_ext(1 + segment_count);
}
pub(crate) fn count_jump(&mut self) {
self.count_instr();
}
@@ -1307,25 +1302,13 @@ impl Compiler {
| Expression::SequenceExpression(_)
| Expression::LogicalExpression(_) => self.count_conditional_chain(expr, ctx),
Expression::ChainExpression(_) => self.count_chain_expression(expr, ctx),
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => self.count_object_domain(expr, ctx),
Expression::TemplateLiteral(_) | Expression::TaggedTemplateExpression(_) => {
self.count_template_domain(expr, ctx)
}
Expression::ArrowFunctionExpression(_)
| Expression::FunctionExpression(_)
| Expression::ClassExpression(_) => self.count_function_domain(expr, ctx),
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_) => self.count_member_domain(expr, ctx),
Expression::ParenthesizedExpression(_) => self.count_parenthesized_expression(expr, ctx),
Expression::ThisExpression(_) => self.count_this_expression(ctx),
Expression::Identifier(_) => self.count_identifier_expression(expr, ctx),
Expression::NumericLiteral(_)
| Expression::StringLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::NullLiteral(_)
| Expression::RegExpLiteral(_) => self.count_literal(expr, ctx),
_ => self.count_default_expression(ctx),
_ => {}
}
}
@@ -1,35 +1,7 @@
use crate::compiler::{CompileCtx, Compiler};
use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::Expression;
impl Compiler {
pub(crate) fn count_identifier_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::Identifier(ident) = expr else {
return;
};
let name = ident.name.as_str();
for up in &ctx.current_upvalue_captures {
if up.name == name {
ctx.alloc_reg();
ctx.count_instr();
return;
}
}
if ctx.scopes.symbols.lookup_is_captured(name) {
ctx.alloc_reg();
ctx.count_instr();
return;
}
if CompileCtx::is_known_builtin(name) {
let _ = ctx.lookup_or_builtin(name);
}
ctx.alloc_reg();
ctx.projected_pc += 1;
}
pub(crate) fn emit_identifier_expression(
&self, ident: &oxide_parser::IdentifierReference, ctx: &mut CompileCtx,
) -> Result<u8, String> {
-21
View File
@@ -4,27 +4,6 @@ use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::Expression;
impl Compiler {
pub(crate) fn count_literal(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::RegExpLiteral(_) => self.count_regexp_literal(ctx),
_ => self.count_default_expression(ctx),
}
}
fn count_regexp_literal(&self, ctx: &mut CompileCtx) {
ctx.alloc_reg();
ctx.projected_pc += 1;
ctx.alloc_reg();
ctx.projected_pc += 1;
ctx.alloc_reg();
ctx.projected_pc += 1;
}
pub(crate) fn count_default_expression(&self, ctx: &mut CompileCtx) {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
pub(crate) fn emit_literal(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::NumericLiteral(n) => self.emit_numeric_literal_expression(n, ctx),
@@ -3,27 +3,7 @@ use oxide_bytecode::{
module::Constant,
opcode::{self, OpCode},
};
use oxide_parser::Expression;
impl Compiler {
pub(crate) fn count_array_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::ArrayExpression(arr) = expr else {
return;
};
ctx.alloc_reg();
ctx.projected_pc += 1;
let elem_checkpoint = ctx.reg_checkpoint();
for elem in &arr.elements {
if let Some(e) = elem.as_expression() {
self.count_expression(e, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1;
ctx.projected_pc += 1;
ctx.restore_reg_checkpoint(elem_checkpoint);
}
}
}
pub(crate) fn emit_array_expression(
&self, arr: &oxide_parser::ArrayExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
@@ -5,14 +5,6 @@ mod array;
mod object_literal;
impl Compiler {
pub(crate) fn count_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::ObjectExpression(_) => self.count_object_expression(expr, ctx),
Expression::ArrayExpression(_) => self.count_array_expression(expr, ctx),
_ => {}
}
}
pub(crate) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::ObjectExpression(obj) => self.emit_object_expression(obj, ctx),
@@ -6,30 +6,6 @@ use oxide_bytecode::{
use oxide_parser::{Expression, ObjectPropertyKind, PropertyKey, PropertyKind};
impl Compiler {
pub(crate) fn count_object_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::ObjectExpression(obj) = expr else {
return;
};
ctx.alloc_reg();
ctx.projected_pc += 1;
let prop_checkpoint = ctx.reg_checkpoint();
for prop in &obj.properties {
if let ObjectPropertyKind::ObjectProperty(p) = prop {
if matches!(p.kind, PropertyKind::Get | PropertyKind::Set) {
self.count_expression(&p.value, ctx);
ctx.count_load_const();
ctx.count_define_accessor();
} else {
ctx.alloc_reg();
ctx.projected_pc += 1;
self.count_expression(&p.value, ctx);
ctx.projected_pc += 1;
}
ctx.restore_reg_checkpoint(prop_checkpoint);
}
}
}
pub(crate) fn emit_object_expression(
&self, obj: &oxide_parser::ObjectExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
@@ -1,13 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use oxide_parser::Expression;
impl Compiler {
pub(crate) fn count_parenthesized_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
if let Expression::ParenthesizedExpression(p) = expr {
self.count_expression(&p.expression, ctx);
}
}
pub(crate) fn emit_parenthesized_expression(
&self, p: &oxide_parser::ParenthesizedExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
@@ -5,14 +5,6 @@ mod tagged;
mod template_literal;
impl Compiler {
pub(crate) fn count_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
match expr {
Expression::TemplateLiteral(_) => self.count_template_literal(expr, ctx),
Expression::TaggedTemplateExpression(_) => self.count_tagged_template_expression(expr, ctx),
_ => {}
}
}
pub(crate) fn emit_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::TemplateLiteral(tl) => self.emit_template_literal_expression(tl, ctx),
@@ -3,48 +3,7 @@ use oxide_bytecode::{
module::Constant,
opcode::{self, OpCode},
};
use oxide_parser::Expression;
impl Compiler {
pub(crate) fn count_tagged_template_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::TaggedTemplateExpression(tt) = expr else {
return;
};
self.count_expression(&tt.tag, ctx);
let quasi_count = tt.quasi.quasis.len();
for _ in 0..quasi_count {
ctx.count_load_const();
ctx.count_load_const();
ctx.count_instr();
}
ctx.alloc_reg();
ctx.count_instr();
for _ in 0..quasi_count {
ctx.count_load_const();
ctx.count_load_const();
ctx.count_instr();
}
ctx.alloc_reg();
ctx.count_instr();
for expr in &tt.quasi.expressions {
self.count_expression(expr, ctx);
}
ctx.alloc_reg();
ctx.alloc_reg();
for _ in &tt.quasi.expressions {
ctx.alloc_reg();
ctx.count_instr();
}
ctx.count_words(2);
ctx.count_load_const();
ctx.count_call_instr_with_arg_ext();
ctx.count_load_var();
}
pub(crate) fn emit_tagged_template_expression(
&self, tt: &oxide_parser::TaggedTemplateExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
@@ -3,20 +3,7 @@ use oxide_bytecode::{
module::Constant,
opcode::{self, OpCode},
};
use oxide_parser::Expression;
impl Compiler {
pub(crate) fn count_template_literal(&self, expr: &Expression, ctx: &mut CompileCtx) {
let Expression::TemplateLiteral(tl) = expr else {
return;
};
for expr in &tl.expressions {
self.count_expression(expr, ctx);
}
let segment_count = tl.quasis.len() + tl.expressions.len();
ctx.count_template_str(segment_count);
}
pub(crate) fn emit_template_literal_expression(
&self, tl: &oxide_parser::TemplateLiteral, ctx: &mut CompileCtx,
) -> Result<u8, String> {
-5
View File
@@ -2,11 +2,6 @@ use crate::compiler::{CompileCtx, Compiler};
use oxide_bytecode::opcode::{self, OpCode};
impl Compiler {
pub(crate) fn count_this_expression(&self, ctx: &mut CompileCtx) {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
pub(crate) fn emit_this_expression(&self, ctx: &mut CompileCtx) -> Result<u8, String> {
let r = ctx.alloc_reg();
let src = ctx.static_block_this_reg.unwrap_or(254);