mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-15 00:18:06 -05:00
Removed remaining inline assembly
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Module {
|
||||
pub definitions: Vec<Definition>,
|
||||
}
|
||||
@@ -103,7 +103,7 @@ impl std::fmt::Display for Class {
|
||||
Value::Object(object) => {
|
||||
write!(f, "{{\n")?;
|
||||
for (name, method) in &object.properties {
|
||||
write!(f, " {}: {}\n", name, method)?;
|
||||
write!(f, " {}: {},\n", name, method)?;
|
||||
}
|
||||
write!(f, "}})\n")?;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,10 @@ use swc_common::{errors::Handler, FileName, SourceMap, Spanned};
|
||||
use swc_ecma_ast::EsVersion;
|
||||
use swc_ecma_parser::{Syntax, TsConfig};
|
||||
|
||||
use crate::asm::{Instruction, Pointer, Register, Value};
|
||||
use crate::asm::{
|
||||
Class, Definition, DefinitionContent, Function, Instruction, InstructionOrLabel, Module, Object,
|
||||
Pointer, Register, Value,
|
||||
};
|
||||
|
||||
use super::diagnostic::{Diagnostic, DiagnosticLevel};
|
||||
use super::expression_compiler::{CompiledExpression, ExpressionCompiler};
|
||||
@@ -71,24 +74,16 @@ pub fn compile_program(program: &swc_ecma_ast::Program) -> CompilerOutput {
|
||||
let mut compiler = Compiler::default();
|
||||
compiler.compile_program(&program);
|
||||
|
||||
let mut assembly = Vec::<String>::new();
|
||||
let mut first = true;
|
||||
let mut module = Module::default();
|
||||
module.definitions.append(&mut compiler.definitions);
|
||||
|
||||
for def in compiler.definitions {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
assembly.push("".to_string());
|
||||
}
|
||||
|
||||
for line in def {
|
||||
assembly.push(line);
|
||||
}
|
||||
}
|
||||
let assembly_str = module.to_string();
|
||||
let assembly_lines = assembly_str.split("\n");
|
||||
let assembly_lines_vec = assembly_lines.map(|s| s.to_string()).collect();
|
||||
|
||||
return CompilerOutput {
|
||||
diagnostics: compiler.diagnostics,
|
||||
assembly,
|
||||
assembly: assembly_lines_vec,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -110,7 +105,7 @@ pub fn compile(source: &str) -> CompilerOutput {
|
||||
struct Compiler {
|
||||
diagnostics: Vec<Diagnostic>,
|
||||
definition_allocator: Rc<RefCell<NameAllocator>>,
|
||||
definitions: Vec<Vec<String>>,
|
||||
definitions: Vec<Definition>,
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
@@ -730,7 +725,7 @@ impl Compiler {
|
||||
functionish: Functionish,
|
||||
parent_scope: &Scope,
|
||||
) {
|
||||
let (defn, mut diagnostics) = FunctionCompiler::compile(
|
||||
let (mut defn, mut diagnostics) = FunctionCompiler::compile(
|
||||
defn_pointer,
|
||||
fn_name,
|
||||
functionish,
|
||||
@@ -738,12 +733,14 @@ impl Compiler {
|
||||
parent_scope,
|
||||
);
|
||||
|
||||
self.definitions.push(defn);
|
||||
self.definitions.append(&mut defn);
|
||||
self.diagnostics.append(&mut diagnostics);
|
||||
}
|
||||
|
||||
fn compile_class_decl(&mut self, class_decl: &swc_ecma_ast::ClassDecl, parent_scope: &Scope) {
|
||||
let mut defn = Vec::<String>::new();
|
||||
let mut constructor: Value = Value::Void;
|
||||
let mut methods: Object = Object::default();
|
||||
let mut dependent_definitions: Vec<Definition>;
|
||||
|
||||
let class_name = class_decl.ident.sym.to_string();
|
||||
|
||||
@@ -760,8 +757,6 @@ impl Compiler {
|
||||
}
|
||||
};
|
||||
|
||||
let mut constructor_defn_name: Option<Pointer> = None;
|
||||
|
||||
let mut member_initializers_fnc = FunctionCompiler::new(self.definition_allocator.clone());
|
||||
|
||||
for class_member in &class_decl.class.body {
|
||||
@@ -806,11 +801,12 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
let mut member_initializers_assembly = Vec::<String>::new();
|
||||
member_initializers_assembly.append(&mut member_initializers_fnc.lines);
|
||||
let mut member_initializers_assembly = Vec::<InstructionOrLabel>::new();
|
||||
member_initializers_assembly.append(&mut member_initializers_fnc.current.body);
|
||||
|
||||
// Include any other definitions that were created by the member initializers
|
||||
member_initializers_fnc.process_queue(parent_scope);
|
||||
defn.append(&mut member_initializers_fnc.lines);
|
||||
dependent_definitions = std::mem::take(&mut member_initializers_fnc.definitions);
|
||||
|
||||
let mut has_constructor = false;
|
||||
|
||||
@@ -824,11 +820,9 @@ impl Compiler {
|
||||
self.compile_fn(
|
||||
ctor_defn_name.clone(),
|
||||
None,
|
||||
Functionish::Constructor(member_initializers_assembly.clone(), constructor.clone()),
|
||||
Functionish::Constructor(constructor.clone()),
|
||||
parent_scope,
|
||||
);
|
||||
|
||||
constructor_defn_name = Some(ctor_defn_name);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -837,27 +831,16 @@ impl Compiler {
|
||||
if member_initializers_assembly.len() > 0 && !has_constructor {
|
||||
let ctor_defn_name = self.allocate_defn(&format!("{}_constructor", class_name));
|
||||
|
||||
defn.push(format!("{} = function() {{", ctor_defn_name));
|
||||
|
||||
for line in member_initializers_assembly {
|
||||
defn.push(line.clone());
|
||||
}
|
||||
|
||||
defn.push("}".to_string());
|
||||
defn.push("".to_string());
|
||||
|
||||
constructor_defn_name = Some(ctor_defn_name);
|
||||
constructor = Value::Pointer(ctor_defn_name.clone());
|
||||
dependent_definitions.push(Definition {
|
||||
pointer: ctor_defn_name.clone(),
|
||||
content: DefinitionContent::Function(Function {
|
||||
parameters: vec![],
|
||||
body: member_initializers_assembly,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
defn.push(format!(
|
||||
"{} = class({}, {{",
|
||||
defn_name,
|
||||
match constructor_defn_name {
|
||||
None => "void".to_string(),
|
||||
Some(d) => d.to_string(),
|
||||
},
|
||||
));
|
||||
|
||||
for class_member in &class_decl.class.body {
|
||||
use swc_ecma_ast::ClassMember::*;
|
||||
|
||||
@@ -886,7 +869,9 @@ impl Compiler {
|
||||
parent_scope,
|
||||
);
|
||||
|
||||
defn.push(format!(" {}: {},", Value::String(name), method_defn_name,));
|
||||
methods
|
||||
.properties
|
||||
.push((Value::String(name), Value::Pointer(method_defn_name)));
|
||||
}
|
||||
PrivateMethod(private_method) => {
|
||||
self.diagnostics.push(Diagnostic {
|
||||
@@ -921,8 +906,14 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
defn.push("})".to_string());
|
||||
self.definitions.push(Definition {
|
||||
pointer: defn_name,
|
||||
content: DefinitionContent::Class(Class {
|
||||
constructor,
|
||||
methods: Value::Object(Box::new(methods)),
|
||||
}),
|
||||
});
|
||||
|
||||
self.definitions.push(defn);
|
||||
self.definitions.append(&mut dependent_definitions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1157,7 +1157,7 @@ impl<'a> ExpressionCompiler<'a> {
|
||||
match &qfn.functionish {
|
||||
Functionish::Fn(fn_) => fn_.span,
|
||||
Functionish::Arrow(arrow) => arrow.span,
|
||||
Functionish::Constructor(_, constructor) => constructor.span,
|
||||
Functionish::Constructor(constructor) => constructor.span,
|
||||
},
|
||||
&qfn.definition_pointer,
|
||||
&qfn.capture_params,
|
||||
@@ -1313,7 +1313,7 @@ impl<'a> ExpressionCompiler<'a> {
|
||||
match &qfn.functionish {
|
||||
Functionish::Fn(fn_) => fn_.span,
|
||||
Functionish::Arrow(arrow) => arrow.span,
|
||||
Functionish::Constructor(_, constructor) => constructor.span,
|
||||
Functionish::Constructor(constructor) => constructor.span,
|
||||
},
|
||||
&qfn.definition_pointer,
|
||||
&qfn.capture_params,
|
||||
|
||||
@@ -5,7 +5,10 @@ use std::rc::Rc;
|
||||
|
||||
use swc_common::Spanned;
|
||||
|
||||
use crate::asm::{Instruction, Label, Pointer, Register, Value};
|
||||
use crate::asm::{
|
||||
Definition, DefinitionContent, Function, Instruction, InstructionOrLabel, Label, Pointer,
|
||||
Register, Value,
|
||||
};
|
||||
use crate::scope::scope_reg;
|
||||
|
||||
use super::capture_finder::CaptureFinder;
|
||||
@@ -19,7 +22,7 @@ use super::scope::{init_std_scope, MappedName, Scope};
|
||||
pub enum Functionish {
|
||||
Fn(swc_ecma_ast::Function),
|
||||
Arrow(swc_ecma_ast::ArrowExpr),
|
||||
Constructor(Vec<String>, swc_ecma_ast::Constructor),
|
||||
Constructor(swc_ecma_ast::Constructor),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -36,7 +39,8 @@ pub struct LoopLabels {
|
||||
}
|
||||
|
||||
pub struct FunctionCompiler {
|
||||
pub lines: Vec<String>,
|
||||
pub current: Function,
|
||||
pub definitions: Vec<Definition>,
|
||||
pub definition_allocator: Rc<RefCell<NameAllocator>>,
|
||||
pub reg_allocator: NameAllocator,
|
||||
pub label_allocator: NameAllocator,
|
||||
@@ -53,7 +57,8 @@ impl FunctionCompiler {
|
||||
reg_allocator.allocate(&"ignore".to_string());
|
||||
|
||||
return FunctionCompiler {
|
||||
lines: vec![],
|
||||
current: Function::default(),
|
||||
definitions: vec![],
|
||||
definition_allocator,
|
||||
reg_allocator,
|
||||
label_allocator: NameAllocator::default(),
|
||||
@@ -64,11 +69,14 @@ impl FunctionCompiler {
|
||||
}
|
||||
|
||||
pub fn push(&mut self, instruction: Instruction) {
|
||||
self.lines.push(format!(" {}", instruction));
|
||||
self
|
||||
.current
|
||||
.body
|
||||
.push(InstructionOrLabel::Instruction(instruction));
|
||||
}
|
||||
|
||||
pub fn label(&mut self, label: Label) {
|
||||
self.lines.push(format!("{}", label));
|
||||
self.current.body.push(InstructionOrLabel::Label(label));
|
||||
}
|
||||
|
||||
pub fn todo(&mut self, span: swc_common::Span, message: &str) {
|
||||
@@ -134,7 +142,7 @@ impl FunctionCompiler {
|
||||
functionish: Functionish,
|
||||
definition_allocator: Rc<RefCell<NameAllocator>>,
|
||||
parent_scope: &Scope,
|
||||
) -> (Vec<String>, Vec<Diagnostic>) {
|
||||
) -> (Vec<Definition>, Vec<Diagnostic>) {
|
||||
let mut self_ = FunctionCompiler::new(definition_allocator);
|
||||
|
||||
self_
|
||||
@@ -149,7 +157,7 @@ impl FunctionCompiler {
|
||||
|
||||
self_.process_queue(parent_scope);
|
||||
|
||||
return (self_.lines, self_.diagnostics);
|
||||
return (self_.definitions, self_.diagnostics);
|
||||
}
|
||||
|
||||
pub fn process_queue(&mut self, parent_scope: &Scope) {
|
||||
@@ -188,22 +196,11 @@ impl FunctionCompiler {
|
||||
None => {}
|
||||
}
|
||||
|
||||
let mut heading = format!("{} = function(", definition_pointer);
|
||||
|
||||
let mut param_count = 0;
|
||||
|
||||
for cap_param in &capture_params {
|
||||
if param_count != 0 {
|
||||
heading += ", ";
|
||||
}
|
||||
|
||||
let reg = self.allocate_reg(cap_param);
|
||||
|
||||
heading += ®.to_string();
|
||||
|
||||
self.current.parameters.push(reg.clone());
|
||||
scope.set(cap_param.clone(), MappedName::Register(reg));
|
||||
|
||||
param_count += 1;
|
||||
}
|
||||
|
||||
self.populate_fn_scope_params(functionish, &scope);
|
||||
@@ -211,18 +208,9 @@ impl FunctionCompiler {
|
||||
let param_registers = self.allocate_param_registers(functionish, &scope);
|
||||
|
||||
for reg in ¶m_registers {
|
||||
if param_count != 0 {
|
||||
heading += ", ";
|
||||
}
|
||||
|
||||
heading += ®.to_string();
|
||||
param_count += 1;
|
||||
self.current.parameters.push(reg.clone());
|
||||
}
|
||||
|
||||
heading += ") {";
|
||||
|
||||
self.lines.push(heading);
|
||||
|
||||
self.add_param_code(functionish, ¶m_registers, &scope);
|
||||
|
||||
let mut handle_block_body = |block: &swc_ecma_ast::BlockStmt| {
|
||||
@@ -259,7 +247,7 @@ impl FunctionCompiler {
|
||||
expression_compiler.compile(expr, Some(Register::Return));
|
||||
}
|
||||
},
|
||||
Functionish::Constructor(_, constructor) => {
|
||||
Functionish::Constructor(constructor) => {
|
||||
match &constructor.body {
|
||||
Some(block) => {
|
||||
handle_block_body(block);
|
||||
@@ -267,9 +255,12 @@ impl FunctionCompiler {
|
||||
None => self.todo(constructor.span(), "constructor without body"),
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.lines.push("}".to_string());
|
||||
self.definitions.push(Definition {
|
||||
pointer: definition_pointer,
|
||||
content: DefinitionContent::Function(std::mem::take(&mut self.current)),
|
||||
});
|
||||
}
|
||||
|
||||
fn populate_fn_scope_params(&mut self, functionish: &Functionish, scope: &Scope) {
|
||||
@@ -284,7 +275,7 @@ impl FunctionCompiler {
|
||||
self.populate_scope_pat(p, scope);
|
||||
}
|
||||
}
|
||||
Functionish::Constructor(_, constructor) => {
|
||||
Functionish::Constructor(constructor) => {
|
||||
for potspp in &constructor.params {
|
||||
match potspp {
|
||||
swc_ecma_ast::ParamOrTsParamProp::TsParamProp(ts_param_prop) => {
|
||||
@@ -333,7 +324,7 @@ impl FunctionCompiler {
|
||||
param_registers.push(self.get_pattern_register(p, scope));
|
||||
}
|
||||
}
|
||||
Functionish::Constructor(_, constructor) => {
|
||||
Functionish::Constructor(constructor) => {
|
||||
for potspp in &constructor.params {
|
||||
match potspp {
|
||||
swc_ecma_ast::ParamOrTsParamProp::TsParamProp(ts_param_prop) => {
|
||||
@@ -402,7 +393,7 @@ impl FunctionCompiler {
|
||||
ec.pat(p, ¶m_registers[i], false, scope);
|
||||
}
|
||||
}
|
||||
Functionish::Constructor(_, constructor) => {
|
||||
Functionish::Constructor(constructor) => {
|
||||
for (i, potspp) in constructor.params.iter().enumerate() {
|
||||
match potspp {
|
||||
swc_ecma_ast::ParamOrTsParamProp::TsParamProp(_) => {
|
||||
|
||||
Reference in New Issue
Block a user