Lines Matching refs:dex
26 static dex::u2 Pack_Z_8(dex::u4 a) {
27 dex::u2 fa = (a & 0xff);
33 static dex::u2 Pack_8_8(dex::u4 a, dex::u4 b) {
34 dex::u2 fa = (a & 0xff);
36 dex::u2 fb = (b & 0xff);
42 static dex::u2 Pack_4_4_8(dex::u4 a, dex::u4 b, dex::u4 c) {
43 dex::u2 fa = (a & 0xf);
45 dex::u2 fb = (b & 0xf);
47 dex::u2 fc = (c & 0xff);
53 static dex::u2 Pack_4_4_4_4(dex::u4 a, dex::u4 b, dex::u4 c, dex::u4 d) {
54 dex::u2 fa = (a & 0xf);
56 dex::u2 fb = (b & 0xf);
58 dex::u2 fc = (c & 0xf);
60 dex::u2 fd = (d & 0xf);
66 static dex::u2 Pack_16(dex::u4 a) {
67 dex::u2 fa = (a & 0xffff);
73 static dex::u4 Trim_S0(dex::u4 value) {
74 dex::u4 trim = value & 0xf;
75 SLICER_CHECK(dex::u4(dex::s4(trim << 28) >> 28) == value);
80 static dex::u4 Trim_S1(dex::u4 value) {
81 dex::u4 trim = value & 0xff;
82 SLICER_CHECK(dex::u4(dex::s4(trim << 24) >> 24) == value);
87 static dex::u4 Trim_S2(dex::u4 value) {
88 dex::u4 trim = value & 0xffff;
89 SLICER_CHECK(dex::u4(dex::s4(trim << 16) >> 16) == value);
95 static dex::u4 GetRegA(const Bytecode* bytecode, int index) {
96 auto flags = dex::GetFlagsFromOpcode(bytecode->opcode);
97 return (flags & dex::kInstrWideRegA) != 0
104 static dex::u4 GetRegB(const Bytecode* bytecode, int index) {
105 auto flags = dex::GetFlagsFromOpcode(bytecode->opcode);
106 return (flags & dex::kInstrWideRegB) != 0
113 static dex::u4 GetRegC(const Bytecode* bytecode, int index) {
114 auto flags = dex::GetFlagsFromOpcode(bytecode->opcode);
115 return (flags & dex::kInstrWideRegC) != 0
120 // Encode one instruction into a .dex bytecode
127 dex::Opcode opcode = bytecode->opcode;
132 if (opcode == dex::OP_GOTO) {
133 opcode = dex::OP_GOTO_16;
137 auto format = dex::GetFormatFromOpcode(opcode);
140 case dex::kFmt10x: // op
143 bytecode_.Push<dex::u2>(Pack_Z_8(opcode));
146 case dex::kFmt12x: // op vA, vB
149 dex::u4 vA = GetRegA(bytecode, 0);
150 dex::u4 vB = GetRegB(bytecode, 1);
151 bytecode_.Push<dex::u2>(Pack_4_4_8(vB, vA, opcode));
154 case dex::kFmt22x: // op vAA, vBBBB
157 dex::u4 vA = GetRegA(bytecode, 0);
158 dex::u4 vB = GetRegB(bytecode, 1);
159 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
160 bytecode_.Push<dex::u2>(Pack_16(vB));
163 case dex::kFmt32x: // op vAAAA, vBBBB
166 dex::u4 vA = GetRegA(bytecode, 0);
167 dex::u4 vB = GetRegB(bytecode, 1);
168 bytecode_.Push<dex::u2>(Pack_Z_8(opcode));
169 bytecode_.Push<dex::u2>(Pack_16(vA));
170 bytecode_.Push<dex::u2>(Pack_16(vB));
173 case dex::kFmt11n: // op vA, #+B
176 dex::u4 vA = GetRegA(bytecode, 0);
177 dex::u4 B = Trim_S0(bytecode->CastOperand<Const32>(1)->u.u4_value);
178 bytecode_.Push<dex::u2>(Pack_4_4_8(B, vA, opcode));
181 case dex::kFmt21s: // op vAA, #+BBBB
184 dex::u4 vA = GetRegA(bytecode, 0);
185 dex::u4 B = Trim_S2(bytecode->CastOperand<Const32>(1)->u.u4_value);
186 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
187 bytecode_.Push<dex::u2>(Pack_16(B));
190 case dex::kFmt11x: // op vAA
193 dex::u4 vA = GetRegA(bytecode, 0);
194 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
197 case dex::kFmt31i: // op vAA, #+BBBBBBBB
200 dex::u4 vA = GetRegA(bytecode, 0);
201 dex::u4 B = bytecode->CastOperand<Const32>(1)->u.u4_value;
202 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
203 bytecode_.Push<dex::u2>(Pack_16(B & 0xffff));
204 bytecode_.Push<dex::u2>(Pack_16(B >> 16));
207 case dex::kFmt20t: // op +AAAA
211 dex::u4 A = 0;
220 bytecode_.Push<dex::u2>(Pack_Z_8(opcode));
221 bytecode_.Push<dex::u2>(Pack_16(A & 0xffff));
224 case dex::kFmt30t: // op +AAAAAAAA
228 dex::u4 A = 0;
236 bytecode_.Push<dex::u2>(Pack_Z_8(opcode));
237 bytecode_.Push<dex::u2>(Pack_16(A & 0xffff));
238 bytecode_.Push<dex::u2>(Pack_16(A >> 16));
241 case dex::kFmt21t: // op vAA, +BBBB
244 dex::u4 vA = GetRegA(bytecode, 0);
246 dex::u4 B = 0;
255 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
256 bytecode_.Push<dex::u2>(Pack_16(B & 0xffff));
259 case dex::kFmt22t: // op vA, vB, +CCCC
262 dex::u4 vA = GetRegA(bytecode, 0);
263 dex::u4 vB = GetRegB(bytecode, 1);
265 dex::u4 C = 0;
274 bytecode_.Push<dex::u2>(Pack_4_4_8(vB, vA, opcode));
275 bytecode_.Push<dex::u2>(Pack_16(C & 0xffff));
278 case dex::kFmt31t: // op vAA, +BBBBBBBB
281 dex::u4 vA = GetRegA(bytecode, 0);
283 dex::u4 B = 0;
291 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
292 bytecode_.Push<dex::u2>(Pack_16(B & 0xffff));
293 bytecode_.Push<dex::u2>(Pack_16(B >> 16));
296 case dex::kFmt23x: // op vAA, vBB, vCC
299 dex::u4 vA = GetRegA(bytecode, 0);
300 dex::u4 vB = GetRegB(bytecode, 1);
301 dex::u4 vC = GetRegC(bytecode, 2);
302 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
303 bytecode_.Push<dex::u2>(Pack_8_8(vC, vB));
306 case dex::kFmt22b: // op vAA, vBB, #+CC
309 dex::u4 vA = GetRegA(bytecode, 0);
310 dex::u4 vB = GetRegB(bytecode, 1);
311 dex::u4 C = Trim_S1(bytecode->CastOperand<Const32>(2)->u.u4_value);
312 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
313 bytecode_.Push<dex::u2>(Pack_8_8(C, vB));
316 case dex::kFmt22s: // op vA, vB, #+CCCC
319 dex::u4 vA = GetRegA(bytecode, 0);
320 dex::u4 vB = GetRegB(bytecode, 1);
321 dex::u4 C = Trim_S2(bytecode->CastOperand<Const32>(2)->u.u4_value);
322 bytecode_.Push<dex::u2>(Pack_4_4_8(vB, vA, opcode));
323 bytecode_.Push<dex::u2>(Pack_16(C));
326 case dex::kFmt22c: // op vA, vB, thing@CCCC
329 dex::u4 vA = GetRegA(bytecode, 0);
330 dex::u4 vB = GetRegB(bytecode, 1);
331 dex::u4 C = bytecode->CastOperand<IndexedOperand>(2)->index;
332 bytecode_.Push<dex::u2>(Pack_4_4_8(vB, vA, opcode));
333 bytecode_.Push<dex::u2>(Pack_16(C));
336 case dex::kFmt21c: // op vAA, thing@BBBB
339 dex::u4 vA = GetRegA(bytecode, 0);
340 dex::u4 B = bytecode->CastOperand<IndexedOperand>(1)->index;
341 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
342 bytecode_.Push<dex::u2>(Pack_16(B));
345 case dex::kFmt31c: // op vAA, string@BBBBBBBB
348 dex::u4 vA = GetRegA(bytecode, 0);
349 dex::u4 B = bytecode->CastOperand<IndexedOperand>(1)->index;
350 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
351 bytecode_.Push<dex::u2>(Pack_16(B & 0xffff));
352 bytecode_.Push<dex::u2>(Pack_16(B >> 16));
355 case dex::kFmt35c: // op {vC,vD,vE,vF,vG}, thing@BBBB
359 dex::u4 B = bytecode->CastOperand<IndexedOperand>(1)->index;
360 dex::u4 A = regs.size();
361 dex::u4 C = (A > 0) ? regs[0] : 0;
362 dex::u4 D = (A > 1) ? regs[1] : 0;
363 dex::u4 E = (A > 2) ? regs[2] : 0;
364 dex::u4 F = (A > 3) ? regs[3] : 0;
365 dex::u4 G = (A > 4) ? regs[4] : 0;
366 bytecode_.Push<dex::u2>(Pack_4_4_8(A, G, opcode));
367 bytecode_.Push<dex::u2>(Pack_16(B));
368 bytecode_.Push<dex::u2>(Pack_4_4_4_4(F, E, D, C));
371 if ((dex::GetFlagsFromOpcode(opcode) & dex::kInstrInvoke) != 0) {
376 case dex::kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
380 dex::u4 A = vreg_range->count;
381 dex::u4 B = bytecode->CastOperand<IndexedOperand>(1)->index;
382 dex::u4 C = vreg_range->base_reg;
383 bytecode_.Push<dex::u2>(Pack_8_8(A, opcode));
384 bytecode_.Push<dex::u2>(Pack_16(B));
385 bytecode_.Push<dex::u2>(Pack_16(C));
388 if ((dex::GetFlagsFromOpcode(opcode) & dex::kInstrInvoke) != 0) {
393 case dex::kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB
396 dex::u4 vA = GetRegA(bytecode, 0);
397 dex::u8 B = bytecode->CastOperand<Const64>(1)->u.u8_value;
398 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
399 bytecode_.Push<dex::u2>(Pack_16((B >> 0) & 0xffff));
400 bytecode_.Push<dex::u2>(Pack_16((B >> 16) & 0xffff));
401 bytecode_.Push<dex::u2>(Pack_16((B >> 32) & 0xffff));
402 bytecode_.Push<dex::u2>(Pack_16((B >> 48) & 0xffff));
405 case dex::kFmt21h: // op vAA, #+BBBB0000[00000000]
408 case dex::OP_CONST_HIGH16: {
409 dex::u4 vA = GetRegA(bytecode, 0);
410 dex::u4 B = bytecode->CastOperand<Const32>(1)->u.u4_value >> 16;
411 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
412 bytecode_.Push<dex::u2>(Pack_16(B));
415 case dex::OP_CONST_WIDE_HIGH16: {
416 dex::u4 vA = GetRegA(bytecode, 0);
417 dex::u4 B = bytecode->CastOperand<Const64>(1)->u.u8_value >> 48;
418 bytecode_.Push<dex::u2>(Pack_8_8(vA, opcode));
419 bytecode_.Push<dex::u2>(Pack_16(B));
447 bytecode_.Push<dex::u2>(dex::kPackedSwitchSignature);
448 bytecode_.Push<dex::u2>(Pack_16(packed_switch->targets.size()));
449 bytecode_.Push<dex::s4>(packed_switch->first_key);
451 bytecode_.Push<dex::u4>(0);
471 bytecode_.Push<dex::u2>(dex::kSparseSwitchSignature);
472 bytecode_.Push<dex
474 bytecode_.Push<dex::s4>(switch_case.key);
477 bytecode_.Push<dex::u4>(0);
499 bytecode_.Push<dex::u2>(dex::OP_NOP);
528 dex::u2* const begin = bytecode_.ptr<dex::u2>(0);
529 dex::u2* const end = begin + bytecode_.size() / 2;
530 dex::u2* ptr = begin;
532 const auto opcode = dex::OpcodeFromBytecode(*ptr);
534 if (opcode == dex::OP_PACKED_SWITCH) {
535 auto dex_instr = dex::DecodeInstruction(ptr);
536 FixupPackedSwitch(offset, offset + dex::s4(dex_instr.vB));
537 } else if (opcode == dex::OP_SPARSE_SWITCH) {
538 auto dex_instr = dex::DecodeInstruction(ptr);
539 FixupSparseSwitch(offset, offset + dex::s4(dex_instr.vB));
541 auto isize = dex::GetWidthFromBytecode(ptr);
548 void BytecodeEncoder::FixupPackedSwitch(dex::u4 base_offset,
549 dex::u4 payload_offset) {
553 auto payload = bytecode_.ptr<dex::PackedSwitchPayload>(payload_offset * 2);
554 SLICER_CHECK(payload->ident == dex::kPackedSwitchSignature);
555 SLICER_CHECK(reinterpret_cast<dex::u1*>(payload->targets + payload->size) <=
565 void BytecodeEncoder::FixupSparseSwitch(dex::u4 base_offset,
566 dex::u4 payload_offset) {
570 auto payload = bytecode_.ptr<dex::SparseSwitchPayload>(payload_offset * 2);
571 SLICER_CHECK(payload->ident == dex::kSparseSwitchSignature);
573 dex::s4* const targets = payload->data + payload->size;
574 SLICER_CHECK(reinterpret_cast<dex::u1*>(targets + payload->size) <=
586 dex::u4 label_offset = fixup.label->offset;
589 dex::u4 rel_offset = label_offset - fixup.offset;
591 dex::u2* instr = bytecode_.ptr<dex::u2>(fixup.offset * 2);
618 // generate the .dex bytecodes
630 ir_code->instructions = slicer::ArrayView<const dex::u2>(
631 bytecode_.ptr<dex::u2>(0), bytecode_.size() / 2);