-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
471 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
## Call Convention | ||
1. We use `ra` to store the continuation address, as it's otherwise unused in our language. We do need to push it onto stack to preserve it's value when doing a native call, though. | ||
2. We store `closure` pointer after any arguments, so we should be able to work with native functions just fine. This differs from what is being done in the book "Compiling with Continuations". | ||
|
||
## TODO | ||
1. fix call convention for external functions. For example: | ||
``` | ||
:print_int([?26, kont_main.4.22]) | ||
``` | ||
Inside print_int, we should do something like this: | ||
``` | ||
fn_ptr.99 = kont_main.4.22.0 | ||
fn_ptr.99([result, kont_main.4.22]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,185 +1,100 @@ | ||
fn add_interpreter_fns(interpreter : @knf_eval.KnfInterpreter) -> Unit { | ||
fn add_clops_interp_fns(interpreter : @closureps_eval.CloPSInterpreter) -> Unit { | ||
interpreter.add_extern_fn( | ||
"minimbt_print_int", | ||
"print_int", | ||
fn(args) { | ||
match args[0] { | ||
Int(i) => @io.print(i) | ||
_ => @util.die("print_int expects Int") | ||
} | ||
Unit | ||
}, | ||
1, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_print_endline", | ||
"print_endline", | ||
fn(_args) { | ||
@io.print("\n") | ||
Unit | ||
}, | ||
0, | ||
) | ||
let create_array = fn(args : Array[@knf_eval.Value]) { | ||
let create_array = fn(args : Array[@closureps_eval.Value]) { | ||
match args[0] { | ||
Int(n) => @knf_eval.Value::Array(Array::make(n, args[1])) | ||
Int(n) => @closureps_eval.Value::Array(Array::make(n, args[1])) | ||
_ => @util.die("create_array expects Int") | ||
} | ||
} | ||
interpreter.add_extern_fn("minimbt_create_array", create_array) | ||
interpreter.add_extern_fn("minimbt_create_float_array", create_array) | ||
interpreter.add_extern_fn("minimbt_create_ptr_array", create_array) | ||
interpreter.add_extern_fn("create_array", create_array, 2) | ||
interpreter.add_extern_fn("create_float_array", create_array, 2) | ||
interpreter.add_extern_fn("create_ptr_array", create_array, 2) | ||
interpreter.add_extern_fn( | ||
"minimbt_truncate", | ||
"truncate", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Int(d.to_int()) | ||
_ => @util.die("truncate expects Double") | ||
_ => @util.die("expects Double") | ||
} | ||
}, | ||
1, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_sin", | ||
"sin", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(@math.sin(d)) | ||
_ => @util.die("sin expects Double") | ||
} | ||
}, | ||
1, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_cos", | ||
"cos", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(@math.cos(d)) | ||
_ => @util.die("cos expects Double") | ||
} | ||
}, | ||
1, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_sqrt", | ||
"sqrt", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(d.sqrt()) | ||
_ => @util.die("sqrt expects Double") | ||
} | ||
}, | ||
1, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_abs_float", | ||
"abs_float", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(@double.abs(d)) | ||
_ => @util.die("abs_float expects Double") | ||
} | ||
}, | ||
1, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_int_of_float", | ||
"int_of_float", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Int(d.to_int()) | ||
_ => @util.die("int_of_float expects Double") | ||
} | ||
}, | ||
1, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_float_of_int", | ||
fn(args) { | ||
match args[0] { | ||
Int(i) => Double(i.to_double()) | ||
_ => @util.die("float_of_int expects Int") | ||
} | ||
}, | ||
) | ||
} | ||
|
||
fn add_closure_interpreter_fns( | ||
interpreter : @closure_eval.ClosureInterpreter | ||
) -> Unit { | ||
interpreter.add_extern_fn( | ||
"minimbt_print_int", | ||
fn(args) { | ||
match args[0] { | ||
Int(i) => @io.print(i) | ||
_ => @util.die("print_int expects Int") | ||
} | ||
Unit | ||
}, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_print_endline", | ||
fn(_args) { | ||
@io.print("\n") | ||
Unit | ||
}, | ||
) | ||
let create_array = fn(args : Array[@closure_eval.Value]) { | ||
match args[0] { | ||
Int(n) => @closure_eval.Value::Array(Array::make(n, args[1])) | ||
_ => @util.die("create_array expects Int") | ||
} | ||
} | ||
interpreter.add_extern_fn("minimbt_create_array", create_array) | ||
interpreter.add_extern_fn("minimbt_create_float_array", create_array) | ||
interpreter.add_extern_fn("minimbt_create_ptr_array", create_array) | ||
interpreter.add_extern_fn( | ||
"minimbt_truncate", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Int(d.to_int()) | ||
_ => @util.die("truncate expects Double") | ||
} | ||
}, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_sin", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(@math.sin(d)) | ||
_ => @util.die("sin expects Double") | ||
} | ||
}, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_cos", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(@math.cos(d)) | ||
_ => @util.die("cos expects Double") | ||
} | ||
}, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_sqrt", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(d.sqrt()) | ||
_ => @util.die("sqrt expects Double") | ||
} | ||
}, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_abs_float", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Double(@double.abs(d)) | ||
_ => @util.die("abs_float expects Double") | ||
} | ||
}, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_int_of_float", | ||
fn(args) { | ||
match args[0] { | ||
Double(d) => Int(d.to_int()) | ||
_ => @util.die("int_of_float expects Double") | ||
} | ||
}, | ||
) | ||
interpreter.add_extern_fn( | ||
"minimbt_float_of_int", | ||
"float_of_int", | ||
fn(args) { | ||
match args[0] { | ||
Int(i) => Double(i.to_double()) | ||
_ => @util.die("float_of_int expects Int") | ||
} | ||
}, | ||
1, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.