Source

hotpy_2 / Python / trace_recorder_stack.c

Full commit
  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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/* Trace Recorder */
/* This is the final stage of the pipeline.
 * It just records the instructions it receives */

#include <stddef.h> /* For offsetof */
#include "Python.h"
#include "opcode.h"
#include "optimiser.h"
#include "trace_opcode.h"

typedef struct _trace_recorder {
    HotPyOptimiser opt_base;
    Py_ssize_t next_byte;
    Py_ssize_t allocated_bytes;
    unsigned char *bytecodes;
    Py_ssize_t loop_header;
    PyObject *exits;
    PyObject *name_ids;
    PyObject *name_list;
    PyObject *constant_ids;
    PyObject *constant_list;
    HotPyTraceObject *trace;
} HotPyTraceRecorder;

#define FAILED(opt) do { Py_CLEAR(opt->trace); PyErr_Clear(); return -1; } while(0)

static void
resize_bytes(HotPyTraceRecorder *opt) {
    unsigned char *new_bytes;
    assert(opt->next_byte == opt->allocated_bytes);
    if (opt->trace == NULL)
        return;
    new_bytes = PyMem_Malloc(opt->allocated_bytes * 2);
    if (new_bytes == NULL) {
        opt->next_byte = 0;
        Py_CLEAR(opt->trace);
        return;
    }
    memcpy(new_bytes, opt->bytecodes, opt->allocated_bytes);
    PyMem_Free(opt->bytecodes);
    opt->bytecodes = new_bytes;
    opt->allocated_bytes *= 2;
}

#ifdef Py_DEBUG
#define APPEND_BYTE(b) append_byte(opt, b)
static void append_byte(HotPyTraceRecorder *opt, int b) {
    assert(opt->next_byte <= opt->allocated_bytes);
    if (opt->next_byte == opt->allocated_bytes)
        resize_bytes(opt);
    opt->bytecodes[opt->next_byte++] = (b);
}
#else
#define APPEND_BYTE(b) do { \
    assert(opt->next_byte <= opt->allocated_bytes); \
    if (opt->next_byte == opt->allocated_bytes) \
        resize_bytes(opt); \
    opt->bytecodes[opt->next_byte++] = (b); \
} while (0)
#endif

/* Steals a reference to the context */
static Py_ssize_t
new_exit(HotPyTraceRecorder *opt, HotPyContext *context)
{
    int err;
    Py_ssize_t index;
    PyObject *exit;
    exit = HotPyExitObject_New(context);
    Py_DECREF(context);
    if (exit == NULL)
        FAILED(opt);
    index = PyList_Size(opt->exits);
    err = PyList_Append(opt->exits, exit);
    Py_DECREF(exit);
    if (err)
        FAILED(opt);
    return index;
}

static Py_ssize_t
index_for_object(HotPyTraceRecorder *opt, PyObject *ids,
                 PyObject *list, PyObject *value)
{
    PyObject *index_obj;
    Py_ssize_t index;
    PyObject *id = PyLong_FromVoidPtr(value);
    if (id == NULL)
        FAILED(opt);
    index_obj = PyDict_GetItem(ids, id);
    if (index_obj == NULL) {
        index = PyList_Size(list);
        index_obj = PyLong_FromSize_t(index);
        if (index_obj == NULL)
            FAILED(opt);
        if (PyDict_SetItem(ids, id, index_obj))
            FAILED(opt);
        if (PyList_Append(list, value))
            FAILED(opt);
    } else
        index = PyLong_AsSsize_t(index_obj);
    Py_DECREF(id);
    return index;
}

static Py_ssize_t
index_for_constant(HotPyTraceRecorder *opt, PyObject *value)
{
    return index_for_object(opt, opt->constant_ids, opt->constant_list, value);
}

static Py_ssize_t
index_for_name(HotPyTraceRecorder *opt, PyObject *value)
{
    return index_for_object(opt, opt->name_ids, opt->name_list, value);
}

static void
push_constant(HotPyOptimiser *x, PyObject *value)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    Py_ssize_t index;
    assert(value);
    index = index_for_constant(opt, value);
    if (index < 0)
        return;
    APPEND_BYTE(TRACE_LOAD_CONSTANT);
    APPEND_BYTE(index);
    APPEND_BYTE(index >> 8);
}

static void
pick(HotPyOptimiser *x, int depth)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    if (depth == 1)
        APPEND_BYTE(DUP_TOP);
    else {
        APPEND_BYTE(PICK);
        APPEND_BYTE(depth);
        APPEND_BYTE(depth >> 8);
    }
}

static void
ensure(HotPyTraceRecorder *opt, int exit_index)
{
    APPEND_BYTE(EXIT_IF_DIFFERENT);
    APPEND_BYTE(exit_index);
    APPEND_BYTE(exit_index >> 8);
}

/* Debug info for context */
#ifdef Py_DEBUG
#define NAME_CONTEXT(context, name) context->cf_name = name
#else
#define NAME_CONTEXT(context, name) ((void)0)
#endif

static HotPyContext *
current_context(HotPyOptimiser *x)
{
    HotPyContext *context = HotPyContext_New();
    NAME_CONTEXT(context, "current");
    return context;
}

static void
dead_locals(HotPyOptimiser *x)
{
    /* No code to emit */
}

static HotPyContext *
require_type(HotPyOptimiser *x, PyTypeObject* type)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    Py_ssize_t index;
    HotPyContext *context = HotPyContext_New();
    NAME_CONTEXT(context, "require type");
    index = new_exit(opt, context);
    if (index < 0)
        return NULL;
    APPEND_BYTE(GET_TYPE);
    push_constant(x, (PyObject *)type);
    ensure(opt, index);
    return context;
}

static HotPyContext *
require_value(HotPyOptimiser *x, PyObject* value)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    Py_ssize_t index;
    HotPyContext *context = HotPyContext_New();
    NAME_CONTEXT(context, "require value");
    index = new_exit(opt, context);
    if (index < 0)
        return NULL;
    push_constant(x, value);
    ensure(opt, index);
    return context;
}

static HotPyContext *
require_lasti(HotPyOptimiser *x, int lasti)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    Py_ssize_t index;
    HotPyContext *context = HotPyContext_New();
    PyObject *offset;
    NAME_CONTEXT(context, "require lasti");
    index = new_exit(opt, context);
    if (index < 0)
        return NULL;
    offset = PyLong_FromLong(lasti);
    push_constant(x, offset);
    Py_DECREF(offset);
    APPEND_BYTE(GEN_CHECK_OR_EXIT);
    APPEND_BYTE(index);
    APPEND_BYTE(index >> 8);
    return context;
}

static HotPyContext *
require_boolean_or_int(HotPyOptimiser *x)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    Py_ssize_t index = new_exit(opt, context);
    NAME_CONTEXT(context, "require bool or int");
    if (index < 0)
        return NULL;
    APPEND_BYTE(EXIT_IF_NOT_BOOL_OR_INT);
    APPEND_BYTE(index);
    APPEND_BYTE(index >> 8);
    return context;
}

static HotPyContext *
opcode_with_exit(HotPyOptimiser *x, int op)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context = HotPyContext_New();
    Py_ssize_t index = new_exit(opt, context);
    NAME_CONTEXT(context, "opcode with exit");
    if (index < 0)
        return NULL;
    APPEND_BYTE(op);
    APPEND_BYTE(index);
    APPEND_BYTE(index >> 8);
    return context;
}


static void clear(HotPyOptimiser *x)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    opt->next_byte = 0;
    opt->allocated_bytes = 0;
    if (opt->bytecodes)
        PyMem_Free(opt->bytecodes);
    opt->bytecodes = NULL;
    Py_CLEAR(opt->exits);
    Py_CLEAR(opt->constant_ids);
    Py_CLEAR(opt->constant_list);
    Py_CLEAR(opt->name_ids);
    Py_CLEAR(opt->name_list);
    Py_CLEAR(opt->trace);
}

static void
fail(HotPyOptimiser *x, int op, char *msg)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    /* Blacklist the trace */
    HotPyTrace_Invalidate((PyObject *)opt->trace);
    opt->trace->execution_count = opt->trace->execution_count/2 - 1000;
    clear(x);
}

static void
finish(HotPyOptimiser *x)
{
    PyObject *consts;
    PyObject *exits;
    PyObject *bytes;
    PyObject *names;
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    if (opt->trace) {
        bytes = PyBytes_FromStringAndSize((char *)opt->bytecodes, opt->next_byte);
        consts = PyList_AsTuple(opt->constant_list);
        names = PyList_AsTuple(opt->name_list);
        exits = PyList_AsTuple(opt->exits);
        if (bytes && consts && exits && opt->trace->status == TRACE_NOT_READY)
            HotPyTraceObject_Init(opt->trace, bytes, exits, consts, names);
        else {
            HotPyTrace_Invalidate((PyObject *)opt->trace);
            Py_XDECREF(bytes);
            Py_XDECREF(consts);
            Py_XDECREF(exits);
        }
    }
    clear(x);
}

static int
start(HotPyOptimiser *x, HotPyTraceObject *trace,
      PyFrameObject *f, HotPyContext *ctx, char *reason)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    opt->next_byte = 0;
    opt->loop_header = 0;
    opt->bytecodes = PyMem_Malloc(128);
    opt->allocated_bytes = 128;
    assert(opt->exits == NULL);
    opt->exits = PyList_New(0);
    assert(opt->constant_ids == NULL);
    opt->constant_ids = PyDict_New();
    assert(opt->constant_list == NULL);
    opt->constant_list = PyList_New(0);
    assert(opt->name_ids == NULL);
    opt->name_ids = PyDict_New();
    assert(opt->name_list == NULL);
    opt->name_list = PyList_New(0);
    if (opt->bytecodes == NULL || opt->exits == NULL ||
        opt->constant_ids == NULL || opt->constant_list == NULL ||
        opt->name_ids == NULL || opt->name_list == NULL) {
        clear(x);
        return -1;
    }
    Py_INCREF(trace);
    opt->trace = trace;
    /* Mark as not-ready at start, so that any guards that are triggered
       during tracing (which is possible) are able to invalidate the trace */
    opt->trace->status = TRACE_NOT_READY;
    return 0;
}

static void
opcode(HotPyOptimiser *x, int op)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    APPEND_BYTE(op);
}

static void
opcode_with_arg(HotPyOptimiser *x, int op, int oparg)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    assert(oparg >= 0);
    if (oparg >= (1 << 16)) {
        APPEND_BYTE(EXTENDED_ARG);
        APPEND_BYTE(oparg >> 16);
        APPEND_BYTE(oparg >> 24);
    }
    APPEND_BYTE(op);
    APPEND_BYTE(oparg);
    APPEND_BYTE(oparg >> 8);
}

static void
opcode_with_name(HotPyOptimiser *x, int op, PyObject *name)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    Py_ssize_t index = index_for_name(opt, name);
    if (index < 0)
        return;
    opcode_with_arg(x, op, index);
}

static HotPyContext *
load_member(HotPyOptimiser *x, PyMemberDescrObject *desc)
{
    PyMemberDef *def = desc->d_member;
    if (def->type == T_OBJECT) {
        assert(def->offset < (1 << 16));
        opcode_with_arg(x, LOAD_SLOT, def->offset);
    }
    else if (def->type == T_OBJECT_EX) {
        /* XXX -- No EXIT here? */
        assert(def->offset < (1 << 16));
        opcode_with_arg(x, LOAD_SLOT_EX, def->offset);
    }
    else {
        /* Don't support int/float slots efficiently yet */
        push_constant(x, (PyObject *)desc);
        opcode(x, ROT_TWO);
        return opcode_with_exit(x, TRACE_DESCRIPTOR_GET);
    }
    return NULL;
}

static PyMemberDef func_code_def = {
    "__code__", T_OBJECT, offsetof(PyFunctionObject, func_code), READONLY
};

static PyMemberDef gen_code_def = {
    "gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY
};

static HotPyContext *
require_code(HotPyOptimiser *x, PyObject* obj)
{
    /* This should be rare if the specialiser is turned on */
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    HotPyContext *context;
    int index;
    context = HotPyContext_New();
    NAME_CONTEXT(context, "require code");
    index = new_exit(opt, context);
    if (index < 0)
        return NULL;
    if (PyFunction_Check(obj)) {
        PyObject *code = PyFunction_GET_CODE(obj);
        opcode_with_arg(x, LOAD_SLOT, func_code_def.offset);
        push_constant(x, code);
    }
    else {
        assert(PyGen_Check(obj));
        opcode_with_arg(x, LOAD_SLOT, gen_code_def.offset);
        push_constant(x, ((PyGenObject *)obj)->gi_code);
    }
    ensure(opt, index);
    return context;
}

static void
binary_op(HotPyOptimiser *x, int func, PyObject *res)
{
    opcode_with_arg(x, BINARY_OP, func);
}

static void
binary_op_kv(HotPyOptimiser *x, int func, PyObject *k, PyObject *res)
{
    push_constant(x, k);
    opcode(x, ROT_TWO);
    opcode_with_arg(x, BINARY_OP, func);
}

static void
binary_op_vk(HotPyOptimiser *x, int func, PyObject *k, PyObject *res)
{
    push_constant(x, k);
    opcode_with_arg(x, BINARY_OP, func);
}

static void
set_lasti(HotPyOptimiser *x, int line)
{
    opcode_with_arg(x, SET_LASTI, line);
}

static void
load_fast(HotPyOptimiser *x, int arg)
{
    opcode_with_arg(x, LOAD_FAST, arg);
}

static void
store_fast(HotPyOptimiser *x, int index)
{
    opcode_with_arg(x, STORE_FAST, index);
}

static void
delete_fast(HotPyOptimiser *x, int arg)
{
    opcode_with_arg(x, DELETE_FAST, arg);
}


/* Do some very simple peephole optimisation */

#define PREVIOUS_OP opt->bytecodes[opt->next_byte-3]
#define PREVIOUS_PREVIOUS_OP opt->bytecodes[opt->next_byte-6]
#define PREVIOUS_OPARG \
(opt->bytecodes[opt->next_byte-2] | (opt->bytecodes[opt->next_byte-1] << 8))
#define PREVIOUS_PREVIOUS_OPARG \
(opt->bytecodes[opt->next_byte-5] | (opt->bytecodes[opt->next_byte-4] << 8))

static void
load_clear_register(HotPyOptimiser *x, int arg)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    if (opt->next_byte > 5) {
        if (PREVIOUS_OP == STORE_REGISTER && PREVIOUS_OPARG == arg) {
            opt->next_byte -= 3;
            return;
        }
        else if (PREVIOUS_OP == TRACE_LOAD_CONSTANT &&
                 PREVIOUS_PREVIOUS_OP == STORE_REGISTER &&
                 PREVIOUS_PREVIOUS_OPARG == arg) {
            PREVIOUS_PREVIOUS_OP = TRACE_LOAD_CONSTANT;
            opt->bytecodes[opt->next_byte-5] = opt->bytecodes[opt->next_byte-2];
            opt->bytecodes[opt->next_byte-4] = opt->bytecodes[opt->next_byte-1];
            PREVIOUS_OP = ROT_TWO;
            opt->next_byte -= 2;
            return;
        }
    }
    opcode_with_arg(x, LOAD_AND_CLEAR_REGISTER, arg);
}

static HotPyContext *
call_cfunction(HotPyOptimiser *x, PyObject *f, int args)
{
    push_constant(x, f);
    switch (args) {
    case 0:
        return opcode_with_exit(x, CALL_CFUNC_0);
    case 1:
        return opcode_with_exit(x, CALL_CFUNC_1);
    case 2:
        return opcode_with_exit(x, CALL_CFUNC_2);
    default:
        assert("Illegal arg" && 0);
        abort();
    }
}

static HotPyContext *
call_cmethod(HotPyOptimiser *x, PyObject *m, int args)
{
    push_constant(x, m);
    switch (args) {
    case 0:
        return opcode_with_exit(x, CALL_CMETH_0);
    case 1:
        return opcode_with_exit(x, CALL_CMETH_1);
    case 2:
        return opcode_with_exit(x, CALL_CMETH_2);
    default:
        assert("Illegal arg" && 0);
        abort();
    }
}

static HotPyContext *
call_wrapper(HotPyOptimiser *x, int opcode, PyObject *w)
{
    push_constant(x, w);
    return opcode_with_exit(x, opcode);
}

static void
load_register(HotPyOptimiser *x, int arg)
{
    opcode_with_arg(x, LOAD_REGISTER, arg);
}

static void
store_register(HotPyOptimiser *x, int arg)
{
    opcode_with_arg(x, STORE_REGISTER, arg);
}

static void
tuple_concat(HotPyOptimiser *x)
{
    static int tuple_concat_function = 0;
    if (tuple_concat_function == 0) {
        int index;
        _Py_AddBinaryFastFunction(&_Py_operator_nb_add, &PyTuple_Type,
                  &PyTuple_Type, PyTuple_Type.tp_as_sequence->sq_concat);
        index = BINARY_FUNCTION_INDEX(&PyTuple_Type, &PyTuple_Type);
        tuple_concat_function =_Py_operator_nb_add.functions[index];
        assert(tuple_concat_function && "must be non-zero");
    }
    binary_op(x, tuple_concat_function, NULL);
}

static void
get_globals(HotPyOptimiser *x)
{
    opcode(x, GET_GLOBAL_AND_BUILTINS);
}

static void
load_from_globals(HotPyOptimiser *x, PyObject *name)
{
    opcode_with_name(x, LOAD_FROM_GLOBAL_AND_BUILTINS, name);
}

static void
fast_load_from_globals(HotPyOptimiser *x, PyObject *globals, PyObject *name)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    PyObject *builtins = PyDict_GetItemString(globals, "__builtins__");
    push_constant(x, globals);
    if (builtins)
        push_constant(x, builtins);
    else
        Py_CLEAR(opt->trace);
    opcode_with_name(x, LOAD_FROM_GLOBAL_AND_BUILTINS, name);
}

static void
store_to_globals(HotPyOptimiser *x, PyObject *name)
{
    opcode_with_name(x, STORE_TO_GLOBALS, name);
}

static void
has_special(HotPyOptimiser *x, PyObject *name)
{
    opcode_with_name(x, HAS_SPECIAL, name);
}

static void
fast_frame(HotPyOptimiser *x, int arg, PyCodeObject *code)
{
    opcode_with_arg(x, FAST_FRAME, arg);
}

static HotPyContext *
make_frame(HotPyOptimiser *x, PyObject *func)
{
    opcode(x, MAKE_FRAME);
    return NULL;
}

static HotPyContext *
make_frame_const(HotPyOptimiser *x, PyObject *func)
{
    opcode(x, MAKE_FRAME);
    return NULL;
}

static void
gen_enter(HotPyOptimiser *x, PyCodeObject *code)
{
    opcode(x, GEN_ENTER);
}

static void
enter_frame(HotPyOptimiser *x, PyCodeObject *code)
{
    opcode(x, ENTER_FRAME);
}

static void
leave_frame(HotPyOptimiser *x)
{
    opcode(x, LEAVE_FRAME);
}

static void unrolling(HotPyOptimiser *x)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    opt->loop_header = opt->next_byte;
}

static HotPyContext *
prepare_call1(HotPyOptimiser *x)
{
    return NULL;
}

static HotPyContext *
prepare_call2(HotPyOptimiser *x)
{
    return NULL;
}

static HotPyContext *
call_object(HotPyOptimiser *x)
{
    return opcode_with_exit(x, CALL_OBJECT);
}

static void
bind_callable(HotPyOptimiser *x, PyTypeObject *bound_type)
{
    /* XXX -- Is this guaranteed not to fail (except for MemoryError)? */
    opcode(x, DESCRIPTOR_GET);
}

static void
comment(HotPyOptimiser *x, const char *text)
{
}

static HotPyContext*
store_member(HotPyOptimiser *x, PyMemberDef *def)
{
    assert((def->flags & READONLY) == 0);
    if (def->type == T_OBJECT || def->type == T_OBJECT_EX) {
        assert(def->offset < (1 << 16));
        opcode_with_arg(x, STORE_SLOT, def->offset);
    }
    else {
        /* Index for store-member is 5 bit type code + 11 bit offset
         * Type code is divided into 2 bit size code & 2 bit
        */
        assert(def->offset < (1 << 11));
        opcode_with_arg(x, STORE_MEMBER, ((def->type & 31) << 11) | def->offset);
    }
    return NULL;
}

static HotPyContext*
from_object_dict_fast(HotPyOptimiser *x, int dict_offset,
                      PyDictKeysObject *keys, PyObject *key)
{
    int key_index = index_for_name((HotPyTraceRecorder *)x, key);
    if (key_index < 256) {
        opcode_with_arg(x, VALUE_FROM_OBJECT_DICT_AND_JUMP, (key_index<<8));
        return opcode_with_exit(x, EXIT);
    } else {
        push_constant(x, key);
        opcode(x, ROT_TWO);
        return opcode_with_exit(x, VALUE_FROM_OBJECT_DICT_OR_EXIT);
    }
}

static void
set_in_object_dict_fast(HotPyOptimiser *x, int dict_offset, PyDictKeysObject *keys, PyObject *key)
{
    int key_index = index_for_name((HotPyTraceRecorder *)x, key);
    if (key_index < 256) {
        opcode_with_arg(x, SET_IN_OBJECT_DICT_CONST, (key_index<<8));
    } else {
        push_constant(x, key);
        opcode(x, SET_IN_OBJECT_DICT);
    }
}

static void
setup_block(HotPyOptimiser *x, int op, int handler, int level)
{
    HotPyTraceRecorder *opt = (HotPyTraceRecorder *)x;
    if (op == SETUP_EXCEPT_HANDLER) {
        assert(handler == -1);
        opcode(x, op);
    } else {
        if (handler >= (1 << 12) || level >= (1 << 4))
            HotPyTrace_Invalidate((PyObject *)opt->trace);
        else
            opcode_with_arg(x, op, (handler << 4) | level);
    }
}

HotPyOptimiser *HotPyTraceRecorder_New(void) {
    HotPyTraceRecorder *x = PyMem_Malloc(sizeof(HotPyTraceRecorder));
    x->opt_base.opt_name = "trace recorder";
    x->opt_base.next = NULL;
    x->opt_base.push_constant = push_constant;
    x->opt_base.require_type = require_type;
    x->opt_base.require_lasti = require_lasti;
    x->opt_base.require_value = require_value;
    x->opt_base.require_code = require_code;
    x->opt_base.require_boolean_or_int = require_boolean_or_int;
    x->opt_base.opcode_with_exit = opcode_with_exit;
    x->opt_base.clear = clear;
    x->opt_base.start = start;
    x->opt_base.setup_block = setup_block;
    x->opt_base.finish = finish;
    x->opt_base.fail = fail;
    x->opt_base.comment = comment;
    x->opt_base.set_lasti = set_lasti;
    x->opt_base.unrolling = unrolling;
    x->opt_base.load_fast = load_fast;
    x->opt_base.store_fast = store_fast;
    x->opt_base.delete_fast = delete_fast;
    x->opt_base.get_globals = get_globals;
    x->opt_base.load_from_globals = load_from_globals;
    x->opt_base.fast_load_from_globals = fast_load_from_globals;
    x->opt_base.store_to_globals = store_to_globals;
    x->opt_base.has_special = has_special;
    x->opt_base.load_register = load_register;
    x->opt_base.load_clear_register = load_clear_register;
    x->opt_base.store_register = store_register;
    x->opt_base.set_lasti = set_lasti;
    x->opt_base.fast_frame = fast_frame;
    x->opt_base.make_frame = make_frame;
    x->opt_base.make_frame_const = make_frame_const;
    x->opt_base.enter_frame = enter_frame;
    x->opt_base.gen_enter = gen_enter;
    x->opt_base.leave_gen_frame = leave_frame;
    x->opt_base.leave_frame = leave_frame;
    x->opt_base.binary_op = binary_op;
    x->opt_base.binary_op_kv = binary_op_kv;
    x->opt_base.binary_op_vk = binary_op_vk;
    x->opt_base.tuple_concat = tuple_concat;
    x->opt_base.pick = pick;
    x->opt_base.bind_callable = bind_callable;
    x->opt_base.load_member = load_member;
    x->opt_base.store_member = store_member;
    x->opt_base.opcode = opcode;
    x->opt_base.opcode_with_arg = opcode_with_arg;
    x->opt_base.current_context = current_context;
    x->opt_base.dead_locals = dead_locals;
    x->opt_base.call_object = call_object;
    x->opt_base.call_cfunction = call_cfunction;
    x->opt_base.call_cmethod = call_cmethod;
    x->opt_base.call_wrapper = call_wrapper;
    x->opt_base.prepare_call1 = prepare_call1;
    x->opt_base.prepare_call2 = prepare_call2;
    x->opt_base.from_object_dict_fast = from_object_dict_fast;
    x->opt_base.set_in_object_dict_fast = set_in_object_dict_fast;
    x->next_byte = 0;
    x->bytecodes = NULL;
    x->allocated_bytes = 0;
    x->exits = NULL;
    x->name_ids = NULL;
    x->name_list = NULL;
    x->constant_ids = NULL;
    x->constant_list = NULL;
    x->trace = NULL;
    return (HotPyOptimiser *)x;
}