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.
+
+= format of binary files
+
+
extern int yylex();
FILE *outfp;
-int setup_parser(char *filename) {
- yyin = fopen(filename, "r");
- if (yyin == NULL)
- error("Unable to open input file");
+/* labels are represented by a linked list */
+struct label {
+ char *name;
+ int offset;
+ struct label *next;
+};
- outfp = fopen("a.bin", "w+");
- if (outfp == NULL)
- error("Unable to open output file");
+struct label* label_head;
+
+struct label* alloc_label(char *name, int offset) {
+ struct label* la = calloc(1, sizeof(struct label));
+
+ if (la == NULL)
+ error("Unable to allocate memory for one label");
+
+ la->name = strdup(name);
+ la->offset = offset;
+
+ return la;
+}
+
+struct label* search_label(char *name) {
+ struct label* l = label_head->next;
+
+ while(l != NULL) {
+ if (strcmp(name, l->name) == 0)
+ return l;
+
+ l = l->next;
+ }
+}
+
+void append_label(struct label* l, struct label* head) {
+ struct label *t = head->next;
+ head->next = l;
+ l->next = t;
+}
+
+
+/* writeint: write an int in a file by breaking it
+ * in 8 chars
+ */
+int writeint(int val, FILE* fp) {
+
+ if (fp == NULL)
+ return -1;
+
+ /* 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;
+
+ fputc(c1, fp);
+ fputc(c2, fp);
+ fputc(c3, fp);
+ fputc(c4, fp);
return 0;
}
int parse() {
int t;
+ int current_offset = 0; /* the offset in the generated binary file */
+
while((t = yylex()) != -1) {
switch(t) {
case T_PUSH:
yylex();
- fputc(PUSH, outfp);
- fputc(atoi(yytext), outfp);
+ writeint(PUSH, outfp);
+ writeint(atoi(yytext), outfp);
+ current_offset++; /* we're pushing to values to the file, so update
+ current_offset accordingly
+ */
break;
case T_POP:
yylex();
- fputc(POP, outfp);
- fputc(atoi(yytext), outfp);
+ writeint(POP, outfp);
+ writeint(atoi(yytext), outfp);
break;
case T_DPRINT:
- fputc(DPRINT, outfp);
+ writeint(DPRINT, outfp);
break;
case T_NOP:
- fputc(NOP, outfp);
+ writeint(NOP, outfp);
+ break;
+
+ case T_INC:
+ writeint(INC, outfp);
+ break;
+
+ case T_DEC:
+ writeint(DEC, outfp);
break;
default:
printf("unexpected %d in input", t);
break;
}
+
+ current_offset++;
}
}
int main(int argc, char **argv)
{
- if (argc != 2) {
+ if (argc > 2) {
puts("as file.as");
exit(0);
}
- setup_parser(argv[1]);
+ if (argc == 1) {
+ yyin = stdin;
+ } else {
+ 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");
+
+ label_head = alloc_label("", 0);
+
parse();
}
"pop" { return T_POP; }
"nop" { return T_NOP; }
"dprint" { return T_DPRINT; }
+"inc" { return T_INC; }
+"dec" { return T_DEC; }
+[ \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 WORD; }
NOP
PUSH 23
DPRINT
+INC
+DPRINT
#ifndef __TOKENS_H__
#define __TOKENS_H__
-enum tokens {NUMBER = 0, COMMA, WORD, EOL, T_PUSH, T_POP, T_NOP, T_DPRINT};
+enum tokens {NUMBER = 0, COMMA, WORD, EOL, T_PUSH, T_POP, T_NOP, T_DPRINT, T_INC, T_DEC};
#endif
if (v == NULL)
error("Unable to allocate virtual machine");
- v->mem = calloc(memsize, sizeof(int));
+ v->mem = calloc(memsize, sizeof(char));
if (v->mem == NULL)
error("Unable to allocate memory for vm");
- v->stack = calloc(stacksize, sizeof(int));
+ v->stack = calloc(stacksize, sizeof(char));
if (v->stack == NULL)
error("Unable to allocate memory for vm stack");
v->memsize = memsize;
v->stacksize = stacksize;
+ v->stackoffset = -1;
return v;
}
if (fd == NULL || v == NULL)
error("Unable to load file");
- fread(v->mem, sizeof(char), v->memsize, fd);
+ fread(v->mem, sizeof(int), v->memsize, fd);
}
int run(struct vm *v)
break;
case PUSH:
- puts("ho");
if (v->stackoffset >= v->stacksize)
error("stack overflow");
ip++;
- printf("%d", *ip);
- v->stack[v->stackoffset] = *ip;
v->stackoffset++;
+ v->stack[v->stackoffset] = *ip;
break;
case POP:
- if (v->stackoffset == 0)
+ if (v->stackoffset < 0)
error("stack underflow");
v->stackoffset--;
/* a debug instruction - print the top of the stack */
printf("%d\n", v->stack[v->stackoffset]);
break;
+
+ case INC:
+ v->stack[v->stackoffset]++;
+ break;
+
+ case DEC:
+ v->stack[v->stackoffset]--;
+ break;
+
+ case CALL:
+ /* save the previous instruction pointer */
+ if (v->stackoffset >= v->stacksize)
+ error("stack overflow");
+
+ v->stackoffset++;
+ v->stack[v->stackoffset] = (int) ip;
+
+ //yylex();
+ //ip = atoi(
+ break;
+
+
}
ip++;
#include <stdlib.h>
#include <stdio.h>
-enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT};
+enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT, INC, DEC};
struct vm {
int *mem;