blob: 617f81819291132894182861ab6efbfa85ba52d7 [file] [log] [blame]
Carl Shapiro12eb78e2011-06-24 14:51:06 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_DEX_INSTRUCTION_H_
4#define ART_SRC_DEX_INSTRUCTION_H_
5
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
7#include "logging.h"
8#include "macros.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -07009
10namespace art {
11
Carl Shapiro12eb78e2011-06-24 14:51:06 -070012class Instruction {
13 public:
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070014 // NOP-encoded switch-statement signatures.
15 enum {
16 kPackedSwitchSignature = 0x0100,
17 kSparseSwitchSignature = 0x0200,
18 kArrayDataSignature = 0x0300
19 };
20
Carl Shapiro12eb78e2011-06-24 14:51:06 -070021 enum Code {
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070022#define INSTRUCTION_ENUM(opcode, cname, p, f, r, i, a) cname = opcode,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "dex_instruction_list.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070024 DEX_INSTRUCTION_LIST(INSTRUCTION_ENUM)
Carl Shapirod84f49c2011-06-29 00:27:46 -070025#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070026#undef INSTRUCTION_ENUM
Carl Shapirod84f49c2011-06-29 00:27:46 -070027 };
Carl Shapiro12eb78e2011-06-24 14:51:06 -070028
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070029 enum InstructionFormat {
30 k10x, // op
31 k12x, // op vA, vB
32 k11n, // op vA, #+B
33 k11x, // op vAA
34 k10t, // op +AA
35 k20t, // op +AAAA
36 k22x, // op vAA, vBBBB
37 k21t, // op vAA, +BBBB
38 k21s, // op vAA, #+BBBB
39 k21h, // op vAA, #+BBBB00000[00000000]
40 k21c, // op vAA, thing@BBBB
41 k23x, // op vAA, vBB, vCC
42 k22b, // op vAA, vBB, #+CC
43 k22t, // op vA, vB, +CCCC
44 k22s, // op vA, vB, #+CCCC
45 k22c, // op vA, vB, thing@CCCC
46 k32x, // op vAAAA, vBBBB
47 k30t, // op +AAAAAAAA
48 k31t, // op vAA, +BBBBBBBB
49 k31i, // op vAA, #+BBBBBBBB
50 k31c, // op vAA, thing@BBBBBBBB
51 k35c, // op {vC, vD, vE, vF, vG}, thing@BBBB (B: count, A: vG)
52 k3rc, // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
53 k51l, // op vAA, #+BBBBBBBBBBBBBBBB
54 };
55
56 enum Flags {
57 kBranch = 0x01, // conditional or unconditional branch
58 kContinue = 0x02, // flow can continue to next statement
59 kSwitch = 0x04, // switch statement
60 kThrow = 0x08, // could cause an exception to be thrown
61 kReturn = 0x10, // returns, no additional statements
62 kInvoke = 0x20, // a flavor of invoke
63 // TODO: kUnconditional
64 };
65
Carl Shapiro12eb78e2011-06-24 14:51:06 -070066 // Returns the size in bytes of this instruction.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070067 size_t Size() const;
Carl Shapiro12eb78e2011-06-24 14:51:06 -070068
69 // Returns a pointer to the next instruction in the stream.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070070 const Instruction* Next() const;
71
72 // Name of the instruction.
73 const char* Name() const {
74 return kInstructionNames[Opcode()];
75 }
Carl Shapiro12eb78e2011-06-24 14:51:06 -070076
77 // Returns the opcode field of the instruction.
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070078 Code Opcode() const;
Carl Shapiro12eb78e2011-06-24 14:51:06 -070079
80 // Reads an instruction out of the stream at the specified address.
81 static Instruction* At(byte* code) {
82 CHECK(code != NULL);
83 return reinterpret_cast<Instruction*>(code);
84 }
85
Carl Shapiroe4c1ce42011-07-09 02:31:57 -070086 // Returns the format of the current instruction.
87 InstructionFormat Format() const {
88 return kInstructionFormats[Opcode()];
89 }
90
91 // Returns true if this instruction is a branch.
92 bool IsBranch() const {
93 return (kInstructionFlags[Opcode()] & kBranch) != 0;
94 }
95
96 // Determine if the instruction is any of 'return' instructions.
97 bool IsReturn() const {
98 return (kInstructionFlags[Opcode()] & kReturn) != 0;
99 }
100
101 // Determine if this instruction ends execution of its basic block.
102 bool IsBasicBlockEnd() const {
103 return IsBranch() || IsReturn() || Opcode() == THROW;
104 }
105
106 // Determine if this instruction is an invoke.
107 bool IsInvoke() const {
108 return (kInstructionFlags[Opcode()] & kInvoke) != 0;
109 }
110
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700111 private:
Carl Shapiroe4c1ce42011-07-09 02:31:57 -0700112 static const char* const kInstructionNames[];
113 static InstructionFormat const kInstructionFormats[];
114 static int const kInstructionFlags[];
Carl Shapiro12eb78e2011-06-24 14:51:06 -0700115 DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
116};
117
118} // namespace art
119
120#endif // ART_SRC_DEX_INSTRUCTION_H_