• 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 <regex>
28 
29 #include "examples.h"
30 #include "non-const-visitor.h"
31 
32 using namespace vixl;
33 using namespace vixl::aarch64;
34 
35 #define __ masm->
36 
37 
Visit(Metadata * metadata,const Instruction * instr)38 void SwitchAddSubRegisterSources::Visit(Metadata* metadata,
39                                         const Instruction* instr) {
40   const std::string& form = (*metadata)["form"];
41 
42   // Match the forms for 32/64-bit add/subtract with shift, with optional flag
43   // setting.
44   if (std::regex_match(form,  // NOLINT: avoid clang-tidy-4.0 errors.
45                        std::regex("(?:add|sub)s?_(?:32|64)_addsub_shift"))) {
46     int rn = instr->GetRn();
47     int rm = instr->GetRm();
48     // Only non-const visitors are allowed to discard constness of the visited
49     // instruction.
50     Instruction* mutable_instr = MutableInstruction(instr);
51     Instr instr_bits = mutable_instr->GetInstructionBits();
52 
53     // Switch the bitfields for the `rn` and `rm` registers.
54     instr_bits &= ~(Rn_mask | Rm_mask);
55     instr_bits |= (rn << Rm_offset) | (rm << Rn_offset);
56 
57     // Rewrite the instruction.
58     mutable_instr->SetInstructionBits(instr_bits);
59   }
60 }
61 
62 
GenerateNonConstVisitorTestCode(MacroAssembler * masm)63 void GenerateNonConstVisitorTestCode(MacroAssembler* masm) {
64   // int64_t foo(int64_t a, int64_t b)
65   //  Argument locations:
66   //    a -> x0
67   //    b -> x1
68   __ Sub(x0, x0, x1);
69   // The return value is in x0.
70   __ Ret();
71 }
72 
73 
RunNonConstVisitorTestGeneratedCode(const Instruction * start_instr)74 int64_t RunNonConstVisitorTestGeneratedCode(const Instruction* start_instr) {
75 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
76   Decoder simulator_decoder;
77   Simulator simulator(&simulator_decoder);
78 
79   int64_t a = 5;
80   int64_t b = 2;
81   simulator.WriteXRegister(0, a);
82   simulator.WriteXRegister(1, b);
83   simulator.RunFrom(start_instr);
84   int64_t res = simulator.ReadXRegister(0);
85   printf("foo(%" PRId64 ", %" PRId64 ") = %" PRId64 "\n", a, b, res);
86 
87   return res;
88 #else
89   // Without the simulator there is nothing to test.
90   USE(start_instr);
91   return 0;
92 #endif
93 }
94 
95 
96 #ifndef TEST_EXAMPLES
97 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
main(void)98 int main(void) {
99   MacroAssembler masm;
100 
101   // Generate the code.
102   Label code_start, code_end;
103   masm.Bind(&code_start);
104   GenerateNonConstVisitorTestCode(&masm);
105   masm.Bind(&code_end);
106   masm.FinalizeCode();
107   Instruction* instr_start = masm.GetLabelAddress<Instruction*>(&code_start);
108   Instruction* instr_end = masm.GetLabelAddress<Instruction*>(&code_end);
109 
110   // Run the code a first time.
111   RunNonConstVisitorTestGeneratedCode(instr_start);
112 
113   // Instantiate a decoder, disassembler, and our custom modifying visitor.
114   Decoder decoder;
115   PrintDisassembler disasm(stdout);
116   SwitchAddSubRegisterSources modifying_visitor;
117 
118   // Register visitors in such a way that when visiting instructions, the
119   // decoder will first disassemble the original instruction, modify it, and
120   // then disassemble the modified instruction.
121   decoder.AppendVisitor(&disasm);
122   decoder.AppendVisitor(&modifying_visitor);
123   decoder.AppendVisitor(&disasm);
124 
125   // Iterate through the instructions.
126   Instruction* instr;
127   for (instr = instr_start; instr < instr_end; instr += kInstructionSize) {
128     printf("---\n");
129     decoder.Decode(instr);
130   }
131 
132   // Run the modified code and observe the different output from before.
133   RunNonConstVisitorTestGeneratedCode(instr_start);
134 
135   return 0;
136 }
137 #else
138 // Without the simulator there is nothing to test.
main(void)139 int main(void) { return 0; }
140 #endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
141 #endif  // TEST_EXAMPLES
142 
143 
144 // This is only used by the testing code.
ModifyNonConstVisitorTestGeneratedCode(Instruction * start,Instruction * end)145 void ModifyNonConstVisitorTestGeneratedCode(Instruction* start,
146                                             Instruction* end) {
147   Decoder decoder;
148   SwitchAddSubRegisterSources modifying_visitor;
149   decoder.AppendVisitor(&modifying_visitor);
150 
151   Instruction* instr;
152   for (instr = start; instr < end; instr += kInstructionSize) {
153     printf("---\n");
154     decoder.Decode(instr);
155   }
156 }
157