-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLuaJitBytecode.cpp
1173 lines (1064 loc) · 31.3 KB
/
LuaJitBytecode.cpp
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019, 2020 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the JuaJIT BC Viewer application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "LuaJitBytecode.h"
#include <QtDebug>
#include <QFile>
#include <QtEndian>
#include <lj_bc.h>
#include <QBuffer>
#include "StreamSpy.h"
using namespace Lua;
// Adapted from LuaJIT 2.0.5 lj_bcread.c
/* Bytecode dump header. */
#define BCDUMP_HEAD1 0x1b
#define BCDUMP_HEAD2 0x4c
#define BCDUMP_HEAD3 0x4a
/* If you perform *any* kind of private modifications to the bytecode itself
** or to the dump format, you *must* set BCDUMP_VERSION to 0x80 or higher.
*/
#define BCDUMP_VERSION 1
/* Compatibility flags. */
#define BCDUMP_F_BE 0x01
#define BCDUMP_F_STRIP 0x02
#define BCDUMP_F_FFI 0x04
#define BCDUMP_F_KNOWN (BCDUMP_F_FFI*2-1)
/* Flags for prototype. */
#define PROTO_CHILD 0x01 /* Has child prototypes. */
#define PROTO_VARARG 0x02 /* Vararg function. */
#define PROTO_FFI 0x04 /* Uses BC_KCDATA for FFI datatypes. */
#define PROTO_NOJIT 0x08 /* JIT disabled for this function. */
#define PROTO_ILOOP 0x10 /* Patched bytecode with ILOOP etc. */
/* Type codes for the GC constants of a prototype. Plus length for strings. */
enum {
BCDUMP_KGC_CHILD, BCDUMP_KGC_TAB, BCDUMP_KGC_I64, BCDUMP_KGC_U64,
BCDUMP_KGC_COMPLEX, BCDUMP_KGC_STR
};
/* Type codes for the keys/values of a constant table. */
enum {
BCDUMP_KTAB_NIL, BCDUMP_KTAB_FALSE, BCDUMP_KTAB_TRUE,
BCDUMP_KTAB_INT, BCDUMP_KTAB_NUM, BCDUMP_KTAB_STR
};
/* Fixed internal variable names. */
enum {
VARNAME_END,
VARNAME_FOR_IDX,
VARNAME_FOR_STOP,
VARNAME_FOR_STEP,
VARNAME_FOR_GEN,
VARNAME_FOR_STATE,
VARNAME_FOR_CTL,
VARNAME__MAX
};
static const char* s_varname[] = {
"",
"(for index)",
"(for limit)",
"(for step)",
"(for generator)",
"(for state)",
"(for control)"
};
// helper
union TValue {
lua_Number d;
struct {
quint32 lo;
quint32 hi;
};
};
typedef uint8_t BCReg;
struct _ByteCode
{
const char* d_op;
quint8 d_fa;
quint8 d_fb;
quint8 d_fcd;
} s_byteCodes[] =
{
#define BCSTRUCT(name, ma, mb, mc, mt) { #name, JitBytecode::Instruction::_##ma, \
JitBytecode::Instruction::_##mb, JitBytecode::Instruction::_##mc },
BCDEF(BCSTRUCT)
#undef BCSTRUCT
};
const char* JitBytecode::Instruction::s_typeName[] =
{
"",
"var",
"str",
"num",
"pri",
"dst",
"rbase",
"cdata",
"lit",
"lits",
"base",
"uv",
"jump",
"func",
"tab",
};
/* Read ULEB128 value from buffer. */
static quint32 bcread_uleb128(QIODevice* in)
{
quint32 result = 0;
int shift = 0;
while(true)
{
quint8 byte;
if( !in->getChar( (char*)&byte ) )
break;
result |= ( byte & 0x7f ) << shift;
if( ( byte & 0x80 ) == 0 )
break;
shift += 7;
}
return result;
}
/* Read ULEB128 value. */
static quint32 debug_read_uleb128(const quint8 *p, int& pos )
{
quint32 v = p[pos++];
if (v >= 0x80) {
int sh = 0;
v &= 0x7f;
do { v |= ((p[pos] & 0x7f) << (sh += 7)); } while (p[pos++] >= 0x80);
}
return v;
}
/* Add ULEB128 value to buffer. */
static void bcwrite_uleb128(QIODevice* out, uint32_t v)
{
for (; v >= 0x80; v >>= 7)
out->putChar( (char)((v & 0x7f) | 0x80) );
out->putChar( v );
}
/* Read top 32 bits of 33 bit ULEB128 value from buffer. */
static quint32 bcread_uleb128_33(QIODevice* in)
{
quint8 byte;
if( !in->getChar( (char*)&byte ) )
return 0;
quint32 result = (byte >> 1);
if( result >= 0x40 )
{
result &= 0x3f;
int shift = -1;
while(true)
{
if( !in->getChar( (char*)&byte ) )
break;
result |= ( byte & 0x7f ) << ( shift += 7 );
if( ( byte & 0x80 ) == 0 )
break;
}
}
return result;
}
static inline quint8 readByte(QIODevice* in)
{
char ch;
if( in->getChar(&ch) )
return quint8(ch);
return 0;
}
static inline void writeByte(QIODevice* out, quint8 b)
{
out->putChar((char)b);
}
static JitBytecode::CodeList readCode( QIODevice* in, bool swap, quint32 len )
{
static const int codeLen = 4;
JitBytecode::CodeList res(len);
for( int i = 0; i < len; i++ )
{
const QByteArray buf = in->read(codeLen);
if( buf.size() < codeLen)
return res;
quint32 tmp;
memcpy( &tmp, buf.constData(), codeLen );
if( swap )
res[i] = qbswap(tmp);
else
res[i] = tmp;
}
return res;
}
static JitBytecode::UpvalList readUpval( QIODevice* in, bool swap, quint32 len )
{
static const int codeLen = 2;
JitBytecode::UpvalList res(len);
for( int i = 0; i < len; i++ )
{
const QByteArray buf = in->read(codeLen);
if( buf.size() < codeLen)
return res;
quint16 tmp;
memcpy( &tmp, buf.constData(), codeLen );
if( swap )
res[i] = qbswap(tmp);
else
res[i] = tmp;
}
return res;
}
/* Read a single constant key/value of a template table. */
static QVariant bcread_ktabk(QIODevice* in )
{
const quint32 tp = bcread_uleb128(in);
if (tp >= BCDUMP_KTAB_STR) {
const quint32 len = tp - BCDUMP_KTAB_STR;
return in->read(len);
} else if (tp == BCDUMP_KTAB_INT) {
return bcread_uleb128(in);
} else if (tp == BCDUMP_KTAB_NUM) {
TValue u;
u.lo = bcread_uleb128(in);
u.hi = bcread_uleb128(in);
return u.d;
} else if ( tp == BCDUMP_KTAB_TRUE )
return true;
else if( tp == BCDUMP_KTAB_FALSE )
return false;
//else
Q_ASSERT( tp == BCDUMP_KTAB_NIL );
return QVariant();
}
JitBytecode::VariantList JitBytecode::readObjConsts( Function* f, QIODevice* in, quint32 len )
{
VariantList res(len);
for( int i = 0; i < len; i++ )
{
const quint32 tp = bcread_uleb128(in);
if( tp >= BCDUMP_KGC_STR )
{
quint32 len = tp - BCDUMP_KGC_STR;
const QByteArray str = in->read(len);
res[i] = str;
}else if( tp == BCDUMP_KGC_TAB )
{
ConstTable tbl;
const quint32 narray = bcread_uleb128(in);
const quint32 nhash = bcread_uleb128(in);
if( narray )
{
for (int j = 0; j < narray; j++)
tbl.d_array << bcread_ktabk(in);
// first item is always nil
tbl.d_array.pop_front();
}
if( nhash )
{
for ( int j = 0; j < nhash; j++)
tbl.d_hash.insert( bcread_ktabk(in), bcread_ktabk(in) );
}
res[i] = QVariant::fromValue(tbl);
}else if (tp != BCDUMP_KGC_CHILD) {
qCritical() << "FFI not supported";
} else {
Q_ASSERT(tp == BCDUMP_KGC_CHILD);
if( d_fstack.isEmpty() )
error(tr("referencing unknown child function"));
else
{
FuncRef r = d_fstack.back();
if( r->d_outer != 0 )
error(tr("invalid function hierarchy"));
else
r->d_outer = f;
res[i] = QVariant::fromValue(r);
d_fstack.pop_back();
}
}
}
return res;
}
static JitBytecode::VariantList readNumConsts( QIODevice* in, quint32 len )
{
JitBytecode::VariantList res(len);
for ( int i = 0; i < len; i++ )
{
const QByteArray ch = in->peek(1);
const int isnum = !ch.isEmpty() && ( ch[0] & 1 );
const quint32 lo = bcread_uleb128_33(in);
if (isnum) {
TValue u;
u.lo = lo;
u.hi = bcread_uleb128(in);
#if 0
if ((u.hi << 1) < 0xffe00000) { /* Finite? */ // 1111 1111 1110 0000 0000 0000 0000 0000
res << u.d;
} else if (((u.hi & 0x000fffff) | u.lo) != 0) { // 0000 0000 0000 1111 1111 1111 1111 1111
qDebug() << "nan";
} else if ((u.hi & 0x80000000) == 0) { // 1000 0000 0000 0000 0000 0000 0000 0000
qDebug() << "+inf";
} else {
qDebug() << "-inf";
}
#else
// const quint32 test = u.d + 6755399441055744.0; /* 2^52 + 2^51 */
// if op == "TSETM " then kc = kc - 2^52 end ???
res[i] = u.d;
#endif
} else {
res[i] = lo;
}
}
return res;
}
static QVector<quint32> readLineNumbers( QIODevice* in, bool swap, int sizeli, int sizebc, int numline, int firstline )
{
if( sizeli == 0 )
return QVector<quint32>();
const QByteArray buf = in->read(sizeli);
// buf contains a line number per bytecode encoded in 1, 2 or 4 bytes depending on line count,
// and then other stuff
if( buf.isEmpty() )
return QVector<quint32>();
if( buf.size() < sizeli )
{
qCritical() << "chunk too short";
return QVector<quint32>();
}
QVector<quint32> lines( sizebc ); // empty or one line nr per byteCodes entry
if( numline < 256 )
{
// 1 byte per number
for( int i = 0; i < sizebc; i++ )
lines[i] = quint8(buf[i]) + firstline;
}else if( numline < 65536 )
{
// 2 bytes per number
int j = 0;
quint16 tmp;
for( int i = 0; i < sizebc; i++, j += 2 )
{
memcpy( &tmp, buf.constData()+j, 2 );
if( swap )
tmp = qbswap(tmp);
lines[i] = tmp + firstline;
}
}else
{
// 4 bytes per number
int j = 0;
quint32 tmp;
for( int i = 0; i < sizebc; i++, j += 4 )
{
memcpy( &tmp, buf.constData()+j, 4 );
if( swap )
tmp = qbswap(tmp);
lines[i] = tmp + firstline;
}
}
return lines;
}
static QByteArray readNames(QIODevice* in, int len, int sizeuv, QByteArrayList& ups, QList<JitBytecode::Function::Var>& vars )
{
if( len == 0 )
return QByteArray();
const QByteArray tmp = in->read(len);
int pos = 0;
// the upvalue part is just a sequence of zero terminated strings
for( int i = 0; i < sizeuv; i++ )
{
int old = pos;
pos = tmp.indexOf(char(0),pos);
if( pos == -1 )
{
qCritical() << "invalid upval debug info";
return QByteArray();
}
ups.append( tmp.mid(old,pos-old));
pos++;
}
const QByteArray rawVars = tmp.mid(pos);
// interpreted from debug_varname in lj_debug.c
// the var part is a sequence of records terminated by zero
// each record is a sequence of a zero terminated string or a VARNAME, and then two uleb128 numbers
quint32 lastpc = 0;
while( true )
{
if( tmp.size() <= pos || tmp[pos] == 0 )
break;
JitBytecode::Function::Var var;
if( tmp[pos] > VARNAME__MAX )
{
int old = pos;
pos = tmp.indexOf(char(0),pos);
if( pos == -1 )
{
qCritical() << "invalid upval debug info";
return QByteArray();
}
var.d_name = tmp.mid(old,pos-old);
}else
var.d_name = s_varname[quint8(tmp[pos])];
pos++;
// there is an n:m relation between names and slot numbers
lastpc = var.d_startpc = lastpc + debug_read_uleb128( (const quint8*)tmp.constData(), pos );
var.d_endpc = var.d_startpc + debug_read_uleb128( (const quint8*)tmp.constData(), pos );
vars.append( var );
}
return rawVars;
}
JitBytecode::JitBytecode(QObject *parent) : QObject(parent)
{
//for( int i = 0; i < BC__MAX; i++ )
// qDebug() << QString("OP_%1, ").arg(s_byteCodes[i].d_op).toUtf8().constData();
}
bool JitBytecode::parse(const QString& file)
{
QFile in(file);
if( !in.open(QIODevice::ReadOnly) )
return error( tr("cannot open file for reading: %1").arg(file) );
return parse(&in);
}
bool JitBytecode::parse(QIODevice* in, const QString& path)
{
Q_ASSERT( in != 0 );
d_name.clear();
d_funcs.clear();
d_fstack.clear();
d_flags = 0;
if( !parseHeader(in) )
return false;
if( d_name.isEmpty() )
d_name = path;
while( !in->atEnd() )
{
if( !parseFunction(in) )
break; // eof
}
if( getRoot() )
getRoot()->d_isRoot = true;
return true;
}
bool JitBytecode::write(QIODevice* out, const QString& path)
{
if( d_fstack.size() != 1 )
return false;
writeHeader(out);
writeFunction(out,d_fstack.first().data());
out->putChar(0);
return true;
}
bool JitBytecode::write(const QString& file)
{
QFile out(file);
if( !out.open(QIODevice::WriteOnly|QIODevice::Unbuffered) )
return error( tr("cannot open file for writing: %1").arg(file) );
// OutStreamSpy spy(&out);
// return write(&spy);
return write(&out);
}
JitBytecode::Function* JitBytecode::getRoot() const
{
if( d_fstack.size() == 1 )
return d_fstack.first().data();
else
return 0;
}
bool JitBytecode::isStripped() const
{
return d_flags & BCDUMP_F_STRIP;
}
void JitBytecode::calcVarNames()
{
for( int i = 0; i < d_funcs.size(); i++ )
{
d_funcs[i]->calcVarNames();
}
}
void JitBytecode::clear()
{
d_funcs.clear();
d_fstack.clear();
d_name.clear();
d_flags = 0;
}
JitBytecode::Instruction JitBytecode::dissectInstruction(quint32 i)
{
Instruction res;
const int op = bc_op(i);
if( op >= 0 && op < BC__MAX )
{
const _ByteCode& bc = s_byteCodes[op];
res.d_name = bc.d_op;
res.d_op = op;
res.d_ta = bc.d_fa;
res.d_tb = bc.d_fb;
res.d_tcd = bc.d_fcd;
if( bc.d_fa != Instruction::Unused )
res.d_a = bc_a(i);
if( bc.d_fb != Instruction::Unused )
{
res.d_b = bc_b(i);
if( bc.d_fcd != Instruction::Unused )
res.d_cd = bc_c(i);
}else if( bc.d_fcd != Instruction::Unused )
res.d_cd = (i) >>16;
}else
res.d_name = "???";
return res;
}
JitBytecode::Op JitBytecode::opFromBc(quint32 i)
{
return (Op) bc_op(i);
}
JitBytecode::Format JitBytecode::formatFromOp(quint8 op)
{
if( op < BC__MAX && s_byteCodes[op].d_fb != Instruction::Unused )
return ABC;
else
return AD;
}
JitBytecode::Instruction::FieldType JitBytecode::typeCdFromOp(quint8 op)
{
if( op < BC__MAX )
return (Instruction::FieldType)s_byteCodes[op].d_fcd;
else
return Instruction::Unused;
}
JitBytecode::Instruction::FieldType JitBytecode::typeBFromOp(quint8 op)
{
if( op < BC__MAX )
return (Instruction::FieldType)s_byteCodes[op].d_fb;
else
return Instruction::Unused;
}
JitBytecode::Instruction::FieldType JitBytecode::typeAFromOp(quint8 op)
{
if( op < BC__MAX )
return (Instruction::FieldType)s_byteCodes[op].d_fa;
else
return Instruction::Unused;
}
bool JitBytecode::parseHeader(QIODevice* in)
{
const QByteArray buf = in->read(4);
const QString err = checkFileHeader(buf);
if( !err.isEmpty() )
return error(err);
d_flags = bcread_uleb128(in);
if ((d_flags & ~(BCDUMP_F_KNOWN)) != 0)
return error("unknown dump");
if ((d_flags & BCDUMP_F_FFI))
return error("FFI dumps not supported");
if( (d_flags & BCDUMP_F_STRIP) == 0 )
{
const quint32 len = bcread_uleb128(in);
d_name = in->read(len); // "@test.lua"
}
return true;
}
bool JitBytecode::writeHeader(QIODevice* out)
{
writeByte(out,BCDUMP_HEAD1);
writeByte(out,BCDUMP_HEAD2);
writeByte(out,BCDUMP_HEAD3);
writeByte(out,BCDUMP_VERSION);
writeByte( out, ( isStripped() ? BCDUMP_F_STRIP : 0 ) +
( QSysInfo::ByteOrder == QSysInfo::BigEndian ? BCDUMP_F_BE : 0 ) );
if( !isStripped() )
{
const QByteArray name = d_name.toUtf8();
bcwrite_uleb128(out, name.size() );
out->write( name );
}
return true;
}
bool JitBytecode::parseFunction(QIODevice* in )
{
/* Read length. */
quint32 len = bcread_uleb128(in);
if (!len)
return false; /* EOF */
FuncRef fr( new Function() );
Function& f = *fr.data();
f.d_sourceFile = d_name;
f.d_id = d_funcs.size();
/* Read prototype header. */
f.d_flags = readByte(in);
f.d_numparams = readByte(in);
f.d_framesize = readByte(in);
const quint8 sizeuv = readByte(in);
const quint32 sizekgc = bcread_uleb128(in);
const quint32 sizekn = bcread_uleb128(in);
const quint32 sizebc = bcread_uleb128(in);
const quint32 sizedbg = (d_flags & BCDUMP_F_STRIP) ? 0: bcread_uleb128(in);
f.d_firstline = sizedbg ? bcread_uleb128(in) : 0;
f.d_numline = sizedbg ? bcread_uleb128(in) : 0;
const bool swap = ( d_flags & BCDUMP_F_BE ) != ( QSysInfo::ByteOrder == QSysInfo::BigEndian );
f.d_byteCodes = readCode(in, swap, sizebc);
// Note: original prefixes bc with BC_FUNCV or BC_FUNCF and framesize, depending on flags PROTO_VARARG
f.d_upvals = readUpval( in, swap, sizeuv );
f.d_constObjs = readObjConsts( fr.data(), in, sizekgc );
f.d_constNums = readNumConsts( in, sizekn );
const quint32 sizeli = sizebc << (f.d_numline < 256 ? 0 : ( f.d_numline < 65536 ? 1 : 2 ) );
f.d_lines = readLineNumbers( in, swap, sizedbg ? sizeli : 0, sizebc, f.d_numline, f.d_firstline ); // empty or one line nr per byteCodes entry
readNames( in, sizedbg ? sizedbg - sizeli : 0, sizeuv, f.d_upNames, f.d_vars );
d_funcs.append(fr);
d_fstack.push_back(fr);
return true;
}
static void writeUpval( QIODevice* out, const JitBytecode::UpvalList& l )
{
static const int codeLen = 2;
char buf[2];
foreach( quint16 uv, l )
{
memcpy( buf, &uv, codeLen );
out->write(buf,codeLen);
}
}
bool JitBytecode::writeFunction(QIODevice* out, JitBytecode::Function* f)
{
for( int i = f->d_constObjs.size() - 1; i >= 0; i-- )
{
const QVariant&v = f->d_constObjs[i];
if( v.canConvert<FuncRef>() )
{
f->d_flags |= PROTO_CHILD;
writeFunction(out,v.value<FuncRef>().data() );
}
}
QBuffer tmp;
tmp.open(QIODevice::WriteOnly);
/* Write prototype header. */
writeByte(&tmp,f->d_flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
writeByte(&tmp,f->d_numparams);
writeByte(&tmp,f->d_framesize);
writeByte(&tmp,f->d_upvals.size());
// sizekgc
bcwrite_uleb128(&tmp, f->d_constObjs.size() );
// sizekn
bcwrite_uleb128(&tmp, f->d_constNums.size() );
// sizebc
bcwrite_uleb128(&tmp, f->d_byteCodes.size() );
QByteArray dbgInfo;
if( !isStripped() )
{
dbgInfo = writeDbgInfo(f);
bcwrite_uleb128(&tmp,dbgInfo.size());
if( !dbgInfo.isEmpty() )
{
bcwrite_uleb128(&tmp, f->d_firstline);
bcwrite_uleb128(&tmp, f->d_numline);
}
}
writeByteCodes(&tmp,f->d_byteCodes);
writeUpval(&tmp,f->d_upvals);
writeObjConsts( &tmp, f->d_constObjs );
writeNumConsts( &tmp, f->d_constNums );
// LineNumbers, Names
tmp.write(dbgInfo);
tmp.close();
bcwrite_uleb128(out,tmp.data().size());
out->write(tmp.data());
return true;
}
QByteArray JitBytecode::writeDbgInfo(JitBytecode::Function* f)
{
QBuffer buf;
buf.open(QIODevice::WriteOnly|QIODevice::Unbuffered);
char b[4];
if( f->d_numline < 256 )
{
// 1 byte per number
quint8 tmp;
quint32 len;
for( int i = 0; i < f->d_lines.size(); i++ )
{
len = f->d_lines[i] == 0 ? 0 : f->d_lines[i] - f->d_firstline;
if( len >= 256 )
qWarning() << "1 byte line number overflow at" << f->d_sourceFile << f->d_lines[i] << len;
tmp = len;
writeByte( &buf, tmp );
}
}else if( f->d_numline < 65536 )
{
// 2 bytes per number
quint16 tmp;
quint32 len;
for( int i = 0; i < f->d_lines.size(); i++ )
{
len = f->d_lines[i] == 0 ? 0 : f->d_lines[i] - f->d_firstline;
if( len >= 65536 )
qWarning() << "2 byte line number overflow at" << f->d_sourceFile << f->d_lines[i] << len;
tmp = len;
memcpy( b, &tmp, sizeof(tmp) );
buf.write(b,sizeof(tmp));
}
}else
{
// 4 bytes per number
quint32 tmp;
for( int i = 0; i < f->d_lines.size(); i++ )
{
tmp = f->d_lines[i] == 0 ? 0 : f->d_lines[i] - f->d_firstline;
memcpy( b, &tmp, sizeof(tmp) );
buf.write(b,sizeof(tmp));
}
}
// the upvalue part is just a sequence of zero terminated strings
for( int i = 0; i < f->d_upNames.size(); i++ )
{
buf.write(f->d_upNames[i]);
buf.putChar(0);
}
quint32 lastpc = 0;
for( int i = 0; i < f->d_vars.size(); i++ )
{
if( f->d_vars[i].d_name.startsWith('(') )
{
int code = -1;
for( int j = 1; j < VARNAME__MAX; j++ )
{
if( s_varname[j] == f->d_vars[i].d_name )
{
code = j;
break;
}
}
Q_ASSERT(code > 0);
writeByte(&buf,code);
}else
{
buf.write(f->d_vars[i].d_name);
buf.putChar(0);
}
bcwrite_uleb128(&buf, f->d_vars[i].d_startpc - lastpc );
lastpc = f->d_vars[i].d_startpc;
bcwrite_uleb128(&buf, f->d_vars[i].d_endpc - f->d_vars[i].d_startpc );
}
buf.putChar(0);
buf.close();
return buf.data();
}
static int32_t lj_num2bit(lua_Number n)
{
TValue o;
o.d = n + 6755399441055744.0; /* 2^52 + 2^51 */
return (int32_t)o.lo;
}
bool JitBytecode::writeNumConsts(QIODevice* out, const VariantList& l)
{
// adopted from LuaJIT lj_bcwrite
for( int i = 0; i < l.size(); i++ )
{
/* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */
/* Narrow number constants to integers. */
TValue o;
int32_t k;
bool isInt = false;
if( l[i].type() == QVariant::Int )
{
k = l[i].toInt();
isInt = true;
}else
{
o.d = l[i].toDouble();
const lua_Number num = o.d;
k = lj_num2bit(num);
isInt = ( num == (lua_Number)k );
}
if (isInt) { /* -0 is never a constant. */
QBuffer tmp;
tmp.open(QIODevice::WriteOnly|QIODevice::Unbuffered);
bcwrite_uleb128(&tmp, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u));
tmp.close();
if (k < 0) {
char *p = tmp.buffer().data() + tmp.buffer().size() - 1;
*p = (*p & 7) | ((k>>27) & 0x18);
}
out->write(tmp.buffer());
continue;
}
QBuffer tmp;
tmp.open(QIODevice::WriteOnly|QIODevice::Unbuffered);
bcwrite_uleb128(&tmp, 1+(2*o.lo | (o.lo & 0x80000000u)));
if (o.lo >= 0x80000000u) {
char *p = tmp.buffer().data() + tmp.buffer().size() - 1;
*p = (*p & 7) | ((o.lo>>27) & 0x18);
}
out->write(tmp.buffer());
bcwrite_uleb128(out, o.hi);
}
return true;
}
static void bcwrite_ktabk(QIODevice* out, const QVariant& v, bool narrow )
{
if( v.type() == QVariant::ByteArray )
{
const QByteArray str = v.toByteArray();
bcwrite_uleb128(out,BCDUMP_KGC_STR + str.size() );
out->write(str);
}else if( v.type() == QVariant::Int )
{
writeByte(out, BCDUMP_KTAB_INT);
bcwrite_uleb128(out, v.toInt());
}else if( v.type() == QVariant::Bool )
{
if( v.toBool() )
writeByte(out, BCDUMP_KTAB_TRUE);
else
writeByte(out, BCDUMP_KTAB_FALSE );
}else if( v.isNull() )
writeByte(out,BCDUMP_KTAB_NIL);
else
{
if( narrow )
{
/* Narrow number constants to integers. */
lua_Number num = v.toDouble();
int32_t k = lj_num2bit(num);
if (num == (lua_Number)k) { /* -0 is never a constant. */
writeByte(out, BCDUMP_KTAB_INT);
bcwrite_uleb128(out, k);
return;
}
}
TValue o;
o.d = v.toDouble();
writeByte(out, BCDUMP_KTAB_NUM);
bcwrite_uleb128(out, o.lo);
bcwrite_uleb128(out, o.hi);
}
}
static QByteArray unescape( QByteArray str )
{
str.replace("\\\\", "\\" );
str.replace("\\n", "\n" );
str.replace("\\a", "\a" );
str.replace("\\b", "\b" );
str.replace("\\f", "\f" );
str.replace("\\r", "\r" );
str.replace("\\t", "\t" );
str.replace("\\v", "\v" );
str.replace("\\\"", "\"" );
str.replace("\\'", "'" );
return str;
}
bool JitBytecode::writeObjConsts(QIODevice* out, const VariantList& l)
{
for( int i = 0; i < l.size(); i++ )
{
const QVariant& v = l[i];
if( isString(v) )
{
const QByteArray str = unescape(v.toByteArray());
bcwrite_uleb128(out,BCDUMP_KGC_STR + str.size() );
out->write(str);
}else if( v.canConvert<FuncRef>())
bcwrite_uleb128(out,BCDUMP_KGC_CHILD);
else if( v.canConvert<ConstTable>() )
{
ConstTable t = v.value<ConstTable>();
bcwrite_uleb128(out,BCDUMP_KGC_TAB);
if( !t.d_array.isEmpty() )
bcwrite_uleb128(out,t.d_array.size()+1);
else
bcwrite_uleb128(out,0);
bcwrite_uleb128(out,t.d_hash.size());
if( !t.d_array.isEmpty() )
{
bcwrite_ktabk(out,QVariant(),true); // the first element is always null
for( int i = 0; i < t.d_array.size(); i++ )
bcwrite_ktabk(out,t.d_array[i],true);
}
QHash<QVariant,QVariant>::const_iterator i;
for( i = t.d_hash.begin(); i != t.d_hash.end(); ++i )
{
bcwrite_ktabk(out,i.key(),false);
bcwrite_ktabk(out,i.value(),true);
}
}
}
return true;
}
bool JitBytecode::writeByteCodes(QIODevice* out, const JitBytecode::CodeList& l)
{
char buf[4];
for( int i = 0; i < l.size(); i++ )
{
const quint32 tmp = l[i];
::memcpy( buf, &tmp, 4 );
out->write(buf,4);
}
return true;
}
bool JitBytecode::error(const QString& msg)
{
qCritical() << msg;
return false;
}