Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pufferfish101007 committed Aug 27, 2023
1 parent 7ed7848 commit 2c445c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
28 changes: 10 additions & 18 deletions src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ impl IrOpcode {
| math_positive_number { .. } => BlockDescriptor::new(vec![], Number),
data_variable { .. } => BlockDescriptor::new(vec![], Any),
data_setvariableto { .. } => BlockDescriptor::new(vec![Any], Stack),
//data_changevariableby { .. } => BlockDescriptor::new(vec![Number], Stack),
text { .. } => BlockDescriptor::new(vec![], Text),
operator_lt | operator_gt => BlockDescriptor::new(vec![Number, Number], Boolean),
operator_equals | operator_contains => BlockDescriptor::new(vec![Any, Any], Boolean),
Expand Down Expand Up @@ -489,7 +488,7 @@ impl Step {
pub struct ThreadContext {
pub target_index: u32,
pub dbg: bool,
pub vars: Rc<Vec<IrVar>>, // hopefully there can't be two variables with the same id in differwnt sprites, otherwise this will break horrendously
pub vars: Rc<Vec<IrVar>>, // todo: fix variable id collisions between targets
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -637,7 +636,6 @@ impl IrBlockVec for Vec<IrBlock> {
let block = blocks.get(&block_id).unwrap();
match block {
Block::Normal { block_info, .. } => {
//println!("{}: {:?}", &block_id, &block_info.opcode);
self.add_inputs(&block_info.inputs, blocks, Rc::clone(&context), steps);

self.append(&mut (match block_info.opcode {
Expand Down Expand Up @@ -733,32 +731,26 @@ impl IrBlockVec for Vec<IrBlock> {
BlockOpcode::control_repeat => vec![IrOpcode::hq_drop(1)],
BlockOpcode::control_repeat_until => {
let substack_id = if let BlockArrayOrId::Id(id) = block_info.inputs.get("SUBSTACK").expect("missing SUBSTACK input for control_if").get_1().unwrap().clone().unwrap() { id } else { panic!("malformed SUBSTACK input") };
let condition_opcodes = vec![
IrOpcode::hq_goto_if { step: Some(block_info.next.clone().unwrap()), does_yield: false }.into(),
IrOpcode::hq_goto { step: Some(substack_id.clone()), does_yield: false }.into(),
];
let looper_id = block_id.clone() + &mut block_id.clone();
if !steps.contains_key(&looper_id) {
let mut looper_opcodes = vec![];
looper_opcodes.add_inputs(&block_info.inputs, blocks, Rc::clone(&context), steps);
looper_opcodes.append(&mut vec![
IrOpcode::hq_goto_if { step: Some(block_info.next.clone().unwrap()), does_yield: false }.into(),
IrOpcode::hq_goto { step: Some(substack_id.clone()), does_yield: false }.into(),
]);
looper_opcodes.append(&mut condition_opcodes.clone());
looper_opcodes.fixup_types();
steps.insert(looper_id.clone(), Step::new(looper_opcodes, Rc::clone(&context)));
}
step_from_top_block(block_info.next.clone().unwrap(), last_nexts.clone(), blocks, Rc::clone(&context), steps);
step_from_top_block(substack_id.clone(), vec![looper_id], blocks, Rc::clone(&context), steps);
step_from_top_block(block_info.next.clone().unwrap(), last_nexts, blocks, Rc::clone(&context), steps);
step_from_top_block(substack_id, vec![looper_id], blocks, Rc::clone(&context), steps);
let mut opcodes = vec![];
opcodes.add_inputs(&block_info.inputs, blocks, Rc::clone(&context), steps);
opcodes.append(&mut vec![
IrOpcode::hq_goto_if { step: Some(block_info.next.clone().unwrap()), does_yield: false }.into(),
IrOpcode::hq_goto { step: Some(substack_id.clone()), does_yield: false }.into(),
]);
opcodes.append(&mut condition_opcodes.clone());
opcodes.fixup_types();
steps.insert(block_id.clone(), Step::new(opcodes.clone(), Rc::clone(&context)));
//step_from_top_block(block_id.clone(), last_nexts.clone(), blocks, Rc::clone(&context), steps);
vec![
IrOpcode::hq_goto_if { step: Some(block_info.next.clone().unwrap()), does_yield: false }.into(),
IrOpcode::hq_goto { step: Some(substack_id.clone()), does_yield: false }.into(),
]
condition_opcodes.into_iter().map(|block| block.opcode().clone()).collect::<_>()
}
_ => todo!(),
}).into_iter().map(IrBlock::from).collect());
Expand Down
20 changes: 8 additions & 12 deletions src/targets/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use wasm_encoder::{
fn instructions(
op: &IrBlock,
context: Rc<ThreadContext>,
_step_funcs: &mut IndexMap<Option<String>, Function, BuildHasherDefault<FNV1aHasher64>>,
string_consts: &mut Vec<String>,
steps: &IndexMap<String, Step, BuildHasherDefault<FNV1aHasher64>>,
) -> Vec<Instruction<'static>> {
Expand Down Expand Up @@ -248,7 +247,6 @@ fn instructions(
} => {
let _next_step = steps.get(next_step_id).unwrap();
let next_step_index = steps.get_index_of(next_step_id).unwrap();
//(next_step_id, next_step).compile_wasm(step_funcs, string_consts, steps);
let thread_indices: u64 = byte_offset::THREADS
.try_into()
.expect("THREAD_INDICES out of bounds (E018)");
Expand All @@ -274,7 +272,6 @@ fn instructions(
} => {
let _next_step = steps.get(next_step_id).unwrap();
let next_step_index = steps.get_index_of(next_step_id).unwrap();
//(next_step_id, next_step).compile_wasm(step_funcs, string_consts, steps);
vec![
LocalGet(step_func_locals::MEM_LOCATION),
I32Const(
Expand All @@ -298,7 +295,7 @@ fn instructions(

vec![
I32WrapI64,
If(WasmBlockType::Empty), //WasmBlockType::FunctionType(types::NOPARAM_I32)),
If(WasmBlockType::Empty),
LocalGet(0),
I32Const(byte_offset::THREADS),
I32Add, // destination (= current thread pos in memory)
Expand Down Expand Up @@ -352,13 +349,12 @@ fn instructions(
} => {
let _next_step = steps.get(next_step_id).unwrap();
let next_step_index = steps.get_index_of(next_step_id).unwrap();
//(next_step_id, next_step).compile_wasm(step_funcs, string_consts, steps);
let thread_indices: u64 = byte_offset::THREADS
.try_into()
.expect("THREAD_INDICES out of bounds (E018)");
vec![
I32WrapI64,
If(WasmBlockType::Empty), //WasmBlockType::FunctionType(types::NOPARAM_I32)),
If(WasmBlockType::Empty),
LocalGet(0),
I32Const(
next_step_index
Expand All @@ -381,10 +377,9 @@ fn instructions(
} => {
let _next_step = steps.get(next_step_id).unwrap();
let next_step_index = steps.get_index_of(next_step_id).unwrap();
//(next_step_id, next_step).compile_wasm(step_funcs, string_consts, steps);
vec![
I32WrapI64,
If(WasmBlockType::Empty), //WasmBlockType::FunctionType(types::NOPARAM_I32)),
If(WasmBlockType::Empty),
LocalGet(step_func_locals::MEM_LOCATION),
I32Const(
next_step_index
Expand Down Expand Up @@ -477,7 +472,7 @@ impl CompileToWasm for (&String, &Step) {
];
let mut func = Function::new_with_locals_types(locals);
for op in self.1.opcodes() {
let instrs = instructions(op, self.1.context(), step_funcs, string_consts, steps);
let instrs = instructions(op, self.1.context(), string_consts, steps);
for instr in instrs {
func.instruction(&instr);
}
Expand Down Expand Up @@ -519,7 +514,7 @@ pub mod func_indices {
pub const DBG_ASSERT: u32 = 1;
pub const LOOKS_SAY: u32 = 2;
pub const LOOKS_THINK: u32 = 3;
pub const CAST_PRIMITIVE_FLOAT_STRING: u32 = 4; // js functions can only rwturm 1 value so need wrapper functions for casting
pub const CAST_PRIMITIVE_FLOAT_STRING: u32 = 4; // js functions can only return 1 value so need wrapper functions for casting
pub const CAST_PRIMITIVE_STRING_FLOAT: u32 = 5;
pub const CAST_PRIMITIVE_STRING_BOOL: u32 = 6;
pub const DBG_LOGI32: u32 = 7;
Expand Down Expand Up @@ -987,7 +982,7 @@ impl From<IrProject> for WebWasmFile {

for step in &project.steps {
// make sure to skip the 0th (noop) step because we've added the noop step function 3 lines above
if step.0 == "" {
if step.0.is_empty() {
continue;
};
step.compile_wasm(&mut step_funcs, &mut string_consts, &project.steps);
Expand Down Expand Up @@ -1357,7 +1352,8 @@ mod tests {
let ir: IrProject = proj.into();
let wasm: WebWasmFile = ir.into();
fs::write("./bad.wasm", wasm.wasm_bytes()).expect("failed to write to bad.wasm");
fs::write("./bad.mjs", format!("export default {};", wasm.js_string())).expect("failed to write to bad.js");
fs::write("./bad.mjs", format!("export default {};", wasm.js_string()))
.expect("failed to write to bad.js");
let output = Command::new("node")
.arg("-e")
.arg(format!("({})().catch(console.error)", wasm.js_string()))
Expand Down

0 comments on commit 2c445c1

Please sign in to comment.