Move some code around so it's more accessible.

This commit is contained in:
Bill Currie 2012-05-08 11:43:29 +09:00
parent 7519ec7bbd
commit 034139b806
5 changed files with 114 additions and 108 deletions

View file

@ -30,6 +30,8 @@
#ifndef statement_h
#define statement_h
#include "QF/pr_comp.h"
typedef enum {
op_symbol,
op_value,
@ -78,5 +80,6 @@ struct expr_s;
sblock_t *make_statements (struct expr_s *expr);
void print_statement (statement_t *s);
void print_flow (sblock_t *sblock, const char *filename);
const char *operand_string (operand_t *op);
#endif//statement_h

View file

@ -55,4 +55,6 @@ const char *save_string (const char *str);
const char *make_string (char *token, char **end);
const char *quote_string (const char *str);
#endif//__strpool_h

View file

@ -45,122 +45,18 @@
#include "expr.h"
#include "statements.h"
#include "strpool.h"
#include "symtab.h"
#include "type.h"
static const char *
quote_string (const char *str)
{
static dstring_t *q;
char c[2] = {0, 0};
if (!str)
return "(null)";
if (!q)
q = dstring_new ();
dstring_clearstr (q);
while ((c[0] = *str++)) {
switch (c[0]) {
case '\n':
dstring_appendstr (q, "\\\\n");
break;
case '<':
dstring_appendstr (q, "&lt;");
break;
case '>':
dstring_appendstr (q, "&gt;");
break;
case '&':
dstring_appendstr (q, "&amp;");
break;
case '"':
dstring_appendstr (q, "&quot;");
break;
default:
dstring_appendstr (q, c);
break;
}
}
return q->str;
}
static const char *
get_operand (operand_t *op)
{
type_t *type;
if (!op)
return "";
switch (op->op_type) {
case op_symbol:
return op->o.symbol->name;
case op_value:
switch (op->o.value->type) {
case ev_string:
return quote_string (op->o.value->v.string_val);
case ev_float:
return va ("%g", op->o.value->v.float_val);
case ev_vector:
return va ("'%g %g %g'",
op->o.value->v.vector_val[0],
op->o.value->v.vector_val[1],
op->o.value->v.vector_val[2]);
case ev_quat:
return va ("'%g %g %g %g'",
op->o.value->v.quaternion_val[0],
op->o.value->v.quaternion_val[1],
op->o.value->v.quaternion_val[2],
op->o.value->v.quaternion_val[3]);
case ev_pointer:
return va ("ptr %d", op->o.value->v.pointer.val);
case ev_field:
return va ("field %d", op->o.value->v.pointer.val);
case ev_entity:
return va ("ent %d", op->o.value->v.integer_val);
case ev_func:
return va ("func %d", op->o.value->v.integer_val);
case ev_integer:
return va ("int %d", op->o.value->v.integer_val);
case ev_uinteger:
return va ("uint %u", op->o.value->v.uinteger_val);
case ev_short:
return va ("short %d", op->o.value->v.short_val);
case ev_void:
return "(void)";
case ev_invalid:
return "(invalid)";
case ev_type_count:
return "(type_count)";
}
break;
case op_label:
return op->o.label->name;
case op_temp:
return va ("tmp %p", op);
case op_pointer:
type = op->o.pointer->type;
if (op->o.pointer->def)
return va ("(%s)[%d]&lt;%s&gt;",
type ? pr_type_name[type->type] : "???",
op->o.pointer->val, op->o.pointer->def->name);
else
return va ("(%s)[%d]",
type ? pr_type_name[type->type] : "???",
op->o.pointer->val);
case op_alias:
return get_operand (op->o.alias);//FIXME better output
}
return ("??");
}
static void
flow_statement (dstring_t *dstr, statement_t *s)
{
dasprintf (dstr, " <tr>");
dasprintf (dstr, "<td>%s</td>", quote_string (s->opcode));
dasprintf (dstr, "<td>%s</td>", get_operand (s->opa));
dasprintf (dstr, "<td>%s</td>", get_operand (s->opb));
dasprintf (dstr, "<td>%s</td>", get_operand (s->opc));
dasprintf (dstr, "<td>%s</td>", operand_string (s->opa));
dasprintf (dstr, "<td>%s</td>", operand_string (s->opb));
dasprintf (dstr, "<td>%s</td>", operand_string (s->opc));
dasprintf (dstr, "</tr>\n");
}

View file

@ -53,6 +53,75 @@
#include "type.h"
#include "qc-parse.h"
const char *
operand_string (operand_t *op)
{
type_t *type;
if (!op)
return "";
switch (op->op_type) {
case op_symbol:
return op->o.symbol->name;
case op_value:
switch (op->o.value->type) {
case ev_string:
return quote_string (op->o.value->v.string_val);
case ev_float:
return va ("%g", op->o.value->v.float_val);
case ev_vector:
return va ("'%g %g %g'",
op->o.value->v.vector_val[0],
op->o.value->v.vector_val[1],
op->o.value->v.vector_val[2]);
case ev_quat:
return va ("'%g %g %g %g'",
op->o.value->v.quaternion_val[0],
op->o.value->v.quaternion_val[1],
op->o.value->v.quaternion_val[2],
op->o.value->v.quaternion_val[3]);
case ev_pointer:
return va ("ptr %d", op->o.value->v.pointer.val);
case ev_field:
return va ("field %d", op->o.value->v.pointer.val);
case ev_entity:
return va ("ent %d", op->o.value->v.integer_val);
case ev_func:
return va ("func %d", op->o.value->v.integer_val);
case ev_integer:
return va ("int %d", op->o.value->v.integer_val);
case ev_uinteger:
return va ("uint %u", op->o.value->v.uinteger_val);
case ev_short:
return va ("short %d", op->o.value->v.short_val);
case ev_void:
return "(void)";
case ev_invalid:
return "(invalid)";
case ev_type_count:
return "(type_count)";
}
break;
case op_label:
return op->o.label->name;
case op_temp:
return va ("tmp %p", op);
case op_pointer:
type = op->o.pointer->type;
if (op->o.pointer->def)
return va ("(%s)[%d]&lt;%s&gt;",
type ? pr_type_name[type->type] : "???",
op->o.pointer->val, op->o.pointer->def->name);
else
return va ("(%s)[%d]",
type ? pr_type_name[type->type] : "???",
op->o.pointer->val);
case op_alias:
return operand_string (op->o.alias);//FIXME better output
}
return ("??");
}
static void
print_operand (operand_t *op)
{

View file

@ -358,3 +358,39 @@ make_string (char *token, char **end)
return save_string (str->str);
}
const char *
quote_string (const char *str)
{
static dstring_t *q;
char c[2] = {0, 0};
if (!str)
return "(null)";
if (!q)
q = dstring_new ();
dstring_clearstr (q);
while ((c[0] = *str++)) {
switch (c[0]) {
case '\n':
dstring_appendstr (q, "\\\\n");
break;
case '<':
dstring_appendstr (q, "&lt;");
break;
case '>':
dstring_appendstr (q, "&gt;");
break;
case '&':
dstring_appendstr (q, "&amp;");
break;
case '"':
dstring_appendstr (q, "&quot;");
break;
default:
dstring_appendstr (q, c);
break;
}
}
return q->str;
}