diff --git a/lang/boolean.c b/lang/boolean.c index e96cf39..9cb7037 100644 --- a/lang/boolean.c +++ b/lang/boolean.c @@ -16,7 +16,7 @@ Object boolean_create(VmState state, bool value) { Object obj = object_create(state, &boolean_class, sizeof(struct boolean)); - abort_if(!obj, "unable to allocate boolean"); + abort_if(state, !obj, "unable to allocate boolean"); struct boolean *boolean = (struct boolean *)object_priv(state, obj, &boolean_class); boolean->value = value; @@ -37,7 +37,8 @@ static void boolean_invert(VmState state, Object obj, void *priv) { (void)priv; int arg_count = vm_stack_depth(state); - abort_if(arg_count != 1, "boolean_invert called with extra arguments"); + abort_if(state, arg_count != 1, + "boolean_invert called with extra arguments"); bool value = boolean_value(state, obj); bool invert = !value; vm_stack_set(state, 0, boolean_create(state, invert)); diff --git a/lang/bytecode.c b/lang/bytecode.c index f0ab824..396347b 100644 --- a/lang/bytecode.c +++ b/lang/bytecode.c @@ -48,7 +48,8 @@ // First argument is a 1-byte boolean case OP_BOOLEAN: { int value = *pos_code++; - abort_if(value != 0 && value != 1, "invalid boolean"); + abort_if(state, value != 0 && value != 1, + "invalid boolean"); vm_stack_push(state, boolean_create(state, value)); break; } @@ -117,7 +118,7 @@ // First argument is the expected depth case OP_DEPTH_CHECK: { int depth = *pos_code++; - abort_if(vm_stack_depth(state) != depth, + abort_if(state, vm_stack_depth(state) != depth, "stack depth mismatch"); break; } @@ -149,9 +150,9 @@ } // Unknown op, error out default: - abort_msg("bytecode_run: Unknown op"); + abort_msg(state, "bytecode_run: Unknown op"); } } // Uh-oh, no more bytecode? - abort_msg("bytecode_run: Ran out of bytecode to run!"); + abort_msg(state, "bytecode_run: Ran out of bytecode to run!"); } diff --git a/lang/compile.py b/lang/compile.py index eb46433..ce057fb 100755 --- a/lang/compile.py +++ b/lang/compile.py @@ -753,7 +753,7 @@ Object module_create(VmState state) { (void)state; Object obj = object_create(state, &module_class, 1); - abort_if(!obj, "unable to allocate module"); + abort_if(state, !obj, "unable to allocate module"); return obj; } diff --git a/lang/error.c b/lang/error.c index 3e472a5..16e001b 100644 --- a/lang/error.c +++ b/lang/error.c @@ -6,14 +6,15 @@ #include // Aborts the program with a message -__attribute__((noreturn)) void abort_msg(const char *msg) { +__attribute__((noreturn)) void abort_msg(VmState state, const char *msg) { + (void)state; fprintf(stderr, "ERROR: %s\n", msg); abort(); } // Aborts the program if a value is true -void abort_if(int value, const char *msg) { +void abort_if(VmState state, int value, const char *msg) { if (value) { - abort_msg(msg); + abort_msg(state, msg); } } diff --git a/lang/error.h b/lang/error.h index 8885930..b374544 100644 --- a/lang/error.h +++ b/lang/error.h @@ -4,10 +4,12 @@ #ifndef ERROR_H #define ERROR_H +#include "types.h" + // Aborts the program with a message -__attribute__((noreturn)) void abort_msg(const char *msg); +__attribute__((noreturn)) void abort_msg(VmState state, const char *msg); // Aborts the program if a value is true -void abort_if(int value, const char *msg); +void abort_if(VmState state, int value, const char *msg); #endif diff --git a/lang/main.c b/lang/main.c index 41e2ee2..c025a74 100644 --- a/lang/main.c +++ b/lang/main.c @@ -17,7 +17,8 @@ vm_call(state, numA, "Add", 2); Object numC = vm_stack_pop(state); printf("numC value is %i\n", number_value(state, numC)); - abort_if(number_value(state, numC) != 8, "add return value is not 8"); + abort_if(state, number_value(state, numC) != 8, + "add return value is not 8"); object_drop(state, &numA); object_drop(state, &numC); } @@ -29,23 +30,23 @@ vm_call(state, module, "MakeNumber", 1); Object numA = vm_stack_pop(state); printf("code return value is %i\n", number_value(state, numA)); - abort_if(number_value(state, numA) != 1234, + abort_if(state, number_value(state, numA) != 1234, "code return value is not 1234"); vm_stack_push(state, object_none()); vm_call(state, module, "BoolTest", 1); Object boolA = vm_stack_pop(state); - abort_if(boolean_value(state, boolA) != false, + abort_if(state, boolean_value(state, boolA) != false, "code return bool is not false"); object_drop(state, &boolA); vm_stack_push(state, object_none()); vm_call(state, module, "NoneTest", 1); Object noneA = vm_stack_pop(state); - abort_if(noneA != object_none(), "code return none is not none"); + abort_if(state, noneA != object_none(), "code return none is not none"); object_drop(state, &noneA); vm_stack_push(state, object_none()); vm_call(state, module, "IfTest", 1); Object ifA = vm_stack_pop(state); - abort_if(boolean_value(state, ifA) == false, + abort_if(state, boolean_value(state, ifA) == false, "if return bool is not true"); object_drop(state, &ifA); vm_stack_push(state, object_none()); diff --git a/lang/number.c b/lang/number.c index 1e134e7..6a27025 100644 --- a/lang/number.c +++ b/lang/number.c @@ -18,7 +18,7 @@ Object number_create(VmState state, int value) { Object obj = object_create(state, &num_class, sizeof(struct number)); - abort_if(!obj, "unable to allocate number"); + abort_if(state, !obj, "unable to allocate number"); struct number *num = (struct number *)object_priv(state, obj, &num_class); mpq_init(num->value); @@ -48,7 +48,8 @@ static void number_math(VmState state, Object obj, void *priv) { int arg_count = vm_stack_depth(state); - abort_if(arg_count != 2, "number_math called without 2 arguments"); + abort_if(state, arg_count != 2, + "number_math called without 2 arguments"); Object arg1 = vm_stack_get(state, 1); int result = 0; if (priv == OP_ADD) { @@ -56,7 +57,7 @@ } else if (priv == OP_SUBTRACT) { result = number_value(state, obj) - number_value(state, arg1); } else { - abort_msg("number_math called with invalid priv"); + abort_msg(state, "number_math called with invalid priv"); } vm_stack_set(state, 0, number_create(state, result)); vm_stack_drop(state, 1); @@ -66,7 +67,8 @@ static void number_equals(VmState state, Object obj, void *priv) { (void)priv; int arg_count = vm_stack_depth(state); - abort_if(arg_count != 2, "number_equals called without 2 arguments"); + abort_if(state, arg_count != 2, + "number_equals called without 2 arguments"); Object arg1 = vm_stack_get(state, 1); bool equals = number_value(state, obj) == number_value(state, arg1); vm_stack_set(state, 0, boolean_create(state, equals)); diff --git a/lang/object.c b/lang/object.c index 7d00c77..dd0c129 100644 --- a/lang/object.c +++ b/lang/object.c @@ -18,11 +18,11 @@ Object object_create(VmState state, struct object_class *class, int priv_size) { (void)state; - abort_if(priv_size < 1, "object_create: priv_size too small"); + abort_if(state, priv_size < 1, "object_create: priv_size too small"); size_t size = sizeof(struct object) + priv_size; struct object *obj = calloc(size, 1); - abort_if( - obj == NULL, "object_create: not enough memory for an objects"); + abort_if(state, obj == NULL, + "object_create: not enough memory for an objects"); obj->class_data = class; obj->ref_count = 1; return (Object)obj; @@ -30,9 +30,11 @@ char *object_priv(VmState state, Object object, struct object_class *class) { (void)state; - abort_if(object == object_none(), "object_priv: no priv on none"); + abort_if( + state, object == object_none(), "object_priv: no priv on none"); struct object *obj = (struct object *)object; - abort_if(obj->class_data != class, "object_priv: incompatible class"); + abort_if(state, obj->class_data != class, + "object_priv: incompatible class"); return obj->priv_data; } @@ -60,7 +62,7 @@ void dispatch_call(VmState state, Object object, const char *name) { if (object == object_none()) { - abort_msg("dispatch_call: cannot dispatch on none"); + abort_msg(state, "dispatch_call: cannot dispatch on none"); } struct object *obj = (struct object *)object; struct object_call *call = obj->class_data->calls; @@ -71,5 +73,5 @@ } ++call; } - abort_msg("dispatch_calll: no call found to dispatch"); + abort_msg(state, "dispatch_calll: no call found to dispatch"); } diff --git a/lang/vm.c b/lang/vm.c index 200fa23..c54a232 100644 --- a/lang/vm.c +++ b/lang/vm.c @@ -18,14 +18,15 @@ VmState vm_create(void) { struct vm_state *state = malloc(sizeof(struct vm_state)); - abort_if(state == NULL, "vm_create: unable to allocate state"); + abort_if(state, state == NULL, "vm_create: unable to allocate state"); state->stack_size = 16; state->stack_base = 0; state->stack_next = 0; state->tail_obj = object_none(); state->tail_name = NULL; state->stack = malloc(sizeof(Object) * state->stack_size); - abort_if(state->stack == NULL, "vm_create: unable to allocate stack"); + abort_if(state, state->stack == NULL, + "vm_create: unable to allocate stack"); return (VmState)state; } @@ -38,8 +39,9 @@ void vm_stack_set(VmState state, int index, Object obj) { struct vm_state *priv = (struct vm_state *)state; int offset = priv->stack_base + index; - abort_if(index < 0, "vm_stack_set: negative value"); - abort_if(offset >= priv->stack_next, "vm_stack_set: stack overflow"); + abort_if(state, index < 0, "vm_stack_set: negative value"); + abort_if(state, offset >= priv->stack_next, + "vm_stack_set: stack overflow"); Object *stack_pos = &priv->stack[offset]; object_drop(state, stack_pos); *stack_pos = obj; @@ -48,8 +50,9 @@ Object vm_stack_get(VmState state, int index) { struct vm_state *priv = (struct vm_state *)state; int offset = priv->stack_base + index; - abort_if(index < 0, "vm_stack_get: negative value"); - abort_if(offset >= priv->stack_next, "vm_stack_get: stack overflow"); + abort_if(state, index < 0, "vm_stack_get: negative value"); + abort_if(state, offset >= priv->stack_next, + "vm_stack_get: stack overflow"); Object obj = priv->stack[offset]; object_hold(state, obj); return obj; @@ -62,13 +65,14 @@ int max_stack = priv->stack_size - 1; // Check if stack_next is outside the stack before // writing to it - abort_if(priv->stack_next > max_stack, "vm_stack_push: stack overflow"); + abort_if(state, priv->stack_next > max_stack, + "vm_stack_push: stack overflow"); priv->stack[priv->stack_next++] = obj; } Object vm_stack_pop(VmState state) { struct vm_state *priv = (struct vm_state *)state; - abort_if(priv->stack_next <= priv->stack_base, + abort_if(state, priv->stack_next <= priv->stack_base, "vm_stack_pop: stack base underflow"); Object obj = priv->stack[--priv->stack_next]; priv->stack[priv->stack_next] = object_none(); @@ -76,7 +80,7 @@ } void vm_stack_drop(VmState state, int count) { - abort_if(count < 1, "vm_stack_drop: invalid drop count"); + abort_if(state, count < 1, "vm_stack_drop: invalid drop count"); while (count--) { Object obj = vm_stack_pop(state); object_drop(state, &obj);