-OBJS=vm.o main.o error.o
+OBJS=vm.o main.o fail.o
CFLAGS=-c -g
vm_and_as: vm as
-as: as.o lexer error.o
- $(CC) as.o aslexer.o -o as -lfl
+as: as.o lexer fail.o
+ $(CC) as.o aslexer.o fail.o -o as -lfl
as.o: as.c
$(CC) $(CFLAGS) as.c
$(CC) $(CFLAGS) main.c
-error.o: error.c error.h
- $(CC) $(CFLAGS) error.c
+fail.o: fail.c fail.h
+ $(CC) $(CFLAGS) fail.c
clean:
rm *.o
-/* a very crude assembler for the vm - it's one pass and
- * spits out error messages the ken thompson way
+/* a very crude assembler for the vm -
+ * it spits out fail messages the ken thompson way
* */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
+#include <string.h>
#include "vm.h"
#include "tokens.h"
-#include "error.h"
+#include "fail.h"
extern FILE* yyin;
extern char* yytext;
struct label* la = calloc(1, sizeof(struct label));
if (la == NULL)
- error("Unable to allocate memory for one label");
+ fail("Unable to allocate memory for one label");
la->name = strdup(name);
la->offset = offset;
if (fp == NULL)
return -1;
+ return fwrite(&val, sizeof(int), 1, fp);
+}
- /* comes from:
- * http://stackoverflow.com/questions/1105938/writing-and-reading-long-int-value-in-c-code/1105961#1105961
- */
-
- unsigned char c1 =(val >> 0) & 0xff;
- unsigned char c2 = (val >> 8) & 0xff;
- unsigned char c3 = (val >> 16) & 0xff;
- unsigned char c4 = (val >> 24) & 0xff;
+/* the first pass only scans the file for labels */
+int first_pass() {
+ int t;
+ int current_offset = 0; /* the offset in the generated binary file */
+ int s;
+ struct label *l;
- fputc(c1, fp);
- fputc(c2, fp);
- fputc(c3, fp);
- fputc(c4, fp);
+ while((t = yylex()) != -1) {
+ switch(t) {
+ case T_LABEL:
+ s = strlen(yytext);
+ yytext[s-1] = '\0'; /* get rid of the ':' */
- return 0;
+ l = alloc_label(yytext, current_offset);
+ append_label(l, label_head);
+ break;
+ default:
+ current_offset++;
+ break;
+ }
+ }
}
int parse() {
int t;
int current_offset = 0; /* the offset in the generated binary file */
+ int s;
+ struct label *l;
while((t = yylex()) != -1) {
switch(t) {
writeint(DEC, outfp);
break;
+ case T_LABEL:
+ break;
+
+ case T_CALL:
+ yylex();
+ l = search_label(yytext);
+ if (l == NULL)
+ fail("the label %s doesn't exists", yytext);
+
+ writeint(CALL, outfp);
+ writeint(l->offset, outfp);
+ break;
+
+ case T_JMP:
+ yylex();
+ l = search_label(yytext);
+ if (l == NULL)
+ fail("the label %s doesn't exists", yytext);
+
+ writeint(JMP, outfp);
+ writeint(l->offset, outfp);
+ break;
+
default:
printf("unexpected %d in input", t);
break;
int main(int argc, char **argv)
{
- if (argc > 2) {
+ if (argc != 2) {
puts("as file.as");
exit(0);
}
- if (argc == 1) {
- yyin = stdin;
- } else {
- yyin = fopen(filename, "r");
-
- if (yyin == NULL)
- error("Unable to open input file");
- }
+ yyin = fopen(argv[1], "r");
+ if (yyin == NULL)
+ error("Unable to open input file");
outfp = fopen("a.bin", "w+");
if (outfp == NULL)
label_head = alloc_label("", 0);
+ first_pass();
+ fclose(yyin);
+ fopen(argv[1], "r");
+ yyrestart(yyin);
parse();
}
"dprint" { return T_DPRINT; }
"inc" { return T_INC; }
"dec" { return T_DEC; }
+"call" { return T_CALL; }
+"jmp" { return T_JMP; }
[ \t\n]+ /* ignore whitespace */
[0-9]+ { return NUMBER; }
; { return COMMA; }
-[a-zA-Z][a-zA-Z0-9]*: { print("ho"); return LABEL; }
+[a-zA-Z][a-zA-Z0-9]*: { return T_LABEL; }
[a-zA-Z][a-zA-Z0-9]* { return WORD; }
-#include "error.h"
+#include "fail.h"
-void error(char *fmt, ...)
+void fail(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
- va_end(ap);
puts("");
- fflush(stdout);
+ va_end(ap);
exit(EXIT_FAILURE);
}
#ifndef __TOKENS_H__
#define __TOKENS_H__
-enum tokens {NUMBER = 0, COMMA, WORD, EOL, T_PUSH, T_POP, T_NOP, T_DPRINT, T_INC, T_DEC};
+enum tokens {NUMBER = 0, COMMA, WORD, T_LABEL, T_PUSH, T_POP, T_NOP, T_DPRINT, T_INC, T_DEC, T_CALL, T_JMP};
#endif
{
struct vm *v = calloc(1, sizeof(struct vm));
if (v == NULL)
- error("Unable to allocate virtual machine");
+ fail("Unable to allocate virtual machine");
v->mem = calloc(memsize, sizeof(char));
if (v->mem == NULL)
- error("Unable to allocate memory for vm");
+ fail("Unable to allocate memory for vm");
v->stack = calloc(stacksize, sizeof(char));
if (v->stack == NULL)
- error("Unable to allocate memory for vm stack");
+ fail("Unable to allocate memory for vm stack");
v->memsize = memsize;
v->stacksize = stacksize;
int load_file(struct vm *v, FILE *fd)
{
if (fd == NULL || v == NULL)
- error("Unable to load file");
+ fail("Unable to load file");
fread(v->mem, sizeof(int), v->memsize, fd);
}
case PUSH:
if (v->stackoffset >= v->stacksize)
- error("stack overflow");
+ fail("stack overflow");
ip++;
v->stackoffset++;
case POP:
if (v->stackoffset < 0)
- error("stack underflow");
+ fail("stack underflow");
v->stackoffset--;
break;
case CALL:
/* save the previous instruction pointer */
if (v->stackoffset >= v->stacksize)
- error("stack overflow");
+ fail("stack overflow");
v->stackoffset++;
v->stack[v->stackoffset] = (int) ip;
-
- //yylex();
- //ip = atoi(
+ ip++;
+ ip = v->stack + (*ip);
break;
+ case JMP:
+ ip++;
+ ip = v->stack + (*ip);
+ break;
}
#include <stdlib.h>
#include <stdio.h>
-enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT, INC, DEC};
+enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, JMP, RET, DPRINT, INC, DEC};
struct vm {
int *mem;