mirror of
https://github.com/exoticorn/curlywas.git
synced 2026-01-20 11:46:43 +01:00
add load/store intrinsics
This commit is contained in:
16
examples/microw8/mem_intrinsics.cwa
Normal file
16
examples/microw8/mem_intrinsics.cwa
Normal file
@@ -0,0 +1,16 @@
|
||||
import "env.memory" memory(4);
|
||||
import "env.random" fn random() -> i32;
|
||||
|
||||
export fn upd() {
|
||||
let i: i32;
|
||||
loop pixels {
|
||||
let inline left = i32.load8_u((i + (320*240 - 319)) % (320*240), 120);
|
||||
let inline top = i32.load8_u((i + 320*239) % (320*240), 120, 0);
|
||||
let inline here = i32.load16_u(i, 119);
|
||||
let inline all = (left << 24) | (top << 16) | here;
|
||||
let lazy r = random();
|
||||
let inline new = (all #>> ((r & 3) * 8)) ^ ((r & 31) * !(r #>> 22));
|
||||
i32.store8(new, i, 120);
|
||||
branch_if (i := i + 1) < 320*240: pixels;
|
||||
}
|
||||
}
|
||||
@@ -337,6 +337,7 @@ pub enum BinOp {
|
||||
pub enum MemSize {
|
||||
Byte,
|
||||
Word,
|
||||
Float
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
|
||||
69
src/emit.rs
69
src/emit.rs
@@ -6,7 +6,11 @@ use wasm_encoder::{
|
||||
MemArg, MemoryType, Module, NameMap, NameSection, StartSection, TypeSection, ValType,
|
||||
};
|
||||
|
||||
use crate::{ast, intrinsics::Intrinsics, Options};
|
||||
use crate::{
|
||||
ast,
|
||||
intrinsics::{Intrinsics, MemInstruction},
|
||||
Options,
|
||||
};
|
||||
|
||||
pub fn emit(script: &ast::Script, module_name: &str, options: &Options) -> Vec<u8> {
|
||||
let mut module = Module::new();
|
||||
@@ -65,9 +69,7 @@ pub fn emit(script: &ast::Script, module_name: &str, options: &Options) -> Vec<u
|
||||
} => {
|
||||
function_map.insert(name.clone(), function_map.len() as u32);
|
||||
EntityType::Function(
|
||||
*function_types
|
||||
.get(&(params.clone(), *result))
|
||||
.unwrap() as u32,
|
||||
*function_types.get(&(params.clone(), *result)).unwrap() as u32
|
||||
)
|
||||
}
|
||||
};
|
||||
@@ -254,9 +256,7 @@ fn collect_function_types(script: &ast::Script) -> HashMap<FunctionTypeKey, usiz
|
||||
} = import.type_
|
||||
{
|
||||
let index = types.len();
|
||||
types
|
||||
.entry((params.clone(), *result))
|
||||
.or_insert(index);
|
||||
types.entry((params.clone(), *result)).or_insert(index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,6 +346,11 @@ fn mem_arg_for_location(mem_location: &ast::MemoryLocation) -> MemArg {
|
||||
memory_index: 0,
|
||||
offset,
|
||||
},
|
||||
ast::MemSize::Float => MemArg {
|
||||
align: 2,
|
||||
memory_index: 0,
|
||||
offset,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,6 +396,7 @@ fn emit_expression<'a>(ctx: &mut FunctionContext<'a>, expr: &'a ast::Expression)
|
||||
ctx.function.instruction(&match mem_location.size {
|
||||
ast::MemSize::Byte => Instruction::I32Load8_U(mem_arg),
|
||||
ast::MemSize::Word => Instruction::I32Load(mem_arg),
|
||||
ast::MemSize::Float => Instruction::F32Load(mem_arg),
|
||||
});
|
||||
}
|
||||
ast::Expr::Poke {
|
||||
@@ -403,6 +409,7 @@ fn emit_expression<'a>(ctx: &mut FunctionContext<'a>, expr: &'a ast::Expression)
|
||||
ctx.function.instruction(&match mem_location.size {
|
||||
ast::MemSize::Byte => Instruction::I32Store8(mem_arg),
|
||||
ast::MemSize::Word => Instruction::I32Store(mem_arg),
|
||||
ast::MemSize::Float => Instruction::F32Store(mem_arg),
|
||||
});
|
||||
}
|
||||
ast::Expr::UnaryOp { op, value } => {
|
||||
@@ -654,19 +661,45 @@ fn emit_expression<'a>(ctx: &mut FunctionContext<'a>, expr: &'a ast::Expression)
|
||||
}
|
||||
}
|
||||
ast::Expr::FuncCall { name, params, .. } => {
|
||||
for param in params {
|
||||
emit_expression(ctx, param);
|
||||
fn mem_instruction(
|
||||
inst: MemInstruction,
|
||||
params: &[ast::Expression],
|
||||
) -> Instruction<'static> {
|
||||
let offset = params
|
||||
.get(0)
|
||||
.map(|e| e.const_i32() as u32 as u64)
|
||||
.unwrap_or(0);
|
||||
let alignment = params.get(1).map(|e| e.const_i32() as u32);
|
||||
(inst.instruction)(MemArg {
|
||||
offset,
|
||||
align: alignment.unwrap_or(inst.natural_alignment),
|
||||
memory_index: 0,
|
||||
})
|
||||
}
|
||||
|
||||
if let Some(index) = ctx.functions.get(name) {
|
||||
ctx.function.instruction(&Instruction::Call(*index));
|
||||
} else {
|
||||
let mut types = vec![];
|
||||
for param in params {
|
||||
types.push(param.type_.unwrap());
|
||||
}
|
||||
if let Some(load) = ctx.intrinsics.find_load(name) {
|
||||
emit_expression(ctx, ¶ms[0]);
|
||||
ctx.function
|
||||
.instruction(&ctx.intrinsics.get_instr(name, &types).unwrap());
|
||||
.instruction(&mem_instruction(load, ¶ms[1..]));
|
||||
} else if let Some(store) = ctx.intrinsics.find_store(name) {
|
||||
emit_expression(ctx, ¶ms[1]);
|
||||
emit_expression(ctx, ¶ms[0]);
|
||||
ctx.function
|
||||
.instruction(&mem_instruction(store, ¶ms[2..]));
|
||||
} else {
|
||||
for param in params {
|
||||
emit_expression(ctx, param);
|
||||
}
|
||||
|
||||
if let Some(index) = ctx.functions.get(name) {
|
||||
ctx.function.instruction(&Instruction::Call(*index));
|
||||
} else {
|
||||
let mut types = vec![];
|
||||
for param in params {
|
||||
types.push(param.type_.unwrap());
|
||||
}
|
||||
ctx.function
|
||||
.instruction(&ctx.intrinsics.get_instr(name, &types).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::Expr::Select {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::ast::Type;
|
||||
use std::collections::HashMap;
|
||||
use enc::MemArg;
|
||||
use wasm_encoder as enc;
|
||||
|
||||
pub struct Intrinsics(HashMap<String, HashMap<Vec<Type>, (Type, enc::Instruction<'static>)>>);
|
||||
@@ -137,4 +138,59 @@ impl Intrinsics {
|
||||
.or_default()
|
||||
.insert(params.to_vec(), (ret, ins.clone()));
|
||||
}
|
||||
|
||||
pub fn find_load(&self, name: &str) -> Option<MemInstruction> {
|
||||
use enc::Instruction as I;
|
||||
use Type::*;
|
||||
let ins = match name {
|
||||
"i32.load" => MemInstruction::new(I32, I::I32Load, 2),
|
||||
"i32.load8_s" => MemInstruction::new(I32, I::I32Load8_S, 0),
|
||||
"i32.load8_u" => MemInstruction::new(I32, I::I32Load8_U, 0),
|
||||
"i32.load16_s" => MemInstruction::new(I32, I::I32Load16_S, 1),
|
||||
"i32.load16_u" => MemInstruction::new(I32, I::I32Load16_U, 1),
|
||||
"i64.load" => MemInstruction::new(I64, I::I64Load, 3),
|
||||
"i64.load8_s" => MemInstruction::new(I64, I::I64Load8_S, 0),
|
||||
"i64.load8_u" => MemInstruction::new(I64, I::I64Load8_U, 0),
|
||||
"i64.load16_s" => MemInstruction::new(I64, I::I64Load16_S, 1),
|
||||
"i64.load16_u" => MemInstruction::new(I64, I::I64Load16_U, 1),
|
||||
"i64.load32_s" => MemInstruction::new(I64, I::I64Load32_S, 2),
|
||||
"i64.load32_u" => MemInstruction::new(I64, I::I64Load32_U, 2),
|
||||
"f32.load" => MemInstruction::new(F32, I::F32Load, 2),
|
||||
"f64.load" => MemInstruction::new(F64, I::F64Load, 3),
|
||||
_ => return None
|
||||
};
|
||||
return Some(ins);
|
||||
}
|
||||
|
||||
pub fn find_store(&self, name: &str) -> Option<MemInstruction> {
|
||||
use enc::Instruction as I;
|
||||
use Type::*;
|
||||
let ins = match name {
|
||||
"i32.store" => MemInstruction::new(I32, I::I32Store, 2),
|
||||
"i32.store8" => MemInstruction::new(I32, I::I32Store8, 0),
|
||||
"i32.store16" => MemInstruction::new(I32, I::I32Store16, 1),
|
||||
"i64.store" => MemInstruction::new(I64, I::I64Store, 3),
|
||||
"i64.store8" => MemInstruction::new(I64, I::I64Store8, 0),
|
||||
"i64.store16" => MemInstruction::new(I64, I::I64Store16, 1),
|
||||
"i64.store32" => MemInstruction::new(I64, I::I64Store32, 2),
|
||||
"f32.store" => MemInstruction::new(F32, I::F32Store, 2),
|
||||
"f64.store" => MemInstruction::new(F64, I::F64Store, 3),
|
||||
_ => return None
|
||||
};
|
||||
return Some(ins);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemInstruction {
|
||||
pub type_: Type,
|
||||
pub instruction: fn(MemArg) -> enc::Instruction<'static>,
|
||||
pub natural_alignment: u32
|
||||
}
|
||||
|
||||
impl MemInstruction {
|
||||
fn new(type_: Type, instruction: fn(MemArg) -> enc::Instruction<'static>, natural_alignment: u32) -> MemInstruction {
|
||||
MemInstruction {
|
||||
type_, instruction, natural_alignment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +212,7 @@ fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
|
||||
.collect::<String>()
|
||||
.map(Token::Op);
|
||||
|
||||
let ctrl = one_of("(){};,:?!".chars()).map(Token::Ctrl);
|
||||
let ctrl = one_of("(){};,:?!$".chars()).map(Token::Ctrl);
|
||||
|
||||
fn ident() -> impl Parser<char, String, Error = Simple<char>> + Copy + Clone {
|
||||
filter(|c: &char| c.is_ascii_alphabetic() || *c == '_')
|
||||
@@ -498,7 +498,8 @@ fn script_parser() -> impl Parser<Token, ast::Script, Error = Simple<Token>> + C
|
||||
|
||||
let mem_size = just(Token::Ctrl('?'))
|
||||
.to(ast::MemSize::Byte)
|
||||
.or(just(Token::Ctrl('!')).to(ast::MemSize::Word));
|
||||
.or(just(Token::Ctrl('!')).to(ast::MemSize::Word))
|
||||
.or(just(Token::Ctrl('$')).to(ast::MemSize::Float));
|
||||
|
||||
let mem_op = mem_size.then(op_cast.clone());
|
||||
|
||||
|
||||
163
src/typecheck.rs
163
src/typecheck.rs
@@ -1,7 +1,7 @@
|
||||
use ariadne::{Color, Label, Report, ReportKind, Source};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::ast;
|
||||
use crate::ast::{self, MemSize};
|
||||
use crate::intrinsics::Intrinsics;
|
||||
use crate::Span;
|
||||
use ast::Type::*;
|
||||
@@ -353,12 +353,12 @@ fn type_mismatch(
|
||||
Err(())
|
||||
}
|
||||
|
||||
fn expected_type(span: &Span, source: &str) -> Result<()> {
|
||||
fn report_error(msg: &str, span: &Span, source: &str) -> Result<()> {
|
||||
Report::build(ReportKind::Error, (), span.start)
|
||||
.with_message("Expected value but found expression of type void")
|
||||
.with_message(msg)
|
||||
.with_label(
|
||||
Label::new(span.clone())
|
||||
.with_message("Expected value but found expression of type void")
|
||||
.with_message(msg)
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
@@ -367,46 +367,24 @@ fn expected_type(span: &Span, source: &str) -> Result<()> {
|
||||
Err(())
|
||||
}
|
||||
|
||||
fn expected_type(span: &Span, source: &str) -> Result<()> {
|
||||
report_error(
|
||||
"Expected value but found expression of type void",
|
||||
span,
|
||||
source,
|
||||
)
|
||||
}
|
||||
|
||||
fn unknown_variable(span: &Span, source: &str) -> Result<()> {
|
||||
Report::build(ReportKind::Error, (), span.start)
|
||||
.with_message("Unknown variable")
|
||||
.with_label(
|
||||
Label::new(span.clone())
|
||||
.with_message("Unknown variable")
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint(Source::from(source))
|
||||
.unwrap();
|
||||
Err(())
|
||||
report_error("Unknown variable", span, source)
|
||||
}
|
||||
|
||||
fn immutable_assign(span: &Span, source: &str) -> Result<()> {
|
||||
Report::build(ReportKind::Error, (), span.start)
|
||||
.with_message("Trying to assign to immutable variable")
|
||||
.with_label(
|
||||
Label::new(span.clone())
|
||||
.with_message("Trying to assign to immutable variable")
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint(Source::from(source))
|
||||
.unwrap();
|
||||
Err(())
|
||||
report_error("Trying to assign to immutable variable", span, source)
|
||||
}
|
||||
|
||||
fn missing_label(span: &Span, source: &str) -> Result<()> {
|
||||
Report::build(ReportKind::Error, (), span.start)
|
||||
.with_message("Label not found")
|
||||
.with_label(
|
||||
Label::new(span.clone())
|
||||
.with_message("Label not found")
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint(Source::from(source))
|
||||
.unwrap();
|
||||
Err(())
|
||||
report_error("Label not found", span, source)
|
||||
}
|
||||
|
||||
fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()> {
|
||||
@@ -471,23 +449,17 @@ fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()
|
||||
*local_id = Some(id);
|
||||
context.local_vars.insert(name.clone(), id);
|
||||
} else {
|
||||
Report::build(ReportKind::Error, (), expr.span.start)
|
||||
.with_message("Type missing")
|
||||
.with_label(
|
||||
Label::new(expr.span.clone())
|
||||
.with_message("Type missing")
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint(Source::from(context.source))
|
||||
.unwrap();
|
||||
return Err(());
|
||||
return report_error("Type missing", &expr.span, context.source);
|
||||
}
|
||||
None
|
||||
}
|
||||
ast::Expr::Peek(ref mut mem_location) => {
|
||||
tc_mem_location(context, mem_location)?;
|
||||
Some(I32)
|
||||
let ty = match mem_location.size {
|
||||
MemSize::Float => F32,
|
||||
_ => I32,
|
||||
};
|
||||
Some(ty)
|
||||
}
|
||||
ast::Expr::Poke {
|
||||
ref mut mem_location,
|
||||
@@ -495,9 +467,13 @@ fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()
|
||||
} => {
|
||||
tc_mem_location(context, mem_location)?;
|
||||
tc_expression(context, value)?;
|
||||
if value.type_ != Some(I32) {
|
||||
let ty = match mem_location.size {
|
||||
MemSize::Float => F32,
|
||||
_ => I32,
|
||||
};
|
||||
if value.type_ != Some(ty) {
|
||||
return type_mismatch(
|
||||
Some(I32),
|
||||
Some(ty),
|
||||
&expr.span,
|
||||
value.type_,
|
||||
&value.span,
|
||||
@@ -725,7 +701,27 @@ fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()
|
||||
return expected_type(¶m.span, context.source);
|
||||
}
|
||||
}
|
||||
if let Some(type_map) = context
|
||||
if let Some(load) = context.intrinsics.find_load(name) {
|
||||
tc_memarg(context, params.as_mut_slice(), &expr.span)?;
|
||||
Some(load.type_)
|
||||
} else if let Some(store) = context.intrinsics.find_store(name) {
|
||||
if let Some(value) = params.first_mut() {
|
||||
tc_expression(context, value)?;
|
||||
if value.type_ != Some(store.type_) {
|
||||
type_mismatch(
|
||||
Some(store.type_),
|
||||
&expr.span,
|
||||
value.type_,
|
||||
&value.span,
|
||||
context.source,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
return report_error("Missing parameters", &expr.span, context.source);
|
||||
}
|
||||
tc_memarg(context, &mut params[1..], &expr.span)?;
|
||||
None
|
||||
} else if let Some(type_map) = context
|
||||
.functions
|
||||
.get(name)
|
||||
.map(|fnc| HashMap::from_iter([(fnc.params.clone(), fnc.type_)]))
|
||||
@@ -759,17 +755,11 @@ fn tc_expression(context: &mut Context, expr: &mut ast::Expression) -> Result<()
|
||||
return Err(());
|
||||
}
|
||||
} else {
|
||||
Report::build(ReportKind::Error, (), expr.span.start)
|
||||
.with_message(format!("Unknown function {}", name))
|
||||
.with_label(
|
||||
Label::new(expr.span.clone())
|
||||
.with_message(format!("Unknown function {}", name))
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint(Source::from(context.source))
|
||||
.unwrap();
|
||||
return Err(());
|
||||
return report_error(
|
||||
&format!("Unknown function {}", name),
|
||||
&expr.span,
|
||||
context.source,
|
||||
);
|
||||
}
|
||||
}
|
||||
ast::Expr::Select {
|
||||
@@ -890,19 +880,40 @@ fn tc_const(expr: &mut ast::Expression, source: &str) -> Result<()> {
|
||||
I64Const(_) => I64,
|
||||
F32Const(_) => F32,
|
||||
F64Const(_) => F64,
|
||||
_ => {
|
||||
Report::build(ReportKind::Error, (), expr.span.start)
|
||||
.with_message("Expected constant value")
|
||||
.with_label(
|
||||
Label::new(expr.span.clone())
|
||||
.with_message("Expected constant value")
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint(Source::from(source))
|
||||
.unwrap();
|
||||
return Err(());
|
||||
}
|
||||
_ => return report_error("Expected constant value", &expr.span, source),
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn tc_memarg(context: &mut Context, params: &mut [ast::Expression], span: &Span) -> Result<()> {
|
||||
if params.is_empty() || params.len() > 3 {
|
||||
let msg = if params.is_empty() {
|
||||
"Missing base address parameter"
|
||||
} else {
|
||||
"Too many MemArg parameters"
|
||||
};
|
||||
return report_error(msg, span, context.source);
|
||||
}
|
||||
|
||||
for (index, param) in params.iter_mut().enumerate() {
|
||||
tc_expression(context, param)?;
|
||||
if param.type_ != Some(I32) {
|
||||
return type_mismatch(Some(I32), &span, param.type_, ¶m.span, context.source);
|
||||
}
|
||||
if index > 0 {
|
||||
tc_const(param, context.source)?;
|
||||
}
|
||||
if index == 2 {
|
||||
let align = param.const_i32();
|
||||
if align < 0 || align > 4 {
|
||||
return report_error(
|
||||
&format!("Alignment {} out of range (0-4)", align),
|
||||
¶m.span,
|
||||
context.source,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user