chikvdf 1.1.12

Bindings to the chikvdf C++ library.
Documentation
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
#ifndef ASM_TYPES_H
#define ASM_TYPES_H

/*
0   rax
1   rbx
2   rcx
3   rdx
rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15
rsp - stack pointer (used by stack engine)
rax/rdx - output of multiplication and division; temporaries

notation:
-each name is either a 64 bit scalar register or a 256 bit ymm register
-for ymm registers, a "_128" suffix is used for the xmm register
-for scalar registers: "_32" is used for "eax/r8d/etc", "_16" is used for "ax/r8w/etc", "_8" is used for "al/r8b/etc"
-writing to a 32 bit register zero-extends the result to 64 bits. writing to a 8/16 bit register does not zero extend
***/

const int spill_bytes=1024;
const int comment_asm_line_size=40;

const vector<string> scalar_register_names_64={
    "RSP", //  0 - stack pointer; used by stack engine etc. not allocated
    "RAX", //  1 - temporary; used for mul/div/etc. this is allocated last
    "RDX", //  2 - temporary; used for mul/div/etc. allocated 2nd last
    "RCX", //  3 - temporary; used for shr/etc. allocated 3rd last
    "RBX", //  4
    "RBP", //  5
    "RSI", //  6
    "RDI", //  7
    "R8",  //  8
    "R9",  //  9
    "R10", // 10
    "R11", // 11
    "R12", // 12
    "R13", // 13
    "R14", // 14
    "R15"  // 15
};

const vector<string> scalar_register_names_32={
    "ESP" , "EAX" , "EDX" , "ECX" ,
    "EBX" , "EBP" , "ESI" , "EDI" ,
    "R8D" , "R9D" , "R10D", "R11D",
    "R12D", "R13D", "R14D", "R15D"
};

const vector<string> scalar_register_names_16={
    "SP"  , "AX"  , "DX"  , "CX"  ,
    "BX"  , "BP"  , "SI"  , "DI"  ,
    "R8W" , "R9W" , "R10W", "R11W",
    "R12W", "R13W", "R14W", "R15W"
};

const vector<string> scalar_register_names_8={
    "SPL" , "AL"  , "DL"  , "CL"  ,
    "BL"  , "BPL" , "SIL" , "DIL" ,
    "R8B" , "R9B" , "R10B", "R11B",
    "R12B", "R13B", "R14B", "R15B"
};

string to_hex(int128 i) {
    int128 i_abs=(i<0)? -i : i;
    assert(i_abs>=0);
    assert(uint64(i_abs)==i_abs);

    ostringstream ss;
    ss << ((i<0)? "-" : "") << "0x" << hex << uint64(i_abs);
    return ss.str();
}

struct expand_macros_recording {
    int start_pos=-1;
    int end_pos=-1;

    ~expand_macros_recording() {
        assert((start_pos==-1 && end_pos==-1) || (start_pos!=-1 && end_pos!=-1));
    }
};

struct expand_macros {
    struct scope_data {
        string scope_name;
        map<string, string> name_to_value;
        bool is_public=false;
    };

    vector<scope_data> scopes;
    map<string, set<pair<int, string>>> value_to_name; //int is scope

    vector<vector<string>> res_text; //first entry is tag

    int next_label_id=0;
    int next_error_label_id=1; //can't be 0 since the id is used as the return code
    int next_output_error_label_id=1;

    int num_active_recordings=0;

    vector<string> tag_stack;
    bool output_tags=false;

    void begin_recording(expand_macros_recording& res) {
        assert(res.start_pos==-1 && res.end_pos==-1);
        res.start_pos=res_text.size();
        ++num_active_recordings;
    }

    vector<vector<string>> end_recording(expand_macros_recording& res) {
        assert(res.start_pos!=-1 && res.end_pos==-1);
        res.end_pos=res_text.size();
        --num_active_recordings;

        vector<vector<string>> c_text;
        for (int x=res.start_pos;x<res.end_pos;++x) {
            c_text.push_back(res_text.at(x));
        }
        return c_text;
    }

    void append_recording(vector<vector<string>> c_text) {
        for (auto& c : c_text) {
            res_text.push_back(c);
        }
    }

    string alloc_label() {
        assert(num_active_recordings==0);
        string res = "_" + asmprefix + "label_" + to_string(next_label_id);
        ++next_label_id;
        return res;
    }

    string alloc_error_label() {
        assert(num_active_recordings==0);
        string res = asmprefix + "label_error_" + to_string(next_error_label_id);
        ++next_error_label_id;
        return res;
    }

    void begin_scope(string name, bool is_public=false) {
        scopes.emplace_back(scope_data());
        scopes.back().scope_name=name;
        scopes.back().is_public=is_public;
    }

    void end_scope() {
        assert(!scopes.empty());
        for (pair<const string, string>& n : scopes.back().name_to_value) {
            bool erase_res=value_to_name.at(n.second).erase(make_pair(scopes.size()-1, n.first));
            assert(erase_res);
        }
        scopes.pop_back();
    }

    void bind_impl(string name, string value) {
        assert(!scopes.empty());

        bool emplace_res_1=scopes.back().name_to_value.emplace(name, value).second;
        assert(emplace_res_1);

        bool emplace_res_2=value_to_name[value].emplace(scopes.size()-1, name).second;
        assert(emplace_res_2);
    }

    string lookup_value(string name) {
        for (int x=scopes.size()-1;x>=0;--x) {
            if (x!=scopes.size()-1 && !scopes[x].is_public) {
                continue;
            }

            auto i=scopes[x].name_to_value.find(name);
            if (i!=scopes[x].name_to_value.end()) {
                return i->second;
            }
        }

        assert(false);
        return "";
    }

    string describe_scope() {
        string res;
        for (auto& c : scopes) {
            if (!res.empty()) {
                res+="/";
            }
            res+=c.scope_name;
        }
        return res;
    }

    string describe_name(string name) {
        string value=lookup_value(name);

        set<pair<int, string>>& names=value_to_name.at(value);

        string res;
        res+=name;
        res+="=";
        res+=value;

        if (names.size()>=2) {
            res+="(";
            bool first=true;
            for (auto& c : names) {
                if (!first) {
                    res+=",";
                }
                if (c.second!=name) {
                    res+=c.second;
                    first=false;
                }
            }
            res+=")";
        }

        return res;
    }

    pair<string, vector<string>> expand(string s) {
        string res;
        vector<string> res_names;
        string buffer;
        bool in_name=false;

        s+='\0';
        for (char c : s) {
            if (in_name) {
                if ((c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z') || c=='_') {
                    buffer+=c;
                } else {
                    in_name=false;
                    res+=lookup_value(buffer);
                    res_names.push_back(buffer);
                    buffer.clear();
                }
            }

            if (!in_name) {
                if (c=='`') {
                    in_name=true;
                } else {
                    if (c!='\0') {
                        res+=c;
                    }
                }
            }
        }

        return make_pair(res, res_names);
    }

    void append(string s, int line, string file, string func) {
        bool add_comment=true;

        assert(!s.empty());

        auto r=expand(s);

        res_text.emplace_back();
        res_text.back().push_back((tag_stack.empty())? "" : tag_stack.back());
        res_text.back().push_back(r.first);

        if (add_comment) {
            res_text.back().push_back( " # " + scopes.back().scope_name + ":" + to_string(line) + "    " );
            res_text.back().push_back(s);
        }
    }

    template<class type> typename type::bindable bind(const type& a, string n) {
        a.bind_impl(*this, n);
    }

    template<class type> struct void_box {
        typedef void value;
    };

    template<class type> typename void_box<typename type::value_type>::value bind(
        const type& a, string n
    ) {
        int x=0;
        for (const auto& c : a) {
            bind(c, n + "_" + to_string(x));
            ++x;
        }
    }

    string format_res_text() {
        string res;
        vector<int> sizes;

        int next_line=1;
        for (vector<string>& c : res_text) {
            string c_tag=c.at(0);
            if (output_tags && !c_tag.empty()) {
                c_tag = "_" + c_tag;
            }
            c.at(1)=asmprefix + str( "Xx_##: ", next_line, c_tag ) + c.at(1);
            ++next_line;


            for (int x=1;x<c.size();++x) {
                while (sizes.size()<=x) {
                    sizes.push_back(0);
                }
                sizes[x]=max(sizes[x], int(c[x].size()));
            }
        }

        sizes.at(1)=comment_asm_line_size;

        for (vector<string>& c : res_text) {
            for (int x=1;x<c.size();++x) {
                res+=c[x];
                if (x!=c.size()-1) {
                    for (int y=c[x].size();y<sizes.at(x);++y) {
                        res+= " " ;
                    }
                }
            }
            res+= "\n" ;
        }

        return res;
    }
};

struct expand_macros_tag {
    expand_macros& m;
    expand_macros_tag(expand_macros& t_m, string name) : m(t_m) {
        m.tag_stack.push_back(name);
    }
    ~expand_macros_tag() {
        m.tag_stack.pop_back();
    }
};

struct expand_macros_scope {
    expand_macros& m;

    expand_macros_scope(expand_macros& t_m, string name, bool is_public=false) : m(t_m) {
        m.begin_scope(name, is_public);
    }

    ~expand_macros_scope() {
        m.end_scope();
    }
};

#define EXPAND_MACROS_SCOPE expand_macros_scope c_scope(m, __func__)
#define EXPAND_MACROS_SCOPE_PUBLIC expand_macros_scope c_scope(m, __func__, true)

struct reg_scalar {
    static const bool is_spill=false;

    int value=-1;

    reg_scalar() {}
    explicit reg_scalar(int i) : value(i) {}

    string name(int num_bits=64) const {
        assert(value>=0);

        const vector<string>* names=nullptr;
        if (num_bits==64) {
            names=&scalar_register_names_64;
        } else
        if (num_bits==32) {
            names=&scalar_register_names_32;
        } else
        if (num_bits==16) {
            names=&scalar_register_names_16;
        } else {
            assert(num_bits==8);
            names=&scalar_register_names_8;
        }

        if (value<names->size()) {
            return names->at(value);
        } else {
            return str( "PSEUDO_#_#", value, num_bits );
        }
    }

    typedef void bindable;
    void bind_impl(expand_macros& m, string n) const {
        m.bind_impl(n, name(64));
        m.bind_impl(n + "_32", name(32));
        m.bind_impl(n + "_16", name(16));
        m.bind_impl(n + "_8", name(8));
    }
};

const reg_scalar reg_rsp=reg_scalar(0);
const reg_scalar reg_rax=reg_scalar(1);
const reg_scalar reg_rdx=reg_scalar(2);
const reg_scalar reg_rcx=reg_scalar(3);
const reg_scalar reg_rbx=reg_scalar(4);
const reg_scalar reg_rbp=reg_scalar(5);
const reg_scalar reg_rsi=reg_scalar(6);
const reg_scalar reg_rdi=reg_scalar(7);
const reg_scalar reg_r8=reg_scalar(8);
const reg_scalar reg_r9=reg_scalar(9);
const reg_scalar reg_r10=reg_scalar(10);
const reg_scalar reg_r11=reg_scalar(11);
const reg_scalar reg_r12=reg_scalar(12);
const reg_scalar reg_r13=reg_scalar(13);
const reg_scalar reg_r14=reg_scalar(14);
const reg_scalar reg_r15=reg_scalar(15);

struct reg_vector {
    static const bool is_spill=false;

    int default_num_bits=128;

    int value=-1;

    reg_vector() {}
    explicit reg_vector(int i, int t_default_num_bits) : value(i), default_num_bits(t_default_num_bits) {}

    string name(int num_bits=512) const {
        assert(value>=0);

        string prefix;
        if (num_bits==512) {
            prefix = "Z";
        } else
        if (num_bits==256) {
            prefix = "Y";
        } else {
            assert(num_bits==128);
            prefix = "X";
        }

        if (value>=32 || (!enable_all_instructions && (value>=16 || num_bits!=128))) {
            prefix = "PSEUDO_" + prefix;
        }

        return str( "#MM#", prefix, value );
    }

    typedef void bindable;
    void bind_impl(expand_macros& m, string n) const {
        m.bind_impl(n, name(default_num_bits));
        m.bind_impl(n + "_512", name(512));
        m.bind_impl(n + "_256", name(256));
        m.bind_impl(n + "_128", name(128));
    }
};

struct reg_spill {
    static const bool is_spill=true;

    int value=-1; //byte offset
    int size=-1;
    int alignment=-1; //power of 2, up to 64

    reg_spill() {}
    reg_spill(int t_value, int t_size, int t_alignment) : value(t_value), size(t_size), alignment(t_alignment) {}

    int get_rsp_offset() const {
        return value-spill_bytes;
    }

    //this is negative
    uint64 get_rsp_offset_uint64() const {
        return uint64(value-spill_bytes);
    }

    string name() const {
        assert(value>=0 && size>=1 && alignment>=1);
        assert(value%alignment==0);
        assert(value+size<=spill_bytes);

        return str( "[RSP+#]", to_hex(value-spill_bytes) );
    }

    typedef void bindable;
    void bind_impl(expand_macros& m, string n) const {
        m.bind_impl(n, name());
        m.bind_impl(n + "_rsp_offset", to_hex(value-spill_bytes));
    }

    reg_spill operator+(int byte_offset) const {
        reg_spill res=*this;
        res.value+=byte_offset;
        res.size-=byte_offset;
        res.alignment=1;
        return res;
    }
};

struct reg_alloc {
    vector<int> order_to_scalar;
    vector<int> scalar_to_order;

    set<int> scalars;
    set<int> vectors;
    vector<bool> spills;

    reg_alloc() {}

    void add(reg_scalar s) {
        bool insert_res=scalars.insert(scalar_to_order.at(s.value)).second;
        assert(insert_res);
    }

    void init() {
        const int num=32; //defines how many pseudo-registers to have

        order_to_scalar.resize(num, -1);
        scalar_to_order.resize(num, -1);

        int next_order=0;
        auto add_scalar=[&](reg_scalar scalar_reg) {
            int scalar=scalar_reg.value;

            int order=next_order;
            ++next_order;

            assert(order_to_scalar.at(order)==-1);
            order_to_scalar.at(order)=scalar;

            assert(scalar_to_order.at(scalar)==-1);
            scalar_to_order.at(scalar)=order;

            add(reg_scalar(scalar));
        };

        add_scalar(reg_rbx);
        add_scalar(reg_rbp);
        add_scalar(reg_rsi);
        add_scalar(reg_rdi);
        add_scalar(reg_r8);
        add_scalar(reg_r9);
        add_scalar(reg_r10);
        add_scalar(reg_r11);
        add_scalar(reg_r12);
        add_scalar(reg_r13);
        add_scalar(reg_r14);
        add_scalar(reg_r15);

        add_scalar(reg_rcx);
        add_scalar(reg_rdx);
        add_scalar(reg_rax);

        for (int x=16;x<num;++x) {
            reg_scalar r;
            r.value=x;
            add_scalar(r);
        }

        for (int x=0;x<num;++x) {
            vectors.insert(x);
        }
        for (int x=0;x<spill_bytes;++x) {
            spills.push_back(true);
        }
    }

    reg_scalar get_scalar(reg_scalar t_reg=reg_scalar()) {
        assert(!scalars.empty());

        int res=(t_reg.value==-1)? *scalars.begin() : scalar_to_order.at(t_reg.value);
        bool erase_res=scalars.erase(res);
        assert(erase_res);

        return reg_scalar(order_to_scalar.at(res));
    }

    reg_vector get_vector(int default_num_bits=128) {
        assert(!vectors.empty());

        int res=*vectors.begin();
        bool erase_res=vectors.erase(res);
        assert(erase_res);
        return reg_vector(res, default_num_bits);
    }

    reg_spill get_spill(int size=8, int alignment=-1) {
        if (alignment==-1) {
            alignment=size;
        }

        assert(alignment==1 || alignment==2 || alignment==4 || alignment==8 || alignment==16 || alignment==32 || alignment==64);

        for (int x=0;x<spills.size();++x) {
            if (x%alignment!=0) {
                continue;
            }

            bool valid=true;

            for (int y=0;y<size;++y) {
                if (x+y>=spills.size() || !spills[x+y]) {
                    valid=false;
                    break;
                }
            }

            if (valid) {
                for (int y=0;y<size;++y) {
                    spills.at(x+y)=false;
                }

                reg_spill res;
                res.value=x;
                res.size=size;
                res.alignment=alignment;
                return res;
            }
        }

        assert(false);
        return reg_spill();
    }

    reg_scalar bind_scalar(expand_macros& m, string name, reg_scalar t_reg=reg_scalar()) {
        reg_scalar res=get_scalar(t_reg);
        m.bind(res, name);
        return res;
    }

    reg_vector bind_vector(expand_macros& m, string name, int default_num_bits=128) {
        reg_vector res=get_vector(default_num_bits);
        m.bind(res, name);
        return res;
    }

    reg_spill bind_spill(expand_macros& m, string name, int size=8, int alignment=-1) {
        reg_spill res=get_spill(size, alignment);
        m.bind(res, name);
        return res;
    }
};

namespace asm_code {
    expand_macros m;
    #define APPEND_M(data) m.append(data, __LINE__, __FILE__, __func__)
}

// end Headerguard ASM_TYPES_H
#endif