Skip to content

Commit

Permalink
write instr nodes to a file now as well. (a.m1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjs committed Jun 27, 2012
1 parent b452fa2 commit a6a5c43
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/gencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2458,15 +2458,14 @@ gencode_chunk(M1_compiler *comp, m1_chunk *c) {
#define PRELOAD_0_AND_1 0

comp->current_m0chunk = CHUNK (c->name);

fprintf(OUT, ".chunk \"%s\"\n", c->name);

/* for each chunk, reset the register allocator */
reset_reg(comp);

gencode_consts(&c->constants);
gencode_metadata(c);

write_chunk(comp, c);

fprintf(OUT, ".chunk \"%s\"\n", c->name);
gencode_consts(&c->constants);
gencode_metadata(c);
fprintf(OUT, ".bytecode\n");


Expand Down Expand Up @@ -2575,7 +2574,8 @@ gencode(M1_compiler *comp, m1_chunk *ast) {
m1_type *decliter;

fprintf(OUT, ".version 0\n");

write_m0b_file(comp);

while (iter != NULL) {
/* set pointer to current chunk, so that the code generator
has access to anything that belongs to the chunk.
Expand Down
56 changes: 56 additions & 0 deletions src/instr.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,60 @@ mk_chunk(M1_compiler *comp, char *name) {
return ch;
}

static void
write_consts(M1_compiler *comp, m1_symboltable *consttable) {
m1_symbol *iter;

fprintf(OUT, ".constants\n");

assert(consttable != NULL);
iter = consttable->syms;

while (iter != NULL) {

switch (iter->valtype) {
case VAL_STRING:
fprintf(OUT, "%d %s\n", iter->constindex, iter->value.sval);
break;
case VAL_FLOAT:
fprintf(OUT, "%d %f\n", iter->constindex, iter->value.fval);
break;
case VAL_INT:
fprintf(OUT, "%d %d\n", iter->constindex, iter->value.ival);
break;
case VAL_CHUNK:
fprintf(OUT, "%d &%s\n", iter->constindex, iter->value.sval);
break;
default:
fprintf(stderr, "unknown symbol type (%d)\n", iter->valtype);
assert(0); /* should never happen. */
}
iter = iter->next;
}

}

static void
write_metadata(M1_compiler *comp, m1_chunk *c) {
assert(c != NULL);
fprintf(OUT, ".metadata\n");
}


void
write_chunk(M1_compiler *comp, m1_chunk *c) {
fprintf(OUT, ".chunk \"%s\"\n", c->name);

write_consts(comp, &c->constants);
write_metadata(comp, c);

fprintf(OUT, ".bytecode\n");

}

void
write_m0b_file(M1_compiler *comp) {
fprintf(OUT, ".version 0\n");

}

3 changes: 3 additions & 0 deletions src/instr.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,8 @@ extern m0_instr *mk_instr(M1_compiler *comp, m0_opcode, char const * const forma

extern void mk_label(M1_compiler *comp, unsigned labelno);

extern void write_chunk(M1_compiler *comp, struct m1_chunk *c);
extern void write_m0b_file(M1_compiler *comp);

#endif

0 comments on commit a6a5c43

Please sign in to comment.