summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/mruby/src/codedump.c
blob: e3a33419c39ba88f277ba248e00329b12fe42696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <mruby.h>
#include <mruby/irep.h>
#include <mruby/debug.h>
#include <mruby/opcode.h>
#include <mruby/string.h>
#include <mruby/proc.h>

#ifndef MRB_DISABLE_STDIO
static int
print_r(mrb_state *mrb, mrb_irep *irep, size_t n, int pre)
{
  size_t i;

  if (n == 0) return 0;

  for (i=0; i+1<irep->nlocals; i++) {
    if (irep->lv[i].r == n) {
      mrb_sym sym = irep->lv[i].name;
      if (pre) printf(" ");
      printf("R%d:%s", (int)n, mrb_sym2name(mrb, sym));
      return 1;
    }
  }
  return 0;
}

#define RA  1
#define RB  2
#define RAB 3

static void
print_lv(mrb_state *mrb, mrb_irep *irep, mrb_code c, int r)
{
  int pre = 0;

  if (!irep->lv
      || ((!(r & RA) || GETARG_A(c) >= irep->nlocals)
       && (!(r & RB) || GETARG_B(c) >= irep->nlocals))) {
    printf("\n");
    return;
  }
  printf("\t; ");
  if (r & RA) {
    pre = print_r(mrb, irep, GETARG_A(c), 0);
  }
  if (r & RB) {
    print_r(mrb, irep, GETARG_B(c), pre);
  }
  printf("\n");
}
#endif

static void
codedump(mrb_state *mrb, mrb_irep *irep)
{
#ifndef MRB_DISABLE_STDIO
  int i;
  int ai;
  mrb_code c;
  const char *file = NULL, *next_file;
  int32_t line;

  if (!irep) return;
  printf("irep %p nregs=%d nlocals=%d pools=%d syms=%d reps=%d\n", (void*)irep,
         irep->nregs, irep->nlocals, (int)irep->plen, (int)irep->slen, (int)irep->rlen);

  for (i = 0; i < (int)irep->ilen; i++) {
    ai = mrb_gc_arena_save(mrb);

    next_file = mrb_debug_get_filename(irep, i);
    if (next_file && file != next_file) {
      printf("file: %s\n", next_file);
      file = next_file;
    }
    line = mrb_debug_get_line(irep, i);
    if (line < 0) {
      printf("      ");
    }
    else {
      printf("%5d ", line);
    }

    printf("%03d ", i);
    c = irep->iseq[i];
    switch (GET_OPCODE(c)) {
    case OP_NOP:
      printf("OP_NOP\n");
      break;
    case OP_MOVE:
      printf("OP_MOVE\tR%d\tR%d\t", GETARG_A(c), GETARG_B(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_LOADL:
      {
        mrb_value v = irep->pool[GETARG_Bx(c)];
        mrb_value s = mrb_inspect(mrb, v);
        printf("OP_LOADL\tR%d\tL(%d)\t; %s", GETARG_A(c), GETARG_Bx(c), RSTRING_PTR(s));
      }
      print_lv(mrb, irep, c, RA);
      break;
    case OP_LOADI:
      printf("OP_LOADI\tR%d\t%d\t", GETARG_A(c), GETARG_sBx(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_LOADSYM:
      printf("OP_LOADSYM\tR%d\t:%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_LOADNIL:
      printf("OP_LOADNIL\tR%d\t\t", GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_LOADSELF:
      printf("OP_LOADSELF\tR%d\t\t", GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_LOADT:
      printf("OP_LOADT\tR%d\t\t", GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_LOADF:
      printf("OP_LOADF\tR%d\t\t", GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_GETGLOBAL:
      printf("OP_GETGLOBAL\tR%d\t:%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_SETGLOBAL:
      printf("OP_SETGLOBAL\t:%s\tR%d\t",
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_GETCONST:
      printf("OP_GETCONST\tR%d\t:%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_SETCONST:
      printf("OP_SETCONST\t:%s\tR%d\t",
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_GETMCNST:
      printf("OP_GETMCNST\tR%d\tR%d::%s", GETARG_A(c), GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_SETMCNST:
      printf("OP_SETMCNST\tR%d::%s\tR%d", GETARG_A(c)+1,
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_GETIV:
      printf("OP_GETIV\tR%d\t%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_SETIV:
      printf("OP_SETIV\t%s\tR%d",
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_GETUPVAR:
      printf("OP_GETUPVAR\tR%d\t%d\t%d",
             GETARG_A(c), GETARG_B(c), GETARG_C(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_SETUPVAR:
      printf("OP_SETUPVAR\tR%d\t%d\t%d",
             GETARG_A(c), GETARG_B(c), GETARG_C(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_GETCV:
      printf("OP_GETCV\tR%d\t%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_SETCV:
      printf("OP_SETCV\t%s\tR%d",
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_JMP:
      printf("OP_JMP\t%03d\n", i+GETARG_sBx(c));
      break;
    case OP_JMPIF:
      printf("OP_JMPIF\tR%d\t%03d\n", GETARG_A(c), i+GETARG_sBx(c));
      break;
    case OP_JMPNOT:
      printf("OP_JMPNOT\tR%d\t%03d\n", GETARG_A(c), i+GETARG_sBx(c));
      break;
    case OP_SEND:
      printf("OP_SEND\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_SENDB:
      printf("OP_SENDB\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_TAILCALL:
      printf("OP_TAILCALL\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_SUPER:
      printf("OP_SUPER\tR%d\t%d\n", GETARG_A(c),
             GETARG_C(c));
      break;
    case OP_ARGARY:
      printf("OP_ARGARY\tR%d\t%d:%d:%d:%d", GETARG_A(c),
             (GETARG_Bx(c)>>10)&0x3f,
             (GETARG_Bx(c)>>9)&0x1,
             (GETARG_Bx(c)>>4)&0x1f,
             (GETARG_Bx(c)>>0)&0xf);
      print_lv(mrb, irep, c, RA);
      break;

    case OP_ENTER:
      printf("OP_ENTER\t%d:%d:%d:%d:%d:%d:%d\n",
             (GETARG_Ax(c)>>18)&0x1f,
             (GETARG_Ax(c)>>13)&0x1f,
             (GETARG_Ax(c)>>12)&0x1,
             (GETARG_Ax(c)>>7)&0x1f,
             (GETARG_Ax(c)>>2)&0x1f,
             (GETARG_Ax(c)>>1)&0x1,
             GETARG_Ax(c) & 0x1);
      break;
    case OP_RETURN:
      printf("OP_RETURN\tR%d", GETARG_A(c));
      switch (GETARG_B(c)) {
      case OP_R_NORMAL:
        printf("\tnormal\t"); break;
      case OP_R_RETURN:
        printf("\treturn\t"); break;
      case OP_R_BREAK:
        printf("\tbreak\t"); break;
      default:
        printf("\tbroken\t"); break;
      }
      print_lv(mrb, irep, c, RA);
      break;
    case OP_BLKPUSH:
      printf("OP_BLKPUSH\tR%d\t%d:%d:%d:%d", GETARG_A(c),
             (GETARG_Bx(c)>>10)&0x3f,
             (GETARG_Bx(c)>>9)&0x1,
             (GETARG_Bx(c)>>4)&0x1f,
             (GETARG_Bx(c)>>0)&0xf);
      print_lv(mrb, irep, c, RA);
      break;

    case OP_LAMBDA:
      printf("OP_LAMBDA\tR%d\tI(%+d)\t", GETARG_A(c), GETARG_b(c)+1);
      switch (GETARG_c(c)) {
      case OP_L_METHOD:
        printf("method"); break;
      case OP_L_BLOCK:
        printf("block"); break;
      case OP_L_LAMBDA:
        printf("lambda"); break;
      }
      print_lv(mrb, irep, c, RA);
      break;
    case OP_RANGE:
      printf("OP_RANGE\tR%d\tR%d\t%d", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_METHOD:
      printf("OP_METHOD\tR%d\t:%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]));
      print_lv(mrb, irep, c, RA);
      break;

    case OP_ADD:
      printf("OP_ADD\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_ADDI:
      printf("OP_ADDI\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_SUB:
      printf("OP_SUB\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_SUBI:
      printf("OP_SUBI\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_MUL:
      printf("OP_MUL\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_DIV:
      printf("OP_DIV\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_LT:
      printf("OP_LT\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_LE:
      printf("OP_LE\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_GT:
      printf("OP_GT\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_GE:
      printf("OP_GE\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
    case OP_EQ:
      printf("OP_EQ\t\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;

    case OP_STOP:
      printf("OP_STOP\n");
      break;

    case OP_ARRAY:
      printf("OP_ARRAY\tR%d\tR%d\t%d", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_ARYCAT:
      printf("OP_ARYCAT\tR%d\tR%d\t", GETARG_A(c), GETARG_B(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_ARYPUSH:
      printf("OP_ARYPUSH\tR%d\tR%d\t", GETARG_A(c), GETARG_B(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_AREF:
      printf("OP_AREF\tR%d\tR%d\t%d", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_APOST:
      printf("OP_APOST\tR%d\t%d\t%d", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_STRING:
      {
        mrb_value v = irep->pool[GETARG_Bx(c)];
        mrb_value s = mrb_str_dump(mrb, mrb_str_new(mrb, RSTRING_PTR(v), RSTRING_LEN(v)));
        printf("OP_STRING\tR%d\tL(%d)\t; %s", GETARG_A(c), GETARG_Bx(c), RSTRING_PTR(s));
      }
      print_lv(mrb, irep, c, RA);
      break;
    case OP_STRCAT:
      printf("OP_STRCAT\tR%d\tR%d\t", GETARG_A(c), GETARG_B(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_HASH:
      printf("OP_HASH\tR%d\tR%d\t%d", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      print_lv(mrb, irep, c, RAB);
      break;

    case OP_OCLASS:
      printf("OP_OCLASS\tR%d\t\t", GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_CLASS:
      printf("OP_CLASS\tR%d\t:%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_MODULE:
      printf("OP_MODULE\tR%d\t:%s", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_EXEC:
      printf("OP_EXEC\tR%d\tI(%+d)", GETARG_A(c), GETARG_Bx(c)+1);
      print_lv(mrb, irep, c, RA);
      break;
    case OP_SCLASS:
      printf("OP_SCLASS\tR%d\tR%d\t", GETARG_A(c), GETARG_B(c));
      print_lv(mrb, irep, c, RAB);
      break;
    case OP_TCLASS:
      printf("OP_TCLASS\tR%d\t\t", GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_ERR:
      {
        mrb_value v = irep->pool[GETARG_Bx(c)];
        mrb_value s = mrb_str_dump(mrb, mrb_str_new(mrb, RSTRING_PTR(v), RSTRING_LEN(v)));
        printf("OP_ERR\t%s\n", RSTRING_PTR(s));
      }
      break;
    case OP_EPUSH:
      printf("OP_EPUSH\t:I(%+d)\n", GETARG_Bx(c)+1);
      break;
    case OP_ONERR:
      printf("OP_ONERR\t%03d\n", i+GETARG_sBx(c));
      break;
    case OP_RESCUE:
      {
        int a = GETARG_A(c);
        int b = GETARG_B(c);
        int cnt = GETARG_C(c);

        if (b == 0) {
          printf("OP_RESCUE\tR%d\t\t%s", a, cnt ? "cont" : "");
          print_lv(mrb, irep, c, RA);
          break;
        }
        else {
          printf("OP_RESCUE\tR%d\tR%d\t%s", a, b, cnt ? "cont" : "");
          print_lv(mrb, irep, c, RAB);
          break;
        }
      }
      break;
    case OP_RAISE:
      printf("OP_RAISE\tR%d\t\t", GETARG_A(c));
      print_lv(mrb, irep, c, RA);
      break;
    case OP_POPERR:
      printf("OP_POPERR\t%d\t\t\n", GETARG_A(c));
      break;
    case OP_EPOP:
      printf("OP_EPOP\t%d\n", GETARG_A(c));
      break;

    default:
      printf("OP_unknown %d\t%d\t%d\t%d\n", GET_OPCODE(c),
             GETARG_A(c), GETARG_B(c), GETARG_C(c));
      break;
    }
    mrb_gc_arena_restore(mrb, ai);
  }
  printf("\n");
#endif
}

static void
codedump_recur(mrb_state *mrb, mrb_irep *irep)
{
  int i;

  codedump(mrb, irep);
  for (i=0; i<irep->rlen; i++) {
    codedump_recur(mrb, irep->reps[i]);
  }
}

void
mrb_codedump_all(mrb_state *mrb, struct RProc *proc)
{
  codedump_recur(mrb, proc->body.irep);
}