2 * get.c: Functions to get stringified values from the metadata tables.
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
20 get_typedef (metadata_t *m, int idx)
24 mono_metadata_decode_row (&m->tables [META_TABLE_TYPEDEF], idx - 1, cols, CSIZE (cols));
26 return g_strdup_printf (
28 mono_metadata_string_heap (m, cols [2]),
29 mono_metadata_string_heap (m, cols [1]));
33 get_module (metadata_t *m, int idx)
38 * There MUST BE only one module in the Module table
42 mono_metadata_decode_row (&m->tables [META_TABLE_MODULEREF], idx - 1, cols, CSIZE (cols));
44 return g_strdup (mono_metadata_string_heap (m, cols [6]));
48 get_assemblyref (metadata_t *m, int idx)
52 mono_metadata_decode_row (&m->tables [META_TABLE_ASSEMBLYREF], idx - 1, cols, CSIZE (cols));
54 return g_strdup (mono_metadata_string_heap (m, cols [6]));
59 * Returns a string representing the ArrayShape (22.2.16).
62 get_array_shape (metadata_t *m, const char *ptr, char **result)
64 GString *res = g_string_new ("[");
65 guint32 rank, num_sizes, num_lo_bounds;
66 guint32 *sizes = NULL, *lo_bounds = NULL;
70 rank = mono_metadata_decode_value (ptr, &ptr);
71 num_sizes = mono_metadata_decode_value (ptr, &ptr);
74 sizes = g_new (guint32, num_sizes);
76 for (i = 0; i < num_sizes; i++)
77 sizes [i] = mono_metadata_decode_value (ptr, &ptr);
79 num_lo_bounds = mono_metadata_decode_value (ptr, &ptr);
80 if (num_lo_bounds > 0)
81 lo_bounds = g_new (guint32, num_lo_bounds);
83 for (i = 0; i < num_lo_bounds; i++)
84 lo_bounds [i] = mono_metadata_decode_value (ptr, &ptr);
86 for (r = 0; r < rank; r++){
88 if (r < num_lo_bounds){
89 sprintf (buffer, "%d..%d", lo_bounds [r], lo_bounds [r] + sizes [r] - 1);
91 sprintf (buffer, "0..%d", sizes [r] - 1);
96 g_string_append (res, buffer);
98 g_string_append (res, ", ");
100 g_string_append (res, "]");
109 g_string_free (res, FALSE);
116 * @m: metadata context
117 * @blob_idx: index into the blob heap
119 * Returns the stringified representation of a TypeSpec signature (22.2.17)
122 get_typespec (metadata_t *m, guint32 idx)
127 GString *res = g_string_new ("");
130 mono_metadata_decode_row (&m->tables [META_TABLE_TYPESPEC], idx-1, cols, CSIZE (cols));
131 ptr = mono_metadata_blob_heap (m, cols [0]);
132 len = mono_metadata_decode_value (ptr, &ptr);
135 case ELEMENT_TYPE_PTR:
136 ptr = get_custom_mod (m, ptr, &s);
138 g_string_append (res, s);
139 g_string_append_c (res, ' ');
143 if (*ptr == ELEMENT_TYPE_VOID)
144 g_string_append (res, "void");
146 ptr = get_type (m, ptr, &s);
148 g_string_append (res, s);
152 case ELEMENT_TYPE_FNPTR:
153 g_string_append (res, "FNPTR ");
155 * we assume MethodRefSig, as we do not know
156 * whether it is a MethodDefSig or a MethodRefSig.
158 printf ("\n FNPTR:\n");
160 hex_dump (ptr, 0, 40);
163 case ELEMENT_TYPE_ARRAY:
164 ptr = get_type (m, ptr, &s);
165 g_string_append (res, s);
167 g_string_append_c (res, ' ');
168 ptr = get_array_shape (m, ptr, &s);
169 g_string_append (res, s);
173 case ELEMENT_TYPE_SZARRAY:
174 ptr = get_custom_mod (m, ptr, &s);
176 g_string_append (res, s);
177 g_string_append_c (res, ' ');
180 ptr = get_type (m, ptr, &s);
181 g_string_append (res, s);
182 g_string_append (res, "[]");
187 g_string_free (res, FALSE);
193 get_typeref (metadata_t *m, int idx)
198 guint32 rs_idx, table;
200 mono_metadata_decode_row (&m->tables [META_TABLE_TYPEREF], idx - 1, cols, CSIZE (cols));
202 t = mono_metadata_string_heap (m, cols [1]);
203 s = mono_metadata_string_heap (m, cols [2]);
205 rs_idx = cols [0] >> 2;
208 * ECMA spec claims 3 bits
210 table = cols [0] & 3;
214 x = get_module (m, rs_idx);
215 ret = g_strdup_printf ("TODO:TypeRef-Module [%s] %s.%s", x, s, t);
219 case 1: /* ModuleRef */
220 ret = g_strdup_printf ("TODO:TypeRef-ModuleRef (%s.%s)", s, t);
224 * AssemblyRef (ECMA docs claim it is 3, but it looks to
225 * me like it is 2 (tokens are prefixed with 0x23)
227 x = get_assemblyref (m, rs_idx);
228 ret = g_strdup_printf ("[%s] %s.%s", x, s, t);
232 case 4: /* TypeRef */
233 ret = g_strdup_printf ("TODO:TypeRef-TypeRef: TYPEREF! (%s.%s)", s, t);
237 ret = g_strdup_printf ("Unknown table in TypeRef %d", table);
244 * get_typedef_or_ref:
245 * @m: metadata context
246 * @dor_token: def or ref encoded index
248 * Low two bits contain table to lookup from
249 * high bits contain the index into the def or ref table
251 * Returns: a stringified version of the MethodDef or MethodRef
252 * at (dor_token >> 2)
255 get_typedef_or_ref (metadata_t *m, guint32 dor_token)
257 char *temp = NULL, *s;
261 * low 2 bits contain encoding
263 table = dor_token & 0x03;
264 idx = dor_token >> 2;
267 case 0: /* TypeDef */
268 temp = get_typedef (m, idx);
269 s = g_strdup_printf ("%s", temp);
272 case 1: /* TypeRef */
273 temp = get_typeref (m, idx);
274 s = g_strdup_printf ("%s", temp);
277 case 2: /* TypeSpec */
278 s = get_typespec (m, idx);
282 g_error ("Unhandled encoding for typedef-or-ref coded index");
293 * get_encoded_typedef_or_ref:
294 * @m: metadata context
295 * @ptr: location to decode from.
296 * @result: pointer to string where resulting decoded string is stored
298 * result will point to a g_malloc()ed string.
300 * Returns: the new ptr to continue decoding
303 get_encoded_typedef_or_ref (metadata_t *m, const char *ptr, char **result)
307 token = mono_metadata_decode_value (ptr, &ptr);
309 *result = get_typedef_or_ref (m, token);
317 * Decodes a CustomMod (22.2.7)
319 * Returns: updated pointer location
322 get_custom_mod (metadata_t *m, const char *ptr, char **return_value)
326 if ((*ptr == ELEMENT_TYPE_CMOD_OPT) ||
327 (*ptr == ELEMENT_TYPE_CMOD_REQD)){
329 ptr = get_encoded_typedef_or_ref (m, ptr, &s);
331 *return_value = g_strconcat ("CMOD ", s, NULL);
334 *return_value = NULL;
339 static map_t element_type_map [] = {
340 { ELEMENT_TYPE_END , "end" },
341 { ELEMENT_TYPE_VOID , "void" },
342 { ELEMENT_TYPE_BOOLEAN , "bool" },
343 { ELEMENT_TYPE_CHAR , "char" },
344 { ELEMENT_TYPE_I1 , "sbyte" },
345 { ELEMENT_TYPE_U1 , "byte" },
346 { ELEMENT_TYPE_I2 , "int16" },
347 { ELEMENT_TYPE_U2 , "uint16" },
348 { ELEMENT_TYPE_I4 , "int32" },
349 { ELEMENT_TYPE_U4 , "uint32" },
350 { ELEMENT_TYPE_I8 , "int64" },
351 { ELEMENT_TYPE_U8 , "uint64" },
352 { ELEMENT_TYPE_R4 , "float32" },
353 { ELEMENT_TYPE_R8 , "float64" },
354 { ELEMENT_TYPE_STRING , "string" },
355 { ELEMENT_TYPE_TYPEDBYREF , "TypedByRef" },
356 { ELEMENT_TYPE_I , "native int" },
357 { ELEMENT_TYPE_U , "native unsigned int" },
358 { ELEMENT_TYPE_OBJECT , "object" },
362 static map_t call_conv_type_map [] = {
363 { MONO_CALL_DEFAULT , "default" },
364 { MONO_CALL_C , "c" },
365 { MONO_CALL_STDCALL , "stdcall" },
366 { MONO_CALL_THISCALL , "thiscall" },
367 { MONO_CALL_FASTCALL , "fastcall" },
368 { MONO_CALL_VARARG , "vararg" },
373 dis_stringify_token (metadata_t *m, guint32 token)
375 guint idx = token & 0xffffff;
376 switch (token >> 24) {
377 case META_TABLE_TYPEDEF: return get_typedef (m, idx);
378 case META_TABLE_TYPEREF: return get_typeref (m, idx);
379 case META_TABLE_TYPESPEC: return get_typespec (m, idx);
383 return g_strdup_printf("0x%08x", token);
387 dis_stringify_array (metadata_t *m, MonoArray *array)
390 GString *s = g_string_new("");
393 type = dis_stringify_type (m, array->type);
394 g_string_append (s, type);
396 g_string_append_c (s, '[');
397 for (i = 0; i < array->rank; ++i) {
399 g_string_append_c (s, ',');
400 if (i < array->numsizes) {
401 if (i < array->numlobounds && array->lobounds[i] != 0)
402 g_string_sprintfa (s, "%d..%d", array->lobounds[i], array->sizes[i]);
404 g_string_sprintfa (s, "%d", array->sizes[i]);
407 g_string_append_c (s, ']');
409 g_string_free (s, FALSE);
414 dis_stringify_modifiers (metadata_t *m, int n, MonoCustomMod *mod)
416 GString *s = g_string_new("");
419 for (i = 0; i < n; ++i) {
420 char *tok = dis_stringify_token (m, mod[i].token);
421 g_string_sprintfa (s, "%s %s", mod[i].mod == ELEMENT_TYPE_CMOD_OPT ? "opt": "reqd", tok);
424 g_string_append_c (s, ' ');
426 g_string_free (s, FALSE);
431 dis_stringify_param (metadata_t *m, MonoParam *param)
436 if (param->num_modifiers)
437 mods = dis_stringify_modifiers (m, param->num_modifiers, param->modifiers);
438 if (param->typedbyref)
439 t = g_strdup ("TypedByRef");
440 else if (!param->type)
441 t = g_strdup ("void");
443 t = dis_stringify_type (m, param->type);
444 result = g_strjoin (mods ? mods : "", t, NULL);
451 dis_stringify_method_signature (metadata_t *m, MonoMethodSignature *method)
453 return g_strdup ("method-signature");
457 dis_stringify_type (metadata_t *m, MonoType *type)
464 return g_strdup ("void");
466 byref = type->byref ? "ref " : "";
469 case ELEMENT_TYPE_BOOLEAN:
470 case ELEMENT_TYPE_CHAR:
471 case ELEMENT_TYPE_I1:
472 case ELEMENT_TYPE_U1:
473 case ELEMENT_TYPE_I2:
474 case ELEMENT_TYPE_U2:
475 case ELEMENT_TYPE_I4:
476 case ELEMENT_TYPE_U4:
477 case ELEMENT_TYPE_I8:
478 case ELEMENT_TYPE_U8:
479 case ELEMENT_TYPE_R4:
480 case ELEMENT_TYPE_R8:
483 case ELEMENT_TYPE_STRING:
484 case ELEMENT_TYPE_OBJECT:
485 case ELEMENT_TYPE_TYPEDBYREF:
486 bare = g_strdup (map (type->type, element_type_map));
489 case ELEMENT_TYPE_VALUETYPE:
490 case ELEMENT_TYPE_CLASS:
491 bare = dis_stringify_token (m, type->data.token);
494 case ELEMENT_TYPE_FNPTR:
495 bare = dis_stringify_method_signature (m, type->data.method);
497 case ELEMENT_TYPE_PTR:
498 case ELEMENT_TYPE_SZARRAY: {
501 if (type->custom_mod) {
502 mods = dis_stringify_modifiers (m, type->data.mtype->num_modifiers, type->data.mtype->modifiers);
503 child_type = dis_stringify_type (m, type->data.mtype->type);
506 child_type = dis_stringify_type (m, type->data.type);
509 bare = g_strdup_printf (type->type == ELEMENT_TYPE_PTR ? "%s%s*" : "%s%s[]", mods, child_type);
514 case ELEMENT_TYPE_ARRAY:
515 bare = dis_stringify_array (m, type->data.array);
518 g_error ("Do not know how to stringify type 0x%x", type->type);
521 result = g_strjoin (byref, bare, NULL);
528 * @m: metadata context
529 * @ptr: location to decode from.
530 * @result: pointer to string where resulting decoded string is stored
532 * This routine returs in @result the stringified type pointed by @ptr.
535 * Returns: the new ptr to continue decoding
538 get_type (metadata_t *m, const char *ptr, char **result)
540 MonoType *type = mono_metadata_parse_type (m, ptr, &ptr);
541 *result = dis_stringify_type (m, type);
542 mono_metadata_free_type (type);
548 * Returns a stringified representation of a FieldSig (22.2.4)
551 get_field_signature (metadata_t *m, guint32 blob_signature)
553 char *allocated_modifier_string, *allocated_type_string;
554 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
559 len = mono_metadata_decode_value (ptr, &ptr);
562 g_assert (*ptr == 0x06);
563 /* hex_dump (ptr, 0, len); */
566 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
567 ptr = get_type (m, ptr, &allocated_type_string);
569 res = g_strdup_printf (
571 allocated_modifier_string ? allocated_modifier_string : "",
572 allocated_type_string);
574 if (allocated_modifier_string)
575 g_free (allocated_modifier_string);
576 if (allocated_type_string)
577 g_free (allocated_modifier_string);
583 get_field_literal_type (metadata_t *m, guint32 blob_signature)
585 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
587 char *allocated_modifier_string;
589 len = mono_metadata_decode_value (ptr, &ptr);
592 g_assert (*ptr == 0x06);
595 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
596 if (allocated_modifier_string)
597 g_free (allocated_modifier_string);
599 return (ElementTypeEnum) *ptr;
605 * @m: metadata context
606 * @token: token to decode
608 * decodes the literal indexed by @token.
611 decode_literal (metadata_t *m, guint32 token)
613 return g_strdup ("LITERAL_VALUE");
618 * @m: metadata context
619 * @ptr: location to decode from.
620 * @result: pointer to string where resulting decoded string is stored
622 * This routine returns in @result the stringified RetType (22.2.11)
624 * Returns: the new ptr to continue decoding.
627 get_ret_type (metadata_t *m, const char *ptr, char **ret_type)
629 GString *str = g_string_new ("");
631 char *allocated_type_string;
633 ptr = get_custom_mod (m, ptr, &mod);
635 g_string_append (str, mod);
636 g_string_append_c (str, ' ');
640 if (*ptr == ELEMENT_TYPE_TYPEDBYREF){
641 /* TODO: what does `typedbyref' mean? */
642 g_string_append (str, "/* FIXME: What does this mean? */ typedbyref ");
644 } else if (*ptr == ELEMENT_TYPE_VOID){
645 g_string_append (str, "void");
648 if (*ptr == ELEMENT_TYPE_BYREF){
649 g_string_append (str, "[out] ");
653 ptr = get_type (m, ptr, &allocated_type_string);
654 g_string_append (str, allocated_type_string);
655 g_free (allocated_type_string);
658 *ret_type = str->str;
659 g_string_free (str, FALSE);
666 * @m: metadata context
667 * @ptr: location to decode from.
668 * @result: pointer to string where resulting decoded string is stored
670 * This routine returns in @result the stringified Param (22.2.10)
672 * Returns: the new ptr to continue decoding.
675 get_param (metadata_t *m, const char *ptr, char **retval)
677 GString *str = g_string_new ("");
678 char *allocated_mod_string, *allocated_type_string;
680 ptr = get_custom_mod (m, ptr, &allocated_mod_string);
681 if (allocated_mod_string){
682 g_string_append (str, allocated_mod_string);
683 g_string_append_c (str, ' ');
684 g_free (allocated_mod_string);
687 if (*ptr == ELEMENT_TYPE_TYPEDBYREF){
688 g_string_append (str, "/*FIXME: what does typedbyref mean? */ typedbyref ");
691 if (*ptr == ELEMENT_TYPE_BYREF){
692 g_string_append (str, "[out] ");
695 ptr = get_type (m, ptr, &allocated_type_string);
696 g_string_append (str, allocated_type_string);
697 g_free (allocated_type_string);
701 g_string_free (str, FALSE);
705 static map_t param_map [] = {
706 { PARAM_ATTRIBUTE_IN, "[in] " },
707 { PARAM_ATTRIBUTE_OUT, "[out] " },
708 { PARAM_ATTRIBUTE_OPTIONAL, "optional " },
709 { PARAM_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
710 { PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL, "fieldmarshal " },
715 param_flags (guint32 f)
717 return g_strdup (flags (f, param_map));
720 static map_t field_access_map [] = {
721 { FIELD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
722 { FIELD_ATTRIBUTE_PRIVATE, "private " },
723 { FIELD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
724 { FIELD_ATTRIBUTE_ASSEMBLY, "assembly " },
725 { FIELD_ATTRIBUTE_FAMILY, "family " },
726 { FIELD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
727 { FIELD_ATTRIBUTE_PUBLIC, "public " },
731 static map_t field_flags_map [] = {
732 { FIELD_ATTRIBUTE_STATIC, "static " },
733 { FIELD_ATTRIBUTE_INIT_ONLY, "initonly " },
734 { FIELD_ATTRIBUTE_LITERAL, "literal " },
735 { FIELD_ATTRIBUTE_NOT_SERIALIZED, "notserialized " },
736 { FIELD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
737 { FIELD_ATTRIBUTE_PINVOKE_IMPL, "FIXME:pinvokeimpl " },
744 * Returns a stringified version of a Field's flags
747 field_flags (guint32 f)
749 static char buffer [1024];
750 int access = f & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
754 strcat (buffer, map (access, field_access_map));
755 strcat (buffer, flags (f, field_flags_map));
756 return g_strdup (buffer);
760 * Returns a stringifed representation of a MethodRefSig (22.2.2)
763 get_methodref_signature (metadata_t *m, guint32 blob_signature, const char *fancy_name)
765 GString *res = g_string_new ("");
766 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
767 char *allocated_ret_type, *s;
768 gboolean seen_vararg = 0;
769 int param_count, signature_len;
772 signature_len = mono_metadata_decode_value (ptr, &ptr);
776 g_string_append (res, "explicit-this ");
778 g_string_append (res, "instance "); /* has-this */
785 param_count = mono_metadata_decode_value (ptr, &ptr);
786 ptr = get_ret_type (m, ptr, &allocated_ret_type);
788 g_string_append (res, allocated_ret_type);
791 g_string_append_c (res, ' ');
792 g_string_append (res, fancy_name);
795 g_string_append (res, " (");
798 * param_count describes parameters *before* and *after*
799 * the vararg sentinel
801 for (i = 0; i < param_count; i++){
805 * If ptr is a SENTINEL
808 g_string_append (res, " varargs ");
812 ptr = get_param (m, ptr, ¶m);
813 g_string_append (res, param);
814 if (i+1 != param_count)
815 g_string_append (res, ", ");
818 g_string_append (res, ")");
823 g_free (allocated_ret_type);
825 g_string_free (res, FALSE);
831 * @m: metadata context
832 * @token: a FIELD_DEF token
834 * This routine has to locate the TypeDef that "owns" this Field.
835 * Since there is no backpointer in the Field table, we have to scan
836 * the TypeDef table and locate the actual "owner" of the field
839 get_field (metadata_t *m, guint32 token)
841 int idx = mono_metadata_token_index (token);
842 metadata_tableinfo_t *tdef = &m->tables [META_TABLE_TYPEDEF];
844 char *sig, *res, *type;
848 * We can get here also with a MenberRef token (for a field
849 * defined in another module/assembly, just like in get_method ()
851 if (mono_metadata_token_code (token) == TOKEN_TYPE_MEMBER_REF) {
852 return g_strdup_printf ("fieldref-0x%08x", token);
854 g_assert (mono_metadata_token_code (token) == TOKEN_TYPE_FIELD_DEF);
856 mono_metadata_decode_row (&m->tables [META_TABLE_FIELD], idx - 1, cols, CSIZE (cols));
857 sig = get_field_signature (m, cols [2]);
860 * To locate the actual "container" for this field, we have to scan
861 * the TypeDef table. LAME!
863 type_idx = mono_metadata_typedef_from_field (m, idx);
865 type = get_typedef (m, type_idx);
866 res = g_strdup_printf ("%s %s.%s",
868 mono_metadata_string_heap (m, cols [1]));
876 get_memberref_parent (metadata_t *m, guint32 mrp_token)
879 * mrp_index is a MemberRefParent coded index
881 guint32 table = mrp_token & 7;
882 guint32 idx = mrp_token >> 3;
885 case 0: /* TypeDef */
886 return get_typedef (m, idx);
888 case 1: /* TypeRef */
889 return get_typeref (m, idx);
891 case 2: /* ModuleRef */
892 return g_strdup_printf ("TODO:MemberRefParent-ModuleRef");
894 case 3: /* MethodDef */
895 return g_strdup ("TODO:MethodDef");
897 case 4: /* TypeSpec */
898 return get_typespec (m, idx);
900 g_assert_not_reached ();
906 * @m: metadata context
907 * @token: a METHOD_DEF or MEMBER_REF token
909 * This routine has to locate the TypeDef that "owns" this Field.
910 * Since there is no backpointer in the Field table, we have to scan
911 * the TypeDef table and locate the actual "owner" of the field
914 get_method (metadata_t *m, guint32 token)
916 int idx = mono_metadata_token_index (token);
917 guint32 member_cols [3], method_cols [6];
918 char *res, *class, *fancy_name, *sig;
920 switch (mono_metadata_token_code (token)){
921 case TOKEN_TYPE_METHOD_DEF:
923 mono_metadata_decode_row (&m->tables [META_TABLE_METHOD],
924 idx - 1, method_cols, 6);
926 fancy_name = mono_metadata_string_heap (m, method_cols [3]);
928 sig = get_methodref_signature (m, method_cols [4], fancy_name);
932 case TOKEN_TYPE_MEMBER_REF: {
934 mono_metadata_decode_row (&m->tables [META_TABLE_MEMBERREF],
935 idx - 1, member_cols,
936 CSIZE (member_cols));
937 class = get_memberref_parent (m, member_cols [0]);
938 fancy_name = g_strconcat (
940 mono_metadata_string_heap (m, member_cols [1]),
943 sig = get_methodref_signature (
944 m, member_cols [2], fancy_name);
947 res = g_strdup_printf ("%s", sig);
953 g_assert_not_reached ();
955 g_assert_not_reached ();
961 * @m: metadata context
962 * @blob_index: index into the blob where the constant is stored
964 * Returns: An allocated value representing a stringified version of the
968 get_constant (metadata_t *m, ElementTypeEnum t, guint32 blob_index)
970 const char *ptr = mono_metadata_blob_heap (m, blob_index);
973 len = mono_metadata_decode_value (ptr, &ptr);
976 case ELEMENT_TYPE_BOOLEAN:
977 return g_strdup_printf ("%s", *ptr ? "true" : "false");
979 case ELEMENT_TYPE_CHAR:
980 return g_strdup_printf ("%c", *ptr);
982 case ELEMENT_TYPE_U1:
983 return g_strdup_printf ("0x%02x", (int) (*ptr));
986 case ELEMENT_TYPE_I2:
987 return g_strdup_printf ("%d", (int) (*(gint16 *) ptr));
989 case ELEMENT_TYPE_I4:
990 return g_strdup_printf ("%d", *(gint32 *) ptr);
992 case ELEMENT_TYPE_I8:
994 * FIXME: This is not endian portable, does only
995 * matter for debugging, but still.
997 return g_strdup_printf ("0x%08x%08x", *(guint32 *) ptr, *(guint32 *) (ptr + 4));
999 case ELEMENT_TYPE_U8:
1000 return g_strdup_printf ("0x%08x%08x", *(guint32 *) ptr, *(guint32 *) (ptr + 4));
1001 case ELEMENT_TYPE_R4:
1002 return g_strdup_printf ("%g", (double) (* (float *) ptr));
1004 case ELEMENT_TYPE_R8:
1005 return g_strdup_printf ("%g", * (double *) ptr);
1007 case ELEMENT_TYPE_STRING: {
1011 for (i = 0; !ptr [i+1]; i += 2){
1016 case '\n': /* add more */
1020 res = g_malloc (len + e + 3);
1024 for (i = 0; i < len; i += 2){
1037 res[j++] = isprint (ptr [i]) ? ptr [i] : '.';
1046 case ELEMENT_TYPE_CLASS:
1047 return g_strdup ("CLASS CONSTANT. MUST BE ZERO");
1050 * These are non CLS compliant:
1052 case ELEMENT_TYPE_I1:
1053 return g_strdup_printf ("%d", (int) *ptr);
1055 case ELEMENT_TYPE_U2:
1056 return g_strdup_printf ("0x%04x", (unsigned int) (*(guint16 *) ptr));
1058 case ELEMENT_TYPE_U4:
1059 return g_strdup_printf ("0x%04x", (unsigned int) (*(guint32 *) ptr));
1062 g_error ("Unknown ELEMENT_TYPE (%d) on constant at Blob index (0x%08x)\n",
1063 (int) *ptr, blob_index);
1064 return g_strdup_printf ("Unknown");
1071 * @m: metadata context
1072 * @token: token that we want to decode.
1074 * Returns: An allocated value representing a stringified version of the
1078 get_token (metadata_t *m, guint32 token)
1080 switch (mono_metadata_token_code (token)){
1081 case TOKEN_TYPE_FIELD_DEF:
1082 return (get_field (m, token));
1085 g_error ("Do not know how to decode tokens of type 0x%08x", token);
1088 g_assert_not_reached ();
1089 return g_strdup ("ERROR");
1094 * @m: metadata context
1095 * @token: the token can belong to any of the following tables:
1096 * TOKEN_TYPE_TYPE_REF, TOKEN_TYPE_TYPE_DEF, TOKEN_TYPE_TYPE_SPEC
1098 * Returns: a stringified version of the MethodDef or MethodRef or TypeSpecn
1099 * at (token & 0xffffff)
1102 get_token_type (metadata_t *m, guint32 token)
1104 char *temp = NULL, *s;
1107 idx = mono_metadata_token_index (token);
1109 switch (mono_metadata_token_code (token)){
1110 case TOKEN_TYPE_TYPE_DEF:
1111 temp = get_typedef (m, idx);
1112 s = g_strdup_printf ("%s", temp);
1115 case TOKEN_TYPE_TYPE_REF:
1116 temp = get_typeref (m, idx);
1117 s = g_strdup_printf ("%s", temp);
1120 case TOKEN_TYPE_TYPE_SPEC:
1121 s = get_typespec (m, idx);
1125 g_error ("Unhandled encoding for typedef-or-ref coded index");