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