2005-07-05 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / opcodes.c
index 3009d9397da5a27c787251811eefe1bd42e90ec6..f8b430234727715fce4e60022f9aa18f24cffd21 100644 (file)
@@ -7,24 +7,85 @@
  * (C) 2002 Ximian, Inc.
  */
 #include <mono/metadata/opcodes.h>
-#include <malloc.h> /* for NULL */
+#include <stddef.h> /* for NULL */
+#include <config.h>
+
+#define MONO_PREFIX1_OFFSET MONO_CEE_ARGLIST
+#define MONO_CUSTOM_PREFIX_OFFSET MONO_CEE_MONO_ICALL
 
 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
-       { Mono ## e, MONO_FLOW_ ## j, ((g-1)<<8) | i },
+       { Mono ## e, MONO_FLOW_ ## j, MONO_ ## a },
 
 const MonoOpcode
-mono_opcodes [MONO_N_OPCODES] = {
+mono_opcodes [MONO_CEE_LAST + 1] = {
 #include "mono/cil/opcode.def"
        {0}
 };
 
 #undef OPDEF
 
+#ifdef HAVE_ARRAY_ELEM_INIT
+#define MSGSTRFIELD(line) MSGSTRFIELD1(line)
+#define MSGSTRFIELD1(line) str##line
+static const struct msgstr_t {
+#define OPDEF(a,b,c,d,e,f,g,h,i,j) char MSGSTRFIELD(__LINE__) [sizeof (b)];
+#include "mono/cil/opcode.def"
+#undef OPDEF
+} opstr = {
 #define OPDEF(a,b,c,d,e,f,g,h,i,j) b,
+#include "mono/cil/opcode.def"
+#undef OPDEF
+};
+static const gint16 opidx [] = {
+#define OPDEF(a,b,c,d,e,f,g,h,i,j) [MONO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
+#include "mono/cil/opcode.def"
+#undef OPDEF
+};
+
+const char*
+mono_opcode_name (int opcode)
+{
+       return (const char*)&opstr + opidx [opcode];
+}
 
-const char* const
-mono_opcode_names [MONO_N_OPCODES] = {
+#else
+#define OPDEF(a,b,c,d,e,f,g,h,i,j) b,
+static const char* const
+mono_opcode_names [MONO_CEE_LAST + 1] = {
 #include "mono/cil/opcode.def"
        NULL
 };
 
+const char*
+mono_opcode_name (int opcode)
+{
+       return mono_opcode_names [opcode];
+}
+
+#endif
+
+MonoOpcodeEnum
+mono_opcode_value (const guint8 **ip, const guint8 *end)
+{
+       MonoOpcodeEnum res;
+       const guint8 *p = *ip;
+
+       if (p >= end)
+               return -1;
+       if (*p == 0xfe) {
+               ++p;
+               if (p >= end)
+                       return -1;
+               res = *p + MONO_PREFIX1_OFFSET;
+       } else if (*p == MONO_CUSTOM_PREFIX) {
+               ++p;
+               if (p >= end)
+                       return -1;
+               res = *p + MONO_CUSTOM_PREFIX_OFFSET;
+       } else {
+               res = *p;
+       }
+       *ip = p;
+       return res;
+}
+