Skip to content
Browse files

more conversion for fprintf() to INS ().

  • Loading branch information...
1 parent 6d985e4 commit 846db66399b913507daa13d3fbb391d4f0b73f2b @kjs kjs committed Jun 23, 2012
Showing with 37 additions and 4 deletions.
  1. +23 −2 src/gencode.c
  2. +11 −1 src/instr.c
  3. +1 −0 src/instr.h
  4. +2 −1 src/symtab.h
View
25 src/gencode.c
@@ -54,8 +54,11 @@ static unsigned gencode_obj(M1_compiler *comp, m1_object *obj, m1_object **paren
static const char type_chars[REG_TYPE_NUM] = {'i', 'n', 's', 'p'};
static const char reg_chars[REG_TYPE_NUM] = {'I', 'N', 'S', 'P'};
-#define INS(opcode, format, ...) mk_instr(comp, opcode, format, ##__VA_ARGS__)
-#define CHUNK(name) mk_chunk(comp, name)
+#define LABEL(label) mk_label(comp, label)
+
+#define INS(opcode, format, ...) mk_instr(comp, opcode, format, ##__VA_ARGS__)
+
+#define CHUNK(name) mk_chunk(comp, name)
static void
@@ -672,6 +675,7 @@ OBJECT_LINK------> L3
m1_reg parentreg = popreg(comp->regstack);
m1_reg target = alloc_reg(comp, VAL_INT);
+ INS (M0_DEREF, "%I, %I, %I", target.no, parentreg.no, offset.no);
fprintf(OUT, "\tderef\tI%d, I%d, I%d\n", target.no, parentreg.no, offset.no);
pushreg(comp->regstack, target);
@@ -737,6 +741,7 @@ OBJECT_LINK------> L3
assert(fieldsym != NULL);
/* load the offset into a reg. and make it available through the regstack. */
+ INS (M0_SET_IMM, "%I, %d, %d", fieldreg.no, 0, fieldsym->offset);
fprintf(OUT, "\tset_imm I%d, 0, %d\n", fieldreg.no, fieldsym->offset);
/* make it available through the regstack */
@@ -800,16 +805,20 @@ gencode_while(M1_compiler *comp, m1_whileexpr *w) {
push(comp->breakstack, endlabel);
push(comp->continuestack, startlabel);
+ INS (M0_GOTO, "%L", endlabel);
fprintf(OUT, "\tgoto L%d\n", endlabel);
+ LABEL (startlabel);
fprintf(OUT, "L%d:\n", startlabel);
gencode_expr(comp, w->block);
+ LABEL (endlabel);
fprintf(OUT, "L%d:\n", endlabel);
gencode_expr(comp, w->cond);
reg = popreg(comp->regstack);
+ INS (M0_GOTO_IF, "%L, %R", startlabel, reg);
fprintf(OUT, "\tgoto_if\tL%d, %c%d\n", startlabel, reg_chars[(int)reg.type], reg.no);
free_reg(comp, reg);
@@ -837,14 +846,18 @@ gencode_dowhile(M1_compiler *comp, m1_whileexpr *w) {
push(comp->breakstack, endlabel);
push(comp->continuestack, startlabel);
+ LABEL (startlabel);
fprintf(OUT, "L%d:\n", startlabel);
+
gencode_expr(comp, w->block);
gencode_expr(comp, w->cond);
reg = popreg(comp->regstack);
+ INS (M0_GOTO_IF, "%L, %R", startlabel, reg);
fprintf(OUT, "\tgoto_if\tL%d, %c%d\n", startlabel, reg_chars[(int)reg.type], reg.no);
+ LABEL (endlabel);
fprintf(OUT, "L%d:\n", endlabel);
free_reg(comp, reg);
@@ -880,29 +893,37 @@ gencode_for(M1_compiler *comp, m1_forexpr *i) {
if (i->init)
gencode_expr(comp, i->init);
+ LABEL (startlabel);
fprintf(OUT, "L%d:\n", startlabel);
if (i->cond) {
m1_reg reg;
gencode_expr(comp, i->cond);
reg = popreg(comp->regstack);
+
+ INS (M0_GOTO_IF, "%L, %R", blocklabel, reg);
fprintf(OUT, "\tgoto_if L%d, %c%d\n", blocklabel, reg_chars[(int)reg.type], reg.no);
free_reg(comp, reg);
}
+ INS (M0_GOTO, "%L", endlabel);
fprintf(OUT, "\tgoto L%d\n", endlabel);
+ LABEL (blocklabel);
fprintf(OUT, "L%d:\n", blocklabel);
if (i->block)
gencode_expr(comp, i->block);
+ LABEL (steplabel);
fprintf(OUT, "L%d:\n", steplabel);
if (i->step)
gencode_expr(comp, i->step);
+ INS (M0_GOTO, "%L", startlabel);
fprintf(OUT, "\tgoto L%d\n", startlabel);
+ LABEL (endlabel);
fprintf(OUT, "L%d:\n", endlabel);
(void)pop(comp->breakstack);
View
12 src/instr.c
@@ -12,6 +12,7 @@ The following specifiers may be used:
%S like %I, but for S registers.
%P like %I, but for P registers.
%d to pass a integer literal.
+ %L to pass a label number.
*/
#include <stdlib.h>
@@ -68,7 +69,7 @@ char const * const m0_instr_names[] = {
};
-static const char regs[REG_TYPE_NUM + 1] = {'I', 'N', 'S', 'P', ' '};
+static const char regs[REG_TYPE_NUM + 2] = {'I', 'N', 'S', 'P', ' ', 'L'};
#define OUT stdout
@@ -167,7 +168,12 @@ mk_instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
m1_reg r = va_arg(argp, m1_reg);
ins->operands[index].type = r.type;
ins->operands[index].value = r.no;
+ break;
}
+ case 'L':
+ ins->operands[index].type = VAL_LABEL;
+ ins->operands[index].value = va_arg(argp, int);
+ break;
case 'd':
ins->operands[index].type = VAL_VOID;
ins->operands[index].value = va_arg(argp, int);
@@ -197,6 +203,10 @@ mk_instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
}
+void
+mk_label(M1_compiler *comp, unsigned labelno) {
+
+}
m0_chunk *
mk_chunk(M1_compiler *comp, char *name) {
View
1 src/instr.h
@@ -120,6 +120,7 @@ extern m0_chunk *mk_chunk(M1_compiler *comp, char *name);
extern m0_instr *mk_instr(M1_compiler *comp, m0_opcode, char const * const format, ...);
+extern void mk_label(M1_compiler *comp, unsigned labelno);
#endif
View
3 src/symtab.h
@@ -26,7 +26,8 @@ typedef enum m1_valuetype {
VAL_CHUNK = 3, /* uses sval field of m1_value union */
VAL_ADDRESS = 0, /* uses ival field of m1_value union */
VAL_USERTYPE = 3,
- VAL_VOID = 4
+ VAL_VOID = 4,
+ VAL_LABEL = 5
} m1_valuetype;

0 comments on commit 846db66

Please sign in to comment.
Something went wrong with that request. Please try again.