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#define __ masm->
31
32
33void GenerateNonConstVisitorTestCode(MacroAssembler* masm) {
34  // int64_t foo(int64_t a, int64_t b)
35  //  Argument locations:
36  //    a -> x0
37  //    b -> x1
38  __ Sub(x0, x0, x1);
39  // The return value is in x0.
40  __ Ret();
41}
42
43
44int64_t RunNonConstVisitorTestGeneratedCode(const Instruction* start_instr) {
45#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
46  Decoder simulator_decoder;
47  Simulator simulator(&simulator_decoder);
48
49  int64_t a = 5;
50  int64_t b = 2;
51  simulator.WriteXRegister(0, a);
52  simulator.WriteXRegister(1, b);
53  simulator.RunFrom(start_instr);
54  int64_t res = simulator.ReadXRegister(0);
55  printf("foo(%" PRId64 ", %" PRId64 ") = %" PRId64 "\n", a, b, res);
56
57  return res;
58#else
59  // Without the simulator there is nothing to test.
60  USE(start_instr);
61  return 0;
62#endif
63}
64
65
66#ifndef TEST_EXAMPLES
67#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
68int main(void) {
69  MacroAssembler masm;
70
71  // Generate the code.
72  Label code_start, code_end;
73  masm.Bind(&code_start);
74  GenerateNonConstVisitorTestCode(&masm);
75  masm.Bind(&code_end);
76  masm.FinalizeCode();
77  Instruction* instr_start = masm.GetLabelAddress<Instruction*>(&code_start);
78  Instruction* instr_end = masm.GetLabelAddress<Instruction*>(&code_end);
79
80  // Run the code a first time.
81  RunNonConstVisitorTestGeneratedCode(instr_start);
82
83  // Instantiate a decoder, disassembler, and our custom modifying visitor.
84  Decoder decoder;
85  PrintDisassembler disasm(stdout);
86  SwitchAddSubRegisterSources modifying_visitor;
87
88  // Register visitors in such a way that when visiting instructions, the
89  // decoder will first disassemble the original instruction, modify it, and
90  // then disassemble the modified instruction.
91  decoder.AppendVisitor(&disasm);
92  decoder.AppendVisitor(&modifying_visitor);
93  decoder.AppendVisitor(&disasm);
94
95  // Iterate through the instructions.
96  Instruction* instr;
97  for (instr = instr_start; instr < instr_end; instr += kInstructionSize) {
98    printf("---\n");
99    decoder.Decode(instr);
100  }
101
102  // Run the modified code and observe the different output from before.
103  RunNonConstVisitorTestGeneratedCode(instr_start);
104
105  return 0;
106}
107#else
108// Without the simulator there is nothing to test.
109int main(void) { return 0; }
110#endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
111#endif  // TEST_EXAMPLES
112
113
114// This is only used by the testing code.
115void ModifyNonConstVisitorTestGeneratedCode(Instruction* start,
116                                            Instruction* end) {
117  Decoder decoder;
118  SwitchAddSubRegisterSources modifying_visitor;
119  decoder.AppendVisitor(&modifying_visitor);
120
121  Instruction* instr;
122  for (instr = start; instr < end; instr += kInstructionSize) {
123    printf("---\n");
124    decoder.Decode(instr);
125  }
126}
127