1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use dfwasm_template::{Args, Block};
use wasmparser::Operator;
use crate::{
DFWasmCompiler, DFWasmResult,
df_helper::{
DF_FUNC_CALL_FUNC, DF_VAR_STORE_GLOBALS, DF_VAR_STORE_TABLES, TemplateExt, get_local_name,
num, var,
},
};
pub fn compile_basic_operator(
compiler: &mut DFWasmCompiler,
operator: Operator,
_location: usize,
) -> DFWasmResult<()> {
let template = compiler.get_current_template();
match operator {
Operator::Nop => {
template.add_block(Block::EntityAction {
args: Args::default(),
action: String::new(),
target: None,
});
}
Operator::Call { function_index } => {
// Call: Calls a function by its index.
// Since you can import functions, we defer to DiamondFire to
// handle finding the correct function.
let signature = compiler.function_to_type_signature[function_index as usize].as_ref();
let arg_count = DFWasmCompiler::arg_count_of_type(signature).expect("Not a function?");
let result_count =
DFWasmCompiler::result_count_of_type(signature).expect("Not a function?");
// get the template reference again due to borrow checker
let template = compiler.get_current_template();
template.call_function(
DF_FUNC_CALL_FUNC,
Args::with(vec![
num(function_index.to_string()),
num(arg_count),
num(result_count),
]),
);
}
Operator::CallIndirect {
type_index,
table_index,
} => {
// CallIndirect: Pop an index to call into a function table.
let signature = &compiler.function_signatures[type_index as usize];
let arg_count = DFWasmCompiler::arg_count_of_type(signature).expect("Not a function?");
let res_count =
DFWasmCompiler::result_count_of_type(signature).expect("Not a function?");
// get the template reference again due to borrow checker
let template = compiler.get_current_template();
template
.pop_op_stack(var("$index"))
.set_var(
"GetListValue",
Args::with(vec![
var("$table_ptr"),
var(DF_VAR_STORE_TABLES),
num(table_index + 1),
]),
)
.set_var(
"GetListValue",
Args::with(vec![
var("$func_idx"),
var("%var($table_ptr)"),
num("%math(1000*%var($index)+1)"),
]),
)
.call_function(
DF_FUNC_CALL_FUNC,
Args::with(vec![var("$func_idx"), num(arg_count), num(res_count)]),
);
}
Operator::Drop => {
// Drop: Pops a value from the stack and does nothing.
// i.e. Noop, but also pops a value.
template.pop_op_stack(var("$_"));
}
Operator::Select | Operator::TypedSelect { .. } => {
// Select: Pops three values from the stack and pushes one of them based on a condition.
// stack: $if_true, $if_false, $cond -> $result
template
.pop_op_stack(var("$cond"))
.pop_op_stack(var("$if_false"))
.pop_op_stack(var("$if_true"))
.if_var("!=", Args::with(vec![var("$cond"), num("0")]))
.open_bracket()
.push_op_stack(var("$if_true"))
.close_bracket()
.else_block()
.open_bracket()
.push_op_stack(var("$if_false"))
.close_bracket();
}
Operator::LocalGet { local_index } => {
// LocalGet: Gets a local variable by its index and pushes it to the stack.
let local_var_name = get_local_name(local_index);
template.push_op_stack(var(local_var_name));
}
Operator::LocalSet { local_index } => {
// LocalSet: Pops a value from the stack and sets it to a local variable.
let local_var_name = get_local_name(local_index);
template
.pop_op_stack(var("$value"))
.set_var("=", Args::with(vec![var(local_var_name), var("$value")]));
}
Operator::LocalTee { local_index } => {
// LocalTee: Pops a value from the stack and sets it to a local variable, but also
// keeps the value on the stack
let local_var_name = get_local_name(local_index);
template
.pop_op_stack(var("$tee"))
.push_op_stack(var("$tee"))
.set_var("=", Args::with(vec![var(local_var_name), var("$tee")]));
}
Operator::GlobalGet { global_index } => {
// GlobalGet: Gets a global variable by its index and pushes it to the stack.
// todo: make globals pointers to better allow for imports
template
.set_var(
"GetListValue",
Args::with(vec![
var("$value"),
var(DF_VAR_STORE_GLOBALS),
num(global_index + 1),
]),
)
.push_op_stack(var("$value"));
}
Operator::GlobalSet { global_index } => {
// GlobalSet: Pops a value from the stack and sets it to a global variable.
template.pop_op_stack(var("$value")).set_var(
"SetListValue",
Args::with(vec![
var(DF_VAR_STORE_GLOBALS),
num(global_index + 1),
var("$value"),
]),
);
}
_ => unreachable!(),
}
Ok(())
}