Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard the calls with checks #669

Closed
wants to merge 13 commits into from
34 changes: 17 additions & 17 deletions compiler/backend_inkwell/candy_runtime/candy_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string.h>
#include "candy_runtime.h"

const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right)
const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
if (left
->type != right->type)
Expand All @@ -22,25 +22,25 @@ const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *ri
}
}

const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise)
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise, candy_value_t *responsible)
{
candy_value_t *body = candy_tag_to_bool(condition) ? then : otherwise;
candy_function function = (body->value).function.function;
candy_value_t *environment = (body->value).function.environment;
return function(environment);
}

candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer + right->value.integer);
}

candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer - right->value.integer);
}

candy_value_t *candy_builtin_int_bit_length(candy_value_t *value)
candy_value_t *candy_builtin_int_bit_length(candy_value_t *value, candy_value_t *responsible)
{
int64_t int_value = value->value.integer;
int is_negative = int_value < 0;
Expand All @@ -57,22 +57,22 @@ candy_value_t *candy_builtin_int_bit_length(candy_value_t *value)
return make_candy_int(shifts + is_negative);
}

candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer & right->value.integer);
}

candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer | right->value.integer);
}

candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer ^ right->value.integer);
}

const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right)
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
int64_t left_value = left->value.integer;
int64_t right_value = right->value.integer;
Expand All @@ -90,7 +90,7 @@ const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_val
}
}

candy_value_t *candy_builtin_list_length(const candy_value_t *list)
candy_value_t *candy_builtin_list_length(const candy_value_t *list, candy_value_t *responsible)
{
size_t index = 0;
while (list->value.list[index] != NULL)
Expand All @@ -100,42 +100,42 @@ candy_value_t *candy_builtin_list_length(const candy_value_t *list)
return make_candy_int(index);
}

const candy_value_t *candy_builtin_print(candy_value_t *value)
const candy_value_t *candy_builtin_print(candy_value_t *value, candy_value_t *responsible)
{
print_candy_value(value);
printf("\n");
return &__internal_nothing;
}

candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key)
candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible)
{
size_t index = 0;
while (!candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key)))
while (!candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key, responsible)))
{
index++;
}
return structure->value.structure.values[index];
}

candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure)
candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure, candy_value_t *responsible)
{
return make_candy_list(structure->value.structure.keys);
}

const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key)
const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible)
{
size_t index = 0;
while (structure->value.structure.keys[index] != NULL)
{
if (candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key)))
if (candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key, responsible)))
{
return &__internal_true;
}
}
return &__internal_false;
}

const candy_value_t *candy_builtin_type_of(candy_value_t *value)
const candy_value_t *candy_builtin_type_of(candy_value_t *value, candy_value_t *responsible)
{
switch (value->type)
{
Expand Down
30 changes: 15 additions & 15 deletions compiler/backend_inkwell/candy_runtime/candy_builtin.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "candy_runtime.h"

const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right);
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise);
candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bit_length(candy_value_t *value);
candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right);
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_list_length(const candy_value_t *list);
const candy_value_t *candy_builtin_print(candy_value_t *value);
candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key);
candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure);
const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key);
const candy_value_t *candy_builtin_type_of(candy_value_t *value);
const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise, candy_value_t *responsible);
candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bit_length(candy_value_t *value, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_list_length(const candy_value_t *list, candy_value_t *responsible);
const candy_value_t *candy_builtin_print(candy_value_t *value, candy_value_t *responsible);
candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible);
candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure, candy_value_t *responsible);
const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible);
const candy_value_t *candy_builtin_type_of(candy_value_t *value, candy_value_t *responsible);
9 changes: 7 additions & 2 deletions compiler/backend_inkwell/candy_runtime/candy_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ void print_candy_value(const candy_value_t *value)
break;
case CANDY_TYPE_LIST:
printf("(");
candy_value_t *length = candy_builtin_list_length(value);
// TODO: The responsible parameter is not by builtin_list_length
// anyways, so we just pass anything.
candy_value_t *length = candy_builtin_list_length(value, value);
size_t list_length = length->value.integer;
free_candy_value(length);
size_t index = 0;
Expand Down Expand Up @@ -183,11 +185,14 @@ void *get_candy_function_environment(candy_value_t *function)
return function->value.function.environment;
}

void candy_panic(const candy_value_t *reason)
void candy_panic(const candy_value_t *reason, const candy_value_t* responsible)
{
printf("The program panicked for the following reason: \n");
print_candy_value(reason);
printf("\n");
printf("The code at ");
print_candy_value(responsible);
printf(" is responsible.");
exit(-1);
}

Expand Down
13 changes: 7 additions & 6 deletions compiler/backend_inkwell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,7 @@ impl<'ctx> CodeGen<'ctx> {
original_hirs,
parameters,
body,
responsible_parameter,
} => {
self.unrepresented_ids.insert(*responsible_parameter);
let name = original_hirs
.iter()
.sorted()
Expand Down Expand Up @@ -616,9 +614,7 @@ impl<'ctx> CodeGen<'ctx> {
candy_frontend::mir::Expression::Call {
function,
arguments,
responsible,
} => {
self.unrepresented_ids.insert(*responsible);
let mut args: Vec<_> = arguments
.iter()
.map(|arg| self.get_value_with_id(function_ctx, arg).unwrap().into())
Expand Down Expand Up @@ -700,12 +696,17 @@ impl<'ctx> CodeGen<'ctx> {
}
}
candy_frontend::mir::Expression::UseModule { .. } => unreachable!(),
candy_frontend::mir::Expression::Panic { reason, .. } => {
candy_frontend::mir::Expression::Panic {
reason,
responsible,
} => {
let panic_fn = self.module.get_function("candy_panic").unwrap();

let reason = self.get_value_with_id(function_ctx, reason).unwrap();
let responsible = self.get_value_with_id(function_ctx, responsible).unwrap();

self.builder.build_call(panic_fn, &[reason.into()], "");
self.builder
.build_call(panic_fn, &[reason.into(), responsible.into()], "");

self.builder.build_unreachable();
}
Expand Down
16 changes: 10 additions & 6 deletions compiler/cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ pub(crate) fn run(options: Options) -> ProgramResult {

debug!("Running main function.");
// TODO: Add more environment stuff.
let stdout = Handle::new(&mut heap, 1);
let stdin = Handle::new(&mut heap, 0);
let stdout = Handle::new(&mut heap, 2);
let stdin = Handle::new(&mut heap, 1);
let environment = Struct::create_with_symbol_keys(
&mut heap,
true,
Expand All @@ -98,24 +98,28 @@ pub(crate) fn run(options: Options) -> ProgramResult {
lir.clone(),
heap,
main,
&[environment],
platform,
&[environment, platform.into()],
StackTracer::default(),
);

let result = loop {
match vm.run_forever() {
StateAfterRunForever::CallingHandle(mut call) => {
if call.handle == stdout {
let message = call.arguments[0];
let [message, _responsible] = call.arguments[..] else {
unreachable!()
};

match message.into() {
Data::Text(text) => println!("{}", text.get()),
_ => info!("Non-text value sent to stdout: {message:?}"),
}
vm = call.complete(Tag::create_nothing());
} else if call.handle == stdin {
print!(">> ");
let [_responsible] = call.arguments[..] else {
unreachable!()
};

io::stdout().flush().unwrap();
let input = {
let stdin = io::stdin();
Expand Down
6 changes: 5 additions & 1 deletion compiler/frontend/src/builtin_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum BuiltinFunction {
TagGetValue, // tag -> any
TagHasValue, // tag -> booleanTag
TagWithoutValue, // tag -> tag
TagWithValue, // tag value -> tag
TextCharacters, // text -> (listOfText: list)
TextConcatenate, // (textA: text) (textB: text) -> (concatenated: text)
TextContains, // text (pattern: text) -> booleanTag
Expand Down Expand Up @@ -94,6 +95,7 @@ impl BuiltinFunction {
Self::TagGetValue => true,
Self::TagHasValue => true,
Self::TagWithoutValue => true,
Self::TagWithValue => true,
Self::TextCharacters => true,
Self::TextConcatenate => true,
Self::TextContains => true,
Expand All @@ -112,7 +114,8 @@ impl BuiltinFunction {

#[must_use]
pub const fn num_parameters(&self) -> usize {
match self {
// Responsibility parameter.
1 + match self {
Self::Equals => 2,
Self::FunctionRun => 1,
Self::GetArgumentCount => 1,
Expand Down Expand Up @@ -144,6 +147,7 @@ impl BuiltinFunction {
Self::TagGetValue => 1,
Self::TagHasValue => 1,
Self::TagWithoutValue => 1,
Self::TagWithValue => 2,
Self::TextCharacters => 1,
Self::TextConcatenate => 2,
Self::TextContains => 2,
Expand Down
Loading