#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
enum {
OP_HALT = 0,
OP_CONST = 1,
OP_ADD = 2,
OP_MUL = 3,
OP_NEG = 4,
OP_COUNT
};
typedef struct {
const uint8_t *code;
size_t code_len;
const double *consts;
size_t const_count;
} Chunk;
#define RUN_OK 0
#define ERR_TRUNCATED -1
#define ERR_BAD_CONST -2
#define ERR_BAD_OPCODE -3
#define ERR_NO_HALT -4
#define ERR_STACK -5
#define STACK_MAX 16
static void push(double *stack, size_t *sp, double v) {
stack[(*sp)++] = v;
}
static double pop(double *stack, size_t *sp) {
return stack[--*sp];
}
int run(const Chunk *chunk, double *result) {
double stack[STACK_MAX];
size_t sp = 0;
size_t pc = 0;
while (pc < chunk->code_len) {
uint8_t op = chunk->code[pc++];
switch (op) {
case OP_HALT:
if (sp == 0) {
return ERR_STACK;
}
*result = stack[sp - 1];
return RUN_OK;
case OP_CONST: {
if (pc >= chunk->code_len) {
return ERR_TRUNCATED;
}
uint8_t idx = chunk->code[pc++];
if (idx >= chunk->const_count) {
return ERR_BAD_CONST;
}
push(stack, &sp, chunk->consts[idx]);
break;
}
case OP_ADD: {
double b = pop(stack, &sp);
double a = pop(stack, &sp);
push(stack, &sp, a + b);
break;
}
case OP_MUL: {
double b = pop(stack, &sp);
double a = pop(stack, &sp);
push(stack, &sp, a * b);
break;
}
case OP_NEG:
push(stack, &sp, -pop(stack, &sp));
break;
default:
return ERR_BAD_OPCODE;
}
}
return ERR_NO_HALT;
}
#ifndef TEST
int main(void) {
static const uint8_t code[] = { OP_ADD, OP_HALT };
static const double consts[] = { 1.0 };
Chunk chunk = { code, sizeof(code), consts, 1 };
double result = 0.0;
int rc = run(&chunk, &result);
printf("pop from empty stack: rc=%d (expected %d)\n", rc, ERR_STACK);
return 0;
}
#else
#include "cmetal_test.h"
TEST(test_well_formed_program_runs) {
static const uint8_t code[] = {
OP_CONST, 0, OP_CONST, 1, OP_ADD, OP_CONST, 2, OP_MUL, OP_HALT
};
static const double consts[] = { 2.0, 3.0, 4.0 };
Chunk chunk = { code, sizeof(code), consts, 3 };
double result = 0.0;
ASSERT_EQ(run(&chunk, &result), RUN_OK);
ASSERT(result == 20.0);
}
TEST(test_underflow_is_reported) {
static const uint8_t add_on_empty[] = { OP_ADD, OP_HALT };
static const uint8_t neg_on_empty[] = { OP_NEG, OP_HALT };
static const double consts[] = { 1.0 };
double result = 0.0;
Chunk a = { add_on_empty, sizeof(add_on_empty), consts, 1 };
ASSERT_EQ(run(&a, &result), ERR_STACK);
Chunk n = { neg_on_empty, sizeof(neg_on_empty), consts, 1 };
ASSERT_EQ(run(&n, &result), ERR_STACK);
}
TEST(test_full_stack_is_legal) {
uint8_t code[STACK_MAX * 2 + 1];
for (int i = 0; i < STACK_MAX; i++) {
code[i * 2] = OP_CONST;
code[i * 2 + 1] = 0;
}
code[STACK_MAX * 2] = OP_HALT;
static const double consts[] = { 7.0 };
Chunk chunk = { code, sizeof(code), consts, 1 };
double result = 0.0;
ASSERT_EQ(run(&chunk, &result), RUN_OK);
ASSERT(result == 7.0);
}
TEST(test_overflow_is_reported) {
uint8_t code[(STACK_MAX + 1) * 2 + 1];
for (int i = 0; i < STACK_MAX + 1; i++) {
code[i * 2] = OP_CONST;
code[i * 2 + 1] = 0;
}
code[(STACK_MAX + 1) * 2] = OP_HALT;
static const double consts[] = { 7.0 };
Chunk chunk = { code, sizeof(code), consts, 1 };
double result = 0.0;
ASSERT_EQ(run(&chunk, &result), ERR_STACK);
}
int main(void) {
RUN_TEST(test_well_formed_program_runs);
RUN_TEST(test_underflow_is_reported);
RUN_TEST(test_full_stack_is_legal);
RUN_TEST(test_overflow_is_reported);
TEST_REPORT();
}
#endif