-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLjDisasm.cpp
824 lines (788 loc) · 25.1 KB
/
LjDisasm.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
/*
* Copyright 2019 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the LjAsm parser library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library 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 "LjDisasm.h"
#include "LuaJitBytecode.h"
#include "LuaJitComposer.h"
#include <QtDebug>
#include <QSet>
#include <QTextStream>
#include <QFile>
using namespace Ljas;
using namespace Lua;
const char* Disasm::s_opName[] = {
"???",
"ISLT", "ISGE", "ISLE", "ISGT",
"ISEQ", "ISNE",
"ISTC", "ISFC", "IST", "ISF",
"MOV",
"NOT", "UNM",
"LEN", "ADD", "SUB", "MUL", "DIV", "MOD",
"POW",
"CAT",
"KSET", "KNIL",
"UGET", "USET",
"UCLO",
"FNEW",
"TNEW", "TDUP",
"GGET", "GSET",
"TGET", "TSET",
"CALL", "CALLT", "RET",
"FORI", "FORL",
"LOOP",
"JMP"
};
const char* Disasm::s_opHelp[] = {
"operation not supported",
"ISLT lhs:desig rhs:desig",
"ISGE lhs:desig rhs:desig",
"ISLE lhs:desig rhs:desig",
"ISGT lhs:desig rhs:desig",
"ISEQ lhs:desig rhs:( desig | string | number | primitive )",
"ISNE lhs:desig rhs:( desig | string | number | primitive )",
"ISTC lhs:desig rhs:desig, copy rhs to lhs and jump, if rhs is true",
"ISFC lhs:desig rhs:desig, copy rhs to lhs and jump, if rhs is false",
"IST slot:desig, jump if slot is true",
"ISF slot:desig, jump if slot is false",
"MOV dst:desig src:desig",
"NOT dst:desig src:desig",
"UNM dst:desig src:desig",
"LEN dst:desig table:desig",
"ADD dst:desig lhs:( desig | number ) rhs:( desig | number )",
"SUB dst:desig lhs:( desig | number ) rhs:( desig | number )",
"MUL dst:desig lhs:( desig | number ) rhs:( desig | number )",
"DIV dst:desig lhs:( desig | number ) rhs:( desig | number )",
"MOD dst:desig lhs:( desig | number ) rhs:( desig | number )",
"POW dst:desig lhs:desig rhs:desig, dst = lhs ^ rhs",
"CAT dst:desig from:desig [ len:posint ], dst = from .. ~ .. from + len - 1",
"KSET dst:desig const:( string | number | primitive | cname )",
"KNIL from:desig [ len:posint ], Set slots from to from + len - 1 to nil",
"UGET dst:desig uv:desig",
"USET uv:desig src:( string | number | primitive | desig )",
"UCLO from:desig [ label ], Close upvalues for slots ≥ from and jump to label",
"FNEW dst:desig fname",
"TNEW dst:desig [ arraySize:posint [ hashSize:posint ] ]",
"TDUP dst:desig src:( cname | table_literal )",
"GGET dst:desig index:( string | cname )",
"GSET src:desig index:( string | cname )",
"TGET dst:desig table:desig index:( desig | string | posint )",
"TSET src:desig table:desig index:( desig | string | posint )",
"CALL slots:desig [ numOfReturns:posint [ numOfArgs:posint ] ]",
"CALLT slots:desig [ numOfArgs:posint ]",
"RET [ slots:desig [ numOfSlots:posint ] ]",
"FORI slots:desig label, slots=index,stop,step,index copy",
"FORL desig label",
"LOOP",
"JMP label"
};
bool Disasm::disassemble(const JitBytecode& bc, QIODevice* f, const QString& path, bool stripped, bool alloc)
{
QTextStream out(f);
out.setCodec("UTF-8");
out << "-- disassembled from ";
if( path.isEmpty() )
out << "Lua source";
else
out << path;
out << endl << endl;
if( !writeFunc( out, bc.getRoot(), stripped, alloc, 0 ) )
return false;
return true;
}
bool Disasm::adaptToLjasm(JitBytecode::Instruction& bc, OP& op, QByteArray& warning )
{
switch( JitBytecode::Op(bc.d_op) )
{
case JitBytecode::OP_INVALID:
op = INVALID;
break;
case JitBytecode::OP_ISLT:
op = ISLT;
break;
case JitBytecode::OP_ISGE:
op = ISGE;
break;
case JitBytecode::OP_ISLE:
op = ISLE;
break;
case JitBytecode::OP_ISGT:
op = ISGT;
break;
case JitBytecode::OP_ISTC:
op = ISTC;
break;
case JitBytecode::OP_ISFC:
op = ISFC;
break;
case JitBytecode::OP_IST:
op = IST;
break;
case JitBytecode::OP_ISF:
op = ISF;
break;
case JitBytecode::OP_MOV:
op = MOV;
break;
case JitBytecode::OP_NOT:
op = NOT;
break;
case JitBytecode::OP_UNM:
op = UNM;
break;
case JitBytecode::OP_LEN:
op = LEN;
break;
case JitBytecode::OP_POW:
op = POW;
break;
case JitBytecode::OP_UGET:
op = UGET;
break;
case JitBytecode::OP_UCLO:
op = UCLO;
break;
case JitBytecode::OP_FNEW:
op = FNEW;
break;
case JitBytecode::OP_TDUP:
op = TDUP;
break;
case JitBytecode::OP_GGET:
op = GGET;
break;
case JitBytecode::OP_GSET:
op = GSET;
break;
case JitBytecode::OP_FORI:
op = FORI;
break;
case JitBytecode::OP_FORL:
op = FORL;
break;
case JitBytecode::OP_LOOP:
op = LOOP;
break;
case JitBytecode::OP_JMP:
op = JMP;
break;
case JitBytecode::OP_ISEQV:
case JitBytecode::OP_ISEQS:
case JitBytecode::OP_ISEQN:
case JitBytecode::OP_ISEQP:
op = ISEQ;
break;
case JitBytecode::OP_ISNEV:
case JitBytecode::OP_ISNES:
case JitBytecode::OP_ISNEN:
case JitBytecode::OP_ISNEP:
op = ISNE;
break;
case JitBytecode::OP_ADDVN:
case JitBytecode::OP_ADDNV:
case JitBytecode::OP_ADDVV:
op = ADD;
break;
case JitBytecode::OP_SUBVN:
case JitBytecode::OP_SUBNV:
case JitBytecode::OP_SUBVV:
op = SUB;
break;
case JitBytecode::OP_MULVN:
case JitBytecode::OP_MULNV:
case JitBytecode::OP_MULVV:
op = MUL;
break;
case JitBytecode::OP_DIVVN:
case JitBytecode::OP_DIVNV:
case JitBytecode::OP_DIVVV:
op = DIV;
break;
case JitBytecode::OP_MODVN:
case JitBytecode::OP_MODNV:
case JitBytecode::OP_MODVV:
op = MOD;
break;
case JitBytecode::OP_KSTR:
case JitBytecode::OP_KCDATA:
case JitBytecode::OP_KSHORT:
case JitBytecode::OP_KNUM:
case JitBytecode::OP_KPRI:
op = KSET;
break;
case JitBytecode::OP_USETV:
case JitBytecode::OP_USETS:
case JitBytecode::OP_USETN:
case JitBytecode::OP_USETP:
op = USET;
break;
case JitBytecode::OP_TGETV:
case JitBytecode::OP_TGETS:
case JitBytecode::OP_TGETB:
op = TGET;
break;
case JitBytecode::OP_TSETV:
case JitBytecode::OP_TSETS:
case JitBytecode::OP_TSETB:
op = TSET;
break;
case JitBytecode::OP_RET:
op = RET;
bc.d_cd--; // Operand D is one plus the number of results to return.
break;
case JitBytecode::OP_RET0:
op = RET;
bc.d_tcd = JitBytecode::Instruction::Unused;
bc.d_ta = JitBytecode::Instruction::Unused;
break;
case JitBytecode::OP_RET1:
op = RET;
bc.d_tcd = JitBytecode::Instruction::Unused;
break;
case JitBytecode::OP_KNIL:
op = KNIL;
bc.d_cd = bc.d_cd - bc.d_a + 1;
if( bc.d_cd == 1 )
bc.d_tcd = JitBytecode::Instruction::Unused;
else
bc.d_tcd = JitBytecode::Instruction::_lit;
break;
case JitBytecode::OP_TNEW:
op = TNEW;
bc.d_b = bc.d_cd & 0x7ff; // lowest 11 bits
bc.d_cd = bc.d_cd >> 11; // upper 5 bits
bc.d_tb = JitBytecode::Instruction::_lit;
bc.d_tcd = JitBytecode::Instruction::_lit;
if( bc.d_cd == 0 && bc.d_b == 0 )
{
bc.d_tcd = JitBytecode::Instruction::Unused;
bc.d_tb = JitBytecode::Instruction::Unused;
}else if( bc.d_cd == 0 )
bc.d_tcd = JitBytecode::Instruction::Unused;
break;
case JitBytecode::OP_CALL:
{
op = CALL;
bc.d_cd--;
if( bc.d_b >= 1 )
bc.d_b = bc.d_b - 1;
else if( bc.d_b == 0 ) // Operand B is zero for calls which return all results; modify to 1 return here
{
warning = "original second argument is MULTRES (not supported)";
bc.d_b = 1;
}
if( bc.d_b == 0 && bc.d_cd == 0 )
{
bc.d_tb = JitBytecode::Instruction::Unused;
bc.d_tcd = JitBytecode::Instruction::Unused;
}else if( bc.d_cd == 0 )
bc.d_tcd = JitBytecode::Instruction::Unused;
}
break;
case JitBytecode::OP_CALLM:
{
warning = "original is CALLM " + QByteArray::number(bc.d_b) + " "
+ QByteArray::number(bc.d_cd) + " (not supported)";
op = CALL;
// compared to CALL bc.d_cd is already the true number of fixed args
if( bc.d_cd == 0 )
bc.d_cd = 1; // we return at least one fixed arg
if( bc.d_b >= 1 )
bc.d_b = bc.d_b - 1;
else if( bc.d_b == 0 ) // Operand B is zero for calls which return all results; modify to 1 return here
bc.d_b = 1;
if( bc.d_b == 0 && bc.d_cd == 0 )
{
bc.d_tb = JitBytecode::Instruction::Unused;
bc.d_tcd = JitBytecode::Instruction::Unused;
}else if( bc.d_cd == 0 )
bc.d_tcd = JitBytecode::Instruction::Unused;
}
break;
case JitBytecode::OP_CALLT:
op = CALLT;
bc.d_cd--;
break;
case JitBytecode::OP_CALLMT:
op = INVALID;
warning = "original is CALLMT " + QByteArray::number(bc.d_cd) + " (not supported)";
if( bc.d_cd == 0 )
bc.d_cd = 1; // we return at least one fixed arg
break;
case JitBytecode::OP_CAT:
op = CAT;
bc.d_cd = bc.d_cd - bc.d_b + 1;
if( bc.d_cd == 1 )
bc.d_tcd = JitBytecode::Instruction::Unused;
else
bc.d_tcd = JitBytecode::Instruction::_lit;
break;
// implement later
case JitBytecode::OP_TSETM:
case JitBytecode::OP_RETM:
case JitBytecode::OP_VARG:
case JitBytecode::OP_ITERC:
case JitBytecode::OP_ITERN:
case JitBytecode::OP_ITERL:
// internals
case JitBytecode::OP_JFORI:
case JitBytecode::OP_IFORL:
case JitBytecode::OP_JFORL:
case JitBytecode::OP_IITERL:
case JitBytecode::OP_JITERL:
case JitBytecode::OP_ILOOP:
case JitBytecode::OP_JLOOP:
case JitBytecode::OP_ISNEXT:
warning = "operator not supported";
op = INVALID;
break;
}
return true;
}
bool Disasm::adaptToLjasm(JitBytecode::Instruction& bc, QByteArray& mnemonic, QByteArray& warning)
{
OP op;
bool res = adaptToLjasm( bc, op, warning );
mnemonic = s_opName[op];
if( op == INVALID )
mnemonic = bc.d_name;
return res;
}
static inline QByteArray ws(int level)
{
return QByteArray(level,'\t');
}
static inline QByteArray varName( const QByteArray& name, int v )
{
if( name.startsWith('(') )
{
return "R" + QByteArray::number(v);
}else
return name;
}
bool Disasm::writeFunc(QTextStream& out, const JitBytecode::Function* f, bool stripped, bool alloc, int level)
{
if( f == 0 )
return false;
f->calcVarNames();
const bool doStrip = f->isStripped() || stripped;
//int nextR = 0;
const int nextR = f->d_numparams;
out << ws(level) << "function F" << f->d_id << "(";
for( int i = 0; i < f->d_numparams; i++ )
{
if( i < f->d_varNames.size() && !doStrip )
{
if( i != 0 )
out << " ";
// Q_ASSERT( !f->d_varNames[i].isEmpty() );
out << f->d_varNames[i];
}else
{
if( i != 0 )
out << " ";
out << "R" << i;
// out << "__p" << i;
// nextR = i+1;
}
}
out << ") ";
if( !f->isStripped() )
{
out << "\t-- lines ";
out << JitComposer::unpackRow(f->d_firstline) << " to " <<
JitComposer::unpackRow(f->lastLine() );
}
out << endl;
#if 0
QHash<QByteArray,int> unique;
for( int i = 0; i < f->d_numparams && i < f->d_vars.size(); i++ )
unique.insert( f->d_vars[i].d_name,i);
if( f->d_framesize > f->d_numparams || f->d_framesize != 0 && f->d_vars.isEmpty() )
{
// print variable declarations
QByteArray buf;
buf = ws(level+1);
buf += "var\t";
if( f->d_vars.size() > f->d_numparams )
{
buf += "{ ";
for( int i = f->d_numparams; i < f->d_vars.size(); i++ )
{
QByteArray name = f->d_vars[i].d_name;
if( name.startsWith('(') )
continue; // don't use these as vars; they're used as Rx instead
if( unique.contains(name) )
{
// qDebug() << "duplicate var" << name;
continue;
}
unique.insert(name,i);
buf += name + " ";
if( buf.size() > 80 )
{
out << buf << endl;
buf = ws(level+2);
}
}
buf += "} ";
}
out << buf;
#ifdef _USE_REGISTER_ARRAY_
if( f->d_framesize > 0 )
{
out << "T[" << f->d_framesize << "]" << endl;
}
#else
buf.clear();
if( f->d_vars.size() > f->d_numparams )
{
out << endl;
buf = ws(level+2);
}
buf += "{ ";
for( int i = 0; i < qMax( int(f->d_framesize), unique.size() ); i++ )
{
buf += "R" + QByteArray::number( i ) + " ";
if( buf.size() > 80 )
{
out << buf << endl;
buf = ws(level+2);
}
}
buf += "} ";
out << buf << endl;
#endif
}
#else
// print variable declarations
if( f->d_framesize > f->d_numparams )
{
QHash<QByteArray,int> unique;
QByteArray buf;
buf = ws(level+1);
buf += "var\t";
if( !doStrip && f->d_vars.size() > f->d_numparams )
{
buf += "{ ";
for( int i = f->d_numparams; i < f->d_varNames.size(); i++ )
{
QByteArray name = f->d_varNames[i];
if( name.isEmpty() )
continue;
if( name.startsWith('(') )
continue; // don't use these as vars; they're used as Rx instead
if( unique.contains(name) )
continue;
unique.insert(name,i);
buf += name;
if( alloc )
buf += "(" + QByteArray::number(i) + ") ";
else
buf += " ";
if( buf.size() > 80 )
{
out << buf << endl;
buf = ws(level+2);
}
}
buf += "} ";
}
// print Rx declarations
out << buf;
buf.clear();
if( !doStrip && f->d_vars.size() > f->d_numparams )
{
out << endl;
buf = ws(level+2);
}
buf += "{ ";
for( int i = nextR; i < f->d_framesize; i++ )
{
buf += "R" + QByteArray::number( i );
if( alloc )
buf += "(" + QByteArray::number(i) + ") ";
else
buf += " ";
if( buf.size() > 80 )
{
out << buf << endl;
buf = ws(level+2);
}
}
buf += "} ";
out << buf << endl;
}
#endif
#ifdef _DEBUG_
for( int i = 0; i < f->d_vars.size(); i++ )
out << ws(level+2) << "-- " << f->d_vars[i].d_name << " pc " <<
f->d_vars[i].d_startpc << " to " << f->d_vars[i].d_endpc << endl;
#endif
#ifdef _DEBUG_
QFile dbg(QString("F%1_vars.txt").arg(f->d_id));
dbg.open(QIODevice::WriteOnly);
QTextStream out2(&dbg);
for( int pc = 0; pc < f->d_byteCodes.size(); pc++ )
{
QByteArray str = "pc" + QByteArray::number(pc+2) + "\t";
for( int slot = 0; slot < f->d_framesize; slot++ )
{
const QByteArray name = f->getVarName(pc,slot);
if( !name.isEmpty() )
str += "s" + QByteArray::number(slot) + ":" + name + "\t";
}
out2 << str << endl;
//out << ws(level+2) << "-- " << str << endl;
}
#endif
int funCount = 0;
for( int i = f->d_constObjs.size() - 1; i >= 0; i-- )
{
const QVariant& o = f->d_constObjs[i];
if( o.canConvert<JitBytecode::FuncRef>() )
{
if( funCount++ == 0 )
out << endl;
writeFunc(out, o.value<JitBytecode::FuncRef>().constData(), stripped, alloc, level+1 );
}
}
if( !f->d_byteCodes.isEmpty() )
{
out << ws(level) << "begin" << endl;
QSet<quint32> labels;
for( int pc = 0; pc < f->d_byteCodes.size(); pc++ )
{
const JitBytecode::Instruction bc = JitBytecode::dissectInstruction(f->d_byteCodes[pc]);
if( bc.d_tcd == JitBytecode::Instruction::_jump && bc.d_op != JitBytecode::OP_LOOP )
labels.insert( pc + 1 + bc.getCd() );
}
quint32 lastLine = 0;
for( int pc = 0; pc < f->d_byteCodes.size(); pc++ )
{
QByteArray warning;
if( labels.contains(pc) )
out << ws(level) << "__L" << pc << ":" << endl;
JitBytecode::Instruction bc = JitBytecode::dissectInstruction(f->d_byteCodes[pc]);
out << ws(level+1);
QByteArray mnemonic;
adaptToLjasm( bc, mnemonic, warning );
out << mnemonic << " ";
const QByteArray a = renderArg(f,bc.d_ta,bc.d_a,pc,stripped);
const QByteArray b = renderArg(f,bc.d_tb,bc.d_b,pc,stripped);
const QByteArray c = renderArg(f,bc.d_tcd,bc.getCd(),pc,stripped);
if( bc.d_op == JitBytecode::OP_LOOP )
{
// NOP
}else if( bc.d_op == JitBytecode::OP_JMP )
{
out << c;
}else
{
if( !a.isEmpty() )
out << " " << a;
if( !b.isEmpty() )
out << " " << b;
if( !c.isEmpty() )
out << " " << c;
}
if( !warning.isEmpty() )
out << " -- WARNING " << warning;
#ifdef _DEBUG_
out << " -- #" << pc + 1;
#endif
if( !f->isStripped() )
{
if( lastLine != f->d_lines[pc] )
{
lastLine = f->d_lines[pc];
out << "\t\t-- ";
if( JitComposer::isRowCol() )
out << JitComposer::unpackRow(lastLine) << ":" << JitComposer::unpackCol(lastLine);
else
out << lastLine;
}
}
out << endl;
} // end for each statement
}
out << ws(level) << "end F" << f->d_id << endl << endl;
return true;
}
static inline QByteArray getPriConst(int i)
{
switch(i)
{
case 1:
return "false";
case 2:
return "true";
default:
return "nil";
}
}
static QByteArray escape( QByteArray str )
{
if( !str.isEmpty() && str[0] == char(0) )
return QByteArray();
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;
}
static QByteArray tostring(const QVariant& v)
{
if( v.type() == QVariant::ByteArray )
{
return "\"" + escape( v.toByteArray() ) + "\"";
}else if( JitBytecode::isString( v ) )
return "\"" + v.toString().toUtf8() + "\"";
else if( JitBytecode::isNumber( v ) )
return QByteArray::number( v.toDouble() );
else
return v.toByteArray();
}
QByteArray Disasm::renderArg(const JitBytecode::Function* f, int t, int v, int pc, bool stripped, bool alt)
{
switch( t )
{
case JitBytecode::Instruction::Unused:
return QByteArray();
case JitBytecode::Instruction::_var:
case JitBytecode::Instruction::_dst:
case JitBytecode::Instruction::_base:
case JitBytecode::Instruction::_rbase:
{
// int idx;
// QByteArray name = varName(f->getVarName(pc, v, &idx ),idx);
QByteArray name;
if( !stripped )
name= varName( v < f->d_varNames.size() ? f->d_varNames[v] : QByteArray(),v);
if( !name.isEmpty() )
return name;
// if( v < f->d_numparams && ( f->isStripped() || stripped ) )
// return "__p" + QByteArray::number(v);
#ifdef _USE_REGISTER_ARRAY_
return "T[" + QByteArray::number(v) + "]";
#else
if( alt )
return "[" + QByteArray::number(v) + "]";
else
return "R" + QByteArray::number(v);
#endif
}
break;
case JitBytecode::Instruction::_str:
if( v >= 0 && v < f->d_constObjs.size() )
return tostring( f->d_constObjs[ f->d_constObjs.size() - v - 1] );
else
return "<invalid string>";
case JitBytecode::Instruction::_num:
if( v >= 0 && v < f->d_constNums.size())
return QByteArray::number( f->d_constNums[v].toDouble() );
else
return "<invalid number>";
case JitBytecode::Instruction::_pri:
return getPriConst(v);
case JitBytecode::Instruction::_cdata:
break; // ??
case JitBytecode::Instruction::_lit:
case JitBytecode::Instruction::_lits:
return QByteArray::number(v);
case JitBytecode::Instruction::_jump:
if( alt )
return "->" + QByteArray::number(pc+1+v);
else
return "__L" + QByteArray::number(pc+1+v);
case JitBytecode::Instruction::_uv:
{
const QPair<quint8, JitBytecode::Function*> up = f->getFuncSlotFromUpval(v);
if( up.second == 0 )
return "???";
QByteArray quali;
if( up.second != f )
quali = "F" + QByteArray::number(up.second->d_id) + ".";
// if( v < f->d_upNames.size() )
// return quali + f->d_upNames[v];
if( !stripped && up.first < up.second->d_varNames.size() && !up.second->d_varNames[up.first].isEmpty() )
return quali + up.second->d_varNames[up.first];
else
{
#ifdef _USE_REGISTER_ARRAY_
return quali + "T[" + QByteArray::number(up.first) + "]";
#else
if( alt )
return quali + "[" + QByteArray::number(up.first) + "]";
else
return quali + "R" + QByteArray::number(up.first);
#endif
}
}
break;
case JitBytecode::Instruction::_func:
{
const int i = f->d_constObjs.size() - v - 1;
if( i < 0 || i >= f->d_constObjs.size() )
return "<invalid const>";
JitBytecode::FuncRef fr = f->d_constObjs[ i ].value<JitBytecode::FuncRef>();
if( fr.data() != 0 )
return QString("F%1").arg(fr->d_id).toUtf8();
}
break;
case JitBytecode::Instruction::_tab:
if( v >= 0 && v < f->d_constObjs.size() &&
f->d_constObjs[ f->d_constObjs.size() - v - 1 ].canConvert<JitBytecode::ConstTable>() )
{
QByteArray str;
QTextStream out(&str);
out << "{ ";
JitBytecode::ConstTable t = f->d_constObjs[ f->d_constObjs.size() - v - 1 ].value<JitBytecode::ConstTable>();
if( !t.d_array.isEmpty() )
{
for( int i = 0; i < t.d_array.size(); i++ )
{
if( i != 0 )
out << " ";
out << tostring( t.d_array[i] );
}
}
if( !t.d_hash.isEmpty() )
{
QHash<QVariant,QVariant>::const_iterator i;
int n = 0;
for( i = t.d_hash.begin(); i != t.d_hash.end(); ++i, n++ )
{
if( n != 0 )
out << " ";
out << tostring( i.key() ) << " = " << tostring( i.value() );
}
}
out << " }";
out.flush();
return str;
}
return "???";
}
return QByteArray();
}