|
From: | Dmitrij K |
Subject: | Re: [Tinycc-devel] How to get var and its type by ex ecuted c-string ? |
Date: | Sun, 20 Oct 2013 18:55:03 +0000 |
I am sorry too .. =(
Here is a little proc-helper by me: (but would better if tcc keeps it for stack vars as native) [CODE=C] // value of var to string: void vv2str (TCCState *s, const char *varname, int vt, char *buf, int buf_size) { // before call this proc, we have to have knowledges // about type of var (ask user or parse known .h/.c file and extract info) if (s == NULL || varname == NULL || vt < 0 || buf == NULL || buf_size <= 0) return ; *buf = '\0'; char* value = tcc_get_symbol (s, varname); if (value == NULL) return ; int t = vt & VT_TYPE; int bt = t & VT_BTYPE; switch ( bt ) { case VT_BYTE: { if (t & VT_ARRAY) { snprintf (buf, buf_size, "%s", value); } else { snprintf (buf, buf_size, "%c", *value); } break; } case VT_BOOL: { snprintf (buf, buf_size, "%d", *value != 0); break; } case VT_SHORT: { short tv; memcpy (&tv, value, sizeof(tv)); if (t & VT_UNSIGNED) { snprintf (buf, buf_size, "%u", (unsigned)tv); } else { snprintf (buf, buf_size, "%d", tv); } break; } case VT_INT: { int tv; memcpy (&tv, value, sizeof(tv)); if (t & VT_UNSIGNED) { snprintf (buf, buf_size, "%u", (unsigned)tv); } else { snprintf (buf, buf_size, "%d", tv); } break; } case VT_LONG: { long int tv; memcpy (&tv, value, sizeof(tv)); if (t & VT_UNSIGNED) { snprintf (buf, buf_size, "%lu", (unsigned long int)tv); } else { snprintf (buf, buf_size, "%ld", (long int)tv); } break; } case VT_LLONG: { long long int tv; memcpy (&tv, value, sizeof(tv)); if (t & VT_UNSIGNED) { snprintf (buf, buf_size, "%llu", (unsigned long long int)tv); } else { snprintf (buf, buf_size, "%lld", tv); } break; } case VT_FLOAT: { float tv; memcpy (&tv, value, sizeof(tv)); snprintf (buf, buf_size, "%f", tv); break; } case VT_DOUBLE: { double tv; memcpy (&tv, value, sizeof(tv)); snprintf (buf, buf_size, "%f", tv); break; } case VT_LDOUBLE: { long double tv; memcpy (&tv, value, sizeof(tv)); snprintf (buf, buf_size, "%Lf", tv); break; } case VT_VOID: case VT_PTR: case VT_FUNC: case VT_STRUCT: snprintf (buf, buf_size, "%p", value); break; default: { snprintf (buf, buf_size, "%s", value); break; } } return ; } [/CODE] -- The best regards. > From: address@hidden > To: address@hidden > Subject: Re: [Tinycc-devel] How to get var and its type by executed c-string ? > Date: Sun, 20 Oct 2013 22:01:31 +0800 > CC: address@hidden > > Le dimanche 20 octobre 2013 07:19:14, Dmitrij K a écrit : > > > > > // HERE is I can not to know what is the type of a var (for example is the > > myint now) // but I need it for sprintf into a buffer (for return the > > buffer in the cstring mode for other things) > > I don't think it's possible. There is type_to_str which converts an internal > representation of a type to a string but that's a function not exported in > libtcc. > > Sorry, > > Thomas |
[Prev in Thread] | Current Thread | [Next in Thread] |