Merge pull request #2802 from BrzVlad/feature-evacuation-opt2
[mono.git] / mono / metadata / opcodes.c
1 /*
2  * opcodes.c: CIL instruction information
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
8  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11 #include <mono/metadata/opcodes.h>
12 #include <stddef.h> /* for NULL */
13 #include <config.h>
14
15 #define MONO_PREFIX1_OFFSET MONO_CEE_ARGLIST
16 #define MONO_CUSTOM_PREFIX_OFFSET MONO_CEE_MONO_ICALL
17
18 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
19         { Mono ## e, MONO_FLOW_ ## j, MONO_ ## a },
20
21 const MonoOpcode
22 mono_opcodes [MONO_CEE_LAST + 1] = {
23 #include "mono/cil/opcode.def"
24         {0}
25 };
26
27 #undef OPDEF
28
29 #ifdef HAVE_ARRAY_ELEM_INIT
30 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
31 #define MSGSTRFIELD1(line) str##line
32 static const struct msgstr_t {
33 #define OPDEF(a,b,c,d,e,f,g,h,i,j) char MSGSTRFIELD(__LINE__) [sizeof (b)];
34 #include "mono/cil/opcode.def"
35 #undef OPDEF
36 } opstr = {
37 #define OPDEF(a,b,c,d,e,f,g,h,i,j) b,
38 #include "mono/cil/opcode.def"
39 #undef OPDEF
40 };
41 static const int16_t opidx [] = {
42 #define OPDEF(a,b,c,d,e,f,g,h,i,j) [MONO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
43 #include "mono/cil/opcode.def"
44 #undef OPDEF
45 };
46
47 const char*
48 mono_opcode_name (int opcode)
49 {
50         return (const char*)&opstr + opidx [opcode];
51 }
52
53 #else
54 #define OPDEF(a,b,c,d,e,f,g,h,i,j) b,
55 static const char* const
56 mono_opcode_names [MONO_CEE_LAST + 1] = {
57 #include "mono/cil/opcode.def"
58         NULL
59 };
60
61 const char*
62 mono_opcode_name (int opcode)
63 {
64         return mono_opcode_names [opcode];
65 }
66
67 #endif
68
69 MonoOpcodeEnum
70 mono_opcode_value (const mono_byte **ip, const mono_byte *end)
71 {
72         MonoOpcodeEnum res;
73         const mono_byte *p = *ip;
74
75         if (p >= end)
76                 return (MonoOpcodeEnum)-1;
77         if (*p == 0xfe) {
78                 ++p;
79                 if (p >= end)
80                         return (MonoOpcodeEnum)-1;
81                 res = (MonoOpcodeEnum)(*p + MONO_PREFIX1_OFFSET);
82         } else if (*p == MONO_CUSTOM_PREFIX) {
83                 ++p;
84                 if (p >= end)
85                         return (MonoOpcodeEnum)-1;
86                 res = (MonoOpcodeEnum)(*p + MONO_CUSTOM_PREFIX_OFFSET);
87         } else {
88                 res = (MonoOpcodeEnum)*p;
89         }
90         *ip = p;
91         return res;
92 }
93