compile(t->pt.tif.i, if_branch);
compile(t->pt.tif.i, else_branch);
- new_instr(nop, if_branch);
new_instr(nop, else_branch);
jmp.u.jump.target = list_last(else_branch);
new_instr(jmp, if_branch);
+ new_instr(nop, if_branch);
ins.opcode = OPC_JUMP_UNLESS;
ins.u.jump_unless.cond = c;
}
}
+static char *
+lookup_var_name(int id)
+{
+ int h;
+
+ for (h=0; h<HASHSIZE; h++) {
+ struct variable *p;
+ LIST_FOREACH(p, var_hash + h)
+ if (p->varcode == id)
+ return p->name;
+ }
+ return "";
+}
+
void
print_code(void)
{
struct code* p;
+ int i;
+
+ printf("Known constants:\n");
+ for (i=1; i<cur_const_n; i++)
+ printf("-%d\t\"%s\"\n", i, const_tab[i]);
+ printf("\n");
+
+ printf("Known variables:\n");
+ // This is grossly inefficient...
+ for (i=0; i<temp_varcode_start; i++)
+ printf("%d\t%s\n", i, lookup_var_name(i));
+ printf("\n");
LIST_FOREACH(p, &input_code) {
+ printf("%p: ", p);
switch (p->opcode) {
case OPC_SET:
printf("SET %d %d\n", p->u.set.l, p->u.set.r);
p->u.tpop.r, p->u.tpop.res);
break;
case OPC_JUMP:
- printf("JUMP %d\n", (int) p->u.jump.target);
+ printf("JUMP %p\n", p->u.jump.target);
break;
case OPC_JUMP_UNLESS:
- printf("JUMP_UNLESS %d %d\n", p->u.jump_unless.cond,(int) p->u.jump_unless.target);
+ printf("JUMP_UNLESS %d %p\n", p->u.jump_unless.cond, p->u.jump_unless.target);
break;
case OPC_GT:
printf("GT %d %d %d\n", p->u.tpop.l, p->u.tpop.r, p->u.tpop.res);
printf("NOT %d %d\n", p->u.dpop.par, p->u.dpop.res);
break;
case OPC_NOP:
- puts("NOP");
+ printf("NOP\n");
break;
case OPC_PIPE:
printf("PIPE %d %d\n", p->u.arrow.what, p->u.arrow.copy);
return;
buf = xmalloc(INT_TO_STRING_LEN);
- sscanf(buf,"%d", &len);
+ sprintf(buf,"%d", len);
set_var(pv->varcode, buf);
free_string(buf);
}
return;
buf = xmalloc(INT_TO_STRING_LEN);
- sscanf(buf,"%d", &code);
+ sprintf(buf,"%d", code);
set_var(pv->varcode, buf);
free_string(buf);
}
found = 1;
free_string(ph->value);
ph->value = xmalloc(INT_TO_STRING_LEN);
- sscanf(ph->value, "%d", &len);
+ sprintf(ph->value, "%d", len);
break;
}
}
char* buf, * tmpbuf;
buf = xmalloc(INT_TO_STRING_LEN + strlen(clheader));
tmpbuf = xmalloc(INT_TO_STRING_LEN);
- sscanf(tmpbuf, "%d", &len);
+ sprintf(tmpbuf, "%d", len);
+ strcpy(buf, clheader);
strcat(buf, tmpbuf);
new_header(buf, h);
free_string(buf);
LIST_FOREACH(p, ins) {
switch (p->opcode) {
case OPC_SET:
- set_var(p->u.set.l, get_var(p->u.set.r));
+ result = get_var(p->u.set.r);
+ set_var(p->u.set.l, result);
+ free_string(result);
break;
case OPC_JUMP:
p = p->u.jump.target;
break;
case OPC_NOT:
result = xmalloc(INT_TO_STRING_LEN);
- sscanf(get_var(p->u.tpop.l),"%d", &v);
+ sscanf(get_var(p->u.dpop.par),"%d", &v);
sprintf(result, "%d", !v);
- set_var(p->u.tpop.res, result);
+ set_var(p->u.dpop.res, result);
+ free_string(result);
+ break;
case OPC_CAT:
case OPC_RE:
case OPC_NRE: