• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "non-const-visitor.h"
28 #include "examples.h"
29 
30 using namespace vixl;
31 using namespace vixl::aarch64;
32 
33 #define __ masm->
34 
35 
VisitAddSubShifted(const Instruction * instr)36 void SwitchAddSubRegisterSources::VisitAddSubShifted(const Instruction* instr) {
37   int rn = instr->GetRn();
38   int rm = instr->GetRm();
39   // Only non-const visitors are allowed to discard constness of the visited
40   // instruction.
41   Instruction* mutable_instr = MutableInstruction(instr);
42   Instr instr_bits = mutable_instr->GetInstructionBits();
43 
44   // Switch the bitfields for the `rn` and `rm` registers.
45   instr_bits &= ~(Rn_mask | Rm_mask);
46   instr_bits |= (rn << Rm_offset) | (rm << Rn_offset);
47 
48   // Rewrite the instruction.
49   mutable_instr->SetInstructionBits(instr_bits);
50 }
51 
52 
GenerateNonConstVisitorTestCode(MacroAssembler * masm)53 void GenerateNonConstVisitorTestCode(MacroAssembler* masm) {
54   // int64_t foo(int64_t a, int64_t b)
55   //  Argument locations:
56   //    a -> x0
57   //    b -> x1
58   __ Sub(x0, x0, x1);
59   // The return value is in x0.
60   __ Ret();
61 }
62 
63 
RunNonConstVisitorTestGeneratedCode(const Instruction * start_instr)64 int64_t RunNonConstVisitorTestGeneratedCode(const Instruction* start_instr) {
65 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
66   Decoder simulator_decoder;
67   Simulator simulator(&simulator_decoder);
68 
69   int64_t a = 5;
70   int64_t b = 2;
71   simulator.WriteXRegister(0, a);
72   simulator.WriteXRegister(1, b);
73   simulator.RunFrom(start_instr);
74   int64_t res = simulator.ReadXRegister(0);
75   printf("foo(%" PRId64 ", %" PRId64 ") = %" PRId64 "\n", a, b, res);
76 
77   return res;
78 #else
79   // Without the simulator there is nothing to test.
80   USE(start_instr);
81   return 0;
82 #endif
83 }
84 
85 
86 #ifndef TEST_EXAMPLES
87 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
main(void)88 int main(void) {
89   MacroAssembler masm;
90 
91   // Generate the code.
92   Label code_start, code_end;
93   masm.Bind(&code_start);
94   GenerateNonConstVisitorTestCode(&masm);
95   masm.Bind(&code_end);
96   masm.FinalizeCode();
97   Instruction* instr_start = masm.GetLabelAddress<Instruction*>(&code_start);
98   Instruction* instr_end = masm.GetLabelAddress<Instruction*>(&code_end);
99 
100   // Run the code a first time.
101   RunNonConstVisitorTestGeneratedCode(instr_start);
102 
103   // Instantiate a decoder, disassembler, and our custom modifying visitor.
104   Decoder decoder;
105   PrintDisassembler disasm(stdout);
106   SwitchAddSubRegisterSources modifying_visitor;
107 
108   // Register visitors in such a way that when visiting instructions, the
109   // decoder will first disassemble the original instruction, modify it, and
110   // then disassemble the modified instruction.
111   decoder.AppendVisitor(&disasm);
112   decoder.AppendVisitor(&modifying_visitor);
113   decoder.AppendVisitor(&disasm);
114 
115   // Iterate through the instructions.
116   Instruction* instr;
117   for (instr = instr_start; instr < instr_end; instr += kInstructionSize) {
118     printf("---\n");
119     decoder.Decode(instr);
120   }
121 
122   // Run the modified code and observe the different output from before.
123   RunNonConstVisitorTestGeneratedCode(instr_start);
124 
125   return 0;
126 }
127 #else
128 // Without the simulator there is nothing to test.
main(void)129 int main(void) { return 0; }
130 #endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
131 #endif  // TEST_EXAMPLES
132 
133 
134 // This is only used by the testing code.
ModifyNonConstVisitorTestGeneratedCode(Instruction * start,Instruction * end)135 void ModifyNonConstVisitorTestGeneratedCode(Instruction* start,
136                                             Instruction* end) {
137   Decoder decoder;
138   SwitchAddSubRegisterSources modifying_visitor;
139   decoder.AppendVisitor(&modifying_visitor);
140 
141   Instruction* instr;
142   for (instr = start; instr < end; instr += kInstructionSize) {
143     printf("---\n");
144     decoder.Decode(instr);
145   }
146 }
147