VIXL Release 1.0

Refer to the README.md and LICENCE files for details.
diff --git a/examples/abs.cc b/examples/abs.cc
new file mode 100644
index 0000000..fa4f582
--- /dev/null
+++ b/examples/abs.cc
@@ -0,0 +1,67 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateAbs(MacroAssembler* masm) {
+  // int64_t abs(int64_t x)
+  // Argument location:
+  //   x -> x0
+
+  // This example uses a conditional instruction (cneg) to compute the
+  // absolute value of an integer.
+  __ Cmp(x0, 0);
+  __ Cneg(x0, x0, mi);
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label abs;
+  masm.Bind(&abs);
+  GenerateAbs(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  int64_t input_value = -42;
+  simulator.set_xreg(0, input_value);
+  simulator.RunFrom(abs.target());
+  printf("abs(%ld) = %ld\n", input_value, simulator.xreg(0));
+
+  return 0;
+}
+#endif
diff --git a/examples/add3-double.cc b/examples/add3-double.cc
new file mode 100644
index 0000000..c2d3f6f
--- /dev/null
+++ b/examples/add3-double.cc
@@ -0,0 +1,72 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateAdd3Double(MacroAssembler* masm) {
+  // double add3_double(double x, double y, double z)
+  //  Argument locations:
+  //    x -> d0
+  //    y -> d1
+  //    z -> d2
+  __ Fadd(d0, d0, d1);    // d0 <- x + y
+  __ Fadd(d0, d0, d2);    // d0 <- d0 + z
+
+  // The return value is already in d0.
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label add3_double;
+  masm.Bind(&add3_double);
+  GenerateAdd3Double(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  double a = 498.36547;
+  double b = 23.369;
+  double c = 7964.697954;
+  simulator.set_dreg(0, a);
+  simulator.set_dreg(1, b);
+  simulator.set_dreg(2, c);
+  simulator.RunFrom(add3_double.target());
+  printf("%f + %f + %f = %f\n", a, b, c, simulator.dreg(0));
+
+  return 0;
+}
+#endif
diff --git a/examples/add4-double.cc b/examples/add4-double.cc
new file mode 100644
index 0000000..ca1050f
--- /dev/null
+++ b/examples/add4-double.cc
@@ -0,0 +1,82 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateAdd4Double(MacroAssembler* masm) {
+  // double Add4Double(uint64_t a, double b, uint64_t c, double d)
+  //  Argument locations:
+  //    a -> x0
+  //    b -> d0
+  //    c -> x1
+  //    d -> d1
+
+  // Turn 'a' and 'c' into double values.
+  __ Ucvtf(d2, x0);
+  __ Ucvtf(d3, x1);
+
+  // Add everything together.
+  __ Fadd(d0, d0, d1);
+  __ Fadd(d2, d2, d3);
+  __ Fadd(d0, d0, d2);
+
+  // The return value is in d0.
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label add4_double;
+  masm.Bind(&add4_double);
+  GenerateAdd4Double(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  uint64_t a = 21;
+  double b = 987.3654;
+  uint64_t c = 4387;
+  double d = 36.698754;
+  simulator.set_xreg(0, a);
+  simulator.set_dreg(0, b);
+  simulator.set_xreg(1, c);
+  simulator.set_dreg(1, d);
+  simulator.RunFrom(add4_double.target());
+  printf("%ld + %f + %ld + %f = %f\n", a, b, c, d, simulator.dreg(0));
+
+  return 0;
+}
+#endif
diff --git a/examples/check-bounds.cc b/examples/check-bounds.cc
new file mode 100644
index 0000000..c4f1d39
--- /dev/null
+++ b/examples/check-bounds.cc
@@ -0,0 +1,95 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateCheckBounds(MacroAssembler* masm) {
+  // uint64_t check_bounds(uint64_t value, uint64_t low, uint64_t high)
+  // Argument locations:
+  //   value -> x0
+  //   low   -> x1
+  //   high  -> x2
+
+  // First we compare 'value' with the 'low' bound. If x1 <= x0 the N flag will
+  // be cleared. This configuration can be checked with the 'pl' condition.
+  __ Cmp(x0, x1);
+
+  // Now we will compare 'value' and 'high' (x0 and x2) but only if the 'pl'
+  // condition is verified. If the condition is not verified, we will clear
+  // all the flags except the carry one (C flag).
+  __ Ccmp(x0, x2, CFlag, pl);
+
+  // We set x0 to 1 only if the 'ls' condition is satisfied.
+  // 'ls' performs the following test: !(C==1 && Z==0). If the previous
+  // comparison has been skipped we have C==1 and Z==0, so the 'ls' test
+  // will fail and x0 will be set to 0.
+  // Otherwise if the previous comparison occurred, x0 will be set to 1
+  // only if x0 is less than or equal to x2.
+  __ Cset(x0, ls);
+
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+void run_function(Simulator *simulator, Label *function,
+                  uint64_t value, uint64_t low, uint64_t high) {
+  simulator->set_xreg(0, value);
+  simulator->set_xreg(1, low);
+  simulator->set_xreg(2, high);
+
+  simulator->RunFrom(function->target());
+  printf("%ld %s between %ld and %ld\n", value,
+         simulator->xreg(0) ? "is" : "is not",
+         low, high);
+
+  simulator->ResetState();
+}
+
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label check_bounds;
+  masm.Bind(&check_bounds);
+  GenerateCheckBounds(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  run_function(&simulator, &check_bounds, 546, 50, 1000);
+  run_function(&simulator, &check_bounds, 62, 100, 200);
+  run_function(&simulator, &check_bounds, 200, 100, 200);
+
+  return 0;
+}
+#endif
diff --git a/examples/debugger.cc b/examples/debugger.cc
new file mode 100644
index 0000000..35feb09
--- /dev/null
+++ b/examples/debugger.cc
@@ -0,0 +1,70 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+// This is an interactive example, not to be used for testing.
+#ifndef TEST_EXAMPLES
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+// The aim is to let the user "play" with the debugger. Brk will trigger the
+// debugger shell.
+void GenerateBreak(MacroAssembler* masm) {
+  Label hop;
+  __ Brk();
+  __ Nop();
+  __ B(&hop);
+  __ Nop();
+  __ Bind(&hop);
+  __ Mov(x1, 123);
+  __ Mov(x2, 456);
+  __ Add(x0, x1, x2);
+  __ Ret();
+}
+
+
+int main(void) {
+  // Create and initialize the assembler and the debugger.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Debugger debugger(&decoder);
+
+  // Generate the code for the example function.
+  Label start;
+  masm.Bind(&start);
+  GenerateBreak(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  debugger.RunFrom(start.target());
+  printf("Debugger example run\n");
+
+  return 0;
+}
+#endif
diff --git a/examples/examples.h b/examples/examples.h
new file mode 100644
index 0000000..1952a12
--- /dev/null
+++ b/examples/examples.h
@@ -0,0 +1,100 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef VIXL_EXAMPLE_EXAMPLES_H_
+# define VIXL_EXAMPLE_EXAMPLES_H_
+
+#include "a64/simulator-a64.h"
+#include "a64/debugger-a64.h"
+#include "a64/macro-assembler-a64.h"
+
+using namespace vixl;
+
+// Generate a function with the following prototype:
+//   uint64_t factorial(uint64_t n)
+//
+// It provides an iterative implementation of the factorial computation.
+void GenerateFactorial(MacroAssembler* masm);
+
+// Generate a function with the following prototype:
+//   uint64_t factorial_rec(uint64_t n)
+//
+// It provides a recursive implementation of the factorial computation.
+void GenerateFactorialRec(MacroAssembler* masm);
+
+// Generate a function with the following prototype:
+//   double add3_double(double x, double y, double z)
+//
+// This example is intended to show the calling convention with double
+// floating point arguments.
+void GenerateAdd3Double(MacroAssembler* masm);
+
+// Generate a function with the following prototype:
+//   double add4_double(uint64_t a, double b, uint64_t c, double d)
+//
+// The generated function pictures the calling convention for functions
+// mixing integer and floating point arguments.
+void GenerateAdd4Double(MacroAssembler* masm);
+
+// Generate a function with the following prototype:
+//   uint32_t sum_array(uint8_t* array, uint32_t size)
+//
+// The generated function computes the sum of all the elements in
+// the given array.
+void GenerateSumArray(MacroAssembler* masm);
+
+// Generate a function with the following prototype:
+//   int64_t abs(int64_t x)
+//
+// The generated function computes the absolute value of an integer.
+void GenerateAbs(MacroAssembler* masm);
+
+// Generate a function with the following prototype:
+//   uint64_t check_bounds(uint64_t value, uint64_t low, uint64_t high)
+//
+// The goal of this example is to illustrate the use of conditional
+// instructions. The generated function will check that the given value is
+// contained within the given boundaries. It returns 1 if 'value' is between
+// 'low' and 'high' (ie. low <= value <= high).
+void GenerateCheckBounds(MacroAssembler* masm);
+
+// Generate a function which uses the stack to swap the content of the x0, x1,
+// x2 and x3 registers.
+void GenerateSwap4(MacroAssembler* masm);
+
+// Generate a function which swaps the content of w0 and w1.
+// This example demonstrates some interesting features of VIXL's stack
+// operations.
+void GenerateSwapInt32(MacroAssembler* masm);
+
+// Generate a function with the following prototype:
+//   uint64_t demo_function(uint64_t x)
+//
+// This is the example used in doc/getting-started.txt
+void GenerateDemoFunction(MacroAssembler *masm);
+
+
+#endif /* !VIXL_EXAMPLE_EXAMPLES_H_ */
diff --git a/examples/factorial-rec.cc b/examples/factorial-rec.cc
new file mode 100644
index 0000000..2d5cb4c
--- /dev/null
+++ b/examples/factorial-rec.cc
@@ -0,0 +1,79 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateFactorialRec(MacroAssembler* masm) {
+  // uint64_t factorial_rec(uint64_t n)
+  // Argument location:
+  //   n -> x0
+
+  Label entry, input_is_zero;
+
+  __ Bind(&entry);
+  // Check for the stopping condition: the input number is null.
+  __ Cbz(x0, &input_is_zero);
+
+  __ Mov(x1, x0);
+  __ Sub(x0, x0, 1);
+  __ Push(x1, lr);
+  __ Bl(&entry);    // Recursive call factorial_rec(n - 1).
+  __ Pop(lr, x1);
+  __ Mul(x0, x0, x1);
+  __ Ret();
+
+  __ Bind(&input_is_zero);
+  __ Mov(x0, 1);
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label factorial_rec;
+  masm.Bind(&factorial_rec);
+  GenerateFactorialRec(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  uint64_t input_val = 16;
+  simulator.set_xreg(0, input_val);
+  simulator.RunFrom(factorial_rec.target());
+  printf("factorial(%ld) = %ld\n", input_val, simulator.xreg(0));
+
+  return 0;
+}
+#endif
diff --git a/examples/factorial.cc b/examples/factorial.cc
new file mode 100644
index 0000000..b5e6097
--- /dev/null
+++ b/examples/factorial.cc
@@ -0,0 +1,77 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateFactorial(MacroAssembler* masm) {
+  // uint64_t factorial(uint64_t n)
+  // Argument location:
+  //   n -> x0
+
+  Label loop, end;
+
+  __ Mov(x1, x0);
+  __ Mov(x0, 1);     // Use x0 as the accumulator.
+
+  __ Cbz(x1, &end);  // Nothing to do if the input is null.
+
+  __ Bind(&loop);
+  __ Mul(x0, x0, x1);
+  __ Sub(x1, x1, 1);
+  __ Cbnz(x1, &loop);
+
+  __ Bind(&end);
+  // The return value is in x0.
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label factorial;
+  masm.Bind(&factorial);
+  GenerateFactorial(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  uint64_t input_val = 16;
+  simulator.set_xreg(0, input_val);
+  simulator.RunFrom(factorial.target());
+  printf("factorial(%ld) = %ld\n", input_val, simulator.xreg(0));
+
+  return 0;
+}
+#endif
diff --git a/examples/getting-started.cc b/examples/getting-started.cc
new file mode 100644
index 0000000..f7dae9e
--- /dev/null
+++ b/examples/getting-started.cc
@@ -0,0 +1,61 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "a64/simulator-a64.h"
+#include "a64/macro-assembler-a64.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+using namespace vixl;
+
+void GenerateDemoFunction(MacroAssembler *masm) {
+  // uint64_t demo_function(uint64_t x)
+  __ Ldr(x1, 0x1122334455667788);
+  __ And(x0, x0, x1);
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main() {
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  Label demo_function;
+  masm.Bind(&demo_function);
+  GenerateDemoFunction(&masm);
+  masm.FinalizeCode();
+
+  simulator.set_xreg(0, 0x8899aabbccddeeff);
+  simulator.RunFrom(demo_function.target());
+  printf("x0 = %" PRIx64 "\n", simulator.xreg(0));
+
+  return 0;
+}
+#endif
diff --git a/examples/sum-array.cc b/examples/sum-array.cc
new file mode 100644
index 0000000..5f23e6a
--- /dev/null
+++ b/examples/sum-array.cc
@@ -0,0 +1,90 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateSumArray(MacroAssembler* masm) {
+  // uint32_t sum_array(uint8_t* array, uint32_t size)
+  //  Argument locations:
+  //    array (pointer) -> x0
+  //    size            -> x1
+
+  Label loop, end;
+
+  __ Mov(x2, x0);
+  __ Mov(w0, 0);
+
+  // There's nothing to do if the array is empty.
+  __ Cbz(w1, &end);
+
+  // Go through the array and sum the elements.
+  __ Bind(&loop);
+
+  __ Ldrb(w3, MemOperand(x2, 1, PostIndex));  // w3 = *(x2++)
+  __ Add(w0, w0, w3);
+
+  __ Sub(w1, w1, 1);
+  __ Cbnz(w1, &loop);
+
+  __ Bind(&end);
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label sum_array;
+  masm.Bind(&sum_array);
+  GenerateSumArray(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  uint8_t data[] = { 2, 45, 63, 7, 245, 38 };
+  uintptr_t data_addr = reinterpret_cast<uintptr_t>(data);
+  simulator.set_xreg(0, data_addr);
+  simulator.set_xreg(1, ARRAY_SIZE(data));
+  simulator.RunFrom(sum_array.target());
+
+  unsigned int i;
+  for (i = 0; i < ARRAY_SIZE(data) - 1; ++i) {
+    printf("%d + ", data[i]);
+  }
+  printf("%d = %d\n", data[i], simulator.wreg(0));
+
+  return 0;
+}
+#endif
diff --git a/examples/swap-int32.cc b/examples/swap-int32.cc
new file mode 100644
index 0000000..1b14c4b
--- /dev/null
+++ b/examples/swap-int32.cc
@@ -0,0 +1,95 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateSwapInt32(MacroAssembler* masm) {
+  {
+    // In this scope the register x2 will be used by the macro-assembler
+    // as the stack pointer (via peek, poke, push, etc).
+    const Register old_stack_pointer = __ StackPointer();
+    __ Mov(x2, __ StackPointer());
+    __ SetStackPointer(x2);
+
+    // This call to Claim is not 16-byte aligned and would have failed
+    // if the current stack pointer was sp.
+    __ Claim(8);
+
+    __ Poke(w0, 0);
+    __ Poke(w1, 4);
+    __ Peek(w1, 0);
+    __ Peek(w0, 4);
+
+    __ Drop(8);
+
+    // Even if we didn't use the system stack pointer, sp might have been
+    // modified because the ABI forbids access to memory below the stack
+    // pointer.
+    __ Mov(old_stack_pointer, __ StackPointer());
+    __ SetStackPointer(old_stack_pointer);
+  }
+
+  // The stack pointer has now been switched back to sp.
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label swap_int32;
+  masm.Bind(&swap_int32);
+  GenerateSwapInt32(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  simulator.set_wreg(0, 0x11111111);
+  simulator.set_wreg(1, 0x22222222);
+
+  printf("Before swap_int32:\n"
+         "x0 = 0x%" PRIx32 "\n"
+         "x1 = 0x%" PRIx32 "\n",
+         simulator.wreg(0), simulator.wreg(1));
+
+  simulator.RunFrom(swap_int32.target());
+
+  printf("After swap_int32:\n"
+         "x0 = 0x%" PRIx32 "\n"
+         "x1 = 0x%" PRIx32 "\n",
+         simulator.wreg(0), simulator.wreg(1));
+
+  return 0;
+}
+#endif
diff --git a/examples/swap4.cc b/examples/swap4.cc
new file mode 100644
index 0000000..746cc3e
--- /dev/null
+++ b/examples/swap4.cc
@@ -0,0 +1,89 @@
+// Copyright 2013, ARM Limited
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//   * Redistributions of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//   * Redistributions in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//   * Neither the name of ARM Limited nor the names of its contributors may be
+//     used to endorse or promote products derived from this software without
+//     specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "examples.h"
+
+#define BUF_SIZE (4096)
+#define __ masm->
+
+void GenerateSwap4(MacroAssembler* masm) {
+  // VIXL's macro assembler provides some functions to manipulate the stack.
+  // This example shows some of these functions.
+  __ Claim(16);
+  __ Poke(x0, 0);
+  __ Poke(x1, 8);
+  __ Push(x3, x2);
+
+  __ Pop(x1, x0);
+  __ Peek(x3, 0);
+  __ Peek(x2, 8);
+  __ Drop(16);
+
+  __ Ret();
+}
+
+
+#ifndef TEST_EXAMPLES
+int main(void) {
+  // Create and initialize the assembler and the simulator.
+  byte assm_buf[BUF_SIZE];
+  MacroAssembler masm(assm_buf, BUF_SIZE);
+  Decoder decoder;
+  Simulator simulator(&decoder);
+
+  // Generate the code for the example function.
+  Label swap4;
+  masm.Bind(&swap4);
+  GenerateSwap4(&masm);
+  masm.FinalizeCode();
+
+  // Run the example function.
+  simulator.set_xreg(0, 0x1111111111111111);
+  simulator.set_xreg(1, 0x2222222222222222);
+  simulator.set_xreg(2, 0x3333333333333333);
+  simulator.set_xreg(3, 0x4444444444444444);
+
+  printf("Before swap4:\n"
+         "x0 = 0x%" PRIx64 "\n"
+         "x1 = 0x%" PRIx64 "\n"
+         "x2 = 0x%" PRIx64 "\n"
+         "x3 = 0x%" PRIx64 "\n",
+         simulator.xreg(0), simulator.xreg(1),
+         simulator.xreg(2), simulator.xreg(3));
+
+  simulator.RunFrom(swap4.target());
+
+  printf("After swap4:\n"
+         "x0 = 0x%" PRIx64 "\n"
+         "x1 = 0x%" PRIx64 "\n"
+         "x2 = 0x%" PRIx64 "\n"
+         "x3 = 0x%" PRIx64 "\n",
+         simulator.xreg(0), simulator.xreg(1),
+         simulator.xreg(2), simulator.xreg(3));
+
+  return 0;
+}
+#endif