Add function(uses_this) to kal

This commit is contained in:
Andrew Morris
2023-07-25 17:34:01 +10:00
parent b300428a38
commit c1fca06f23
2 changed files with 35 additions and 3 deletions

View File

@@ -12,7 +12,7 @@ use std::{
};
use crate::{
asm::{self, Builtin, Function, Number, Pointer, Register, Value},
asm::{self, Builtin, FnLine, Function, Number, Pointer, Register, Value},
instruction::Instruction,
name_allocator::RegAllocator,
};
@@ -47,6 +47,7 @@ pub enum Kal {
String(String),
Array(Box<Array>),
Object(Box<Object>),
Function(KFunction),
Class(Box<Class>),
Register(Register),
Pointer(Pointer),
@@ -63,6 +64,11 @@ pub struct Object {
pub properties: Vec<(Kal, Kal)>,
}
#[derive(Clone)]
pub struct KFunction {
pub uses_this: bool,
}
#[derive(Clone)]
pub struct Class {
pub constructor: Kal,
@@ -89,6 +95,7 @@ impl Kal {
v.visit_kals_mut(visit);
}
}
Kal::Function(_) => {}
Kal::Class(class) => {
class.constructor.visit_kals_mut(visit);
class.prototype.visit_kals_mut(visit);
@@ -138,6 +145,22 @@ impl Kal {
}
}
pub fn from_function(fn_: &mut Function) -> Kal {
let mut uses_this = false;
for line in &mut fn_.body {
if let FnLine::Instruction(instr) = line {
instr.visit_registers_mut_rev(&mut |rvm| {
if rvm.register.is_this() {
uses_this = true;
}
});
}
}
Kal::Function(KFunction { uses_this })
}
fn try_to_value(&self) -> Option<Value> {
match self {
Kal::Unknown => None,
@@ -183,6 +206,7 @@ impl Kal {
properties
},
}))),
Kal::Function(_) => None,
Kal::Class(class) => Some(Value::Class(Box::new(asm::Class {
constructor: class.constructor.try_to_value()?,
prototype: class.prototype.try_to_value()?,
@@ -226,6 +250,7 @@ impl Kal {
}
.to_val()
}
Kal::Function(_) => return None,
Kal::Class(class) => VsClass {
constructor: class.constructor.try_to_val()?,
prototype: class.prototype.try_to_val()?,
@@ -252,6 +277,7 @@ impl Kal {
Kal::String(s) => Some(s.clone()),
Kal::Array(_) => None,
Kal::Object(_) => None,
Kal::Function(_) => None,
Kal::Class(_) => None,
Kal::Register(_) => None,
Kal::Pointer(_) => None,

View File

@@ -8,8 +8,14 @@ pub fn simplify(module: &mut Module, take_registers: bool) {
let mut pointer_kals = HashMap::<Pointer, Kal>::new();
for defn in &mut module.definitions {
if let DefinitionContent::Value(value) = &defn.content {
pointer_kals.insert(defn.pointer.clone(), Kal::from_value(value));
match &mut defn.content {
DefinitionContent::Function(fn_) => {
pointer_kals.insert(defn.pointer.clone(), Kal::from_function(fn_));
}
DefinitionContent::Value(value) => {
pointer_kals.insert(defn.pointer.clone(), Kal::from_value(value));
}
DefinitionContent::Lazy(_) => {}
}
}