10cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// Copyright 2014, ARM Limited
2ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// All rights reserved.
3ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
4ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// Redistribution and use in source and binary forms, with or without
5ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// modification, are permitted provided that the following conditions are met:
6ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
7ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Redistributions of source code must retain the above copyright notice,
8ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     this list of conditions and the following disclaimer.
9ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Redistributions in binary form must reproduce the above copyright notice,
10ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     this list of conditions and the following disclaimer in the documentation
11ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     and/or other materials provided with the distribution.
12ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//   * Neither the name of ARM Limited nor the names of its contributors may be
13ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     used to endorse or promote products derived from this software without
14ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//     specific prior written permission.
15ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
16ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
270cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#include "vixl/a64/macro-assembler-a64.h"
280cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#include "vixl/a64/instructions-a64.h"
290cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#include "vixl/globals.h"
30ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
31ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixlusing namespace vixl;
32ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
33ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixlstatic const unsigned kDefaultInstructionCount = 100000;
34ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
35ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// This program focuses on emitting simple instructions.
36ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl//
37ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// This code will emit a given number of 'add x0, x1, x2' in a fixed size
38ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// buffer, looping over the buffer if necessary. This code therefore focuses
39ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl// on Emit and Operand.
40ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixlint main(int argc, char* argv[]) {
41ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  unsigned instructions = 0;
42ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
43ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  switch (argc) {
44ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl    case 1: instructions = kDefaultInstructionCount; break;
45ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl    case 2: instructions = atoi(argv[1]); break;
46ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl    default:
47ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl      printf("Usage: %s [#instructions]\n", argv[0]);
48ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl      exit(1);
49ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  }
50ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
51ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  const unsigned buffer_size = 256 * KBytes;
524a06316541258e3c058792321295ee36d409f419armvixl  const unsigned buffer_instruction_count = buffer_size / kInstructionSize;
534a06316541258e3c058792321295ee36d409f419armvixl  MacroAssembler masm(buffer_size);
54ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
554a06316541258e3c058792321295ee36d409f419armvixl  #define __ masm.
56ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
57ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  unsigned rounds = instructions / buffer_instruction_count;
58ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  for (unsigned i = 0; i < rounds; ++i) {
594a06316541258e3c058792321295ee36d409f419armvixl    {
604a06316541258e3c058792321295ee36d409f419armvixl      InstructionAccurateScope scope(&masm, buffer_instruction_count);
614a06316541258e3c058792321295ee36d409f419armvixl      for (unsigned j = 0; j < buffer_instruction_count; ++j) {
624a06316541258e3c058792321295ee36d409f419armvixl        __ add(x0, x1, Operand(x2));
634a06316541258e3c058792321295ee36d409f419armvixl      }
64ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl    }
654a06316541258e3c058792321295ee36d409f419armvixl    masm.Reset();
66ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  }
67ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
68ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  unsigned remaining = instructions % buffer_instruction_count;
694a06316541258e3c058792321295ee36d409f419armvixl  {
704a06316541258e3c058792321295ee36d409f419armvixl    InstructionAccurateScope scope(&masm, remaining);
714a06316541258e3c058792321295ee36d409f419armvixl    for (unsigned i = 0; i < remaining; ++i) {
724a06316541258e3c058792321295ee36d409f419armvixl      __ add(x0, x1, Operand(x2));
734a06316541258e3c058792321295ee36d409f419armvixl    }
74ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  }
75ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
764a06316541258e3c058792321295ee36d409f419armvixl  masm.FinalizeCode();
77ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl
78ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl  return 0;
79ad96eda8944ab1c1ba55715c50d9d6f0a3ed1dcarmvixl}
80