Made pointer syntax consistent

This commit is contained in:
Joseph Kogut 2013-01-23 11:06:19 -07:00
parent caf4767696
commit d270cc5706
14 changed files with 121 additions and 129 deletions

View file

@ -9,17 +9,17 @@
typedef struct tvm_s
{
tvm_program_t* pProgram;
tvm_memory_t* pMemory;
tvm_program_t *pProgram;
tvm_memory_t *pMemory;
} tvm_t;
tvm_t* tvm_create();
void tvm_destroy(tvm_t* vm);
tvm_t *tvm_create();
void tvm_destroy(tvm_t *vm);
int tvm_interpret(tvm_t* vm, char* filename);
void tvm_run(tvm_t* vm);
int tvm_interpret(tvm_t *vm, char *filename);
void tvm_run(tvm_t *vm);
inline void tvm_step(tvm_t* vm, int* instr_idx)
inline void tvm_step(tvm_t *vm, int *instr_idx)
{
int *arg0 = vm->pProgram->args[*instr_idx][0], *arg1 = vm->pProgram->args[*instr_idx][1];

View file

@ -5,12 +5,12 @@
#include <stdlib.h>
#include <string.h>
FILE* tvm_fopen(const char* filename, const char* extension, const char* mode);
FILE *tvm_fopen(const char *filename, const char *extension, const char *mode);
/* Copy the contents of a file from file handle "src" to char array "dest" */
int tvm_fcopy(char* dest, size_t size, FILE* src);
int tvm_fcopy(char *dest, size_t size, FILE *src);
/* Get the length of a file in bytes (including EOF character) */
int tvm_flength(FILE* f);
int tvm_flength(FILE *f);
#endif

View file

@ -6,22 +6,22 @@
typedef struct tvm_htable_node_s
{
char* key;
char *key;
int value;
struct tvm_htable_node_s* next;
struct tvm_htable_node_s *next;
} tvm_htable_node_t;
typedef struct tvm_htab_s
{
unsigned int num_nodes;
unsigned int size;
tvm_htable_node_t** nodes;
tvm_htable_node_t **nodes;
} tvm_htab_t;
tvm_htab_t* create_htab();
void destroy_htab(tvm_htab_t* htab);
void destroy_htab(tvm_htab_t *htab);
int htab_add(tvm_htab_t* htab, const char* key, int value);
int htab_find(tvm_htab_t* htab, const char* key);
int htab_add(tvm_htab_t *htab, const char *key, int value);
int htab_find(tvm_htab_t *htab, const char *key);
#endif

View file

@ -6,14 +6,14 @@
typedef struct tvm_lexer_s
{
char** source_lines;
char*** tokens;
char **source_lines;
char ***tokens;
} tvm_lexer_t;
tvm_lexer_t* lexer_create();
void lexer_destroy(tvm_lexer_t* l);
tvm_lexer_t *lexer_create();
void lexer_destroy(tvm_lexer_t *l);
/* Tokenize the character array "source" into lines and tokens */
void lex(tvm_lexer_t* lexer, char* source);
void lex(tvm_lexer_t *lexer, char *source);
#endif

View file

@ -9,7 +9,7 @@
typedef union
{
int32_t i32;
int32_t* i32_ptr;
int32_t *i32_ptr;
union
{
@ -32,13 +32,13 @@ typedef struct
int FLAGS;
int remainder;
void* mem_space;
void *mem_space;
int mem_space_size;
tvm_register_t* registers;
tvm_register_t *registers;
} tvm_memory_t;
tvm_memory_t* create_memory(size_t size);
void destroy_memory(tvm_memory_t* mem);
tvm_memory_t *create_memory(size_t size);
void destroy_memory(tvm_memory_t *mem);
#endif

View file

@ -13,26 +13,26 @@ typedef struct tvm_program_s
int start;
int num_instructions;
int* instr;
int *instr;
int*** args;
int ***args;
int** values;
int **values;
int num_values;
tvm_htab_t* label_htab;
tvm_htab_t *label_htab;
} tvm_program_t;
/* Create and initialize an empty program object */
tvm_program_t* create_program();
tvm_program_t *create_program();
/* Interpret a source file into bytecode, and store it in a program object */
int interpret_program(tvm_program_t* p, char* filename, tvm_memory_t* pMemory);
int interpret_program(tvm_program_t *p, char *filename, tvm_memory_t *pMemory);
void destroy_program(tvm_program_t* p);
void destroy_program(tvm_program_t *p);
int* tvm_add_value(tvm_program_t* p, const int val);
int tvm_parse_value(const char* str);
int *tvm_add_value(tvm_program_t *p, const int val);
int tvm_parse_value(const char *str);
#endif

View file

@ -6,9 +6,23 @@
#include "tvm_memory.h"
/* Initialize our stack by setting the base pointer and stack pointer */
void create_stack(tvm_memory_t* mem, size_t size);
void stack_push(tvm_memory_t* mem, int* item);
void stack_pop(tvm_memory_t* mem, int* dest);
inline void create_stack(tvm_memory_t *mem, size_t size)
{
mem->registers[0x7].i32_ptr = ((int32_t *)mem->mem_space) + (size / sizeof(int32_t));
mem->registers[0x6].i32_ptr = mem->registers[0x7].i32_ptr;
}
inline void stack_push(tvm_memory_t *mem, int *item)
{
mem->registers[0x6].i32_ptr -= 1;
*mem->registers[0x6].i32_ptr = *item;
}
inline void stack_pop(tvm_memory_t *mem, int *dest)
{
*dest = *mem->registers[0x6].i32_ptr;
mem->registers[0x6].i32_ptr += 1;
}
#endif

View file

@ -1,8 +1,8 @@
#include <tvm/tvm.h>
tvm_t* tvm_create(char* filename)
tvm_t *tvm_create(char *filename)
{
tvm_t* vm = (tvm_t*)malloc(sizeof(tvm_t));
tvm_t *vm = (tvm_t*)malloc(sizeof(tvm_t));
vm->pMemory = create_memory(MIN_MEMORY_SIZE);
vm->pProgram = create_program();
@ -14,22 +14,22 @@ tvm_t* tvm_create(char* filename)
return vm;
}
int tvm_interpret(tvm_t* vm, char* filename)
int tvm_interpret(tvm_t *vm, char *filename)
{
if(interpret_program(vm->pProgram, filename, vm->pMemory) != 0) return 1;
return 0;
}
void tvm_destroy(tvm_t* vm)
void tvm_destroy(tvm_t *vm)
{
if(vm && vm->pMemory)destroy_memory(vm->pMemory);
if(vm && vm->pProgram)destroy_program(vm->pProgram);
if(vm) free(vm);
}
void tvm_run(tvm_t* vm)
void tvm_run(tvm_t *vm)
{
int* instr_idx = &vm->pMemory->registers[0x8].i32; *instr_idx = vm->pProgram->start;
int *instr_idx = &vm->pMemory->registers[0x8].i32; *instr_idx = vm->pProgram->start;
for(;vm->pProgram->instr[*instr_idx] != -0x1; ++(*instr_idx)) tvm_step(vm, instr_idx);
}

View file

@ -1,10 +1,10 @@
#include <tvm/tvm_file.h>
FILE* tvm_fopen(const char* filename, const char* extension, const char* mode)
FILE *tvm_fopen(const char *filename, const char *extension, const char *mode)
{
FILE* pFile = NULL;
FILE *pFile = NULL;
size_t fname_chars = strlen(filename) + strlen(extension) + 1;
char* fname = malloc(sizeof(char) * fname_chars);
char *fname = malloc(sizeof(char) * fname_chars);
int i;
strcpy(fname, filename);
@ -19,7 +19,7 @@ FILE* tvm_fopen(const char* filename, const char* extension, const char* mode)
return pFile;
}
int tvm_fcopy(char* dest, size_t size, FILE* src)
int tvm_fcopy(char *dest, size_t size, FILE* src)
{
size_t i;
long pos = ftell(src);
@ -32,11 +32,11 @@ int tvm_fcopy(char* dest, size_t size, FILE* src)
return 0;
}
int tvm_flength(FILE* f)
int tvm_flength(FILE *f)
{
int length;
long pos = ftell(f);
for(length = 0; !feof(f); length++) fgetc(f);
fseek(f, pos, SEEK_SET);

View file

@ -3,16 +3,16 @@
#include <tvm/tvm_hashtab.h>
tvm_htab_t* create_htab()
tvm_htab_t *create_htab()
{
tvm_htab_t *htab = (tvm_htab_t *)malloc(sizeof(tvm_htab_t));
htab->size = HTAB_SIZE;
htab->nodes = (tvm_htable_node_t**)calloc(htab->size, sizeof(tvm_htable_node_t *));
htab->nodes = (tvm_htable_node_t **)calloc(htab->size, sizeof(tvm_htable_node_t *));
htab->num_nodes = 0;
return htab;
}
void destroy_htab(tvm_htab_t* htab)
void destroy_htab(tvm_htab_t *htab)
{
int i;
tvm_htable_node_t *node, *next;
@ -33,24 +33,24 @@ void destroy_htab(tvm_htab_t* htab)
free(htab);
}
static inline unsigned htab_hash(const char* k, const unsigned int size)
static inline unsigned htab_hash(const char *k, const unsigned int size)
{
unsigned int hash = 1;
char* c; for(c = (char*)k; *c; c++)
char *c; for(c = (char*)k; *c; c++)
hash += (hash << *c) - *c;
return hash % size;
}
static void htab_rehash(tvm_htab_t* orig, unsigned int size)
static void htab_rehash(tvm_htab_t *orig, unsigned int size)
{
int i;
tvm_htable_node_t *node, *next;
tvm_htab_t *new;
new = (tvm_htab_t *)malloc(sizeof(tvm_htab_t));
new->nodes = (tvm_htable_node_t**)calloc(size, sizeof(tvm_htable_node_t *));
new->nodes = (tvm_htable_node_t **)calloc(size, sizeof(tvm_htable_node_t *));
new->size = size;
new->num_nodes = 0;
@ -80,7 +80,7 @@ static void htab_rehash(tvm_htab_t* orig, unsigned int size)
free(new);
}
int htab_add(tvm_htab_t* htab, const char* k, int v)
int htab_add(tvm_htab_t *htab, const char *k, int v)
{
int hash = htab_hash(k, htab->size);
tvm_htable_node_t *node = htab->nodes[hash];
@ -99,7 +99,7 @@ int htab_add(tvm_htab_t* htab, const char* k, int v)
node = calloc(1, sizeof(tvm_htable_node_t));
node->key = (char*)malloc(sizeof(char) * (strlen(k) + 1));
node->key = (char *)malloc(sizeof(char) * (strlen(k) + 1));
strcpy(node->key, k);
node->value = v;
@ -119,7 +119,7 @@ int htab_add(tvm_htab_t* htab, const char* k, int v)
return 0;
}
int htab_find(tvm_htab_t* htab, const char* key)
int htab_find(tvm_htab_t *htab, const char *key)
{
int hash = htab_hash(key, htab->size);
tvm_htable_node_t *node = htab->nodes[hash];

View file

@ -4,12 +4,12 @@
#include <stdlib.h>
#include <string.h>
tvm_lexer_t* lexer_create()
tvm_lexer_t *lexer_create()
{
return (tvm_lexer_t*)calloc(1, sizeof(tvm_lexer_t));
return (tvm_lexer_t *)calloc(1, sizeof(tvm_lexer_t));
}
void lexer_destroy(tvm_lexer_t* lexer)
void lexer_destroy(tvm_lexer_t *lexer)
{
int i, j;
for(i = 0; lexer->source_lines[i]; i++) free(lexer->source_lines[i]);
@ -27,7 +27,7 @@ void lexer_destroy(tvm_lexer_t* lexer)
free(lexer);
}
void lex(tvm_lexer_t* lexer, char* source)
void lex(tvm_lexer_t *lexer, char *source)
{
int i, j;
char *pToken, *pLine = strtok(source, "\n");
@ -35,10 +35,10 @@ void lex(tvm_lexer_t* lexer, char* source)
/* Split the source into individual lines */
for(i = 0; pLine; i++)
{
char* comment_delimiter;
char *comment_delimiter;
lexer->source_lines = (char**)realloc(lexer->source_lines, sizeof(char*) * (i + 2));
lexer->source_lines[i] = (char*)calloc(1, strlen(pLine) + 1);
lexer->source_lines = (char **)realloc(lexer->source_lines, sizeof(char *) * (i + 2));
lexer->source_lines[i] = (char *)calloc(1, strlen(pLine) + 1);
strcpy(lexer->source_lines[i], pLine);
@ -56,14 +56,14 @@ void lex(tvm_lexer_t* lexer, char* source)
/* Split the source into individual tokens */
for(i = 0; lexer->source_lines[i]; i++)
{
lexer->tokens = (char***)realloc(lexer->tokens, sizeof(char**) * (i + 2));
lexer->tokens[i] = (char**)calloc(MAX_TOKENS, sizeof(char*));
lexer->tokens = (char ***)realloc(lexer->tokens, sizeof(char **) * (i + 2));
lexer->tokens[i] = (char **)calloc(MAX_TOKENS, sizeof(char *));
pToken = strtok(lexer->source_lines[i], " \t,");
for(j = 0; (pToken && j < MAX_TOKENS); j++)
{
lexer->tokens[i][j] = (char*)calloc(1, (strlen(pToken) + 1));
lexer->tokens[i][j] = (char *)calloc(1, (strlen(pToken) + 1));
strcpy(lexer->tokens[i][j], pToken);
pToken = strtok(NULL, " \t,");

View file

@ -5,19 +5,19 @@
#define NUM_REGISTERS 17
tvm_memory_t* create_memory(size_t size)
tvm_memory_t *create_memory(size_t size)
{
tvm_memory_t* m = (tvm_memory_t*)calloc(1, sizeof(tvm_memory_t));
tvm_memory_t *m = (tvm_memory_t *)calloc(1, sizeof(tvm_memory_t));
m->registers = calloc(NUM_REGISTERS, sizeof(tvm_register_t));
m->mem_space_size = size;
m->mem_space = (int*)calloc(size, 1);
m->mem_space = (int *)calloc(size, 1);
return m;
}
void destroy_memory(tvm_memory_t* m)
void destroy_memory(tvm_memory_t *m)
{
free(m->mem_space);
free(m->registers);

View file

@ -2,26 +2,26 @@
#include <tvm/tvm_program.h>
#include <tvm/tvm_lexer.h>
const char* tvm_opcode_map[] = {"nop", "int", "mov", "push", "pop", "pushf", "popf", "inc", "dec", "add", "sub", "mul", "div", "mod", "rem",
const char *tvm_opcode_map[] = {"nop", "int", "mov", "push", "pop", "pushf", "popf", "inc", "dec", "add", "sub", "mul", "div", "mod", "rem",
"not", "xor", "or", "and", "shl", "shr", "cmp", "jmp", "call", "ret", "je", "jne", "jg", "jge", "jl", "jle", "prn", 0};
const char* tvm_register_map[] = {"eax", "ebx", "ecx", "edx", "esi", "edi", "esp", "ebp", "eip", "r08", "r09", "r10", "r11", "r12", "r13", "r14", "r15", 0};
const char *tvm_register_map[] = {"eax", "ebx", "ecx", "edx", "esi", "edi", "esp", "ebp", "eip", "r08", "r09", "r10", "r11", "r12", "r13", "r14", "r15", 0};
static int parse_labels(tvm_program_t* p, const char*** tokens);
static int parse_instructions(tvm_program_t* p, const char*** tokens, tvm_memory_t* pMemory);
static int parse_labels(tvm_program_t *p, const char ***tokens);
static int parse_instructions(tvm_program_t *p, const char ***tokens, tvm_memory_t *pMemory);
static int* token_to_register(const char* token, tvm_memory_t* pMemory);
static int instr_to_opcode(const char* instr);
static int *token_to_register(const char *token, tvm_memory_t *pMemory);
static int instr_to_opcode(const char *instr);
tvm_program_t* create_program()
tvm_program_t *create_program()
{
tvm_program_t* p = (tvm_program_t*)calloc(1, sizeof(tvm_program_t));
tvm_program_t *p = (tvm_program_t *)calloc(1, sizeof(tvm_program_t));
p->label_htab = create_htab();
return p;
}
void destroy_program(tvm_program_t* p)
void destroy_program(tvm_program_t *p)
{
int i = 0;
destroy_htab(p->label_htab);
@ -42,15 +42,15 @@ void destroy_program(tvm_program_t* p)
free(p);
}
int interpret_program(tvm_program_t* p, char* filename, tvm_memory_t* pMemory)
int interpret_program(tvm_program_t *p, char *filename, tvm_memory_t *pMemory)
{
int i;
FILE* pFile = NULL;
FILE *pFile = NULL;
int source_length = 0;
char* source = NULL;
char *source = NULL;
tvm_lexer_t* lexer = NULL;
tvm_lexer_t *lexer = NULL;
/* Attempt to open the file. If the file cannot be opened, try once more. */
if(filename)
@ -74,17 +74,17 @@ int interpret_program(tvm_program_t* p, char* filename, tvm_memory_t* pMemory)
free(source);
fclose(pFile);
if(parse_labels(p, (const char***)lexer->tokens) != 0)
if(parse_labels(p, (const char ***)lexer->tokens) != 0)
return 1;
if(parse_instructions(p, (const char***)lexer->tokens, pMemory) != 0)
if(parse_instructions(p, (const char ***)lexer->tokens, pMemory) != 0)
return 1;
lexer_destroy(lexer);
return 0;
}
int parse_labels(tvm_program_t* p, const char*** tokens)
int parse_labels(tvm_program_t *p, const char ***tokens)
{
int i, num_instructions = 0;
for(i = 0; tokens[i]; i++)
@ -92,7 +92,7 @@ int parse_labels(tvm_program_t* p, const char*** tokens)
int token_idx, valid_instruction = 0;
for(token_idx = 0; token_idx < MAX_TOKENS; token_idx++)
{
char* label_delimiter;
char *label_delimiter;
/* If the token is empty, or non-existent, skip it */
if(!tokens[i][token_idx]) continue;
@ -132,16 +132,16 @@ int parse_labels(tvm_program_t* p, const char*** tokens)
return 0;
}
int parse_instructions(tvm_program_t* p, const char*** tokens, tvm_memory_t* pMemory)
int parse_instructions(tvm_program_t *p, const char ***tokens, tvm_memory_t *pMemory)
{
int line_idx;
for(line_idx = 0; tokens[line_idx]; line_idx++)
{
p->instr = (int*)realloc(p->instr, sizeof(int) * (line_idx + 2));
p->instr = (int *)realloc(p->instr, sizeof(int) * (line_idx + 2));
p->instr[line_idx] = 0;
p->args = (int***)realloc(p->args, sizeof(int**) * (line_idx + 2));
p->args[line_idx] = (int**)calloc(MAX_ARGS, sizeof(int*));
p->args = (int ***)realloc(p->args, sizeof(int **) * (line_idx + 2));
p->args[line_idx] = (int **)calloc(MAX_ARGS, sizeof(int *));
int token_idx;
for(token_idx = 0; token_idx < MAX_TOKENS; token_idx++)
@ -161,14 +161,13 @@ int parse_instructions(tvm_program_t* p, const char*** tokens, tvm_memory_t* pMe
for(i = ++token_idx; i < (token_idx + 2); ++i)
{
char* newline;
if(!tokens[line_idx][i] || strlen(tokens[line_idx][i]) <= 0) continue;
newline = strchr(tokens[line_idx][i], '\n');
char *newline = strchr(tokens[line_idx][i], '\n');
if(newline) *newline = 0;
/* Check to see if the token specifies a register */
int* pRegister = token_to_register(tokens[line_idx][i], pMemory);
int *pRegister = token_to_register(tokens[line_idx][i], pMemory);
if(pRegister)
{
p->args[num_instr][i - token_idx] = pRegister;
@ -178,11 +177,11 @@ int parse_instructions(tvm_program_t* p, const char*** tokens, tvm_memory_t* pMe
/* Check to see whether the token specifies an address */
if(tokens[line_idx][i][0] == '[')
{
char* end_symbol = strchr(tokens[line_idx][i], ']');
char *end_symbol = strchr(tokens[line_idx][i], ']');
if(end_symbol)
{
*end_symbol = 0;
p->args[num_instr][i - token_idx] = &((int*)pMemory->mem_space)[tvm_parse_value(tokens[line_idx][i] + 1)];
p->args[num_instr][i - token_idx] = &((int *)pMemory->mem_space)[tvm_parse_value(tokens[line_idx][i] + 1)];
continue;
}
@ -210,7 +209,7 @@ int parse_instructions(tvm_program_t* p, const char*** tokens, tvm_memory_t* pMe
return 0;
}
int* token_to_register(const char* token, tvm_memory_t* pMemory)
int* token_to_register(const char *token, tvm_memory_t *pMemory)
{
int i = 0;
while(tvm_register_map[i])
@ -223,7 +222,7 @@ int* token_to_register(const char* token, tvm_memory_t* pMemory)
return NULL;
}
int instr_to_opcode(const char* instr)
int instr_to_opcode(const char *instr)
{
int i;
for(i = 0; tvm_opcode_map[i]; i++)
@ -233,23 +232,23 @@ int instr_to_opcode(const char* instr)
return -1;
}
int* tvm_add_value(tvm_program_t* p, const int val)
int *tvm_add_value(tvm_program_t *p, const int val)
{
p->values = realloc(p->values, sizeof(int*) * (p->num_values + 1));
p->values[p->num_values] = (int*)calloc(1, sizeof(int));
p->values = realloc(p->values, sizeof(int *) * (p->num_values + 1));
p->values[p->num_values] = (int *)calloc(1, sizeof(int));
*p->values[p->num_values] = val;
return p->values[p->num_values++];
}
int tvm_parse_value(const char* str)
int tvm_parse_value(const char *str)
{
char* delimiter = strchr(str, '|'), base = 0;
char *delimiter = strchr(str, '|'), base = 0;
if(delimiter)
{
char* identifier = delimiter + 1;
char *identifier = delimiter + 1;
switch(*identifier)
{

View file

@ -1,21 +0,0 @@
#include <stdlib.h>
#include <tvm/tvm_stack.h>
void create_stack(tvm_memory_t* mem, size_t size)
{
mem->registers[0x7].i32_ptr = ((int32_t*)mem->mem_space) + (size / sizeof(int32_t));
mem->registers[0x6].i32_ptr = mem->registers[0x7].i32_ptr;
}
void stack_push(tvm_memory_t* mem, int* item)
{
mem->registers[0x6].i32_ptr -= 1;
*mem->registers[0x6].i32_ptr = *item;
}
void stack_pop(tvm_memory_t* mem, int* dest)
{
*dest = *mem->registers[0x6].i32_ptr;
mem->registers[0x6].i32_ptr += 1;
}