diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/as_analyzer.c | 18 | ||||
| -rw-r--r-- | src/as_analyzer.h | 6 | ||||
| -rw-r--r-- | src/as_main.c | 4 | ||||
| -rw-r--r-- | src/as_parser.c | 40 | ||||
| -rw-r--r-- | src/as_parser.h | 30 | ||||
| -rw-r--r-- | src/as_tokenizer.c | 24 | ||||
| -rw-r--r-- | src/as_tokenizer.h | 15 | ||||
| -rw-r--r-- | src/fvm.c | 62 | ||||
| -rw-r--r-- | src/fvm.h | 37 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rw-r--r-- | src/utils.c | 8 | ||||
| -rw-r--r-- | src/utils.h | 7 |
12 files changed, 120 insertions, 133 deletions
diff --git a/src/as_analyzer.c b/src/as_analyzer.c index a3e7ea4..f48353f 100644 --- a/src/as_analyzer.c +++ b/src/as_analyzer.c @@ -3,7 +3,7 @@ #include <stddef.h> #include <string.h> -const char * compose_section_label(allocator_t alct, const char * section, const char * name) { +const char * compose_section_label(struct allocator * alct, const char * section, const char * name) { size_t section_len = strlen(section); size_t name_len = strlen(name); size_t sz = section_len + name_len; @@ -14,9 +14,9 @@ const char * compose_section_label(allocator_t alct, const char * section, const return buf; } -void process_section_label(allocator_t alct, prog_t prog) { +void process_section_label(struct allocator * alct, struct prog * prog) { const char * section = ""; - stmt_t* stmts = prog->stmts->stmts; + struct stmt ** stmts = prog->stmts->stmts; for (size_t i = 0; ; i++) { if (stmts[i] == NULL) break; if (stmts[i]->label == NULL) continue; @@ -30,11 +30,11 @@ void process_section_label(allocator_t alct, prog_t prog) { } } -size_t instr_size(instr_t instr) { +size_t instr_size(struct instr * instr) { return op_size(instr->op); } -struct sym_table new_sym_table(allocator_t alct) { +struct sym_table new_sym_table(struct allocator * alct) { struct sym_table tbl; tbl.cap = 16; tbl.size = 0; @@ -42,7 +42,7 @@ struct sym_table new_sym_table(allocator_t alct) { return tbl; } -void sym_table_add(allocator_t alct, struct sym_table* tbl, const char* name, int pos) { +void sym_table_add(struct allocator * alct, struct sym_table* tbl, const char* name, int pos) { if (tbl->cap == tbl->size) { void *old_buf = tbl->buf; tbl->buf = allocate(alct, sizeof(struct sym_table_entry) * tbl->cap * 2); @@ -53,14 +53,14 @@ void sym_table_add(allocator_t alct, struct sym_table* tbl, const char* name, in tbl->size += 1; } -struct sym_table analyze_prog(allocator_t alct, prog_t prog) { +struct sym_table analyze_prog(struct allocator * alct, struct prog * prog) { process_section_label(alct, prog); - stmt_t * stmts = prog->stmts->stmts; + struct stmt * * stmts = prog->stmts->stmts; struct sym_table tbl = new_sym_table(alct); size_t cur_pos = 0; for (int i = 0; ; i++) { if (stmts[i] == NULL) break; - stmt_t stmt = stmts[i]; + struct stmt * stmt = stmts[i]; if (stmt->label) { sym_table_add(alct, &tbl, stmt->label->name, cur_pos); } diff --git a/src/as_analyzer.h b/src/as_analyzer.h index 988cca0..b59fe3c 100644 --- a/src/as_analyzer.h +++ b/src/as_analyzer.h @@ -15,9 +15,9 @@ struct sym_table { struct sym_table_entry *buf; }; -struct sym_table new_sym_table(allocator_t alct); -void sym_table_add(allocator_t alct, struct sym_table* tbl, const char* name, int pos); +struct sym_table new_sym_table(struct allocator * alct); +void sym_table_add(struct allocator * alct, struct sym_table* tbl, const char* name, int pos); -struct sym_table analyze_prog(allocator_t alct, prog_t prog); +struct sym_table analyze_prog(struct allocator * alct, struct prog * prog); #endif // FVM_AS_ANALYZER_H_ diff --git a/src/as_main.c b/src/as_main.c index dc61ea9..6c91a5f 100644 --- a/src/as_main.c +++ b/src/as_main.c @@ -8,9 +8,9 @@ int main(int argc, char** argv) { fprintf(stderr, "usage: fvm-as <inputfile>\n"); return 1; } - allocator_t alct = new_allocator(); + struct allocator * alct = new_allocator(); FILE *fp = fopen(argv[1], "r"); - token_stream_t ts = new_token_stream(alct, fp); + struct token_stream * ts = new_token_stream(alct, fp); delete_allocator(alct); return 0; diff --git a/src/as_parser.c b/src/as_parser.c index 1044e3f..39f8859 100644 --- a/src/as_parser.c +++ b/src/as_parser.c @@ -16,25 +16,25 @@ // <label> ::= tag ":" // <op> ::= "add" | "sub" | "mul" | "div" | "mod" | "eq" | ... -prog_t parse_prog(allocator_t alct, token_stream_t ts) { - prog_t p = allocate(alct, sizeof(struct prog)); +struct prog * parse_prog(struct allocator * alct, struct token_stream * ts) { + struct prog * p = allocate(alct, sizeof(struct prog)); p->stmts = parse_stmts(alct, ts); return p; } -stmts_t parse_stmts(allocator_t alct, token_stream_t ts) { - stmts_t ss = allocate(alct, sizeof(struct stmts)); - ss->stmts = allocate(alct, sizeof(stmt_t)); +struct stmts * parse_stmts(struct allocator * alct, struct token_stream * ts) { + struct stmts * ss = allocate(alct, sizeof(struct stmts)); + ss->stmts = allocate(alct, sizeof(struct stmt *)); ss->stmts[0] = NULL; int capacity = 0; int len = 0; while (peek_token(alct, ts)->type != TK_ENDOFFILE) { - stmt_t s = parse_stmt(alct, ts); + struct stmt * s = parse_stmt(alct, ts); if (s == NULL) continue; if (len == capacity) { size_t new_capacity = capacity * 2 + 1; - void* new_stmts = allocate(alct, sizeof(stmt_t*) * new_capacity); - memcpy(new_stmts, ss->stmts, sizeof(stmt_t*) * capacity); + void* new_stmts = allocate(alct, sizeof(struct stmt **) * new_capacity); + memcpy(new_stmts, ss->stmts, sizeof(struct stmt **) * capacity); ss->stmts = new_stmts; capacity = new_capacity; } @@ -51,13 +51,13 @@ stmts_t parse_stmts(allocator_t alct, token_stream_t ts) { return ss; } -label_t parse_label(allocator_t alct, token_stream_t ts) { - token_t t = next_token(alct, ts); +struct label * parse_label(struct allocator * alct, struct token_stream * ts) { + struct token * t = next_token(alct, ts); if (t->type != TK_TAG) { fprintf(stderr, "%d:%d expect label.\n", t->line, t->col); exit(-1); } - label_t l = allocate(alct, sizeof(label_t)); + struct label * l = allocate(alct, sizeof(struct label *)); l->name = t->sval; t = next_token(alct, ts); if (t->type != TK_COLON) { @@ -67,9 +67,9 @@ label_t parse_label(allocator_t alct, token_stream_t ts) { return l; } -stmt_t parse_stmt(allocator_t alct, token_stream_t ts) { - token_t t = peek_token(alct, ts); - stmt_t stmt = allocate(alct, sizeof(struct stmt)); +struct stmt * parse_stmt(struct allocator * alct, struct token_stream * ts) { + struct token * t = peek_token(alct, ts); + struct stmt * stmt = allocate(alct, sizeof(struct stmt)); stmt->label = NULL; stmt->instr = NULL; if (t->type == TK_TAG) { @@ -92,8 +92,8 @@ stmt_t parse_stmt(allocator_t alct, token_stream_t ts) { exit(-1); } -enum op parse_op(allocator_t alct, token_stream_t ts) { - token_t t = next_token(alct, ts); +enum op parse_op(struct allocator * alct, struct token_stream * ts) { + struct token * t = next_token(alct, ts); enum op op; if (t->type == TK_OP) { op = str2op(t->sval); @@ -108,9 +108,9 @@ enum op parse_op(allocator_t alct, token_stream_t ts) { return op; } -instr_t parse_instr(allocator_t alct, token_stream_t ts) { - token_t t = peek_token(alct, ts); - instr_t i = allocate(alct, sizeof(struct instr)); +struct instr * parse_instr(struct allocator * alct, struct token_stream * ts) { + struct token * t = peek_token(alct, ts); + struct instr * i = allocate(alct, sizeof(struct instr)); i->tag_name = NULL; i->arg = NULL; i->op = OP_END; @@ -118,7 +118,7 @@ instr_t parse_instr(allocator_t alct, token_stream_t ts) { i->op = parse_op(alct, ts); t = peek_token(alct, ts); if (t->type == TK_ARG) { - arg_t a = allocate(alct, sizeof(struct arg)); + struct arg * a = allocate(alct, sizeof(struct arg)); a->ival = t->ival; a->fval = t->fval; i->arg = a; diff --git a/src/as_parser.h b/src/as_parser.h index 0c31df7..4630186 100644 --- a/src/as_parser.h +++ b/src/as_parser.h @@ -10,41 +10,35 @@ struct arg { int64_t ival; double fval; }; -typedef struct arg * arg_t; struct instr { enum op op; - arg_t arg; + struct arg * arg; const char* tag_name; }; -typedef struct instr * instr_t; struct label { const char* name; }; -typedef struct label * label_t; struct stmt { - label_t label; - instr_t instr; + struct label * label; + struct instr * instr; }; -typedef struct stmt * stmt_t; struct stmts { - stmt_t * stmts; + struct stmt ** stmts; }; -typedef struct stmts * stmts_t; struct prog { - stmts_t stmts; + struct stmts * stmts; }; -typedef struct prog * prog_t; - -prog_t parse_prog(allocator_t alct, token_stream_t ts); -stmt_t parse_stmt(allocator_t alct, token_stream_t ts); -stmts_t parse_stmts(allocator_t alct, token_stream_t ts); -instr_t parse_instr(allocator_t alct, token_stream_t ts); -label_t parse_label(allocator_t alct, token_stream_t ts); -enum op parse_op(allocator_t alct, token_stream_t ts); + +struct prog * parse_prog(struct allocator * alct, struct token_stream * ts); +struct stmt * parse_stmt(struct allocator * alct, struct token_stream * ts); +struct stmts * parse_stmts(struct allocator * alct, struct token_stream * ts); +struct instr * parse_instr(struct allocator * alct, struct token_stream * ts); +struct label * parse_label(struct allocator * alct, struct token_stream * ts); +enum op parse_op(struct allocator * alct, struct token_stream * ts); #endif diff --git a/src/as_tokenizer.c b/src/as_tokenizer.c index 216ee8a..6c40b9f 100644 --- a/src/as_tokenizer.c +++ b/src/as_tokenizer.c @@ -6,7 +6,7 @@ #include "as_op.h" #include "utils.h" -int input_stream_next_char(input_stream_t s) { +int input_stream_next_char(struct input_stream * s) { if (s->cursor == -1) { return EOF; } @@ -30,7 +30,7 @@ int input_stream_next_char(input_stream_t s) { return c; } -int input_stream_peek_char(input_stream_t s) { +int input_stream_peek_char(struct input_stream * s) { if (s->cursor == -1) { return EOF; } @@ -71,8 +71,8 @@ int is_part_of_identifier(int c) { return 0; } -token_t next_token_impl(allocator_t alct, input_stream_t s) { - token_t t = allocate(alct, sizeof(struct token)); +struct token * next_token_impl(struct allocator * alct, struct input_stream * s) { + struct token * t = allocate(alct, sizeof(struct token)); int c; while (1) { c = input_stream_peek_char(s); @@ -138,17 +138,17 @@ token_t next_token_impl(allocator_t alct, input_stream_t s) { return t; } -token_t next_token(allocator_t alct, token_stream_t ts) { +struct token * next_token(struct allocator * alct, struct token_stream * ts) { if (ts->buf != NULL) { - token_t t = ts->buf; + struct token * t = ts->buf; ts->buf = NULL; return t; } - token_t t = next_token_impl(alct, ts->s); + struct token * t = next_token_impl(alct, ts->s); return t; } -token_t peek_token(allocator_t alct, token_stream_t ts) { +struct token * peek_token(struct allocator * alct, struct token_stream * ts) { if (ts->buf != NULL) { return ts->buf; } @@ -156,7 +156,7 @@ token_t peek_token(allocator_t alct, token_stream_t ts) { return ts->buf; } -void print_token(token_t t) { +void print_token(struct token * t) { switch (t->type) { case TK_OP: printf("OP: %s, line: %d, col: %d\n", t->sval, t->line, t->col); @@ -179,15 +179,15 @@ void print_token(token_t t) { } } -token_stream_t new_token_stream(allocator_t alct, FILE* fp) { - input_stream_t s = allocate(alct, sizeof(struct input_stream)); +struct token_stream * new_token_stream(struct allocator * alct, FILE* fp) { + struct input_stream * s = allocate(alct, sizeof(struct input_stream)); s->fp = fp; s->buf = allocate(alct, INPUT_STREAM_BUF_SIZE); s->buf_pos = 0; s->cursor = 0; s->line = 1; s->col = 1; - token_stream_t ts = allocate(alct, sizeof(struct token_stream)); + struct token_stream * ts = allocate(alct, sizeof(struct token_stream)); ts->s = s; ts->buf = NULL; return ts; diff --git a/src/as_tokenizer.h b/src/as_tokenizer.h index 72a5d8b..d22ab55 100644 --- a/src/as_tokenizer.h +++ b/src/as_tokenizer.h @@ -18,7 +18,6 @@ struct token { int64_t ival; double fval; }; -typedef struct token * token_t; #define INPUT_STREAM_BUF_SIZE 1024 @@ -30,17 +29,15 @@ struct input_stream{ int line; int col; }; -typedef struct input_stream *input_stream_t; struct token_stream { - token_t buf; - input_stream_t s; + struct token *buf; + struct input_stream *s; }; -typedef struct token_stream *token_stream_t; -token_t next_token(allocator_t alct, token_stream_t ts); -token_t peek_token(allocator_t alct, token_stream_t ts); -void print_token(token_t t); -token_stream_t new_token_stream(allocator_t alct, FILE* fp); +struct token* next_token(struct allocator * alct, struct token_stream * ts); +struct token* peek_token(struct allocator * alct, struct token_stream * ts); +void print_token(struct token *t); +struct token_stream * new_token_stream(struct allocator * alct, FILE* fp); #endif // FMV_AS_TOKENIZER_H_ @@ -7,77 +7,77 @@ int fvm_init(struct fvm *vm, void *code, int64_t stack_size) { void *stack_vm = malloc(stack_size); - vm->sp = (fvm_word_t)(stack_vm + stack_size); + vm->sp = (int64_t)(stack_vm + stack_size); vm->bp = vm->sp; - vm->pc = (fvm_word_t)code; + vm->pc = (int64_t)code; vm->rv = 0; return 1; } -void fvm_push(struct fvm *vm, fvm_word_t val) { - vm->sp -= sizeof(fvm_word_t); +void fvm_push(struct fvm *vm, int64_t val) { + vm->sp -= sizeof(int64_t); fvm_store(vm, vm->sp, val); } int64_t fvm_pop(struct fvm *vm) { int64_t r = fvm_load(vm, vm->sp); - vm->sp += sizeof(fvm_word_t); + vm->sp += sizeof(int64_t); return r; } -void fvm_pushf(struct fvm *vm, fvm_float_t val) { - assert(sizeof(fvm_word_t) >= sizeof(fvm_float_t)); - vm->sp -= sizeof(fvm_word_t); +void fvm_pushf(struct fvm *vm, double val) { + assert(sizeof(int64_t) >= sizeof(double)); + vm->sp -= sizeof(int64_t); fvm_storef(vm, vm->sp, val); } -fvm_float_t fvm_popf(struct fvm *vm) { - assert(sizeof(fvm_word_t) >= sizeof(fvm_float_t)); - fvm_float_t r = fvm_loadf(vm, vm->sp); - vm->sp += sizeof(fvm_word_t); +double fvm_popf(struct fvm *vm) { + assert(sizeof(int64_t) >= sizeof(double)); + double r = fvm_loadf(vm, vm->sp); + vm->sp += sizeof(int64_t); return r; } -fvm_float_t fvm_loadf(struct fvm *vm, fvm_word_t addr) { - return *(fvm_float_t*)addr; +double fvm_loadf(struct fvm *vm, int64_t addr) { + return *(double*)addr; } -void fvm_storef(struct fvm *vm, fvm_word_t addr, fvm_float_t val) { - *(fvm_float_t*)addr = val; +void fvm_storef(struct fvm *vm, int64_t addr, double val) { + *(double*)addr = val; } -fvm_word_t fvm_load(struct fvm *vm, fvm_word_t addr) { - return *(fvm_word_t*)addr; +int64_t fvm_load(struct fvm *vm, int64_t addr) { + return *(int64_t*)addr; } -void fvm_store(struct fvm *vm, fvm_word_t addr, int64_t value) { - *(fvm_word_t*)addr = value; +void fvm_store(struct fvm *vm, int64_t addr, int64_t value) { + *(int64_t*)addr = value; } -int32_t fvm_load32(struct fvm *vm, fvm_word_t addr) { +int32_t fvm_load32(struct fvm *vm, int64_t addr) { return *(int32_t*)addr; } -void fvm_store32(struct fvm *vm, fvm_word_t addr, int32_t value) { +void fvm_store32(struct fvm *vm, int64_t addr, int32_t value) { *(int32_t*)addr = value; } -int16_t fvm_load16(struct fvm *vm, fvm_word_t addr) { +int16_t fvm_load16(struct fvm *vm, int64_t addr) { return *(int16_t*)addr; } -void fvm_store16(struct fvm *vm, fvm_word_t addr, int16_t value) { +void fvm_store16(struct fvm *vm, int64_t addr, int16_t value) { *(int16_t*)addr = value; } -int8_t fvm_load8(struct fvm *vm, fvm_word_t addr) { +int8_t fvm_load8(struct fvm *vm, int64_t addr) { return *(int8_t*)addr; } -void fvm_store8(struct fvm *vm, fvm_word_t addr, int8_t value) { +void fvm_store8(struct fvm *vm, int64_t addr, int8_t value) { *(int8_t*)addr = value; } int64_t fvm_execute(struct fvm *vm) { - fvm_word_t a, b, c; - fvm_float_t x, y, z; + int64_t a, b, c; + double x, y, z; while (1) { enum fvm_op op = (enum fvm_op)(uint8_t)fvm_load8(vm, vm->pc); switch (op) { @@ -113,7 +113,7 @@ int64_t fvm_execute(struct fvm *vm) { break; case FVM_OP_IMM: fvm_push(vm, fvm_load(vm, vm->pc + 1)); - vm->pc += sizeof(fvm_word_t) + 1; + vm->pc += sizeof(int64_t) + 1; break; case FVM_OP_LD: fvm_push(vm, fvm_load(vm, fvm_pop(vm))); @@ -416,12 +416,12 @@ int64_t fvm_execute(struct fvm *vm) { break; case FVM_OP_FTI: x = fvm_popf(vm); - fvm_push(vm, (fvm_word_t)x); + fvm_push(vm, (int64_t)x); vm->pc++; break; case FVM_OP_ITF: a = fvm_pop(vm); - fvm_pushf(vm, (fvm_float_t)x); + fvm_pushf(vm, (double)x); vm->pc++; break; case FVM_OP_EXIT: @@ -3,20 +3,17 @@ #include <stdint.h> -typedef int64_t fvm_word_t; -typedef double fvm_float_t;; - struct fvm; // return non-zero if vm should be suspended -typedef void (*fvm_syscall_fn_t)(struct fvm* vm); +typedef void (*fvm_syscall_fn)(struct fvm* vm); struct fvm { - fvm_word_t sp; // stack pointer - fvm_word_t bp; // base pointer - fvm_word_t pc; // programm counter - fvm_word_t rv; // return value - fvm_syscall_fn_t *syscall_table; + int64_t sp; // stack pointer + int64_t bp; // base pointer + int64_t pc; // programm counter + int64_t rv; // return value + fvm_syscall_fn *syscall_table; }; enum fvm_op { @@ -100,22 +97,22 @@ enum fvm_op { int fvm_init(struct fvm *vm, void *code, int64_t stack_size); int64_t fvm_execute(struct fvm *vm); -void fvm_push(struct fvm *vm, fvm_word_t value); +void fvm_push(struct fvm *vm, int64_t value); int64_t fvm_pop(struct fvm *vm); -fvm_float_t fvm_loadf(struct fvm *vm, fvm_word_t addr); -void fvm_storef(struct fvm *vm, fvm_word_t addr, fvm_float_t val); +double fvm_loadf(struct fvm *vm, int64_t addr); +void fvm_storef(struct fvm *vm, int64_t addr, double val); -fvm_word_t fvm_load(struct fvm *vm, fvm_word_t addr); -void fvm_store(struct fvm *vm, fvm_word_t addr, int64_t value); +int64_t fvm_load(struct fvm *vm, int64_t addr); +void fvm_store(struct fvm *vm, int64_t addr, int64_t value); -int32_t fvm_load32(struct fvm *vm, fvm_word_t addr); -void fvm_store32(struct fvm *vm, fvm_word_t addr, int32_t value); +int32_t fvm_load32(struct fvm *vm, int64_t addr); +void fvm_store32(struct fvm *vm, int64_t addr, int32_t value); -int16_t fvm_load16(struct fvm *vm, fvm_word_t addr); -void fvm_store16(struct fvm *vm, fvm_word_t addr, int16_t value); +int16_t fvm_load16(struct fvm *vm, int64_t addr); +void fvm_store16(struct fvm *vm, int64_t addr, int16_t value); -int8_t fvm_load8(struct fvm *vm, fvm_word_t addr); -void fvm_store8(struct fvm *vm, fvm_word_t addr, int8_t value); +int8_t fvm_load8(struct fvm *vm, int64_t addr); +void fvm_store8(struct fvm *vm, int64_t addr, int8_t value); #endif @@ -15,7 +15,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Usage: fvm <program-file>\n"); return -1; } - vm.syscall_table = malloc(256 * sizeof(fvm_syscall_fn_t)); + vm.syscall_table = malloc(256 * sizeof(fvm_syscall_fn)); vm.syscall_table[1] = &print_num; FILE* fp = fopen(argv[1], "rb"); if (fp == NULL) { diff --git a/src/utils.c b/src/utils.c index b9e7eaf..fdd8452 100644 --- a/src/utils.c +++ b/src/utils.c @@ -9,8 +9,8 @@ struct allocator { size_t len; }; -allocator_t new_allocator() { - allocator_t alct = malloc(sizeof(struct allocator)); +struct allocator * new_allocator() { + struct allocator * alct = malloc(sizeof(struct allocator)); alct->bufs = malloc(sizeof(void*) * 16); alct->cap = 16; alct->len = 0; @@ -18,7 +18,7 @@ allocator_t new_allocator() { return alct; } -void delete_allocator(allocator_t alct) { +void delete_allocator(struct allocator * alct) { for (size_t i = 0; i < alct->len; i++) { free(alct->bufs[i]); } @@ -26,7 +26,7 @@ void delete_allocator(allocator_t alct) { free(alct); } -void * allocate(allocator_t alct, size_t size) { +void * allocate(struct allocator * alct, size_t size) { assert(size > 0); if (alct->len >= alct->cap) { alct->cap = alct->cap * 2; // Doubling the capacity diff --git a/src/utils.h b/src/utils.h index 1a88220..2381385 100644 --- a/src/utils.h +++ b/src/utils.h @@ -4,11 +4,10 @@ #include <stdlib.h> struct allocator; -typedef struct allocator * allocator_t; -allocator_t new_allocator(); -void delete_allocator(allocator_t allocator); +struct allocator * new_allocator(); +void delete_allocator(struct allocator * allocator); -void* allocate(allocator_t allocator, size_t size); +void* allocate(struct allocator * allocator, size_t size); #endif // FVM_UTILS_H_ |
