[interp] Make newarr lockfree
[mono-project.git] / mono / mini / helpers.c
blob3ceffe6f13765ca00c17abddf91e4349a7e943ca
1 /**
2 * \file
3 * Assorted routines
5 * (C) 2003 Ximian, Inc.
6 */
8 #include <config.h>
10 #include "mini.h"
11 #include <ctype.h>
12 #include <mono/metadata/opcodes.h>
14 #ifndef HOST_WIN32
15 #include <unistd.h>
16 #endif
18 #ifndef DISABLE_JIT
20 #ifndef DISABLE_LOGGING
22 #ifdef MINI_OP
23 #undef MINI_OP
24 #endif
25 #ifdef MINI_OP3
26 #undef MINI_OP3
27 #endif
29 // This, instead of an array of pointers, to optimize away a pointer and a relocation per string.
30 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
31 #define MSGSTRFIELD1(line) str##line
32 static const struct msgstr_t {
33 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
34 #define MINI_OP3(a,b,dest,src1,src2,src3) char MSGSTRFIELD(__LINE__) [sizeof (b)];
35 #include "mini-ops.h"
36 #undef MINI_OP
37 #undef MINI_OP3
38 } opstr = {
39 #define MINI_OP(a,b,dest,src1,src2) b,
40 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
41 #include "mini-ops.h"
42 #undef MINI_OP
43 #undef MINI_OP3
45 static const gint16 opidx [] = {
46 #define MINI_OP(a,b,dest,src1,src2) offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
47 #define MINI_OP3(a,b,dest,src1,src2,src3) offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
48 #include "mini-ops.h"
49 #undef MINI_OP
50 #undef MINI_OP3
53 #endif /* DISABLE_LOGGING */
55 #if defined(__i386__) || defined(__x86_64__)
56 #if !defined(TARGET_ARM64) && !defined(__APPLE__)
57 #define emit_debug_info TRUE
58 #else
59 #define emit_debug_info FALSE
60 #endif
61 #else
62 #define emit_debug_info FALSE
63 #endif
65 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
66 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
68 //#define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
70 #endif
72 #define ARCH_PREFIX ""
73 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
75 const char*
76 mono_inst_name (int op) {
77 #ifndef DISABLE_LOGGING
78 if (op >= OP_LOAD && op <= OP_LAST)
79 return (const char*)&opstr + opidx [op - OP_LOAD];
80 if (op < OP_LOAD)
81 return mono_opcode_name (op);
82 g_error ("unknown opcode name for %d", op);
83 return NULL;
84 #else
85 g_error ("unknown opcode name for %d", op);
86 g_assert_not_reached ();
87 #endif
90 void
91 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
93 #ifndef DISABLE_LOGGING
94 int i;
96 if (name)
97 g_print ("%s:", name);
99 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
100 if (idom == i)
101 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
102 else
103 g_print (" BB%d", cfg->bblocks [i]->block_num);
106 g_print ("\n");
107 #endif
111 * \param cfg compilation context
112 * \param code a pointer to the code
113 * \param size the code size in bytes
115 * Disassemble to code to stdout.
117 void
118 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
120 #ifndef DISABLE_LOGGING
121 GHashTable *offset_to_bb_hash = NULL;
122 int i, cindex, bb_num;
123 FILE *ofd;
124 #ifdef HOST_WIN32
125 const char *tmp = g_get_tmp_dir ();
126 #endif
127 char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
128 char *as_file;
129 char *o_file;
130 char *cmd;
131 int unused G_GNUC_UNUSED;
133 #ifdef HOST_WIN32
134 as_file = g_strdup_printf ("%s/test.s", tmp);
136 if (!(ofd = fopen (as_file, "w")))
137 g_assert_not_reached ();
138 #else
139 i = g_file_open_tmp (NULL, &as_file, NULL);
140 ofd = fdopen (i, "w");
141 g_assert (ofd);
142 #endif
144 for (i = 0; id [i]; ++i) {
145 if (i == 0 && isdigit (id [i]))
146 fprintf (ofd, "_");
147 else if (!isalnum (id [i]))
148 fprintf (ofd, "_");
149 else
150 fprintf (ofd, "%c", id [i]);
152 fprintf (ofd, ":\n");
154 if (emit_debug_info && cfg != NULL) {
155 MonoBasicBlock *bb;
157 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
158 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
159 fprintf (ofd, ".Ltext0:\n");
161 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
162 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
163 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
167 cindex = 0;
168 for (i = 0; i < size; ++i) {
169 if (emit_debug_info && cfg != NULL) {
170 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
171 if (bb_num) {
172 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
173 cindex = 0;
176 if (cindex == 0) {
177 fprintf (ofd, "\n.byte %u", (unsigned int) code [i]);
178 } else {
179 fprintf (ofd, ",%u", (unsigned int) code [i]);
181 cindex++;
182 if (cindex == 64)
183 cindex = 0;
185 fprintf (ofd, "\n");
186 fclose (ofd);
188 #ifdef __APPLE__
189 #ifdef __ppc64__
190 #define DIS_CMD "otool64 -v -t"
191 #else
192 #define DIS_CMD "otool -v -t"
193 #endif
194 #else
195 #if defined(sparc) && !defined(__GNUC__)
196 #define DIS_CMD "dis"
197 #elif defined(TARGET_X86)
198 #define DIS_CMD "objdump -l -d"
199 #elif defined(TARGET_AMD64)
200 #if defined(HOST_WIN32)
201 #define DIS_CMD "x86_64-w64-mingw32-objdump.exe -M x86-64 -d"
202 #else
203 #define DIS_CMD "objdump -l -d"
204 #endif
205 #else
206 #define DIS_CMD "objdump -d"
207 #endif
208 #endif
210 #if defined(sparc)
211 #define AS_CMD "as -xarch=v9"
212 #elif defined (TARGET_X86)
213 # if defined(__APPLE__)
214 # define AS_CMD "as -arch i386"
215 # else
216 # define AS_CMD "as -gstabs"
217 # endif
218 #elif defined (TARGET_AMD64)
219 # if defined (__APPLE__)
220 # define AS_CMD "as -arch x86_64"
221 # else
222 # define AS_CMD "as -gstabs"
223 # endif
224 #elif defined (TARGET_ARM)
225 # if defined (__APPLE__)
226 # define AS_CMD "as -arch arm"
227 # else
228 # define AS_CMD "as -gstabs"
229 # endif
230 #elif defined (TARGET_ARM64)
231 # if defined (__APPLE__)
232 # define AS_CMD "clang -c -arch arm64 -g -x assembler"
233 # else
234 # define AS_CMD "as -gstabs"
235 # endif
236 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
237 #define AS_CMD "as -mips32"
238 #elif defined(__ppc64__)
239 #define AS_CMD "as -arch ppc64"
240 #elif defined(__powerpc64__)
241 #define AS_CMD "as -mppc64"
242 #elif defined (TARGET_RISCV64)
243 #define AS_CMD "as -march=rv64ima"
244 #elif defined (TARGET_RISCV32)
245 #define AS_CMD "as -march=rv32ima"
246 #else
247 #define AS_CMD "as"
248 #endif
250 #ifdef HOST_WIN32
251 o_file = g_strdup_printf ("%s/test.o", tmp);
252 #else
253 i = g_file_open_tmp (NULL, &o_file, NULL);
254 close (i);
255 #endif
257 #ifdef HAVE_SYSTEM
258 cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
259 unused = system (cmd);
260 g_free (cmd);
261 if (!objdump_args)
262 objdump_args = g_strdup ("");
264 fflush (stdout);
266 #if defined(__arm__) || defined(__aarch64__)
268 * The arm assembler inserts ELF directives instructing objdump to display
269 * everything as data.
271 cmd = g_strdup_printf (ARCH_PREFIX "strip -s %s", o_file);
272 unused = system (cmd);
273 g_free (cmd);
274 #endif
276 cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
277 unused = system (cmd);
278 g_free (cmd);
279 g_free (objdump_args);
280 #else
281 g_assert_not_reached ();
282 #endif /* HAVE_SYSTEM */
284 #ifndef HOST_WIN32
285 unlink (o_file);
286 unlink (as_file);
287 #endif
288 g_free (o_file);
289 g_free (as_file);
290 #endif
293 #else /* DISABLE_JIT */
295 void
296 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
300 #endif /* DISABLE_JIT */