Skip to content

Commit

Permalink
come command can generate wasm now
Browse files Browse the repository at this point in the history
  • Loading branch information
longfangsong committed May 10, 2024
1 parent 771de31 commit e8b541a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/backend/wasm/control_flow/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@ impl CFSelector {
return None;
}
let self_unique_part = self.range(shared_part.len()..);
Some(
self_unique_part
.0.len(),
)
Some(self_unique_part.0.len())
}
}
2 changes: 1 addition & 1 deletion src/backend/wasm/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use wasm_encoder::{BlockType, Function, Instruction, MemArg, ValType};

use crate::{
ir::{
analyzer::{BindedControlFlowGraph},
analyzer::BindedControlFlowGraph,
function::basic_block::BasicBlock,
quantity::Quantity,
statement::{
Expand Down
54 changes: 52 additions & 2 deletions src/backend/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use itertools::Itertools;
use std::mem;
use wasm_encoder::{CodeSection, ExportKind, ExportSection, FunctionSection, TypeSection};
use wasm_encoder::{
CodeSection, ConstExpr, ExportKind, ExportSection, FunctionSection, GlobalSection, GlobalType,
MemorySection, MemoryType, Module, TypeSection, ValType,
};

use crate::ir::{
analyzer::{BindedControlFlowGraph, BindedScc, ControlFlowGraph, IsAnalyzer},
editor::Analyzer,
statement::IRStatement,
FunctionDefinition,
FunctionDefinition, IR,
};

use self::{
Expand Down Expand Up @@ -210,6 +213,53 @@ fn generate_function(
result.3.function(&function);
}

pub fn compile(ir_content: &[IR]) -> Module {
let mut module = Module::new();
let mut types = TypeSection::new();
let mut functions = FunctionSection::new();
let mut exports = ExportSection::new();
let mut codes = CodeSection::new();
let mut global_section = GlobalSection::new();
let mut memory_section = MemorySection::new();
// stack pointer
global_section.global(
GlobalType {
val_type: ValType::I32,
mutable: true,
shared: false,
},
&ConstExpr::i32_const(0),
);
memory_section.memory(MemoryType {
minimum: 1,
maximum: None,
memory64: false,
shared: false,
page_size_log2: None,
});

for ir_part in ir_content {
if let IR::FunctionDefinition(function) = ir_part {
let folded = fold(function);
let root = ControlFlowElement::new_block(folded);
generate_function(
(&mut types, &mut functions, &mut exports, &mut codes),
function,
&root,
);
}
}

module.section(&types);
module.section(&functions);
module.section(&memory_section);
module.section(&global_section);
module.section(&exports);
module.section(&codes);

module
}

#[cfg(test)]
mod tests {
use std::{assert_matches::assert_matches, fs::File, io::Write, str::FromStr};
Expand Down
30 changes: 25 additions & 5 deletions src/bin/come.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use std::path::PathBuf;
use std::{fs::File, path::PathBuf};

use clap::Parser;
use clap::{Parser, ValueEnum};
use come::{
ast,
backend::riscv,
backend::{riscv, wasm},
ir::{self, optimize},
};
use ezio::file;
use shadow_rs::shadow;
use std::io::Write;
shadow!(build);

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, ValueEnum)]
enum Target {
/// riscv backend
RISCV,
/// web assembly backend
WASM,
}

/// Come language compiler.
#[derive(Parser, Debug)]
#[command(version, long_version = build::CLAP_LONG_VERSION, about, long_about = None)]
Expand All @@ -29,6 +37,9 @@ struct Args {

#[arg(short = 'O', long, value_delimiter = ',')]
optimize: Vec<ir::optimize::pass::Pass>,

#[arg(short = 't', long, value_enum)]
target: Target,
}

fn main() {
Expand All @@ -43,6 +54,15 @@ fn main() {
writeln!(w, "{ir}").unwrap();
}
}
let code = riscv::from_ir::emit_asm(&ir);
file::write(args.output, &code);
match args.target {
Target::RISCV => {
let code = riscv::from_ir::emit_asm(&ir);
file::write(args.output, &code);
}
Target::WASM => {
let module = wasm::compile(&ir);
let mut output_file = File::create(args.output).unwrap();
output_file.write_all(module.as_slice()).unwrap();
}
}
}
1 change: 0 additions & 1 deletion src/ir/editor/analyzer/control_flow/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
borrow::Borrow,
cell::{OnceCell, Ref, RefCell},
collections::HashMap,
};
Expand Down
4 changes: 1 addition & 3 deletions src/ir/function/parameter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use nom::{
character::complete::space0, combinator::map, sequence::tuple, IResult,
};
use nom::{character::complete::space0, combinator::map, sequence::tuple, IResult};

use crate::{
ast,
Expand Down

0 comments on commit e8b541a

Please sign in to comment.