Skip to content

Commit

Permalink
fix everything
Browse files Browse the repository at this point in the history
  • Loading branch information
pufferfish101007 committed Mar 16, 2024
1 parent 82e1508 commit fe36391
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
12 changes: 6 additions & 6 deletions playground/lib/project-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ export default async (
operator_join: (str1, str2) =>
str1.toString() +
str2.toString(),
operator_letterof: (idx, ty, val) =>
wasm_val_to_js(ty, val).toString()[idx - 1] ?? "",
operator_letterof: (idx, str) =>
str.toString()[idx - 1] ?? "",
operator_length: (str) => str.length,
operator_contains: (ty1, val1, ty2, val2) =>
wasm_val_to_js(ty1, val1)
operator_contains: (str1, str2) =>
str1
.toString()
.toLowerCase()
.includes(wasm_val_to_js(ty2, val2).toString().toLowerCase()),
.includes(str2.toString().toLowerCase()),
mathop_sin: (n) =>
parseFloat(Math.sin((Math.PI * n) / 180).toFixed(10)),
mathop_cos: (n) =>
Expand Down Expand Up @@ -285,7 +285,7 @@ export default async (
cast: {
stringtofloat: parseFloat,
stringtobool: Boolean,
floattostring: Number.prototype.toString,
floattostring: (i) => i.toString(),
},
};
try {
Expand Down
31 changes: 17 additions & 14 deletions src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,12 @@ impl IrBlock {
opcode,
);
}
let arity = expected_inputs.len();
for i in 0..expected_inputs.len() {
let expected = expected_inputs.get(i).ok_or(make_hq_bug!(""))?;
let actual = &type_stack.get(i).borrow().clone().unwrap().1;
let actual = &type_stack.get(arity - 1 - i).borrow().clone().unwrap().1;
if !expected.includes(actual) {
add_cast(i, expected);
add_cast(arity - 1 - i, expected);
}
}
let output_stack = opcode.output(type_stack)?;
Expand All @@ -445,10 +446,11 @@ impl IrBlock {
type_stack.len()
);
}
let arity = expected_inputs.len();
for i in 0..expected_inputs.len() {
let expected = expected_inputs.get(i).ok_or(make_hq_bug!(""))?;
let actual = &type_stack
.get(i)
.get(arity - 1 - i)
.borrow()
.clone()
.ok_or(make_hq_bug!(""))?
Expand Down Expand Up @@ -623,9 +625,10 @@ impl IrOpcode {
type_stack.len()
);
}
let arity = expected_inputs.len();
let get_input = |i| {
Ok(type_stack
.get(i)
.get(arity - 1 - i)
.borrow()
.clone()
.ok_or(make_hq_bug!(""))?
Expand Down Expand Up @@ -1350,8 +1353,7 @@ impl IrBlockVec for Vec<IrBlock> {
),
)?);
}
for op in condition_opcodes.iter()
{
for op in condition_opcodes.iter() {
looper_opcodes.push(IrBlock::new_with_stack_no_cast(
op.clone(),
Rc::clone(
Expand Down Expand Up @@ -1390,11 +1392,12 @@ impl IrBlockVec for Vec<IrBlock> {
target_id.clone(),
)?;
for op in [
IrOpcode::math_whole_number { NUM: 1.0 },
IrOpcode::operator_lt,
]
.into_iter()
.chain(condition_opcodes.clone().into_iter()) {
IrOpcode::math_whole_number { NUM: 1.0 },
IrOpcode::operator_lt,
]
.into_iter()
.chain(condition_opcodes.clone().into_iter())
{
opcodes.push(IrBlock::new_with_stack_no_cast(
op,
Rc::clone(&opcodes.last().ok_or(make_hq_bug!(""))?.type_stack),
Expand All @@ -1411,7 +1414,7 @@ impl IrBlockVec for Vec<IrBlock> {
IrOpcode::data_teevariable {
VARIABLE: looper_id,
},
IrOpcode::hq_cast(Unknown, ConcreteInteger),
IrOpcode::hq_cast(Unknown, Float),
IrOpcode::math_number { NUM: 1.0 },
IrOpcode::operator_lt,
IrOpcode::hq_goto_if {
Expand Down Expand Up @@ -1502,7 +1505,7 @@ impl IrBlockVec for Vec<IrBlock> {
steps,
target_id.clone(),
)?;
for op in condition_opcodes.clone().into_iter() {
for op in condition_opcodes.into_iter() {
opcodes.push(IrBlock::new_with_stack_no_cast(
op,
Rc::clone(&opcodes.last().ok_or(make_hq_bug!(""))?.type_stack),
Expand Down Expand Up @@ -1584,7 +1587,7 @@ impl IrBlockVec for Vec<IrBlock> {
)?;
let mut opcodes = vec![];
//opcodes.add_inputs(&block_info.inputs, blocks, Rc::clone(&context), steps, target_id.clone());
for op in condition_opcodes.clone().into_iter() {
for op in condition_opcodes.into_iter() {
opcodes.push(IrBlock::new_with_stack_no_cast(
op,
Rc::new(RefCell::new(None)),
Expand Down
44 changes: 38 additions & 6 deletions src/targets/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn instructions(
operator_divide => vec![F64Div],
operator_multiply => vec![F64Mul], // todo: int opt
operator_mod => vec![Call(func_indices::FMOD)], // todo: int opt
operator_round => vec![F64Nearest, I32TruncF64S],
operator_round => vec![F64Nearest, I64TruncF64S],
math_number { NUM }
| math_integer { NUM }
| math_angle { NUM }
Expand Down Expand Up @@ -247,7 +247,7 @@ fn instructions(
operator_random => vec![Call(func_indices::OPERATOR_RANDOM)],
operator_join => vec![Call(func_indices::OPERATOR_JOIN)],
operator_letter_of => vec![Call(func_indices::OPERATOR_LETTEROF)],
operator_length => vec![Call(func_indices::OPERATOR_LENGTH)],
operator_length => vec![Call(func_indices::OPERATOR_LENGTH), I64ExtendI32S],
operator_contains => vec![Call(func_indices::OPERATOR_CONTAINS)],
operator_mathop { OPERATOR } => match OPERATOR.as_str() {
"abs" => vec![F64Abs],
Expand Down Expand Up @@ -1224,6 +1224,8 @@ pub mod types {
pub const F64x3F32x4_NORESULT: u32 = 37;
pub const F64x5F32x4_NORESULT: u32 = 38;
pub const EXTERNREFx2_EXTERNREF: u32 = 39;
pub const EXTERNREFx2_I64: u32 = 40;
pub const F64EXTERNREF_EXTERNREF: u32 = 41;
}

pub mod table_indices {
Expand Down Expand Up @@ -1377,7 +1379,27 @@ impl TryFrom<IrProject> for WasmProject {
],
[],
);
types.function([ValType::Ref(RefType::EXTERNREF), ValType::Ref(RefType::EXTERNREF)], [ValType::Ref(RefType::EXTERNREF)]);
types.function(
[
ValType::Ref(RefType::EXTERNREF),
ValType::Ref(RefType::EXTERNREF),
],
[ValType::Ref(RefType::EXTERNREF)],
);
types.function(
[
ValType::Ref(RefType::EXTERNREF),
ValType::Ref(RefType::EXTERNREF),
],
[ValType::I64],
);
types.function(
[
ValType::F64,
ValType::Ref(RefType::EXTERNREF),
],
[ValType::Ref(RefType::EXTERNREF)],
);

imports.import("dbg", "log", EntityType::Function(types::I32I64_NORESULT));
imports.import(
Expand Down Expand Up @@ -1429,17 +1451,17 @@ impl TryFrom<IrProject> for WasmProject {
imports.import(
"runtime",
"operator_letterof",
EntityType::Function(types::F64I32I64_EXTERNREF),
EntityType::Function(types::F64EXTERNREF_EXTERNREF),
);
imports.import(
"runtime",
"operator_length",
EntityType::Function(types::EXTERNREF_F64),
EntityType::Function(types::EXTERNREF_I32),
);
imports.import(
"runtime",
"operator_contains",
EntityType::Function(types::I32I64I32I64_I64),
EntityType::Function(types::EXTERNREFx2_I64),
);
imports.import(
"runtime",
Expand Down Expand Up @@ -1699,11 +1721,21 @@ impl TryFrom<IrProject> for WasmProject {
any2float_func.instruction(&Instruction::LocalGet(1));
any2float_func.instruction(&Instruction::F64ReinterpretI64);
any2float_func.instruction(&Instruction::Else);
any2float_func.instruction(&Instruction::LocalGet(0));
any2float_func.instruction(&Instruction::I32Const(hq_value_types::INT64));
any2float_func.instruction(&Instruction::I32Eq);
any2float_func.instruction(&Instruction::If(WasmBlockType::FunctionType(
types::NOPARAM_F64,
)));
any2float_func.instruction(&Instruction::LocalGet(1));
any2float_func.instruction(&Instruction::F64ConvertI64S);
any2float_func.instruction(&Instruction::Else);
any2float_func.instruction(&Instruction::Unreachable);
any2float_func.instruction(&Instruction::End);
any2float_func.instruction(&Instruction::End);
any2float_func.instruction(&Instruction::End);
any2float_func.instruction(&Instruction::End);
any2float_func.instruction(&Instruction::End);
code.function(&any2float_func);

functions.function(types::I32I64_I64);
Expand Down

0 comments on commit fe36391

Please sign in to comment.