From 602c9919987e81b58b3a43ed42d7676636f2b58a Mon Sep 17 00:00:00 2001 From: khamidou Date: Sun, 16 Jan 2011 20:05:47 +0100 Subject: [PATCH] initial commit --- Makefile | 31 ++++++++++++++++++++++++ README | 5 ++++ as.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ aslexer.l | 16 ++++++++++++ error.c | 13 ++++++++++ error.h | 9 +++++++ main.c | 36 +++++++++++++++++++++++++++ tests/t1.as | 1 + tests/t2.as | 4 +++ tokens.h | 6 +++++ vm.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ vm.h | 19 +++++++++++++++ 12 files changed, 277 insertions(+) create mode 100644 Makefile create mode 100644 README create mode 100644 as.c create mode 100644 aslexer.l create mode 100644 error.c create mode 100644 error.h create mode 100644 main.c create mode 100644 tests/t1.as create mode 100644 tests/t2.as create mode 100644 tokens.h create mode 100644 vm.c create mode 100644 vm.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f6bcbb6 --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +OBJS=vm.o main.o error.o +CFLAGS=-c -g + +vm_and_as: vm as + +as: as.o lexer error.o + $(CC) as.o aslexer.o -o as -lfl + +as.o: as.c + $(CC) $(CFLAGS) as.c + +lexer: aslexer.l + lex -i -o aslexer.c aslexer.l + $(CC) -c aslexer.c + +vm: $(OBJS) + $(CC) $(OBJS) -o vm + +vm.o: vm.c vm.h + $(CC) $(CFLAGS) vm.c + +main.o: main.c + $(CC) $(CFLAGS) main.c + + +error.o: error.c error.h + $(CC) $(CFLAGS) error.c + +clean: + rm *.o + rm as vm diff --git a/README b/README new file mode 100644 index 0000000..b307618 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +miniforth + +at the moment, there is only a minimal vm implemented. +see as.c and vm.c to see the assembler and the vm +source code. diff --git a/as.c b/as.c new file mode 100644 index 0000000..99cd25a --- /dev/null +++ b/as.c @@ -0,0 +1,70 @@ +/* a very crude assembler for the vm - it's one pass and + * spits out error messages the ken thompson way + * */ + +#include +#include +#include +#include "vm.h" +#include "tokens.h" +#include "error.h" + +extern FILE* yyin; +extern char* yytext; +extern int yylex(); +FILE *outfp; + +int setup_parser(char *filename) { + yyin = fopen(filename, "r"); + if (yyin == NULL) + error("Unable to open input file"); + + outfp = fopen("a.bin", "w+"); + if (outfp == NULL) + error("Unable to open output file"); + + return 0; +} + +int parse() { + int t; + while((t = yylex()) != -1) { + switch(t) { + case T_PUSH: + yylex(); + fputc(PUSH, outfp); + fputc(atoi(yytext), outfp); + break; + + case T_POP: + yylex(); + fputc(POP, outfp); + fputc(atoi(yytext), outfp); + break; + + case T_DPRINT: + fputc(DPRINT, outfp); + break; + + case T_NOP: + fputc(NOP, outfp); + break; + + default: + printf("unexpected %d in input", t); + break; + } + } +} + +int main(int argc, char **argv) +{ + if (argc != 2) { + puts("as file.as"); + exit(0); + } + + setup_parser(argv[1]); + parse(); +} + diff --git a/aslexer.l b/aslexer.l new file mode 100644 index 0000000..8ed3d12 --- /dev/null +++ b/aslexer.l @@ -0,0 +1,16 @@ +/* a lexer for the assembler */ +%{ + #include "tokens.h" +%} + +%% + +<> { return -1; } +"push" { return T_PUSH; } +"pop" { return T_POP; } +"nop" { return T_NOP; } +"dprint" { return T_DPRINT; } +[0-9]+ { return NUMBER; } +; { return COMMA; } +[a-zA-Z][a-zA-Z0-9]* { return WORD; } + diff --git a/error.c b/error.c new file mode 100644 index 0000000..9b2636d --- /dev/null +++ b/error.c @@ -0,0 +1,13 @@ +#include "error.h" + +void error(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + puts(""); + fflush(stdout); + exit(EXIT_FAILURE); +} + diff --git a/error.h b/error.h new file mode 100644 index 0000000..6f71093 --- /dev/null +++ b/error.h @@ -0,0 +1,9 @@ +#ifndef __ERROR_H__ +#define __ERROR_H__ + +#include +#include +#include + +#endif + diff --git a/main.c b/main.c new file mode 100644 index 0000000..47feedd --- /dev/null +++ b/main.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +#include "vm.h" + +int filelength(FILE *fp) { + if (fp == NULL) + return -1; + + struct stat s; + fstat(fileno(fp), &s); + return (int) s.st_size; +} + +int main(int argc, char **argv) { + if (argc != 2) { + puts("vm file.bin"); + exit(0); + } + + FILE *fp = fopen(argv[1], "r"); + if (fp == NULL) + error("unable to find the requested file"); + + int flen = filelength(fp); + + struct vm *v = alloc_vm(flen, 234); + load_file(v, fp); + + printf("vm launched, memsize: %d, stacksize: %d\n", v->memsize, v->stacksize); + run(v); + +} diff --git a/tests/t1.as b/tests/t1.as new file mode 100644 index 0000000..4099407 --- /dev/null +++ b/tests/t1.as @@ -0,0 +1 @@ +23 diff --git a/tests/t2.as b/tests/t2.as new file mode 100644 index 0000000..f3b5056 --- /dev/null +++ b/tests/t2.as @@ -0,0 +1,4 @@ +NOP +NOP +PUSH 23 +DPRINT diff --git a/tokens.h b/tokens.h new file mode 100644 index 0000000..53831d1 --- /dev/null +++ b/tokens.h @@ -0,0 +1,6 @@ +#ifndef __TOKENS_H__ +#define __TOKENS_H__ + +enum tokens {NUMBER = 0, COMMA, WORD, EOL, T_PUSH, T_POP, T_NOP, T_DPRINT}; + +#endif diff --git a/vm.c b/vm.c new file mode 100644 index 0000000..0c170ca --- /dev/null +++ b/vm.c @@ -0,0 +1,67 @@ +#include "vm.h" + +struct vm* alloc_vm(int memsize, int stacksize) +{ + struct vm *v = calloc(1, sizeof(struct vm)); + if (v == NULL) + error("Unable to allocate virtual machine"); + + v->mem = calloc(memsize, sizeof(int)); + if (v->mem == NULL) + error("Unable to allocate memory for vm"); + + v->stack = calloc(stacksize, sizeof(int)); + if (v->stack == NULL) + error("Unable to allocate memory for vm stack"); + + v->memsize = memsize; + v->stacksize = stacksize; + return v; +} + +int load_file(struct vm *v, FILE *fd) +{ + if (fd == NULL || v == NULL) + error("Unable to load file"); + + fread(v->mem, sizeof(char), v->memsize, fd); +} + +int run(struct vm *v) +{ + if (v == NULL) + return -1; + + int *ip = v->mem; + while(ip < v->mem + v->memsize) { + switch(*ip) { + case NOP: + break; + + case PUSH: + puts("ho"); + if (v->stackoffset >= v->stacksize) + error("stack overflow"); + + ip++; + printf("%d", *ip); + v->stack[v->stackoffset] = *ip; + v->stackoffset++; + break; + + case POP: + if (v->stackoffset == 0) + error("stack underflow"); + + v->stackoffset--; + break; + + case DPRINT: + /* a debug instruction - print the top of the stack */ + printf("%d\n", v->stack[v->stackoffset]); + break; + } + + ip++; + } +} diff --git a/vm.h b/vm.h new file mode 100644 index 0000000..466745c --- /dev/null +++ b/vm.h @@ -0,0 +1,19 @@ +#ifndef __VM_H__ +#define __VM_H__ + +#include +#include + +enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT}; + +struct vm { + int *mem; + int memsize; + int *stack; + int stacksize; + int stackoffset; +}; + +struct vm* alloc_vm(int memsize, int stacksize); + +#endif -- 2.43.0