Skip to content

Commit

Permalink
checkpoint: rv64 backend WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
glyh committed Oct 21, 2024
1 parent 7f82d77 commit 654f6d3
Show file tree
Hide file tree
Showing 48 changed files with 2,129 additions and 233 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ __pycache__
/submit.zip

/tmp

test/*
!test/test_src
!test/script.gdb
!test/build_*
!test/*_exe
2 changes: 1 addition & 1 deletion build_rvlinux.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

pushd libriscv/emulator
./build.sh --no-C --native --64 -b -v
./build.sh --native --64 -b -v
popd
ln -fs libriscv/emulator/.build/rvlinux .
6 changes: 3 additions & 3 deletions riscv_rt/src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export fn minimbt_create_array(n: u32, v: i32) [*]i32 {
return @ptrCast(arr);
}

export fn minimbt_create_ptr_array(n: u32, init: u32) [*]u32 {
const arr = allocator.alloc(u32, n) catch @panic("Alloc failed!");
export fn minimbt_create_ptr_array(n: u32, init: u64) [*]u64 {
const arr = allocator.alloc(u64, n) catch @panic("Alloc failed!");
for (arr) |*item| {
item.* = init;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ export fn mincaml_create_array(n: u32, v: i32) [*]i32 {
return minimbt_create_array(n, v);
}

export fn mincaml_create_ptr_array(n: u32, init: u32) [*]u32 {
export fn mincaml_create_ptr_array(n: u32, init: u64) [*]u64 {
return minimbt_create_ptr_array(n, init);
}

Expand Down
41 changes: 17 additions & 24 deletions src/bin/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum Stages {
Cps
CloPS
// NOTE: add stages here.
SsaCfg
Asm
Finished
} derive(Show, Eq, Compare)
Expand All @@ -17,6 +18,7 @@ fn Stages::from_string(s : String) -> Stages? {
"precps" => Some(Stages::PreCps)
"cps" => Some(Stages::Cps)
"clops" => Some(Stages::CloPS)
"ssacfg" => Some(Stages::SsaCfg)
// NOTE: add stages here.
"riscv" => Some(Stages::Asm)
"finished" => Some(Stages::Finished)
Expand All @@ -30,7 +32,8 @@ fn Stages::next(self : Stages) -> Stages {
Stages::Typecheck => Stages::PreCps
Stages::PreCps => Stages::Cps
Stages::Cps => Stages::CloPS
Stages::CloPS => Stages::Asm
Stages::CloPS => Stages::SsaCfg
Stages::SsaCfg => Stages::Asm
// NOTE: add stages here.
Stages::Asm => Stages::Finished
Stages::Finished => Stages::Finished
Expand All @@ -47,6 +50,7 @@ struct CompileStatus {
mut precps : @precps.PreCps?
mut cps : @cps.Cps?
mut clops : @closureps.ClosurePS?
mut cfg : @ssacfg.SsaCfg?
// NOTE: add stages here.
mut asm : Array[@riscv.AssemblyFunction]?
}
Expand All @@ -66,6 +70,7 @@ fn CompileStatus::initialize(
precps: None,
cps: None,
clops: None,
cfg: None,
// NOTE: add stages here.
asm: None,
}
Expand Down Expand Up @@ -112,9 +117,13 @@ fn CompileStatus::step(self : CompileStatus) -> Bool {
self.clops = Some(clops)
self.counter = clops.counter.val
}
SsaCfg => {
let cfg = @ssacfg.clops2ssacfg(self.clops.unwrap())
self.cfg = Some(cfg)
}
// NOTE: add stages here.
Asm => {
let real_asm = @riscv.emit(@util.die("TODO5"))
let real_asm = @riscv.emit(self.cfg.unwrap())
self.asm = Some(real_asm)
}
Finished => ()
Expand All @@ -123,18 +132,15 @@ fn CompileStatus::step(self : CompileStatus) -> Bool {
self.curr_stage >= self.end_stage
}

fn CompileStatus::output(
self : CompileStatus,
json : Bool,
closureps_verbose : Bool
) -> String {
fn CompileStatus::output(self : CompileStatus, json : Bool) -> String {
if json {
match self.curr_stage {
Parse => self.source_code.unwrap()
Typecheck => @json.stringify(self.ast.unwrap().to_json())
PreCps => @util.die("TODO1")
Cps => @util.die("TODO2")
CloPS => @util.die("TODO999")
SsaCfg => @util.die("TODO998")
// NOTE: add stages here.
Asm => @util.die("TODO3")
Finished => @riscv.print_functions(self.asm.unwrap())
Expand All @@ -146,12 +152,8 @@ fn CompileStatus::output(
PreCps => self.typechecked.unwrap().to_string()
Cps => self.precps.unwrap().to_string()
CloPS => self.cps.unwrap().to_string()
Asm =>
if closureps_verbose {
self.clops.unwrap().to_verbose()
} else {
self.clops.unwrap().to_string()
}
SsaCfg => self.clops.unwrap().to_string()
Asm => self.cfg.unwrap().to_string()
// NOTE: add stages here.
Finished => @riscv.print_functions(self.asm.unwrap())
}
Expand All @@ -166,12 +168,9 @@ fn main {
let json = Ref::new(false)
let start_stage = Ref::new(Stages::Parse)
let end_stage = Ref::new(Stages::Finished)
let closureps_verbose = Ref::new(false)
let closureps_interpreter = Ref::new(false)
let out_file = Ref::new("-")
let print = Ref::new([])

//
@ArgParser.parse(
[
(
Expand Down Expand Up @@ -200,12 +199,6 @@ fn main {
),
"End stage",
),
(
"--clops-v",
"",
@ArgParser.Set(closureps_verbose),
"Show more information of closure passing style",
),
(
"--clops-interp",
"",
Expand Down Expand Up @@ -273,7 +266,7 @@ fn main {
let stop = status.step()
if stages_to_print.contains(stg) {
println("Stage: \{stg}")
println(status.output(json.val, closureps_verbose.val))
println(status.output(json.val))
}
if stop {
break
Expand All @@ -293,7 +286,7 @@ fn main {
v => ignore(v)
}
} else {
let out_string = status.output(json.val, closureps_verbose.val)
let out_string = status.output(json.val)
if out_file.val == "-" {
println(out_string)
} else {
Expand Down
1 change: 1 addition & 0 deletions src/bin/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"moonbitlang/minimbt/precps",
"moonbitlang/minimbt/closureps",
"moonbitlang/minimbt/ssacfg",
"moonbitlang/minimbt/cps",
"moonbitlang/minimbt/knf",
"moonbitlang/minimbt/typing",
Expand Down
30 changes: 29 additions & 1 deletion src/closureps/cloenv.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ struct CloEnv {
fnblocks : @hashmap.T[Var, FuncDef]
counter : Ref[Int]
bindings : @immut/hashmap.T[Var, Var]
// NOTE: when collecting free vars of function body,
// we may have to issue the collecter there's actually a closure pointer passed
// even if closure conversion is not done
pre_clo_gen : @hashmap.T[Var, Var]
named_fns : @hashset.T[Var]
}

fn CloEnv::collect_named_fns(self : CloEnv, c : @cps.Cps) -> Unit {
match c {
Tuple(_, _, c) | KthTuple(_, _, _, c) | Prim(_, _, _, c) =>
self.collect_named_fns(c)
Fix(f, _, body, rest) => {
self.named_fns.insert(f)
self.collect_named_fns(body)
self.collect_named_fns(rest)
}
If(_, _then, _else) => {
self.collect_named_fns(_then)
self.collect_named_fns(_else)
}
App(_) | MakeArray(_) | Exit => ()
}
}

fn CloEnv::add_rebind(self : CloEnv, name : Var, cb : Var) -> CloEnv {
Expand All @@ -13,7 +35,13 @@ fn CloEnv::add_rebind(self : CloEnv, name : Var, cb : Var) -> CloEnv {

fn CloEnv::new(counter : Int) -> CloEnv {
let counter = { val: counter }
{ fnblocks: @hashmap.new(), counter, bindings: @immut/hashmap.new() }
{
fnblocks: @hashmap.new(),
counter,
bindings: @immut/hashmap.new(),
pre_clo_gen: @hashmap.new(),
named_fns: @hashset.new(),
}
}

fn CloEnv::new_tmp(self : CloEnv, t : T) -> Var {
Expand Down
Loading

0 comments on commit 654f6d3

Please sign in to comment.