Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorTaelin committed Jun 15, 2024
1 parent 154b9e3 commit 67a4ecb
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 81 deletions.
36 changes: 0 additions & 36 deletions examples/sort/bitonic/main.hvm

This file was deleted.

25 changes: 17 additions & 8 deletions src/compiler/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,19 @@ pub fn build_function_rule_rhs(
free : &mut Vec<Option<(String,u64)>>,
nams : &mut u64,
dups : &mut HashMap<u64, (String,String)>,
dupf : u64,
glob : u64,
) -> (String, String) {
if let Some(got) = dups.get(&glob) {
return got.clone();
} else {
let coln = fresh(nams, "col");
let name = fresh(nams, "dup");
line(code, tab + 1, &format!("let {} = gen_dup(ctx.heap, ctx.tid);", coln));
let coln = fresh(nams, "col");
if dupf != 0xFFFFFFF {
line(code, tab + 1, &format!("let {} = {};", coln, dupf));
} else {
line(code, tab + 1, &format!("let {} = gen_dup(ctx.heap, ctx.tid);", coln));
}
line(code, tab + 1, &format!("let {} = {};", name, alloc_node(free, 3)));
line(code, tab, &format!("link(ctx.heap, {} + 0, Era());", name)); // FIXME: remove when possible (same as above)
line(code, tab, &format!("link(ctx.heap, {} + 1, Era());", name)); // FIXME: remove when possible (same as above)
Expand Down Expand Up @@ -457,19 +462,19 @@ pub fn build_function_rule_rhs(
return format!("Var({})", alloc_lam(code, tab, free, nams, lams, *glob));
}
runtime::DP0 => {
let (coln, name) = alloc_dup(code, tab, free, nams, dups, *glob);
let (coln, name) = alloc_dup(code, tab, free, nams, dups, 0, *glob);
return format!("Dp0({}, {})", coln, name);
}
runtime::DP1 => {
let (coln, name) = alloc_dup(code, tab, free, nams, dups, *glob);
let (coln, name) = alloc_dup(code, tab, free, nams, dups, 0, *glob);
return format!("Dp1({}, {})", coln, name);
}
_ => {
panic!("Unexpected error.");
}
}
}
runtime::Core::Dup { eras, glob, expr, body } => {
runtime::Core::Dup { dupf, eras, glob, expr, body } => {
let copy = fresh(nams, "cpy");
let dup0 = fresh(nams, "dp0");
let dup1 = fresh(nams, "dp1");
Expand All @@ -484,7 +489,7 @@ pub fn build_function_rule_rhs(
line(code, tab + 1, &format!("{} = {};", dup1, copy));
line(code, tab + 0, "} else {");
}
let (coln, name) = alloc_dup(code, tab, &mut vec![], nams, dups, *glob);
let (coln, name) = alloc_dup(code, tab, &mut vec![], nams, dups, *dupf, *glob);
if eras.0 {
line(code, tab + 1, &format!("link(ctx.heap, {} + 0, Era());", name));
}
Expand All @@ -504,12 +509,16 @@ pub fn build_function_rule_rhs(
vars.pop();
body
}
runtime::Core::Sup { val0, val1 } => {
runtime::Core::Sup { dupf, val0, val1 } => {
let name = fresh(nams, "sup");
let val0 = build_term(book, code, tab, free, vars, nams, lams, dups, val0);
let val1 = build_term(book, code, tab, free, vars, nams, lams, dups, val1);
let coln = fresh(nams, "col");
line(code, tab + 1, &format!("let {} = gen_dup(ctx.heap, ctx.tid);", coln));
if *dupf != 0xFFFFFFF {
line(code, tab + 1, &format!("let {} = {};", coln, dupf));
} else {
line(code, tab + 1, &format!("let {} = gen_dup(ctx.heap, ctx.tid);", coln));
}
line(code, tab, &format!("let {} = {};", name, alloc_node(free, 2)));
line(code, tab, &format!("link(ctx.heap, {} + 0, {});", name, val0));
line(code, tab, &format!("link(ctx.heap, {} + 1, {});", name, val1));
Expand Down
8 changes: 5 additions & 3 deletions src/language/readback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ pub fn as_term(heap: &Heap, prog: &Program, host: u64) -> Box<language::syntax::
stacks.push(col, old);
got
} else {
let dupf = format!("{}", if col >= 0x8000000 { col - 0x8000000 } else { 0 });
let val0 = runtime::load_arg(&ctx.heap, term, 0);
let val1 = runtime::load_arg(&ctx.heap, term, 1);
let val0 = readback(heap, prog, ctx, stacks, val0, depth + 1);
let val1 = readback(heap, prog, ctx, stacks, val1, depth + 1);
return Box::new(language::syntax::Term::Sup { val0, val1 });
return Box::new(language::syntax::Term::Sup { dupf, val0, val1 });
}
}
runtime::DP0 => {
Expand Down Expand Up @@ -312,11 +313,12 @@ pub fn as_linear_term(heap: &Heap, prog: &Program, host: u64) -> Box<language::s
let name = names.get(&pos).unwrap_or(&what);
let nam0 = if runtime::load_ptr(heap, pos + 0) == runtime::Era() { String::from("*") } else { format!("a{}", name) };
let nam1 = if runtime::load_ptr(heap, pos + 1) == runtime::Era() { String::from("*") } else { format!("b{}", name) };
let dupf = format!("{}", kinds.get(&pos).unwrap_or(&0));
let expr = expr(heap, prog, runtime::load_ptr(heap, pos + 2), &names);
if i == 0 {
output = language::syntax::Term::Dup { nam0, nam1, expr: Box::new(expr), body: Box::new(cont.clone()) };
output = language::syntax::Term::Dup { dupf, nam0, nam1, expr: Box::new(expr), body: Box::new(cont.clone()) };
} else {
output = language::syntax::Term::Dup { nam0, nam1, expr: Box::new(expr), body: Box::new(output) };
output = language::syntax::Term::Dup { dupf, nam0, nam1, expr: Box::new(expr), body: Box::new(output) };
}
}
output
Expand Down
18 changes: 11 additions & 7 deletions src/language/rulebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn add_group(book: &mut RuleBook, name: &str, group: &RuleGroup) {
register(book, expr, false);
register(book, body, false);
}
language::syntax::Term::Sup { val0, val1 } => {
language::syntax::Term::Sup { dupf: _, val0, val1 } => {
register(book, val0, false);
register(book, val1, false);
}
Expand Down Expand Up @@ -295,7 +295,7 @@ pub fn sanitize_rule(rule: &language::syntax::Rule) -> Result<language::syntax::
}
}
}
language::syntax::Term::Dup { expr, body, nam0, nam1 } => {
language::syntax::Term::Dup { dupf, expr, body, nam0, nam1 } => {
let is_global_0 = runtime::get_global_name_misc(nam0).is_some();
let is_global_1 = runtime::get_global_name_misc(nam1).is_some();
if is_global_0 && runtime::get_global_name_misc(nam0) != Some(runtime::DP0) {
Expand Down Expand Up @@ -334,15 +334,17 @@ pub fn sanitize_rule(rule: &language::syntax::Rule) -> Result<language::syntax::
if let Some(x) = got_nam1 {
tbl.insert(nam1.clone(), x);
}
let dupf = dupf.clone();
let nam0 = format!("{}{}", new_nam0, if !is_global_0 { ".0" } else { "" });
let nam1 = format!("{}{}", new_nam1, if !is_global_0 { ".0" } else { "" });
let term = language::syntax::Term::Dup { nam0, nam1, expr, body };
let term = language::syntax::Term::Dup { dupf, nam0, nam1, expr, body };
Box::new(term)
}
language::syntax::Term::Sup { val0, val1 } => {
language::syntax::Term::Sup { dupf, val0, val1 } => {
let dupf = dupf.clone();
let val0 = sanitize_term(val0, lhs, tbl, ctx)?;
let val1 = sanitize_term(val1, lhs, tbl, ctx)?;
let term = language::syntax::Term::Sup { val0, val1 };
let term = language::syntax::Term::Sup { dupf, val0, val1 };
Box::new(term)
}
language::syntax::Term::Let { name, expr, body } => {
Expand Down Expand Up @@ -459,6 +461,7 @@ pub fn sanitize_rule(rule: &language::syntax::Rule) -> Result<language::syntax::

// use aux variables to duplicate the variable
let dup = language::syntax::Term::Dup {
dupf: "".to_string(),
nam0: vars.pop().unwrap(),
nam1: vars.pop().unwrap(),
expr,
Expand Down Expand Up @@ -487,6 +490,7 @@ pub fn sanitize_rule(rule: &language::syntax::Rule) -> Result<language::syntax::
let nam1 = vars.pop().unwrap();
let exp0 = Box::new(language::syntax::Term::Var { name: format!("c.{}", i - 1) });
Box::new(language::syntax::Term::Dup {
dupf: "".to_string(),
nam0,
nam1,
expr: exp0,
Expand Down Expand Up @@ -667,13 +671,13 @@ pub fn subst(term: &mut language::syntax::Term, sub_name: &str, value: &language
*term = value.clone();
}
}
language::syntax::Term::Dup { nam0, nam1, expr, body } => {
language::syntax::Term::Dup { dupf: _, nam0, nam1, expr, body } => {
subst(&mut *expr, sub_name, value);
if nam0 != sub_name && nam1 != sub_name {
subst(&mut *body, sub_name, value);
}
}
language::syntax::Term::Sup { val0, val1 } => {
language::syntax::Term::Sup { dupf: _, val0, val1 } => {
subst(&mut *val0, sub_name, value);
subst(&mut *val1, sub_name, value);
}
Expand Down
53 changes: 47 additions & 6 deletions src/language/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::runtime::data::f60;
#[derive(Clone, Debug)]
pub enum Term {
Var { name: String }, // TODO: add `global: bool`
Dup { nam0: String, nam1: String, expr: Box<Term>, body: Box<Term> },
Sup { val0: Box<Term>, val1: Box<Term> },
Dup { dupf: String, nam0: String, nam1: String, expr: Box<Term>, body: Box<Term> },
Sup { dupf: String, val0: Box<Term>, val1: Box<Term> },
Let { name: String, expr: Box<Term>, body: Box<Term> },
Lam { name: String, body: Box<Term> },
App { func: Box<Term>, argm: Box<Term> },
Expand Down Expand Up @@ -133,8 +133,8 @@ impl std::fmt::Display for Term {
}
match self {
Self::Var { name } => write!(f, "{}", name),
Self::Dup { nam0, nam1, expr, body } => write!(f, "dup {} {} = {}; {}", nam0, nam1, expr, body),
Self::Sup { val0, val1 } => write!(f, "{{{} {}}}", val0, val1),
Self::Dup { dupf, nam0, nam1, expr, body } => write!(f, "dup #{}{{{} {}}} = {}; {}", dupf, nam0, nam1, expr, body),
Self::Sup { dupf, val0, val1 } => write!(f, "#{}{{{} {}}}", dupf, val0, val1),
Self::Let { name, expr, body } => write!(f, "let {} = {}; {}", name, expr, body),
Self::Lam { name, body } => write!(f, "λ{} {}", name, body),
Self::App { func, argm } => {
Expand Down Expand Up @@ -207,13 +207,35 @@ pub fn parse_dup(state: HOPA::State) -> HOPA::Answer<Option<Box<Term>>> {
HOPA::do_there_take_exact("dup "),
Box::new(|state| {
let (state, _) = HOPA::force_there_take_exact("dup ", state)?;
let (state, _) = HOPA::force_there_take_exact("{", state)?;
let (state, nam0) = HOPA::there_nonempty_name(state)?;
let (state, nam1) = HOPA::there_nonempty_name(state)?;
let (state, _) = HOPA::force_there_take_exact("}", state)?;
let (state, _) = HOPA::force_there_take_exact("=", state)?;
let (state, expr) = parse_term(state)?;
let (state, _) = HOPA::there_take_exact(";", state)?;
let (state, body) = parse_term(state)?;
Ok((state, Box::new(Term::Dup { dupf: "".to_string(), nam0, nam1, expr, body })))
}),
state,
);
}

pub fn parse_dup_tag(state: HOPA::State) -> HOPA::Answer<Option<Box<Term>>> {
return HOPA::guard(
HOPA::do_there_take_exact("dup #"),
Box::new(|state| {
let (state, _) = HOPA::force_there_take_exact("dup #", state)?;
let (state, dupf) = HOPA::there_name(state)?;
let (state, _) = HOPA::force_there_take_exact("{", state)?;
let (state, nam0) = HOPA::there_nonempty_name(state)?;
let (state, nam1) = HOPA::there_nonempty_name(state)?;
let (state, _) = HOPA::force_there_take_exact("}", state)?;
let (state, _) = HOPA::force_there_take_exact("=", state)?;
let (state, expr) = parse_term(state)?;
let (state, _) = HOPA::there_take_exact(";", state)?;
let (state, body) = parse_term(state)?;
Ok((state, Box::new(Term::Dup { nam0, nam1, expr, body })))
Ok((state, Box::new(Term::Dup { dupf, nam0, nam1, expr, body })))
}),
state,
);
Expand All @@ -223,11 +245,28 @@ pub fn parse_sup(state: HOPA::State) -> HOPA::Answer<Option<Box<Term>>> {
HOPA::guard(
HOPA::do_there_take_exact("{"),
Box::new(move |state| {
let (state, dupf) = HOPA::there_name(state)?;
let (state, _) = HOPA::force_there_take_exact("{", state)?;
let (state, val0) = parse_term(state)?;
let (state, val1) = parse_term(state)?;
let (state, _) = HOPA::force_there_take_exact("}", state)?;
Ok((state, Box::new(Term::Sup { dupf, val0, val1 })))
}),
state,
)
}

pub fn parse_sup_tag(state: HOPA::State) -> HOPA::Answer<Option<Box<Term>>> {
HOPA::guard(
HOPA::do_there_take_exact("#"),
Box::new(move |state| {
let (state, _) = HOPA::force_there_take_exact("#", state)?;
let (state, dupf) = HOPA::there_name(state)?;
let (state, _) = HOPA::force_there_take_exact("{", state)?;
let (state, val0) = parse_term(state)?;
let (state, val1) = parse_term(state)?;
let (state, _) = HOPA::force_there_take_exact("}", state)?;
Ok((state, Box::new(Term::Sup { val0, val1 })))
Ok((state, Box::new(Term::Sup { dupf, val0, val1 })))
}),
state,
)
Expand Down Expand Up @@ -554,12 +593,14 @@ pub fn parse_bng(state: HOPA::State) -> HOPA::Answer<Option<Box<Term>>> {
pub fn parse_term(state: HOPA::State) -> HOPA::Answer<Box<Term>> {
HOPA::attempt("Term", &[
Box::new(parse_let),
Box::new(parse_dup_tag),
Box::new(parse_dup),
Box::new(parse_lam),
Box::new(parse_ctr),
Box::new(parse_op2),
Box::new(parse_app),
Box::new(parse_sup),
Box::new(parse_sup_tag),
Box::new(parse_num),
Box::new(parse_sym_sugar),
Box::new(parse_chr_sugar),
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/base/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub fn inc_cost(heap: &Heap, tid: usize) {
}

pub fn gen_dup(heap: &Heap, tid: usize) -> u64 {
return unsafe { heap.lvar.get_unchecked(tid) }.dups.fetch_add(1, Ordering::Relaxed) & 0xFFF_FFFF;
return unsafe { heap.lvar.get_unchecked(tid) }.dups.fetch_add(1, Ordering::Relaxed) & 0x7FF_FFFF;
}

pub fn arity_of(arit: &ArityMap, lnk: Ptr) -> u64 {
Expand Down
Loading

0 comments on commit 67a4ecb

Please sign in to comment.