From: Peep P. <so...@us...> - 2004-06-07 15:38:55
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7453 Modified Files: debug.c Log Message: Fixed headers; changed to work with new compiler and bytecode Index: debug.c =================================================================== RCS file: /cvsroot/agd/server/src/debug.c,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- debug.c 3 Apr 2004 20:19:08 -0000 1.11 +++ debug.c 7 Jun 2004 15:38:46 -0000 1.12 @@ -1,14 +1,56 @@ -#include "std.h" -#include "lpc_incl.h" +/*#include "config.h"*/ +#include "compile_options.h" +#include "list.h" +#include "lpc.h" +#include "object.h" + +#include "compile.h" +#include "lang.h" +#include "instr.h" #ifdef DEBUG -#define NAME() if(v->name) printf("%s", v->name); -void print_var(variable_t *v, int values) +/* Puts out a string on stdout, without \n and \r. */ +void puts_nonl(char *s) { char buf[MAX_STRING_LENGTH]; - char *p, *bufptr; + char *bufptr; + + if(!s) + return; + + bufptr = buf; + while(*s) { + if(bufptr >= buf + MAX_STRING_LENGTH) { + puts("<string too long>"); + return; + } + + switch(*s) { + case '\n': + *bufptr++ = '\\'; + *bufptr++ = 'n'; + break; + case '\r': + *bufptr++ = '\\'; + *bufptr++ = 'r'; + break; + case '\t': + *bufptr++ = '\\'; + *bufptr++ = 't'; + break; + default: + *bufptr++ = *s; + } + s++; + } + *bufptr = '\0'; + printf("\"%s\"", buf); +} +#define NAME() if(v->name) printf("%s", v->name); +void print_var(variable_t *v, int values) +{ int lval_lvl; if(!v) { @@ -19,53 +61,30 @@ if(!values) return; - if(v->type & T_ARRAY) { - printf("array of "); - v->type &= ~T_ARRAY; - } + printf("%s ", type2str(v->type)); + if(v->type & T_ARRAY) + return; + switch(v->type) { case T_INT: - printf("int "); NAME(); printf("=%ld", v->u.i); break; case T_STRING: - printf("string "); NAME(); - if(v->u.s) { - p = v->u.s; - bufptr = buf; - while(*p) { - if(bufptr >= buf + MAX_STRING_LENGTH) { - printf("=<string too long>"); - goto get_out; - } - if(*p == '\n') { - *bufptr++ = '\\'; - *bufptr++ = 'n'; - } else if(*p != '\r') { - *bufptr++ = *p; - } - p++; - } - *bufptr = '\0'; - printf("=\"%s\"", buf); - } -get_out: + putchar('='); + puts_nonl(v->u.s); break; case T_VOID: - printf("void "); NAME(); break; case T_OBJECT: - printf("object "); NAME(); if(v->u.ob) printf("=<%s>", v->u.ob->name); else printf("=<NULL>"); break; case T_LVALUE: - printf("lvalue "); NAME(); printf("=<"); lval_lvl++; @@ -87,152 +106,56 @@ char *opc_name(int i) { switch(i) { - case F_PUSH_INT: - return "F_PUSH_INT"; - case F_PUSH_STRING: - return "F_PUSH_STRING"; - case F_PUSH_VOID: - return "F_PUSH_VOID"; - case F_PUSH_NULL_OBJECT: - return "F_PUSH_NULL_OBJECT"; - case F_PUSH_ARRAY: - return "F_PUSH_ARRAY"; - case F_PUSH_LVAR: - return "F_PUSH_LVAR"; - case F_PUSH_LVAR_LVALUE: - return "F_PUSH_LVAR_LVALUE"; - case F_PUSH_GVAR: - return "F_PUSH_GVAR"; - case F_PUSH_GVAR_LVALUE: - return "F_PUSH_GVAR_LVALUE"; - case F_ADD: - return "F_ADD"; - case F_SUB: - return "F_SUB"; - case F_MUL: - return "F_MUL"; - case F_DIV: - return "F_DIV"; - case F_MOD: - return "F_MOD"; - case F_ASSIGN: - return "F_ASSIGN"; - case F_POP: - return "F_POP"; - case F_CALL_LFUN: - return "F_CALL_LFUN"; - case F_CALL_DFUN: - return "F_CALL_DFUN"; - case F_CALL_OTHER: - return "F_CALL_OTHER"; - case F_JMP: - return "F_JMP"; - case F_JMPF: - return "F_JMPF"; - case F_EQ: - return "F_EQ"; - case F_NE: - return "F_NE"; - case F_AND: - return "F_AND"; - case F_OR: - return "F_OR"; - case F_NEG: - return "F_NEG"; - case F_NOT: - return "F_NOT"; - case F_GT: - return "F_GT"; - case F_GE: - return "F_GE"; - case F_LT: - return "F_LT"; - case F_LE: - return "F_LE"; - case F_POSTINC: - return "F_POSTINC"; - case F_PREINC: - return "F_PREINC"; - case F_POSTDEC: - return "F_POSTDEC"; - case F_PREDEC: - return "F_PREDEC"; - case F_INDEX: - return "F_INDEX"; - case F_INDEX_LVALUE: - return "F_INDEX_LVALUE"; - case F_SLICE: - return "F_SLICE"; - case F_SLICE_LVALUE: - return "F_SLICE_LVALUE"; - case F_RETURN: - return "F_RETURN"; - default: - return "unknown"; - } -} - -void print_vm_token(array_t *a, int *i) -{ - printf("@[%d]: %d/%s ", *i, (int)a->data[*i], opc_name((int)a->data[*i])); - - switch((int)a->data[*i]) { - int index, num; - char *s; - -/* case F_PUSH: - print_var(a->data[++*i], 1); - printf("\n"); - break;*/ - case F_PUSH_INT: - num = (int)a->data[++*i]; - printf("%d", num); - break; - case F_PUSH_STRING: - s = a->data[++*i]; - printf("\"%s\"", s); - break; - case F_PUSH_ARRAY: - num = (int)a->data[++*i]; - printf("type %d ", num); - num = (int)a->data[++*i]; - printf("%d", num); - break; - case F_CALL_DFUN: - index = (int)a->data[++*i]; - num = (int)a->data[++*i]; - printf("index %d num_arg: %d", index, num); - break; - case F_CALL_LFUN: - index = (int)a->data[++*i]; - num = (int)a->data[++*i]; - printf("index %d num_arg: %d", index, num); - break; - case F_CALL_OTHER: - s = a->data[++*i]; - num = (int)a->data[++*i]; - printf("fun \"%s\" num_arg: %d", s, num); - break; - case F_PUSH_LVAR: - case F_PUSH_LVAR_LVALUE: - printf("index %d", (int)a->data[++*i]); - break; - case F_PUSH_GVAR: - case F_PUSH_GVAR_LVALUE: - printf("index %d", (int)a->data[++*i]); - break; - case F_JMP: - num = (int)a->data[++*i]; - printf("%d (to %d)", num, *i + num); - break; - case F_JMPF: - num = (int)a->data[++*i]; - printf("%d (to %d)", num, *i + num); - break; + case F_PUSH_INT: return "F_PUSH_INT"; + case F_PUSH_STRING: return "F_PUSH_STRING"; + case F_PUSH_VOID: return "F_PUSH_VOID"; + case F_PUSH_NULL_OBJECT: return "F_PUSH_NULL_OBJECT"; + case F_PUSH_ARRAY: return "F_PUSH_ARRAY"; + case F_PUSH_LVAR: return "F_PUSH_LVAR"; + case F_PUSH_LVAR_LVALUE: return "F_PUSH_LVAR_LVALUE"; + case F_PUSH_GVAR: return "F_PUSH_GVAR"; + case F_PUSH_GVAR_LVALUE: return "F_PUSH_GVAR_LVALUE"; + case F_ADD: return "F_ADD"; + case F_ADD_EQ: return "F_ADD_EQ"; + case F_SUB: return "F_SUB"; + case F_SUB_EQ: return "F_SUB_EQ"; + case F_MUL: return "F_MUL"; + case F_MUL_EQ: return "F_MUL_EQ"; + case F_DIV: return "F_DIV"; + case F_DIV_EQ: return "F_DIV_EQ"; + case F_MOD: return "F_MOD"; + case F_MOD_EQ: return "F_MOD_EQ"; + case F_ASSIGN: return "F_ASSIGN"; + case F_POP: return "F_POP"; + case F_CALL_LFUN: return "F_CALL_LFUN"; + case F_CALL_DFUN: return "F_CALL_DFUN"; + case F_CALL_OTHER: return "F_CALL_OTHER"; + case F_JUMP: return "F_JUMP"; + case F_JUMP_IF_FALSE: return "F_JUMP_IF_FALSE"; + case F_EQ: return "F_EQ"; + case F_NE: return "F_NE"; + case F_AND: return "F_AND"; + case F_OR: return "F_OR"; + case F_NEG: return "F_NEG"; + case F_NOT: return "F_NOT"; + case F_GT: return "F_GT"; + case F_GE: return "F_GE"; + case F_LT: return "F_LT"; + case F_LE: return "F_LE"; + case F_POSTINC: return "F_POSTINC"; + case F_PREINC: return "F_PREINC"; + case F_POSTDEC: return "F_POSTDEC"; + case F_PREDEC: return "F_PREDEC"; + case F_INDEX: return "F_INDEX"; + case F_INDEX_LVALUE: return "F_INDEX_LVALUE"; + case F_SLICE: return "F_SLICE"; + case F_SLICE_LVALUE: return "F_SLICE_LVALUE"; + case F_RETURN: return "F_RETURN"; + default: return "unknown"; } - putchar('\n'); } +#if 0 void print_array(array_t *a) { int i; @@ -277,11 +200,77 @@ printf(" }) "); } -void print_vm_tokens(array_t *a) +#endif + +void print_instr(program_t *prog, function_t *f) { int i; - for(i = 0; i < a->length; i++) { - print_vm_token(a, &i); + long int *code; + int codelen; + + codelen = f->codelen; + code = f->code; + + for(i = 0; i < codelen; i++) { + printf("@[%d]: %d/%s ", i, code[i], opc_name(code[i])); + + switch(code[i]) { + int index, num; + char *s; + + /* case F_PUSH: + print_var(a->data[++*i], 1); + printf("\n"); + break;*/ + case F_PUSH_INT: + num = code[++i]; + printf("%d", num); + break; + case F_PUSH_STRING: + i++; + printf("%d (", code[i]); + puts_nonl(prog->str_tbl[code[i]]); + putchar(')'); +/* s = (char *) code[++i]; + printf("\"%s\"", s);*/ + break; + case F_PUSH_ARRAY: + num = code[++i]; + printf("type %d ", num); + num = code[++i]; + printf("%d", num); + break; + case F_CALL_DFUN: + index = code[++i]; + num = code[++i]; + printf("index %d num_arg: %d", index, num); + break; + case F_CALL_LFUN: + index = code[++i]; + num = code[++i]; + printf("index %d num_arg: %d", index, num); + break; + case F_CALL_OTHER: + s = (char *) code[++i]; + num = code[++i]; + printf("fun \"%s\" num_arg: %d", s, num); + break; + case F_PUSH_LVAR: + case F_PUSH_LVAR_LVALUE: + case F_PUSH_GVAR: + case F_PUSH_GVAR_LVALUE: + printf("index %d", code[++i]); + break; + case F_JUMP: + num = code[++i]; + printf("%d (to %d)", num, i + num); + break; + case F_JUMP_IF_FALSE: + num = code[++i]; + printf("%d (to %d)", num, i + num); + break; + } + putchar('\n'); } } @@ -291,15 +280,15 @@ function_t *f; printf("Functions: \n"); - for(i = 0; i < p->functions.length; i++) { - f = (function_t *) p->functions.data[i]; + for(i = 0; i < p->numfunc; i++) { + f = p->functions[i]; printf("Function %d:\n", i); printf(" Type %d\n", f->type); printf(" Arguments: "); - print_var_arr(&f->args); +/* print_var_arr(&f->args);*/ printf("\n"); printf(" Code:\n"); - print_vm_tokens(&f->code); + print_instr(p, f); printf("\n"); } if(i == 0) |