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