├── .github
├── dependabot.yml
└── workflows
│ └── main.yml
├── .gitignore
├── CMakeLists.txt
├── Dockerfile
├── LICENSE
├── README.md
├── docker-compose.yml
├── docs
└── index.html
├── lib
├── CMakeLists.txt
└── Target
│ ├── CBackend
│ ├── CBackend.cpp
│ ├── CBackend.h
│ ├── CMakeLists.txt
│ ├── CTargetMachine.cpp
│ ├── CTargetMachine.h
│ ├── IDMap.h
│ ├── LLVMBuild.txt
│ └── TargetInfo
│ │ ├── CBackendTargetInfo.cpp
│ │ ├── CMakeLists.txt
│ │ └── LLVMBuild.txt
│ └── CMakeLists.txt
├── runtime
└── windows
│ └── atomics.c
├── test
├── c_tests
│ ├── compound_types
│ │ ├── test_array_of_structs.c
│ │ ├── test_get_array_element.c
│ │ ├── test_get_struct_member.c
│ │ ├── test_get_union_member.c
│ │ ├── test_nested_struct.c
│ │ ├── test_self_ref_struct.c
│ │ ├── test_sizeof_packed_struct.c
│ │ ├── test_sizeof_union.c
│ │ ├── test_two_dim_array.c
│ │ ├── test_two_dim_array_pointer.c
│ │ ├── test_typedef_struct.c
│ │ └── test_zero_length_array.c
│ ├── declarations
│ │ ├── test_declare_char.c
│ │ ├── test_declare_double.c
│ │ ├── test_declare_float.c
│ │ ├── test_declare_int.c
│ │ ├── test_declare_long.c
│ │ ├── test_declare_long_double.c
│ │ ├── test_declare_long_int.c
│ │ ├── test_declare_long_long.c
│ │ ├── test_declare_long_long_int.c
│ │ ├── test_declare_short.c
│ │ ├── test_declare_short_int.c
│ │ ├── test_declare_signed_char.c
│ │ ├── test_declare_signed_int.c
│ │ ├── test_declare_signed_long.c
│ │ ├── test_declare_signed_long_int.c
│ │ ├── test_declare_signed_long_long.c
│ │ ├── test_declare_signed_long_long_int.c
│ │ ├── test_declare_signed_short.c
│ │ ├── test_declare_signed_short_int.c
│ │ ├── test_declare_unsigned_char.c
│ │ ├── test_declare_unsigned_int.c
│ │ ├── test_declare_unsigned_long.c
│ │ ├── test_declare_unsigned_long_int.c
│ │ ├── test_declare_unsigned_long_long.c
│ │ ├── test_declare_unsigned_long_long_int.c
│ │ ├── test_declare_unsigned_short.c
│ │ ├── test_declare_unsigned_short_int.c
│ │ └── test_declare_volatile_unsigned_int.c
│ ├── functions
│ │ ├── test_casted_nested_function_ptr_argument.c
│ │ ├── test_fibonacci_function.c
│ │ ├── test_function_ptr.c
│ │ ├── test_function_ptr_argument.c
│ │ ├── test_function_ptr_select.c
│ │ ├── test_head_recursion.c
│ │ ├── test_nested_function_ptr_argument.c
│ │ ├── test_recursive_function.c
│ │ ├── test_simple_function.c
│ │ ├── test_static_function.c
│ │ ├── test_struct_function_argument.c
│ │ ├── test_tail_recursion.c
│ │ └── test_zero_length_array_argument.c
│ ├── operators
│ │ ├── test_add_int_vars.c
│ │ ├── test_bitwise_and.c
│ │ ├── test_bitwise_not.c
│ │ ├── test_bitwise_or.c
│ │ ├── test_bitwise_shl.c
│ │ ├── test_bitwise_shr.c
│ │ ├── test_bitwise_xor.c
│ │ ├── test_compound_add_assign.c
│ │ ├── test_compound_bitwise_and_assign.c
│ │ ├── test_compound_bitwise_or_assign.c
│ │ ├── test_compound_bitwise_xor_assign.c
│ │ ├── test_compound_divide_assign.c
│ │ ├── test_compound_modulus_assign.c
│ │ ├── test_compound_multiply_assign.c
│ │ ├── test_compound_shl_assign.c
│ │ ├── test_compound_shr_assign.c
│ │ ├── test_compound_subtract_assign.c
│ │ ├── test_divide_int_vars.c
│ │ ├── test_equal_operator.c
│ │ ├── test_greater_than.c
│ │ ├── test_greater_than_or_equal.c
│ │ ├── test_less_than.c
│ │ ├── test_less_than_or_equal.c
│ │ ├── test_logical_and.c
│ │ ├── test_logical_or.c
│ │ ├── test_modulus_int_vars.c
│ │ ├── test_multiply_int_vars.c
│ │ ├── test_not_equal_operator.c
│ │ ├── test_postincrement.c
│ │ ├── test_preincrement.c
│ │ ├── test_subtract_int_vars.c
│ │ ├── test_unary_minus_int.c
│ │ └── test_unary_plus_char.c
│ ├── other
│ │ ├── test_constructor.c
│ │ ├── test_macro_substitution.c
│ │ ├── test_mutual_ref_globals.c
│ │ ├── test_null_is_0.c
│ │ ├── test_register_vars.c
│ │ ├── test_return_success.c
│ │ ├── test_static_vars.c
│ │ └── test_test_failure.c
│ ├── pointers
│ │ ├── test_address_of_operator.c
│ │ ├── test_char_sized_ptr_math_decr.c
│ │ ├── test_char_sized_ptr_math_incr.c
│ │ ├── test_deref_array_item.c
│ │ ├── test_deref_char_ptr.c
│ │ ├── test_deref_double_ptr.c
│ │ ├── test_deref_float_ptr.c
│ │ ├── test_deref_int_ptr.c
│ │ ├── test_deref_struct_ptr.c
│ │ ├── test_int_sized_ptr_math_decr.c
│ │ └── test_int_sized_ptr_math_incr.c
│ ├── statements
│ │ ├── test_break_stmt.c
│ │ ├── test_goto_stmt.c
│ │ ├── test_nested_for_loop.c
│ │ ├── test_nested_switch_stmt.c
│ │ ├── test_nested_while_loop.c
│ │ ├── test_simple_do_while.c
│ │ ├── test_simple_for_loop.c
│ │ ├── test_simple_if_else.c
│ │ ├── test_simple_if_else_use_else.c
│ │ ├── test_simple_if_stmt.c
│ │ ├── test_simple_switch_stmt.c
│ │ └── test_simple_while_loop.c
│ └── stdatomic
│ │ ├── test_atomic_compare_exchange_strong.c
│ │ ├── test_atomic_compare_exchange_weak.c
│ │ ├── test_atomic_exchange.c
│ │ ├── test_atomic_fence.c
│ │ ├── test_atomic_fetch_add.c
│ │ ├── test_atomic_fetch_and.c
│ │ ├── test_atomic_fetch_or.c
│ │ ├── test_atomic_fetch_sub.c
│ │ ├── test_atomic_fetch_xor.c
│ │ ├── test_atomic_load.c
│ │ └── test_atomic_store.c
├── cpp_tests
│ ├── test_ctor.cpp
│ ├── test_dtor.cpp
│ ├── test_dtor_called_when_unwinding.cpp
│ ├── test_exception_from_ctor.cpp
│ ├── test_is_constant.cpp
│ ├── test_multiple_exception_handlers.cpp
│ ├── test_my_exception.cpp
│ ├── test_std_exception.cpp
│ └── test_virtual_function_calls.cpp
├── hello_world
│ ├── .gitignore
│ └── main.c
├── ll_tests
│ ├── test_addressable_empty_types.ll
│ ├── test_bitcounts.ll
│ ├── test_bswap.ll
│ ├── test_byval_struct_param.ll
│ ├── test_compile_opaque_struct_with_function.ll
│ ├── test_empty_array_geps.ll
│ ├── test_empty_array_geps_struct.ll
│ ├── test_fun_ptr_dependencies.ll
│ ├── test_fun_ptr_only_refd_by_function.ll
│ ├── test_fun_ptr_only_refd_by_struct.ll
│ ├── test_function_alias.ll
│ ├── test_gep_index_vec.ll
│ ├── test_global_var_order.ll
│ ├── test_if_bool_xor.ll
│ ├── test_intrinsic_sadd_sat.ll
│ ├── test_intrinsic_sadd_with_overflow.ll
│ ├── test_intrinsic_sshl_sat.ll
│ ├── test_intrinsic_uadd_sat.ll
│ ├── test_intrinsic_uadd_with_overflow.ll
│ ├── test_intrinsic_ushl_sat.ll
│ ├── test_min_max_intrinsics.ll
│ ├── test_not_power_2_ints.ll
│ ├── test_simple_return.ll
│ └── test_trap.ll
├── selectionsort
│ └── main.c
└── test_cbe.py
├── tools
├── CMakeLists.txt
└── llvm-cbe
│ ├── CMakeLists.txt
│ ├── LLVMBuild.txt
│ └── llvm-cbe.cpp
└── unittests
├── CMakeLists.txt
└── TypeNameTest.cpp
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
2 | version: 2
3 | updates:
4 | - package-ecosystem: "github-actions"
5 | directory: "/" # Location of package manifests
6 | schedule:
7 | interval: "monthly"
8 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: Build and Test
2 |
3 | on:
4 | workflow_dispatch:
5 | pull_request:
6 | push:
7 | branches:
8 | - "master"
9 |
10 | permissions:
11 | contents: read
12 |
13 | # This allows a subsequently queued workflow run to interrupt previous runs
14 | concurrency:
15 | group: '${{ github.workflow }} @ ${{ github.ref_protected && github.run_id || github.event.pull_request.number || github.ref }}'
16 | cancel-in-progress: true
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | matrix:
22 | include:
23 | - os: ubuntu-latest
24 | - os: macos-latest
25 | runs-on: ${{ matrix.os }}
26 | steps:
27 | - name: Checkout
28 | uses: actions/checkout@v4
29 |
30 | - name: Setup LLVM (Linux)
31 | if: ${{ runner.os == 'Linux' }}
32 | run: |
33 | wget https://apt.llvm.org/llvm.sh
34 | chmod +x llvm.sh
35 | sudo ./llvm.sh 19 all
36 | sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-19 160
37 | sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-19 160
38 | sudo update-alternatives --install /usr/bin/lli lli /usr/bin/lli-19 160
39 | sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-19 160
40 |
41 | - name: Setup LLVM and GCC (MacOS)
42 | if: ${{ runner.os == 'macOS' }}
43 | run: |
44 | brew install llvm@19 gcc@14
45 | echo "$(brew --prefix llvm@19)/bin" >> $GITHUB_PATH
46 | echo "CC=$(brew --prefix llvm@19)/bin/clang" >> $GITHUB_ENV
47 | echo "CXX=$(brew --prefix llvm@19)/bin/clang++" >> $GITHUB_ENV
48 | cd /usr/local/bin
49 | ln -s `which gcc-14` gcc
50 |
51 | - name: Update Python
52 | uses: actions/setup-python@v5
53 | with:
54 | python-version: '3.x'
55 |
56 | - name: Install PyTest
57 | run: |
58 | python -m pip install --upgrade pip
59 | pip install pytest pytest-xdist
60 |
61 | - name: Check formatting
62 | if: ${{ runner.os == 'Linux' }}
63 | run: find . -iname '*.h' -o -iname '*.cpp' | xargs clang-format -Werror --dry-run --style=LLVM --verbose
64 |
65 | - name: Build
66 | run: |
67 | mkdir build
68 | cmake -S . -B build -DLLVM_INCLUDE_TESTS=On -DLLVM_DIR=/usr/lib/llvm-19/cmake
69 | cmake --build build
70 |
71 | - name: Test
72 | run: |
73 | gcc --version
74 | pytest -n 16
75 |
76 | # The llvm-dev package doesn't exist for Windows, so we need to build LLVM ourselves.
77 | build-windows:
78 | runs-on: windows-latest
79 | env:
80 | SCCACHE_GHA_ENABLED: "true"
81 | steps:
82 | - name: Checkout LLVM
83 | id: checkout-llvm
84 | uses: actions/checkout@master
85 | with:
86 | repository: llvm/llvm-project
87 | ref: llvmorg-19.1.1
88 |
89 | - name: Checkout
90 | uses: actions/checkout@v4
91 | with:
92 | path: ${{ github.workspace }}/llvm/projects/llvm-cbe
93 |
94 | - name: Update Python
95 | uses: actions/setup-python@v5
96 | with:
97 | python-version: '3.x'
98 |
99 | - name: Install PyTest
100 | run: |
101 | python -m pip install --upgrade pip
102 | pip install pytest pytest-xdist
103 |
104 | - name: Initialize LLVM build cache
105 | uses: mozilla-actions/sccache-action@v0.0.7
106 |
107 | - name: Build
108 | shell: pwsh
109 | run: |
110 | $vsWhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
111 | $visualStudioInstallationPath = & $vsWhere -latest -property installationPath
112 | Import-Module (Join-Path $visualStudioInstallationPath 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll')
113 | Enter-VsDevShell -VsInstallPath $visualStudioInstallationPath -DevCmdArguments '-arch=x64 -host_arch=x64'
114 | cd ${{ github.workspace }}
115 | if (!(Test-Path '${{ github.workspace }}\build\')) { mkdir '${{ github.workspace }}\build' }
116 | cmake -S llvm -B build -G Ninja -DLLVM_INCLUDE_TESTS=On -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache
117 | cmake --build build
118 |
119 | - name: Test
120 | shell: pwsh
121 | run: |
122 | $vsWhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
123 | $visualStudioInstallationPath = & $vsWhere -latest -property installationPath
124 | Import-Module (Join-Path $visualStudioInstallationPath 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll')
125 | Enter-VsDevShell -VsInstallPath $visualStudioInstallationPath -DevCmdArguments '-arch=x64 -host_arch=x64'
126 | cd (Join-Path '${{ github.workspace }}' 'llvm\projects\llvm-cbe')
127 | $env:PATH=$env:Path + ";${{ github.workspace }}\build\bin"
128 | $env:LLVMToolDir="${{ github.workspace }}\build\bin"
129 | pytest -n 16
130 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled Object files
2 | *.slo
3 | *.lo
4 | *.o
5 | *.obj
6 |
7 | # Compiled Dynamic libraries
8 | *.so
9 | *.dylib
10 | *.dll
11 |
12 | # Compiled Static libraries
13 | *.lai
14 | *.la
15 | *.a
16 | *.lib
17 |
18 | # Executables
19 | *.exe
20 | *.out
21 | *.app
22 |
23 | # Python
24 | __pycache__
25 | .pytest_cache/
26 |
27 | # Build folder
28 | build/
29 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | if (NOT DEFINED LLVM_VERSION_MAJOR)
2 | project(llvm-cbe)
3 | set (USE_SYSTEM_LLVM 1)
4 | cmake_minimum_required(VERSION 3.20.0)
5 | find_package(LLVM 19.1 REQUIRED CONFIG)
6 | message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
7 | message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
8 | message(STATUS "LLVM_INCLUDE_DIRS: ${LLVM_INCLUDE_DIRS}")
9 | include_directories(${LLVM_INCLUDE_DIRS})
10 | add_definitions(${LLVM_DEFINITIONS})
11 |
12 | list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
13 | include(AddLLVM)
14 |
15 | set (CMAKE_CXX_STANDARD 17)
16 | endif()
17 |
18 | add_subdirectory(lib)
19 | add_subdirectory(tools)
20 |
21 | if( LLVM_INCLUDE_TESTS )
22 | add_subdirectory(unittests)
23 | endif()
24 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM buildpack-deps:stretch
2 |
3 | RUN \
4 | set -e; \
5 | apt update && apt install -y \
6 | clang \
7 | ninja-build \
8 | cmake; \
9 | cd /root; \
10 | curl -fL http://releases.llvm.org/8.0.0/llvm-8.0.0.src.tar.xz \
11 | | tar xJf -; \
12 | mv /root/llvm-8.0.0.src /root/llvm; \
13 | mkdir -p /root/llvm/build;
14 |
15 | COPY . /root/llvm/projects/llvm-cbe/
16 |
17 | RUN \
18 | set -e; \
19 | mkdir -p /root/llvm/build; \
20 | cd /root/llvm/build; \
21 | cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..; \
22 | ninja llvm-cbe; \
23 | ninja lli; \
24 | ninja CBEUnitTests; \
25 | /root/llvm/build/projects/llvm-cbe/unittests/CWriterTest; \
26 | ln -s /root/llvm/build/bin/llvm-cbe /bin/llvm-cbe;
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | All changes to LLVM-CBE contributed by Julia Computing are made available under
2 | the original University of Illinois/NCSA Open Source License.
3 | Copyright(c) 2015-2021 Julia Computing and contributors.
4 |
5 | ==============================================================================
6 | LLVM Release License
7 | ==============================================================================
8 | University of Illinois/NCSA
9 | Open Source License
10 | Copyright (c) 2003-2014 University of Illinois at Urbana-Champaign.
11 | All rights reserved.
12 | Developed by:
13 | LLVM Team
14 | University of Illinois at Urbana-Champaign
15 | http://llvm.org
16 | Permission is hereby granted, free of charge, to any person obtaining a copy of
17 | this software and associated documentation files (the "Software"), to deal with
18 | the Software without restriction, including without limitation the rights to
19 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
20 | of the Software, and to permit persons to whom the Software is furnished to do
21 | so, subject to the following conditions:
22 | * Redistributions of source code must retain the above copyright notice,
23 | this list of conditions and the following disclaimers.
24 | * Redistributions in binary form must reproduce the above copyright notice,
25 | this list of conditions and the following disclaimers in the
26 | documentation and/or other materials provided with the distribution.
27 | * Neither the names of the LLVM Team, University of Illinois at
28 | Urbana-Champaign, nor the names of its contributors may be used to
29 | endorse or promote products derived from this Software without specific
30 | prior written permission.
31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
33 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
37 | SOFTWARE.
38 | ==============================================================================
39 | Copyrights and Licenses for Third Party Software Distributed with LLVM:
40 | ==============================================================================
41 | The LLVM software contains code written by third parties. Such software will
42 | have its own individual LICENSE.TXT file in the directory in which it appears.
43 | This file will describe the copyrights, license, and restrictions which apply
44 | to that code.
45 | The disclaimer of warranty in the University of Illinois Open Source License
46 | applies to all code in the LLVM Distribution, and nothing in any of the
47 | other licenses gives permission to use the names of the LLVM Team or the
48 | University of Illinois to endorse or promote products derived from this
49 | Software.
50 | The following pieces of software have additional or alternate copyrights,
51 | licenses, and/or restrictions:
52 | Program Directory
53 | ------- ---------
54 | Autoconf llvm/autoconf
55 | llvm/projects/ModuleMaker/autoconf
56 | llvm/projects/sample/autoconf
57 | Google Test llvm/utils/unittest/googletest
58 | OpenBSD regex llvm/lib/Support/{reg*, COPYRIGHT.regex}
59 | pyyaml tests llvm/test/YAMLParser/{*.data, LICENSE.TXT}
60 | ARM contributions llvm/lib/Target/ARM/LICENSE.TXT
61 | md5 contributions llvm/lib/Support/MD5.cpp llvm/include/llvm/Support/MD5.h
62 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | llvm-cbe
2 | ========
3 |
4 | This LLVM C backend has been resurrected by Julia Computing with various improvements.
5 |
6 | Installation instructions
7 | =========================
8 |
9 | This version of the LLVM C backend works with LLVM 19, for older versions please check the [tags](https://github.com/JuliaHubOSS/llvm-cbe/tags).
10 |
11 | Step 1: Installing LLVM
12 | =======================
13 |
14 | Either install the LLVM packages on your system:
15 | --------------------------------------------
16 |
17 | On macOS, use [pkgsrc](http://pkgsrc.joyent.com/install-on-osx/) and run the following commands:
18 | ```sh
19 | ~$ pkgin in llvm clang ninja-build
20 | ```
21 |
22 | On CentOS, install the llvm-devel package:
23 | ```sh
24 | ~# dnf install llvm-devel clang ninja-build
25 | ```
26 |
27 | On Debian and derivatives, install the llvm-dev package via:
28 | ```sh
29 | ~# apt install llvm-dev clang ninja-build
30 | ```
31 |
32 | Note: this project uses LLVM 19, so make sure that the package manager is installing it and not some other version. At the time of writing, Ubuntu installs version 14.
33 |
34 | Or compile LLVM yourself:
35 | -----------------------------
36 | Note: to convert C to LLVM IR to run the tests, you will also need a C compiler using the LLVM infrastructure, such as clang.
37 |
38 | The first step is to compile LLVM on your machine
39 | (this assumes an in-tree build, but out-of-tree will also work):
40 |
41 | ```sh
42 | ~$ git clone https://github.com/llvm/llvm-project.git
43 | ~$ cd llvm-project
44 | llvm-project$ git checkout tags/19.1.1
45 | llvm-project$ mkdir llvm/build
46 | llvm-project$ cd llvm/build
47 | build$ cmake .. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DLLVM_PARALLEL_LINK_JOBS=1
48 | build$ ninja
49 | ```
50 | To run tests, you need to build `lli`.
51 |
52 |
53 | Step 2: Compiling LLVM-CBE
54 | ==========================
55 |
56 | Now you can download and compile llvm-cbe.
57 |
58 | If you built LLVM yourself, put it in the same folder you built LLVM in:
59 | ```sh
60 | ~$ cd llvm-project/llvm/projects
61 | projects$ git clone https://github.com/JuliaHubOSS/llvm-cbe
62 | projects$ cd ../build
63 | build$ cmake -S .. -G "Ninja"
64 | build$ ninja llvm-cbe
65 | ```
66 | If you used your distribution's package, put it wherever you feel like:
67 | ```sh
68 | ~$ git clone https://github.com/JuliaHubOSS/llvm-cbe
69 | ~$ cd llvm-cbe && mkdir build && cd build
70 | build$ cmake -S .. -G "Ninja"
71 | build$ ninja llvm-cbe
72 | ```
73 | Step 3: Usage Examples
74 | ======================
75 |
76 | If llvm-cbe compiles, you should be able to run it with the following commands.
77 | ```sh
78 | llvm-cbe$ cd test/selectionsort
79 | selectionsort$ ls
80 | main.c
81 | selectionsort$ clang-19 -S -emit-llvm -g main.c
82 | selectionsort$ ls
83 | main.c main.ll
84 | selectionsort$ ../../build/tools/llvm-cbe/llvm-cbe main.ll
85 | ```
86 |
87 | You can find options to configure the C backend's output with `llvm-cbe --help`.
88 | Look for options beginning with `--cbe-`.
89 |
90 | Compile Generated C Code and Run
91 | ================================
92 |
93 | ```sh
94 | selectionsort$ gcc -o main.cbe main.cbe.c
95 | selectionsort$ ls
96 | main.c main.cbe main.cbe.c main.ll
97 | selectionsort$ ./main.cbe
98 | ```
99 |
100 | Running tests
101 | =============
102 |
103 | Unit tests:
104 |
105 | ```sh
106 | llvm-project$ cd llvm/build
107 | build$ make CBEUnitTests && projects/llvm-cbe/unittests/CWriterTest
108 | ```
109 |
110 | Note that you need to have passed `-DLLVM_INCLUDE_TESTS=1` to cmake if you used
111 | your distribution's LLVM package. You also will need to install gtest (on Debian
112 | derivatives: `apt install libgtest-dev`).
113 |
114 | Other tests:
115 |
116 | First, compile llvm-cbe, and install pytest (e.g. `pip install pytest`). Then:
117 |
118 | ```sh
119 | llvm-cbe$ pytest
120 | ```
121 |
122 | You might have to adjust the llvm-cbe and lli paths in that configuration.
123 |
124 | If you want the tests to run faster, installing `pytest-xdist` will allow you to run the test suite in parallel, e.g. `pytest -n 4` if you want to use 4 cores.
125 |
126 | FAQ: Version Update Policy
127 | ==========================
128 |
129 | The policy of this project is to only support latest released version of LLVM on the head of repo here.
130 | The rationale is that supporting multiple versions is additional work for a project which has no sponsors.
131 | Additionally, since there are very few changes, other than version compatibility maintenance,
132 | anyone using an older version of LLVM can just checkout the last commit to work on that branch.
133 | For discussion, see https://github.com/JuliaHubOSS/llvm-cbe/issues/60.
134 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3"
2 |
3 | services:
4 | llvm-cbe:
5 | build: .
6 | working_dir: /root
7 | volumes:
8 | - ./test:/test
9 | command: |
10 | bash -c "
11 | set -e;
12 |
13 | # Navigate to test folder
14 | cd /test/hello_world;
15 |
16 | # Compile main.c to llvm IR
17 | clang -S -emit-llvm main.c;
18 |
19 | # Compile llvm IR back to C
20 | llvm-cbe main.ll;
21 |
22 | # Compile llvm-cbe generated C code
23 | gcc -g -o main.cbe main.cbe.c;
24 |
25 | # Run it
26 | ./main.cbe;
27 | "
28 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | SAMPLE PROJECT DOCUMENTATION
4 | This is just a placeholder
5 |
6 |
7 |
--------------------------------------------------------------------------------
/lib/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | add_subdirectory(Target)
2 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | add_llvm_component_group(CBackend)
2 |
3 | add_subdirectory(TargetInfo)
4 |
5 | add_llvm_target(CBackendCodeGen
6 | CBackend.cpp
7 | CTargetMachine.cpp
8 |
9 | LINK_COMPONENTS
10 | Analysis
11 | CBackendInfo
12 | CodeGen
13 | Core
14 | MC
15 | ScalarOpts
16 | Support
17 | Target
18 | SelectionDAG
19 | TransformUtils
20 |
21 | DEPENDS
22 | intrinsics_gen
23 |
24 | ADD_TO_COMPONENT
25 | CBackend
26 | )
27 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/CTargetMachine.cpp:
--------------------------------------------------------------------------------
1 | //===-- CTargetMachine.cpp - TargetMachine for the C backend ----*- C++ -*-===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This file implements the TargetMachine that is used by the C backend.
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | #include "CTargetMachine.h"
15 | #include "CBackend.h"
16 | #include "llvm/CodeGen/TargetPassConfig.h"
17 | #include "llvm/Transforms/Utils.h"
18 |
19 | namespace llvm {
20 |
21 | bool CTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
22 | raw_pwrite_stream &Out,
23 | raw_pwrite_stream *DwoOut,
24 | CodeGenFileType FileType,
25 | bool DisableVerify,
26 | MachineModuleInfoWrapperPass *MMI) {
27 |
28 | if (FileType != CodeGenFileType::AssemblyFile)
29 | return true;
30 |
31 | PM.add(new TargetPassConfig(*this, PM));
32 | PM.add(createGCLoweringPass());
33 |
34 | // Remove exception handling with LowerInvokePass. This would be done with
35 | // TargetPassConfig if TargetPassConfig supported TargetMachines that aren't
36 | // LLVMTargetMachines.
37 | PM.add(createLowerInvokePass());
38 | PM.add(createUnreachableBlockEliminationPass());
39 |
40 | // Lower atomic operations to libcalls
41 | PM.add(createAtomicExpandLegacyPass());
42 |
43 | // Lower vector operations into shuffle sequences
44 | PM.add(createExpandReductionsPass());
45 |
46 | PM.add(new llvm_cbe::CWriter(Out));
47 | return false;
48 | }
49 |
50 | const TargetSubtargetInfo *
51 | CTargetMachine::getSubtargetImpl(const Function &) const {
52 | return &SubtargetInfo;
53 | }
54 |
55 | bool CTargetSubtargetInfo::enableAtomicExpand() const { return true; }
56 |
57 | const TargetLowering *CTargetSubtargetInfo::getTargetLowering() const {
58 | return &Lowering;
59 | }
60 |
61 | } // namespace llvm
62 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/CTargetMachine.h:
--------------------------------------------------------------------------------
1 | //===-- CTargetMachine.h - TargetMachine for the C backend ------*- C++ -*-===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This file declares the TargetMachine that is used by the C backend.
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | #ifndef CTARGETMACHINE_H
15 | #define CTARGETMACHINE_H
16 |
17 | #include "llvm/CodeGen/TargetLowering.h"
18 | #include "llvm/CodeGen/TargetSubtargetInfo.h"
19 | #include "llvm/IR/DataLayout.h"
20 | #include "llvm/Target/TargetMachine.h"
21 |
22 | namespace llvm {
23 |
24 | class CTargetLowering : public TargetLowering {
25 | public:
26 | explicit CTargetLowering(const TargetMachine &TM) : TargetLowering(TM) {
27 | setMaxAtomicSizeInBitsSupported(0);
28 | }
29 | };
30 |
31 | class CTargetSubtargetInfo : public TargetSubtargetInfo {
32 | public:
33 | CTargetSubtargetInfo(const TargetMachine &TM, const Triple &TT, StringRef CPU,
34 | StringRef TuneCPU, StringRef FS)
35 | : TargetSubtargetInfo(TT, CPU, TuneCPU, FS,
36 | ArrayRef(),
37 | ArrayRef(), nullptr, nullptr,
38 | nullptr, nullptr, nullptr, nullptr),
39 | Lowering(TM) {}
40 | bool enableAtomicExpand() const override;
41 | const TargetLowering *getTargetLowering() const override;
42 | const CTargetLowering Lowering;
43 | };
44 |
45 | class CTargetMachine : public LLVMTargetMachine {
46 | public:
47 | CTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
48 | const TargetOptions &Options, std::optional RM,
49 | std::optional CM, CodeGenOptLevel OL,
50 | bool /*JIT*/)
51 | : LLVMTargetMachine(T, "", TT, CPU, FS, Options,
52 | RM.value_or(Reloc::Static),
53 | CM.value_or(CodeModel::Small), OL),
54 | SubtargetInfo(*this, TT, CPU, "", FS) {}
55 |
56 | /// Add passes to the specified pass manager to get the specified file
57 | /// emitted. Typically this will involve several steps of code generation.
58 | bool
59 | addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
60 | raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
61 | bool DisableVerify = true,
62 | MachineModuleInfoWrapperPass *MMI = nullptr) override;
63 |
64 | // TargetMachine interface
65 | const TargetSubtargetInfo *getSubtargetImpl(const Function &) const override;
66 | const CTargetSubtargetInfo SubtargetInfo;
67 | };
68 |
69 | extern Target TheCBackendTarget;
70 |
71 | } // namespace llvm
72 |
73 | #endif
74 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/IDMap.h:
--------------------------------------------------------------------------------
1 | #include "llvm/ADT/DenseMap.h"
2 |
3 | namespace llvm_cbe {
4 | using namespace llvm;
5 |
6 | template class IDMap {
7 | DenseMap Map;
8 | unsigned NextValue = 0;
9 |
10 | public:
11 | IDMap() {}
12 |
13 | unsigned has(KeyT key) { return Map.count(key) > 0; }
14 |
15 | unsigned getOrInsert(KeyT Key) {
16 | unsigned &i = Map[Key];
17 | if (i == 0) {
18 | i = ++NextValue;
19 | }
20 |
21 | return i;
22 | }
23 |
24 | void clear() {
25 | Map.clear();
26 | NextValue = 0;
27 | }
28 |
29 | typename DenseMap::iterator begin() { return Map.begin(); }
30 |
31 | typename DenseMap::iterator end() { return Map.end(); }
32 |
33 | typename DenseMap::iterator find(KeyT Key) {
34 | return Map.find(Key);
35 | }
36 | };
37 |
38 | } // namespace llvm_cbe
39 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/LLVMBuild.txt:
--------------------------------------------------------------------------------
1 | ;===- ./lib/Target/CBackend/LLVMBuild.txt ----------------------*- Conf -*--===;
2 | ;
3 | ; The LLVM Compiler Infrastructure
4 | ;
5 | ; This file is distributed under the University of Illinois Open Source
6 | ; License. See LICENSE.TXT for details.
7 | ;
8 | ;===------------------------------------------------------------------------===;
9 | ;
10 | ; This is an LLVMBuild description file for the components in this subdirectory.
11 | ;
12 | ; For more information on the LLVMBuild system, please see:
13 | ;
14 | ; http://llvm.org/docs/LLVMBuild.html
15 | ;
16 | ;===------------------------------------------------------------------------===;
17 |
18 | [common]
19 | subdirectories = TargetInfo
20 |
21 | [component_0]
22 | type = TargetGroup
23 | name = CBackend
24 | parent = Target
25 |
26 | [component_1]
27 | type = Library
28 | name = CBackendCodeGen
29 | parent = CBackend
30 | required_libraries = Analysis CBackendInfo CodeGen Core MC ScalarOpts Support Target TransformUtils
31 | add_to_library_groups = CBackend
32 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/TargetInfo/CBackendTargetInfo.cpp:
--------------------------------------------------------------------------------
1 | //===-- CBackendTargetInfo.cpp - CBackend Target Implementation -----------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 |
10 | #include "../CTargetMachine.h"
11 | #include "llvm/IR/Module.h"
12 | #include "llvm/MC/TargetRegistry.h"
13 |
14 | using namespace llvm;
15 |
16 | Target llvm::TheCBackendTarget;
17 |
18 | extern "C" void LLVMInitializeCBackendTargetInfo() {
19 | RegisterTarget<> X(TheCBackendTarget, "c", "C backend", "C");
20 | }
21 |
22 | extern "C" void LLVMInitializeCBackendTargetMC() {}
23 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/TargetInfo/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | include_directories( ${CMAKE_CURRENT_BINARY_DIR}/.. ${CMAKE_CURRENT_SOURCE_DIR}/.. )
2 |
3 | add_llvm_component_library(LLVMCBackendInfo
4 | CBackendTargetInfo.cpp
5 |
6 | LINK_COMPONENTS
7 | MC
8 | ScalarOpts
9 | Support
10 | Target
11 |
12 | ADD_TO_COMPONENT
13 | CBackend
14 | )
15 |
--------------------------------------------------------------------------------
/lib/Target/CBackend/TargetInfo/LLVMBuild.txt:
--------------------------------------------------------------------------------
1 | ;===- ./lib/Target/CBackend/TargetInfo/LLVMBuild.txt -----------*- Conf -*--===;
2 | ;
3 | ; The LLVM Compiler Infrastructure
4 | ;
5 | ; This file is distributed under the University of Illinois Open Source
6 | ; License. See LICENSE.TXT for details.
7 | ;
8 | ;===------------------------------------------------------------------------===;
9 | ;
10 | ; This is an LLVMBuild description file for the components in this subdirectory.
11 | ;
12 | ; For more information on the LLVMBuild system, please see:
13 | ;
14 | ; http://llvm.org/docs/LLVMBuild.html
15 | ;
16 | ;===------------------------------------------------------------------------===;
17 |
18 | [component_0]
19 | type = Library
20 | name = CBackendInfo
21 | parent = CBackend
22 | required_libraries = MC ScalarOpts Support Target
23 | add_to_library_groups = CBackend
24 |
--------------------------------------------------------------------------------
/lib/Target/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | add_subdirectory(CBackend)
2 |
--------------------------------------------------------------------------------
/runtime/windows/atomics.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #pragma warning(disable:4100) // Unused formal parameters.
6 |
7 | // Workaround for MSVC bug
8 | // https://developercommunity.visualstudio.com/t/Using-atomic_load_explicit-with-an-unsig/10414053?q=atomic_load_explicit+c4244
9 | #pragma warning(push)
10 | #pragma warning(disable:4244)
11 | uint8_t __atomic_load_1(uint8_t *src, int model) {
12 | return atomic_load_explicit((atomic_uchar *)src, model);
13 | }
14 |
15 | uint16_t __atomic_load_2(uint16_t *src, int model) {
16 | return atomic_load_explicit((atomic_ushort *)src, model);
17 | }
18 | #pragma warning(pop)
19 |
20 | uint32_t __atomic_load_4(uint32_t *src, int model) {
21 | return atomic_load_explicit((atomic_uint *)src, model);
22 | }
23 |
24 | uint64_t __atomic_load_8(uint64_t *src, int model) {
25 | return atomic_load_explicit((atomic_ullong *)src, model);
26 | }
27 |
28 | void __atomic_store_1(uint8_t *dest, uint8_t val, int model) {
29 | atomic_store_explicit((atomic_uchar *)dest, val, model);
30 | }
31 |
32 | void __atomic_store_2(uint16_t *dest, uint16_t val, int model) {
33 | atomic_store_explicit((atomic_ushort *)dest, val, model);
34 | }
35 |
36 | void __atomic_store_4(uint32_t *dest, uint32_t val, int model) {
37 | atomic_store_explicit((atomic_uint *)dest, val, model);
38 | }
39 |
40 | void __atomic_store_8(uint64_t *dest, uint64_t val, int model) {
41 | atomic_store_explicit((atomic_ullong *)dest, val, model);
42 | }
43 |
44 | uint8_t __atomic_exchange_1(uint8_t *dest, uint8_t val, int model) {
45 | return atomic_exchange_explicit((atomic_uchar *)dest, val, model);
46 | }
47 |
48 | uint16_t __atomic_exchange_2(uint16_t *dest, uint16_t val, int model) {
49 | return atomic_exchange_explicit((atomic_ushort *)dest, val, model);
50 | }
51 |
52 | uint32_t __atomic_exchange_4(uint32_t *dest, uint32_t val, int model) {
53 | return atomic_exchange_explicit((atomic_uint *)dest, val, model);
54 | }
55 |
56 | uint64_t __atomic_exchange_8(uint64_t *dest, uint64_t val, int model) {
57 | return atomic_exchange_explicit((atomic_ullong *)dest, val, model);
58 | }
59 |
60 | bool __atomic_compare_exchange_1(uint8_t *ptr, uint8_t *expected,
61 | uint8_t desired, int success, int failure) {
62 | return atomic_compare_exchange_strong_explicit((atomic_uchar *)ptr, expected, desired, success, failure);
63 | }
64 |
65 | bool __atomic_compare_exchange_2(uint16_t *ptr, uint16_t *expected,
66 | uint16_t desired, int success, int failure) {
67 | return atomic_compare_exchange_strong_explicit((atomic_ushort *)ptr, expected, desired, success, failure);
68 | }
69 |
70 | bool __atomic_compare_exchange_4(uint32_t *ptr, uint32_t *expected,
71 | uint32_t desired, int success, int failure) {
72 | return atomic_compare_exchange_strong_explicit((atomic_uint *)ptr, expected, desired, success, failure);
73 | }
74 |
75 | bool __atomic_compare_exchange_8(uint64_t *ptr, uint64_t *expected,
76 | uint64_t desired, int success, int failure) {
77 | return atomic_compare_exchange_strong_explicit((atomic_ullong *)ptr, expected, desired, success, failure);
78 | }
79 |
80 | uint8_t __atomic_fetch_add_1(uint8_t *ptr, uint8_t val, int model) {
81 | return atomic_fetch_add_explicit((atomic_uchar *)ptr, val, model);
82 | }
83 |
84 | uint16_t __atomic_fetch_add_2(uint16_t *ptr, uint16_t val, int model) {
85 | return atomic_fetch_add_explicit((atomic_ushort *)ptr, val, model);
86 | }
87 |
88 | uint32_t __atomic_fetch_add_4(uint32_t *ptr, uint32_t val, int model) {
89 | return atomic_fetch_add_explicit((atomic_uint *)ptr, val, model);
90 | }
91 |
92 | uint64_t __atomic_fetch_add_8(uint64_t *ptr, uint64_t val, int model) {
93 | return atomic_fetch_add_explicit((atomic_ullong *)ptr, val, model);
94 | }
95 |
96 | uint8_t __atomic_fetch_sub_1(uint8_t *ptr, uint8_t val, int model) {
97 | return atomic_fetch_sub_explicit((atomic_uchar *)ptr, val, model);
98 | }
99 |
100 | uint16_t __atomic_fetch_sub_2(uint16_t *ptr, uint16_t val, int model) {
101 | return atomic_fetch_sub_explicit((atomic_ushort *)ptr, val, model);
102 | }
103 |
104 | uint32_t __atomic_fetch_sub_4(uint32_t *ptr, uint32_t val, int model) {
105 | return atomic_fetch_sub_explicit((atomic_uint *)ptr, val, model);
106 | }
107 |
108 | uint64_t __atomic_fetch_sub_8(uint64_t *ptr, uint64_t val, int model) {
109 | return atomic_fetch_sub_explicit((atomic_ullong *)ptr, val, model);
110 | }
111 |
112 | uint8_t __atomic_fetch_and_1(uint8_t *ptr, uint8_t val, int model) {
113 | return atomic_fetch_and_explicit((atomic_uchar *)ptr, val, model);
114 | }
115 |
116 | uint16_t __atomic_fetch_and_2(uint16_t *ptr, uint16_t val, int model) {
117 | return atomic_fetch_and_explicit((atomic_ushort *)ptr, val, model);
118 | }
119 |
120 | uint32_t __atomic_fetch_and_4(uint32_t *ptr, uint32_t val, int model) {
121 | return atomic_fetch_and_explicit((atomic_uint *)ptr, val, model);
122 | }
123 |
124 | uint64_t __atomic_fetch_and_8(uint64_t *ptr, uint64_t val, int model) {
125 | return atomic_fetch_and_explicit((atomic_ullong *)ptr, val, model);
126 | }
127 |
128 | uint8_t __atomic_fetch_or_1(uint8_t *ptr, uint8_t val, int model) {
129 | return atomic_fetch_or_explicit((atomic_uchar *)ptr, val, model);
130 | }
131 |
132 | uint16_t __atomic_fetch_or_2(uint16_t *ptr, uint16_t val, int model) {
133 | return atomic_fetch_or_explicit((atomic_ushort *)ptr, val, model);
134 | }
135 |
136 | uint32_t __atomic_fetch_or_4(uint32_t *ptr, uint32_t val, int model) {
137 | return atomic_fetch_or_explicit((atomic_uint *)ptr, val, model);
138 | }
139 |
140 | uint64_t __atomic_fetch_or_8(uint64_t *ptr, uint64_t val, int model) {
141 | return atomic_fetch_or_explicit((atomic_ullong *)ptr, val, model);
142 | }
143 |
144 | uint8_t __atomic_fetch_xor_1(uint8_t *ptr, uint8_t val, int model) {
145 | return atomic_fetch_xor_explicit((atomic_uchar *)ptr, val, model);
146 | }
147 |
148 | uint16_t __atomic_fetch_xor_2(uint16_t *ptr, uint16_t val, int model) {
149 | return atomic_fetch_xor_explicit((atomic_ushort *)ptr, val, model);
150 | }
151 |
152 | uint32_t __atomic_fetch_xor_4(uint32_t *ptr, uint32_t val, int model) {
153 | return atomic_fetch_xor_explicit((atomic_uint *)ptr, val, model);
154 | }
155 |
156 | uint64_t __atomic_fetch_xor_8(uint64_t *ptr, uint64_t val, int model) {
157 | return atomic_fetch_xor_explicit((atomic_ullong *)ptr, val, model);
158 | }
159 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_array_of_structs.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute an array of structures.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | struct Shows {
16 | char show[20];
17 | int runlength;
18 | int rating;
19 | };
20 |
21 | int main() {
22 | struct Shows b1[3] = {
23 | {"Big Bang Theory", 22, 6},
24 | {"NCIS", 45, 9},
25 | };
26 | return b1[0].rating;
27 | }
28 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_get_array_element.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute an array correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int example[10];
17 | int i;
18 | for (i = 0; i < 10; ++i) {
19 | example[i] = i;
20 | }
21 | return example[6];
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_get_struct_member.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a struct correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | struct test {
16 | int var1;
17 | int var2;
18 | int var3;
19 | };
20 |
21 | int main() {
22 | struct test variable;
23 |
24 | variable.var2 = 5;
25 | variable.var3 = 6;
26 | variable.var1 = 9;
27 |
28 | return variable.var3;
29 | }
30 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_get_union_member.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will access and return union members
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | union Data {
16 | char unit1[6];
17 | char unit2;
18 | char unit3;
19 | };
20 |
21 | int main() {
22 | union Data data;
23 | return sizeof(data);
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_nested_struct.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a nested structure properly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | struct layer1 {
16 | int depth1;
17 | char name1[20];
18 | };
19 |
20 | struct layer2 {
21 | int depth2;
22 | char name2[20];
23 | struct layer1 layer_data;
24 | } layer2_data;
25 |
26 | int main() {
27 | struct layer2 layer2_data = {1, "test", {6, "test2"}};
28 |
29 | return layer2_data.layer_data.depth1;
30 | }
31 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_self_ref_struct.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a self referencing
11 | // structure. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 | #include //for NULL
15 |
16 | struct data {
17 | int a;
18 | struct data *ptr;
19 | };
20 |
21 | int main() {
22 | struct data p =
23 | (struct data){.a = 3, .ptr = &(struct data){.a = 6, .ptr = NULL}};
24 | return p.ptr->a;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_sizeof_packed_struct.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute data-packing in a structure
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | #pragma pack(push)
16 | #pragma pack(1)
17 |
18 | struct DataSize {
19 | char Data2;
20 | char Data3;
21 | int Data1;
22 | };
23 |
24 | int main() {
25 | struct DataSize example;
26 | return sizeof(example);
27 | }
28 |
29 | #pragma pack(pop)
30 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_sizeof_union.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a union and check the data
11 | // size correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | union Data {
16 | int i;
17 | float f;
18 | char str[8];
19 | };
20 |
21 | int main() {
22 | union Data data;
23 | int datasize = sizeof(data) - 2;
24 |
25 | return datasize;
26 | }
27 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_two_dim_array.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly declare a multi-dimension.
11 | // This example works by using a pointer to access address[1][1] to return 6.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int a[2][2];
18 | int *ip;
19 | a[0][0] = 0;
20 | a[0][1] = 1;
21 | a[1][0] = 3;
22 | a[1][1] = 6;
23 | ip = &a[1][1];
24 |
25 | return *ip;
26 | }
27 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_two_dim_array_pointer.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests that the CBE correctly handles pointers to two-dimensional
11 | // arrays. Specifically, it is a test for GEPs where the first index is not zero
12 | // and the element type is an array.
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int sumArray(int array[2][2]) {
17 | return array[0][0] + array[0][1] + array[1][0] + array[1][1];
18 | }
19 |
20 | int main(void) {
21 | int sumsToSix[2][2] = {{1, -2}, {3, 4}};
22 | return sumArray(sumsToSix);
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_typedef_struct.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a structure using typedef.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | typedef struct test {
16 | int var1;
17 | int var2;
18 | int var3;
19 | } testrename;
20 |
21 | int main() {
22 | testrename variable;
23 |
24 | variable.var2 = 5;
25 | variable.var3 = 6;
26 | variable.var1 = 9;
27 |
28 | return variable.var3;
29 | }
30 |
--------------------------------------------------------------------------------
/test/c_tests/compound_types/test_zero_length_array.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will handle zero-length arrays properly.
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | // xfail: zero length arrays don't work
15 |
16 | int main() {
17 | union {
18 | char zl_chars[0];
19 | struct {
20 | char first_char;
21 | char second_char;
22 | };
23 | } x;
24 |
25 | x.first_char = 2;
26 | x.second_char = 4;
27 | return x.zl_chars[0] + x.zl_chars[1];
28 | }
29 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_char.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // char. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | char a = 'A'; // 65
17 | int ia = 0;
18 |
19 | ia = a;
20 | ia -= 59;
21 |
22 | return ia;
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_double.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // double. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | double a = 6.0;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_float.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // float. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | float a = 6.0;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning an
11 | // int. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int a = 6;
17 | return a;
18 | }
19 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_long.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // long. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | long a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_long_double.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a long
11 | // double. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | long double a = 6.0;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_long_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle
11 | // declaring and returning a long int.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | long int a = 6;
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_long_long.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a long
11 | // long. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | long long a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_long_long_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a long
11 | // long int. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | long long int a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_short.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // short. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | short a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_short_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle
11 | // declaring and returning a short int.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | short int a = 6;
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_char.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring
11 | // and returning a signed char.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | signed char a = 'A';
18 | int ia = 0;
19 |
20 | ia = a;
21 | ia -= 59;
22 |
23 | return ia;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle
11 | // declaring and returning a signed int.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | signed int a = 6;
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_long.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // signed long. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | signed long a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_long_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // signed long int. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | signed long int a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_long_long.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // signed long long. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | signed long long a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_long_long_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring
11 | // and returning a signed long long int.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | signed long long int a = 6;
18 |
19 | int ia = 0;
20 | ia = (int)a;
21 |
22 | return ia;
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_short.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // signed short. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | signed short a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_signed_short_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // signed short int. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | signed short int a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_char.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring
11 | // and returning an unsigned char.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned char a = 'A';
18 | int ia = 0;
19 |
20 | ia = a;
21 | ia -= 59;
22 |
23 | return ia;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle
11 | // declaring and returning a unsigned int.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 6;
18 |
19 | int ia = 0;
20 | ia = (int)a;
21 |
22 | return ia;
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_long.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // unsigned long. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | unsigned long a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_long_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and
11 | // returning an unsigned long int.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned long int a = 6;
18 |
19 | int ia = 0;
20 | ia = (int)a;
21 |
22 | return ia;
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_long_long.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // unsigned long long. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | unsigned long long a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_long_long_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning an
11 | // unsigned long long int. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | unsigned long long int a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_short.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning an
11 | // unsigned short. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | unsigned short a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_unsigned_short_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle declaring and returning a
11 | // unsigned short int. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | unsigned short int a = 6;
17 |
18 | int ia = 0;
19 | ia = (int)a;
20 |
21 | return ia;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/declarations/test_declare_volatile_unsigned_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle volatile integers
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | int main() {
15 | volatile unsigned int a = 6;
16 | return a;
17 | }
18 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_casted_nested_function_ptr_argument.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Tests if the CBE can handle casting void* to and from a function pointer with
11 | // a second function pointer as one of its arguments.
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | static int return_arg(int x) { return x; }
16 |
17 | static int call1(void *f, int x) { return ((int (*)(int))f)(x); }
18 |
19 | static int call2(void *f2, void *f, int x) {
20 | return ((int (*)(int (*)(int), int))f2)(f, x);
21 | }
22 |
23 | int main() { return call2(call1, return_arg, 6); }
24 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_fibonacci_function.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Tests if the CBE will execute a fibonacci numbers example.
11 | // Credit: http://en.literateprograms.org/Fibonacci_numbers_(C)
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | unsigned int fastfib(unsigned int n);
17 |
18 | int main() { return fastfib(6) - 2; }
19 |
20 | unsigned int fastfib(unsigned int n) {
21 | unsigned int a[3];
22 | unsigned int *p = a;
23 | unsigned int i;
24 |
25 | for (i = 0; i <= n; ++i) {
26 | if (i < 2)
27 | *p = i;
28 | else {
29 | if (p == a)
30 | *p = *(a + 1) + *(a + 2);
31 | else if (p == a + 1)
32 | *p = *a + *(a + 2);
33 | else
34 | *p = *a + *(a + 1);
35 | }
36 | if (++p > a + 2)
37 | p = a;
38 | }
39 | return p == a ? *(p + 2) : *(p - 1);
40 | }
41 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_function_ptr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Tests if the CBE will execute a function pointer.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int subtract(int x, int y) {
16 | int z = x - y;
17 | return z;
18 | }
19 |
20 | int main() {
21 | int (*sabPtr)(int, int) = subtract;
22 | return (*sabPtr)(10, 4);
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_function_ptr_argument.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Tests if the CBE can handle a function pointer passed as an argument.
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | static int do_op(int (*op)(int, int), int x, int y) { return op(x, y); }
15 |
16 | static int subtract(int x, int y) {
17 | int z = x - y;
18 | return z;
19 | }
20 |
21 | int main() { return do_op(subtract, 10, 4); }
22 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_function_ptr_select.c:
--------------------------------------------------------------------------------
1 | int a(void)
2 | {
3 | return 6;
4 | }
5 |
6 | int b(void)
7 | {
8 | return 0;
9 | }
10 |
11 | int main(int argc, char **argv)
12 | {
13 | int (*fp)(void) = (argc > 1 ? b : a);
14 | return fp();
15 | }
16 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_head_recursion.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests if the CBE will execute a head recursion example correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | // When a phi gets set to `poison` we don't initialize the PHI_TEMPORARY variable
16 | // so suppress "uninitialized local variable '...' used".
17 | // msvc_extra_args: /wd4700
18 |
19 | int head(int n) {
20 | if (n == 6)
21 | return n;
22 | else
23 | return head(n + 1);
24 | }
25 |
26 | int main() { return head(0); }
27 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_nested_function_ptr_argument.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Tests if the CBE can handle a function pointer with a second function pointer
11 | // as one of its arguments.
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | static int return_arg(int x) { return x; }
16 |
17 | static int call1(int (*f)(int), int x) { return f(x); }
18 |
19 | static int call2(int (*f2)(int (*)(int), int), int (*f)(int), int x) {
20 | return f2(f, x);
21 | }
22 |
23 | int main() { return call2(call1, return_arg, 6); }
24 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_recursive_function.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Test if CBE can handle a recursive function.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int fibonaci(int i) {
16 | if (i == 0) {
17 | return 0;
18 | }
19 | if (i == 1) {
20 | return 1;
21 | }
22 | return fibonaci(i - 1) + fibonaci(i - 2);
23 | }
24 |
25 | int main() {
26 | int returnval;
27 | returnval = fibonaci(6) - 2;
28 |
29 | return returnval;
30 | }
31 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_simple_function.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle a simple addition function.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int addby2(int x);
16 |
17 | int main() {
18 | int n;
19 | n = addby2(4);
20 | return n;
21 | }
22 |
23 | int addby2(int x) {
24 | int p;
25 | p = x + 2;
26 | return (p);
27 | }
28 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_static_function.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle a static function.
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | static int foo(int x);
15 |
16 | int main() { return foo(4); }
17 |
18 | static int foo(int x) { return x + 2; }
19 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_struct_function_argument.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will pass a structure into a function
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int k = 0;
16 |
17 | struct test {
18 | int i;
19 | float f;
20 | };
21 |
22 | void funct(struct test example) { k = example.i; }
23 |
24 | int main() {
25 | struct test example;
26 |
27 | example.i = 6;
28 | example.f = 6.0;
29 | funct(example);
30 |
31 | return k;
32 | }
33 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_tail_recursion.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests if the CBE will execute a tail recursion example correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | // When a phi gets set to `poison` we don't initialize the PHI_TEMPORARY variable
16 | // so suppress "uninitialized local variable '...' used".
17 | // msvc_extra_args: /wd4700
18 |
19 | int tail(int n) {
20 | if (n == 6)
21 | return n;
22 | else
23 | return tail(n + 1);
24 | }
25 |
26 | int main() { return tail(0); }
27 |
--------------------------------------------------------------------------------
/test/c_tests/functions/test_zero_length_array_argument.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will handle zero-length arrays properly,
11 | // when they are used as function arguments.
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | // xfail: zero length arrays don't work
16 |
17 | int foo(char some_array[0]) { return some_array[0] + some_array[1]; }
18 |
19 | int main() {
20 | union {
21 | char zl_chars[0];
22 | struct {
23 | char first_char;
24 | char second_char;
25 | };
26 | } x;
27 |
28 | x.first_char = 2;
29 | x.second_char = 4;
30 | return foo(x.zl_chars);
31 | }
32 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_add_int_vars.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle addition between two
11 | // variables. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i = 2, t = 4, x = 0;
17 | x = i + t;
18 |
19 | return x;
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_bitwise_and.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // AND (a&b) bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 6; // 0110
18 | unsigned int b = 15; // 1111
19 | unsigned int c = 0;
20 |
21 | c = a & b;
22 | if (c == 6) {
23 | return 6;
24 | }
25 | return 1;
26 | }
27 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_bitwise_not.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // NOT (~a) bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = -7;
18 | unsigned int b = 0;
19 |
20 | b = ~a;
21 | if (b == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_bitwise_or.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // OR (a&b) bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 2;
18 | unsigned int b = 4;
19 | unsigned int c = 0;
20 |
21 | c = a | b;
22 | if (c == 6) {
23 | return 6;
24 | }
25 | return 1;
26 | }
27 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_bitwise_shl.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Binary Shift Left (a<>b) bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 13; // 1100
18 | unsigned int b = 0;
19 |
20 | b = a >> 1; // 0110
21 | if (b == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_bitwise_xor.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // XOR (a^b) bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 9; // 1001
18 | unsigned int b = 15; // 1111
19 | unsigned int c = 0;
20 |
21 | c = a ^ b;
22 | if (c == 6) {
23 | return 6;
24 | }
25 | return 1;
26 | }
27 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_add_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Compound Addition Assignment(a+=b) operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int a = 3;
18 | int b = 3;
19 |
20 | a += b;
21 | if (a == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_bitwise_and_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // AND (a&=b) Compound Assignment bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 6; // 0110
18 | unsigned int b = 15; // 1111
19 |
20 | a &= b;
21 | if (a == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_bitwise_or_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // OR (a|=b) Compound Assignment bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 2;
18 | unsigned int b = 4;
19 |
20 | a |= b;
21 | if (a == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_bitwise_xor_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // XOR (a^=b) Compound Assignment bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 9; // 1001
18 | unsigned int b = 15; // 1111
19 |
20 | a ^= b;
21 | if (a == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_divide_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Compound Division Assignment(a/=b) operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int a = 30;
18 | int b = 5;
19 |
20 | a /= b;
21 | if (a == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_modulus_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Compound Modulus Assignment(a%=b) operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int a = 20;
18 | int b = 14;
19 |
20 | a %= b;
21 | if (a == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_multiply_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Compound Multiplication Assignment(a*=b) operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 | int main() {
16 | int a = 2;
17 | int b = 3;
18 |
19 | a *= b;
20 | if (a == 6) {
21 | return 6;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_shl_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Binary Shift Left (a<<=b) Compound Assignment bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 3; // 0011
18 |
19 | a <<= 1; // 0110
20 | if (a == 6) {
21 | return 6;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_shr_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Binary Shift Right (a>>=b) Compound Assignment bitwise operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned int a = 13; // 1100
18 |
19 | a >>= 1; // 0110
20 | if (a == 6) {
21 | return 6;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_compound_subtract_assign.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // Compound Subtraction Assignment(a-=b) operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int a = 9;
18 | int b = 3;
19 |
20 | a -= b;
21 | if (a == 6) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_divide_int_vars.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle division between two
11 | // variables. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i = 30, t = 5, x = 0;
17 | x = i / t;
18 |
19 | return x;
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_equal_operator.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // equal (==) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 6;
19 |
20 | if (x == y) {
21 | return x;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_greater_than.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // greater than (>) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 3;
19 |
20 | if (x > y) {
21 | return x;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_greater_than_or_equal.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // greater than or equal (>=) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 6;
19 |
20 | if (x >= y) {
21 | return x;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_less_than.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // less than (<) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 12;
19 |
20 | if (x < y) {
21 | return x;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_less_than_or_equal.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // less than or equal to (=<) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 6;
19 |
20 | if (x <= y) {
21 | return x;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_logical_and.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // AND (&&) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 6;
19 | int z = 6;
20 |
21 | if (x == y && x == z) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_logical_or.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // OR (||) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 6;
19 | int z = 6;
20 |
21 | if (x == y || x != z) {
22 | return 6;
23 | }
24 | return 1;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_modulus_int_vars.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle modulus between two variables.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i = 26, t = 20, x = 0;
17 | x = i % t;
18 |
19 | return x;
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_multiply_int_vars.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle multiplication between two
11 | // variables. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i = 3, t = 2, x = 0;
17 | x = i * t;
18 |
19 | return x;
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_not_equal_operator.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the
11 | // not equal (!=) relational logic operator.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6;
18 | int y = 2;
19 |
20 | if (x != y) {
21 | return x;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_postincrement.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the decremental (--a)
11 | // operator. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 7;
17 |
18 | --x;
19 |
20 | return x;
21 | }
22 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_preincrement.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the incremental (++a)
11 | // operator. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 5;
17 |
18 | ++x;
19 |
20 | return x;
21 | }
22 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_subtract_int_vars.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle subtraction between two
11 | // variables.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int i = 8, t = 2, x = 0;
18 | x = i - t;
19 |
20 | return x;
21 | }
22 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_unary_minus_int.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the unary minus (-a) operator.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | signed int a = 10;
17 | signed int b = -a;
18 |
19 | if (b == -10) {
20 | return 6;
21 | }
22 | return 1;
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/operators/test_unary_plus_char.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE can handle the unary plus (+a) operator.
11 | // Increases the amount of space the char data type to 4.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | char ch;
18 |
19 | if (sizeof(+ch) == 4) {
20 | return 6;
21 | }
22 | return 1;
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_constructor.c:
--------------------------------------------------------------------------------
1 | #ifndef _MSC_VER
2 | int global_var = 0;
3 |
4 | __attribute__((constructor)) static void update_global_var_in_constructor() {
5 | global_var = 6;
6 | }
7 |
8 | int main() { return global_var; }
9 | #else
10 | int main() {
11 | return 25; /* constructor/destructor attributes not supported on MSVC */
12 | }
13 | #endif
14 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_macro_substitution.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Tests if the CBE will execute Macro Substitution.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 | #define A 3
15 | #define B 3
16 | #define C A + B
17 |
18 | int main() {
19 | int x = C;
20 | return x;
21 | }
22 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_mutual_ref_globals.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly initialize global variables
11 | // that reference each other. (Issue #4)
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | // xfail: mutually referring globals don't work (#4)
16 |
17 | extern void *pA;
18 | extern void *pB;
19 | void *pA = &pB;
20 | void *pB = &pA;
21 |
22 | int main() {
23 | if (**(void ***)pA != pA) {
24 | return 1;
25 | }
26 | if (**(void ***)pB != pB) {
27 | return 2;
28 | }
29 | return 6;
30 | }
31 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_null_is_0.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use a NULL pointer and
11 | // confirm that it is equal to zero.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 | #include
16 |
17 | int main() {
18 | int *ptr = NULL;
19 | if (ptr == 0) {
20 | return 6;
21 | }
22 | return 1;
23 | }
24 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_register_vars.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Test if CBE can handle register variables.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | register int counter = 0;
17 | counter += 6;
18 |
19 | return 6;
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_return_success.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will return the correct number.
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | int main() { return 6; }
15 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_static_vars.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Test if CBE can handle static variables.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int subtrby2(int x);
16 | static int eight = 8;
17 | static int two = 2;
18 |
19 | int main() {
20 | int n;
21 | n = subtrby2(eight);
22 | return n;
23 | }
24 |
25 | int subtrby2(int x) {
26 | int p;
27 | p = x - two;
28 | return (p);
29 | }
30 |
--------------------------------------------------------------------------------
/test/c_tests/other/test_test_failure.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // Does not return 6, will always fail. Exception for 25 so it will succeed.
11 | //
12 | //===----------------------------------------------------------------------===//
13 |
14 | int main() { return 25; }
15 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_address_of_operator.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use the address-of value
11 | // (&) variable and and return the value-at address (*) variable from integer
12 | // 'num'. *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int *ptr;
18 | int num = 6;
19 | ptr = #
20 | int deref = *ptr;
21 | return deref;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_char_sized_ptr_math_decr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly decrement a pointer via
11 | // char. This example works by subtracting two mem. addresses and adding 2 to
12 | // return 6. *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | // Suppress "array subscript -1 is outside array bounds"
17 | // gcc_extra_args: -Wno-array-bounds
18 |
19 | int main() {
20 | unsigned char inc0 = 0, inc1 = 0;
21 | int diff = 0, a = 100;
22 | int *p = &a;
23 | inc0 = (long)p;
24 | --(*p--); //--*p--);
25 | inc1 = (long)p;
26 | diff = inc0 - inc1;
27 | diff += 2;
28 | return diff;
29 | }
30 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_char_sized_ptr_math_incr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly increment a pointer via
11 | // char. This example works by subtracting two mem. addresses and adding 2 to
12 | // return 6. *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | unsigned char inc0 = 0, inc1 = 0;
18 | int diff = 0, a = 100;
19 | int *p = &a;
20 | inc0 = (long)p;
21 | ++(*p++); //++(*p++);
22 | inc1 = (long)p;
23 | diff = inc1 - inc0;
24 | diff += 2;
25 | return diff;
26 | }
27 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_deref_array_item.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use a pointer to
11 | // access an array.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int *ip;
18 | int a[2];
19 | a[0] = 1;
20 | a[1] = 6;
21 | ip = &a[1];
22 |
23 | return *ip;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_deref_char_ptr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use a char pointer and
11 | // return the value located originally at the address of x.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | char x = 'a', y = 'b', *cp;
18 | cp = &x;
19 | y = *cp;
20 | if (y == 'a') {
21 | return 6;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_deref_double_ptr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use a double pointer and
11 | // confirms the value located originally at the address of x.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | double x = 6, y = 0, *dp;
18 | dp = &x;
19 | y = *dp;
20 | if (y == 6) {
21 | return 6;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_deref_float_ptr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use a double pointer and
11 | // confirms the value located originally at the address of x.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | float x = 6, y = 0, *fp;
18 | fp = &x;
19 | y = *fp;
20 | if (y == 6) {
21 | return 6;
22 | }
23 | return 1;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_deref_int_ptr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use an integer pointer and
11 | // return the value located originally at the address of x.
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int x = 6, y = 0, *ip = 0;
18 | ip = &x;
19 | y = *ip;
20 | return y;
21 | }
22 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_deref_struct_ptr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly use a pointer to
11 | // a structure (->).
12 | // *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | struct Number {
17 | int price;
18 | };
19 |
20 | int main() {
21 | struct Number a;
22 | struct Number *ptr = &a;
23 | ptr->price = 6;
24 | return ptr->price;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_int_sized_ptr_math_decr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly decrement a pointer via
11 | // int. This example works by subtracting two mem. addresses and adding 2 to
12 | // return 6. *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int inc0 = 0, inc1 = 0, diff = 0, a = 100;
18 | int *p = &a;
19 | inc0 = (long)p;
20 | --(*p--); //--(*p--);
21 | inc1 = (long)p;
22 | diff = inc0 - inc1;
23 | diff += 2;
24 | return diff;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/pointers/test_int_sized_ptr_math_incr.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will properly increment a pointer via
11 | // int. This example works by subtracting two mem. addresses and adding 2 to
12 | // return 6. *TW
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | int main() {
17 | int inc0 = 0, inc1 = 0, diff = 0, a = 100;
18 | int *p = &a;
19 | inc0 = (long)p;
20 | ++(*p++); //++(*p++);
21 | inc1 = (long)p;
22 | diff = inc1 - inc0;
23 | diff += 2;
24 | return diff;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_break_stmt.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a break/continue statement
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x;
17 | for (x = 0; x <= 25; x++) {
18 | if (x == 6)
19 | break;
20 | if (x < 15)
21 | continue;
22 | }
23 | return x;
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_goto_stmt.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a Goto-Label statement
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 0;
17 | goto label;
18 |
19 | for (;;) {
20 | x = 10;
21 | return x;
22 | }
23 |
24 | label:
25 | x = 6;
26 | return x;
27 | }
28 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_nested_for_loop.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a nested for loop correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i, j, x = 0;
17 | for (i = 0; i < 3; i++)
18 | for (j = 0; j < 2; j++)
19 | ++x;
20 |
21 | return x;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_nested_switch_stmt.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a nested switch statement
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | char var = 'x', var2;
17 | switch (var) {
18 | case 'z':
19 | return 0;
20 | break;
21 | case 'y':
22 | return 1;
23 | break;
24 | case 'x':
25 | var2 = 'b';
26 |
27 | switch (var2) {
28 | case 'a':
29 | return 10;
30 | break;
31 | case 'b':
32 | return 6;
33 | break;
34 | default:
35 | return 18;
36 | }
37 |
38 | case 'w':
39 | return 7;
40 | break;
41 | default:
42 | return 100;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_nested_while_loop.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a nested while loop
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i = 0, j = 0, x = 0;
17 | while (i < 6) {
18 | while (j < 6) {
19 | ++x;
20 | ++j;
21 | }
22 | ++i;
23 | }
24 | return x;
25 | }
26 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_simple_do_while.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a do-while statement
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 0;
17 | do {
18 | x++;
19 | } while (x < 6);
20 |
21 | return x;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_simple_for_loop.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a for loop correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i, x = 0;
17 | for (i = 0; i < 6; i++)
18 | ++x;
19 | return x;
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_simple_if_else.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute an if/else statement
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 3;
17 | x += 3;
18 | if (x == 6)
19 | return x;
20 | else
21 | return 0;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_simple_if_else_use_else.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute an else-if statement
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 6;
17 | if (x == 4) {
18 | return 2;
19 | } else if (x == 6) {
20 | return 6;
21 | } else {
22 | return 8;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_simple_if_stmt.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute an if statement correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 6;
17 | if (x == 6)
18 | return x;
19 | }
20 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_simple_switch_stmt.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a switch statement
11 | // correctly. *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | char var = 'x';
17 |
18 | switch (var) {
19 | case 'z':
20 | return 0;
21 | break;
22 | case 'y':
23 | return 1;
24 | break;
25 | case 'x':
26 | return 6;
27 | break;
28 | case 'w':
29 | return 7;
30 | break;
31 | default:
32 | return 100;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test/c_tests/statements/test_simple_while_loop.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a while loop correctly.
11 | // *TW
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int i = 0, x = 0;
17 | while (i < 6) {
18 | ++x;
19 | ++i;
20 | }
21 | return x;
22 | }
23 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_compare_exchange_strong.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0;
5 | int y = 1;
6 |
7 | if (atomic_compare_exchange_strong(&x, &y, 6)) {
8 | return 1;
9 | }
10 | // compare exchange failed, so x shouldn't be 6 now
11 | if (atomic_load(&x) == 6) {
12 | return 1;
13 | }
14 |
15 | y = 0;
16 | if (!atomic_compare_exchange_strong(&x, &y, 6)) {
17 | return 1;
18 | }
19 | return atomic_load(&x);
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_compare_exchange_weak.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0;
5 | int y = 1;
6 |
7 | if (atomic_compare_exchange_weak(&x, &y, 6)) {
8 | return 1;
9 | }
10 | // compare exchange failed, so x shouldn't be 6 now
11 | if (atomic_load(&x) == 6) {
12 | return 1;
13 | }
14 |
15 | y = 0;
16 | if (!atomic_compare_exchange_weak(&x, &y, 6)) {
17 | return 1;
18 | }
19 | return atomic_load(&x);
20 | }
21 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_exchange.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0;
5 | atomic_exchange(&x, 6);
6 | return atomic_load(&x);
7 | }
8 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_fence.c:
--------------------------------------------------------------------------------
1 | //===---------------------- LLVM C Backend test file ----------------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This code tests to see that the CBE will execute a thread fence statement
11 | // correctly.
12 | //
13 | //===----------------------------------------------------------------------===//
14 |
15 | int main() {
16 | int x = 6;
17 | __atomic_thread_fence(__ATOMIC_SEQ_CST);
18 | return x;
19 | }
20 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_fetch_add.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0;
5 | atomic_fetch_add(&x, 3);
6 | atomic_fetch_add(&x, 3);
7 | return atomic_load(&x);
8 | }
9 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_fetch_and.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0b1111;
5 | atomic_fetch_and(&x, 0b1110);
6 | atomic_fetch_and(&x, 0b0111);
7 | return atomic_load(&x);
8 | }
9 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_fetch_or.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0;
5 | atomic_fetch_or(&x, 2);
6 | atomic_fetch_or(&x, 4);
7 | return atomic_load(&x);
8 | }
9 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_fetch_sub.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 12;
5 | atomic_fetch_sub(&x, 3);
6 | atomic_fetch_sub(&x, 3);
7 | return atomic_load(&x);
8 | }
9 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_fetch_xor.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0;
5 | atomic_fetch_xor(&x, 7);
6 | atomic_fetch_xor(&x, 1);
7 | return atomic_load(&x);
8 | }
9 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_load.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 6;
5 | return atomic_load(&x);
6 | }
7 |
--------------------------------------------------------------------------------
/test/c_tests/stdatomic/test_atomic_store.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | atomic_int x = 0;
5 | atomic_store(&x, 6);
6 | return atomic_load(&x);
7 | }
8 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_ctor.cpp:
--------------------------------------------------------------------------------
1 | class Foo {
2 | public:
3 | Foo(int x) : x(x) {}
4 | int x;
5 | };
6 |
7 | int main() {
8 | Foo f(6);
9 | return f.x;
10 | }
11 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_dtor.cpp:
--------------------------------------------------------------------------------
1 | class Foo {
2 | public:
3 | Foo(int *x) : x(x) {}
4 | ~Foo() { *x = 6; }
5 | int *x;
6 | };
7 |
8 | int main() {
9 | int y = 0;
10 | {
11 | Foo f(&y);
12 | }
13 | return y;
14 | }
15 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_dtor_called_when_unwinding.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | // xfail: no landingpad support (#17)
4 |
5 | class Foo {
6 | public:
7 | Foo(int *x) : x(x) {}
8 | ~Foo() { *x = 6; }
9 | int *x;
10 | };
11 |
12 | int main() {
13 | int y = 1;
14 |
15 | try {
16 | Foo f(&y);
17 | // f's dtor should modify y to 6 during exception unwinding, when the
18 | // exception is thrown
19 | throw std::exception();
20 | } catch (const std::exception &e) {
21 | }
22 |
23 | return y;
24 | }
25 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_exception_from_ctor.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | // xfail: no landingpad support (#17)
4 |
5 | class Foo {
6 | public:
7 | Foo() { throw std::exception(); }
8 | };
9 |
10 | int main() {
11 | try {
12 | Foo f;
13 | } catch (const std::exception &e) {
14 | return 6;
15 | }
16 |
17 | return 0;
18 | }
19 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_is_constant.cpp:
--------------------------------------------------------------------------------
1 | int f(int i) {
2 | if (__builtin_constant_p(i) &&
3 | i == 1) /* generates the llvm.is.constant.i32 intrinsic at -O0 */
4 | return i + 5;
5 | return 6;
6 | }
7 |
8 | int main() { return f(1); }
9 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_multiple_exception_handlers.cpp:
--------------------------------------------------------------------------------
1 | // xfail: no landingpad support (#17)
2 |
3 | class exc1 {
4 | public:
5 | exc1() {}
6 | virtual ~exc1() {}
7 | };
8 |
9 | class exc2 {
10 | public:
11 | exc2() {}
12 | virtual ~exc2() {}
13 | };
14 |
15 | int main() {
16 | try {
17 | throw exc2();
18 | } catch (const exc1 &e) {
19 | return 0;
20 | } catch (const exc2 &e) {
21 | return 6;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_my_exception.cpp:
--------------------------------------------------------------------------------
1 | // xfail: no landingpad support (#17)
2 |
3 | class my_exception {
4 | public:
5 | my_exception() {}
6 | virtual ~my_exception() {}
7 | };
8 |
9 | int main() {
10 | try {
11 | throw my_exception();
12 | } catch (const my_exception &e) {
13 | return 6;
14 | }
15 |
16 | return 0;
17 | }
18 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_std_exception.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | // xfail: no std::exception support
4 |
5 | int main() {
6 | try {
7 | throw std::exception();
8 | } catch (const std::exception &e) {
9 | return 6;
10 | }
11 |
12 | return 0;
13 | }
14 |
--------------------------------------------------------------------------------
/test/cpp_tests/test_virtual_function_calls.cpp:
--------------------------------------------------------------------------------
1 | #ifndef _MSC_VER
2 | // Suppress "dereferencing type-punned pointer will break strict-aliasing rules"
3 | // gcc_extra_args: -Wno-strict-aliasing
4 | struct A {
5 | int a;
6 | int b;
7 | int f1(int i) { return 1 + i; }
8 | int f2(int i) { return 1 + i; }
9 | typedef int (A::*Function)(int);
10 | int function_wrapper(Function f, int i) __attribute__((noinline)) {
11 | return (this->*f)(i);
12 | }
13 | };
14 |
15 | int main() {
16 | A a{};
17 | if (a.function_wrapper(&A::f1, 1) != 2)
18 | return 0;
19 | if (a.function_wrapper(&A::f2, 1) != 2)
20 | return 0;
21 | return 6;
22 | }
23 |
24 | #else
25 |
26 | /* MSVC doesn't support adding alignment to functions, treat it as an expected
27 | * failure */
28 |
29 | int main() { return 25; }
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/test/hello_world/.gitignore:
--------------------------------------------------------------------------------
1 | *
2 | !.gitignore
3 | !main.c
4 |
--------------------------------------------------------------------------------
/test/hello_world/main.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | printf("Hello, World!\n");
5 | return 0;
6 | }
7 |
--------------------------------------------------------------------------------
/test/ll_tests/test_addressable_empty_types.ll:
--------------------------------------------------------------------------------
1 | ; This tests the handling of global variables, constants, and allocas which have
2 | ; zero-sized/empty types, and in particular taking pointers to them.
3 | ; Obviously these pointers can't be dereferenced, but it's legal to pass them
4 | ; around and compare them, so we must produce reasonable code for this.
5 |
6 | %emptyStruct = type {}
7 | %emptyArray = type [0 x i64]
8 | %veryEmptyArray = type [0 x %emptyStruct]
9 | %veryEmptyStruct = type { %emptyStruct }
10 | %extremelyEmptyArray = type [0 x %veryEmptyStruct]
11 |
12 | @constant1 = constant %emptyStruct zeroinitializer
13 | @constant2 = constant %emptyArray zeroinitializer
14 | @constant3 = constant %veryEmptyArray zeroinitializer
15 | @constant4 = constant %veryEmptyStruct zeroinitializer
16 | @constant5 = constant %extremelyEmptyArray zeroinitializer
17 |
18 | @global1 = private global %emptyStruct zeroinitializer, align 16
19 | @global2 = private global %emptyArray zeroinitializer, align 8
20 | @global3 = private global %veryEmptyArray zeroinitializer, align 4
21 | @global4 = private global %veryEmptyStruct zeroinitializer, align 2
22 | @global5 = private global %extremelyEmptyArray zeroinitializer, align 1
23 |
24 | define i32 @main() {
25 | %alloca1 = alloca %emptyStruct, align 1
26 | %alloca2 = alloca %emptyArray, align 2
27 | %alloca3 = alloca %veryEmptyArray, align 4
28 | %alloca4 = alloca %veryEmptyStruct, align 8
29 | %alloca5 = alloca %extremelyEmptyArray, align 16
30 |
31 | %eq1a = icmp eq %emptyStruct* @constant1, @global1
32 | %eq1b = icmp eq %emptyStruct* @constant1, %alloca1
33 | %eq2a = icmp eq %emptyArray* @constant2, @global2
34 | %eq2b = icmp eq %emptyArray* @constant2, %alloca2
35 | %eq3a = icmp eq %veryEmptyArray* @constant3, @global3
36 | %eq3b = icmp eq %veryEmptyArray* @constant3, %alloca3
37 | %eq4a = icmp eq %veryEmptyStruct* @constant4, @global4
38 | %eq4b = icmp eq %veryEmptyStruct* @constant4, %alloca4
39 | %eq5a = icmp eq %extremelyEmptyArray* @constant5, @global5
40 | %eq5b = icmp eq %extremelyEmptyArray* @constant5, %alloca5
41 |
42 | %zext1a = zext i1 %eq1a to i32
43 | %zext1b = zext i1 %eq1b to i32
44 | %zext2a = zext i1 %eq2a to i32
45 | %zext2b = zext i1 %eq2b to i32
46 | %zext3a = zext i1 %eq3a to i32
47 | %zext3b = zext i1 %eq3b to i32
48 | %zext4a = zext i1 %eq4a to i32
49 | %zext4b = zext i1 %eq4b to i32
50 | %zext5a = zext i1 %eq5a to i32
51 | %zext5b = zext i1 %eq5b to i32
52 |
53 | %sum1 = add i32 %zext1a, %zext1b
54 | %sum2 = add i32 %zext2a, %zext2b
55 | %sum3 = add i32 %zext3a, %zext3b
56 | %sum4 = add i32 %zext4a, %zext4b
57 | %sum5 = add i32 %zext5a, %zext5b
58 |
59 | %sum12 = add i32 %sum1, %sum2
60 | %sum34 = add i32 %sum3, %sum4
61 | %sum1234 = add i32 %sum12, %sum34
62 | %sum = add i32 %sum1234, %sum5
63 |
64 | %correct = icmp eq i32 %sum, 0
65 |
66 | %retCode = select i1 %correct, i32 6, i32 0
67 |
68 | ret i32 %retCode
69 | }
70 |
--------------------------------------------------------------------------------
/test/ll_tests/test_bitcounts.ll:
--------------------------------------------------------------------------------
1 | declare i64 @llvm.ctpop.i64(i64)
2 | declare i64 @llvm.ctlz.i64(i64, i1) ; i1: is_zero_poison
3 | declare i16 @llvm.ctlz.i16(i16, i1) ; i1: is_zero_poison
4 | declare i64 @llvm.cttz.i64(i64, i1); i1: is_zero_poison>
5 | declare i16 @llvm.cttz.i16(i16, i1); i1: is_zero_poison>
6 |
7 |
8 | define dso_local i32 @main() #0 {
9 | ctpop.1:
10 | %ctpop.1.val = call i64 @llvm.ctpop.i64(i64 18446744073709551615) ; 0xffffffffffffffff
11 | %ctpop.1.ok = icmp eq i64 %ctpop.1.val, 64
12 | br i1 %ctpop.1.ok, label %ctpop.2, label %error
13 |
14 | ctpop.2:
15 | %ctpop.2.val = call i64 @llvm.ctpop.i64(i64 0)
16 | %ctpop.2.ok = icmp eq i64 %ctpop.2.val, 0
17 | br i1 %ctpop.2.ok, label %ctpop.3, label %error
18 |
19 | ctpop.3:
20 | %ctpop.3.val = call i64 @llvm.ctpop.i64(i64 181) ; 0b10110101
21 | %ctpop.3.ok = icmp eq i64 %ctpop.3.val, 5
22 | br i1 %ctpop.3.ok, label %ctlz.1, label %error
23 |
24 | ctlz.1:
25 | %ctlz.1.val = call i64 @llvm.ctlz.i64(i64 18446744073709551615, i1 0) ; 0xffffffffffffffff
26 | %ctlz.1.ok = icmp eq i64 %ctlz.1.val, 0
27 | br i1 %ctlz.1.ok, label %ctlz.2, label %error
28 |
29 | ctlz.2:
30 | %ctlz.2.val = call i64 @llvm.ctlz.i64(i64 1, i1 0)
31 | %ctlz.2.ok = icmp eq i64 %ctlz.2.val, 63
32 | br i1 %ctlz.2.ok, label %ctlz.3, label %error
33 |
34 | ctlz.3:
35 | %ctlz.3.val = call i16 @llvm.ctlz.i16(i16 181, i1 0) ; 0b10110101
36 | %ctlz.3.ok = icmp eq i16 %ctlz.3.val, 8
37 | br i1 %ctlz.3.ok, label %cttz.1, label %error
38 |
39 | cttz.1:
40 | %cttz.1.val = call i64 @llvm.cttz.i64(i64 18446744073709551615, i1 0) ; 0xffffffffffffffff
41 | %cttz.1.ok = icmp eq i64 %cttz.1.val, 0
42 | br i1 %cttz.1.ok, label %cttz.2, label %error
43 |
44 | cttz.2:
45 | %cttz.2.val = call i64 @llvm.cttz.i64(i64 1, i1 0)
46 | %cttz.2.ok = icmp eq i64 %cttz.2.val, 0
47 | br i1 %cttz.2.ok, label %cttz.3, label %error
48 |
49 | cttz.3:
50 | %cttz.3.val = call i16 @llvm.cttz.i16(i16 180, i1 0) ; 0b10110100
51 | %cttz.3.ok = icmp eq i16 %cttz.3.val, 2
52 | br i1 %cttz.3.ok, label %ok, label %error
53 |
54 | ok:
55 | ret i32 6
56 |
57 | error:
58 | %retVal = phi i32
59 | [ 20, %ctpop.1 ],
60 | [ 21, %ctpop.2 ],
61 | [ 22, %ctpop.3 ],
62 | [ 23, %ctlz.1 ],
63 | [ 24, %ctlz.2 ],
64 | [ 25, %ctlz.3 ],
65 | [ 26, %cttz.1 ],
66 | [ 27, %cttz.2 ],
67 | [ 28, %cttz.3 ]
68 | ret i32 %retVal
69 | }
70 |
--------------------------------------------------------------------------------
/test/ll_tests/test_bswap.ll:
--------------------------------------------------------------------------------
1 | declare i16 @llvm.bswap.i16(i16)
2 | declare i32 @llvm.bswap.i32(i32)
3 | declare i64 @llvm.bswap.i64(i64)
4 |
5 | define dso_local i32 @main() #0 {
6 | bswap.i16:
7 | %bswap.i16.val = call i16 @llvm.bswap.i16(i16 4660) ; 0x1234
8 | %bswap.i16.ok = icmp eq i16 %bswap.i16.val, 13330 ; 0x3412
9 | br i1 %bswap.i16.ok, label %bswap.i32, label %error
10 |
11 | bswap.i32:
12 | %bswap.i32.val = call i32 @llvm.bswap.i32(i32 305419896) ; 0x12345678
13 | %bswap.i32.ok = icmp eq i32 %bswap.i32.val, 2018915346 ; 0x78563412
14 | br i1 %bswap.i32.ok, label %bswap.i64, label %error
15 |
16 | bswap.i64:
17 | %bswap.i64.val = call i64 @llvm.bswap.i64(i64 1311768467463790320) ; 0x123456789abcdef0
18 | %bswap.i64.ok = icmp eq i64 %bswap.i64.val, 17356517385562371090 ; 0xf0debc9a78563412
19 | br i1 %bswap.i64.ok, label %ok, label %error
20 |
21 | ok:
22 | ret i32 6
23 |
24 | error:
25 | %retVal = phi i32
26 | [ 20, %bswap.i16 ],
27 | [ 21, %bswap.i32 ],
28 | [ 22, %bswap.i64 ]
29 | ret i32 %retVal
30 | }
31 |
--------------------------------------------------------------------------------
/test/ll_tests/test_byval_struct_param.ll:
--------------------------------------------------------------------------------
1 | ; Test byval parameter attribute works.
2 |
3 | %struct.intbox = type { i32 }
4 |
5 | ; Function Attrs: noinline nounwind optnone ssp uwtable
6 | define void @add(%struct.intbox* noalias sret(%struct.intbox) %0, %struct.intbox* byval(%struct.intbox) align 4 %1, %struct.intbox* byval(%struct.intbox) align 4 %2) #0 {
7 | %4 = getelementptr inbounds %struct.intbox, %struct.intbox* %1, i32 0, i32 0
8 | %5 = load i32, i32* %4, align 4
9 | %6 = getelementptr inbounds %struct.intbox, %struct.intbox* %2, i32 0, i32 0
10 | %7 = load i32, i32* %6, align 4
11 | %8 = add nsw i32 %5, %7
12 | %9 = getelementptr inbounds %struct.intbox, %struct.intbox* %0, i32 0, i32 0
13 | store i32 %8, i32* %9, align 4
14 | ret void
15 | }
16 |
17 | ; Function Attrs: noinline nounwind optnone ssp uwtable
18 | define i32 @main(i32 %0, i8** %1) #0 {
19 | %3 = alloca %struct.intbox, align 4
20 | %4 = getelementptr inbounds %struct.intbox, %struct.intbox* %3, i32 0, i32 0
21 | store i32 2, i32* %4, align 4
22 | %5 = alloca %struct.intbox, align 4
23 | %6 = getelementptr inbounds %struct.intbox, %struct.intbox* %5, i32 0, i32 0
24 | store i32 4, i32* %6, align 4
25 | %7 = alloca %struct.intbox, align 4
26 | call void @add(%struct.intbox* sret(%struct.intbox) %7, %struct.intbox* byval(%struct.intbox) align 4 %3, %struct.intbox* byval(%struct.intbox) align 4 %5)
27 | %8 = getelementptr inbounds %struct.intbox, %struct.intbox* %7, i32 0, i32 0
28 | %9 = load i32, i32* %8, align 4
29 | ret i32 %9
30 | }
31 |
--------------------------------------------------------------------------------
/test/ll_tests/test_compile_opaque_struct_with_function.ll:
--------------------------------------------------------------------------------
1 | %TOpaque = type opaque
2 | %TReal = type { i32 (%TOpaque*) *}
3 |
4 | ; Function Attrs: nounwind uwtable
5 | define i32 @call4(%TOpaque*) unnamed_addr {
6 | %bcast = bitcast %TOpaque* %0 to %TReal*
7 | %2 = getelementptr %TReal, %TReal* %bcast, i32 0, i32 0
8 | %3 = load i32 (%TOpaque*) *, i32 (%TOpaque*) ** %2
9 | %4 = call i32 %3(%TOpaque* %0)
10 | ret i32 %4
11 | }
12 |
13 | ; Function Attrs: noinline nounwind optnone uwtable
14 | define dso_local i32 @main() #0 {
15 | ret i32 6
16 | }
17 |
--------------------------------------------------------------------------------
/test/ll_tests/test_empty_array_geps.ll:
--------------------------------------------------------------------------------
1 | ; This tests GEPs and Phi nodes for pointers to zero-sized arrays.
2 | ; It is inspired by LLVM IR produced by rustc 1.45.0 for code like
3 | ;
4 | ; match some_expression {
5 | ; Case1 => "Test string",
6 | ; Case2 => "Different test string",
7 | ; }.as_ptr()
8 | ;
9 | ; but with some simplifications.
10 |
11 | ; Suppress "array is too small to include a terminating null character"
12 | ; msvc_extra_args: /wd4295
13 |
14 | @correctStringConstant = constant [3 x i8] c"\01\02\03"
15 | @wrongStringConstant = constant [5 x i8] c"test\00"
16 |
17 | define i32 @main(i32 %argc, i8** %argv) {
18 | bb0:
19 | %alwaysTrue = icmp eq i32 %argc, 1
20 | br i1 %alwaysTrue, label %iftrue, label %iffalse
21 |
22 | iftrue:
23 | br label %end
24 |
25 | iffalse:
26 | br label %end
27 |
28 | end:
29 | %stringConstant = phi [0 x i8]* [ bitcast ([3 x i8]* @correctStringConstant to [0 x i8]*), %iftrue ], [ bitcast ([5 x i8]* @wrongStringConstant to [0 x i8]*), %iffalse ]
30 |
31 | %char1ptr = getelementptr [0 x i8], [0 x i8]* %stringConstant, i64 0, i64 0
32 | %char2ptr = getelementptr [0 x i8], [0 x i8]* %stringConstant, i64 0, i64 1
33 | %char3ptr = getelementptr [0 x i8], [0 x i8]* %stringConstant, i64 0, i64 2
34 |
35 | %char1 = load i8, i8* %char1ptr
36 | %char2 = load i8, i8* %char2ptr
37 | %char3 = load i8, i8* %char3ptr
38 |
39 | %sum12 = add i8 %char1, %char2
40 | %sum = add i8 %sum12, %char3
41 | %res = zext i8 %sum to i32
42 |
43 | ret i32 %res
44 | }
45 |
--------------------------------------------------------------------------------
/test/ll_tests/test_empty_array_geps_struct.ll:
--------------------------------------------------------------------------------
1 | ; This tests GEPs and Phi nodes for pointers to zero-sized arrays of structs.
2 |
3 | %struct.intbox = type { i32 }
4 |
5 | @correctConstant = constant [3 x %struct.intbox] [%struct.intbox { i32 1 }, %struct.intbox { i32 2 }, %struct.intbox { i32 3 }]
6 | @wrongConstant = constant [5 x %struct.intbox] [%struct.intbox { i32 4 }, %struct.intbox { i32 5 }, %struct.intbox { i32 6 }, %struct.intbox { i32 7 }, %struct.intbox { i32 8 }]
7 |
8 | define i32 @main(i32 %argc, i8** %argv) {
9 | bb0:
10 | %alwaysTrue = icmp eq i32 %argc, 1
11 | br i1 %alwaysTrue, label %iftrue, label %iffalse
12 |
13 | iftrue:
14 | br label %end
15 |
16 | iffalse:
17 | br label %end
18 |
19 | end:
20 | %theConstant = phi [0 x %struct.intbox]* [ bitcast ([3 x %struct.intbox]* @correctConstant to [0 x %struct.intbox]*), %iftrue ], [ bitcast ([5 x %struct.intbox]* @wrongConstant to [0 x %struct.intbox]*), %iffalse ]
21 |
22 | %int1ptr = getelementptr [0 x %struct.intbox], [0 x %struct.intbox]* %theConstant, i64 0, i64 0, i32 0
23 | %int2ptr = getelementptr [0 x %struct.intbox], [0 x %struct.intbox]* %theConstant, i64 0, i64 1, i32 0
24 | %int3ptr = getelementptr [0 x %struct.intbox], [0 x %struct.intbox]* %theConstant, i64 0, i64 2, i32 0
25 |
26 | %int1 = load i32, i32* %int1ptr, align 4
27 | %int2 = load i32, i32* %int2ptr, align 4
28 | %int3 = load i32, i32* %int3ptr, align 4
29 |
30 | %sum12 = add i32 %int1, %int2
31 | %res = add i32 %sum12, %int3
32 |
33 | ret i32 %res
34 | }
35 |
--------------------------------------------------------------------------------
/test/ll_tests/test_fun_ptr_dependencies.ll:
--------------------------------------------------------------------------------
1 | ; This tests that CBE will emit function type definitions before other function
2 | ; type definitions that use them, even in the presence of more than one level of
3 | ; pointer indirection.
4 |
5 | %struct.funptr_struct = type { void (void ()**)*, void ()* }
6 |
7 | define i32 @main() {
8 | %1 = alloca %struct.funptr_struct, align 8
9 | ret i32 6
10 | }
11 |
--------------------------------------------------------------------------------
/test/ll_tests/test_fun_ptr_only_refd_by_function.ll:
--------------------------------------------------------------------------------
1 | ; This tests whether the CBE will emit a definition for a function type whose
2 | ; only direct reference is a function parameter list.
3 |
4 | %funptr = type void ()*
5 |
6 | declare i32 @someFun(%funptr)
7 |
8 | define i32 @main() {
9 | ret i32 6
10 | }
11 |
--------------------------------------------------------------------------------
/test/ll_tests/test_fun_ptr_only_refd_by_struct.ll:
--------------------------------------------------------------------------------
1 | ; This tests whether the CBE will emit a definition for a function type whose
2 | ; only direct reference is a struct type. (Issue #101)
3 |
4 | %struct.funptr_struct = type { void ()* }
5 |
6 | define i32 @main() {
7 | %1 = alloca %struct.funptr_struct, align 8
8 | ret i32 6
9 | }
10 |
--------------------------------------------------------------------------------
/test/ll_tests/test_function_alias.ll:
--------------------------------------------------------------------------------
1 | ; Test function aliases
2 | ; It is inspired by LLVM IR produced by rustc 1.69 for code like
3 | ;
4 | ; #[no_mangle]
5 | ; unsafe extern "C" fn drop_vec_of_a(
6 | ; data: *mut A,
7 | ; len: usize,
8 | ; capacity: usize,
9 | ; ) {
10 | ; drop(Vec::from_raw_parts(data, len, capacity))
11 | ; }
12 | ; #[no_mangle]
13 | ; unsafe extern "C" fn drop_vec_of_b(
14 | ; data: *mut B,
15 | ; len: usize,
16 | ; capacity: usize,
17 | ; ) {
18 | ; drop(Vec::from_raw_parts(data, len, capacity))
19 | ; }
20 | ;
21 | ; but with some simplifications.
22 |
23 | ; Function Attrs: noinline nounwind optnone uwtable
24 | define i32 @drop_vec_of_a(i32 %retVal) #0 {
25 | ret i32 %retVal
26 | }
27 |
28 | @drop_vec_of_b = unnamed_addr alias i32 (i32), ptr @drop_vec_of_a
29 |
30 | ; Function Attrs: noinline nounwind optnone uwtable
31 | define dso_local i32 @main() #1 {
32 | %a = call i32 @drop_vec_of_a(i32 2)
33 | %b = call i32 @drop_vec_of_b(i32 4)
34 | %sum = add i32 %a, %b
35 | ret i32 %sum
36 | }
--------------------------------------------------------------------------------
/test/ll_tests/test_gep_index_vec.ll:
--------------------------------------------------------------------------------
1 | ; This tests GEPs being indexed via vectors.
2 |
3 | ; Suppress "array is too small to include a terminating null character"
4 | ; msvc_extra_args: /wd4295
5 |
6 | @correctStringConstant = constant [3 x i8] c"\01\02\03"
7 | @wrongStringConstant = constant [3 x i8] c"\04\05\06"
8 |
9 | define i32 @main(i32 %argc, i8** %argv) {
10 | bb0:
11 | %alwaysTrue = icmp eq i32 %argc, 1
12 | br i1 %alwaysTrue, label %iftrue, label %iffalse
13 |
14 | iftrue:
15 | br label %end
16 |
17 | iffalse:
18 | br label %end
19 |
20 | end:
21 | %stringConstant = phi ptr [ @correctStringConstant, %iftrue ], [ @wrongStringConstant, %iffalse ]
22 |
23 | %charPtrVec = getelementptr i8, ptr %stringConstant, <3 x i64>
24 |
25 | %char1ptr = extractelement <3 x ptr> %charPtrVec, i64 0
26 | %char2ptr = extractelement <3 x ptr> %charPtrVec, i64 1
27 | %char3ptr = extractelement <3 x ptr> %charPtrVec, i64 2
28 |
29 | %char1 = load i8, i8* %char1ptr
30 | %char2 = load i8, i8* %char2ptr
31 | %char3 = load i8, i8* %char3ptr
32 |
33 | %sum12 = add i8 %char1, %char2
34 | %sum = add i8 %sum12, %char3
35 | %res = zext i8 %sum to i32
36 |
37 | ret i32 %res
38 | }
39 |
--------------------------------------------------------------------------------
/test/ll_tests/test_global_var_order.ll:
--------------------------------------------------------------------------------
1 | @test_array = constant { [1 x i8*] } { [1 x i8*] [i8* @defined_after_test_array] }
2 | @defined_after_test_array = constant i8* null
3 |
4 | define dso_local i32 @main() #0 {
5 | ret i32 6
6 | }
7 |
--------------------------------------------------------------------------------
/test/ll_tests/test_if_bool_xor.ll:
--------------------------------------------------------------------------------
1 | define dso_local i1 @a() {
2 | ret i1 1
3 | }
4 |
5 | define dso_local i32 @main() #0 {
6 | %ret = call zeroext i1 @a()
7 | %cond = xor i1 %ret, true
8 | br i1 %cond, label %a_is_false, label %a_is_true
9 |
10 | a_is_false:
11 | ret i32 -1
12 |
13 | a_is_true:
14 | ret i32 6
15 | }
16 |
--------------------------------------------------------------------------------
/test/ll_tests/test_intrinsic_sadd_sat.ll:
--------------------------------------------------------------------------------
1 | declare i8 @llvm.sadd.sat.i8(i8 %a, i8 %b)
2 | declare i16 @llvm.sadd.sat.i16(i16 %a, i16 %b)
3 | declare i32 @llvm.sadd.sat.i32(i32 %a, i32 %b)
4 | declare i64 @llvm.sadd.sat.i64(i64 %a, i64 %b)
5 |
6 | ; Function Attrs: noinline nounwind optnone uwtable
7 | define dso_local i32 @main() #0 {
8 | basic_8:
9 | %basic_8_sum = call i8 @llvm.sadd.sat.i8(i8 52, i8 -10)
10 | %basic_8_ok = icmp eq i8 %basic_8_sum, 42
11 | br i1 %basic_8_ok, label %overflowing_8, label %error
12 |
13 | overflowing_8:
14 | %overflowing_8_sum = call i8 @llvm.sadd.sat.i8(i8 u0x7C, i8 u0x7C)
15 | %overflowing_8_ok = icmp eq i8 %overflowing_8_sum, 127
16 | br i1 %overflowing_8_ok, label %underflowing_8, label %error
17 |
18 | underflowing_8:
19 | %underflowing_8_sum = call i8 @llvm.sadd.sat.i8(i8 u0x8C, i8 u0x8C)
20 | %underflowing_8_ok = icmp eq i8 %underflowing_8_sum, -128
21 | br i1 %underflowing_8_ok, label %basic_16, label %error
22 |
23 | basic_16:
24 | %basic_16_sum = call i16 @llvm.sadd.sat.i16(i16 52, i16 -10)
25 | %basic_16_ok = icmp eq i16 %basic_16_sum, 42
26 | br i1 %basic_16_ok, label %overflowing_16, label %error
27 |
28 | overflowing_16:
29 | %overflowing_16_sum = call i16 @llvm.sadd.sat.i16(i16 u0x7CCC, i16 u0x7CCC)
30 | %overflowing_16_ok = icmp eq i16 %overflowing_16_sum, 32767
31 | br i1 %overflowing_16_ok, label %underflowing_16, label %error
32 |
33 | underflowing_16:
34 | %underflowing_16_sum = call i16 @llvm.sadd.sat.i16(i16 u0x8CCC, i16 u0x8CCC)
35 | %underflowing_16_ok = icmp eq i16 %underflowing_16_sum, -32768
36 | br i1 %underflowing_16_ok, label %basic_32, label %error
37 |
38 | basic_32:
39 | %basic_32_sum = call i32 @llvm.sadd.sat.i32(i32 52, i32 -10)
40 | %basic_32_ok = icmp eq i32 %basic_32_sum, 42
41 | br i1 %basic_32_ok, label %overflowing_32, label %error
42 |
43 | overflowing_32:
44 | %overflowing_32_sum = call i32 @llvm.sadd.sat.i32(i32 u0x7CCCCCCC, i32 u0x7CCCCCCC)
45 | %overflowing_32_ok = icmp eq i32 %overflowing_32_sum, 2147483647
46 | br i1 %overflowing_32_ok, label %underflowing_32, label %error
47 |
48 | underflowing_32:
49 | %underflowing_32_sum = call i32 @llvm.sadd.sat.i32(i32 u0x8CCCCCCC, i32 u0x8CCCCCCC)
50 | %underflowing_32_ok = icmp eq i32 %underflowing_32_sum, -2147483648
51 | br i1 %underflowing_32_ok, label %basic_64, label %error
52 |
53 | basic_64:
54 | %basic_64_sum = call i64 @llvm.sadd.sat.i64(i64 52, i64 -10)
55 | %basic_64_ok = icmp eq i64 %basic_64_sum, 42
56 | br i1 %basic_64_ok, label %overflowing_64, label %error
57 |
58 | overflowing_64:
59 | %overflowing_64_sum = call i64 @llvm.sadd.sat.i64(i64 u0x7CCCCCCCCCCCCCCC, i64 u0x7CCCCCCCCCCCCCCC)
60 | %overflowing_64_ok = icmp eq i64 %overflowing_64_sum, 9223372036854775807
61 | br i1 %overflowing_64_ok, label %underflowing_64, label %error
62 |
63 | underflowing_64:
64 | %underflowing_64_sum = call i64 @llvm.sadd.sat.i64(i64 u0x8CCCCCCCCCCCCCCC, i64 u0x8CCCCCCCCCCCCCCC)
65 | %underflowing_64_ok = icmp eq i64 %underflowing_64_sum, -9223372036854775808
66 | br i1 %underflowing_64_ok, label %ok, label %error
67 |
68 | ok:
69 | ret i32 6
70 |
71 | error:
72 | %retVal = phi i32
73 | [ 81, %basic_8 ],
74 | [ 82, %overflowing_8 ],
75 | [ 83, %underflowing_8 ],
76 | [ 161, %basic_16 ],
77 | [ 162, %overflowing_16 ],
78 | [ 163, %underflowing_16 ],
79 | [ 321, %basic_32 ],
80 | [ 322, %overflowing_32 ],
81 | [ 323, %underflowing_32 ],
82 | [ 641, %basic_64 ],
83 | [ 642, %overflowing_64 ],
84 | [ 643, %underflowing_64 ]
85 | ret i32 %retVal
86 | }
87 |
--------------------------------------------------------------------------------
/test/ll_tests/test_intrinsic_sadd_with_overflow.ll:
--------------------------------------------------------------------------------
1 | declare {i8, i1} @llvm.sadd.with.overflow.i8(i8 %a, i8 %b)
2 | declare {i16, i1} @llvm.sadd.with.overflow.i16(i16 %a, i16 %b)
3 | declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)
4 | declare {i64, i1} @llvm.sadd.with.overflow.i64(i64 %a, i64 %b)
5 |
6 | ; Function Attrs: noinline nounwind optnone uwtable
7 | define dso_local i32 @main() #0 {
8 | basic_8:
9 | %basic_8_res = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 52, i8 -10)
10 | %basic_8_sum = extractvalue { i8, i1 } %basic_8_res, 0
11 | %basic_8_sum_ok = icmp eq i8 %basic_8_sum, 42
12 | br i1 %basic_8_sum_ok, label %basic_8_overflow_check, label %error
13 |
14 | basic_8_overflow_check:
15 | %basic_8_overflowed = extractvalue { i8, i1 } %basic_8_res, 1
16 | br i1 %basic_8_overflowed, label %error, label %overflowing_8
17 |
18 | overflowing_8:
19 | %overflowing_8_res = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 u0x7C, i8 u0x7C)
20 | %overflowing_8_overflowed = extractvalue { i8, i1 } %overflowing_8_res, 1
21 | br i1 %overflowing_8_overflowed, label %underflowing_8, label %error
22 |
23 | underflowing_8:
24 | %underflowing_8_res = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 u0x8C, i8 u0x8C)
25 | %underflowing_8_overflowed = extractvalue { i8, i1 } %underflowing_8_res, 1
26 | br i1 %underflowing_8_overflowed, label %basic_16, label %error
27 |
28 | basic_16:
29 | %basic_16_res = call { i16, i1 } @llvm.sadd.with.overflow.i16(i16 52, i16 -10)
30 | %basic_16_sum = extractvalue { i16, i1 } %basic_16_res, 0
31 | %basic_16_sum_ok = icmp eq i16 %basic_16_sum, 42
32 | br i1 %basic_16_sum_ok, label %basic_16_overflow_check, label %error
33 |
34 | basic_16_overflow_check:
35 | %basic_16_overflowed = extractvalue { i16, i1 } %basic_16_res, 1
36 | br i1 %basic_16_overflowed, label %error, label %overflowing_16
37 |
38 | overflowing_16:
39 | %overflowing_16_res = call { i16, i1 } @llvm.sadd.with.overflow.i16(i16 u0x7CCC, i16 u0x7CCC)
40 | %overflowing_16_overflowed = extractvalue { i16, i1 } %overflowing_16_res, 1
41 | br i1 %overflowing_16_overflowed, label %underflowing_16, label %error
42 |
43 | underflowing_16:
44 | %underflowing_16_res = call { i16, i1 } @llvm.sadd.with.overflow.i16(i16 u0x8CCC, i16 u0x8CCC)
45 | %underflowing_16_overflowed = extractvalue { i16, i1 } %underflowing_16_res, 1
46 | br i1 %underflowing_16_overflowed, label %basic_32, label %error
47 |
48 | basic_32:
49 | %basic_32_res = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 52, i32 -10)
50 | %basic_32_sum = extractvalue { i32, i1 } %basic_32_res, 0
51 | %basic_32_sum_ok = icmp eq i32 %basic_32_sum, 42
52 | br i1 %basic_32_sum_ok, label %basic_32_overflow_check, label %error
53 |
54 | basic_32_overflow_check:
55 | %basic_32_overflowed = extractvalue { i32, i1 } %basic_32_res, 1
56 | br i1 %basic_32_overflowed, label %error, label %overflowing_32
57 |
58 | overflowing_32:
59 | %overflowing_32_res = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 u0x7CCCCCCC, i32 u0x7CCCCCCC)
60 | %overflowing_32_overflowed = extractvalue { i32, i1 } %overflowing_32_res, 1
61 | br i1 %overflowing_32_overflowed, label %underflowing_32, label %error
62 |
63 | underflowing_32:
64 | %underflowing_32_res = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 u0x8CCCCCCC, i32 u0x8CCCCCCC)
65 | %underflowing_32_overflowed = extractvalue { i32, i1 } %underflowing_32_res, 1
66 | br i1 %underflowing_32_overflowed, label %basic_64, label %error
67 |
68 | basic_64:
69 | %basic_64_res = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 52, i64 -10)
70 | %basic_64_sum = extractvalue { i64, i1 } %basic_64_res, 0
71 | %basic_64_sum_ok = icmp eq i64 %basic_64_sum, 42
72 | br i1 %basic_64_sum_ok, label %basic_64_overflow_check, label %error
73 |
74 | basic_64_overflow_check:
75 | %basic_64_overflowed = extractvalue { i64, i1 } %basic_64_res, 1
76 | br i1 %basic_64_overflowed, label %error, label %overflowing_64
77 |
78 | overflowing_64:
79 | %overflowing_64_res = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 u0x7CCCCCCCCCCCCCCC, i64 u0x7CCCCCCCCCCCCCCC)
80 | %overflowing_64_overflowed = extractvalue { i64, i1 } %overflowing_64_res, 1
81 | br i1 %overflowing_64_overflowed, label %underflowing_64, label %error
82 |
83 | underflowing_64:
84 | %underflowing_64_res = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 u0x8CCCCCCCCCCCCCCC, i64 u0x8CCCCCCCCCCCCCCC)
85 | %underflowing_64_overflowed = extractvalue { i64, i1 } %underflowing_64_res, 1
86 | br i1 %underflowing_64_overflowed, label %ok, label %error
87 |
88 | ok:
89 | ret i32 6
90 |
91 | error:
92 | %retVal = phi i32
93 | [ 81, %basic_8 ],
94 | [ 82, %basic_8_overflow_check ],
95 | [ 83, %overflowing_8 ],
96 | [ 84, %underflowing_8 ],
97 | [ 161, %basic_16 ],
98 | [ 162, %basic_16_overflow_check ],
99 | [ 163, %overflowing_16 ],
100 | [ 164, %underflowing_16 ],
101 | [ 321, %basic_32 ],
102 | [ 322, %basic_32_overflow_check ],
103 | [ 323, %overflowing_32 ],
104 | [ 324, %underflowing_32 ],
105 | [ 641, %basic_64 ],
106 | [ 642, %basic_64_overflow_check ],
107 | [ 643, %overflowing_64 ],
108 | [ 644, %underflowing_64 ]
109 | ret i32 %retVal
110 | }
111 |
--------------------------------------------------------------------------------
/test/ll_tests/test_intrinsic_sshl_sat.ll:
--------------------------------------------------------------------------------
1 | declare i8 @llvm.sshl.sat.i8(i8 %a, i8 %b)
2 | declare i16 @llvm.sshl.sat.i16(i16 %a, i16 %b)
3 | declare i32 @llvm.sshl.sat.i32(i32 %a, i32 %b)
4 | declare i64 @llvm.sshl.sat.i64(i64 %a, i64 %b)
5 |
6 | ; Function Attrs: noinline nounwind optnone uwtable
7 | define dso_local i32 @main() #0 {
8 | basic_8:
9 | %basic_8_shl = call i8 @llvm.sshl.sat.i8(i8 21, i8 1)
10 | %basic_8_ok = icmp eq i8 %basic_8_shl, 42
11 | br i1 %basic_8_ok, label %overflowing_8, label %error
12 |
13 | overflowing_8:
14 | %overflowing_8_shl = call i8 @llvm.sshl.sat.i8(i8 1, i8 7)
15 | %overflowing_8_ok = icmp eq i8 %overflowing_8_shl, 127
16 | br i1 %overflowing_8_ok, label %underflowing_8, label %error
17 |
18 | underflowing_8:
19 | %underflowing_8_shl = call i8 @llvm.sshl.sat.i8(i8 -1, i8 7)
20 | %underflowing_8_ok = icmp eq i8 %underflowing_8_shl, -128
21 | br i1 %underflowing_8_ok, label %basic_16, label %error
22 |
23 | basic_16:
24 | %basic_16_shl = call i16 @llvm.sshl.sat.i16(i16 21, i16 1)
25 | %basic_16_ok = icmp eq i16 %basic_16_shl, 42
26 | br i1 %basic_16_ok, label %overflowing_16, label %error
27 |
28 | overflowing_16:
29 | %overflowing_16_shl = call i16 @llvm.sshl.sat.i16(i16 1, i16 15)
30 | %overflowing_16_ok = icmp eq i16 %overflowing_16_shl, 32767
31 | br i1 %overflowing_16_ok, label %underflowing_16, label %error
32 |
33 | underflowing_16:
34 | %underflowing_16_shl = call i16 @llvm.sshl.sat.i16(i16 -1, i16 15)
35 | %underflowing_16_ok = icmp eq i16 %underflowing_16_shl, -32768
36 | br i1 %underflowing_16_ok, label %basic_32, label %error
37 |
38 | basic_32:
39 | %basic_32_shl = call i32 @llvm.sshl.sat.i32(i32 21, i32 1)
40 | %basic_32_ok = icmp eq i32 %basic_32_shl, 42
41 | br i1 %basic_32_ok, label %overflowing_32, label %error
42 |
43 | overflowing_32:
44 | %overflowing_32_shl = call i32 @llvm.sshl.sat.i32(i32 1, i32 31)
45 | %overflowing_32_ok = icmp eq i32 %overflowing_32_shl, 2147483647
46 | br i1 %overflowing_32_ok, label %underflowing_32, label %error
47 |
48 | underflowing_32:
49 | %underflowing_32_shl = call i32 @llvm.sshl.sat.i32(i32 -1, i32 31)
50 | %underflowing_32_ok = icmp eq i32 %underflowing_32_shl, -2147483648
51 | br i1 %underflowing_32_ok, label %basic_64, label %error
52 |
53 | basic_64:
54 | %basic_64_shl = call i64 @llvm.sshl.sat.i64(i64 21, i64 1)
55 | %basic_64_ok = icmp eq i64 %basic_64_shl, 42
56 | br i1 %basic_64_ok, label %overflowing_64, label %error
57 |
58 | overflowing_64:
59 | %overflowing_64_shl = call i64 @llvm.sshl.sat.i64(i64 1, i64 63)
60 | %overflowing_64_ok = icmp eq i64 %overflowing_64_shl, 9223372036854775807
61 | br i1 %overflowing_64_ok, label %underflowing_64, label %error
62 |
63 | underflowing_64:
64 | %underflowing_64_shl = call i64 @llvm.sshl.sat.i64(i64 -1, i64 63)
65 | %underflowing_64_ok = icmp eq i64 %underflowing_64_shl, -9223372036854775808
66 | br i1 %underflowing_64_ok, label %ok, label %error
67 |
68 | ok:
69 | ret i32 6
70 |
71 | error:
72 | %retVal = phi i32
73 | [ 81, %basic_8 ],
74 | [ 82, %overflowing_8 ],
75 | [ 83, %underflowing_8 ],
76 | [ 161, %basic_16 ],
77 | [ 162, %overflowing_16 ],
78 | [ 163, %underflowing_16 ],
79 | [ 321, %basic_32 ],
80 | [ 322, %overflowing_32 ],
81 | [ 323, %underflowing_32 ],
82 | [ 641, %basic_64 ],
83 | [ 642, %overflowing_64 ],
84 | [ 643, %underflowing_64 ]
85 | ret i32 %retVal
86 | }
87 |
--------------------------------------------------------------------------------
/test/ll_tests/test_intrinsic_uadd_sat.ll:
--------------------------------------------------------------------------------
1 | declare i8 @llvm.uadd.sat.i8(i8 %a, i8 %b)
2 | declare i16 @llvm.uadd.sat.i16(i16 %a, i16 %b)
3 | declare i32 @llvm.uadd.sat.i32(i32 %a, i32 %b)
4 | declare i64 @llvm.uadd.sat.i64(i64 %a, i64 %b)
5 |
6 | ; Function Attrs: noinline nounwind optnone uwtable
7 | define dso_local i32 @main() #0 {
8 | basic_8:
9 | %basic_8_sum = call i8 @llvm.uadd.sat.i8(i8 12, i8 30)
10 | %basic_8_ok = icmp eq i8 %basic_8_sum, 42
11 | br i1 %basic_8_ok, label %overflowing_8, label %error
12 |
13 | overflowing_8:
14 | %overflowing_8_sum = call i8 @llvm.uadd.sat.i8(i8 u0xCC, i8 u0xCC)
15 | %overflowing_8_ok = icmp eq i8 %overflowing_8_sum, 255
16 | br i1 %overflowing_8_ok, label %basic_16, label %error
17 |
18 | basic_16:
19 | %basic_16_sum = call i16 @llvm.uadd.sat.i16(i16 12, i16 30)
20 | %basic_16_ok = icmp eq i16 %basic_16_sum, 42
21 | br i1 %basic_16_ok, label %overflowing_16, label %error
22 |
23 | overflowing_16:
24 | %overflowing_16_sum = call i16 @llvm.uadd.sat.i16(i16 u0xCCCC, i16 u0xCCCC)
25 | %overflowing_16_ok = icmp eq i16 %overflowing_16_sum, 65535
26 | br i1 %overflowing_16_ok, label %basic_32, label %error
27 |
28 | basic_32:
29 | %basic_32_sum = call i32 @llvm.uadd.sat.i32(i32 12, i32 30)
30 | %basic_32_ok = icmp eq i32 %basic_32_sum, 42
31 | br i1 %basic_32_ok, label %overflowing_32, label %error
32 |
33 | overflowing_32:
34 | %overflowing_32_sum = call i32 @llvm.uadd.sat.i32(i32 u0xCCCCCCCC, i32 u0xCCCCCCCC)
35 | %overflowing_32_ok = icmp eq i32 %overflowing_32_sum, 4294967295
36 | br i1 %overflowing_32_ok, label %basic_64, label %error
37 |
38 | basic_64:
39 | %basic_64_sum = call i64 @llvm.uadd.sat.i64(i64 12, i64 30)
40 | %basic_64_ok = icmp eq i64 %basic_64_sum, 42
41 | br i1 %basic_64_ok, label %overflowing_64, label %error
42 |
43 | overflowing_64:
44 | %overflowing_64_sum = call i64 @llvm.uadd.sat.i64(i64 u0xCCCCCCCCCCCCCCCC, i64 u0xCCCCCCCCCCCCCCCC)
45 | %overflowing_64_ok = icmp eq i64 %overflowing_64_sum, 18446744073709551615
46 | br i1 %overflowing_64_ok, label %ok, label %error
47 |
48 | ok:
49 | ret i32 6
50 |
51 | error:
52 | %retVal = phi i32
53 | [ 81, %basic_8 ],
54 | [ 82, %overflowing_8 ],
55 | [ 161, %basic_16 ],
56 | [ 162, %overflowing_16 ],
57 | [ 321, %basic_32 ],
58 | [ 322, %overflowing_32 ],
59 | [ 641, %basic_64 ],
60 | [ 642, %overflowing_64 ]
61 | ret i32 %retVal
62 | }
63 |
--------------------------------------------------------------------------------
/test/ll_tests/test_intrinsic_uadd_with_overflow.ll:
--------------------------------------------------------------------------------
1 | declare {i8, i1} @llvm.uadd.with.overflow.i8(i8 %a, i8 %b)
2 | declare {i16, i1} @llvm.uadd.with.overflow.i16(i16 %a, i16 %b)
3 | declare {i32, i1} @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)
4 | declare {i64, i1} @llvm.uadd.with.overflow.i64(i64 %a, i64 %b)
5 |
6 | ; Function Attrs: noinline nounwind optnone uwtable
7 | define dso_local i32 @main() #0 {
8 | basic_8:
9 | %basic_8_res = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 12, i8 30)
10 | %basic_8_sum = extractvalue { i8, i1 } %basic_8_res, 0
11 | %basic_8_sum_ok = icmp eq i8 %basic_8_sum, 42
12 | br i1 %basic_8_sum_ok, label %basic_8_overflow_check, label %error
13 |
14 | basic_8_overflow_check:
15 | %basic_8_overflowed = extractvalue { i8, i1 } %basic_8_res, 1
16 | br i1 %basic_8_overflowed, label %error, label %overflowing_8
17 |
18 | overflowing_8:
19 | %overflowing_8_res = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 u0xCC, i8 u0xCC)
20 | %overflowing_8_overflowed = extractvalue { i8, i1 } %overflowing_8_res, 1
21 | br i1 %overflowing_8_overflowed, label %basic_16, label %error
22 |
23 | basic_16:
24 | %basic_16_res = call { i16, i1 } @llvm.uadd.with.overflow.i16(i16 12, i16 30)
25 | %basic_16_sum = extractvalue { i16, i1 } %basic_16_res, 0
26 | %basic_16_sum_ok = icmp eq i16 %basic_16_sum, 42
27 | br i1 %basic_16_sum_ok, label %basic_16_overflow_check, label %error
28 |
29 | basic_16_overflow_check:
30 | %basic_16_overflowed = extractvalue { i16, i1 } %basic_16_res, 1
31 | br i1 %basic_16_overflowed, label %error, label %overflowing_16
32 |
33 | overflowing_16:
34 | %overflowing_16_res = call { i16, i1 } @llvm.uadd.with.overflow.i16(i16 u0xCCCC, i16 u0xCCCC)
35 | %overflowing_16_overflowed = extractvalue { i16, i1 } %overflowing_16_res, 1
36 | br i1 %overflowing_16_overflowed, label %basic_32, label %error
37 |
38 | basic_32:
39 | %basic_32_res = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 12, i32 30)
40 | %basic_32_sum = extractvalue { i32, i1 } %basic_32_res, 0
41 | %basic_32_sum_ok = icmp eq i32 %basic_32_sum, 42
42 | br i1 %basic_32_sum_ok, label %basic_32_overflow_check, label %error
43 |
44 | basic_32_overflow_check:
45 | %basic_32_overflowed = extractvalue { i32, i1 } %basic_32_res, 1
46 | br i1 %basic_32_overflowed, label %error, label %overflowing_32
47 |
48 | overflowing_32:
49 | %overflowing_32_res = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 u0xCCCCCCCC, i32 u0xCCCCCCCC)
50 | %overflowing_32_overflowed = extractvalue { i32, i1 } %overflowing_32_res, 1
51 | br i1 %overflowing_32_overflowed, label %basic_64, label %error
52 |
53 | basic_64:
54 | %basic_64_res = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 12, i64 30)
55 | %basic_64_sum = extractvalue { i64, i1 } %basic_64_res, 0
56 | %basic_64_sum_ok = icmp eq i64 %basic_64_sum, 42
57 | br i1 %basic_64_sum_ok, label %basic_64_overflow_check, label %error
58 |
59 | basic_64_overflow_check:
60 | %basic_64_overflowed = extractvalue { i64, i1 } %basic_64_res, 1
61 | br i1 %basic_64_overflowed, label %error, label %overflowing_64
62 |
63 | overflowing_64:
64 | %overflowing_64_res = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 u0xCCCCCCCCCCCCCCCC, i64 u0xCCCCCCCCCCCCCCCC)
65 | %overflowing_64_overflowed = extractvalue { i64, i1 } %overflowing_64_res, 1
66 | br i1 %overflowing_64_overflowed, label %ok, label %error
67 |
68 | ok:
69 | ret i32 6
70 |
71 | error:
72 | %retVal = phi i32
73 | [ 81, %basic_8 ],
74 | [ 82, %basic_8_overflow_check ],
75 | [ 83, %overflowing_8 ],
76 | [ 161, %basic_16 ],
77 | [ 162, %basic_16_overflow_check ],
78 | [ 163, %overflowing_16 ],
79 | [ 321, %basic_32 ],
80 | [ 322, %basic_32_overflow_check ],
81 | [ 323, %overflowing_32 ],
82 | [ 641, %basic_64 ],
83 | [ 642, %basic_64_overflow_check ],
84 | [ 643, %overflowing_64 ]
85 | ret i32 %retVal
86 | }
87 |
--------------------------------------------------------------------------------
/test/ll_tests/test_intrinsic_ushl_sat.ll:
--------------------------------------------------------------------------------
1 | declare i8 @llvm.ushl.sat.i8(i8 %a, i8 %b)
2 | declare i16 @llvm.ushl.sat.i16(i16 %a, i16 %b)
3 | declare i32 @llvm.ushl.sat.i32(i32 %a, i32 %b)
4 | declare i64 @llvm.ushl.sat.i64(i64 %a, i64 %b)
5 |
6 | ; Function Attrs: noinline nounwind optnone uwtable
7 | define dso_local i32 @main() #0 {
8 | basic_8:
9 | %basic_8_shl = call i8 @llvm.ushl.sat.i8(i8 21, i8 1)
10 | %basic_8_ok = icmp eq i8 %basic_8_shl, 42
11 | br i1 %basic_8_ok, label %overflowing_8, label %error
12 |
13 | overflowing_8:
14 | %overflowing_8_shl = call i8 @llvm.ushl.sat.i8(i8 2, i8 7)
15 | %overflowing_8_ok = icmp eq i8 %overflowing_8_shl, 255
16 | br i1 %overflowing_8_ok, label %basic_16, label %error
17 |
18 | basic_16:
19 | %basic_16_shl = call i16 @llvm.ushl.sat.i16(i16 21, i16 1)
20 | %basic_16_ok = icmp eq i16 %basic_16_shl, 42
21 | br i1 %basic_16_ok, label %overflowing_16, label %error
22 |
23 | overflowing_16:
24 | %overflowing_16_shl = call i16 @llvm.ushl.sat.i16(i16 2, i16 15)
25 | %overflowing_16_ok = icmp eq i16 %overflowing_16_shl, 65535
26 | br i1 %overflowing_16_ok, label %basic_32, label %error
27 |
28 | basic_32:
29 | %basic_32_shl = call i32 @llvm.ushl.sat.i32(i32 21, i32 1)
30 | %basic_32_ok = icmp eq i32 %basic_32_shl, 42
31 | br i1 %basic_32_ok, label %overflowing_32, label %error
32 |
33 | overflowing_32:
34 | %overflowing_32_shl = call i32 @llvm.ushl.sat.i32(i32 2, i32 31)
35 | %overflowing_32_ok = icmp eq i32 %overflowing_32_shl, 4294967295
36 | br i1 %overflowing_32_ok, label %basic_64, label %error
37 |
38 | basic_64:
39 | %basic_64_shl = call i64 @llvm.ushl.sat.i64(i64 21, i64 1)
40 | %basic_64_ok = icmp eq i64 %basic_64_shl, 42
41 | br i1 %basic_64_ok, label %overflowing_64, label %error
42 |
43 | overflowing_64:
44 | %overflowing_64_shl = call i64 @llvm.ushl.sat.i64(i64 2, i64 63)
45 | %overflowing_64_ok = icmp eq i64 %overflowing_64_shl, 18446744073709551615
46 | br i1 %overflowing_64_ok, label %ok, label %error
47 |
48 | ok:
49 | ret i32 6
50 |
51 | error:
52 | %retVal = phi i32
53 | [ 81, %basic_8 ],
54 | [ 82, %overflowing_8 ],
55 | [ 161, %basic_16 ],
56 | [ 162, %overflowing_16 ],
57 | [ 321, %basic_32 ],
58 | [ 322, %overflowing_32 ],
59 | [ 641, %basic_64 ],
60 | [ 642, %overflowing_64 ]
61 | ret i32 %retVal
62 | }
63 |
--------------------------------------------------------------------------------
/test/ll_tests/test_min_max_intrinsics.ll:
--------------------------------------------------------------------------------
1 | declare i64 @llvm.smin.i64(i64, i64)
2 | declare i64 @llvm.smax.i64(i64, i64)
3 | declare i64 @llvm.umin.i64(i64, i64)
4 | declare i64 @llvm.umax.i64(i64, i64)
5 |
6 | define dso_local i32 @main() #0 {
7 | smin.check:
8 | %smin.res = call i64 @llvm.smin.i64(i64 6, i64 -6)
9 | %smin.ok = icmp eq i64 %smin.res, -6
10 | br i1 %smin.ok, label %smax.check, label %error
11 |
12 | smax.check:
13 | %smax.res = call i64 @llvm.smax.i64(i64 6, i64 -6)
14 | %smax.ok = icmp eq i64 %smax.res, 6
15 | br i1 %smax.ok, label %umin.check, label %error
16 |
17 | umin.check:
18 | %umin.res = call i64 @llvm.umin.i64(i64 6, i64 -6)
19 | %umin.ok = icmp eq i64 %umin.res, 6
20 | br i1 %umin.ok, label %umax.check, label %error
21 |
22 | umax.check:
23 | %umax.res = call i64 @llvm.umax.i64(i64 6, i64 -6)
24 | %umax.ok = icmp eq i64 %umax.res, -6
25 | br i1 %umax.ok, label %ok, label %error
26 |
27 |
28 | ok:
29 | ret i32 6
30 |
31 | error:
32 | %retVal = phi i32
33 | [ 20, %smin.check ],
34 | [ 21, %smax.check ],
35 | [ 22, %umin.check ],
36 | [ 23, %umax.check ]
37 | ret i32 %retVal
38 | }
39 |
--------------------------------------------------------------------------------
/test/ll_tests/test_not_power_2_ints.ll:
--------------------------------------------------------------------------------
1 | ; Function Attrs: noinline nounwind optnone uwtable
2 | define dso_local i32 @main() #0 {
3 | %a = alloca i3, align 4
4 | store i3 7, i3* %a, align 4
5 | %b = load i3, i3* %a, align 4
6 | %c = sext i3 %b to i17
7 | %d = alloca i17, align 4
8 | store i17 6, i17* %d, align 4
9 | %e = load i17, i17* %d, align 4
10 | %f = and i17 %c, %e
11 | %g = zext i17 %f to i32
12 | ret i32 %g
13 | }
14 |
--------------------------------------------------------------------------------
/test/ll_tests/test_simple_return.ll:
--------------------------------------------------------------------------------
1 | ; Function Attrs: noinline nounwind optnone uwtable
2 | define dso_local i32 @main() #0 {
3 | ret i32 6
4 | }
5 |
--------------------------------------------------------------------------------
/test/ll_tests/test_trap.ll:
--------------------------------------------------------------------------------
1 | declare void @llvm.trap() cold noreturn nounwind
2 |
3 | define dso_local i32 @main() #0 {
4 | ret i32 6
5 | }
6 |
7 | define dso_local void @this_should_not_happen() {
8 | call void @llvm.trap()
9 | unreachable
10 | }
11 |
--------------------------------------------------------------------------------
/test/selectionsort/main.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | int array[100], n, c, d, position, swap;
5 |
6 | printf("Enter number of elements\n");
7 | scanf("%d", &n);
8 |
9 | printf("Enter %d integers\n", n);
10 |
11 | for (c = 0; c < n; c++)
12 | scanf("%d", &array[c]);
13 |
14 | for (c = 0; c < (n - 1); c++) {
15 | position = c;
16 |
17 | for (d = c + 1; d < n; d++) {
18 | if (array[position] > array[d])
19 | position = d;
20 | }
21 | if (position != c) {
22 | swap = array[c];
23 | array[c] = array[position];
24 | array[position] = swap;
25 | }
26 | }
27 |
28 | printf("Sorted list in ascending order:\n");
29 |
30 | for (c = 0; c < n; c++)
31 | printf("%d\n", array[c]);
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/test/test_cbe.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | import os
3 | import io
4 | import re
5 | from glob import glob
6 | from subprocess import call, Popen, PIPE
7 | import pytest
8 |
9 | USE_MSVC = os.name == 'nt'
10 |
11 | # This configuration assumes that you are using the system LLVM.
12 | # And that you created a subdir "build" to compile this from.
13 | # In other cases, please modify the lines below.
14 | TEST_DIR = os.path.dirname(os.path.abspath(__file__))
15 |
16 | LLVM_TOOL_DIR = os.environ.get(
17 | 'LLVMToolDir',
18 | os.path.join(TEST_DIR, '..', 'build', 'tools', 'llvm-cbe'))
19 |
20 | LLVM_CBE_PATH = os.path.join(LLVM_TOOL_DIR, 'llvm-cbe')
21 | LLI_PATH = 'lli'
22 |
23 | CBE_FLAGS = [
24 | # Harder to get right than early declarations, so more value to test it.
25 | '-cbe-declare-locals-late'
26 | ]
27 |
28 | COMMON_CFLAGS = [
29 | '-Iinclude/',
30 | '-Wall',
31 | '-Wno-unused-function',
32 | '-Wno-unused-variable',
33 | '-Wno-uninitialized',
34 | '-Werror',
35 | ]
36 |
37 | GCC = 'gcc'
38 | GCCFLAGS = COMMON_CFLAGS + [
39 | '-Wno-error=unused-but-set-variable',
40 | '-Wno-unused-but-set-variable',
41 | '-Wno-builtin-declaration-mismatch',
42 | '-Wno-error=builtin-declaration-mismatch',
43 | '-Wno-discarded-qualifiers',
44 | '-Wno-packed-not-aligned',
45 | '-latomic',
46 | ]
47 |
48 | CLANG = 'clang'
49 | CLANGPP = 'clang++'
50 | CLANGFLAGS = COMMON_CFLAGS + [
51 | '-Wno-error=unused-variable',
52 | '-Wno-unused-variable',
53 | '-Wno-pointer-to-int-cast',
54 | '-Wno-unused-but-set-variable',
55 | ]
56 |
57 | MSVC = 'cl'
58 | MSVCFLAGS = [
59 | '/std:c17', # Use C17 standard.
60 | '/experimental:c11atomics', # Enable C11 atomics support.
61 | '/W4', # "Informational" warning level.
62 | '/WX', # Warnings as errors.
63 | '/wd4090', # different 'const' qualifiers.
64 | '/wd4100', # unreferenced formal parameter
65 | '/wd4101', # unreferenced local variable
66 | '/wd4115', # Type declared in paren: In C this places it in the global namespace, which is what we want.
67 | '/wd4132', # const object should be initialized
68 | '/wd4189', # Variable is initialized but never refernced.
69 | '/wd4223', # non-lvalue array converted to pointer.
70 | '/wd4245', # Signed/unsigned mismatch.
71 | '/nologo',
72 | ]
73 |
74 | # exit code used by tests to indicate success
75 | TEST_SUCCESS_EXIT_CODE = 6
76 | # exit code used by tests to indicate xfail
77 | TEST_XFAIL_EXIT_CODE = 25
78 |
79 |
80 | def check_no_output(args, cwd):
81 | proc = Popen(args, cwd=cwd, stdout=PIPE, stderr=PIPE)
82 | out, err = proc.communicate()
83 |
84 | if (out and not USE_MSVC) or err or proc.returncode:
85 | out = out.decode("utf-8")
86 | err = err.decode("utf-8")
87 |
88 | msg_stream = io.StringIO()
89 | print(f"Got unexpected output or exit code from process", file=msg_stream)
90 |
91 | print(f"args: {args}", file=msg_stream)
92 | print(file=msg_stream)
93 |
94 | print(f"exit code: {proc.returncode}", file=msg_stream)
95 | print(file=msg_stream)
96 |
97 | print(f"stdout:", file=msg_stream)
98 | print(out, file=msg_stream)
99 | print(file=msg_stream)
100 |
101 | print(f"stderr:", file=msg_stream)
102 | print(err, file=msg_stream)
103 | print(file=msg_stream)
104 |
105 | raise Exception(msg_stream.getvalue())
106 |
107 |
108 | def compile_gcc(c_filename, output_filename, flags=None):
109 | flags = flags or []
110 | try:
111 | check_no_output([GCC, c_filename, '-o', output_filename] + GCCFLAGS + flags, os.path.dirname(output_filename))
112 | except Exception as e:
113 | msg_stream = io.StringIO()
114 | with open(c_filename, 'r') as f:
115 | print(f"source code:", file=msg_stream)
116 | print(f.read(), file=msg_stream)
117 | print(file=msg_stream)
118 | raise Exception(msg_stream.getvalue()) from e
119 |
120 | return output_filename
121 |
122 |
123 | def compile_msvc(c_filename, output_filename, flags=None):
124 | flags = flags or []
125 | atomic_library_file = os.path.join(TEST_DIR, '..', 'runtime', 'windows', 'atomics.c')
126 | check_no_output([MSVC, c_filename, atomic_library_file, '/Fe:' + str(output_filename)] + MSVCFLAGS + flags, os.path.dirname(output_filename))
127 | return output_filename
128 |
129 |
130 | def compile_clang(c_filename, output_filename, flags=None, cplusplus=False):
131 | flags = flags or []
132 | check_no_output([CLANGPP if cplusplus else CLANG, c_filename, '-o', output_filename] + CLANGFLAGS + flags, os.path.dirname(output_filename))
133 | return output_filename
134 |
135 |
136 | def compile_to_ir(c_filename, ir_filename, flags=None, cplusplus=False):
137 | flags = list(flags or [])
138 | flags += ['-S', '-emit-llvm']
139 | return compile_clang(
140 | c_filename, ir_filename, flags=flags, cplusplus=cplusplus)
141 |
142 |
143 | def run_llvm_cbe(ir_filename, c_filename):
144 | check_no_output([LLVM_CBE_PATH, ir_filename, *CBE_FLAGS, '-o', c_filename], os.path.dirname(ir_filename))
145 | return c_filename
146 |
147 |
148 | def collect_tests(base_dir, extensions):
149 | for dirname, _, filenames in os.walk(base_dir):
150 | for fn in filenames:
151 | if fn.startswith('test_') and fn.endswith(extensions):
152 | yield os.path.join(dirname, fn)
153 |
154 |
155 | def get_test_name_from_filename(test_path):
156 | return os.path.splitext(os.path.basename(test_path))[0]
157 |
158 |
159 | def check_xfail(test_path):
160 | code = open(test_path).read()
161 | m = re.search(r'(?m)^(//|;) xfail: (.+)', code)
162 | if m:
163 | pytest.xfail(m.group(2))
164 |
165 |
166 | def get_extra_args(test_path):
167 | code = open(test_path).read()
168 | if USE_MSVC:
169 | m = re.search(r'(?m)^(//|;) msvc_extra_args: (.+)', code)
170 | else:
171 | m = re.search(r'(?m)^(//|;) gcc_extra_args: (.+)', code)
172 | if m:
173 | return m.group(2).split(' ')
174 | else:
175 | return []
176 |
177 |
178 | @pytest.mark.parametrize(
179 | 'cflags',
180 | [['-O0'], ['-O1'], ['-O2'], ['-O3']],
181 | ids=lambda flags: ' '.join(flags)
182 | )
183 | @pytest.mark.parametrize(
184 | 'test_filename',
185 | collect_tests(TEST_DIR, ('.c', '.cpp')),
186 | ids=get_test_name_from_filename,
187 | )
188 | def test_consistent_return_value_c(test_filename, tmpdir, cflags):
189 | """
190 | Compile and execute a C or C++ file with clang, and compare its exit code
191 | with the exit code when compiled with llvm-cbe followed by gcc.
192 |
193 | Also, the exit code must be TEST_SUCCESS_EXIT_CODE for success or
194 | TEST_XFAIL_EXIT_CODE or expected failures.
195 | """
196 |
197 | check_xfail(test_filename)
198 |
199 | cplusplus = test_filename.endswith('.cpp')
200 |
201 | # make sure CBE doesn't have any errors before trying to compile
202 | # executables
203 | ir = compile_to_ir(
204 | test_filename, tmpdir / 'ir.ll', flags=cflags, cplusplus=cplusplus)
205 |
206 | regular_exe = compile_clang(
207 | test_filename,
208 | tmpdir / 'regular.exe',
209 | flags=cflags,
210 | cplusplus=cplusplus)
211 | regular_retval = call([regular_exe])
212 | print('regular executable returned', regular_retval)
213 | assert regular_retval in [TEST_SUCCESS_EXIT_CODE, TEST_XFAIL_EXIT_CODE]
214 |
215 | cbe_c = run_llvm_cbe(ir, tmpdir / 'cbe.c')
216 | cflags = cflags + get_extra_args(test_filename)
217 | if USE_MSVC:
218 | map_flags = {'-O3': '-O2', '-O0': '-Od'}
219 | cbe_exe = compile_msvc(cbe_c, tmpdir / 'cbe.exe', flags=[f if f not in map_flags else map_flags[f] for f in cflags])
220 | else:
221 | cbe_exe = compile_gcc(cbe_c, tmpdir / 'cbe.exe', flags=cflags)
222 | cbe_retval = call([cbe_exe])
223 | print('cbe output returned', cbe_retval)
224 | assert cbe_retval == regular_retval
225 |
226 |
227 | @pytest.mark.parametrize(
228 | 'test_filename',
229 | collect_tests(TEST_DIR, ('.ll', )),
230 | ids=get_test_name_from_filename,
231 | )
232 | def test_consistent_return_value_ll(test_filename, tmpdir):
233 | """
234 | Execute an LLVM IR file with lli, and compare its exit code with the exit
235 | code when compiled with llvm-cbe followed by gcc.
236 |
237 | Also, the exit code must be TEST_SUCCESS_EXIT_CODE for success or
238 | TEST_XFAIL_EXIT_CODE or expected failures.
239 | """
240 |
241 | check_xfail(test_filename)
242 |
243 | lli_retval = call([LLI_PATH, test_filename])
244 | print('lli exit code was', lli_retval)
245 | assert lli_retval in [TEST_SUCCESS_EXIT_CODE, TEST_XFAIL_EXIT_CODE]
246 |
247 | cbe_c = run_llvm_cbe(test_filename, tmpdir / 'cbe.c')
248 | cflags = get_extra_args(test_filename)
249 | if USE_MSVC:
250 | cbe_exe = compile_msvc(cbe_c, tmpdir / 'cbe.exe', flags=cflags)
251 | else:
252 | cbe_exe = compile_gcc(cbe_c, tmpdir / 'cbe.exe', flags=cflags)
253 | cbe_retval = call([cbe_exe])
254 | print('cbe output returned', cbe_retval)
255 | assert cbe_retval == lli_retval
256 |
257 |
258 | if __name__ == '__main__':
259 | raise SystemExit("run me using pytest")
260 |
--------------------------------------------------------------------------------
/tools/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | add_subdirectory(llvm-cbe)
2 |
--------------------------------------------------------------------------------
/tools/llvm-cbe/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # Support plugins.
2 | set(LLVM_NO_DEAD_STRIP 1)
3 |
4 | if (NOT USE_SYSTEM_LLVM)
5 | set(LLVM_LINK_COMPONENTS
6 | ${LLVM_TARGETS_TO_BUILD}
7 | Analysis
8 | AsmParser
9 | AsmPrinter
10 | BitReader
11 | CBackendCodeGen
12 | CBackendInfo
13 | CodeGen
14 | Core
15 | IRReader
16 | MC
17 | ScalarOpts
18 | SelectionDAG
19 | Support
20 | Target
21 | )
22 |
23 |
24 | add_llvm_tool(llvm-cbe
25 | llvm-cbe.cpp
26 | )
27 | export_executable_symbols(llvm-cbe)
28 | endif()
29 |
30 | if(USE_SYSTEM_LLVM)
31 | add_executable(llvm-cbe
32 | llvm-cbe.cpp
33 | )
34 | llvm_map_components_to_libnames(llvm_libs
35 | CBackendCodeGen
36 | CBackendInfo
37 | )
38 | target_link_libraries(llvm-cbe LLVM ${llvm_libs})
39 | endif()
40 |
--------------------------------------------------------------------------------
/tools/llvm-cbe/LLVMBuild.txt:
--------------------------------------------------------------------------------
1 | ;===- ./tools/llc/LLVMBuild.txt --------------------------------*- Conf -*--===;
2 | ;
3 | ; The LLVM Compiler Infrastructure
4 | ;
5 | ; This file is distributed under the University of Illinois Open Source
6 | ; License. See LICENSE.TXT for details.
7 | ;
8 | ;===------------------------------------------------------------------------===;
9 | ;
10 | ; This is an LLVMBuild description file for the components in this subdirectory.
11 | ;
12 | ; For more information on the LLVMBuild system, please see:
13 | ;
14 | ; http://llvm.org/docs/LLVMBuild.html
15 | ;
16 | ;===------------------------------------------------------------------------===;
17 |
18 | [component_0]
19 | type = Tool
20 | name = llc
21 | parent = Tools
22 | required_libraries =
23 | Analysis
24 | AsmParser
25 | AsmPrinter
26 | BitReader
27 | CBackendCodeGen
28 | CBackendInfo
29 | CodeGen
30 | Core
31 | IRReader
32 | MC
33 | ScalarOpts
34 | SelectionDAG
35 | Support
36 | Target
37 | all-targets
38 |
--------------------------------------------------------------------------------
/tools/llvm-cbe/llvm-cbe.cpp:
--------------------------------------------------------------------------------
1 | //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
2 | //
3 | // The LLVM Compiler Infrastructure
4 | //
5 | // This file is distributed under the University of Illinois Open Source
6 | // License. See LICENSE.TXT for details.
7 | //
8 | //===----------------------------------------------------------------------===//
9 | //
10 | // This is the llc code generator driver. It provides a convenient
11 | // command-line interface for generating native assembly-language code
12 | // or C code, given LLVM bitcode.
13 | //
14 | //===----------------------------------------------------------------------===//
15 |
16 | #include "llvm/Analysis/TargetLibraryInfo.h"
17 | #include "llvm/Analysis/TargetTransformInfo.h"
18 | #include "llvm/CodeGen/CommandFlags.h"
19 | #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
20 | #include "llvm/CodeGen/LinkAllCodegenComponents.h"
21 | #include "llvm/CodeGen/TargetPassConfig.h"
22 | #include "llvm/IR/DataLayout.h"
23 | #include "llvm/IR/IRPrintingPasses.h"
24 | #include "llvm/IR/LLVMContext.h"
25 | #include "llvm/IR/LegacyPassManager.h"
26 | #include "llvm/IR/Module.h"
27 | #include "llvm/IRReader/IRReader.h"
28 | #include "llvm/InitializePasses.h"
29 | #include "llvm/MC/MCTargetOptionsCommandFlags.h"
30 | #include "llvm/MC/TargetRegistry.h"
31 | #include "llvm/Pass.h"
32 | #include "llvm/Support/CommandLine.h"
33 | #include "llvm/Support/Debug.h"
34 | #include "llvm/Support/FileSystem.h"
35 | #include "llvm/Support/FormattedStream.h"
36 | #include "llvm/Support/ManagedStatic.h"
37 | #include "llvm/Support/PluginLoader.h"
38 | #include "llvm/Support/PrettyStackTrace.h"
39 | #include "llvm/Support/Signals.h"
40 | #include "llvm/Support/SourceMgr.h"
41 | #include "llvm/Support/TargetSelect.h"
42 | #include "llvm/Support/ToolOutputFile.h"
43 | #include "llvm/Target/TargetMachine.h"
44 | #include "llvm/TargetParser/Host.h"
45 | #include "llvm/TargetParser/SubtargetFeature.h"
46 | #include
47 | #include
48 | using namespace llvm;
49 |
50 | static codegen::RegisterCodeGenFlags CGF;
51 |
52 | extern "C" void LLVMInitializeCBackendTarget();
53 | extern "C" void LLVMInitializeCBackendTargetInfo();
54 | extern "C" void LLVMInitializeCBackendTargetMC();
55 |
56 | // General options for llc. Other pass-specific options are specified
57 | // within the corresponding llc passes, and target-specific options
58 | // and back-end code generation options are specified with the target machine.
59 | //
60 | static cl::opt
61 | InputFilename(cl::Positional, cl::desc(""), cl::init("-"));
62 |
63 | static cl::opt OutputFilename("o", cl::desc("Output filename"),
64 | cl::value_desc("filename"));
65 |
66 | static cl::opt
67 | TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
68 | cl::value_desc("N"),
69 | cl::desc("Repeat compilation N times for timing"));
70 |
71 | // Determine optimization level.
72 | static cl::opt
73 | OptLevel("O",
74 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
75 | "(default = '-O2')"),
76 | cl::Prefix, cl::ZeroOrMore, cl::init(' '));
77 |
78 | static cl::opt
79 | TargetTriple("mtriple", cl::desc("Override target triple for module"));
80 |
81 | cl::opt NoVerify("disable-verify", cl::Hidden,
82 | cl::desc("Do not verify input module"));
83 |
84 | static int compileModule(char **, LLVMContext &);
85 |
86 | // GetFileNameRoot - Helper function to get the basename of a filename.
87 | static inline std::string GetFileNameRoot(const std::string &InputFilename) {
88 | std::string IFN = InputFilename;
89 | std::string outputFilename;
90 | int Len = IFN.length();
91 | if ((Len > 2) && IFN[Len - 3] == '.' &&
92 | ((IFN[Len - 2] == 'b' && IFN[Len - 1] == 'c') ||
93 | (IFN[Len - 2] == 'l' && IFN[Len - 1] == 'l'))) {
94 | outputFilename = std::string(IFN.begin(), IFN.end() - 3); // s/.bc/.s/
95 | } else {
96 | outputFilename = IFN;
97 | }
98 | return outputFilename;
99 | }
100 |
101 | static ToolOutputFile *GetOutputStream(const char *TargetName,
102 | Triple::OSType OS,
103 | const char *ProgName) {
104 | // If we don't yet have an output filename, make one.
105 | if (OutputFilename.empty()) {
106 | if (InputFilename == "-")
107 | OutputFilename = "-";
108 | else {
109 | OutputFilename = GetFileNameRoot(InputFilename);
110 |
111 | switch (codegen::getFileType()) {
112 | case CodeGenFileType::AssemblyFile:
113 | if (TargetName[0] == 'c') {
114 | if (TargetName[1] == 0)
115 | OutputFilename += ".cbe.c";
116 | else if (TargetName[1] == 'p' && TargetName[2] == 'p')
117 | OutputFilename += ".cpp";
118 | else
119 | OutputFilename += ".s";
120 | } else
121 | OutputFilename += ".s";
122 | break;
123 |
124 | case CodeGenFileType::ObjectFile:
125 | if (OS == Triple::Win32)
126 | OutputFilename += ".obj";
127 | else
128 | OutputFilename += ".o";
129 | break;
130 | case CodeGenFileType::Null:
131 | OutputFilename += ".null";
132 | break;
133 | }
134 | }
135 | }
136 |
137 | // Decide if we need "binary" output.
138 | bool Binary = false;
139 | switch (codegen::getFileType()) {
140 | case CodeGenFileType::AssemblyFile:
141 | break;
142 | case CodeGenFileType::ObjectFile:
143 | case CodeGenFileType::Null:
144 | Binary = true;
145 | break;
146 | }
147 |
148 | // Open the file.
149 | std::error_code error;
150 | sys::fs::OpenFlags OpenFlags = sys::fs::OF_None;
151 | if (Binary)
152 | OpenFlags |= sys::fs::OF_Text;
153 | ToolOutputFile *FDOut =
154 | new ToolOutputFile(OutputFilename.c_str(), error, OpenFlags);
155 | if (error) {
156 | errs() << error.message() << '\n';
157 | delete FDOut;
158 | return 0;
159 | }
160 |
161 | return FDOut;
162 | }
163 |
164 | static LLVMContext TheContext;
165 |
166 | // main - Entry point for the llc compiler.
167 | //
168 | int main(int argc, char **argv) {
169 | sys::PrintStackTraceOnErrorSignal(argv[0]);
170 | PrettyStackTraceProgram X(argc, argv);
171 |
172 | // Enable debug stream buffering.
173 | EnableDebugBuffering = true;
174 |
175 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
176 |
177 | // Initialize targets first, so that --version shows registered targets.
178 | InitializeAllTargets();
179 | InitializeAllTargetMCs();
180 | InitializeAllAsmPrinters();
181 | InitializeAllAsmParsers();
182 |
183 | LLVMInitializeCBackendTarget();
184 | LLVMInitializeCBackendTargetInfo();
185 | LLVMInitializeCBackendTargetMC();
186 |
187 | // Initialize codegen and IR passes used by llc so that the -print-after,
188 | // -print-before, and -stop-after options work.
189 | PassRegistry *Registry = PassRegistry::getPassRegistry();
190 | initializeCore(*Registry);
191 | initializeCodeGen(*Registry);
192 | initializeLoopStrengthReducePass(*Registry);
193 | initializeLowerIntrinsicsPass(*Registry);
194 | initializeUnreachableBlockElimLegacyPassPass(*Registry);
195 |
196 | // Register the target printer for --version.
197 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
198 |
199 | cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
200 |
201 | // Compile the module TimeCompilations times to give better compile time
202 | // metrics.
203 | for (unsigned I = TimeCompilations; I; --I)
204 | if (int RetVal = compileModule(argv, TheContext))
205 | return RetVal;
206 | return 0;
207 | }
208 |
209 | static int compileModule(char **argv, LLVMContext &Context) {
210 | // Load the module to be compiled...
211 | SMDiagnostic Err;
212 |
213 | std::unique_ptr M;
214 |
215 | Module *mod = 0;
216 | Triple TheTriple;
217 |
218 | auto MAttrs = codegen::getMAttrs();
219 | bool SkipModule = codegen::getMCPU() == "help" ||
220 | (!MAttrs.empty() && MAttrs.front() == "help");
221 |
222 | // If user just wants to list available options, skip module loading
223 | if (!SkipModule) {
224 | M = parseIRFile(InputFilename, Err, Context);
225 | mod = M.get();
226 | if (mod == 0) {
227 | Err.print(argv[0], errs());
228 | return 1;
229 | }
230 |
231 | // If we are supposed to override the target triple, do so now.
232 | if (!TargetTriple.empty())
233 | mod->setTargetTriple(Triple::normalize(TargetTriple));
234 | TheTriple = Triple(mod->getTargetTriple());
235 | } else {
236 | TheTriple = Triple(Triple::normalize(TargetTriple));
237 | }
238 |
239 | if (TheTriple.getTriple().empty())
240 | TheTriple.setTriple(sys::getDefaultTargetTriple());
241 |
242 | // Get the target specific parser.
243 | std::string Error;
244 | // Override MArch
245 | // codegen::getMArch() = "c";
246 | const std::string MArch = "c";
247 | const Target *TheTarget =
248 | TargetRegistry::lookupTarget(MArch, TheTriple, Error);
249 | if (!TheTarget) {
250 | errs() << argv[0] << ": " << Error << "\n";
251 | return 1;
252 | }
253 |
254 | // Package up features to be passed to target/subtarget
255 | std::string FeaturesStr;
256 | if (MAttrs.size()) {
257 | SubtargetFeatures Features;
258 | for (unsigned i = 0; i != MAttrs.size(); ++i)
259 | Features.AddFeature(MAttrs[i]);
260 | FeaturesStr = Features.getString();
261 | }
262 |
263 | CodeGenOptLevel OLvl = CodeGenOptLevel::Default;
264 |
265 | switch (OptLevel) {
266 | default:
267 | errs() << argv[0] << ": invalid optimization level.\n";
268 | return 1;
269 | case ' ':
270 | break;
271 | case '0':
272 | OLvl = CodeGenOptLevel::None;
273 | break;
274 | case '1':
275 | OLvl = CodeGenOptLevel::Less;
276 | break;
277 | case '2':
278 | OLvl = CodeGenOptLevel::Default;
279 | break;
280 | case '3':
281 | OLvl = CodeGenOptLevel::Aggressive;
282 | break;
283 | }
284 |
285 | TargetOptions Options;
286 | Options.AllowFPOpFusion = codegen::getFuseFPOps();
287 | Options.UnsafeFPMath = codegen::getEnableUnsafeFPMath();
288 | Options.NoInfsFPMath = codegen::getEnableNoInfsFPMath();
289 | Options.NoNaNsFPMath = codegen::getEnableNoNaNsFPMath();
290 | Options.HonorSignDependentRoundingFPMathOption =
291 | codegen::getEnableHonorSignDependentRoundingFPMath();
292 | if (codegen::getFloatABIForCalls() != FloatABI::Default)
293 | Options.FloatABIType = codegen::getFloatABIForCalls();
294 | Options.NoZerosInBSS = codegen::getDontPlaceZerosInBSS();
295 | Options.GuaranteedTailCallOpt = codegen::getEnableGuaranteedTailCallOpt();
296 |
297 | // Jackson Korba 9/30/14
298 | // OwningPtr
299 | std::unique_ptr target(TheTarget->createTargetMachine(
300 | TheTriple.getTriple(), codegen::getMCPU(), FeaturesStr, Options,
301 | llvm::codegen::getRelocModel()));
302 | assert(target.get() && "Could not allocate target machine!");
303 | assert(mod && "Should have exited after outputting help!");
304 | TargetMachine &Target = *target.get();
305 |
306 | // Disable .loc support for older OS X versions.
307 | if (TheTriple.isMacOSX() && TheTriple.isMacOSXVersionLT(10, 6)) {
308 | }
309 | // TODO: Find a replacement to this function
310 | /* Greg Simpson 6-09-13
311 | no member named setMCUseLoc
312 | removed statement
313 | Target.setMCUseLoc(false); */
314 |
315 | // Jackson Korba 9/30/14
316 | std::unique_ptr Out(
317 | GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]));
318 | if (!Out)
319 | return 1;
320 |
321 | // Build up all of the passes that we want to do to the module.
322 | legacy::PassManager PM;
323 |
324 | // Add an appropriate TargetLibraryInfo pass for the module's triple.
325 | TargetLibraryInfoWrapperPass *TLI =
326 | new TargetLibraryInfoWrapperPass(TheTriple);
327 | PM.add(TLI);
328 |
329 | // Add intenal analysis passes from the target machine.
330 | PM.add(createTargetTransformInfoWrapperPass(Target.getTargetIRAnalysis()));
331 |
332 | if (mc::getExplicitRelaxAll()) {
333 | if (codegen::getFileType() != CodeGenFileType::ObjectFile)
334 | errs() << argv[0]
335 | << ": warning: ignoring -mc-relax-all because filetype != obj\n";
336 | }
337 |
338 | // Ask the target to add backend passes as necessary.
339 | if (Target.addPassesToEmitFile(PM, Out->os(), nullptr, codegen::getFileType(),
340 | NoVerify)) {
341 | errs() << argv[0] << ": target does not support generation of this"
342 | << " file type!\n";
343 | return 1;
344 | }
345 |
346 | // Before executing passes, print the final values of the LLVM options.
347 | cl::PrintOptionValues();
348 |
349 | PM.run(*mod);
350 |
351 | // Declare success.
352 | Out->keep();
353 |
354 | return 0;
355 | }
356 |
--------------------------------------------------------------------------------
/unittests/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | add_custom_target(CBEUnitTests)
2 | set_target_properties(CBEUnitTests PROPERTIES FOLDER "Tests")
3 |
4 | if (NOT USE_SYSTEM_LLVM)
5 | set(LLVM_LINK_COMPONENTS
6 | Analysis
7 | CBackendCodeGen
8 | CBackendInfo
9 | CodeGen
10 | Core
11 | MC
12 | ScalarOpts
13 | Support
14 | Target
15 | TransformUtils
16 | )
17 | endif()
18 |
19 | add_unittest(CBEUnitTests CWriterTest
20 | TypeNameTest.cpp
21 | )
22 |
23 | if(USE_SYSTEM_LLVM)
24 | llvm_map_components_to_libnames(llvm_libs
25 | CBackendCodeGen
26 | CBackendInfo
27 | )
28 | target_link_libraries(CWriterTest PRIVATE LLVM ${llvm_libs})
29 | endif()
30 |
--------------------------------------------------------------------------------
/unittests/TypeNameTest.cpp:
--------------------------------------------------------------------------------
1 | #include "../lib/Target/CBackend/CBackend.h"
2 | #include "gtest/gtest.h"
3 |
4 | using namespace llvm;
5 |
6 | namespace llvm_cbe {
7 |
8 | class CWriterTestHelper {
9 | public:
10 | static std::string getTypeName(Type *ty) {
11 | raw_null_ostream NullStm;
12 | CWriter Writer(NullStm);
13 |
14 | std::string Str;
15 | raw_string_ostream StrStm(Str);
16 | Writer.printTypeName(StrStm, ty);
17 | StrStm.flush();
18 |
19 | return Str;
20 | }
21 | };
22 |
23 | TEST(TypeNameTest, Void) {
24 | LLVMContext ctx;
25 | Type *ty = Type::getVoidTy(ctx);
26 | EXPECT_EQ(CWriterTestHelper::getTypeName(ty), "void");
27 | }
28 |
29 | TEST(TypeNameTest, Bool) {
30 | LLVMContext ctx;
31 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getInt1Ty(ctx)), "bool");
32 | }
33 |
34 | TEST(TypeNameTest, SimpleNumeric) {
35 | LLVMContext ctx;
36 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getInt8Ty(ctx)), "uint8_t");
37 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getInt16Ty(ctx)), "uint16_t");
38 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getInt32Ty(ctx)), "uint32_t");
39 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getInt64Ty(ctx)), "uint64_t");
40 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getInt128Ty(ctx)),
41 | "uint128_t");
42 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getFloatTy(ctx)), "float");
43 | EXPECT_EQ(CWriterTestHelper::getTypeName(Type::getDoubleTy(ctx)), "double");
44 | }
45 |
46 | TEST(TypeNameTest, BoolPtr) {
47 | LLVMContext ctx;
48 | EXPECT_EQ(
49 | CWriterTestHelper::getTypeName(Type::getInt1Ty(ctx)->getPointerTo()),
50 | "bool*");
51 | }
52 |
53 | TEST(TypeNameTest, SimpleNumericPtr) {
54 | LLVMContext ctx;
55 | EXPECT_EQ(
56 | CWriterTestHelper::getTypeName(Type::getInt8Ty(ctx)->getPointerTo()),
57 | "uint8_t*");
58 | EXPECT_EQ(
59 | CWriterTestHelper::getTypeName(Type::getInt16Ty(ctx)->getPointerTo()),
60 | "uint16_t*");
61 | EXPECT_EQ(
62 | CWriterTestHelper::getTypeName(Type::getInt32Ty(ctx)->getPointerTo()),
63 | "uint32_t*");
64 | EXPECT_EQ(
65 | CWriterTestHelper::getTypeName(Type::getInt64Ty(ctx)->getPointerTo()),
66 | "uint64_t*");
67 | EXPECT_EQ(
68 | CWriterTestHelper::getTypeName(Type::getInt128Ty(ctx)->getPointerTo()),
69 | "uint128_t*");
70 | EXPECT_EQ(
71 | CWriterTestHelper::getTypeName(Type::getFloatTy(ctx)->getPointerTo()),
72 | "float*");
73 | EXPECT_EQ(
74 | CWriterTestHelper::getTypeName(Type::getDoubleTy(ctx)->getPointerTo()),
75 | "double*");
76 | }
77 |
78 | } // namespace llvm_cbe
79 |
--------------------------------------------------------------------------------