]> git.lizzy.rs Git - rust.git/commitdiff
Merge commit '39683d8eb7a32a74bea96ecbf1e87675d3338506' into sync_cg_gcc-2022-03-26
authorbjorn3 <bjorn3@users.noreply.github.com>
Sat, 26 Mar 2022 17:29:37 +0000 (18:29 +0100)
committerbjorn3 <bjorn3@users.noreply.github.com>
Sat, 26 Mar 2022 17:29:37 +0000 (18:29 +0100)
35 files changed:
1  2 
compiler/rustc_codegen_gcc/.github/workflows/ci.yml
compiler/rustc_codegen_gcc/.gitignore
compiler/rustc_codegen_gcc/Cargo.lock
compiler/rustc_codegen_gcc/Readme.md
compiler/rustc_codegen_gcc/build.sh
compiler/rustc_codegen_gcc/build_sysroot/build_sysroot.sh
compiler/rustc_codegen_gcc/cargo.sh
compiler/rustc_codegen_gcc/example/mini_core.rs
compiler/rustc_codegen_gcc/patches/0022-core-Disable-not-compiling-tests.patch
compiler/rustc_codegen_gcc/patches/0023-core-Ignore-failing-tests.patch
compiler/rustc_codegen_gcc/patches/0024-core-Disable-portable-simd-test.patch
compiler/rustc_codegen_gcc/patches/0028-core-Disable-long-running-tests.patch
compiler/rustc_codegen_gcc/prepare_build.sh
compiler/rustc_codegen_gcc/rust-toolchain
compiler/rustc_codegen_gcc/src/abi.rs
compiler/rustc_codegen_gcc/src/back/write.rs
compiler/rustc_codegen_gcc/src/base.rs
compiler/rustc_codegen_gcc/src/builder.rs
compiler/rustc_codegen_gcc/src/common.rs
compiler/rustc_codegen_gcc/src/consts.rs
compiler/rustc_codegen_gcc/src/context.rs
compiler/rustc_codegen_gcc/src/declare.rs
compiler/rustc_codegen_gcc/src/int.rs
compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
compiler/rustc_codegen_gcc/src/intrinsic/simd.rs
compiler/rustc_codegen_gcc/src/lib.rs
compiler/rustc_codegen_gcc/src/type_.rs
compiler/rustc_codegen_gcc/src/type_of.rs
compiler/rustc_codegen_gcc/test.sh
compiler/rustc_codegen_gcc/tests/run/assign.rs
compiler/rustc_codegen_gcc/tests/run/int.rs
compiler/rustc_codegen_gcc/tests/run/int_overflow.rs
compiler/rustc_codegen_gcc/tests/run/mut_ref.rs
compiler/rustc_codegen_gcc/tests/run/operations.rs
compiler/rustc_codegen_gcc/tests/run/static.rs

index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..337837c40bfb86eb03fcb00a01b576af1156116d
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,109 @@@
++name: CI
++
++on:
++  - push
++  - pull_request
++
++jobs:
++  build:
++    runs-on: ubuntu-latest
++
++    strategy:
++      fail-fast: false
++      matrix:
++        libgccjit_version: ["libgccjit.so", "libgccjit_without_int128.so"]
++
++    steps:
++    - uses: actions/checkout@v2
++
++    - uses: actions/checkout@v2
++      with:
++        repository: llvm/llvm-project
++        path: llvm
++
++    - name: Install packages
++      run: sudo apt-get install ninja-build ripgrep
++
++    - name: Download artifact
++      uses: dawidd6/action-download-artifact@v2
++      with:
++          workflow: main.yml
++          name: ${{ matrix.libgccjit_version }}
++          path: gcc-build
++          repo: antoyo/gcc
++          search_artifacts: true # Because, instead, the action only check the last job ran and that won't work since we want multiple artifacts.
++
++    - name: Setup path to libgccjit
++      run: |
++          echo $(readlink -f gcc-build) > gcc_path
++          # NOTE: the filename is still libgccjit.so even when the artifact name is different.
++          ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0
++
++    - name: Set env
++      run: |
++        echo "LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV
++        echo "LD_LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV
++        echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV
++
++    - name: Set RUST_COMPILER_RT_ROOT
++      run: echo "RUST_COMPILER_RT_ROOT="${{ env.workspace }}/llvm/compiler-rt >> $GITHUB_ENV
++
++    # https://github.com/actions/cache/issues/133
++    - name: Fixup owner of ~/.cargo/
++      # Don't remove the trailing /. It is necessary to follow the symlink.
++      run: sudo chown -R $(whoami):$(id -ng) ~/.cargo/
++
++    - name: Cache cargo installed crates
++      uses: actions/cache@v1.1.2
++      with:
++        path: ~/.cargo/bin
++        key: cargo-installed-crates2-ubuntu-latest
++
++    - name: Cache cargo registry
++      uses: actions/cache@v1
++      with:
++        path: ~/.cargo/registry
++        key: ${{ runner.os }}-cargo-registry2-${{ hashFiles('**/Cargo.lock') }}
++
++    - name: Cache cargo index
++      uses: actions/cache@v1
++      with:
++        path: ~/.cargo/git
++        key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
++
++    - name: Cache cargo target dir
++      uses: actions/cache@v1.1.2
++      with:
++        path: target
++        key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain') }}
++
++    - name: Build
++      run: |
++        ./prepare_build.sh
++        ./build.sh
++        cargo test
++        ./clean_all.sh
++
++    - name: Prepare dependencies
++      run: |
++        git config --global user.email "user@example.com"
++        git config --global user.name "User"
++        ./prepare.sh
++
++    # Compile is a separate step, as the actions-rs/cargo action supports error annotations
++    - name: Compile
++      uses: actions-rs/cargo@v1.0.3
++      with:
++        command: build
++        args: --release
++
++    - name: Test
++      run: |
++        # Enable backtraces for easier debugging
++        export RUST_BACKTRACE=1
++
++        # Reduce amount of benchmark runs as they are slow
++        export COMPILE_RUNS=2
++        export RUN_RUNS=2
++
++        ./test.sh --release
index 1e2f9e3aebb2cd83b6ae296b833efc75c3e10f92,0000000000000000000000000000000000000000..efda74b2633aedb0f50449b1500e9cd0f46ed0f9
mode 100644,000000..100644
--- /dev/null
@@@ -1,20 -1,0 +1,21 @@@
 +target
 +**/*.rs.bk
 +*.rlib
 +*.o
 +perf.data
 +perf.data.old
 +*.events
 +*.string*
 +/build_sysroot/sysroot
 +/build_sysroot/sysroot_src
 +/build_sysroot/Cargo.lock
 +/build_sysroot/test_target/Cargo.lock
 +/rust
 +/simple-raytracer
 +/regex
 +gimple*
 +*asm
 +res
 +test-backend
 +gcc_path
++benchmarks
index 47925f72c2cbdd30b6964213f60da8da3dc26d2f,0000000000000000000000000000000000000000..a1d9f2f5e3822a813641c02e93d869e4e028aa19
mode 100644,000000..100644
--- /dev/null
@@@ -1,330 -1,0 +1,330 @@@
- source = "git+https://github.com/antoyo/gccjit.rs#0672b78d162d65b6f36ea4062947253affe9fdef"
 +# This file is automatically @generated by Cargo.
 +# It is not intended for manual editing.
 +version = 3
 +
 +[[package]]
 +name = "aho-corasick"
 +version = "0.7.18"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
 +dependencies = [
 + "memchr",
 +]
 +
 +[[package]]
 +name = "ar"
 +version = "0.8.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "450575f58f7bee32816abbff470cbc47797397c2a81e0eaced4b98436daf52e1"
 +
 +[[package]]
 +name = "bitflags"
 +version = "1.3.2"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
 +
 +[[package]]
 +name = "cfg-if"
 +version = "1.0.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 +
 +[[package]]
 +name = "fm"
 +version = "0.1.4"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "68fda3cff2cce84c19e5dfa5179a4b35d2c0f18b893f108002b8a6a54984acca"
 +dependencies = [
 + "regex",
 +]
 +
 +[[package]]
 +name = "gccjit"
 +version = "1.0.0"
- source = "git+https://github.com/antoyo/gccjit.rs#0672b78d162d65b6f36ea4062947253affe9fdef"
++source = "git+https://github.com/antoyo/gccjit.rs#bdecdecfb8a02ec861a39a350f990faa33bd31c3"
 +dependencies = [
 + "gccjit_sys",
 +]
 +
 +[[package]]
 +name = "gccjit_sys"
 +version = "0.0.1"
++source = "git+https://github.com/antoyo/gccjit.rs#bdecdecfb8a02ec861a39a350f990faa33bd31c3"
 +dependencies = [
 + "libc 0.1.12",
 +]
 +
 +[[package]]
 +name = "getopts"
 +version = "0.2.21"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
 +dependencies = [
 + "unicode-width",
 +]
 +
 +[[package]]
 +name = "getrandom"
 +version = "0.2.3"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
 +dependencies = [
 + "cfg-if",
 + "libc 0.2.112",
 + "wasi",
 +]
 +
 +[[package]]
 +name = "hermit-abi"
 +version = "0.1.19"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
 +dependencies = [
 + "libc 0.2.112",
 +]
 +
 +[[package]]
 +name = "lang_tester"
 +version = "0.3.13"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "96bd995a092cac79868250589869b5a5d656b02a02bd74c8ebdc566dc7203090"
 +dependencies = [
 + "fm",
 + "getopts",
 + "libc 0.2.112",
 + "num_cpus",
 + "termcolor",
 + "threadpool",
 + "wait-timeout",
 + "walkdir",
 +]
 +
 +[[package]]
 +name = "libc"
 +version = "0.1.12"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "e32a70cf75e5846d53a673923498228bbec6a8624708a9ea5645f075d6276122"
 +
 +[[package]]
 +name = "libc"
 +version = "0.2.112"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
 +
 +[[package]]
 +name = "memchr"
 +version = "2.4.1"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
 +
 +[[package]]
 +name = "num_cpus"
 +version = "1.13.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
 +dependencies = [
 + "hermit-abi",
 + "libc 0.2.112",
 +]
 +
 +[[package]]
 +name = "ppv-lite86"
 +version = "0.2.15"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
 +
 +[[package]]
 +name = "rand"
 +version = "0.8.4"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
 +dependencies = [
 + "libc 0.2.112",
 + "rand_chacha",
 + "rand_core",
 + "rand_hc",
 +]
 +
 +[[package]]
 +name = "rand_chacha"
 +version = "0.3.1"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
 +dependencies = [
 + "ppv-lite86",
 + "rand_core",
 +]
 +
 +[[package]]
 +name = "rand_core"
 +version = "0.6.3"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
 +dependencies = [
 + "getrandom",
 +]
 +
 +[[package]]
 +name = "rand_hc"
 +version = "0.3.1"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
 +dependencies = [
 + "rand_core",
 +]
 +
 +[[package]]
 +name = "redox_syscall"
 +version = "0.2.10"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
 +dependencies = [
 + "bitflags",
 +]
 +
 +[[package]]
 +name = "regex"
 +version = "1.5.4"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
 +dependencies = [
 + "aho-corasick",
 + "memchr",
 + "regex-syntax",
 +]
 +
 +[[package]]
 +name = "regex-syntax"
 +version = "0.6.25"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
 +
 +[[package]]
 +name = "remove_dir_all"
 +version = "0.5.3"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
 +dependencies = [
 + "winapi",
 +]
 +
 +[[package]]
 +name = "rustc_codegen_gcc"
 +version = "0.1.0"
 +dependencies = [
 + "ar",
 + "gccjit",
 + "lang_tester",
 + "target-lexicon",
 + "tempfile",
 +]
 +
 +[[package]]
 +name = "same-file"
 +version = "1.0.6"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
 +dependencies = [
 + "winapi-util",
 +]
 +
 +[[package]]
 +name = "target-lexicon"
 +version = "0.10.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "ab0e7238dcc7b40a7be719a25365910f6807bd864f4cce6b2e6b873658e2b19d"
 +
 +[[package]]
 +name = "tempfile"
 +version = "3.2.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
 +dependencies = [
 + "cfg-if",
 + "libc 0.2.112",
 + "rand",
 + "redox_syscall",
 + "remove_dir_all",
 + "winapi",
 +]
 +
 +[[package]]
 +name = "termcolor"
 +version = "1.1.2"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
 +dependencies = [
 + "winapi-util",
 +]
 +
 +[[package]]
 +name = "threadpool"
 +version = "1.8.1"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa"
 +dependencies = [
 + "num_cpus",
 +]
 +
 +[[package]]
 +name = "unicode-width"
 +version = "0.1.9"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
 +
 +[[package]]
 +name = "wait-timeout"
 +version = "0.2.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
 +dependencies = [
 + "libc 0.2.112",
 +]
 +
 +[[package]]
 +name = "walkdir"
 +version = "2.3.2"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
 +dependencies = [
 + "same-file",
 + "winapi",
 + "winapi-util",
 +]
 +
 +[[package]]
 +name = "wasi"
 +version = "0.10.2+wasi-snapshot-preview1"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
 +
 +[[package]]
 +name = "winapi"
 +version = "0.3.9"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
 +dependencies = [
 + "winapi-i686-pc-windows-gnu",
 + "winapi-x86_64-pc-windows-gnu",
 +]
 +
 +[[package]]
 +name = "winapi-i686-pc-windows-gnu"
 +version = "0.4.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 +
 +[[package]]
 +name = "winapi-util"
 +version = "0.1.5"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
 +dependencies = [
 + "winapi",
 +]
 +
 +[[package]]
 +name = "winapi-x86_64-pc-windows-gnu"
 +version = "0.4.0"
 +source = "registry+https://github.com/rust-lang/crates.io-index"
 +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
index 1fcfb5f6e20a108661fce85ecbc8fa36ab5d7820,0000000000000000000000000000000000000000..27b30e0fd1b336a11de9f2bf6c184d0354ec60fb
mode 100644,000000..100644
--- /dev/null
@@@ -1,137 -1,0 +1,147 @@@
 +# WIP libgccjit codegen backend for rust
 +
 +This is a GCC codegen for rustc, which means it can be loaded by the existing rustc frontend, but benefits from GCC: more architectures are supported and GCC's optimizations are used.
 +
 +**Despite its name, libgccjit can be used for ahead-of-time compilation, as is used here.**
 +
 +## Motivation
 +
 +The primary goal of this project is to be able to compile Rust code on platforms unsupported by LLVM.
 +A secondary goal is to check if using the gcc backend will provide any run-time speed improvement for the programs compiled using rustc.
 +
 +## Building
 +
 +**This requires a patched libgccjit in order to work.
 +The patches in [this repostory](https://github.com/antoyo/libgccjit-patches) need to be applied.
 +(Those patches should work when applied on master, but in case it doesn't work, they are known to work when applied on 079c23cfe079f203d5df83fea8e92a60c7d7e878.)
 +You can also use my [fork of gcc](https://github.com/antoyo/gcc) which already includes these patches.**
 +
 +**Put the path to your custom build of libgccjit in the file `gcc_path`.**
 +
 +```bash
 +$ git clone https://github.com/rust-lang/rustc_codegen_gcc.git
 +$ cd rustc_codegen_gcc
++$ git clone https://github.com/llvm/llvm-project llvm --depth 1 --single-branch
++$ export RUST_COMPILER_RT_ROOT="$PWD/llvm/compiler-rt"
 +$ ./prepare_build.sh # download and patch sysroot src
 +$ ./build.sh --release
 +```
 +
 +To run the tests:
 +
 +```bash
 +$ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking
 +$ ./test.sh --release
 +```
 +
 +## Usage
 +
 +`$cg_gccjit_dir` is the directory you cloned this repo into in the following instructions.
 +
 +### Cargo
 +
 +```bash
 +$ CHANNEL="release" $cg_gccjit_dir/cargo.sh run
 +```
 +
 +If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./test.sh`) you should use `CHANNEL="debug"` instead or omit `CHANNEL="release"` completely.
 +
 +### Rustc
 +
 +> You should prefer using the Cargo method.
 +
 +```bash
 +$ rustc +$(cat $cg_gccjit_dir/rust-toolchain) -Cpanic=abort -Zcodegen-backend=$cg_gccjit_dir/target/release/librustc_codegen_gcc.so --sysroot $cg_gccjit_dir/build_sysroot/sysroot my_crate.rs
 +```
 +
 +## Env vars
 +
 +<dl>
 +    <dt>CG_GCCJIT_INCR_CACHE_DISABLED</dt>
 +    <dd>Don't cache object files in the incremental cache. Useful during development of cg_gccjit
 +    to make it possible to use incremental mode for all analyses performed by rustc without caching
 +    object files when their content should have been changed by a change to cg_gccjit.</dd>
 +    <dt>CG_GCCJIT_DISPLAY_CG_TIME</dt>
 +    <dd>Display the time it took to perform codegen for a crate</dd>
 +</dl>
 +
 +## Debugging
 +
 +Sometimes, libgccjit will crash and output an error like this:
 +
 +```
 +during RTL pass: expand
 +libgccjit.so: error: in expmed_mode_index, at expmed.h:249
 +0x7f0da2e61a35 expmed_mode_index
 +      ../../../gcc/gcc/expmed.h:249
 +0x7f0da2e61aa4 expmed_op_cost_ptr
 +      ../../../gcc/gcc/expmed.h:271
 +0x7f0da2e620dc sdiv_cost_ptr
 +      ../../../gcc/gcc/expmed.h:540
 +0x7f0da2e62129 sdiv_cost
 +      ../../../gcc/gcc/expmed.h:558
 +0x7f0da2e73c12 expand_divmod(int, tree_code, machine_mode, rtx_def*, rtx_def*, rtx_def*, int)
 +      ../../../gcc/gcc/expmed.c:4335
 +0x7f0da2ea1423 expand_expr_real_2(separate_ops*, rtx_def*, machine_mode, expand_modifier)
 +      ../../../gcc/gcc/expr.c:9240
 +0x7f0da2cd1a1e expand_gimple_stmt_1
 +      ../../../gcc/gcc/cfgexpand.c:3796
 +0x7f0da2cd1c30 expand_gimple_stmt
 +      ../../../gcc/gcc/cfgexpand.c:3857
 +0x7f0da2cd90a9 expand_gimple_basic_block
 +      ../../../gcc/gcc/cfgexpand.c:5898
 +0x7f0da2cdade8 execute
 +      ../../../gcc/gcc/cfgexpand.c:6582
 +```
 +
 +To see the code which causes this error, call the following function:
 +
 +```c
 +gcc_jit_context_dump_to_file(ctxt, "/tmp/output.c", 1 /* update_locations */)
 +```
 +
 +This will create a C-like file and add the locations into the IR pointing to this C file.
 +Then, rerun the program and it will output the location in the second line:
 +
 +```
 +libgccjit.so: /tmp/something.c:61322:0: error: in expmed_mode_index, at expmed.h:249
 +```
 +
 +Or add a breakpoint to `add_error` in gdb and print the line number using:
 +
 +```
 +p loc->m_line
++p loc->m_filename->m_buffer
++```
++
++To print a debug representation of a tree:
++
++```c
++debug_tree(expr);
 +```
 +
 +To get the `rustc` command to run in `gdb`, add the `--verbose` flag to `cargo build`.
 +
 +### How to use a custom-build rustc
 +
 + * Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).
 + * Clean and rebuild the codegen with `debug-current` in the file `rust-toolchain`.
 +
 +### How to build a cross-compiling libgccjit
 +
 +#### Building libgccjit
 +
 + * Follow these instructions: https://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/ with the following changes:
 + * Configure gcc with `../gcc/configure --enable-host-shared --disable-multilib --enable-languages=c,jit,c++ --disable-bootstrap --enable-checking=release --prefix=/opt/m68k-gcc/ --target=m68k-linux --without-headers`.
 + * Some shells, like fish, don't define the environment variable `$MACHTYPE`.
 + * Add `CFLAGS="-Wno-error=attributes -g -O2"` at the end of the configure command for building glibc (`CFLAGS="-Wno-error=attributes -Wno-error=array-parameter -Wno-error=stringop-overflow -Wno-error=array-bounds -g -O2"` for glibc 2.31, which is useful for Debian).
 +
 +#### Configuring rustc_codegen_gcc
 +
 + * Set `TARGET_TRIPLE="m68k-unknown-linux-gnu"` in config.sh.
 + * Since rustc doesn't support this architecture yet, set it back to `TARGET_TRIPLE="mips-unknown-linux-gnu"` (or another target having the same attributes). Alternatively, create a [target specification file](https://book.avr-rust.com/005.1-the-target-specification-json-file.html) (note that the `arch` specified in this file must be supported by the rust compiler).
 + * Set `linker='-Clinker=m68k-linux-gcc'`.
 + * Set the path to the cross-compiling libgccjit in `gcc_path`.
 + * Disable the 128-bit integer types if the target doesn't support them by using `let i128_type = context.new_type::<i64>();` in `context.rs` (same for u128_type).
++ * Comment the line: `context.add_command_line_option("-masm=intel");` in src/base.rs.
 + * (might not be necessary) Disable the compilation of libstd.so (and possibly libcore.so?).
index 17a0d2ab3f0601f92b56bccff8b7ba0adde227d5,0000000000000000000000000000000000000000..230ab7b6d42baf42715936bb3805c8e71ef4eb8b
mode 100755,000000..100755
--- /dev/null
@@@ -1,31 -1,0 +1,64 @@@
- if [ -f ./gcc_path ]; then 
 +#!/bin/bash
 +
 +#set -x
 +set -e
 +
- if [[ "$1" == "--release" ]]; then
++codegen_channel=debug
++sysroot_channel=debug
++
++while [[ $# -gt 0 ]]; do
++    case $1 in
++        --release)
++            codegen_channel=release
++            shift
++            ;;
++        --release-sysroot)
++            sysroot_channel=release
++            shift
++            ;;
++        *)
++            echo "Unknown option $1"
++            exit 1
++            ;;
++    esac
++done
++
++if [ -f ./gcc_path ]; then
 +    export GCC_PATH=$(cat gcc_path)
 +else
 +    echo 'Please put the path to your custom build of libgccjit in the file `gcc_path`, see Readme.md for details'
 +    exit 1
 +fi
 +
 +export LD_LIBRARY_PATH="$GCC_PATH"
 +export LIBRARY_PATH="$GCC_PATH"
 +
-     CARGO_INCREMENTAL=1 cargo rustc --release
++features=
++
++if [[ "$1" == "--features" ]]; then
++    shift
++    features="--features $1"
++    shift
++fi
++
++if [[ "$codegen_channel" == "release" ]]; then
 +    export CHANNEL='release'
-     cargo rustc
++    CARGO_INCREMENTAL=1 cargo rustc --release $features
 +else
 +    echo $LD_LIBRARY_PATH
 +    export CHANNEL='debug'
- time ./build_sysroot/build_sysroot.sh $CHANNEL
++    cargo rustc $features
 +fi
 +
 +source config.sh
 +
 +rm -r target/out || true
 +mkdir -p target/out/gccjit
 +
 +echo "[BUILD] sysroot"
++if [[ "$sysroot_channel" == "release" ]]; then
++    time ./build_sysroot/build_sysroot.sh --release
++else
++    time ./build_sysroot/build_sysroot.sh
++fi
++
index d1dcf495db8a3770df197c7f80b22a2573475c0b,0000000000000000000000000000000000000000..a965ca971a07d362635049c8a40a138800d20a60
mode 100755,000000..100755
--- /dev/null
@@@ -1,30 -1,0 +1,30 @@@
-     cargo build --target $TARGET_TRIPLE
 +#!/bin/bash
 +
 +# Requires the CHANNEL env var to be set to `debug` or `release.`
 +
 +set -e
 +cd $(dirname "$0")
 +
 +pushd ../ >/dev/null
 +source ./config.sh
 +popd >/dev/null
 +
 +# Cleanup for previous run
 +#     v Clean target dir except for build scripts and incremental cache
 +rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true
 +rm Cargo.lock test_target/Cargo.lock 2>/dev/null || true
 +rm -r sysroot/ 2>/dev/null || true
 +
 +# Build libs
 +export RUSTFLAGS="$RUSTFLAGS -Z force-unstable-if-unmarked -Cpanic=abort"
 +if [[ "$1" == "--release" ]]; then
 +    sysroot_channel='release'
 +    RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=3" cargo build --target $TARGET_TRIPLE --release
 +else
 +    sysroot_channel='debug'
++    cargo build --target $TARGET_TRIPLE --features compiler_builtins/c
 +fi
 +
 +# Copy files to sysroot
 +mkdir -p sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
 +cp -r target/$TARGET_TRIPLE/$sysroot_channel/deps/* sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
index 1001c522052c800b15d170971d187b6987092288,0000000000000000000000000000000000000000..332f365ce0ceed7cc0cf71fc6c08f51b3e69f4fa
mode 100755,000000..100755
--- /dev/null
@@@ -1,23 -1,0 +1,23 @@@
- TOOLCHAIN=$(cat rust-toolchain)
 +#!/bin/bash
 +
 +if [ -z $CHANNEL ]; then
 +export CHANNEL='debug'
 +fi
 +
 +pushd $(dirname "$0") >/dev/null
 +source config.sh
 +
 +# read nightly compiler from rust-toolchain file
++TOOLCHAIN=$(cat rust-toolchain | grep channel | sed 's/channel = "\(.*\)"/\1/')
 +
 +popd >/dev/null
 +
 +if [[ $(rustc -V) != $(rustc +${TOOLCHAIN} -V) ]]; then
 +    echo "rustc_codegen_gcc is build for $(rustc +${TOOLCHAIN} -V) but the default rustc version is $(rustc -V)."
 +    echo "Using $(rustc +${TOOLCHAIN} -V)."
 +fi
 +
 +cmd=$1
 +shift
 +
 +RUSTDOCFLAGS="$RUSTFLAGS" cargo +${TOOLCHAIN} $cmd --target $TARGET_TRIPLE $@
index 1067cee88148756f597738b0e65ae1c332d45975,0000000000000000000000000000000000000000..a8435287d9fde698eb89c37a44b4c32dc5e3f4a5
mode 100644,000000..100644
--- /dev/null
@@@ -1,585 -1,0 +1,599 @@@
- pub struct Box<T: ?Sized>(*mut T);
 +#![feature(
 +    no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types,
 +    untagged_unions, decl_macro, rustc_attrs, transparent_unions, auto_traits,
 +    thread_local
 +)]
 +#![no_core]
 +#![allow(dead_code)]
 +
 +#[no_mangle]
 +unsafe extern "C" fn _Unwind_Resume() {
 +    intrinsics::unreachable();
 +}
 +
 +#[lang = "sized"]
 +pub trait Sized {}
 +
++#[lang = "destruct"]
++pub trait Destruct {}
++
 +#[lang = "unsize"]
 +pub trait Unsize<T: ?Sized> {}
 +
 +#[lang = "coerce_unsized"]
 +pub trait CoerceUnsized<T> {}
 +
 +impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
 +impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
 +impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
 +impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
 +
 +#[lang = "dispatch_from_dyn"]
 +pub trait DispatchFromDyn<T> {}
 +
 +// &T -> &U
 +impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
 +// &mut T -> &mut U
 +impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
 +// *const T -> *const U
 +impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
 +// *mut T -> *mut U
 +impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
 +impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
 +
 +#[lang = "receiver"]
 +pub trait Receiver {}
 +
 +impl<T: ?Sized> Receiver for &T {}
 +impl<T: ?Sized> Receiver for &mut T {}
 +impl<T: ?Sized> Receiver for Box<T> {}
 +
 +#[lang = "copy"]
 +pub unsafe trait Copy {}
 +
 +unsafe impl Copy for bool {}
 +unsafe impl Copy for u8 {}
 +unsafe impl Copy for u16 {}
 +unsafe impl Copy for u32 {}
 +unsafe impl Copy for u64 {}
 +unsafe impl Copy for usize {}
 +unsafe impl Copy for i8 {}
 +unsafe impl Copy for i16 {}
 +unsafe impl Copy for i32 {}
 +unsafe impl Copy for isize {}
 +unsafe impl Copy for f32 {}
++unsafe impl Copy for f64 {}
 +unsafe impl Copy for char {}
 +unsafe impl<'a, T: ?Sized> Copy for &'a T {}
 +unsafe impl<T: ?Sized> Copy for *const T {}
 +unsafe impl<T: ?Sized> Copy for *mut T {}
 +
 +#[lang = "sync"]
 +pub unsafe trait Sync {}
 +
 +unsafe impl Sync for bool {}
 +unsafe impl Sync for u8 {}
 +unsafe impl Sync for u16 {}
 +unsafe impl Sync for u32 {}
 +unsafe impl Sync for u64 {}
 +unsafe impl Sync for usize {}
 +unsafe impl Sync for i8 {}
 +unsafe impl Sync for i16 {}
 +unsafe impl Sync for i32 {}
 +unsafe impl Sync for isize {}
 +unsafe impl Sync for char {}
 +unsafe impl<'a, T: ?Sized> Sync for &'a T {}
 +unsafe impl Sync for [u8; 16] {}
 +
 +#[lang = "freeze"]
 +unsafe auto trait Freeze {}
 +
 +unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
 +unsafe impl<T: ?Sized> Freeze for *const T {}
 +unsafe impl<T: ?Sized> Freeze for *mut T {}
 +unsafe impl<T: ?Sized> Freeze for &T {}
 +unsafe impl<T: ?Sized> Freeze for &mut T {}
 +
 +#[lang = "structural_peq"]
 +pub trait StructuralPartialEq {}
 +
 +#[lang = "structural_teq"]
 +pub trait StructuralEq {}
 +
 +#[lang = "not"]
 +pub trait Not {
 +    type Output;
 +
 +    fn not(self) -> Self::Output;
 +}
 +
 +impl Not for bool {
 +    type Output = bool;
 +
 +    fn not(self) -> bool {
 +        !self
 +    }
 +}
 +
 +#[lang = "mul"]
 +pub trait Mul<RHS = Self> {
 +    type Output;
 +
 +    #[must_use]
 +    fn mul(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Mul for u8 {
 +    type Output = Self;
 +
 +    fn mul(self, rhs: Self) -> Self::Output {
 +        self * rhs
 +    }
 +}
 +
 +impl Mul for usize {
 +    type Output = Self;
 +
 +    fn mul(self, rhs: Self) -> Self::Output {
 +        self * rhs
 +    }
 +}
 +
 +#[lang = "add"]
 +pub trait Add<RHS = Self> {
 +    type Output;
 +
 +    fn add(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Add for u8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for usize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +#[lang = "sub"]
 +pub trait Sub<RHS = Self> {
 +    type Output;
 +
 +    fn sub(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Sub for usize {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +impl Sub for u8 {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +impl Sub for i8 {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +impl Sub for i16 {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +#[lang = "rem"]
 +pub trait Rem<RHS = Self> {
 +    type Output;
 +
 +    fn rem(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Rem for usize {
 +    type Output = Self;
 +
 +    fn rem(self, rhs: Self) -> Self {
 +        self % rhs
 +    }
 +}
 +
 +#[lang = "bitor"]
 +pub trait BitOr<RHS = Self> {
 +    type Output;
 +
 +    #[must_use]
 +    fn bitor(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl BitOr for bool {
 +    type Output = bool;
 +
 +    fn bitor(self, rhs: bool) -> bool {
 +        self | rhs
 +    }
 +}
 +
 +impl<'a> BitOr<bool> for &'a bool {
 +    type Output = bool;
 +
 +    fn bitor(self, rhs: bool) -> bool {
 +        *self | rhs
 +    }
 +}
 +
 +#[lang = "eq"]
 +pub trait PartialEq<Rhs: ?Sized = Self> {
 +    fn eq(&self, other: &Rhs) -> bool;
 +    fn ne(&self, other: &Rhs) -> bool;
 +}
 +
 +impl PartialEq for u8 {
 +    fn eq(&self, other: &u8) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &u8) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl PartialEq for u16 {
 +    fn eq(&self, other: &u16) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &u16) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl PartialEq for u32 {
 +    fn eq(&self, other: &u32) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &u32) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +
 +impl PartialEq for u64 {
 +    fn eq(&self, other: &u64) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &u64) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl PartialEq for usize {
 +    fn eq(&self, other: &usize) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &usize) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl PartialEq for i8 {
 +    fn eq(&self, other: &i8) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &i8) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl PartialEq for i32 {
 +    fn eq(&self, other: &i32) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &i32) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl PartialEq for isize {
 +    fn eq(&self, other: &isize) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &isize) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl PartialEq for char {
 +    fn eq(&self, other: &char) -> bool {
 +        (*self) == (*other)
 +    }
 +    fn ne(&self, other: &char) -> bool {
 +        (*self) != (*other)
 +    }
 +}
 +
 +impl<T: ?Sized> PartialEq for *const T {
 +    fn eq(&self, other: &*const T) -> bool {
 +        *self == *other
 +    }
 +    fn ne(&self, other: &*const T) -> bool {
 +        *self != *other
 +    }
 +}
 +
 +#[lang = "neg"]
 +pub trait Neg {
 +    type Output;
 +
 +    fn neg(self) -> Self::Output;
 +}
 +
 +impl Neg for i8 {
 +    type Output = i8;
 +
 +    fn neg(self) -> i8 {
 +        -self
 +    }
 +}
 +
 +impl Neg for i16 {
 +    type Output = i16;
 +
 +    fn neg(self) -> i16 {
 +        self
 +    }
 +}
 +
 +impl Neg for isize {
 +    type Output = isize;
 +
 +    fn neg(self) -> isize {
 +        -self
 +    }
 +}
 +
 +impl Neg for f32 {
 +    type Output = f32;
 +
 +    fn neg(self) -> f32 {
 +        -self
 +    }
 +}
 +
 +pub enum Option<T> {
 +    Some(T),
 +    None,
 +}
 +
 +pub use Option::*;
 +
 +#[lang = "phantom_data"]
 +pub struct PhantomData<T: ?Sized>;
 +
 +#[lang = "fn_once"]
 +#[rustc_paren_sugar]
 +pub trait FnOnce<Args> {
 +    #[lang = "fn_once_output"]
 +    type Output;
 +
 +    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
 +}
 +
 +#[lang = "fn_mut"]
 +#[rustc_paren_sugar]
 +pub trait FnMut<Args>: FnOnce<Args> {
 +    extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
 +}
 +
 +#[lang = "panic"]
 +#[track_caller]
 +pub fn panic(_msg: &str) -> ! {
 +    unsafe {
 +        libc::puts("Panicking\n\0" as *const str as *const u8);
 +        intrinsics::abort();
 +    }
 +}
 +
 +#[lang = "panic_bounds_check"]
 +#[track_caller]
 +fn panic_bounds_check(index: usize, len: usize) -> ! {
 +    unsafe {
 +        libc::printf("index out of bounds: the len is %d but the index is %d\n\0" as *const str as *const i8, len, index);
 +        intrinsics::abort();
 +    }
 +}
 +
 +#[lang = "eh_personality"]
 +fn eh_personality() -> ! {
 +    loop {}
 +}
 +
 +#[lang = "drop_in_place"]
 +#[allow(unconditional_recursion)]
 +pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
 +    // Code here does not matter - this is replaced by the
 +    // real drop glue by the compiler.
 +    drop_in_place(to_drop);
 +}
 +
 +#[lang = "deref"]
 +pub trait Deref {
 +    type Target: ?Sized;
 +
 +    fn deref(&self) -> &Self::Target;
 +}
 +
++pub trait Allocator {
++}
++
++pub struct Global;
++
++impl Allocator for Global {}
++
 +#[lang = "owned_box"]
- impl<T: ?Sized> Drop for Box<T> {
++pub struct Box<
++    T: ?Sized,
++    A: Allocator = Global,
++>(*mut T, A);
 +
 +impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
 +
- unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
++impl<T: ?Sized, A: Allocator> Drop for Box<T, A> {
 +    fn drop(&mut self) {
 +        // drop is currently performed by compiler.
 +    }
 +}
 +
 +impl<T> Deref for Box<T> {
 +    type Target = T;
 +
 +    fn deref(&self) -> &Self::Target {
 +        &**self
 +    }
 +}
 +
 +#[lang = "exchange_malloc"]
 +unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
 +    libc::malloc(size)
 +}
 +
 +#[lang = "box_free"]
++unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: *mut T, alloc: A) {
 +    libc::free(ptr as *mut u8);
 +}
 +
 +#[lang = "drop"]
 +pub trait Drop {
 +    fn drop(&mut self);
 +}
 +
 +#[lang = "manually_drop"]
 +#[repr(transparent)]
 +pub struct ManuallyDrop<T: ?Sized> {
 +    pub value: T,
 +}
 +
 +#[lang = "maybe_uninit"]
 +#[repr(transparent)]
 +pub union MaybeUninit<T> {
 +    pub uninit: (),
 +    pub value: ManuallyDrop<T>,
 +}
 +
 +pub mod intrinsics {
 +    extern "rust-intrinsic" {
 +        pub fn abort() -> !;
 +        pub fn size_of<T>() -> usize;
 +        pub fn size_of_val<T: ?::Sized>(val: *const T) -> usize;
 +        pub fn min_align_of<T>() -> usize;
 +        pub fn min_align_of_val<T: ?::Sized>(val: *const T) -> usize;
 +        pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
 +        pub fn transmute<T, U>(e: T) -> U;
 +        pub fn ctlz_nonzero<T>(x: T) -> T;
 +        pub fn needs_drop<T>() -> bool;
 +        pub fn bitreverse<T>(x: T) -> T;
 +        pub fn bswap<T>(x: T) -> T;
 +        pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
 +        pub fn unreachable() -> !;
 +    }
 +}
 +
 +pub mod libc {
 +    #[link(name = "c")]
 +    extern "C" {
 +        pub fn puts(s: *const u8) -> i32;
 +        pub fn printf(format: *const i8, ...) -> i32;
 +        pub fn malloc(size: usize) -> *mut u8;
 +        pub fn free(ptr: *mut u8);
 +        pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
 +        pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
 +        pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
 +    }
 +}
 +
 +#[lang = "index"]
 +pub trait Index<Idx: ?Sized> {
 +    type Output: ?Sized;
 +    fn index(&self, index: Idx) -> &Self::Output;
 +}
 +
 +impl<T> Index<usize> for [T; 3] {
 +    type Output = T;
 +
 +    fn index(&self, index: usize) -> &Self::Output {
 +        &self[index]
 +    }
 +}
 +
 +impl<T> Index<usize> for [T] {
 +    type Output = T;
 +
 +    fn index(&self, index: usize) -> &Self::Output {
 +        &self[index]
 +    }
 +}
 +
 +extern {
 +    type VaListImpl;
 +}
 +
 +#[lang = "va_list"]
 +#[repr(transparent)]
 +pub struct VaList<'a>(&'a mut VaListImpl);
 +
 +#[rustc_builtin_macro]
 +#[rustc_macro_transparency = "semitransparent"]
 +pub macro stringify($($t:tt)*) { /* compiler built-in */ }
 +
 +#[rustc_builtin_macro]
 +#[rustc_macro_transparency = "semitransparent"]
 +pub macro file() { /* compiler built-in */ }
 +
 +#[rustc_builtin_macro]
 +#[rustc_macro_transparency = "semitransparent"]
 +pub macro line() { /* compiler built-in */ }
 +
 +#[rustc_builtin_macro]
 +#[rustc_macro_transparency = "semitransparent"]
 +pub macro cfg() { /* compiler built-in */ }
 +
 +pub static A_STATIC: u8 = 42;
 +
 +#[lang = "panic_location"]
 +struct PanicLocation {
 +    file: &'static str,
 +    line: u32,
 +    column: u32,
 +}
 +
 +#[no_mangle]
 +pub fn get_tls() -> u8 {
 +    #[thread_local]
 +    static A: u8 = 42;
 +
 +    A
 +}
index aae62a938b4577e8ad70ee825d9331c3f687ecd7,0000000000000000000000000000000000000000..301b3f9bde4dd73ff882ffbe675b4e2053758984
mode 100644,000000..100644
--- /dev/null
@@@ -1,63 -1,0 +1,63 @@@
- +edition = "2018"
 +From f6befc4bb51d84f5f1cf35938a168c953d421350 Mon Sep 17 00:00:00 2001
 +From: bjorn3 <bjorn3@users.noreply.github.com>
 +Date: Sun, 24 Nov 2019 15:10:23 +0100
 +Subject: [PATCH] [core] Disable not compiling tests
 +
 +---
 + library/core/tests/Cargo.toml         | 8 ++++++++
 + library/core/tests/num/flt2dec/mod.rs | 1 -
 + library/core/tests/num/int_macros.rs  | 2 ++
 + library/core/tests/num/uint_macros.rs | 2 ++
 + library/core/tests/ptr.rs             | 2 ++
 + library/core/tests/slice.rs           | 2 ++
 + 6 files changed, 16 insertions(+), 1 deletion(-)
 + create mode 100644 library/core/tests/Cargo.toml
 +
 +diff --git a/library/core/tests/Cargo.toml b/library/core/tests/Cargo.toml
 +new file mode 100644
 +index 0000000..46fd999
 +--- /dev/null
 ++++ b/library/core/tests/Cargo.toml
 +@@ -0,0 +1,8 @@
 ++[package]
 ++name = "core"
 ++version = "0.0.0"
+++edition = "2021"
 ++
 ++[lib]
 ++name = "coretests"
 ++path = "lib.rs"
 +diff --git a/library/core/tests/num/flt2dec/mod.rs b/library/core/tests/num/flt2dec/mod.rs
 +index a35897e..f0bf645 100644
 +--- a/library/core/tests/num/flt2dec/mod.rs
 ++++ b/library/core/tests/num/flt2dec/mod.rs
 +@@ -13,7 +13,6 @@ mod strategy {
 +     mod dragon;
 +     mod grisu;
 + }
 +-mod random;
 + 
 + pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
 +     match decode(v).1 {
 +diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs
 +index 6609bc3..241b497 100644
 +--- a/library/core/tests/slice.rs
 ++++ b/library/core/tests/slice.rs
 +@@ -1209,6 +1209,7 @@ fn brute_force_rotate_test_1() {
 +     }
 + }
 + 
 ++/*
 + #[test]
 + #[cfg(not(target_arch = "wasm32"))]
 + fn sort_unstable() {
 +@@ -1394,6 +1395,7 @@ fn partition_at_index() {
 +     v.select_nth_unstable(0);
 +     assert!(v == [0xDEADBEEF]);
 + }
 ++*/
 + 
 + #[test]
 + #[should_panic(expected = "index 0 greater than length of slice")]
 +--
 +2.21.0 (Apple Git-122)
index 73e9c858caf2b6dbb51419d804152e83045a3ab8,0000000000000000000000000000000000000000..ee5ba449fb8e6bb0ee310eea54ac65a01ec8842a
mode 100644,000000..100644
--- /dev/null
@@@ -1,69 -1,0 +1,49 @@@
- diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs
- index 3e00e0a..8e5663b 100644
- --- a/library/core/tests/slice.rs
- +++ b/library/core/tests/slice.rs
- @@ -2108,6 +2108,7 @@ fn test_copy_within_panics_src_out_of_bounds() {
-      bytes.copy_within(usize::MAX..=usize::MAX, 0);
-  }
-  
- +/*
-  #[test]
-  fn test_is_sorted() {
-      let empty: [i32; 0] = [];
- @@ -2122,6 +2123,7 @@ fn test_is_sorted() {
-      assert!(!["c", "bb", "aaa"].is_sorted());
-      assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
-  }
- +*/
-  
-  #[test]
-  fn test_slice_run_destructors() {
 +From dd82e95c9de212524e14fc60155de1ae40156dfc Mon Sep 17 00:00:00 2001
 +From: bjorn3 <bjorn3@users.noreply.github.com>
 +Date: Sun, 24 Nov 2019 15:34:06 +0100
 +Subject: [PATCH] [core] Ignore failing tests
 +
 +---
 + library/core/tests/iter.rs       |  4 ++++
 + library/core/tests/num/bignum.rs | 10 ++++++++++
 + library/core/tests/num/mod.rs    |  5 +++--
 + library/core/tests/time.rs       |  1 +
 + 4 files changed, 18 insertions(+), 2 deletions(-)
 +
 +diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs
 +index 4bc44e9..8e3c7a4 100644
 +--- a/library/core/tests/array.rs
 ++++ b/library/core/tests/array.rs
 +@@ -242,6 +242,7 @@ fn iterator_drops() {
 +     assert_eq!(i.get(), 5);
 + }
 + 
 ++/*
 + // This test does not work on targets without panic=unwind support.
 + // To work around this problem, test is marked is should_panic, so it will
 + // be automagically skipped on unsuitable targets, such as
 +@@ -283,6 +284,7 @@ fn array_default_impl_avoids_leaks_on_panic() {
 +     assert_eq!(COUNTER.load(Relaxed), 0);
 +     panic!("test succeeded")
 + }
 ++*/
 + 
 + #[test]
 + fn empty_array_is_always_default() {
 +@@ -304,6 +304,7 @@ fn array_map() {
 +     assert_eq!(b, [1, 2, 3]);
 + }
 + 
 ++/*
 + // See note on above test for why `should_panic` is used.
 + #[test]
 + #[should_panic(expected = "test succeeded")]
 +@@ -332,6 +333,7 @@ fn array_map_drop_safety() {
 +     assert_eq!(DROPPED.load(Ordering::SeqCst), num_to_create);
 +     panic!("test succeeded")
 + }
 ++*/
 + 
 + #[test]
 + fn cell_allows_array_cycle() {
 +-- 2.21.0 (Apple Git-122)
index 8954f91021f47ab9ccd2e2e3fd0b1227aecb3bc6,0000000000000000000000000000000000000000..03900ba101a900b67f484827b237a9842449a9cc
mode 100644,000000..100644
--- /dev/null
@@@ -1,24 -1,0 +1,228 @@@
- index ec70034..7cd9e21 100644
 +From b1ae000f6da1abd3b8e9b80c40bc11c89b8ae93c Mon Sep 17 00:00:00 2001
 +From: bjorn3 <bjorn3@users.noreply.github.com>
 +Date: Thu, 30 Dec 2021 16:54:40 +0100
 +Subject: [PATCH] [core] Disable portable-simd test
 +
 +---
 + library/core/tests/lib.rs | 1 -
 + 1 file changed, 1 deletion(-)
 +
++diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs
++index aa1ad93..95fbf55 100644
++--- a/library/core/src/lib.rs
+++++ b/library/core/src/lib.rs
++@@ -398,23 +398,4 @@ pub mod arch {
++     }
++ }
++ 
++-// Pull in the `core_simd` crate directly into libcore. The contents of
++-// `core_simd` are in a different repository: rust-lang/portable-simd.
++-//
++-// `core_simd` depends on libcore, but the contents of this module are
++-// set up in such a way that directly pulling it here works such that the
++-// crate uses this crate as its libcore.
++-#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
++-#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn, unused_unsafe)]
++-#[allow(rustdoc::bare_urls)]
++-#[unstable(feature = "portable_simd", issue = "86656")]
++-mod core_simd;
++-
++-#[doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
++-#[unstable(feature = "portable_simd", issue = "86656")]
++-pub mod simd {
++-    #[unstable(feature = "portable_simd", issue = "86656")]
++-    pub use crate::core_simd::simd::*;
++-}
++-
++ include!("primitive_docs.rs");
++diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
++index cd38c3a..ad632dc 100644
++--- a/library/core/src/slice/mod.rs
+++++ b/library/core/src/slice/mod.rs
++@@ -17,6 +17,5 @@ use crate::ptr;
++ use crate::result::Result;
++ use crate::result::Result::{Err, Ok};
++-use crate::simd::{self, Simd};
++ use crate::slice;
++ 
++ #[unstable(
++@@ -3475,121 +3474,6 @@ impl<T> [T] {
++         }
++     }
++ 
++-    /// Split a slice into a prefix, a middle of aligned SIMD types, and a suffix.
++-    ///
++-    /// This is a safe wrapper around [`slice::align_to`], so has the same weak
++-    /// postconditions as that method.  You're only assured that
++-    /// `self.len() == prefix.len() + middle.len() * LANES + suffix.len()`.
++-    ///
++-    /// Notably, all of the following are possible:
++-    /// - `prefix.len() >= LANES`.
++-    /// - `middle.is_empty()` despite `self.len() >= 3 * LANES`.
++-    /// - `suffix.len() >= LANES`.
++-    ///
++-    /// That said, this is a safe method, so if you're only writing safe code,
++-    /// then this can at most cause incorrect logic, not unsoundness.
++-    ///
++-    /// # Panics
++-    ///
++-    /// This will panic if the size of the SIMD type is different from
++-    /// `LANES` times that of the scalar.
++-    ///
++-    /// At the time of writing, the trait restrictions on `Simd<T, LANES>` keeps
++-    /// that from ever happening, as only power-of-two numbers of lanes are
++-    /// supported.  It's possible that, in the future, those restrictions might
++-    /// be lifted in a way that would make it possible to see panics from this
++-    /// method for something like `LANES == 3`.
++-    ///
++-    /// # Examples
++-    ///
++-    /// ```
++-    /// #![feature(portable_simd)]
++-    ///
++-    /// let short = &[1, 2, 3];
++-    /// let (prefix, middle, suffix) = short.as_simd::<4>();
++-    /// assert_eq!(middle, []); // Not enough elements for anything in the middle
++-    ///
++-    /// // They might be split in any possible way between prefix and suffix
++-    /// let it = prefix.iter().chain(suffix).copied();
++-    /// assert_eq!(it.collect::<Vec<_>>(), vec![1, 2, 3]);
++-    ///
++-    /// fn basic_simd_sum(x: &[f32]) -> f32 {
++-    ///     use std::ops::Add;
++-    ///     use std::simd::f32x4;
++-    ///     let (prefix, middle, suffix) = x.as_simd();
++-    ///     let sums = f32x4::from_array([
++-    ///         prefix.iter().copied().sum(),
++-    ///         0.0,
++-    ///         0.0,
++-    ///         suffix.iter().copied().sum(),
++-    ///     ]);
++-    ///     let sums = middle.iter().copied().fold(sums, f32x4::add);
++-    ///     sums.reduce_sum()
++-    /// }
++-    ///
++-    /// let numbers: Vec<f32> = (1..101).map(|x| x as _).collect();
++-    /// assert_eq!(basic_simd_sum(&numbers[1..99]), 4949.0);
++-    /// ```
++-    #[unstable(feature = "portable_simd", issue = "86656")]
++-    pub fn as_simd<const LANES: usize>(&self) -> (&[T], &[Simd<T, LANES>], &[T])
++-    where
++-        Simd<T, LANES>: AsRef<[T; LANES]>,
++-        T: simd::SimdElement,
++-        simd::LaneCount<LANES>: simd::SupportedLaneCount,
++-    {
++-        // These are expected to always match, as vector types are laid out like
++-        // arrays per <https://llvm.org/docs/LangRef.html#vector-type>, but we
++-        // might as well double-check since it'll optimize away anyhow.
++-        assert_eq!(mem::size_of::<Simd<T, LANES>>(), mem::size_of::<[T; LANES]>());
++-
++-        // SAFETY: The simd types have the same layout as arrays, just with
++-        // potentially-higher alignment, so the de-facto transmutes are sound.
++-        unsafe { self.align_to() }
++-    }
++-
++-    /// Split a slice into a prefix, a middle of aligned SIMD types, and a suffix.
++-    ///
++-    /// This is a safe wrapper around [`slice::align_to_mut`], so has the same weak
++-    /// postconditions as that method.  You're only assured that
++-    /// `self.len() == prefix.len() + middle.len() * LANES + suffix.len()`.
++-    ///
++-    /// Notably, all of the following are possible:
++-    /// - `prefix.len() >= LANES`.
++-    /// - `middle.is_empty()` despite `self.len() >= 3 * LANES`.
++-    /// - `suffix.len() >= LANES`.
++-    ///
++-    /// That said, this is a safe method, so if you're only writing safe code,
++-    /// then this can at most cause incorrect logic, not unsoundness.
++-    ///
++-    /// This is the mutable version of [`slice::as_simd`]; see that for examples.
++-    ///
++-    /// # Panics
++-    ///
++-    /// This will panic if the size of the SIMD type is different from
++-    /// `LANES` times that of the scalar.
++-    ///
++-    /// At the time of writing, the trait restrictions on `Simd<T, LANES>` keeps
++-    /// that from ever happening, as only power-of-two numbers of lanes are
++-    /// supported.  It's possible that, in the future, those restrictions might
++-    /// be lifted in a way that would make it possible to see panics from this
++-    /// method for something like `LANES == 3`.
++-    #[unstable(feature = "portable_simd", issue = "86656")]
++-    pub fn as_simd_mut<const LANES: usize>(&mut self) -> (&mut [T], &mut [Simd<T, LANES>], &mut [T])
++-    where
++-        Simd<T, LANES>: AsMut<[T; LANES]>,
++-        T: simd::SimdElement,
++-        simd::LaneCount<LANES>: simd::SupportedLaneCount,
++-    {
++-        // These are expected to always match, as vector types are laid out like
++-        // arrays per <https://llvm.org/docs/LangRef.html#vector-type>, but we
++-        // might as well double-check since it'll optimize away anyhow.
++-        assert_eq!(mem::size_of::<Simd<T, LANES>>(), mem::size_of::<[T; LANES]>());
++-
++-        // SAFETY: The simd types have the same layout as arrays, just with
++-        // potentially-higher alignment, so the de-facto transmutes are sound.
++-        unsafe { self.align_to_mut() }
++-    }
++-
++     /// Checks if the elements of this slice are sorted.
++     ///
++     /// That is, for each element `a` and its following element `b`, `a <= b` must hold. If the
 +diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs
- @@ -121,7 +121,6 @@ mod pattern;
-  mod pin;
++index 06c7be0..359e2e7 100644
 +--- a/library/core/tests/lib.rs
 ++++ b/library/core/tests/lib.rs
- -- 
++@@ -75,7 +75,6 @@
++ #![feature(never_type)]
++ #![feature(unwrap_infallible)]
++ #![feature(result_into_ok_or_err)]
++-#![feature(portable_simd)]
++ #![feature(ptr_metadata)]
++ #![feature(once_cell)]
++ #![feature(option_result_contains)]
++@@ -127,7 +126,6 @@ mod pin;
++ mod pin_macro;
 + mod ptr;
 + mod result;
 +-mod simd;
 + mod slice;
 + mod str;
 + mod str_lossy;
++diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
++index 5dc586d..b6fc48f 100644
++--- a/library/std/src/lib.rs
+++++ b/library/std/src/lib.rs
++@@ -312,6 +312,5 @@
++ #![feature(panic_can_unwind)]
++ #![feature(panic_unwind)]
++ #![feature(platform_intrinsics)]
++-#![feature(portable_simd)]
++ #![feature(prelude_import)]
++ #![feature(ptr_as_uninit)]
++@@ -508,23 +508,6 @@ pub mod time;
++ #[unstable(feature = "once_cell", issue = "74465")]
++ pub mod lazy;
++ 
++-// Pull in `std_float` crate  into libstd. The contents of
++-// `std_float` are in a different repository: rust-lang/portable-simd.
++-#[path = "../../portable-simd/crates/std_float/src/lib.rs"]
++-#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn, unused_unsafe)]
++-#[allow(rustdoc::bare_urls)]
++-#[unstable(feature = "portable_simd", issue = "86656")]
++-mod std_float;
++-
++-#[doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
++-#[unstable(feature = "portable_simd", issue = "86656")]
++-pub mod simd {
++-    #[doc(inline)]
++-    pub use crate::std_float::StdFloat;
++-    #[doc(inline)]
++-    pub use core::simd::*;
++-}
++-
++ #[stable(feature = "futures_api", since = "1.36.0")]
++ pub mod task {
++     //! Types and Traits for working with asynchronous tasks.
++--
 +2.26.2.7.g19db9cfb68
 +
index bf74a74c7c4b8dd44ede03324d707a3bd89acfa2,0000000000000000000000000000000000000000..dc1beae6d2e71311801af88a5d8b496c0cbc51a4
mode 100644,000000..100644
--- /dev/null
@@@ -1,30 -1,0 +1,32 @@@
- From 0ffdd8eda8df364391c8ac6e1ce92c73ba9254d4 Mon Sep 17 00:00:00 2001
++From eb703e627e7a84f1cd8d0d87f0f69da1f0acf765 Mon Sep 17 00:00:00 2001
 +From: bjorn3 <bjorn3@users.noreply.github.com>
 +Date: Fri, 3 Dec 2021 12:16:30 +0100
 +Subject: [PATCH] Disable long running tests
 +
 +---
-  library/core/tests/slice.rs | 3 +++
-  1 file changed, 3 insertions(+)
++ library/core/tests/slice.rs | 2 ++
++ 1 file changed, 2 insertions(+)
 +
 +diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs
- index 2c8f00a..44847ee 100644
++index 8402833..84592e0 100644
 +--- a/library/core/tests/slice.rs
 ++++ b/library/core/tests/slice.rs
- @@ -2332,7 +2332,8 @@ macro_rules! empty_max_mut {
-      };
-  }
++@@ -2462,6 +2462,7 @@ take_tests! {
++ #[cfg(not(miri))] // unused in Miri
++ const EMPTY_MAX: &'static [()] = &[(); usize::MAX];
 + 
 ++/*
-  #[cfg(not(miri))] // Comparing usize::MAX many elements takes forever in Miri (and in rustc without optimizations)
-  take_tests! {
-      slice: &[(); usize::MAX], method: take,
-      (take_in_bounds_max_range_to, (..usize::MAX), Some(EMPTY_MAX), &[(); 0]),
- @@ -2345,3 +2347,4 @@ take_tests! {
++ // can't be a constant due to const mutability rules
++ #[cfg(not(miri))] // unused in Miri
++ macro_rules! empty_max_mut {
++@@ -2485,6 +2486,7 @@ take_tests! {
 +     (take_mut_oob_max_range_to_inclusive, (..=usize::MAX), None, empty_max_mut!()),
 +     (take_mut_in_bounds_max_range_from, (usize::MAX..), Some(&mut [] as _), empty_max_mut!()),
 + }
 ++*/
++ 
++ #[test]
++ fn test_slice_from_ptr_range() {
 +-- 
 +2.26.2.7.g19db9cfb68
 +
index ccf535098301511a2dbaea33bf6dede139aea814,0000000000000000000000000000000000000000..3896775a0b9d27045c0dee3754bbade61027f28e
mode 100755,000000..100755
--- /dev/null
@@@ -1,5 -1,0 +1,4 @@@
- rustup component add rust-src rustc-dev llvm-tools-preview
 +#!/bin/bash --verbose
 +set -e
 +
 +./build_sysroot/prepare_sysroot_src.sh
index ee0822f6c31457c6b4f125a765e613221d466537,0000000000000000000000000000000000000000..db14ea2bebca557044f875a723bdae0fb54cd56e
mode 100644,000000..100644
--- /dev/null
@@@ -1,1 -1,0 +1,3 @@@
- nightly-2021-12-30
++[toolchain]
++channel = "nightly-2022-03-26"
++components = ["rust-src", "rustc-dev", "llvm-tools-preview"]
index a8b1e70e2bb801e432b5940d126bd4cad33eca63,0000000000000000000000000000000000000000..2c796d0f69e593f92efba98ff69cfd0299c1a698
mode 100644,000000..100644
--- /dev/null
@@@ -1,160 -1,0 +1,179 @@@
- use gccjit::{ToRValue, Type};
++use gccjit::{ToLValue, ToRValue, Type};
 +use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
++use rustc_data_structures::stable_set::FxHashSet;
 +use rustc_middle::bug;
 +use rustc_middle::ty::Ty;
 +use rustc_target::abi::call::{CastTarget, FnAbi, PassMode, Reg, RegKind};
 +
 +use crate::builder::Builder;
 +use crate::context::CodegenCx;
 +use crate::intrinsic::ArgAbiExt;
 +use crate::type_of::LayoutGccExt;
 +
 +impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
 +    fn apply_attrs_callsite(&mut self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>, _callsite: Self::Value) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn get_param(&mut self, index: usize) -> Self::Value {
-         self.cx.current_func.borrow().expect("current func")
-             .get_param(index as i32)
-             .to_rvalue()
++        let func = self.current_func();
++        let param = func.get_param(index as i32);
++        let on_stack =
++            if let Some(on_stack_param_indices) = self.on_stack_function_params.borrow().get(&func) {
++                on_stack_param_indices.contains(&index)
++            }
++            else {
++                false
++            };
++        if on_stack {
++            param.to_lvalue().get_address(None)
++        }
++        else {
++            param.to_rvalue()
++        }
 +    }
 +}
 +
 +impl GccType for CastTarget {
 +    fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
 +        let rest_gcc_unit = self.rest.unit.gcc_type(cx);
 +        let (rest_count, rem_bytes) =
 +            if self.rest.unit.size.bytes() == 0 {
 +                (0, 0)
 +            }
 +            else {
 +                (self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
 +            };
 +
 +        if self.prefix.iter().all(|x| x.is_none()) {
 +            // Simplify to a single unit when there is no prefix and size <= unit size
 +            if self.rest.total <= self.rest.unit.size {
 +                return rest_gcc_unit;
 +            }
 +
 +            // Simplify to array when all chunks are the same size and type
 +            if rem_bytes == 0 {
 +                return cx.type_array(rest_gcc_unit, rest_count);
 +            }
 +        }
 +
 +        // Create list of fields in the main structure
 +        let mut args: Vec<_> = self
 +            .prefix
 +            .iter()
 +            .flat_map(|option_reg| {
 +                option_reg.map(|reg| reg.gcc_type(cx))
 +            })
 +            .chain((0..rest_count).map(|_| rest_gcc_unit))
 +            .collect();
 +
 +        // Append final integer
 +        if rem_bytes != 0 {
 +            // Only integers can be really split further.
 +            assert_eq!(self.rest.unit.kind, RegKind::Integer);
 +            args.push(cx.type_ix(rem_bytes * 8));
 +        }
 +
 +        cx.type_struct(&args, false)
 +    }
 +}
 +
 +pub trait GccType {
 +    fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc>;
 +}
 +
 +impl GccType for Reg {
 +    fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
 +        match self.kind {
 +            RegKind::Integer => cx.type_ix(self.size.bits()),
 +            RegKind::Float => {
 +                match self.size.bits() {
 +                    32 => cx.type_f32(),
 +                    64 => cx.type_f64(),
 +                    _ => bug!("unsupported float: {:?}", self),
 +                }
 +            },
 +            RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
 +        }
 +    }
 +}
 +
 +pub trait FnAbiGccExt<'gcc, 'tcx> {
 +    // TODO(antoyo): return a function pointer type instead?
-     fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool);
++    fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool, FxHashSet<usize>);
 +    fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
 +}
 +
 +impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
-     fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool) {
++    fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool, FxHashSet<usize>) {
++        let mut on_stack_param_indices = FxHashSet::default();
 +        let args_capacity: usize = self.args.iter().map(|arg|
 +            if arg.pad.is_some() {
 +                1
 +            }
 +            else {
 +                0
 +            } +
 +            if let PassMode::Pair(_, _) = arg.mode {
 +                2
 +            } else {
 +                1
 +            }
 +        ).sum();
 +        let mut argument_tys = Vec::with_capacity(
 +            if let PassMode::Indirect { .. } = self.ret.mode {
 +                1
 +            }
 +            else {
 +                0
 +            } + args_capacity,
 +        );
 +
 +        let return_ty =
 +            match self.ret.mode {
 +                PassMode::Ignore => cx.type_void(),
 +                PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
 +                PassMode::Cast(cast) => cast.gcc_type(cx),
 +                PassMode::Indirect { .. } => {
 +                    argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
 +                    cx.type_void()
 +                }
 +            };
 +
 +        for arg in &self.args {
 +            // add padding
 +            if let Some(ty) = arg.pad {
 +                argument_tys.push(ty.gcc_type(cx));
 +            }
 +
 +            let arg_ty = match arg.mode {
 +                PassMode::Ignore => continue,
 +                PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
 +                PassMode::Pair(..) => {
 +                    argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 0, true));
 +                    argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 1, true));
 +                    continue;
 +                }
 +                PassMode::Indirect { extra_attrs: Some(_), .. } => {
 +                    unimplemented!();
 +                }
 +                PassMode::Cast(cast) => cast.gcc_type(cx),
-                 PassMode::Indirect { extra_attrs: None, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
++                PassMode::Indirect { extra_attrs: None, on_stack: true, .. } => {
++                    on_stack_param_indices.insert(argument_tys.len());
++                    arg.memory_ty(cx)
++                },
++                PassMode::Indirect { extra_attrs: None, on_stack: false, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
 +            };
 +            argument_tys.push(arg_ty);
 +        }
 +
-         (return_ty, argument_tys, self.c_variadic)
++        (return_ty, argument_tys, self.c_variadic, on_stack_param_indices)
 +    }
 +
 +    fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
-         let (return_type, params, variadic) = self.gcc_type(cx);
++        let (return_type, params, variadic, on_stack_param_indices) = self.gcc_type(cx);
 +        let pointer_type = cx.context.new_function_pointer_type(None, return_type, &params, variadic);
++        cx.on_stack_params.borrow_mut().insert(pointer_type.dyncast_function_ptr_type().expect("function ptr type"), on_stack_param_indices);
 +        pointer_type
 +    }
 +}
index 334ef32f1d1d7471f7bb6b2bf0b511974cfeae9b,0000000000000000000000000000000000000000..b503bd020f6bb87a24267ca96edeb177fc06df43
mode 100644,000000..100644
--- /dev/null
@@@ -1,78 -1,0 +1,83 @@@
-                 if env::var("CG_GCCJIT_DUMP_MODULE").as_deref() == Ok(&module.name) {
 +use std::{env, fs};
 +
 +use gccjit::OutputKind;
 +use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
 +use rustc_codegen_ssa::back::write::{CodegenContext, EmitObj, ModuleConfig};
 +use rustc_errors::Handler;
 +use rustc_session::config::OutputType;
 +use rustc_span::fatal_error::FatalError;
 +use rustc_target::spec::SplitDebuginfo;
 +
 +use crate::{GccCodegenBackend, GccContext};
 +
 +pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, module: ModuleCodegen<GccContext>, config: &ModuleConfig) -> Result<CompiledModule, FatalError> {
 +    let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &module.name[..]);
 +    {
 +        let context = &module.module_llvm.context;
 +
 +        let module_name = module.name.clone();
 +        let module_name = Some(&module_name[..]);
 +
 +        let _bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
 +        let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
 +
 +        if config.bitcode_needed() {
 +            // TODO(antoyo)
 +        }
 +
 +        if config.emit_ir {
 +            unimplemented!();
 +        }
 +
 +        if config.emit_asm {
 +            let _timer = cgcx
 +                .prof
 +                .generic_activity_with_arg("LLVM_module_codegen_emit_asm", &*module.name);
 +            let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
 +            context.compile_to_file(OutputKind::Assembler, path.to_str().expect("path to str"));
 +        }
 +
 +        match config.emit_obj {
 +            EmitObj::ObjectCode(_) => {
 +                let _timer = cgcx
 +                    .prof
 +                    .generic_activity_with_arg("LLVM_module_codegen_emit_obj", &*module.name);
 +                if env::var("CG_GCCJIT_DUMP_MODULE_NAMES").as_deref() == Ok("1") {
 +                    println!("Module {}", module.name);
 +                }
++                if env::var("CG_GCCJIT_DUMP_ALL_MODULES").as_deref() == Ok("1") || env::var("CG_GCCJIT_DUMP_MODULE").as_deref() == Ok(&module.name) {
 +                    println!("Dumping reproducer {}", module.name);
 +                    let _ = fs::create_dir("/tmp/reproducers");
 +                    // FIXME(antoyo): segfault in dump_reproducer_to_file() might be caused by
 +                    // transmuting an rvalue to an lvalue.
 +                    // Segfault is actually in gcc::jit::reproducer::get_identifier_as_lvalue
 +                    context.dump_reproducer_to_file(&format!("/tmp/reproducers/{}.c", module.name));
 +                    println!("Dumped reproducer {}", module.name);
 +                }
++                if env::var("CG_GCCJIT_DUMP_TO_FILE").as_deref() == Ok("1") {
++                    let _ = fs::create_dir("/tmp/gccjit_dumps");
++                    let path = &format!("/tmp/gccjit_dumps/{}.c", module.name);
++                    context.dump_to_file(path, true);
++                }
 +                context.compile_to_file(OutputKind::ObjectFile, obj_out.to_str().expect("path to str"));
 +            }
 +
 +            EmitObj::Bitcode => {
 +                // TODO(antoyo)
 +            }
 +
 +            EmitObj::None => {}
 +        }
 +    }
 +
 +    Ok(module.into_compiled_module(
 +        config.emit_obj != EmitObj::None,
 +        cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked,
 +        config.emit_bc,
 +        &cgcx.output_filenames,
 +    ))
 +}
 +
 +pub(crate) fn link(_cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, mut _modules: Vec<ModuleCodegen<GccContext>>) -> Result<ModuleCodegen<GccContext>, FatalError> {
 +    unimplemented!();
 +}
index 8b23e96066eed1307ffd46982cfe8e0c65b06252,0000000000000000000000000000000000000000..f5aca35cdcbc4e3bc90398e76af00ed514bbfe07
mode 100644,000000..100644
--- /dev/null
@@@ -1,135 -1,0 +1,144 @@@
- pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen<GccContext>, u64) {
 +use std::env;
 +use std::time::Instant;
 +
 +use gccjit::{
 +    Context,
 +    FunctionType,
 +    GlobalKind,
 +};
 +use rustc_middle::dep_graph;
 +use rustc_middle::ty::TyCtxt;
 +use rustc_middle::mir::mono::Linkage;
 +use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
 +use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
 +use rustc_codegen_ssa::mono_item::MonoItemExt;
 +use rustc_codegen_ssa::traits::DebugInfoMethods;
 +use rustc_session::config::DebugInfo;
 +use rustc_span::Symbol;
 +
 +use crate::GccContext;
 +use crate::builder::Builder;
 +use crate::context::CodegenCx;
 +
 +pub fn global_linkage_to_gcc(linkage: Linkage) -> GlobalKind {
 +    match linkage {
 +        Linkage::External => GlobalKind::Imported,
 +        Linkage::AvailableExternally => GlobalKind::Imported,
 +        Linkage::LinkOnceAny => unimplemented!(),
 +        Linkage::LinkOnceODR => unimplemented!(),
 +        Linkage::WeakAny => unimplemented!(),
 +        Linkage::WeakODR => unimplemented!(),
 +        Linkage::Appending => unimplemented!(),
 +        Linkage::Internal => GlobalKind::Internal,
 +        Linkage::Private => GlobalKind::Internal,
 +        Linkage::ExternalWeak => GlobalKind::Imported, // TODO(antoyo): should be weak linkage.
 +        Linkage::Common => unimplemented!(),
 +    }
 +}
 +
 +pub fn linkage_to_gcc(linkage: Linkage) -> FunctionType {
 +    match linkage {
 +        Linkage::External => FunctionType::Exported,
 +        Linkage::AvailableExternally => FunctionType::Extern,
 +        Linkage::LinkOnceAny => unimplemented!(),
 +        Linkage::LinkOnceODR => unimplemented!(),
 +        Linkage::WeakAny => FunctionType::Exported, // FIXME(antoyo): should be similar to linkonce.
 +        Linkage::WeakODR => unimplemented!(),
 +        Linkage::Appending => unimplemented!(),
 +        Linkage::Internal => FunctionType::Internal,
 +        Linkage::Private => FunctionType::Internal,
 +        Linkage::ExternalWeak => unimplemented!(),
 +        Linkage::Common => unimplemented!(),
 +    }
 +}
 +
-         cgu_name,
++pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_128bit_integers: bool) -> (ModuleCodegen<GccContext>, u64) {
 +    let prof_timer = tcx.prof.generic_activity("codegen_module");
 +    let start_time = Instant::now();
 +
 +    let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
 +    let (module, _) = tcx.dep_graph.with_task(
 +        dep_node,
 +        tcx,
-     fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<GccContext> {
++        (cgu_name, supports_128bit_integers),
 +        module_codegen,
 +        Some(dep_graph::hash_result),
 +    );
 +    let time_to_codegen = start_time.elapsed();
 +    drop(prof_timer);
 +
 +    // We assume that the cost to run GCC on a CGU is proportional to
 +    // the time we needed for codegenning it.
 +    let cost = time_to_codegen.as_secs() * 1_000_000_000 + time_to_codegen.subsec_nanos() as u64;
 +
-             let cx = CodegenCx::new(&context, cgu, tcx);
++    fn module_codegen(tcx: TyCtxt<'_>, (cgu_name, supports_128bit_integers): (Symbol, bool)) -> ModuleCodegen<GccContext> {
 +        let cgu = tcx.codegen_unit(cgu_name);
 +        // Instantiate monomorphizations without filling out definitions yet...
 +        //let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
 +        let context = Context::default();
 +        // TODO(antoyo): only set on x86 platforms.
 +        context.add_command_line_option("-masm=intel");
 +        for arg in &tcx.sess.opts.cg.llvm_args {
 +            context.add_command_line_option(arg);
 +        }
 +        // NOTE: an optimization (https://github.com/rust-lang/rustc_codegen_gcc/issues/53).
 +        context.add_command_line_option("-fno-semantic-interposition");
 +        // NOTE: Rust relies on LLVM not doing TBAA (https://github.com/rust-lang/unsafe-code-guidelines/issues/292).
 +        context.add_command_line_option("-fno-strict-aliasing");
++
++        if tcx.sess.opts.debugging_opts.function_sections.unwrap_or(tcx.sess.target.function_sections) {
++            context.add_command_line_option("-ffunction-sections");
++            context.add_command_line_option("-fdata-sections");
++        }
++
 +        if env::var("CG_GCCJIT_DUMP_CODE").as_deref() == Ok("1") {
 +            context.set_dump_code_on_compile(true);
 +        }
 +        if env::var("CG_GCCJIT_DUMP_GIMPLE").as_deref() == Ok("1") {
 +            context.set_dump_initial_gimple(true);
 +        }
 +        context.set_debug_info(true);
 +        if env::var("CG_GCCJIT_DUMP_EVERYTHING").as_deref() == Ok("1") {
 +            context.set_dump_everything(true);
 +        }
 +        if env::var("CG_GCCJIT_KEEP_INTERMEDIATES").as_deref() == Ok("1") {
 +            context.set_keep_intermediates(true);
 +        }
 +
++        // TODO(bjorn3): Remove once unwinding is properly implemented
++        context.set_allow_unreachable_blocks(true);
++
 +        {
++            let cx = CodegenCx::new(&context, cgu, tcx, supports_128bit_integers);
 +
 +            let mono_items = cgu.items_in_deterministic_order(tcx);
 +            for &(mono_item, (linkage, visibility)) in &mono_items {
 +                mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);
 +            }
 +
 +            // ... and now that we have everything pre-defined, fill out those definitions.
 +            for &(mono_item, _) in &mono_items {
 +                mono_item.define::<Builder<'_, '_, '_>>(&cx);
 +            }
 +
 +            // If this codegen unit contains the main function, also create the
 +            // wrapper here
 +            maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx);
 +
 +            // Finalize debuginfo
 +            if cx.sess().opts.debuginfo != DebugInfo::None {
 +                cx.debuginfo_finalize();
 +            }
 +        }
 +
 +        ModuleCodegen {
 +            name: cgu_name.to_string(),
 +            module_llvm: GccContext {
 +                context
 +            },
 +            kind: ModuleKind::Regular,
 +        }
 +    }
 +
 +    (module, cost)
 +}
index be3f6a12706f84c16ddafbb580c74a6ac87e0e6a,0000000000000000000000000000000000000000..b2f46e92eccbf72bb150d2e447b0c4719216326f
mode 100644,000000..100644
--- /dev/null
@@@ -1,1549 -1,0 +1,1397 @@@
-     pub block: Option<Block<'gcc>>,
 +use std::borrow::Cow;
 +use std::cell::Cell;
 +use std::convert::TryFrom;
 +use std::ops::Deref;
 +
 +use gccjit::FunctionType;
 +use gccjit::{
 +    BinaryOp,
 +    Block,
 +    ComparisonOp,
 +    Function,
 +    LValue,
 +    RValue,
 +    ToRValue,
 +    Type,
 +    UnaryOp,
 +};
 +use rustc_codegen_ssa::MemFlags;
 +use rustc_codegen_ssa::common::{AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope};
 +use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
 +use rustc_codegen_ssa::mir::place::PlaceRef;
 +use rustc_codegen_ssa::traits::{
 +    BackendTypes,
 +    BaseTypeMethods,
 +    BuilderMethods,
 +    ConstMethods,
 +    DerivedTypeMethods,
 +    LayoutTypeMethods,
 +    HasCodegen,
 +    OverflowOp,
 +    StaticBuilderMethods,
 +};
++use rustc_data_structures::stable_set::FxHashSet;
 +use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
 +use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout};
 +use rustc_span::Span;
 +use rustc_span::def_id::DefId;
 +use rustc_target::abi::{
 +    self,
 +    call::FnAbi,
 +    Align,
 +    HasDataLayout,
 +    Size,
 +    TargetDataLayout,
 +    WrappingRange,
 +};
 +use rustc_target::spec::{HasTargetSpec, Target};
 +
 +use crate::common::{SignType, TypeReflection, type_is_pointer};
 +use crate::context::CodegenCx;
 +use crate::type_of::LayoutGccExt;
 +
 +// TODO(antoyo)
 +type Funclet = ();
 +
 +// TODO(antoyo): remove this variable.
 +static mut RETURN_VALUE_COUNT: usize = 0;
 +
 +enum ExtremumOperation {
 +    Max,
 +    Min,
 +}
 +
 +trait EnumClone {
 +    fn clone(&self) -> Self;
 +}
 +
 +impl EnumClone for AtomicOrdering {
 +    fn clone(&self) -> Self {
 +        match *self {
 +            AtomicOrdering::NotAtomic => AtomicOrdering::NotAtomic,
 +            AtomicOrdering::Unordered => AtomicOrdering::Unordered,
 +            AtomicOrdering::Monotonic => AtomicOrdering::Monotonic,
 +            AtomicOrdering::Acquire => AtomicOrdering::Acquire,
 +            AtomicOrdering::Release => AtomicOrdering::Release,
 +            AtomicOrdering::AcquireRelease => AtomicOrdering::AcquireRelease,
 +            AtomicOrdering::SequentiallyConsistent => AtomicOrdering::SequentiallyConsistent,
 +        }
 +    }
 +}
 +
 +pub struct Builder<'a: 'gcc, 'gcc, 'tcx> {
 +    pub cx: &'a CodegenCx<'gcc, 'tcx>,
-     fn with_cx(cx: &'a CodegenCx<'gcc, 'tcx>) -> Self {
++    pub block: Block<'gcc>,
 +    stack_var_count: Cell<usize>,
 +}
 +
 +impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
-             block: None,
++    fn with_cx(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
 +        Builder {
 +            cx,
-         let size = self.cx.int_width(src.get_type()) / 8;
++            block,
 +            stack_var_count: Cell::new(0),
 +        }
 +    }
 +
 +    fn atomic_extremum(&mut self, operation: ExtremumOperation, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
-         // NOTE: since jumps were added and compare_exchange doesn't expect this, the current blocks in the
++        let size = src.get_type().get_size();
 +
 +        let func = self.current_func();
 +
 +        let load_ordering =
 +            match order {
 +                // TODO(antoyo): does this make sense?
 +                AtomicOrdering::AcquireRelease | AtomicOrdering::Release => AtomicOrdering::Acquire,
 +                _ => order.clone(),
 +            };
 +        let previous_value = self.atomic_load(dst.get_type(), dst, load_ordering.clone(), Size::from_bytes(size));
 +        let previous_var = func.new_local(None, previous_value.get_type(), "previous_value");
 +        let return_value = func.new_local(None, previous_value.get_type(), "return_value");
 +        self.llbb().add_assignment(None, previous_var, previous_value);
 +        self.llbb().add_assignment(None, return_value, previous_var.to_rvalue());
 +
 +        let while_block = func.new_block("while");
 +        let after_block = func.new_block("after_while");
 +        self.llbb().end_with_jump(None, while_block);
 +
-         self.block = Some(while_block);
-         *self.cx.current_block.borrow_mut() = Some(while_block);
++        // NOTE: since jumps were added and compare_exchange doesn't expect this, the current block in the
 +        // state need to be updated.
-         // NOTE: since jumps were added in a place rustc does not expect, the current blocks in the
++        self.switch_to_block(while_block);
 +
 +        let comparison_operator =
 +            match operation {
 +                ExtremumOperation::Max => ComparisonOp::LessThan,
 +                ExtremumOperation::Min => ComparisonOp::GreaterThan,
 +            };
 +
 +        let cond1 = self.context.new_comparison(None, comparison_operator, previous_var.to_rvalue(), self.context.new_cast(None, src, previous_value.get_type()));
 +        let compare_exchange = self.compare_exchange(dst, previous_var, src, order, load_ordering, false);
 +        let cond2 = self.cx.context.new_unary_op(None, UnaryOp::LogicalNegate, compare_exchange.get_type(), compare_exchange);
 +        let cond = self.cx.context.new_binary_op(None, BinaryOp::LogicalAnd, self.cx.bool_type, cond1, cond2);
 +
 +        while_block.end_with_conditional(None, cond, while_block, after_block);
 +
-         self.block = Some(after_block);
-         *self.cx.current_block.borrow_mut() = Some(after_block);
++        // NOTE: since jumps were added in a place rustc does not expect, the current block in the
 +        // state need to be updated.
-         let size = self.cx.int_width(src.get_type());
-         let compare_exchange = self.context.get_builtin_function(&format!("__atomic_compare_exchange_{}", size / 8));
++        self.switch_to_block(after_block);
 +
 +        return_value.to_rvalue()
 +    }
 +
 +    fn compare_exchange(&self, dst: RValue<'gcc>, cmp: LValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
-             .map(|(_i, (expected_ty, &actual_val))| {
++        let size = src.get_type().get_size();
++        let compare_exchange = self.context.get_builtin_function(&format!("__atomic_compare_exchange_{}", size));
 +        let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
 +        let failure_order = self.context.new_rvalue_from_int(self.i32_type, failure_order.to_gcc());
 +        let weak = self.context.new_rvalue_from_int(self.bool_type, weak as i32);
 +
 +        let void_ptr_type = self.context.new_type::<*mut ()>();
 +        let volatile_void_ptr_type = void_ptr_type.make_volatile();
 +        let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
 +        let expected = self.context.new_cast(None, cmp.get_address(None), void_ptr_type);
 +
 +        // NOTE: not sure why, but we have the wrong type here.
 +        let int_type = compare_exchange.get_param(2).to_rvalue().get_type();
 +        let src = self.context.new_cast(None, src, int_type);
 +        self.context.new_call(None, compare_exchange, &[dst, expected, src, weak, order, failure_order])
 +    }
 +
 +    pub fn assign(&self, lvalue: LValue<'gcc>, value: RValue<'gcc>) {
 +        self.llbb().add_assignment(None, lvalue, value);
 +    }
 +
 +    fn check_call<'b>(&mut self, _typ: &str, func: Function<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
 +        let mut all_args_match = true;
 +        let mut param_types = vec![];
 +        let param_count = func.get_param_count();
 +        for (index, arg) in args.iter().enumerate().take(param_count) {
 +            let param = func.get_param(index as i32);
 +            let param = param.to_rvalue().get_type();
 +            if param != arg.get_type() {
 +                all_args_match = false;
 +            }
 +            param_types.push(param);
 +        }
 +
 +        if all_args_match {
 +            return Cow::Borrowed(args);
 +        }
 +
 +        let casted_args: Vec<_> = param_types
 +            .into_iter()
 +            .zip(args.iter())
 +            .enumerate()
 +            .map(|(_i, (expected_ty, &actual_val))| {
 +                let actual_ty = actual_val.get_type();
 +                if expected_ty != actual_ty {
 +                    self.bitcast(actual_val, expected_ty)
 +                }
 +                else {
 +                    actual_val
 +                }
 +            })
 +            .collect();
 +
 +        Cow::Owned(casted_args)
 +    }
 +
 +    fn check_ptr_call<'b>(&mut self, _typ: &str, func_ptr: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
 +        let mut all_args_match = true;
 +        let mut param_types = vec![];
 +        let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
 +        for (index, arg) in args.iter().enumerate().take(gcc_func.get_param_count()) {
 +            let param = gcc_func.get_param_type(index);
 +            if param != arg.get_type() {
 +                all_args_match = false;
 +            }
 +            param_types.push(param);
 +        }
 +
++        let mut on_stack_param_indices = FxHashSet::default();
++        if let Some(indices) = self.on_stack_params.borrow().get(&gcc_func) {
++            on_stack_param_indices = indices.clone();
++        }
++
 +        if all_args_match {
 +            return Cow::Borrowed(args);
 +        }
 +
 +        let casted_args: Vec<_> = param_types
 +            .into_iter()
 +            .zip(args.iter())
 +            .enumerate()
-                     self.bitcast(actual_val, expected_ty)
++            .map(|(index, (expected_ty, &actual_val))| {
 +                let actual_ty = actual_val.get_type();
 +                if expected_ty != actual_ty {
-         self.block.expect("block").get_function()
++                    if on_stack_param_indices.contains(&index) {
++                        actual_val.dereference(None).to_rvalue()
++                    }
++                    else {
++                        self.bitcast(actual_val, expected_ty)
++                    }
 +                }
 +                else {
 +                    actual_val
 +                }
 +            })
 +            .collect();
 +
 +        Cow::Owned(casted_args)
 +    }
 +
 +    fn check_store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
 +        let dest_ptr_ty = self.cx.val_ty(ptr).make_pointer(); // TODO(antoyo): make sure make_pointer() is okay here.
 +        let stored_ty = self.cx.val_ty(val);
 +        let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
 +
 +        if dest_ptr_ty == stored_ptr_ty {
 +            ptr
 +        }
 +        else {
 +            self.bitcast(ptr, stored_ptr_ty)
 +        }
 +    }
 +
 +    pub fn current_func(&self) -> Function<'gcc> {
-         let current_block = self.current_block.borrow().expect("block");
++        self.block.get_function()
 +    }
 +
 +    fn function_call(&mut self, func: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
 +        // TODO(antoyo): remove when the API supports a different type for functions.
 +        let func: Function<'gcc> = self.cx.rvalue_as_function(func);
 +        let args = self.check_call("call", func, args);
 +
 +        // gccjit requires to use the result of functions, even when it's not used.
 +        // That's why we assign the result to a local or call add_eval().
 +        let return_type = func.get_return_type();
-         let current_func = current_block.get_function();
 +        let void_type = self.context.new_type::<()>();
-             current_block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
++        let current_func = self.block.get_function();
 +        if return_type != void_type {
 +            unsafe { RETURN_VALUE_COUNT += 1 };
 +            let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
-             current_block.add_eval(None, self.cx.context.new_call(None, func, &args));
++            self.block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
 +            result.to_rvalue()
 +        }
 +        else {
-         let current_block = self.current_block.borrow().expect("block");
++            self.block.add_eval(None, self.cx.context.new_call(None, func, &args));
 +            // Return dummy value when not having return value.
 +            self.context.new_rvalue_from_long(self.isize_type, 0)
 +        }
 +    }
 +
 +    fn function_ptr_call(&mut self, func_ptr: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
 +        let args = self.check_ptr_call("call", func_ptr, args);
 +
 +        // gccjit requires to use the result of functions, even when it's not used.
 +        // That's why we assign the result to a local or call add_eval().
 +        let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
 +        let mut return_type = gcc_func.get_return_type();
-         let current_func = current_block.get_function();
 +        let void_type = self.context.new_type::<()>();
-             let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
-             current_block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
++        let current_func = self.block.get_function();
 +
 +        // FIXME(antoyo): As a temporary workaround for unsupported LLVM intrinsics.
 +        if gcc_func.get_param_count() == 0 && format!("{:?}", func_ptr) == "__builtin_ia32_pmovmskb128" {
 +            return_type = self.int_type;
 +        }
 +
 +        if return_type != void_type {
 +            unsafe { RETURN_VALUE_COUNT += 1 };
-                 current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &[]));
++            let result = current_func.new_local(None, return_type, &format!("ptrReturnValue{}", unsafe { RETURN_VALUE_COUNT }));
++            self.block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
 +            result.to_rvalue()
 +        }
 +        else {
 +            if gcc_func.get_param_count() == 0 {
 +                // FIXME(antoyo): As a temporary workaround for unsupported LLVM intrinsics.
-                 current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
++                self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &[]));
 +            }
 +            else {
-             current_block.add_assignment(None, result, self.context.new_rvalue_from_long(self.isize_type, 0));
++                self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
 +            }
 +            // Return dummy value when not having return value.
 +            let result = current_func.new_local(None, self.isize_type, "dummyValueThatShouldNeverBeUsed");
-     pub fn overflow_call(&mut self, func: Function<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
++            self.block.add_assignment(None, result, self.context.new_rvalue_from_long(self.isize_type, 0));
 +            result.to_rvalue()
 +        }
 +    }
 +
-         let current_block = self.current_block.borrow().expect("block");
-         let current_func = current_block.get_function();
++    pub fn overflow_call(&self, func: Function<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
 +        // gccjit requires to use the result of functions, even when it's not used.
 +        // That's why we assign the result to a local.
 +        let return_type = self.context.new_type::<bool>();
-         let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
-         current_block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
++        let current_func = self.block.get_function();
 +        // TODO(antoyo): return the new_call() directly? Since the overflow function has no side-effects.
 +        unsafe { RETURN_VALUE_COUNT += 1 };
-         let mut bx = Builder::with_cx(cx);
-         *cx.current_block.borrow_mut() = Some(block);
-         bx.block = Some(block);
-         bx
++        let result = current_func.new_local(None, return_type, &format!("overflowReturnValue{}", unsafe { RETURN_VALUE_COUNT }));
++        self.block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
 +        result.to_rvalue()
 +    }
 +}
 +
 +impl<'gcc, 'tcx> HasCodegen<'tcx> for Builder<'_, 'gcc, 'tcx> {
 +    type CodegenCx = CodegenCx<'gcc, 'tcx>;
 +}
 +
 +impl<'tcx> HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
 +    fn tcx(&self) -> TyCtxt<'tcx> {
 +        self.cx.tcx()
 +    }
 +}
 +
 +impl HasDataLayout for Builder<'_, '_, '_> {
 +    fn data_layout(&self) -> &TargetDataLayout {
 +        self.cx.data_layout()
 +    }
 +}
 +
 +impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
 +    type LayoutOfResult = TyAndLayout<'tcx>;
 +
 +    #[inline]
 +    fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
 +        self.cx.handle_layout_err(err, span, ty)
 +    }
 +}
 +
 +impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
 +    type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
 +
 +    #[inline]
 +    fn handle_fn_abi_err(
 +        &self,
 +        err: FnAbiError<'tcx>,
 +        span: Span,
 +        fn_abi_request: FnAbiRequest<'tcx>,
 +    ) -> ! {
 +        self.cx.handle_fn_abi_err(err, span, fn_abi_request)
 +    }
 +}
 +
 +impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
 +    type Target = CodegenCx<'gcc, 'tcx>;
 +
 +    fn deref(&self) -> &Self::Target {
 +        self.cx
 +    }
 +}
 +
 +impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
 +    type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
 +    type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
 +    type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
 +    type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
 +    type Funclet = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Funclet;
 +
 +    type DIScope = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIScope;
 +    type DILocation = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DILocation;
 +    type DIVariable = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIVariable;
 +}
 +
 +impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
 +    fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
-         self.block.expect("block")
++        Builder::with_cx(cx, block)
 +    }
 +
 +    fn llbb(&self) -> Block<'gcc> {
-         *self.cx.current_block.borrow_mut() = Some(block);
-         self.block = Some(block);
++        self.block
 +    }
 +
 +    fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: &str) -> Block<'gcc> {
 +        let func = cx.rvalue_as_function(func);
 +        func.new_block(name)
 +    }
 +
 +    fn append_sibling_block(&mut self, name: &str) -> Block<'gcc> {
 +        let func = self.current_func();
 +        func.new_block(name)
 +    }
 +
 +    fn switch_to_block(&mut self, block: Self::BasicBlock) {
-         self.block.expect("block").end_with_switch(None, value, default_block, &gcc_cases);
++        self.block = block;
 +    }
 +
 +    fn ret_void(&mut self) {
 +        self.llbb().end_with_void_return(None)
 +    }
 +
 +    fn ret(&mut self, value: RValue<'gcc>) {
 +        let value =
 +            if self.structs_as_pointer.borrow().contains(&value) {
 +                // NOTE: hack to workaround a limitation of the rustc API: see comment on
 +                // CodegenCx.structs_as_pointer
 +                value.dereference(None).to_rvalue()
 +            }
 +            else {
 +                value
 +            };
 +        self.llbb().end_with_return(None, value);
 +    }
 +
 +    fn br(&mut self, dest: Block<'gcc>) {
 +        self.llbb().end_with_jump(None, dest)
 +    }
 +
 +    fn cond_br(&mut self, cond: RValue<'gcc>, then_block: Block<'gcc>, else_block: Block<'gcc>) {
 +        self.llbb().end_with_conditional(None, cond, then_block, else_block)
 +    }
 +
 +    fn switch(&mut self, value: RValue<'gcc>, default_block: Block<'gcc>, cases: impl ExactSizeIterator<Item = (u128, Block<'gcc>)>) {
 +        let mut gcc_cases = vec![];
 +        let typ = self.val_ty(value);
 +        for (on_val, dest) in cases {
 +            let on_val = self.const_uint_big(typ, on_val);
 +            gcc_cases.push(self.context.new_case(on_val, on_val, dest));
 +        }
-     fn invoke(&mut self, _typ: Type<'gcc>, _func: RValue<'gcc>, _args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
-         let condition = self.context.new_rvalue_from_int(self.bool_type, 0);
++        self.block.end_with_switch(None, value, default_block, &gcc_cases);
 +    }
 +
-         self.context.new_rvalue_from_int(self.int_type, 0)
-         // TODO(antoyo)
++    fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
++        // TODO(bjorn3): Properly implement unwinding.
++        let call_site = self.call(typ, func, args, None);
++        let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
 +        self.llbb().end_with_conditional(None, condition, then, catch);
-         let block = self.block.expect("block");
-         block.add_eval(None, self.context.new_call(None, func, &[]));
-         let return_type = block.get_function().get_return_type();
++        call_site
 +    }
 +
 +    fn unreachable(&mut self) {
 +        let func = self.context.get_builtin_function("__builtin_unreachable");
-             block.end_with_void_return(None)
++        self.block.add_eval(None, self.context.new_call(None, func, &[]));
++        let return_type = self.block.get_function().get_return_type();
 +        let void_type = self.context.new_type::<()>();
 +        if return_type == void_type {
-             block.end_with_return(None, return_value)
++            self.block.end_with_void_return(None)
 +        }
 +        else {
 +            let return_value = self.current_func()
 +                .new_local(None, return_type, "unreachableReturn");
-     fn add(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
-         // FIXME(antoyo): this should not be required.
-         if format!("{:?}", a.get_type()) != format!("{:?}", b.get_type()) {
-             b = self.context.new_cast(None, b, a.get_type());
-         }
-         a + b
++            self.block.end_with_return(None, return_value)
 +        }
 +    }
 +
-     fn sub(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
-         if a.get_type() != b.get_type() {
-             b = self.context.new_cast(None, b, a.get_type());
-         }
-         a - b
++    fn add(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.gcc_add(a, b)
 +    }
 +
 +    fn fadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a + b
 +    }
 +
-         a * b
++    fn sub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.gcc_sub(a, b)
 +    }
 +
 +    fn fsub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a - b
 +    }
 +
 +    fn mul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         // TODO(antoyo): convert the arguments to unsigned?
-         a / b
++        self.gcc_mul(a, b)
 +    }
 +
 +    fn fmul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a * b
 +    }
 +
 +    fn udiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         // TODO(antoyo): convert the arguments to signed?
-         a / b
++        self.gcc_udiv(a, b)
 +    }
 +
 +    fn exactudiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): convert the arguments to unsigned?
 +        // TODO(antoyo): poison if not exact.
 +        a / b
 +    }
 +
 +    fn sdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         a % b
++        self.gcc_sdiv(a, b)
 +    }
 +
 +    fn exactsdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): posion if not exact.
 +        // FIXME(antoyo): rustc_codegen_ssa::mir::intrinsic uses different types for a and b but they
 +        // should be the same.
 +        let typ = a.get_type().to_signed(self);
 +        let b = self.context.new_cast(None, b, typ);
 +        a / b
 +    }
 +
 +    fn fdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a / b
 +    }
 +
 +    fn urem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         a % b
++        self.gcc_urem(a, b)
 +    }
 +
 +    fn srem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         // FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by an unsigned number.
-         let a_type = a.get_type();
-         let b_type = b.get_type();
-         if a_type.is_unsigned(self) && b_type.is_signed(self) {
-             let a = self.context.new_cast(None, a, b_type);
-             let result = a << b;
-             self.context.new_cast(None, result, a_type)
-         }
-         else if a_type.is_signed(self) && b_type.is_unsigned(self) {
-             let b = self.context.new_cast(None, b, a_type);
-             a << b
-         }
-         else {
-             a << b
-         }
++        self.gcc_srem(a, b)
 +    }
 +
 +    fn frem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        if a.get_type() == self.cx.float_type {
 +            let fmodf = self.context.get_builtin_function("fmodf");
 +            // FIXME(antoyo): this seems to produce the wrong result.
 +            return self.context.new_call(None, fmodf, &[a, b]);
 +        }
 +        assert_eq!(a.get_type(), self.cx.double_type);
 +
 +        let fmod = self.context.get_builtin_function("fmod");
 +        return self.context.new_call(None, fmod, &[a, b]);
 +    }
 +
 +    fn shl(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         // FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by an unsigned number.
-         // TODO(antoyo): cast to unsigned to do a logical shift if that does not work.
-         let a_type = a.get_type();
-         let b_type = b.get_type();
-         if a_type.is_unsigned(self) && b_type.is_signed(self) {
-             let a = self.context.new_cast(None, a, b_type);
-             let result = a >> b;
-             self.context.new_cast(None, result, a_type)
-         }
-         else if a_type.is_signed(self) && b_type.is_unsigned(self) {
-             let b = self.context.new_cast(None, b, a_type);
-             a >> b
-         }
-         else {
-             a >> b
-         }
++        self.gcc_shl(a, b)
 +    }
 +
 +    fn lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         // FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by an unsigned number.
-         let a_type = a.get_type();
-         let b_type = b.get_type();
-         if a_type.is_unsigned(self) && b_type.is_signed(self) {
-             let a = self.context.new_cast(None, a, b_type);
-             let result = a >> b;
-             self.context.new_cast(None, result, a_type)
-         }
-         else if a_type.is_signed(self) && b_type.is_unsigned(self) {
-             let b = self.context.new_cast(None, b, a_type);
-             a >> b
-         }
-         else {
-             a >> b
-         }
++        self.gcc_lshr(a, b)
 +    }
 +
 +    fn ashr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): check whether behavior is an arithmetic shift for >> .
-     fn and(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
-         if a.get_type() != b.get_type() {
-             b = self.context.new_cast(None, b, a.get_type());
-         }
-         a & b
++        // It seems to be if the value is signed.
++        self.gcc_lshr(a, b)
 +    }
 +
-     fn or(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
-         if a.get_type() != b.get_type() {
-             b = self.context.new_cast(None, b, a.get_type());
-         }
-         a | b
++    fn and(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.gcc_and(a, b)
 +    }
 +
-         a ^ b
++    fn or(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.cx.gcc_or(a, b)
 +    }
 +
 +    fn xor(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
++        self.gcc_xor(a, b)
 +    }
 +
 +    fn neg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
-         let operation =
-             if a.get_type().is_bool() {
-                 UnaryOp::LogicalNegate
-             }
-             else {
-                 UnaryOp::BitwiseNegate
-             };
-         self.cx.context.new_unary_op(None, operation, a.get_type(), a)
++        self.gcc_neg(a)
 +    }
 +
 +    fn fneg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
 +        self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
 +    }
 +
 +    fn not(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
-         a + b
++        self.gcc_not(a)
 +    }
 +
 +    fn unchecked_sadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a + b
 +    }
 +
 +    fn unchecked_uadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
-         a - b
++        self.gcc_add(a, b)
 +    }
 +
 +    fn unchecked_ssub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a - b
 +    }
 +
 +    fn unchecked_usub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): should generate poison value?
-         use rustc_middle::ty::{Int, IntTy::*, Uint, UintTy::*};
-         let new_kind =
-             match typ.kind() {
-                 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
-                 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
-                 t @ (Uint(_) | Int(_)) => t.clone(),
-                 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
-             };
-         // TODO(antoyo): remove duplication with intrinsic?
-         let name =
-             match oop {
-                 OverflowOp::Add =>
-                     match new_kind {
-                         Int(I8) => "__builtin_add_overflow",
-                         Int(I16) => "__builtin_add_overflow",
-                         Int(I32) => "__builtin_sadd_overflow",
-                         Int(I64) => "__builtin_saddll_overflow",
-                         Int(I128) => "__builtin_add_overflow",
-                         Uint(U8) => "__builtin_add_overflow",
-                         Uint(U16) => "__builtin_add_overflow",
-                         Uint(U32) => "__builtin_uadd_overflow",
-                         Uint(U64) => "__builtin_uaddll_overflow",
-                         Uint(U128) => "__builtin_add_overflow",
-                         _ => unreachable!(),
-                     },
-                 OverflowOp::Sub =>
-                     match new_kind {
-                         Int(I8) => "__builtin_sub_overflow",
-                         Int(I16) => "__builtin_sub_overflow",
-                         Int(I32) => "__builtin_ssub_overflow",
-                         Int(I64) => "__builtin_ssubll_overflow",
-                         Int(I128) => "__builtin_sub_overflow",
-                         Uint(U8) => "__builtin_sub_overflow",
-                         Uint(U16) => "__builtin_sub_overflow",
-                         Uint(U32) => "__builtin_usub_overflow",
-                         Uint(U64) => "__builtin_usubll_overflow",
-                         Uint(U128) => "__builtin_sub_overflow",
-                         _ => unreachable!(),
-                     },
-                 OverflowOp::Mul =>
-                     match new_kind {
-                         Int(I8) => "__builtin_mul_overflow",
-                         Int(I16) => "__builtin_mul_overflow",
-                         Int(I32) => "__builtin_smul_overflow",
-                         Int(I64) => "__builtin_smulll_overflow",
-                         Int(I128) => "__builtin_mul_overflow",
-                         Uint(U8) => "__builtin_mul_overflow",
-                         Uint(U16) => "__builtin_mul_overflow",
-                         Uint(U32) => "__builtin_umul_overflow",
-                         Uint(U64) => "__builtin_umulll_overflow",
-                         Uint(U128) => "__builtin_mul_overflow",
-                         _ => unreachable!(),
-                     },
-             };
-         let intrinsic = self.context.get_builtin_function(&name);
-         let res = self.current_func()
-             // TODO(antoyo): is it correct to use rhs type instead of the parameter typ?
-             .new_local(None, rhs.get_type(), "binopResult")
-             .get_address(None);
-         let overflow = self.overflow_call(intrinsic, &[lhs, rhs, res], None);
-         (res.dereference(None).to_rvalue(), overflow)
++        self.gcc_sub(a, b)
 +    }
 +
 +    fn unchecked_smul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a * b
 +    }
 +
 +    fn unchecked_umul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
 +        a * b
 +    }
 +
 +    fn fadd_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn fsub_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn fmul_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn fdiv_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn frem_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn checked_binop(&mut self, oop: OverflowOp, typ: Ty<'_>, lhs: Self::Value, rhs: Self::Value) -> (Self::Value, Self::Value) {
-         self.context.new_cast(None, value, dest_ty)
++        self.gcc_checked_binop(oop, typ, lhs, rhs)
 +    }
 +
 +    fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
 +        // FIXME(antoyo): this check that we don't call get_aligned() a second time on a type.
 +        // Ideally, we shouldn't need to do this check.
 +        let aligned_type =
 +            if ty == self.cx.u128_type || ty == self.cx.i128_type {
 +                ty
 +            }
 +            else {
 +                ty.get_aligned(align.bytes())
 +            };
 +        // TODO(antoyo): It might be better to return a LValue, but fixing the rustc API is non-trivial.
 +        self.stack_var_count.set(self.stack_var_count.get() + 1);
 +        self.current_func().new_local(None, aligned_type, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
 +    }
 +
 +    fn dynamic_alloca(&mut self, _ty: Type<'gcc>, _align: Align) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn array_alloca(&mut self, _ty: Type<'gcc>, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
 +        // TODO(antoyo): use ty.
 +        let block = self.llbb();
 +        let function = block.get_function();
 +        // NOTE: instead of returning the dereference here, we have to assign it to a variable in
 +        // the current basic block. Otherwise, it could be used in another basic block, causing a
 +        // dereference after a drop, for instance.
 +        // TODO(antoyo): handle align.
 +        let deref = ptr.dereference(None).to_rvalue();
 +        let value_type = deref.get_type();
 +        unsafe { RETURN_VALUE_COUNT += 1 };
 +        let loaded_value = function.new_local(None, value_type, &format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }));
 +        block.add_assignment(None, loaded_value, deref);
 +        loaded_value.to_rvalue()
 +    }
 +
 +    fn volatile_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): use ty.
 +        let ptr = self.context.new_cast(None, ptr, ptr.get_type().make_volatile());
 +        ptr.dereference(None).to_rvalue()
 +    }
 +
 +    fn atomic_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) -> RValue<'gcc> {
 +        // TODO(antoyo): use ty.
 +        // TODO(antoyo): handle alignment.
 +        let atomic_load = self.context.get_builtin_function(&format!("__atomic_load_{}", size.bytes()));
 +        let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
 +
 +        let volatile_const_void_ptr_type = self.context.new_type::<()>()
 +            .make_const()
 +            .make_volatile()
 +            .make_pointer();
 +        let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
 +        self.context.new_call(None, atomic_load, &[ptr, ordering])
 +    }
 +
 +    fn load_operand(&mut self, place: PlaceRef<'tcx, RValue<'gcc>>) -> OperandRef<'tcx, RValue<'gcc>> {
 +        assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
 +
 +        if place.layout.is_zst() {
 +            return OperandRef::new_zst(self, place.layout);
 +        }
 +
 +        fn scalar_load_metadata<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, load: RValue<'gcc>, scalar: &abi::Scalar) {
 +            let vr = scalar.valid_range.clone();
 +            match scalar.value {
 +                abi::Int(..) => {
 +                    if !scalar.is_always_valid(bx) {
 +                        bx.range_metadata(load, scalar.valid_range);
 +                    }
 +                }
 +                abi::Pointer if vr.start < vr.end && !vr.contains(0) => {
 +                    bx.nonnull_metadata(load);
 +                }
 +                _ => {}
 +            }
 +        }
 +
 +        let val =
 +            if let Some(llextra) = place.llextra {
 +                OperandValue::Ref(place.llval, Some(llextra), place.align)
 +            }
 +            else if place.layout.is_gcc_immediate() {
 +                let load = self.load(place.llval.get_type(), place.llval, place.align);
 +                if let abi::Abi::Scalar(ref scalar) = place.layout.abi {
 +                    scalar_load_metadata(self, load, scalar);
 +                }
 +                OperandValue::Immediate(self.to_immediate(load, place.layout))
 +            }
 +            else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
 +                let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
 +                let pair_type = place.layout.gcc_type(self, false);
 +
 +                let mut load = |i, scalar: &abi::Scalar, align| {
 +                    let llptr = self.struct_gep(pair_type, place.llval, i as u64);
 +                    let load = self.load(llptr.get_type(), llptr, align);
 +                    scalar_load_metadata(self, load, scalar);
 +                    if scalar.is_bool() { self.trunc(load, self.type_i1()) } else { load }
 +                };
 +
 +                OperandValue::Pair(
 +                    load(0, a, place.align),
 +                    load(1, b, place.align.restrict_for_offset(b_offset)),
 +                )
 +            }
 +            else {
 +                OperandValue::Ref(place.llval, None, place.align)
 +            };
 +
 +        OperandRef { val, layout: place.layout }
 +    }
 +
 +    fn write_operand_repeatedly(mut self, cg_elem: OperandRef<'tcx, RValue<'gcc>>, count: u64, dest: PlaceRef<'tcx, RValue<'gcc>>) -> Self {
 +        let zero = self.const_usize(0);
 +        let count = self.const_usize(count);
 +        let start = dest.project_index(&mut self, zero).llval;
 +        let end = dest.project_index(&mut self, count).llval;
 +
 +        let header_bb = self.append_sibling_block("repeat_loop_header");
 +        let body_bb = self.append_sibling_block("repeat_loop_body");
 +        let next_bb = self.append_sibling_block("repeat_loop_next");
 +
 +        let ptr_type = start.get_type();
 +        let current = self.llbb().get_function().new_local(None, ptr_type, "loop_var");
 +        let current_val = current.to_rvalue();
 +        self.assign(current, start);
 +
 +        self.br(header_bb);
 +
 +        self.switch_to_block(header_bb);
 +        let keep_going = self.icmp(IntPredicate::IntNE, current_val, end);
 +        self.cond_br(keep_going, body_bb, next_bb);
 +
 +        self.switch_to_block(body_bb);
 +        let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
 +        cg_elem.val.store(&mut self, PlaceRef::new_sized_aligned(current_val, cg_elem.layout, align));
 +
 +        let next = self.inbounds_gep(self.backend_type(cg_elem.layout), current.to_rvalue(), &[self.const_usize(1)]);
 +        self.llbb().add_assignment(None, current, next);
 +        self.br(header_bb);
 +
 +        self.switch_to_block(next_bb);
 +        self
 +    }
 +
 +    fn range_metadata(&mut self, _load: RValue<'gcc>, _range: WrappingRange) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn nonnull_metadata(&mut self, _load: RValue<'gcc>) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn type_metadata(&mut self, _function: RValue<'gcc>, _typeid: String) {
 +        // Unsupported.
 +    }
 +
 +    fn typeid_metadata(&mut self, _typeid: String) -> RValue<'gcc> {
 +        // Unsupported.
 +        self.context.new_rvalue_from_int(self.int_type, 0)
 +    }
 +
 +
 +    fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
 +        self.store_with_flags(val, ptr, align, MemFlags::empty())
 +    }
 +
 +    fn store_with_flags(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, _align: Align, _flags: MemFlags) -> RValue<'gcc> {
 +        let ptr = self.check_store(val, ptr);
 +        self.llbb().add_assignment(None, ptr.dereference(None), val);
 +        // TODO(antoyo): handle align and flags.
 +        // NOTE: dummy value here since it's never used. FIXME(antoyo): API should not return a value here?
 +        self.cx.context.new_rvalue_zero(self.type_i32())
 +    }
 +
 +    fn atomic_store(&mut self, value: RValue<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) {
 +        // TODO(antoyo): handle alignment.
 +        let atomic_store = self.context.get_builtin_function(&format!("__atomic_store_{}", size.bytes()));
 +        let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
 +        let volatile_const_void_ptr_type = self.context.new_type::<()>()
 +            .make_volatile()
 +            .make_pointer();
 +        let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
 +
 +        // FIXME(antoyo): fix libgccjit to allow comparing an integer type with an aligned integer type because
 +        // the following cast is required to avoid this error:
 +        // gcc_jit_context_new_call: mismatching types for argument 2 of function "__atomic_store_4": assignment to param arg1 (type: int) from loadedValue3577 (type: unsigned int  __attribute__((aligned(4))))
 +        let int_type = atomic_store.get_param(1).to_rvalue().get_type();
 +        let value = self.context.new_cast(None, value, int_type);
 +        self.llbb()
 +            .add_eval(None, self.context.new_call(None, atomic_store, &[ptr, value, ordering]));
 +    }
 +
 +    fn gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
 +        let mut result = ptr;
 +        for index in indices {
 +            result = self.context.new_array_access(None, result, *index).get_address(None).to_rvalue();
 +        }
 +        result
 +    }
 +
 +    fn inbounds_gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
 +        // FIXME(antoyo): would be safer if doing the same thing (loop) as gep.
 +        // TODO(antoyo): specify inbounds somehow.
 +        match indices.len() {
 +            1 => {
 +                self.context.new_array_access(None, ptr, indices[0]).get_address(None)
 +            },
 +            2 => {
 +                let array = ptr.dereference(None); // TODO(antoyo): assert that first index is 0?
 +                self.context.new_array_access(None, array, indices[1]).get_address(None)
 +            },
 +            _ => unimplemented!(),
 +        }
 +    }
 +
 +    fn struct_gep(&mut self, value_type: Type<'gcc>, ptr: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
 +        // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
 +        assert_eq!(idx as usize as u64, idx);
 +        let value = ptr.dereference(None).to_rvalue();
 +
 +        if value_type.dyncast_array().is_some() {
 +            let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
 +            let element = self.context.new_array_access(None, value, index);
 +            element.get_address(None)
 +        }
 +        else if let Some(vector_type) = value_type.dyncast_vector() {
 +            let array_type = vector_type.get_element_type().make_pointer();
 +            let array = self.bitcast(ptr, array_type);
 +            let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
 +            let element = self.context.new_array_access(None, array, index);
 +            element.get_address(None)
 +        }
 +        else if let Some(struct_type) = value_type.is_struct() {
 +            ptr.dereference_field(None, struct_type.get_field(idx as i32)).get_address(None)
 +        }
 +        else {
 +            panic!("Unexpected type {:?}", value_type);
 +        }
 +    }
 +
 +    /* Casts */
 +    fn trunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): check that it indeed truncate the value.
-         self.context.new_cast(None, value, dest_ty)
++        self.gcc_int_cast(value, dest_ty)
 +    }
 +
 +    fn sext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): check that it indeed sign extend the value.
 +        if dest_ty.dyncast_vector().is_some() {
 +            // TODO(antoyo): nothing to do as it is only for LLVM?
 +            return value;
 +        }
 +        self.context.new_cast(None, value, dest_ty)
 +    }
 +
 +    fn fptoui(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         self.context.new_cast(None, value, dest_ty)
++        self.gcc_float_to_uint_cast(value, dest_ty)
 +    }
 +
 +    fn fptosi(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         self.context.new_cast(None, value, dest_ty)
++        self.gcc_float_to_int_cast(value, dest_ty)
 +    }
 +
 +    fn uitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         self.context.new_cast(None, value, dest_ty)
++        self.gcc_uint_to_float_cast(value, dest_ty)
 +    }
 +
 +    fn sitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         self.cx.ptrtoint(self.block.expect("block"), value, dest_ty)
++        self.gcc_int_to_float_cast(value, dest_ty)
 +    }
 +
 +    fn fptrunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): make sure it truncates.
 +        self.context.new_cast(None, value, dest_ty)
 +    }
 +
 +    fn fpext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
 +        self.context.new_cast(None, value, dest_ty)
 +    }
 +
 +    fn ptrtoint(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         self.cx.inttoptr(self.block.expect("block"), value, dest_ty)
++        let usize_value = self.cx.const_bitcast(value, self.cx.type_isize());
++        self.intcast(usize_value, dest_ty, false)
 +    }
 +
 +    fn inttoptr(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         self.cx.context.new_cast(None, value, dest_typ)
++        let usize_value = self.intcast(value, self.cx.type_isize(), false);
++        self.cx.const_bitcast(usize_value, dest_ty)
 +    }
 +
 +    fn bitcast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
 +        self.cx.const_bitcast(value, dest_ty)
 +    }
 +
 +    fn intcast(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>, _is_signed: bool) -> RValue<'gcc> {
 +        // NOTE: is_signed is for value, not dest_typ.
-     fn icmp(&mut self, op: IntPredicate, mut lhs: RValue<'gcc>, mut rhs: RValue<'gcc>) -> RValue<'gcc> {
-         let left_type = lhs.get_type();
-         let right_type = rhs.get_type();
-         if left_type != right_type {
-             // NOTE: because libgccjit cannot compare function pointers.
-             if left_type.dyncast_function_ptr_type().is_some() && right_type.dyncast_function_ptr_type().is_some() {
-                 lhs = self.context.new_cast(None, lhs, self.usize_type.make_pointer());
-                 rhs = self.context.new_cast(None, rhs, self.usize_type.make_pointer());
-             }
-             // NOTE: hack because we try to cast a vector type to the same vector type.
-             else if format!("{:?}", left_type) != format!("{:?}", right_type) {
-                 rhs = self.context.new_cast(None, rhs, left_type);
-             }
-         }
-         self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
++        self.gcc_int_cast(value, dest_typ)
 +    }
 +
 +    fn pointercast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
 +        let val_type = value.get_type();
 +        match (type_is_pointer(val_type), type_is_pointer(dest_ty)) {
 +            (false, true) => {
 +                // NOTE: Projecting a field of a pointer type will attempt a cast from a signed char to
 +                // a pointer, which is not supported by gccjit.
 +                return self.cx.context.new_cast(None, self.inttoptr(value, val_type.make_pointer()), dest_ty);
 +            },
 +            (false, false) => {
 +                // When they are not pointers, we want a transmute (or reinterpret_cast).
 +                self.bitcast(value, dest_ty)
 +            },
 +            (true, true) => self.cx.context.new_cast(None, value, dest_ty),
 +            (true, false) => unimplemented!(),
 +        }
 +    }
 +
 +    /* Comparisons */
-     fn memcpy(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
-         if flags.contains(MemFlags::NONTEMPORAL) {
-             // HACK(nox): This is inefficient but there is no nontemporal memcpy.
-             let val = self.load(src.get_type(), src, src_align);
-             let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
-             self.store_with_flags(val, ptr, dst_align, flags);
-             return;
-         }
++    fn icmp(&mut self, op: IntPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
++        self.gcc_icmp(op, lhs, rhs)
 +    }
 +
 +    fn fcmp(&mut self, op: RealPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
 +        self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
 +    }
 +
 +    /* Miscellaneous instructions */
-         let block = self.block.expect("block");
++    fn memcpy(&mut self, dst: RValue<'gcc>, _dst_align: Align, src: RValue<'gcc>, _src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
++        assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
 +        let size = self.intcast(size, self.type_size_t(), false);
 +        let _is_volatile = flags.contains(MemFlags::VOLATILE);
 +        let dst = self.pointercast(dst, self.type_i8p());
 +        let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
 +        let memcpy = self.context.get_builtin_function("memcpy");
-         block.add_eval(None, self.context.new_call(None, memcpy, &[dst, src, size]));
 +        // TODO(antoyo): handle aligns and is_volatile.
-         let block = self.block.expect("block");
++        self.block.add_eval(None, self.context.new_call(None, memcpy, &[dst, src, size]));
 +    }
 +
 +    fn memmove(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
 +        if flags.contains(MemFlags::NONTEMPORAL) {
 +            // HACK(nox): This is inefficient but there is no nontemporal memmove.
 +            let val = self.load(src.get_type(), src, src_align);
 +            let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
 +            self.store_with_flags(val, ptr, dst_align, flags);
 +            return;
 +        }
 +        let size = self.intcast(size, self.type_size_t(), false);
 +        let _is_volatile = flags.contains(MemFlags::VOLATILE);
 +        let dst = self.pointercast(dst, self.type_i8p());
 +        let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
 +
 +        let memmove = self.context.get_builtin_function("memmove");
-         block.add_eval(None, self.context.new_call(None, memmove, &[dst, src, size]));
 +        // TODO(antoyo): handle is_volatile.
-         let block = self.block.expect("block");
++        self.block.add_eval(None, self.context.new_call(None, memmove, &[dst, src, size]));
 +    }
 +
 +    fn memset(&mut self, ptr: RValue<'gcc>, fill_byte: RValue<'gcc>, size: RValue<'gcc>, _align: Align, flags: MemFlags) {
 +        let _is_volatile = flags.contains(MemFlags::VOLATILE);
 +        let ptr = self.pointercast(ptr, self.type_i8p());
 +        let memset = self.context.get_builtin_function("memset");
-         block.add_eval(None, self.context.new_call(None, memset, &[ptr, fill_byte, size]));
 +        // TODO(antoyo): handle align and is_volatile.
 +        let fill_byte = self.context.new_cast(None, fill_byte, self.i32_type);
 +        let size = self.intcast(size, self.type_size_t(), false);
-         if then_val.get_type() != else_val.get_type() {
++        self.block.add_eval(None, self.context.new_call(None, memset, &[ptr, fill_byte, size]));
 +    }
 +
 +    fn select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, mut else_val: RValue<'gcc>) -> RValue<'gcc> {
 +        let func = self.current_func();
 +        let variable = func.new_local(None, then_val.get_type(), "selectVar");
 +        let then_block = func.new_block("then");
 +        let else_block = func.new_block("else");
 +        let after_block = func.new_block("after");
 +        self.llbb().end_with_conditional(None, cond, then_block, else_block);
 +
 +        then_block.add_assignment(None, variable, then_val);
 +        then_block.end_with_jump(None, after_block);
 +
-         // NOTE: since jumps were added in a place rustc does not expect, the current blocks in the
++        if !then_val.get_type().is_compatible_with(else_val.get_type()) {
 +            else_val = self.context.new_cast(None, else_val, then_val.get_type());
 +        }
 +        else_block.add_assignment(None, variable, else_val);
 +        else_block.end_with_jump(None, after_block);
 +
-         self.block = Some(after_block);
-         *self.cx.current_block.borrow_mut() = Some(after_block);
++        // NOTE: since jumps were added in a place rustc does not expect, the current block in the
 +        // state need to be updated.
-         let field1 = self.context.new_field(None, self.u8_type, "landing_pad_field_1");
++        self.switch_to_block(after_block);
 +
 +        variable.to_rvalue()
 +    }
 +
 +    #[allow(dead_code)]
 +    fn va_arg(&mut self, _list: RValue<'gcc>, _ty: Type<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn extract_element(&mut self, _vec: RValue<'gcc>, _idx: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn vector_splat(&mut self, _num_elts: usize, _elt: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn extract_value(&mut self, aggregate_value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
 +        // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
 +        assert_eq!(idx as usize as u64, idx);
 +        let value_type = aggregate_value.get_type();
 +
 +        if value_type.dyncast_array().is_some() {
 +            let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
 +            let element = self.context.new_array_access(None, aggregate_value, index);
 +            element.get_address(None)
 +        }
 +        else if value_type.dyncast_vector().is_some() {
 +            panic!();
 +        }
 +        else if let Some(pointer_type) = value_type.get_pointee() {
 +            if let Some(struct_type) = pointer_type.is_struct() {
 +                // NOTE: hack to workaround a limitation of the rustc API: see comment on
 +                // CodegenCx.structs_as_pointer
 +                aggregate_value.dereference_field(None, struct_type.get_field(idx as i32)).to_rvalue()
 +            }
 +            else {
 +                panic!("Unexpected type {:?}", value_type);
 +            }
 +        }
 +        else if let Some(struct_type) = value_type.is_struct() {
 +            aggregate_value.access_field(None, struct_type.get_field(idx as i32)).to_rvalue()
 +        }
 +        else {
 +            panic!("Unexpected type {:?}", value_type);
 +        }
 +    }
 +
 +    fn insert_value(&mut self, aggregate_value: RValue<'gcc>, value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
 +        // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
 +        assert_eq!(idx as usize as u64, idx);
 +        let value_type = aggregate_value.get_type();
 +
 +        let lvalue =
 +            if value_type.dyncast_array().is_some() {
 +                let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
 +                self.context.new_array_access(None, aggregate_value, index)
 +            }
 +            else if value_type.dyncast_vector().is_some() {
 +                panic!();
 +            }
 +            else if let Some(pointer_type) = value_type.get_pointee() {
 +                if let Some(struct_type) = pointer_type.is_struct() {
 +                    // NOTE: hack to workaround a limitation of the rustc API: see comment on
 +                    // CodegenCx.structs_as_pointer
 +                    aggregate_value.dereference_field(None, struct_type.get_field(idx as i32))
 +                }
 +                else {
 +                    panic!("Unexpected type {:?}", value_type);
 +                }
 +            }
 +            else {
 +                panic!("Unexpected type {:?}", value_type);
 +            };
 +
 +        let lvalue_type = lvalue.to_rvalue().get_type();
 +        let value =
 +            // NOTE: sometimes, rustc will create a value with the wrong type.
 +            if lvalue_type != value.get_type() {
 +                self.context.new_cast(None, value, lvalue_type)
 +            }
 +            else {
 +                value
 +            };
 +
 +        self.llbb().add_assignment(None, lvalue, value);
 +
 +        aggregate_value
 +    }
 +
 +    fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>) -> RValue<'gcc> {
-         unimplemented!();
++        let field1 = self.context.new_field(None, self.u8_type.make_pointer(), "landing_pad_field_1");
 +        let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
 +        let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
 +        self.current_func().new_local(None, struct_type.as_type(), "landing_pad")
 +            .to_rvalue()
 +        // TODO(antoyo): Properly implement unwinding.
 +        // the above is just to make the compilation work as it seems
 +        // rustc_codegen_ssa now calls the unwinding builder methods even on panic=abort.
 +    }
 +
 +    fn resume(&mut self, _exn: RValue<'gcc>) {
-         let size = self.cx.int_width(src.get_type()) / 8;
++        // TODO(bjorn3): Properly implement unwinding.
++        self.unreachable();
 +    }
 +
 +    fn cleanup_pad(&mut self, _parent: Option<RValue<'gcc>>, _args: &[RValue<'gcc>]) -> Funclet {
 +        unimplemented!();
 +    }
 +
 +    fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) {
 +        unimplemented!();
 +    }
 +
 +    fn catch_pad(&mut self, _parent: RValue<'gcc>, _args: &[RValue<'gcc>]) -> Funclet {
 +        unimplemented!();
 +    }
 +
 +    fn catch_switch(
 +        &mut self,
 +        _parent: Option<RValue<'gcc>>,
 +        _unwind: Option<Block<'gcc>>,
 +        _handlers: &[Block<'gcc>],
 +    ) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    // Atomic Operations
 +    fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
 +        let expected = self.current_func().new_local(None, cmp.get_type(), "expected");
 +        self.llbb().add_assignment(None, expected, cmp);
 +        let success = self.compare_exchange(dst, expected, src, order, failure_order, weak);
 +
 +        let pair_type = self.cx.type_struct(&[src.get_type(), self.bool_type], false);
 +        let result = self.current_func().new_local(None, pair_type, "atomic_cmpxchg_result");
 +        let align = Align::from_bits(64).expect("align"); // TODO(antoyo): use good align.
 +
 +        let value_type = result.to_rvalue().get_type();
 +        if let Some(struct_type) = value_type.is_struct() {
 +            self.store(success, result.access_field(None, struct_type.get_field(1)).get_address(None), align);
 +            // NOTE: since success contains the call to the intrinsic, it must be stored before
 +            // expected so that we store expected after the call.
 +            self.store(expected.to_rvalue(), result.access_field(None, struct_type.get_field(0)).get_address(None), align);
 +        }
 +        // TODO(antoyo): handle when value is not a struct.
 +
 +        result.to_rvalue()
 +    }
 +
 +    fn atomic_rmw(&mut self, op: AtomicRmwBinOp, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
-         self.context.new_cast(None, value, dest_typ)
++        let size = src.get_type().get_size();
 +        let name =
 +            match op {
 +                AtomicRmwBinOp::AtomicXchg => format!("__atomic_exchange_{}", size),
 +                AtomicRmwBinOp::AtomicAdd => format!("__atomic_fetch_add_{}", size),
 +                AtomicRmwBinOp::AtomicSub => format!("__atomic_fetch_sub_{}", size),
 +                AtomicRmwBinOp::AtomicAnd => format!("__atomic_fetch_and_{}", size),
 +                AtomicRmwBinOp::AtomicNand => format!("__atomic_fetch_nand_{}", size),
 +                AtomicRmwBinOp::AtomicOr => format!("__atomic_fetch_or_{}", size),
 +                AtomicRmwBinOp::AtomicXor => format!("__atomic_fetch_xor_{}", size),
 +                AtomicRmwBinOp::AtomicMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
 +                AtomicRmwBinOp::AtomicMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
 +                AtomicRmwBinOp::AtomicUMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
 +                AtomicRmwBinOp::AtomicUMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
 +            };
 +
 +
 +        let atomic_function = self.context.get_builtin_function(name);
 +        let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
 +
 +        let void_ptr_type = self.context.new_type::<*mut ()>();
 +        let volatile_void_ptr_type = void_ptr_type.make_volatile();
 +        let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
 +        // FIXME(antoyo): not sure why, but we have the wrong type here.
 +        let new_src_type = atomic_function.get_param(1).to_rvalue().get_type();
 +        let src = self.context.new_cast(None, src, new_src_type);
 +        let res = self.context.new_call(None, atomic_function, &[dst, src, order]);
 +        self.context.new_cast(None, res, src.get_type())
 +    }
 +
 +    fn atomic_fence(&mut self, order: AtomicOrdering, scope: SynchronizationScope) {
 +        let name =
 +            match scope {
 +                SynchronizationScope::SingleThread => "__atomic_signal_fence",
 +                SynchronizationScope::CrossThread => "__atomic_thread_fence",
 +            };
 +        let thread_fence = self.context.get_builtin_function(name);
 +        let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
 +        self.llbb().add_eval(None, self.context.new_call(None, thread_fence, &[order]));
 +    }
 +
 +    fn set_invariant_load(&mut self, load: RValue<'gcc>) {
 +        // NOTE: Hack to consider vtable function pointer as non-global-variable function pointer.
 +        self.normal_function_addresses.borrow_mut().insert(load);
 +        // TODO(antoyo)
 +    }
 +
 +    fn lifetime_start(&mut self, _ptr: RValue<'gcc>, _size: Size) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn lifetime_end(&mut self, _ptr: RValue<'gcc>, _size: Size) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn call(&mut self, _typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
 +        // FIXME(antoyo): remove when having a proper API.
 +        let gcc_func = unsafe { std::mem::transmute(func) };
 +        if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
 +            self.function_call(func, args, funclet)
 +        }
 +        else {
 +            // If it's a not function that was defined, it's a function pointer.
 +            self.function_ptr_call(func, args, funclet)
 +        }
 +    }
 +
 +    fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
 +        // FIXME(antoyo): this does not zero-extend.
 +        if value.get_type().is_bool() && dest_typ.is_i8(&self.cx) {
 +            // FIXME(antoyo): hack because base::from_immediate converts i1 to i8.
 +            // Fix the code in codegen_ssa::base::from_immediate.
 +            return value;
 +        }
-         unimplemented!();
++        self.gcc_int_cast(value, dest_typ)
 +    }
 +
 +    fn cx(&self) -> &CodegenCx<'gcc, 'tcx> {
 +        self.cx
 +    }
 +
 +    fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
- trait ToGccComp {
++        // FIMXE(bjorn3): implement
 +    }
 +
 +    fn set_span(&mut self, _span: Span) {}
 +
 +    fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
 +        if self.cx().val_ty(val) == self.cx().type_i1() {
 +            self.zext(val, self.cx().type_i8())
 +        }
 +        else {
 +            val
 +        }
 +    }
 +
 +    fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
 +        if scalar.is_bool() {
 +            return self.trunc(val, self.cx().type_i1());
 +        }
 +        val
 +    }
 +
 +    fn fptoui_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
 +        None
 +    }
 +
 +    fn fptosi_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
 +        None
 +    }
 +
 +    fn instrprof_increment(&mut self, _fn_name: RValue<'gcc>, _hash: RValue<'gcc>, _num_counters: RValue<'gcc>, _index: RValue<'gcc>) {
 +        unimplemented!();
 +    }
 +}
 +
 +impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
 +    pub fn shuffle_vector(&mut self, v1: RValue<'gcc>, v2: RValue<'gcc>, mask: RValue<'gcc>) -> RValue<'gcc> {
 +        let return_type = v1.get_type();
 +        let params = [
 +            self.context.new_parameter(None, return_type, "v1"),
 +            self.context.new_parameter(None, return_type, "v2"),
 +            self.context.new_parameter(None, mask.get_type(), "mask"),
 +        ];
 +        let shuffle = self.context.new_function(None, FunctionType::Extern, return_type, &params, "_mm_shuffle_epi8", false);
 +        self.context.new_call(None, shuffle, &[v1, v2, mask])
 +    }
 +}
 +
 +impl<'a, 'gcc, 'tcx> StaticBuilderMethods for Builder<'a, 'gcc, 'tcx> {
 +    fn get_static(&mut self, def_id: DefId) -> RValue<'gcc> {
 +        // Forward to the `get_static` method of `CodegenCx`
 +        self.cx().get_static(def_id).get_address(None)
 +    }
 +}
 +
 +impl<'tcx> HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
 +    fn param_env(&self) -> ParamEnv<'tcx> {
 +        self.cx.param_env()
 +    }
 +}
 +
 +impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
 +    fn target_spec(&self) -> &Target {
 +        &self.cx.target_spec()
 +    }
 +}
 +
++pub trait ToGccComp {
 +    fn to_gcc_comparison(&self) -> ComparisonOp;
 +}
 +
 +impl ToGccComp for IntPredicate {
 +    fn to_gcc_comparison(&self) -> ComparisonOp {
 +        match *self {
 +            IntPredicate::IntEQ => ComparisonOp::Equals,
 +            IntPredicate::IntNE => ComparisonOp::NotEquals,
 +            IntPredicate::IntUGT => ComparisonOp::GreaterThan,
 +            IntPredicate::IntUGE => ComparisonOp::GreaterThanEquals,
 +            IntPredicate::IntULT => ComparisonOp::LessThan,
 +            IntPredicate::IntULE => ComparisonOp::LessThanEquals,
 +            IntPredicate::IntSGT => ComparisonOp::GreaterThan,
 +            IntPredicate::IntSGE => ComparisonOp::GreaterThanEquals,
 +            IntPredicate::IntSLT => ComparisonOp::LessThan,
 +            IntPredicate::IntSLE => ComparisonOp::LessThanEquals,
 +        }
 +    }
 +}
 +
 +impl ToGccComp for RealPredicate {
 +    fn to_gcc_comparison(&self) -> ComparisonOp {
 +        // TODO(antoyo): check that ordered vs non-ordered is respected.
 +        match *self {
 +            RealPredicate::RealPredicateFalse => unreachable!(),
 +            RealPredicate::RealOEQ => ComparisonOp::Equals,
 +            RealPredicate::RealOGT => ComparisonOp::GreaterThan,
 +            RealPredicate::RealOGE => ComparisonOp::GreaterThanEquals,
 +            RealPredicate::RealOLT => ComparisonOp::LessThan,
 +            RealPredicate::RealOLE => ComparisonOp::LessThanEquals,
 +            RealPredicate::RealONE => ComparisonOp::NotEquals,
 +            RealPredicate::RealORD => unreachable!(),
 +            RealPredicate::RealUNO => unreachable!(),
 +            RealPredicate::RealUEQ => ComparisonOp::Equals,
 +            RealPredicate::RealUGT => ComparisonOp::GreaterThan,
 +            RealPredicate::RealUGE => ComparisonOp::GreaterThan,
 +            RealPredicate::RealULT => ComparisonOp::LessThan,
 +            RealPredicate::RealULE => ComparisonOp::LessThan,
 +            RealPredicate::RealUNE => ComparisonOp::NotEquals,
 +            RealPredicate::RealPredicateTrue => unreachable!(),
 +        }
 +    }
 +}
 +
 +#[repr(C)]
 +#[allow(non_camel_case_types)]
 +enum MemOrdering {
 +    __ATOMIC_RELAXED,
 +    __ATOMIC_CONSUME,
 +    __ATOMIC_ACQUIRE,
 +    __ATOMIC_RELEASE,
 +    __ATOMIC_ACQ_REL,
 +    __ATOMIC_SEQ_CST,
 +}
 +
 +trait ToGccOrdering {
 +    fn to_gcc(self) -> i32;
 +}
 +
 +impl ToGccOrdering for AtomicOrdering {
 +    fn to_gcc(self) -> i32 {
 +        use MemOrdering::*;
 +
 +        let ordering =
 +            match self {
 +                AtomicOrdering::NotAtomic => __ATOMIC_RELAXED, // TODO(antoyo): check if that's the same.
 +                AtomicOrdering::Unordered => __ATOMIC_RELAXED,
 +                AtomicOrdering::Monotonic => __ATOMIC_RELAXED, // TODO(antoyo): check if that's the same.
 +                AtomicOrdering::Acquire => __ATOMIC_ACQUIRE,
 +                AtomicOrdering::Release => __ATOMIC_RELEASE,
 +                AtomicOrdering::AcquireRelease => __ATOMIC_ACQ_REL,
 +                AtomicOrdering::SequentiallyConsistent => __ATOMIC_SEQ_CST,
 +            };
 +        ordering as i32
 +    }
 +}
index d1ff15367c3c183025a8fb92fe9d50f07f083a87,0000000000000000000000000000000000000000..61709dd92de7961e17524100ae7bdbfe02ac1f15
mode 100644,000000..100644
--- /dev/null
@@@ -1,435 -1,0 +1,395 @@@
- use std::convert::TryFrom;
 +use gccjit::LValue;
- use gccjit::{Block, CType, RValue, Type, ToRValue};
++use gccjit::{RValue, Type, ToRValue};
 +use rustc_codegen_ssa::mir::place::PlaceRef;
 +use rustc_codegen_ssa::traits::{
 +    BaseTypeMethods,
 +    ConstMethods,
 +    DerivedTypeMethods,
 +    MiscMethods,
 +    StaticMethods,
 +};
 +use rustc_middle::mir::Mutability;
 +use rustc_middle::ty::ScalarInt;
 +use rustc_middle::ty::layout::{TyAndLayout, LayoutOf};
 +use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
 +use rustc_span::Symbol;
 +use rustc_target::abi::{self, HasDataLayout, Pointer, Size};
 +
 +use crate::consts::const_alloc_to_gcc;
 +use crate::context::CodegenCx;
 +use crate::type_of::LayoutGccExt;
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
 +    pub fn const_bytes(&self, bytes: &[u8]) -> RValue<'gcc> {
 +        bytes_in_context(self, bytes)
 +    }
 +
 +    fn global_string(&self, string: &str) -> LValue<'gcc> {
 +        // TODO(antoyo): handle non-null-terminated strings.
 +        let string = self.context.new_string_literal(&*string);
 +        let sym = self.generate_local_symbol_name("str");
 +        let global = self.declare_private_global(&sym, self.val_ty(string));
 +        global.global_set_initializer_rvalue(string);
 +        global
 +        // TODO(antoyo): set linkage.
 +    }
-     pub fn inttoptr(&self, block: Block<'gcc>, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         let func = block.get_function();
-         let local = func.new_local(None, value.get_type(), "intLocal");
-         block.add_assignment(None, local, value);
-         let value_address = local.get_address(None);
-         let ptr = self.context.new_cast(None, value_address, dest_ty.make_pointer());
-         ptr.dereference(None).to_rvalue()
-     }
-     pub fn ptrtoint(&self, block: Block<'gcc>, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
-         // TODO(antoyo): when libgccjit allow casting from pointer to int, remove this.
-         let func = block.get_function();
-         let local = func.new_local(None, value.get_type(), "ptrLocal");
-         block.add_assignment(None, local, value);
-         let ptr_address = local.get_address(None);
-         let ptr = self.context.new_cast(None, ptr_address, dest_ty.make_pointer());
-         ptr.dereference(None).to_rvalue()
-     }
 +}
 +
 +pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> RValue<'gcc> {
 +    let context = &cx.context;
 +    let byte_type = context.new_type::<u8>();
 +    let typ = context.new_array_type(None, byte_type, bytes.len() as i32);
 +    let elements: Vec<_> =
 +        bytes.iter()
 +        .map(|&byte| context.new_rvalue_from_int(byte_type, byte as i32))
 +        .collect();
 +    context.new_array_constructor(None, typ, &elements)
 +}
 +
 +pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool {
 +    typ.get_pointee().is_some()
 +}
 +
 +impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    fn const_null(&self, typ: Type<'gcc>) -> RValue<'gcc> {
 +        if type_is_pointer(typ) {
 +            self.context.new_null(typ)
 +        }
 +        else {
 +            self.const_int(typ, 0)
 +        }
 +    }
 +
 +    fn const_undef(&self, typ: Type<'gcc>) -> RValue<'gcc> {
 +        let local = self.current_func.borrow().expect("func")
 +            .new_local(None, typ, "undefined");
 +        if typ.is_struct().is_some() {
 +            // NOTE: hack to workaround a limitation of the rustc API: see comment on
 +            // CodegenCx.structs_as_pointer
 +            let pointer = local.get_address(None);
 +            self.structs_as_pointer.borrow_mut().insert(pointer);
 +            pointer
 +        }
 +        else {
 +            local.to_rvalue()
 +        }
 +    }
 +
 +    fn const_int(&self, typ: Type<'gcc>, int: i64) -> RValue<'gcc> {
-         self.context.new_rvalue_from_long(typ, i64::try_from(int).expect("i64::try_from"))
++        self.gcc_int(typ, int)
 +    }
 +
 +    fn const_uint(&self, typ: Type<'gcc>, int: u64) -> RValue<'gcc> {
-         self.context.new_rvalue_from_long(typ, u64::try_from(int).expect("u64::try_from") as i64)
++        self.gcc_uint(typ, int)
 +    }
 +
 +    fn const_uint_big(&self, typ: Type<'gcc>, num: u128) -> RValue<'gcc> {
-         if num >> 64 != 0 {
-             // FIXME(antoyo): use a new function new_rvalue_from_unsigned_long()?
-             let low = self.context.new_rvalue_from_long(self.u64_type, num as u64 as i64);
-             let high = self.context.new_rvalue_from_long(typ, (num >> 64) as u64 as i64);
-             let sixty_four = self.context.new_rvalue_from_long(typ, 64);
-             (high << sixty_four) | self.context.new_cast(None, low, typ)
-         }
-         else if typ.is_i128(self) {
-             let num = self.context.new_rvalue_from_long(self.u64_type, num as u64 as i64);
-             self.context.new_cast(None, num, typ)
-         }
-         else {
-             self.context.new_rvalue_from_long(typ, num as u64 as i64)
-         }
++        self.gcc_uint_big(typ, num)
 +    }
 +
 +    fn const_bool(&self, val: bool) -> RValue<'gcc> {
 +        self.const_uint(self.type_i1(), val as u64)
 +    }
 +
 +    fn const_i32(&self, i: i32) -> RValue<'gcc> {
 +        self.const_int(self.type_i32(), i as i64)
 +    }
 +
 +    fn const_u32(&self, i: u32) -> RValue<'gcc> {
 +        self.const_uint(self.type_u32(), i as u64)
 +    }
 +
 +    fn const_u64(&self, i: u64) -> RValue<'gcc> {
 +        self.const_uint(self.type_u64(), i)
 +    }
 +
 +    fn const_usize(&self, i: u64) -> RValue<'gcc> {
 +        let bit_size = self.data_layout().pointer_size.bits();
 +        if bit_size < 64 {
 +            // make sure it doesn't overflow
 +            assert!(i < (1 << bit_size));
 +        }
 +
 +        self.const_uint(self.usize_type, i)
 +    }
 +
 +    fn const_u8(&self, _i: u8) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn const_real(&self, _t: Type<'gcc>, _val: f64) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn const_str(&self, s: Symbol) -> (RValue<'gcc>, RValue<'gcc>) {
 +        let s_str = s.as_str();
 +        let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
 +            self.global_string(s_str)
 +        });
 +        let len = s_str.len();
 +        let cs = self.const_ptrcast(str_global.get_address(None),
 +            self.type_ptr_to(self.layout_of(self.tcx.types.str_).gcc_type(self, true)),
 +        );
 +        (cs, self.const_usize(len as u64))
 +    }
 +
 +    fn const_struct(&self, values: &[RValue<'gcc>], packed: bool) -> RValue<'gcc> {
 +        let fields: Vec<_> = values.iter()
 +            .map(|value| value.get_type())
 +            .collect();
 +        // TODO(antoyo): cache the type? It's anonymous, so probably not.
 +        let typ = self.type_struct(&fields, packed);
 +        let struct_type = typ.is_struct().expect("struct type");
 +        self.context.new_struct_constructor(None, struct_type.as_type(), None, values)
 +    }
 +
 +    fn const_to_opt_uint(&self, _v: RValue<'gcc>) -> Option<u64> {
 +        // TODO(antoyo)
 +        None
 +    }
 +
 +    fn const_to_opt_u128(&self, _v: RValue<'gcc>, _sign_ext: bool) -> Option<u128> {
 +        // TODO(antoyo)
 +        None
 +    }
 +
 +    fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, ty: Type<'gcc>) -> RValue<'gcc> {
 +        let bitsize = if layout.is_bool() { 1 } else { layout.value.size(self).bits() };
 +        match cv {
 +            Scalar::Int(ScalarInt::ZST) => {
 +                assert_eq!(0, layout.value.size(self).bytes());
 +                self.const_undef(self.type_ix(0))
 +            }
 +            Scalar::Int(int) => {
 +                let data = int.assert_bits(layout.value.size(self));
 +
 +                // FIXME(antoyo): there's some issues with using the u128 code that follows, so hard-code
 +                // the paths for floating-point values.
 +                if ty == self.float_type {
 +                    return self.context.new_rvalue_from_double(ty, f32::from_bits(data as u32) as f64);
 +                }
 +                else if ty == self.double_type {
 +                    return self.context.new_rvalue_from_double(ty, f64::from_bits(data as u64));
 +                }
 +
 +                let value = self.const_uint_big(self.type_ix(bitsize), data);
-                 if layout.value == Pointer {
-                     self.inttoptr(self.current_block.borrow().expect("block"), value, ty)
-                 } else {
-                     self.const_bitcast(value, ty)
-                 }
++                // TODO(bjorn3): assert size is correct
++                self.const_bitcast(value, ty)
 +            }
 +            Scalar::Ptr(ptr, _size) => {
 +                let (alloc_id, offset) = ptr.into_parts();
 +                let base_addr =
 +                    match self.tcx.global_alloc(alloc_id) {
 +                        GlobalAlloc::Memory(alloc) => {
 +                            let init = const_alloc_to_gcc(self, alloc);
 +                            let alloc = alloc.inner();
 +                            let value =
 +                                match alloc.mutability {
 +                                    Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
 +                                    _ => self.static_addr_of(init, alloc.align, None),
 +                                };
 +                            if !self.sess().fewer_names() {
 +                                // TODO(antoyo): set value name.
 +                            }
 +                            value
 +                        },
 +                        GlobalAlloc::Function(fn_instance) => {
 +                            self.get_fn_addr(fn_instance)
 +                        },
 +                        GlobalAlloc::Static(def_id) => {
 +                            assert!(self.tcx.is_static(def_id));
 +                            self.get_static(def_id).get_address(None)
 +                        },
 +                    };
 +                let ptr_type = base_addr.get_type();
 +                let base_addr = self.const_bitcast(base_addr, self.usize_type);
 +                let offset = self.context.new_rvalue_from_long(self.usize_type, offset.bytes() as i64);
 +                let ptr = self.const_bitcast(base_addr + offset, ptr_type);
 +                if layout.value != Pointer {
 +                    self.const_bitcast(ptr.dereference(None).to_rvalue(), ty)
 +                }
 +                else {
 +                    self.const_bitcast(ptr, ty)
 +                }
 +            }
 +        }
 +    }
 +
 +    fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
 +        const_alloc_to_gcc(self, alloc)
 +    }
 +
 +    fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: ConstAllocation<'tcx>, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
 +        assert_eq!(alloc.inner().align, layout.align.abi);
 +        let ty = self.type_ptr_to(layout.gcc_type(self, true));
 +        let value =
 +            if layout.size == Size::ZERO {
 +                let value = self.const_usize(alloc.inner().align.bytes());
 +                self.context.new_cast(None, value, ty)
 +            }
 +            else {
 +                let init = const_alloc_to_gcc(self, alloc);
 +                let base_addr = self.static_addr_of(init, alloc.inner().align, None);
 +
 +                let array = self.const_bitcast(base_addr, self.type_i8p());
 +                let value = self.context.new_array_access(None, array, self.const_usize(offset.bytes())).get_address(None);
 +                self.const_bitcast(value, ty)
 +            };
 +        PlaceRef::new_sized(value, layout)
 +    }
 +
 +    fn const_ptrcast(&self, val: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> {
 +        self.context.new_cast(None, val, ty)
 +    }
 +}
 +
 +pub trait SignType<'gcc, 'tcx> {
 +    fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
 +    fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
 +}
 +
 +impl<'gcc, 'tcx> SignType<'gcc, 'tcx> for Type<'gcc> {
 +    fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.is_i8(cx) || self.is_i16(cx) || self.is_i32(cx) || self.is_i64(cx) || self.is_i128(cx)
 +    }
 +
 +    fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.is_u8(cx) || self.is_u16(cx) || self.is_u32(cx) || self.is_u64(cx) || self.is_u128(cx)
 +    }
 +
 +    fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
 +        if self.is_u8(cx) {
 +            cx.i8_type
 +        }
 +        else if self.is_u16(cx) {
 +            cx.i16_type
 +        }
 +        else if self.is_u32(cx) {
 +            cx.i32_type
 +        }
 +        else if self.is_u64(cx) {
 +            cx.i64_type
 +        }
 +        else if self.is_u128(cx) {
 +            cx.i128_type
 +        }
 +        else {
 +            self.clone()
 +        }
 +    }
 +
 +    fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
 +        if self.is_i8(cx) {
 +            cx.u8_type
 +        }
 +        else if self.is_i16(cx) {
 +            cx.u16_type
 +        }
 +        else if self.is_i32(cx) {
 +            cx.u32_type
 +        }
 +        else if self.is_i64(cx) {
 +            cx.u64_type
 +        }
 +        else if self.is_i128(cx) {
 +            cx.u128_type
 +        }
 +        else {
 +            self.clone()
 +        }
 +    }
 +}
 +
 +pub trait TypeReflection<'gcc, 'tcx>  {
 +    fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +
 +    fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +
 +    fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +    fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
 +}
 +
 +impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
 +    fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.u8_type
 +    }
 +
 +    fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.u16_type
 +    }
 +
 +    fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.uint_type
 +    }
 +
 +    fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.ulong_type
 +    }
 +
 +    fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.ulonglong_type
 +    }
 +
 +    fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.i8_type
 +    }
 +
 +    fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.u8_type
 +    }
 +
 +    fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.i16_type
 +    }
 +
 +    fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.u16_type
 +    }
 +
 +    fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.i32_type
 +    }
 +
 +    fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.u32_type
 +    }
 +
 +    fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.i64_type
 +    }
 +
 +    fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.u64_type
 +    }
 +
 +    fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
-         self.unqualified() == cx.context.new_c_type(CType::Int128t)
++        self.unqualified() == cx.i128_type.unqualified()
 +    }
 +
 +    fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
-         self.unqualified() == cx.context.new_c_type(CType::UInt128t)
++        self.unqualified() == cx.u128_type.unqualified()
 +    }
 +
 +    fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.context.new_type::<f32>()
 +    }
 +
 +    fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
 +        self.unqualified() == cx.context.new_type::<f64>()
 +    }
 +}
index d53b15159fa44d3d135a56e3fdcff90c576b7a53,0000000000000000000000000000000000000000..de52f3ea2255e7092863403dd028d568b9900b18
mode 100644,000000..100644
--- /dev/null
@@@ -1,395 -1,0 +1,404 @@@
- use gccjit::{LValue, RValue, ToRValue, Type};
++use gccjit::{GlobalKind, LValue, RValue, ToRValue, Type};
 +use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, DerivedTypeMethods, StaticMethods};
 +use rustc_hir as hir;
 +use rustc_hir::Node;
 +use rustc_middle::{bug, span_bug};
 +use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
 +use rustc_middle::mir::mono::MonoItem;
 +use rustc_middle::ty::{self, Instance, Ty};
 +use rustc_middle::ty::layout::LayoutOf;
 +use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
 +use rustc_span::Span;
 +use rustc_span::def_id::DefId;
 +use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRange};
 +
 +use crate::base;
 +use crate::context::CodegenCx;
 +use crate::type_of::LayoutGccExt;
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
 +    pub fn const_bitcast(&self, value: RValue<'gcc>, typ: Type<'gcc>) -> RValue<'gcc> {
 +        if value.get_type() == self.bool_type.make_pointer() {
 +            if let Some(pointee) = typ.get_pointee() {
 +                if pointee.dyncast_vector().is_some() {
 +                    panic!()
 +                }
 +            }
 +        }
 +        self.context.new_bitcast(None, value, typ)
 +    }
 +}
 +
 +impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
 +    fn static_addr_of(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
 +        // TODO(antoyo): implement a proper rvalue comparison in libgccjit instead of doing the
 +        // following:
 +        for (value, variable) in &*self.const_globals.borrow() {
 +            if format!("{:?}", value) == format!("{:?}", cv) {
-                 // TODO(antoyo): upgrade alignment.
++                if let Some(global_variable) = self.global_lvalues.borrow().get(variable) {
++                    let alignment = align.bits() as i32;
++                    if alignment > global_variable.get_alignment() {
++                        global_variable.set_alignment(alignment);
++                    }
++                }
 +                return *variable;
 +            }
 +        }
 +        let global_value = self.static_addr_of_mut(cv, align, kind);
 +        // TODO(antoyo): set global constant.
 +        self.const_globals.borrow_mut().insert(cv, global_value);
 +        global_value
 +    }
 +
 +    fn codegen_static(&self, def_id: DefId, is_mutable: bool) {
 +        let attrs = self.tcx.codegen_fn_attrs(def_id);
 +
 +        let value =
 +            match codegen_static_initializer(&self, def_id) {
 +                Ok((value, _)) => value,
 +                // Error has already been reported
 +                Err(_) => return,
 +            };
 +
 +        let global = self.get_static(def_id);
 +
 +        // boolean SSA values are i1, but they have to be stored in i8 slots,
 +        // otherwise some LLVM optimization passes don't work as expected
 +        let val_llty = self.val_ty(value);
 +        let value =
 +            if val_llty == self.type_i1() {
 +                unimplemented!();
 +            }
 +            else {
 +                value
 +            };
 +
 +        let instance = Instance::mono(self.tcx, def_id);
 +        let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
 +        let gcc_type = self.layout_of(ty).gcc_type(self, true);
 +
 +        // TODO(antoyo): set alignment.
 +
 +        let value =
 +            if value.get_type() != gcc_type {
 +                self.context.new_bitcast(None, value, gcc_type)
 +            }
 +            else {
 +                value
 +            };
 +        global.global_set_initializer_rvalue(value);
 +
 +        // As an optimization, all shared statics which do not have interior
 +        // mutability are placed into read-only memory.
 +        if !is_mutable {
 +            if self.type_is_freeze(ty) {
 +                // TODO(antoyo): set global constant.
 +            }
 +        }
 +
 +        if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
 +            // Do not allow LLVM to change the alignment of a TLS on macOS.
 +            //
 +            // By default a global's alignment can be freely increased.
 +            // This allows LLVM to generate more performant instructions
 +            // e.g., using load-aligned into a SIMD register.
 +            //
 +            // However, on macOS 10.10 or below, the dynamic linker does not
 +            // respect any alignment given on the TLS (radar 24221680).
 +            // This will violate the alignment assumption, and causing segfault at runtime.
 +            //
 +            // This bug is very easy to trigger. In `println!` and `panic!`,
 +            // the `LOCAL_STDOUT`/`LOCAL_STDERR` handles are stored in a TLS,
 +            // which the values would be `mem::replace`d on initialization.
 +            // The implementation of `mem::replace` will use SIMD
 +            // whenever the size is 32 bytes or higher. LLVM notices SIMD is used
 +            // and tries to align `LOCAL_STDOUT`/`LOCAL_STDERR` to a 32-byte boundary,
 +            // which macOS's dyld disregarded and causing crashes
 +            // (see issues #51794, #51758, #50867, #48866 and #44056).
 +            //
 +            // To workaround the bug, we trick LLVM into not increasing
 +            // the global's alignment by explicitly assigning a section to it
 +            // (equivalent to automatically generating a `#[link_section]` attribute).
 +            // See the comment in the `GlobalValue::canIncreaseAlignment()` function
 +            // of `lib/IR/Globals.cpp` for why this works.
 +            //
 +            // When the alignment is not increased, the optimized `mem::replace`
 +            // will use load-unaligned instructions instead, and thus avoiding the crash.
 +            //
 +            // We could remove this hack whenever we decide to drop macOS 10.10 support.
 +            if self.tcx.sess.target.options.is_like_osx {
 +                // The `inspect` method is okay here because we checked relocations, and
 +                // because we are doing this access to inspect the final interpreter state
 +                // (not as part of the interpreter execution).
 +                //
 +                // FIXME: This check requires that the (arbitrary) value of undefined bytes
 +                // happens to be zero. Instead, we should only check the value of defined bytes
 +                // and set all undefined bytes to zero if this allocation is headed for the
 +                // BSS.
 +                unimplemented!();
 +            }
 +        }
 +
 +        // Wasm statics with custom link sections get special treatment as they
 +        // go into custom sections of the wasm executable.
 +        if self.tcx.sess.opts.target_triple.triple().starts_with("wasm32") {
 +            if let Some(_section) = attrs.link_section {
 +                unimplemented!();
 +            }
 +        } else {
 +            // TODO(antoyo): set link section.
 +        }
 +
 +        if attrs.flags.contains(CodegenFnAttrFlags::USED) || attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) {
 +            self.add_used_global(global.to_rvalue());
 +        }
 +    }
 +
 +    /// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
 +    fn add_used_global(&self, _global: RValue<'gcc>) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn add_compiler_used_global(&self, _global: RValue<'gcc>) {
 +        // TODO(antoyo)
 +    }
 +}
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
 +    pub fn static_addr_of_mut(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
 +        let global =
 +            match kind {
 +                Some(kind) if !self.tcx.sess.fewer_names() => {
 +                    let name = self.generate_local_symbol_name(kind);
-                     // TODO(antoyo): check if it's okay that TLS is off here.
-                     // TODO(antoyo): check if it's okay that link_section is None here.
++                    // TODO(antoyo): check if it's okay that no link_section is set.
 +                    // TODO(antoyo): set alignment here as well.
-                     let global = self.define_global(&name[..], self.val_ty(cv), false, None);
-                     // TODO(antoyo): set linkage.
++                    let global = self.declare_private_global(&name[..], self.val_ty(cv));
 +                    global
 +                }
 +                _ => {
 +                    let typ = self.val_ty(cv).get_aligned(align.bytes());
 +                    let global = self.declare_unnamed_global(typ);
 +                    global
 +                },
 +            };
-         // FIXME(antoyo): I think the name coming from generate_local_symbol_name() above cannot be used
-         // globally.
 +        global.global_set_initializer_rvalue(cv);
 +        // TODO(antoyo): set unnamed address.
-         global.get_address(None)
++        let rvalue = global.get_address(None);
++        self.global_lvalues.borrow_mut().insert(rvalue, global);
++        rvalue
 +    }
 +
 +    pub fn get_static(&self, def_id: DefId) -> LValue<'gcc> {
 +        let instance = Instance::mono(self.tcx, def_id);
 +        let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
 +        if let Some(&global) = self.instances.borrow().get(&instance) {
 +            return global;
 +        }
 +
 +        let defined_in_current_codegen_unit =
 +            self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
 +        assert!(
 +            !defined_in_current_codegen_unit,
 +            "consts::get_static() should always hit the cache for \
 +                 statics defined in the same CGU, but did not for `{:?}`",
 +            def_id
 +        );
 +
 +        let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
 +        let sym = self.tcx.symbol_name(instance).name;
 +
 +        let global =
 +            if let Some(def_id) = def_id.as_local() {
 +                let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
 +                let llty = self.layout_of(ty).gcc_type(self, true);
 +                // FIXME: refactor this to work without accessing the HIR
 +                let global = match self.tcx.hir().get(id) {
 +                    Node::Item(&hir::Item { span, kind: hir::ItemKind::Static(..), .. }) => {
 +                        if let Some(global) = self.get_declared_value(&sym) {
 +                            if self.val_ty(global) != self.type_ptr_to(llty) {
 +                                span_bug!(span, "Conflicting types for static");
 +                            }
 +                        }
 +
 +                        let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
-                         let global = self.declare_global(&sym, llty, is_tls, fn_attrs.link_section);
++                        let global = self.declare_global(
++                            &sym,
++                            llty,
++                            GlobalKind::Exported,
++                            is_tls,
++                            fn_attrs.link_section,
++                        );
 +
 +                        if !self.tcx.is_reachable_non_generic(def_id) {
 +                            // TODO(antoyo): set visibility.
 +                        }
 +
 +                        global
 +                    }
 +
 +                    Node::ForeignItem(&hir::ForeignItem {
 +                        span,
 +                        kind: hir::ForeignItemKind::Static(..),
 +                        ..
 +                    }) => {
 +                        let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
 +                        check_and_apply_linkage(&self, &fn_attrs, ty, sym, span)
 +                    }
 +
 +                    item => bug!("get_static: expected static, found {:?}", item),
 +                };
 +
 +                global
 +            }
 +            else {
 +                // FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
 +                //debug!("get_static: sym={} item_attr={:?}", sym, self.tcx.item_attrs(def_id));
 +
 +                let attrs = self.tcx.codegen_fn_attrs(def_id);
 +                let span = self.tcx.def_span(def_id);
 +                let global = check_and_apply_linkage(&self, &attrs, ty, sym, span);
 +
 +                let needs_dll_storage_attr = false; // TODO(antoyo)
 +
 +                // If this assertion triggers, there's something wrong with commandline
 +                // argument validation.
 +                debug_assert!(
 +                    !(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
 +                        && self.tcx.sess.target.options.is_like_msvc
 +                        && self.tcx.sess.opts.cg.prefer_dynamic)
 +                );
 +
 +                if needs_dll_storage_attr {
 +                    // This item is external but not foreign, i.e., it originates from an external Rust
 +                    // crate. Since we don't know whether this crate will be linked dynamically or
 +                    // statically in the final application, we always mark such symbols as 'dllimport'.
 +                    // If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
 +                    // to make things work.
 +                    //
 +                    // However, in some scenarios we defer emission of statics to downstream
 +                    // crates, so there are cases where a static with an upstream DefId
 +                    // is actually present in the current crate. We can find out via the
 +                    // is_codegened_item query.
 +                    if !self.tcx.is_codegened_item(def_id) {
 +                        unimplemented!();
 +                    }
 +                }
 +                global
 +            };
 +
 +        // TODO(antoyo): set dll storage class.
 +
 +        self.instances.borrow_mut().insert(instance, global);
 +        global
 +    }
 +}
 +
 +pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAllocation<'tcx>) -> RValue<'gcc> {
 +    let alloc = alloc.inner();
 +    let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
 +    let dl = cx.data_layout();
 +    let pointer_size = dl.pointer_size.bytes() as usize;
 +
 +    let mut next_offset = 0;
 +    for &(offset, alloc_id) in alloc.relocations().iter() {
 +        let offset = offset.bytes();
 +        assert_eq!(offset as usize as u64, offset);
 +        let offset = offset as usize;
 +        if offset > next_offset {
 +            // This `inspect` is okay since we have checked that it is not within a relocation, it
 +            // is within the bounds of the allocation, and it doesn't affect interpreter execution
 +            // (we inspect the result after interpreter execution). Any undef byte is replaced with
 +            // some arbitrary byte value.
 +            //
 +            // FIXME: relay undef bytes to codegen as undef const bytes
 +            let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(next_offset..offset);
 +            llvals.push(cx.const_bytes(bytes));
 +        }
 +        let ptr_offset =
 +            read_target_uint( dl.endian,
 +                // This `inspect` is okay since it is within the bounds of the allocation, it doesn't
 +                // affect interpreter execution (we inspect the result after interpreter execution),
 +                // and we properly interpret the relocation as a relocation pointer offset.
 +                alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),
 +            )
 +            .expect("const_alloc_to_llvm: could not read relocation pointer")
 +            as u64;
 +        llvals.push(cx.scalar_to_backend(
 +            InterpScalar::from_pointer(
 +                interpret::Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
 +                &cx.tcx,
 +            ),
 +            abi::Scalar { value: Primitive::Pointer, valid_range: WrappingRange { start: 0, end: !0 } },
 +            cx.type_i8p(),
 +        ));
 +        next_offset = offset + pointer_size;
 +    }
 +    if alloc.len() >= next_offset {
 +        let range = next_offset..alloc.len();
 +        // This `inspect` is okay since we have check that it is after all relocations, it is
 +        // within the bounds of the allocation, and it doesn't affect interpreter execution (we
 +        // inspect the result after interpreter execution). Any undef byte is replaced with some
 +        // arbitrary byte value.
 +        //
 +        // FIXME: relay undef bytes to codegen as undef const bytes
 +        let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
 +        llvals.push(cx.const_bytes(bytes));
 +    }
 +
 +    cx.const_struct(&llvals, true)
 +}
 +
 +pub fn codegen_static_initializer<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, def_id: DefId) -> Result<(RValue<'gcc>, ConstAllocation<'tcx>), ErrorHandled> {
 +    let alloc = cx.tcx.eval_static_initializer(def_id)?;
 +    Ok((const_alloc_to_gcc(cx, alloc), alloc))
 +}
 +
 +fn check_and_apply_linkage<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, attrs: &CodegenFnAttrs, ty: Ty<'tcx>, sym: &str, span: Span) -> LValue<'gcc> {
 +    let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
 +    let llty = cx.layout_of(ty).gcc_type(cx, true);
 +    if let Some(linkage) = attrs.linkage {
 +        // If this is a static with a linkage specified, then we need to handle
 +        // it a little specially. The typesystem prevents things like &T and
 +        // extern "C" fn() from being non-null, so we can't just declare a
 +        // static and call it a day. Some linkages (like weak) will make it such
 +        // that the static actually has a null value.
 +        let llty2 =
 +            if let ty::RawPtr(ref mt) = ty.kind() {
 +                cx.layout_of(mt.ty).gcc_type(cx, true)
 +            }
 +            else {
 +                cx.sess().span_fatal(
 +                    span,
 +                    "must have type `*const T` or `*mut T` due to `#[linkage]` attribute",
 +                )
 +            };
 +        // Declare a symbol `foo` with the desired linkage.
 +        let global1 = cx.declare_global_with_linkage(&sym, llty2, base::global_linkage_to_gcc(linkage));
 +
 +        // Declare an internal global `extern_with_linkage_foo` which
 +        // is initialized with the address of `foo`.  If `foo` is
 +        // discarded during linking (for example, if `foo` has weak
 +        // linkage and there are no definitions), then
 +        // `extern_with_linkage_foo` will instead be initialized to
 +        // zero.
 +        let mut real_name = "_rust_extern_with_linkage_".to_string();
 +        real_name.push_str(&sym);
 +        let global2 = cx.define_global(&real_name, llty, is_tls, attrs.link_section);
 +        // TODO(antoyo): set linkage.
 +        global2.global_set_initializer_rvalue(global1.get_address(None));
 +        // TODO(antoyo): use global_set_initializer() when it will work.
 +        global2
 +    }
 +    else {
 +        // Generate an external declaration.
 +        // FIXME(nagisa): investigate whether it can be changed into define_global
 +
 +        // Thread-local statics in some other crate need to *always* be linked
 +        // against in a thread-local fashion, so we need to be sure to apply the
 +        // thread-local attribute locally if it was present remotely. If we
 +        // don't do this then linker errors can be generated where the linker
 +        // complains that one object files has a thread local version of the
 +        // symbol and another one doesn't.
-         cx.declare_global(&sym, llty, is_tls, attrs.link_section)
++        cx.declare_global(&sym, llty, GlobalKind::Imported, is_tls, attrs.link_section)
 +    }
 +}
index 6c1dce969f0f5eed9ec997d4f5af26629c0b33f7,0000000000000000000000000000000000000000..5e5b9e7e9b1b7e85e64dcec0aee15ba37fc55a00
mode 100644,000000..100644
--- /dev/null
@@@ -1,465 -1,0 +1,519 @@@
- use gccjit::{Block, CType, Context, Function, FunctionType, LValue, RValue, Struct, Type};
 +use std::cell::{Cell, RefCell};
 +
- use crate::declare::mangle_name;
++use gccjit::{Block, CType, Context, Function, FunctionPtrType, FunctionType, LValue, RValue, Struct, Type};
 +use rustc_codegen_ssa::base::wants_msvc_seh;
 +use rustc_codegen_ssa::traits::{
 +    BackendTypes,
 +    MiscMethods,
 +};
 +use rustc_data_structures::base_n;
 +use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 +use rustc_middle::span_bug;
 +use rustc_middle::mir::mono::CodegenUnit;
 +use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
 +use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, TyAndLayout, LayoutOfHelpers};
 +use rustc_session::Session;
 +use rustc_span::{Span, Symbol};
 +use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
 +use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
 +
 +use crate::callee::get_fn;
-     // TODO(antoyo): First set it to a dummy block to avoid using Option?
-     pub current_block: RefCell<Option<Block<'gcc>>>,
 +
 +#[derive(Clone)]
 +pub struct FuncSig<'gcc> {
 +    pub params: Vec<Type<'gcc>>,
 +    pub return_type: Type<'gcc>,
 +}
 +
 +pub struct CodegenCx<'gcc, 'tcx> {
 +    pub check_overflow: bool,
 +    pub codegen_unit: &'tcx CodegenUnit<'tcx>,
 +    pub context: &'gcc Context<'gcc>,
 +
-     pub global_gen_sym_counter: Cell<usize>,
++    // TODO(bjorn3): Can this field be removed?
 +    pub current_func: RefCell<Option<Function<'gcc>>>,
 +    pub normal_function_addresses: RefCell<FxHashSet<RValue<'gcc>>>,
 +
 +    pub functions: RefCell<FxHashMap<String, Function<'gcc>>>,
 +
 +    pub tls_model: gccjit::TlsModel,
 +
 +    pub bool_type: Type<'gcc>,
 +    pub i8_type: Type<'gcc>,
 +    pub i16_type: Type<'gcc>,
 +    pub i32_type: Type<'gcc>,
 +    pub i64_type: Type<'gcc>,
 +    pub i128_type: Type<'gcc>,
 +    pub isize_type: Type<'gcc>,
 +
 +    pub u8_type: Type<'gcc>,
 +    pub u16_type: Type<'gcc>,
 +    pub u32_type: Type<'gcc>,
 +    pub u64_type: Type<'gcc>,
 +    pub u128_type: Type<'gcc>,
 +    pub usize_type: Type<'gcc>,
 +
 +    pub int_type: Type<'gcc>,
 +    pub uint_type: Type<'gcc>,
 +    pub long_type: Type<'gcc>,
 +    pub ulong_type: Type<'gcc>,
 +    pub ulonglong_type: Type<'gcc>,
 +    pub sizet_type: Type<'gcc>,
 +
++    pub supports_128bit_integers: bool,
++
 +    pub float_type: Type<'gcc>,
 +    pub double_type: Type<'gcc>,
 +
 +    pub linkage: Cell<FunctionType>,
 +    pub scalar_types: RefCell<FxHashMap<Ty<'tcx>, Type<'gcc>>>,
 +    pub types: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), Type<'gcc>>>,
 +    pub tcx: TyCtxt<'tcx>,
 +
 +    pub struct_types: RefCell<FxHashMap<Vec<Type<'gcc>>, Type<'gcc>>>,
 +
 +    pub types_with_fields_to_set: RefCell<FxHashMap<Type<'gcc>, (Struct<'gcc>, TyAndLayout<'tcx>)>>,
 +
 +    /// Cache instances of monomorphic and polymorphic items
 +    pub instances: RefCell<FxHashMap<Instance<'tcx>, LValue<'gcc>>>,
 +    /// Cache function instances of monomorphic and polymorphic items
 +    pub function_instances: RefCell<FxHashMap<Instance<'tcx>, RValue<'gcc>>>,
 +    /// Cache generated vtables
 +    pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), RValue<'gcc>>>,
 +
++    // TODO(antoyo): improve the SSA API to not require those.
++    // Mapping from function pointer type to indexes of on stack parameters.
++    pub on_stack_params: RefCell<FxHashMap<FunctionPtrType<'gcc>, FxHashSet<usize>>>,
++    // Mapping from function to indexes of on stack parameters.
++    pub on_stack_function_params: RefCell<FxHashMap<Function<'gcc>, FxHashSet<usize>>>,
++
 +    /// Cache of emitted const globals (value -> global)
 +    pub const_globals: RefCell<FxHashMap<RValue<'gcc>, RValue<'gcc>>>,
 +
++    /// Map from the address of a global variable (rvalue) to the global variable itself (lvalue).
++    /// TODO(antoyo): remove when the rustc API is fixed.
++    pub global_lvalues: RefCell<FxHashMap<RValue<'gcc>, LValue<'gcc>>>,
++
 +    /// Cache of constant strings,
 +    pub const_str_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
 +
 +    /// Cache of globals.
 +    pub globals: RefCell<FxHashMap<String, RValue<'gcc>>>,
 +
 +    /// A counter that is used for generating local symbol names
 +    local_gen_sym_counter: Cell<usize>,
-     pub fn new(context: &'gcc Context<'gcc>, codegen_unit: &'tcx CodegenUnit<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
 +
 +    eh_personality: Cell<Option<RValue<'gcc>>>,
 +
 +    pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
 +
 +    /// NOTE: a hack is used because the rustc API is not suitable to libgccjit and as such,
 +    /// `const_undef()` returns struct as pointer so that they can later be assigned a value.
 +    /// As such, this set remembers which of these pointers were returned by this function so that
 +    /// they can be deferenced later.
 +    /// FIXME(antoyo): fix the rustc API to avoid having this hack.
 +    pub structs_as_pointer: RefCell<FxHashSet<RValue<'gcc>>>,
 +}
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
-         // TODO(antoyo): fix this mess. libgccjit seems to return random type when using new_int_type().
-         let isize_type = context.new_c_type(CType::LongLong);
-         let usize_type = context.new_c_type(CType::ULongLong);
-         let bool_type = context.new_type::<bool>();
-         let i8_type = context.new_type::<i8>();
-         let i16_type = context.new_type::<i16>();
-         let i32_type = context.new_type::<i32>();
-         let i64_type = context.new_c_type(CType::LongLong);
-         let i128_type = context.new_c_type(CType::Int128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?
-         let u8_type = context.new_type::<u8>();
-         let u16_type = context.new_type::<u16>();
-         let u32_type = context.new_type::<u32>();
-         let u64_type = context.new_c_type(CType::ULongLong);
-         let u128_type = context.new_c_type(CType::UInt128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?
++    pub fn new(context: &'gcc Context<'gcc>, codegen_unit: &'tcx CodegenUnit<'tcx>, tcx: TyCtxt<'tcx>, supports_128bit_integers: bool) -> Self {
 +        let check_overflow = tcx.sess.overflow_checks();
-         assert_eq!(isize_type, i64_type);
-         assert_eq!(usize_type, u64_type);
++
++        let i8_type = context.new_c_type(CType::Int8t);
++        let i16_type = context.new_c_type(CType::Int16t);
++        let i32_type = context.new_c_type(CType::Int32t);
++        let i64_type = context.new_c_type(CType::Int64t);
++        let u8_type = context.new_c_type(CType::UInt8t);
++        let u16_type = context.new_c_type(CType::UInt16t);
++        let u32_type = context.new_c_type(CType::UInt32t);
++        let u64_type = context.new_c_type(CType::UInt64t);
++
++        let (i128_type, u128_type) =
++            if supports_128bit_integers {
++                let i128_type = context.new_c_type(CType::Int128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
++                let u128_type = context.new_c_type(CType::UInt128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
++                (i128_type, u128_type)
++            }
++            else {
++                let i128_type = context.new_array_type(None, i64_type, 2);
++                let u128_type = context.new_array_type(None, u64_type, 2);
++                (i128_type, u128_type)
++            };
 +
 +        let tls_model = to_gcc_tls_mode(tcx.sess.tls_model());
 +
 +        let float_type = context.new_type::<f32>();
 +        let double_type = context.new_type::<f64>();
 +
 +        let int_type = context.new_c_type(CType::Int);
 +        let uint_type = context.new_c_type(CType::UInt);
 +        let long_type = context.new_c_type(CType::Long);
 +        let ulong_type = context.new_c_type(CType::ULong);
 +        let ulonglong_type = context.new_c_type(CType::ULongLong);
 +        let sizet_type = context.new_c_type(CType::SizeT);
 +
-             current_block: RefCell::new(None),
++        let isize_type = context.new_c_type(CType::LongLong);
++        let usize_type = context.new_c_type(CType::ULongLong);
++        let bool_type = context.new_type::<bool>();
++
++        // TODO(antoyo): only have those assertions on x86_64.
++        assert_eq!(isize_type.get_size(), i64_type.get_size());
++        assert_eq!(usize_type.get_size(), u64_type.get_size());
 +
 +        let mut functions = FxHashMap::default();
 +        let builtins = [
 +            "__builtin_unreachable", "abort", "__builtin_expect", "__builtin_add_overflow", "__builtin_mul_overflow",
 +            "__builtin_saddll_overflow", /*"__builtin_sadd_overflow",*/ "__builtin_smulll_overflow", /*"__builtin_smul_overflow",*/
 +            "__builtin_ssubll_overflow", /*"__builtin_ssub_overflow",*/ "__builtin_sub_overflow", "__builtin_uaddll_overflow",
 +            "__builtin_uadd_overflow", "__builtin_umulll_overflow", "__builtin_umul_overflow", "__builtin_usubll_overflow",
 +            "__builtin_usub_overflow", "sqrtf", "sqrt", "__builtin_powif", "__builtin_powi", "sinf", "sin", "cosf", "cos",
 +            "powf", "pow", "expf", "exp", "exp2f", "exp2", "logf", "log", "log10f", "log10", "log2f", "log2", "fmaf",
 +            "fma", "fabsf", "fabs", "fminf", "fmin", "fmaxf", "fmax", "copysignf", "copysign", "floorf", "floor", "ceilf",
 +            "ceil", "truncf", "trunc", "rintf", "rint", "nearbyintf", "nearbyint", "roundf", "round",
 +            "__builtin_expect_with_probability",
 +        ];
 +
 +        for builtin in builtins.iter() {
 +            functions.insert(builtin.to_string(), context.get_builtin_function(builtin));
 +        }
 +
 +        Self {
 +            check_overflow,
 +            codegen_unit,
 +            context,
-             global_gen_sym_counter: Cell::new(0),
 +            current_func: RefCell::new(None),
 +            normal_function_addresses: Default::default(),
 +            functions: RefCell::new(functions),
 +
 +            tls_model,
 +
 +            bool_type,
 +            i8_type,
 +            i16_type,
 +            i32_type,
 +            i64_type,
 +            i128_type,
 +            isize_type,
 +            usize_type,
 +            u8_type,
 +            u16_type,
 +            u32_type,
 +            u64_type,
 +            u128_type,
 +            int_type,
 +            uint_type,
 +            long_type,
 +            ulong_type,
 +            ulonglong_type,
 +            sizet_type,
 +
++            supports_128bit_integers,
++
 +            float_type,
 +            double_type,
 +
 +            linkage: Cell::new(FunctionType::Internal),
 +            instances: Default::default(),
 +            function_instances: Default::default(),
++            on_stack_params: Default::default(),
++            on_stack_function_params: Default::default(),
 +            vtables: Default::default(),
 +            const_globals: Default::default(),
++            global_lvalues: Default::default(),
 +            const_str_cache: Default::default(),
 +            globals: Default::default(),
 +            scalar_types: Default::default(),
 +            types: Default::default(),
 +            tcx,
 +            struct_types: Default::default(),
 +            types_with_fields_to_set: Default::default(),
 +            local_gen_sym_counter: Cell::new(0),
- pub fn unit_name<'tcx>(codegen_unit: &CodegenUnit<'tcx>) -> String {
-     let name = &codegen_unit.name().to_string();
-     mangle_name(&name.replace('-', "_"))
- }
 +            eh_personality: Cell::new(None),
 +            pointee_infos: Default::default(),
 +            structs_as_pointer: Default::default(),
 +        }
 +    }
 +
 +    pub fn rvalue_as_function(&self, value: RValue<'gcc>) -> Function<'gcc> {
 +        let function: Function<'gcc> = unsafe { std::mem::transmute(value) };
 +        debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
 +            "{:?} ({:?}) is not a function", value, value.get_type());
 +        function
 +    }
 +
++    pub fn is_native_int_type(&self, typ: Type<'gcc>) -> bool {
++        let types = [
++            self.u8_type,
++            self.u16_type,
++            self.u32_type,
++            self.u64_type,
++            self.i8_type,
++            self.i16_type,
++            self.i32_type,
++            self.i64_type,
++        ];
++
++        for native_type in types {
++            if native_type.is_compatible_with(typ) {
++                return true;
++            }
++        }
++
++        self.supports_128bit_integers &&
++            (self.u128_type.is_compatible_with(typ) || self.i128_type.is_compatible_with(typ))
++    }
++
++    pub fn is_non_native_int_type(&self, typ: Type<'gcc>) -> bool {
++        !self.supports_128bit_integers &&
++            (self.u128_type.is_compatible_with(typ) || self.i128_type.is_compatible_with(typ))
++    }
++
++    pub fn is_native_int_type_or_bool(&self, typ: Type<'gcc>) -> bool {
++        self.is_native_int_type(typ) || typ == self.bool_type
++    }
++
++    pub fn is_int_type_or_bool(&self, typ: Type<'gcc>) -> bool {
++        self.is_native_int_type(typ) || self.is_non_native_int_type(typ) || typ == self.bool_type
++    }
++
 +    pub fn sess(&self) -> &Session {
 +        &self.tcx.sess
 +    }
 +}
 +
 +impl<'gcc, 'tcx> BackendTypes for CodegenCx<'gcc, 'tcx> {
 +    type Value = RValue<'gcc>;
 +    type Function = RValue<'gcc>;
 +
 +    type BasicBlock = Block<'gcc>;
 +    type Type = Type<'gcc>;
 +    type Funclet = (); // TODO(antoyo)
 +
 +    type DIScope = (); // TODO(antoyo)
 +    type DILocation = (); // TODO(antoyo)
 +    type DIVariable = (); // TODO(antoyo)
 +}
 +
 +impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<PolyExistentialTraitRef<'tcx>>), RValue<'gcc>>> {
 +        &self.vtables
 +    }
 +
 +    fn get_fn(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
 +        let func = get_fn(self, instance);
 +        *self.current_func.borrow_mut() = Some(self.rvalue_as_function(func));
 +        func
 +    }
 +
 +    fn get_fn_addr(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
 +        let func = get_fn(self, instance);
 +        let func = self.rvalue_as_function(func);
 +        let ptr = func.get_address(None);
 +
 +        // TODO(antoyo): don't do this twice: i.e. in declare_fn and here.
 +        // FIXME(antoyo): the rustc API seems to call get_fn_addr() when not needed (e.g. for FFI).
 +
 +        self.normal_function_addresses.borrow_mut().insert(ptr);
 +
 +        ptr
 +    }
 +
 +    fn eh_personality(&self) -> RValue<'gcc> {
 +        // The exception handling personality function.
 +        //
 +        // If our compilation unit has the `eh_personality` lang item somewhere
 +        // within it, then we just need to codegen that. Otherwise, we're
 +        // building an rlib which will depend on some upstream implementation of
 +        // this function, so we just codegen a generic reference to it. We don't
 +        // specify any of the types for the function, we just make it a symbol
 +        // that LLVM can later use.
 +        //
 +        // Note that MSVC is a little special here in that we don't use the
 +        // `eh_personality` lang item at all. Currently LLVM has support for
 +        // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
 +        // *name of the personality function* to decide what kind of unwind side
 +        // tables/landing pads to emit. It looks like Dwarf is used by default,
 +        // injecting a dependency on the `_Unwind_Resume` symbol for resuming
 +        // an "exception", but for MSVC we want to force SEH. This means that we
 +        // can't actually have the personality function be our standard
 +        // `rust_eh_personality` function, but rather we wired it up to the
 +        // CRT's custom personality function, which forces LLVM to consider
 +        // landing pads as "landing pads for SEH".
 +        if let Some(llpersonality) = self.eh_personality.get() {
 +            return llpersonality;
 +        }
 +        let tcx = self.tcx;
 +        let llfn = match tcx.lang_items().eh_personality() {
 +            Some(def_id) if !wants_msvc_seh(self.sess()) => self.get_fn_addr(
 +                ty::Instance::resolve(
 +                    tcx,
 +                    ty::ParamEnv::reveal_all(),
 +                    def_id,
 +                    tcx.intern_substs(&[]),
 +                )
 +                .unwrap().unwrap(),
 +            ),
 +            _ => {
 +                let _name = if wants_msvc_seh(self.sess()) {
 +                    "__CxxFrameHandler3"
 +                } else {
 +                    "rust_eh_personality"
 +                };
 +                //let func = self.declare_func(name, self.type_i32(), &[], true);
 +                // FIXME(antoyo): this hack should not be needed. That will probably be removed when
 +                // unwinding support is added.
 +                self.context.new_rvalue_from_int(self.int_type, 0)
 +            }
 +        };
 +        // TODO(antoyo): apply target cpu attributes.
 +        self.eh_personality.set(Some(llfn));
 +        llfn
 +    }
 +
 +    fn sess(&self) -> &Session {
 +        &self.tcx.sess
 +    }
 +
 +    fn check_overflow(&self) -> bool {
 +        self.check_overflow
 +    }
 +
 +    fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
 +        self.codegen_unit
 +    }
 +
 +    fn used_statics(&self) -> &RefCell<Vec<RValue<'gcc>>> {
 +        unimplemented!();
 +    }
 +
 +    fn set_frame_pointer_type(&self, _llfn: RValue<'gcc>) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn apply_target_cpu_attr(&self, _llfn: RValue<'gcc>) {
 +        // TODO(antoyo)
 +    }
 +
 +    fn create_used_variable(&self) {
 +        unimplemented!();
 +    }
 +
 +    fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
 +        if self.get_declared_value("main").is_none() {
 +            Some(self.declare_cfn("main", fn_type))
 +        }
 +        else {
 +            // If the symbol already exists, it is an error: for example, the user wrote
 +            // #[no_mangle] extern "C" fn main(..) {..}
 +            // instead of #[start]
 +            None
 +        }
 +    }
 +
 +    fn compiler_used_statics(&self) -> &RefCell<Vec<RValue<'gcc>>> {
 +        unimplemented!()
 +    }
 +
 +    fn create_compiler_used_variable(&self) {
 +        unimplemented!()
 +    }
 +}
 +
 +impl<'gcc, 'tcx> HasTyCtxt<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    fn tcx(&self) -> TyCtxt<'tcx> {
 +        self.tcx
 +    }
 +}
 +
 +impl<'gcc, 'tcx> HasDataLayout for CodegenCx<'gcc, 'tcx> {
 +    fn data_layout(&self) -> &TargetDataLayout {
 +        &self.tcx.data_layout
 +    }
 +}
 +
 +impl<'gcc, 'tcx> HasTargetSpec for CodegenCx<'gcc, 'tcx> {
 +    fn target_spec(&self) -> &Target {
 +        &self.tcx.sess.target
 +    }
 +}
 +
 +impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    type LayoutOfResult = TyAndLayout<'tcx>;
 +
 +    #[inline]
 +    fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
 +        if let LayoutError::SizeOverflow(_) = err {
 +            self.sess().span_fatal(span, &err.to_string())
 +        } else {
 +            span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
 +        }
 +    }
 +}
 +
 +impl<'gcc, 'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
 +
 +    #[inline]
 +    fn handle_fn_abi_err(
 +        &self,
 +        err: FnAbiError<'tcx>,
 +        span: Span,
 +        fn_abi_request: FnAbiRequest<'tcx>,
 +    ) -> ! {
 +        if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
 +            self.sess().span_fatal(span, &err.to_string())
 +        } else {
 +            match fn_abi_request {
 +                FnAbiRequest::OfFnPtr { sig, extra_args } => {
 +                    span_bug!(
 +                        span,
 +                        "`fn_abi_of_fn_ptr({}, {:?})` failed: {}",
 +                        sig,
 +                        extra_args,
 +                        err
 +                    );
 +                }
 +                FnAbiRequest::OfInstance { instance, extra_args } => {
 +                    span_bug!(
 +                        span,
 +                        "`fn_abi_of_instance({}, {:?})` failed: {}",
 +                        instance,
 +                        extra_args,
 +                        err
 +                    );
 +                }
 +            }
 +        }
 +    }
 +}
 +
 +impl<'tcx, 'gcc> HasParamEnv<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    fn param_env(&self) -> ParamEnv<'tcx> {
 +        ParamEnv::reveal_all()
 +    }
 +}
 +
 +impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
 +    /// Generates a new symbol name with the given prefix. This symbol name must
 +    /// only be used for definitions with `internal` or `private` linkage.
 +    pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
 +        let idx = self.local_gen_sym_counter.get();
 +        self.local_gen_sym_counter.set(idx + 1);
 +        // Include a '.' character, so there can be no accidental conflicts with
 +        // user defined names
 +        let mut name = String::with_capacity(prefix.len() + 6);
 +        name.push_str(prefix);
 +        name.push_str(".");
 +        base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
 +        name
 +    }
 +}
 +
 +fn to_gcc_tls_mode(tls_model: TlsModel) -> gccjit::TlsModel {
 +    match tls_model {
 +        TlsModel::GeneralDynamic => gccjit::TlsModel::GlobalDynamic,
 +        TlsModel::LocalDynamic => gccjit::TlsModel::LocalDynamic,
 +        TlsModel::InitialExec => gccjit::TlsModel::InitialExec,
 +        TlsModel::LocalExec => gccjit::TlsModel::LocalExec,
 +    }
 +}
index dbee505a4977b26f1853acb52643f194fd5c437b,0000000000000000000000000000000000000000..43017376916dca5e7f48aa94b4fd5c14f3f4a2d9
mode 100644,000000..100644
--- /dev/null
@@@ -1,144 -1,0 +1,143 @@@
- use crate::context::{CodegenCx, unit_name};
 +use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, Type};
 +use rustc_codegen_ssa::traits::BaseTypeMethods;
 +use rustc_middle::ty::Ty;
 +use rustc_span::Symbol;
 +use rustc_target::abi::call::FnAbi;
 +
 +use crate::abi::FnAbiGccExt;
-             self.declare_global(name, ty, is_tls, link_section)
++use crate::context::CodegenCx;
 +use crate::intrinsic::llvm;
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
 +    pub fn get_or_insert_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
 +        if self.globals.borrow().contains_key(name) {
 +            let typ = self.globals.borrow().get(name).expect("global").get_type();
 +            let global = self.context.new_global(None, GlobalKind::Imported, typ, name);
 +            if is_tls {
 +                global.set_tls_model(self.tls_model);
 +            }
 +            if let Some(link_section) = link_section {
 +                global.set_link_section(link_section.as_str());
 +            }
 +            global
 +        }
 +        else {
-         let index = self.global_gen_sym_counter.get();
-         self.global_gen_sym_counter.set(index + 1);
-         let name = format!("global_{}_{}", index, unit_name(&self.codegen_unit));
-         self.context.new_global(None, GlobalKind::Exported, ty, &name)
++            self.declare_global(name, ty, GlobalKind::Exported, is_tls, link_section)
 +        }
 +    }
 +
 +    pub fn declare_unnamed_global(&self, ty: Type<'gcc>) -> LValue<'gcc> {
-     pub fn declare_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
-         let global = self.context.new_global(None, GlobalKind::Exported, ty, name);
++        let name = self.generate_local_symbol_name("global");
++        self.context.new_global(None, GlobalKind::Internal, ty, &name)
 +    }
 +
 +    pub fn declare_global_with_linkage(&self, name: &str, ty: Type<'gcc>, linkage: GlobalKind) -> LValue<'gcc> {
 +        let global = self.context.new_global(None, linkage, ty, name);
 +        let global_address = global.get_address(None);
 +        self.globals.borrow_mut().insert(name.to_string(), global_address);
 +        global
 +    }
 +
 +    /*pub fn declare_func(&self, name: &str, return_type: Type<'gcc>, params: &[Type<'gcc>], variadic: bool) -> RValue<'gcc> {
 +        self.linkage.set(FunctionType::Exported);
 +        let func = declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, params, variadic);
 +        // FIXME(antoyo): this is a wrong cast. That requires changing the compiler API.
 +        unsafe { std::mem::transmute(func) }
 +    }*/
 +
-         let (return_type, params, variadic) = fn_abi.gcc_type(self);
++    pub fn declare_global(&self, name: &str, ty: Type<'gcc>, global_kind: GlobalKind, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
++        let global = self.context.new_global(None, global_kind, ty, name);
 +        if is_tls {
 +            global.set_tls_model(self.tls_model);
 +        }
 +        if let Some(link_section) = link_section {
 +            global.set_link_section(link_section.as_str());
 +        }
 +        let global_address = global.get_address(None);
 +        self.globals.borrow_mut().insert(name.to_string(), global_address);
 +        global
 +    }
 +
 +    pub fn declare_private_global(&self, name: &str, ty: Type<'gcc>) -> LValue<'gcc> {
 +        let global = self.context.new_global(None, GlobalKind::Internal, ty, name);
 +        let global_address = global.get_address(None);
 +        self.globals.borrow_mut().insert(name.to_string(), global_address);
 +        global
 +    }
 +
 +    pub fn declare_cfn(&self, name: &str, _fn_type: Type<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): use the fn_type parameter.
 +        let const_string = self.context.new_type::<u8>().make_pointer().make_pointer();
 +        let return_type = self.type_i32();
 +        let variadic = false;
 +        self.linkage.set(FunctionType::Exported);
 +        let func = declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, &[self.type_i32(), const_string], variadic);
 +        // NOTE: it is needed to set the current_func here as well, because get_fn() is not called
 +        // for the main function.
 +        *self.current_func.borrow_mut() = Some(func);
 +        // FIXME(antoyo): this is a wrong cast. That requires changing the compiler API.
 +        unsafe { std::mem::transmute(func) }
 +    }
 +
 +    pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> RValue<'gcc> {
++        let (return_type, params, variadic, on_stack_param_indices) = fn_abi.gcc_type(self);
 +        let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &params, variadic);
++        self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices);
 +        // FIXME(antoyo): this is a wrong cast. That requires changing the compiler API.
 +        unsafe { std::mem::transmute(func) }
 +    }
 +
 +    pub fn define_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
 +        self.get_or_insert_global(name, ty, is_tls, link_section)
 +    }
 +
 +    pub fn get_declared_value(&self, name: &str) -> Option<RValue<'gcc>> {
 +        // TODO(antoyo): use a different field than globals, because this seems to return a function?
 +        self.globals.borrow().get(name).cloned()
 +    }
 +}
 +
 +/// Declare a function.
 +///
 +/// If there’s a value with the same name already declared, the function will
 +/// update the declaration and return existing Value instead.
 +fn declare_raw_fn<'gcc>(cx: &CodegenCx<'gcc, '_>, name: &str, _callconv: () /*llvm::CallConv*/, return_type: Type<'gcc>, param_types: &[Type<'gcc>], variadic: bool) -> Function<'gcc> {
 +    if name.starts_with("llvm.") {
 +        return llvm::intrinsic(name, cx);
 +    }
 +    let func =
 +        if cx.functions.borrow().contains_key(name) {
 +            *cx.functions.borrow().get(name).expect("function")
 +        }
 +        else {
 +            let params: Vec<_> = param_types.into_iter().enumerate()
 +                .map(|(index, param)| cx.context.new_parameter(None, *param, &format!("param{}", index))) // TODO(antoyo): set name.
 +                .collect();
 +            let func = cx.context.new_function(None, cx.linkage.get(), return_type, &params, mangle_name(name), variadic);
 +            cx.functions.borrow_mut().insert(name.to_string(), func);
 +            func
 +        };
 +
 +    // TODO(antoyo): set function calling convention.
 +    // TODO(antoyo): set unnamed address.
 +    // TODO(antoyo): set no red zone function attribute.
 +    // TODO(antoyo): set attributes for optimisation.
 +    // TODO(antoyo): set attributes for non lazy bind.
 +
 +    // FIXME(antoyo): invalid cast.
 +    func
 +}
 +
 +// FIXME(antoyo): this is a hack because libgccjit currently only supports alpha, num and _.
 +// Unsupported characters: `$` and `.`.
 +pub fn mangle_name(name: &str) -> String {
 +    name.replace(|char: char| {
 +        if !char.is_alphanumeric() && char != '_' {
 +            debug_assert!("$.".contains(char), "Unsupported char in function name: {}", char);
 +            true
 +        }
 +        else {
 +            false
 +        }
 +    }, "_")
 +}
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..c3ed71ff730368f9bbda3473cf9ccfe8a550163b
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,730 @@@
++//! Module to handle integer operations.
++//! This module exists because some integer types are not supported on some gcc platforms, e.g.
++//! 128-bit integers on 32-bit platforms and thus require to be handled manually.
++
++use std::convert::TryFrom;
++
++use gccjit::{ComparisonOp, FunctionType, RValue, ToRValue, Type, UnaryOp, BinaryOp};
++use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
++use rustc_codegen_ssa::traits::{BackendTypes, BaseTypeMethods, BuilderMethods, OverflowOp};
++use rustc_middle::ty::Ty;
++
++use crate::builder::ToGccComp;
++use crate::{builder::Builder, common::{SignType, TypeReflection}, context::CodegenCx};
++
++impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
++    pub fn gcc_urem(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        // 128-bit unsigned %: __umodti3
++        self.multiplicative_operation(BinaryOp::Modulo, "mod", false, a, b)
++    }
++
++    pub fn gcc_srem(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        // 128-bit signed %:   __modti3
++        self.multiplicative_operation(BinaryOp::Modulo, "mod", true, a, b)
++    }
++
++    pub fn gcc_not(&self, a: RValue<'gcc>) -> RValue<'gcc> {
++        let typ = a.get_type();
++        if self.is_native_int_type_or_bool(typ) {
++            let operation =
++                if typ.is_bool() {
++                    UnaryOp::LogicalNegate
++                }
++                else {
++                    UnaryOp::BitwiseNegate
++                };
++            self.cx.context.new_unary_op(None, operation, typ, a)
++        }
++        else {
++            // TODO(antoyo): use __negdi2 and __negti2 instead?
++            let element_type = typ.dyncast_array().expect("element type");
++            let values = [
++                self.cx.context.new_unary_op(None, UnaryOp::BitwiseNegate, element_type, self.low(a)),
++                self.cx.context.new_unary_op(None, UnaryOp::BitwiseNegate, element_type, self.high(a)),
++            ];
++            self.cx.context.new_array_constructor(None, typ, &values)
++        }
++    }
++
++    pub fn gcc_neg(&self, a: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = a.get_type();
++        if self.is_native_int_type(a_type) {
++            self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
++        }
++        else {
++            let param_a = self.context.new_parameter(None, a_type, "a");
++            let func = self.context.new_function(None, FunctionType::Extern, a_type, &[param_a], "__negti2", false);
++            self.context.new_call(None, func, &[a])
++        }
++    }
++
++    pub fn gcc_and(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.cx.bitwise_operation(BinaryOp::BitwiseAnd, a, b)
++    }
++
++    pub fn gcc_lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = a.get_type();
++        let b_type = b.get_type();
++        let a_native = self.is_native_int_type(a_type);
++        let b_native = self.is_native_int_type(b_type);
++        if a_native && b_native {
++            // FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by a signed number.
++            // TODO(antoyo): cast to unsigned to do a logical shift if that does not work.
++            if a_type.is_signed(self) != b_type.is_signed(self) {
++                let b = self.context.new_cast(None, b, a_type);
++                a >> b
++            }
++            else {
++                a >> b
++            }
++        }
++        else if a_native && !b_native {
++            self.gcc_lshr(a, self.gcc_int_cast(b, a_type))
++        }
++        else {
++            // NOTE: we cannot use the lshr builtin because it's calling hi() (to get the most
++            // significant half of the number) which uses lshr.
++
++            let native_int_type = a_type.dyncast_array().expect("get element type");
++
++            let func = self.current_func();
++            let then_block = func.new_block("then");
++            let else_block = func.new_block("else");
++            let after_block = func.new_block("after");
++            let b0_block = func.new_block("b0");
++            let actual_else_block = func.new_block("actual_else");
++
++            let result = func.new_local(None, a_type, "shiftResult");
++
++            let sixty_four = self.gcc_int(native_int_type, 64);
++            let sixty_three = self.gcc_int(native_int_type, 63);
++            let zero = self.gcc_zero(native_int_type);
++            let b = self.gcc_int_cast(b, native_int_type);
++            let condition = self.gcc_icmp(IntPredicate::IntNE, self.gcc_and(b, sixty_four), zero);
++            self.llbb().end_with_conditional(None, condition, then_block, else_block);
++
++            // TODO(antoyo): take endianness into account.
++            let shift_value = self.gcc_sub(b, sixty_four);
++            let high = self.high(a);
++            let sign =
++                if a_type.is_signed(self) {
++                    high >> sixty_three
++                }
++                else {
++                    zero
++                };
++            let values = [
++                high >> shift_value,
++                sign,
++            ];
++            let array_value = self.context.new_array_constructor(None, a_type, &values);
++            then_block.add_assignment(None, result, array_value);
++            then_block.end_with_jump(None, after_block);
++
++            let condition = self.gcc_icmp(IntPredicate::IntEQ, b, zero);
++            else_block.end_with_conditional(None, condition, b0_block, actual_else_block);
++
++            b0_block.add_assignment(None, result, a);
++            b0_block.end_with_jump(None, after_block);
++
++            let shift_value = self.gcc_sub(sixty_four, b);
++            // NOTE: cast low to its unsigned type in order to perform a logical right shift.
++            let unsigned_type = native_int_type.to_unsigned(&self.cx);
++            let casted_low = self.context.new_cast(None, self.low(a), unsigned_type);
++            let shifted_low = casted_low >> self.context.new_cast(None, b, unsigned_type);
++            let shifted_low = self.context.new_cast(None, shifted_low, native_int_type);
++            let values = [
++                (high << shift_value) | shifted_low,
++                high >> b,
++            ];
++            let array_value = self.context.new_array_constructor(None, a_type, &values);
++            actual_else_block.add_assignment(None, result, array_value);
++            actual_else_block.end_with_jump(None, after_block);
++
++            // NOTE: since jumps were added in a place rustc does not expect, the current block in the
++            // state need to be updated.
++            self.switch_to_block(after_block);
++
++            result.to_rvalue()
++        }
++    }
++
++    fn additive_operation(&self, operation: BinaryOp, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = a.get_type();
++        let b_type = b.get_type();
++        if self.is_native_int_type_or_bool(a_type) && self.is_native_int_type_or_bool(b_type) {
++            if a.get_type() != b.get_type() {
++                b = self.context.new_cast(None, b, a.get_type());
++            }
++            self.context.new_binary_op(None, operation, a_type, a, b)
++        }
++        else {
++            let signed = a_type.is_compatible_with(self.i128_type);
++            let func_name =
++                match (operation, signed) {
++                    (BinaryOp::Plus, true) => "__rust_i128_add",
++                    (BinaryOp::Plus, false) => "__rust_u128_add",
++                    (BinaryOp::Minus, true) => "__rust_i128_sub",
++                    (BinaryOp::Minus, false) => "__rust_u128_sub",
++                    _ => unreachable!("unexpected additive operation {:?}", operation),
++                };
++            let param_a = self.context.new_parameter(None, a_type, "a");
++            let param_b = self.context.new_parameter(None, b_type, "b");
++            let func = self.context.new_function(None, FunctionType::Extern, a_type, &[param_a, param_b], func_name, false);
++            self.context.new_call(None, func, &[a, b])
++        }
++    }
++
++    pub fn gcc_add(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.additive_operation(BinaryOp::Plus, a, b)
++    }
++
++    pub fn gcc_mul(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.multiplicative_operation(BinaryOp::Mult, "mul", true, a, b)
++    }
++
++    pub fn gcc_sub(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.additive_operation(BinaryOp::Minus, a, b)
++    }
++
++    fn multiplicative_operation(&self, operation: BinaryOp, operation_name: &str, signed: bool, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = a.get_type();
++        let b_type = b.get_type();
++        if self.is_native_int_type_or_bool(a_type) && self.is_native_int_type_or_bool(b_type) {
++            self.context.new_binary_op(None, operation, a_type, a, b)
++        }
++        else {
++            let sign =
++                if signed {
++                    ""
++                }
++                else {
++                    "u"
++                };
++            let func_name = format!("__{}{}ti3", sign, operation_name);
++            let param_a = self.context.new_parameter(None, a_type, "a");
++            let param_b = self.context.new_parameter(None, b_type, "b");
++            let func = self.context.new_function(None, FunctionType::Extern, a_type, &[param_a, param_b], func_name, false);
++            self.context.new_call(None, func, &[a, b])
++        }
++    }
++
++    pub fn gcc_sdiv(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        // TODO(antoyo): check if the types are signed?
++        // 128-bit, signed: __divti3
++        // TODO(antoyo): convert the arguments to signed?
++        self.multiplicative_operation(BinaryOp::Divide, "div", true, a, b)
++    }
++
++    pub fn gcc_udiv(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        // 128-bit, unsigned: __udivti3
++        self.multiplicative_operation(BinaryOp::Divide, "div", false, a, b)
++    }
++
++    pub fn gcc_checked_binop(&self, oop: OverflowOp, typ: Ty<'_>, lhs: <Self as BackendTypes>::Value, rhs: <Self as BackendTypes>::Value) -> (<Self as BackendTypes>::Value, <Self as BackendTypes>::Value) {
++        use rustc_middle::ty::{Int, IntTy::*, Uint, UintTy::*};
++
++        let new_kind =
++            match typ.kind() {
++                Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
++                Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
++                t @ (Uint(_) | Int(_)) => t.clone(),
++                _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
++            };
++
++        // TODO(antoyo): remove duplication with intrinsic?
++        let name =
++            if self.is_native_int_type(lhs.get_type()) {
++                match oop {
++                    OverflowOp::Add =>
++                        match new_kind {
++                            Int(I8) => "__builtin_add_overflow",
++                            Int(I16) => "__builtin_add_overflow",
++                            Int(I32) => "__builtin_sadd_overflow",
++                            Int(I64) => "__builtin_saddll_overflow",
++                            Int(I128) => "__builtin_add_overflow",
++
++                            Uint(U8) => "__builtin_add_overflow",
++                            Uint(U16) => "__builtin_add_overflow",
++                            Uint(U32) => "__builtin_uadd_overflow",
++                            Uint(U64) => "__builtin_uaddll_overflow",
++                            Uint(U128) => "__builtin_add_overflow",
++
++                            _ => unreachable!(),
++                        },
++                    OverflowOp::Sub =>
++                        match new_kind {
++                            Int(I8) => "__builtin_sub_overflow",
++                            Int(I16) => "__builtin_sub_overflow",
++                            Int(I32) => "__builtin_ssub_overflow",
++                            Int(I64) => "__builtin_ssubll_overflow",
++                            Int(I128) => "__builtin_sub_overflow",
++
++                            Uint(U8) => "__builtin_sub_overflow",
++                            Uint(U16) => "__builtin_sub_overflow",
++                            Uint(U32) => "__builtin_usub_overflow",
++                            Uint(U64) => "__builtin_usubll_overflow",
++                            Uint(U128) => "__builtin_sub_overflow",
++
++                            _ => unreachable!(),
++                        },
++                    OverflowOp::Mul =>
++                        match new_kind {
++                            Int(I8) => "__builtin_mul_overflow",
++                            Int(I16) => "__builtin_mul_overflow",
++                            Int(I32) => "__builtin_smul_overflow",
++                            Int(I64) => "__builtin_smulll_overflow",
++                            Int(I128) => "__builtin_mul_overflow",
++
++                            Uint(U8) => "__builtin_mul_overflow",
++                            Uint(U16) => "__builtin_mul_overflow",
++                            Uint(U32) => "__builtin_umul_overflow",
++                            Uint(U64) => "__builtin_umulll_overflow",
++                            Uint(U128) => "__builtin_mul_overflow",
++
++                            _ => unreachable!(),
++                        },
++                }
++            }
++            else {
++                match new_kind {
++                    Int(I128) | Uint(U128) => {
++                        let func_name =
++                            match oop {
++                                OverflowOp::Add =>
++                                    match new_kind {
++                                        Int(I128) => "__rust_i128_addo",
++                                        Uint(U128) => "__rust_u128_addo",
++                                        _ => unreachable!(),
++                                    },
++                                OverflowOp::Sub =>
++                                    match new_kind {
++                                        Int(I128) => "__rust_i128_subo",
++                                        Uint(U128) => "__rust_u128_subo",
++                                        _ => unreachable!(),
++                                    },
++                                OverflowOp::Mul =>
++                                    match new_kind {
++                                        Int(I128) => "__rust_i128_mulo", // TODO(antoyo): use __muloti4d instead?
++                                        Uint(U128) => "__rust_u128_mulo",
++                                        _ => unreachable!(),
++                                    },
++                            };
++                        let a_type = lhs.get_type();
++                        let b_type = rhs.get_type();
++                        let param_a = self.context.new_parameter(None, a_type, "a");
++                        let param_b = self.context.new_parameter(None, b_type, "b");
++                        let result_field = self.context.new_field(None, a_type, "result");
++                        let overflow_field = self.context.new_field(None, self.bool_type, "overflow");
++                        let return_type = self.context.new_struct_type(None, "result_overflow", &[result_field, overflow_field]);
++                        let func = self.context.new_function(None, FunctionType::Extern, return_type.as_type(), &[param_a, param_b], func_name, false);
++                        let result = self.context.new_call(None, func, &[lhs, rhs]);
++                        let overflow = result.access_field(None, overflow_field);
++                        let int_result = result.access_field(None, result_field);
++                        return (int_result, overflow);
++                    },
++                    _ => {
++                        match oop {
++                            OverflowOp::Mul =>
++                                match new_kind {
++                                    Int(I32) => "__mulosi4",
++                                    Int(I64) => "__mulodi4",
++                                    _ => unreachable!(),
++                                },
++                            _ => unimplemented!("overflow operation for {:?}", new_kind),
++                        }
++                    }
++                }
++            };
++
++        let intrinsic = self.context.get_builtin_function(&name);
++        let res = self.current_func()
++            // TODO(antoyo): is it correct to use rhs type instead of the parameter typ?
++            .new_local(None, rhs.get_type(), "binopResult")
++            .get_address(None);
++        let overflow = self.overflow_call(intrinsic, &[lhs, rhs, res], None);
++        (res.dereference(None).to_rvalue(), overflow)
++    }
++
++    pub fn gcc_icmp(&self, op: IntPredicate, mut lhs: RValue<'gcc>, mut rhs: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = lhs.get_type();
++        let b_type = rhs.get_type();
++        if self.is_non_native_int_type(a_type) || self.is_non_native_int_type(b_type) {
++            let signed = a_type.is_compatible_with(self.i128_type);
++            let sign =
++                if signed {
++                    ""
++                }
++                else {
++                    "u"
++                };
++            let func_name = format!("__{}cmpti2", sign);
++            let param_a = self.context.new_parameter(None, a_type, "a");
++            let param_b = self.context.new_parameter(None, b_type, "b");
++            let func = self.context.new_function(None, FunctionType::Extern, self.int_type, &[param_a, param_b], func_name, false);
++            let cmp = self.context.new_call(None, func, &[lhs, rhs]);
++            let (op, limit) =
++                match op {
++                    IntPredicate::IntEQ => {
++                        return self.context.new_comparison(None, ComparisonOp::Equals, cmp, self.context.new_rvalue_one(self.int_type));
++                    },
++                    IntPredicate::IntNE => {
++                        return self.context.new_comparison(None, ComparisonOp::NotEquals, cmp, self.context.new_rvalue_one(self.int_type));
++                    },
++                    IntPredicate::IntUGT => (ComparisonOp::Equals, 2),
++                    IntPredicate::IntUGE => (ComparisonOp::GreaterThanEquals, 1),
++                    IntPredicate::IntULT => (ComparisonOp::Equals, 0),
++                    IntPredicate::IntULE => (ComparisonOp::LessThanEquals, 1),
++                    IntPredicate::IntSGT => (ComparisonOp::Equals, 2),
++                    IntPredicate::IntSGE => (ComparisonOp::GreaterThanEquals, 1),
++                    IntPredicate::IntSLT => (ComparisonOp::Equals, 0),
++                    IntPredicate::IntSLE => (ComparisonOp::LessThanEquals, 1),
++                };
++            self.context.new_comparison(None, op, cmp, self.context.new_rvalue_from_int(self.int_type, limit))
++        }
++        else {
++            let left_type = lhs.get_type();
++            let right_type = rhs.get_type();
++            if left_type != right_type {
++                // NOTE: because libgccjit cannot compare function pointers.
++                if left_type.dyncast_function_ptr_type().is_some() && right_type.dyncast_function_ptr_type().is_some() {
++                    lhs = self.context.new_cast(None, lhs, self.usize_type.make_pointer());
++                    rhs = self.context.new_cast(None, rhs, self.usize_type.make_pointer());
++                }
++                // NOTE: hack because we try to cast a vector type to the same vector type.
++                else if format!("{:?}", left_type) != format!("{:?}", right_type) {
++                    rhs = self.context.new_cast(None, rhs, left_type);
++                }
++            }
++            self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
++        }
++    }
++
++    pub fn gcc_xor(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = a.get_type();
++        let b_type = b.get_type();
++        if self.is_native_int_type_or_bool(a_type) && self.is_native_int_type_or_bool(b_type) {
++            a ^ b
++        }
++        else {
++            let values = [
++                self.low(a) ^ self.low(b),
++                self.high(a) ^ self.high(b),
++            ];
++            self.context.new_array_constructor(None, a_type, &values)
++        }
++    }
++
++    pub fn gcc_shl(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = a.get_type();
++        let b_type = b.get_type();
++        let a_native = self.is_native_int_type(a_type);
++        let b_native = self.is_native_int_type(b_type);
++        if a_native && b_native {
++            // FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by an unsigned number.
++            if a_type.is_unsigned(self) && b_type.is_signed(self) {
++                let a = self.context.new_cast(None, a, b_type);
++                let result = a << b;
++                self.context.new_cast(None, result, a_type)
++            }
++            else if a_type.is_signed(self) && b_type.is_unsigned(self) {
++                let b = self.context.new_cast(None, b, a_type);
++                a << b
++            }
++            else {
++                a << b
++            }
++        }
++        else if a_native && !b_native {
++            self.gcc_shl(a, self.gcc_int_cast(b, a_type))
++        }
++        else {
++            // NOTE: we cannot use the ashl builtin because it's calling widen_hi() which uses ashl.
++            let native_int_type = a_type.dyncast_array().expect("get element type");
++
++            let func = self.current_func();
++            let then_block = func.new_block("then");
++            let else_block = func.new_block("else");
++            let after_block = func.new_block("after");
++            let b0_block = func.new_block("b0");
++            let actual_else_block = func.new_block("actual_else");
++
++            let result = func.new_local(None, a_type, "shiftResult");
++
++            let b = self.gcc_int_cast(b, native_int_type);
++            let sixty_four = self.gcc_int(native_int_type, 64);
++            let zero = self.gcc_zero(native_int_type);
++            let condition = self.gcc_icmp(IntPredicate::IntNE, self.gcc_and(b, sixty_four), zero);
++            self.llbb().end_with_conditional(None, condition, then_block, else_block);
++
++            // TODO(antoyo): take endianness into account.
++            let values = [
++                zero,
++                self.low(a) << (b - sixty_four),
++            ];
++            let array_value = self.context.new_array_constructor(None, a_type, &values);
++            then_block.add_assignment(None, result, array_value);
++            then_block.end_with_jump(None, after_block);
++
++            let condition = self.gcc_icmp(IntPredicate::IntEQ, b, zero);
++            else_block.end_with_conditional(None, condition, b0_block, actual_else_block);
++
++            b0_block.add_assignment(None, result, a);
++            b0_block.end_with_jump(None, after_block);
++
++            // NOTE: cast low to its unsigned type in order to perform a logical right shift.
++            let unsigned_type = native_int_type.to_unsigned(&self.cx);
++            let casted_low = self.context.new_cast(None, self.low(a), unsigned_type);
++            let shift_value = self.context.new_cast(None, sixty_four - b, unsigned_type);
++            let high_low = self.context.new_cast(None, casted_low >> shift_value, native_int_type);
++            let values = [
++                self.low(a) << b,
++                (self.high(a) << b) | high_low,
++            ];
++
++            let array_value = self.context.new_array_constructor(None, a_type, &values);
++            actual_else_block.add_assignment(None, result, array_value);
++            actual_else_block.end_with_jump(None, after_block);
++
++            // NOTE: since jumps were added in a place rustc does not expect, the current block in the
++            // state need to be updated.
++            self.switch_to_block(after_block);
++
++            result.to_rvalue()
++        }
++    }
++
++    pub fn gcc_bswap(&mut self, mut arg: RValue<'gcc>, width: u64) -> RValue<'gcc> {
++        let arg_type = arg.get_type();
++        if !self.is_native_int_type(arg_type) {
++            let native_int_type = arg_type.dyncast_array().expect("get element type");
++            let lsb = self.context.new_array_access(None, arg, self.context.new_rvalue_from_int(self.int_type, 0)).to_rvalue();
++            let swapped_lsb = self.gcc_bswap(lsb, width / 2);
++            let swapped_lsb = self.context.new_cast(None, swapped_lsb, native_int_type);
++            let msb = self.context.new_array_access(None, arg, self.context.new_rvalue_from_int(self.int_type, 1)).to_rvalue();
++            let swapped_msb = self.gcc_bswap(msb, width / 2);
++            let swapped_msb = self.context.new_cast(None, swapped_msb, native_int_type);
++
++            // NOTE: we also need to swap the two elements here, in addition to swapping inside
++            // the elements themselves like done above.
++            return self.context.new_array_constructor(None, arg_type, &[swapped_msb, swapped_lsb]);
++        }
++
++        // TODO(antoyo): check if it's faster to use string literals and a
++        // match instead of format!.
++        let bswap = self.cx.context.get_builtin_function(&format!("__builtin_bswap{}", width));
++        // FIXME(antoyo): this cast should not be necessary. Remove
++        // when having proper sized integer types.
++        let param_type = bswap.get_param(0).to_rvalue().get_type();
++        if param_type != arg_type {
++            arg = self.bitcast(arg, param_type);
++        }
++        self.cx.context.new_call(None, bswap, &[arg])
++    }
++}
++
++impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
++    pub fn gcc_int(&self, typ: Type<'gcc>, int: i64) -> RValue<'gcc> {
++        if self.is_native_int_type_or_bool(typ) {
++            self.context.new_rvalue_from_long(typ, i64::try_from(int).expect("i64::try_from"))
++        }
++        else {
++            // NOTE: set the sign in high.
++            self.from_low_high(typ, int, -(int.is_negative() as i64))
++        }
++    }
++
++    pub fn gcc_uint(&self, typ: Type<'gcc>, int: u64) -> RValue<'gcc> {
++        if self.is_native_int_type_or_bool(typ) {
++            self.context.new_rvalue_from_long(typ, u64::try_from(int).expect("u64::try_from") as i64)
++        }
++        else {
++            self.from_low_high(typ, int as i64, 0)
++        }
++    }
++
++    pub fn gcc_uint_big(&self, typ: Type<'gcc>, num: u128) -> RValue<'gcc> {
++        let low = num as u64;
++        let high = (num >> 64) as u64;
++        if num >> 64 != 0 {
++            // FIXME(antoyo): use a new function new_rvalue_from_unsigned_long()?
++            if self.is_native_int_type(typ) {
++                let low = self.context.new_rvalue_from_long(self.u64_type, low as i64);
++                let high = self.context.new_rvalue_from_long(typ, high as i64);
++
++                let sixty_four = self.context.new_rvalue_from_long(typ, 64);
++                let shift = high << sixty_four;
++                shift | self.context.new_cast(None, low, typ)
++            }
++            else {
++                self.from_low_high(typ, low as i64, high as i64)
++            }
++        }
++        else if typ.is_i128(self) {
++            let num = self.context.new_rvalue_from_long(self.u64_type, num as u64 as i64);
++            self.gcc_int_cast(num, typ)
++        }
++        else {
++            self.gcc_uint(typ, num as u64)
++        }
++    }
++
++    pub fn gcc_zero(&self, typ: Type<'gcc>) -> RValue<'gcc> {
++        if self.is_native_int_type_or_bool(typ) {
++            self.context.new_rvalue_zero(typ)
++        }
++        else {
++            self.from_low_high(typ, 0, 0)
++        }
++    }
++
++    pub fn gcc_int_width(&self, typ: Type<'gcc>) -> u64 {
++        if self.is_native_int_type_or_bool(typ) {
++            typ.get_size() as u64 * 8
++        }
++        else {
++            // NOTE: the only unsupported types are u128 and i128.
++            128
++        }
++    }
++
++    fn bitwise_operation(&self, operation: BinaryOp, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
++        let a_type = a.get_type();
++        let b_type = b.get_type();
++        let a_native = self.is_native_int_type_or_bool(a_type);
++        let b_native = self.is_native_int_type_or_bool(b_type);
++        if a_native && b_native {
++            if a_type != b_type {
++                b = self.context.new_cast(None, b, a_type);
++            }
++            self.context.new_binary_op(None, operation, a_type, a, b)
++        }
++        else {
++            assert!(!a_native && !b_native, "both types should either be native or non-native for or operation");
++            let native_int_type = a_type.dyncast_array().expect("get element type");
++            let values = [
++                self.context.new_binary_op(None, operation, native_int_type, self.low(a), self.low(b)),
++                self.context.new_binary_op(None, operation, native_int_type, self.high(a), self.high(b)),
++            ];
++            self.context.new_array_constructor(None, a_type, &values)
++        }
++    }
++
++    pub fn gcc_or(&self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
++        self.bitwise_operation(BinaryOp::BitwiseOr, a, b)
++    }
++
++    // TODO(antoyo): can we use https://github.com/rust-lang/compiler-builtins/blob/master/src/int/mod.rs#L379 instead?
++    pub fn gcc_int_cast(&self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
++        let value_type = value.get_type();
++        if self.is_native_int_type_or_bool(dest_typ) && self.is_native_int_type_or_bool(value_type) {
++            self.context.new_cast(None, value, dest_typ)
++        }
++        else if self.is_native_int_type_or_bool(dest_typ) {
++            self.context.new_cast(None, self.low(value), dest_typ)
++        }
++        else if self.is_native_int_type_or_bool(value_type) {
++            let dest_element_type = dest_typ.dyncast_array().expect("get element type");
++
++            // NOTE: set the sign of the value.
++            let zero = self.context.new_rvalue_zero(value_type);
++            let is_negative = self.context.new_comparison(None, ComparisonOp::LessThan, value, zero);
++            let is_negative = self.gcc_int_cast(is_negative, dest_element_type);
++            let values = [
++                self.context.new_cast(None, value, dest_element_type),
++                self.context.new_unary_op(None, UnaryOp::Minus, dest_element_type, is_negative),
++            ];
++            self.context.new_array_constructor(None, dest_typ, &values)
++        }
++        else {
++            // Since u128 and i128 are the only types that can be unsupported, we know the type of
++            // value and the destination type have the same size, so a bitcast is fine.
++            self.context.new_bitcast(None, value, dest_typ)
++        }
++    }
++
++    fn int_to_float_cast(&self, signed: bool, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
++        let value_type = value.get_type();
++        if self.is_native_int_type_or_bool(value_type) {
++            return self.context.new_cast(None, value, dest_typ);
++        }
++
++        let name_suffix =
++            match self.type_kind(dest_typ) {
++                TypeKind::Float => "tisf",
++                TypeKind::Double => "tidf",
++                kind => panic!("cannot cast a non-native integer to type {:?}", kind),
++            };
++        let sign =
++            if signed {
++                ""
++            }
++            else {
++                "un"
++            };
++        let func_name = format!("__float{}{}", sign, name_suffix);
++        let param = self.context.new_parameter(None, value_type, "n");
++        let func = self.context.new_function(None, FunctionType::Extern, dest_typ, &[param], func_name, false);
++        self.context.new_call(None, func, &[value])
++    }
++
++    pub fn gcc_int_to_float_cast(&self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
++        self.int_to_float_cast(true, value, dest_typ)
++    }
++
++    pub fn gcc_uint_to_float_cast(&self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
++        self.int_to_float_cast(false, value, dest_typ)
++    }
++
++    fn float_to_int_cast(&self, signed: bool, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
++        let value_type = value.get_type();
++        if self.is_native_int_type_or_bool(dest_typ) {
++            return self.context.new_cast(None, value, dest_typ);
++        }
++
++        let name_suffix =
++            match self.type_kind(value_type) {
++                TypeKind::Float => "sfti",
++                TypeKind::Double => "dfti",
++                kind => panic!("cannot cast a {:?} to non-native integer", kind),
++            };
++        let sign =
++            if signed {
++                ""
++            }
++            else {
++                "uns"
++            };
++        let func_name = format!("__fix{}{}", sign, name_suffix);
++        let param = self.context.new_parameter(None, value_type, "n");
++        let func = self.context.new_function(None, FunctionType::Extern, dest_typ, &[param], func_name, false);
++        self.context.new_call(None, func, &[value])
++    }
++
++    pub fn gcc_float_to_int_cast(&self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
++        self.float_to_int_cast(true, value, dest_typ)
++    }
++
++    pub fn gcc_float_to_uint_cast(&self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
++        self.float_to_int_cast(false, value, dest_typ)
++    }
++
++    fn high(&self, value: RValue<'gcc>) -> RValue<'gcc> {
++        self.context.new_array_access(None, value, self.context.new_rvalue_from_int(self.int_type, 1))
++            .to_rvalue()
++    }
++
++    fn low(&self, value: RValue<'gcc>) -> RValue<'gcc> {
++        self.context.new_array_access(None, value, self.context.new_rvalue_from_int(self.int_type, 0))
++            .to_rvalue()
++    }
++
++    fn from_low_high(&self, typ: Type<'gcc>, low: i64, high: i64) -> RValue<'gcc> {
++        let native_int_type = typ.dyncast_array().expect("get element type");
++        let values = [
++            self.context.new_rvalue_from_long(native_int_type, low),
++            self.context.new_rvalue_from_long(native_int_type, high),
++        ];
++        self.context.new_array_constructor(None, typ, &values)
++    }
++}
index ef213f5636907811ad4c0354e1e99153f5dfe287,0000000000000000000000000000000000000000..08e584a46f33b16dfebc212fe5c3c8ab02fec3fd
mode 100644,000000..100644
--- /dev/null
@@@ -1,1078 -1,0 +1,1104 @@@
- use gccjit::{ComparisonOp, Function, RValue, ToRValue, Type, UnaryOp};
 +pub mod llvm;
 +mod simd;
 +
-                                     let zero = self.cx.context.new_rvalue_zero(arg.get_type());
-                                     let cond = self.cx.context.new_comparison(None, ComparisonOp::Equals, arg, zero);
++use gccjit::{ComparisonOp, Function, RValue, ToRValue, Type, UnaryOp, FunctionType};
 +use rustc_codegen_ssa::MemFlags;
 +use rustc_codegen_ssa::base::wants_msvc_seh;
 +use rustc_codegen_ssa::common::{IntPredicate, span_invalid_monomorphization_error};
 +use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
 +use rustc_codegen_ssa::mir::place::PlaceRef;
 +use rustc_codegen_ssa::traits::{ArgAbiMethods, BaseTypeMethods, BuilderMethods, ConstMethods, IntrinsicCallMethods};
 +use rustc_middle::bug;
 +use rustc_middle::ty::{self, Instance, Ty};
 +use rustc_middle::ty::layout::LayoutOf;
 +use rustc_span::{Span, Symbol, symbol::kw, sym};
 +use rustc_target::abi::HasDataLayout;
 +use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
 +use rustc_target::spec::PanicStrategy;
 +
 +use crate::abi::GccType;
 +use crate::builder::Builder;
 +use crate::common::{SignType, TypeReflection};
 +use crate::context::CodegenCx;
 +use crate::type_of::LayoutGccExt;
 +use crate::intrinsic::simd::generic_simd_intrinsic;
 +
 +fn get_simple_intrinsic<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, name: Symbol) -> Option<Function<'gcc>> {
 +    let gcc_name = match name {
 +        sym::sqrtf32 => "sqrtf",
 +        sym::sqrtf64 => "sqrt",
 +        sym::powif32 => "__builtin_powif",
 +        sym::powif64 => "__builtin_powi",
 +        sym::sinf32 => "sinf",
 +        sym::sinf64 => "sin",
 +        sym::cosf32 => "cosf",
 +        sym::cosf64 => "cos",
 +        sym::powf32 => "powf",
 +        sym::powf64 => "pow",
 +        sym::expf32 => "expf",
 +        sym::expf64 => "exp",
 +        sym::exp2f32 => "exp2f",
 +        sym::exp2f64 => "exp2",
 +        sym::logf32 => "logf",
 +        sym::logf64 => "log",
 +        sym::log10f32 => "log10f",
 +        sym::log10f64 => "log10",
 +        sym::log2f32 => "log2f",
 +        sym::log2f64 => "log2",
 +        sym::fmaf32 => "fmaf",
 +        sym::fmaf64 => "fma",
 +        sym::fabsf32 => "fabsf",
 +        sym::fabsf64 => "fabs",
 +        sym::minnumf32 => "fminf",
 +        sym::minnumf64 => "fmin",
 +        sym::maxnumf32 => "fmaxf",
 +        sym::maxnumf64 => "fmax",
 +        sym::copysignf32 => "copysignf",
 +        sym::copysignf64 => "copysign",
 +        sym::floorf32 => "floorf",
 +        sym::floorf64 => "floor",
 +        sym::ceilf32 => "ceilf",
 +        sym::ceilf64 => "ceil",
 +        sym::truncf32 => "truncf",
 +        sym::truncf64 => "trunc",
 +        sym::rintf32 => "rintf",
 +        sym::rintf64 => "rint",
 +        sym::nearbyintf32 => "nearbyintf",
 +        sym::nearbyintf64 => "nearbyint",
 +        sym::roundf32 => "roundf",
 +        sym::roundf64 => "round",
 +        sym::abort => "abort",
 +        _ => return None,
 +    };
 +    Some(cx.context.get_builtin_function(&gcc_name))
 +}
 +
 +impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
 +    fn codegen_intrinsic_call(&mut self, instance: Instance<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, args: &[OperandRef<'tcx, RValue<'gcc>>], llresult: RValue<'gcc>, span: Span) {
 +        let tcx = self.tcx;
 +        let callee_ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
 +
 +        let (def_id, substs) = match *callee_ty.kind() {
 +            ty::FnDef(def_id, substs) => (def_id, substs),
 +            _ => bug!("expected fn item type, found {}", callee_ty),
 +        };
 +
 +        let sig = callee_ty.fn_sig(tcx);
 +        let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), sig);
 +        let arg_tys = sig.inputs();
 +        let ret_ty = sig.output();
 +        let name = tcx.item_name(def_id);
 +        let name_str = name.as_str();
 +
 +        let llret_ty = self.layout_of(ret_ty).gcc_type(self, true);
 +        let result = PlaceRef::new_sized(llresult, fn_abi.ret.layout);
 +
 +        let simple = get_simple_intrinsic(self, name);
 +        let llval =
 +            match name {
 +                _ if simple.is_some() => {
 +                    // FIXME(antoyo): remove this cast when the API supports function.
 +                    let func = unsafe { std::mem::transmute(simple.expect("simple")) };
 +                    self.call(self.type_void(), func, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None)
 +                },
 +                sym::likely => {
 +                    self.expect(args[0].immediate(), true)
 +                }
 +                sym::unlikely => {
 +                    self.expect(args[0].immediate(), false)
 +                }
 +                kw::Try => {
 +                    try_intrinsic(
 +                        self,
 +                        args[0].immediate(),
 +                        args[1].immediate(),
 +                        args[2].immediate(),
 +                        llresult,
 +                    );
 +                    return;
 +                }
 +                sym::breakpoint => {
 +                    unimplemented!();
 +                }
 +                sym::va_copy => {
 +                    unimplemented!();
 +                }
 +                sym::va_arg => {
 +                    unimplemented!();
 +                }
 +
 +                sym::volatile_load | sym::unaligned_volatile_load => {
 +                    let tp_ty = substs.type_at(0);
 +                    let mut ptr = args[0].immediate();
 +                    if let PassMode::Cast(ty) = fn_abi.ret.mode {
 +                        ptr = self.pointercast(ptr, self.type_ptr_to(ty.gcc_type(self)));
 +                    }
 +                    let load = self.volatile_load(ptr.get_type(), ptr);
 +                    // TODO(antoyo): set alignment.
 +                    self.to_immediate(load, self.layout_of(tp_ty))
 +                }
 +                sym::volatile_store => {
 +                    let dst = args[0].deref(self.cx());
 +                    args[1].val.volatile_store(self, dst);
 +                    return;
 +                }
 +                sym::unaligned_volatile_store => {
 +                    let dst = args[0].deref(self.cx());
 +                    args[1].val.unaligned_volatile_store(self, dst);
 +                    return;
 +                }
 +                sym::prefetch_read_data
 +                    | sym::prefetch_write_data
 +                    | sym::prefetch_read_instruction
 +                    | sym::prefetch_write_instruction => {
 +                        unimplemented!();
 +                    }
 +                sym::ctlz
 +                    | sym::ctlz_nonzero
 +                    | sym::cttz
 +                    | sym::cttz_nonzero
 +                    | sym::ctpop
 +                    | sym::bswap
 +                    | sym::bitreverse
 +                    | sym::rotate_left
 +                    | sym::rotate_right
 +                    | sym::saturating_add
 +                    | sym::saturating_sub => {
 +                        let ty = arg_tys[0];
 +                        match int_type_width_signed(ty, self) {
 +                            Some((width, signed)) => match name {
 +                                sym::ctlz | sym::cttz => {
 +                                    let func = self.current_func.borrow().expect("func");
 +                                    let then_block = func.new_block("then");
 +                                    let else_block = func.new_block("else");
 +                                    let after_block = func.new_block("after");
 +
 +                                    let arg = args[0].immediate();
 +                                    let result = func.new_local(None, arg.get_type(), "zeros");
-                                     let zero_result = self.cx.context.new_rvalue_from_long(arg.get_type(), width as i64);
++                                    let zero = self.cx.gcc_zero(arg.get_type());
++                                    let cond = self.gcc_icmp(IntPredicate::IntEQ, arg, zero);
 +                                    self.llbb().end_with_conditional(None, cond, then_block, else_block);
 +
-                                     // count_leading_zeroes() does not expect, the current blocks
++                                    let zero_result = self.cx.gcc_uint(arg.get_type(), width);
 +                                    then_block.add_assignment(None, result, zero_result);
 +                                    then_block.end_with_jump(None, after_block);
 +
 +                                    // NOTE: since jumps were added in a place
-                                     *self.current_block.borrow_mut() = Some(else_block);
-                                     self.block = Some(else_block);
++                                    // count_leading_zeroes() does not expect, the current block
 +                                    // in the state need to be updated.
-                                     else_block.add_assignment(None, result, zeros);
-                                     else_block.end_with_jump(None, after_block);
++                                    self.switch_to_block(else_block);
 +
 +                                    let zeros =
 +                                        match name {
 +                                            sym::ctlz => self.count_leading_zeroes(width, arg),
 +                                            sym::cttz => self.count_trailing_zeroes(width, arg),
 +                                            _ => unreachable!(),
 +                                        };
-                                     // expect, the current blocks in the state need to be updated.
-                                     *self.current_block.borrow_mut() = Some(after_block);
-                                     self.block = Some(after_block);
++                                    self.llbb().add_assignment(None, result, zeros);
++                                    self.llbb().end_with_jump(None, after_block);
 +
 +                                    // NOTE: since jumps were added in a place rustc does not
-                                         // TODO(antoyo): check if it's faster to use string literals and a
-                                         // match instead of format!.
-                                         let bswap = self.cx.context.get_builtin_function(&format!("__builtin_bswap{}", width));
-                                         let mut arg = args[0].immediate();
-                                         // FIXME(antoyo): this cast should not be necessary. Remove
-                                         // when having proper sized integer types.
-                                         let param_type = bswap.get_param(0).to_rvalue().get_type();
-                                         if param_type != arg.get_type() {
-                                             arg = self.bitcast(arg, param_type);
-                                         }
-                                         self.cx.context.new_call(None, bswap, &[arg])
++                                    // expect, the current block in the state need to be updated.
++                                    self.switch_to_block(after_block);
 +
 +                                    result.to_rvalue()
 +                                }
 +                                sym::ctlz_nonzero => {
 +                                    self.count_leading_zeroes(width, args[0].immediate())
 +                                },
 +                                sym::cttz_nonzero => {
 +                                    self.count_trailing_zeroes(width, args[0].immediate())
 +                                }
 +                                sym::ctpop => self.pop_count(args[0].immediate()),
 +                                sym::bswap => {
 +                                    if width == 8 {
 +                                        args[0].immediate() // byte swap a u8/i8 is just a no-op
 +                                    }
 +                                    else {
-             PassMode::Ignore => {}
++                                        self.gcc_bswap(args[0].immediate(), width)
 +                                    }
 +                                },
 +                                sym::bitreverse => self.bit_reverse(width, args[0].immediate()),
 +                                sym::rotate_left | sym::rotate_right => {
 +                                    // TODO(antoyo): implement using algorithm from:
 +                                    // https://blog.regehr.org/archives/1063
 +                                    // for other platforms.
 +                                    let is_left = name == sym::rotate_left;
 +                                    let val = args[0].immediate();
 +                                    let raw_shift = args[1].immediate();
 +                                    if is_left {
 +                                        self.rotate_left(val, raw_shift, width)
 +                                    }
 +                                    else {
 +                                        self.rotate_right(val, raw_shift, width)
 +                                    }
 +                                },
 +                                sym::saturating_add => {
 +                                    self.saturating_add(args[0].immediate(), args[1].immediate(), signed, width)
 +                                },
 +                                sym::saturating_sub => {
 +                                    self.saturating_sub(args[0].immediate(), args[1].immediate(), signed, width)
 +                                },
 +                                _ => bug!(),
 +                            },
 +                            None => {
 +                                span_invalid_monomorphization_error(
 +                                    tcx.sess,
 +                                    span,
 +                                    &format!(
 +                                        "invalid monomorphization of `{}` intrinsic: \
 +                                      expected basic integer type, found `{}`",
 +                                      name, ty
 +                                    ),
 +                                );
 +                                return;
 +                            }
 +                        }
 +                    }
 +
 +                sym::raw_eq => {
 +                    use rustc_target::abi::Abi::*;
 +                    let tp_ty = substs.type_at(0);
 +                    let layout = self.layout_of(tp_ty).layout;
 +                    let _use_integer_compare = match layout.abi() {
 +                        Scalar(_) | ScalarPair(_, _) => true,
 +                        Uninhabited | Vector { .. } => false,
 +                        Aggregate { .. } => {
 +                            // For rusty ABIs, small aggregates are actually passed
 +                            // as `RegKind::Integer` (see `FnAbi::adjust_for_abi`),
 +                            // so we re-use that same threshold here.
 +                            layout.size() <= self.data_layout().pointer_size * 2
 +                        }
 +                    };
 +
 +                    let a = args[0].immediate();
 +                    let b = args[1].immediate();
 +                    if layout.size().bytes() == 0 {
 +                        self.const_bool(true)
 +                    }
 +                    /*else if use_integer_compare {
 +                        let integer_ty = self.type_ix(layout.size.bits()); // FIXME(antoyo): LLVM creates an integer of 96 bits for [i32; 3], but gcc doesn't support this, so it creates an integer of 128 bits.
 +                        let ptr_ty = self.type_ptr_to(integer_ty);
 +                        let a_ptr = self.bitcast(a, ptr_ty);
 +                        let a_val = self.load(integer_ty, a_ptr, layout.align.abi);
 +                        let b_ptr = self.bitcast(b, ptr_ty);
 +                        let b_val = self.load(integer_ty, b_ptr, layout.align.abi);
 +                        self.icmp(IntPredicate::IntEQ, a_val, b_val)
 +                    }*/
 +                    else {
 +                        let void_ptr_type = self.context.new_type::<*const ()>();
 +                        let a_ptr = self.bitcast(a, void_ptr_type);
 +                        let b_ptr = self.bitcast(b, void_ptr_type);
 +                        let n = self.context.new_cast(None, self.const_usize(layout.size().bytes()), self.sizet_type);
 +                        let builtin = self.context.get_builtin_function("memcmp");
 +                        let cmp = self.context.new_call(None, builtin, &[a_ptr, b_ptr, n]);
 +                        self.icmp(IntPredicate::IntEQ, cmp, self.const_i32(0))
 +                    }
 +                }
 +
 +                sym::black_box => {
 +                    args[0].val.store(self, result);
 +
 +                    let block = self.llbb();
 +                    let extended_asm = block.add_extended_asm(None, "");
 +                    extended_asm.add_input_operand(None, "r", result.llval);
 +                    extended_asm.add_clobber("memory");
 +                    extended_asm.set_volatile_flag(true);
 +
 +                    // We have copied the value to `result` already.
 +                    return;
 +                }
 +
 +                _ if name_str.starts_with("simd_") => {
 +                    match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
 +                        Ok(llval) => llval,
 +                        Err(()) => return,
 +                    }
 +                }
 +
 +                _ => bug!("unknown intrinsic '{}'", name),
 +            };
 +
 +        if !fn_abi.ret.is_ignore() {
 +            if let PassMode::Cast(ty) = fn_abi.ret.mode {
 +                let ptr_llty = self.type_ptr_to(ty.gcc_type(self));
 +                let ptr = self.pointercast(result.llval, ptr_llty);
 +                self.store(llval, ptr, result.align);
 +            }
 +            else {
 +                OperandRef::from_immediate_or_packed_pair(self, llval, result.layout)
 +                    .val
 +                    .store(self, result);
 +            }
 +        }
 +    }
 +
 +    fn abort(&mut self) {
 +        let func = self.context.get_builtin_function("abort");
 +        let func: RValue<'gcc> = unsafe { std::mem::transmute(func) };
 +        self.call(self.type_void(), func, &[], None);
 +    }
 +
 +    fn assume(&mut self, value: Self::Value) {
 +        // TODO(antoyo): switch to asumme when it exists.
 +        // Or use something like this:
 +        // #define __assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)
 +        self.expect(value, true);
 +    }
 +
 +    fn expect(&mut self, cond: Self::Value, _expected: bool) -> Self::Value {
 +        // TODO(antoyo)
 +        cond
 +    }
 +
 +    fn type_test(&mut self, _pointer: Self::Value, _typeid: Self::Value) -> Self::Value {
 +        // Unsupported.
 +        self.context.new_rvalue_from_int(self.int_type, 0)
 +    }
 +
 +    fn va_start(&mut self, _va_list: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn va_end(&mut self, _va_list: RValue<'gcc>) -> RValue<'gcc> {
 +        unimplemented!();
 +    }
 +}
 +
 +impl<'a, 'gcc, 'tcx> ArgAbiMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
 +    fn store_fn_arg(&mut self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, idx: &mut usize, dst: PlaceRef<'tcx, Self::Value>) {
 +        arg_abi.store_fn_arg(self, idx, dst)
 +    }
 +
 +    fn store_arg(&mut self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, val: RValue<'gcc>, dst: PlaceRef<'tcx, RValue<'gcc>>) {
 +        arg_abi.store(self, val, dst)
 +    }
 +
 +    fn arg_memory_ty(&self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
 +        arg_abi.memory_ty(self)
 +    }
 +}
 +
 +pub trait ArgAbiExt<'gcc, 'tcx> {
 +    fn memory_ty(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
 +    fn store(&self, bx: &mut Builder<'_, 'gcc, 'tcx>, val: RValue<'gcc>, dst: PlaceRef<'tcx, RValue<'gcc>>);
 +    fn store_fn_arg(&self, bx: &mut Builder<'_, 'gcc, 'tcx>, idx: &mut usize, dst: PlaceRef<'tcx, RValue<'gcc>>);
 +}
 +
 +impl<'gcc, 'tcx> ArgAbiExt<'gcc, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
 +    /// Gets the LLVM type for a place of the original Rust type of
 +    /// this argument/return, i.e., the result of `type_of::type_of`.
 +    fn memory_ty(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
 +        self.layout.gcc_type(cx, true)
 +    }
 +
 +    /// Stores a direct/indirect value described by this ArgAbi into a
 +    /// place for the original Rust type of this argument/return.
 +    /// Can be used for both storing formal arguments into Rust variables
 +    /// or results of call/invoke instructions into their destinations.
 +    fn store(&self, bx: &mut Builder<'_, 'gcc, 'tcx>, val: RValue<'gcc>, dst: PlaceRef<'tcx, RValue<'gcc>>) {
 +        if self.is_ignore() {
 +            return;
 +        }
 +        if self.is_sized_indirect() {
 +            OperandValue::Ref(val, None, self.layout.align.abi).store(bx, dst)
 +        }
 +        else if self.is_unsized_indirect() {
 +            bug!("unsized `ArgAbi` must be handled through `store_fn_arg`");
 +        }
 +        else if let PassMode::Cast(cast) = self.mode {
 +            // FIXME(eddyb): Figure out when the simpler Store is safe, clang
 +            // uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
 +            let can_store_through_cast_ptr = false;
 +            if can_store_through_cast_ptr {
 +                let cast_ptr_llty = bx.type_ptr_to(cast.gcc_type(bx));
 +                let cast_dst = bx.pointercast(dst.llval, cast_ptr_llty);
 +                bx.store(val, cast_dst, self.layout.align.abi);
 +            }
 +            else {
 +                // The actual return type is a struct, but the ABI
 +                // adaptation code has cast it into some scalar type.  The
 +                // code that follows is the only reliable way I have
 +                // found to do a transform like i64 -> {i32,i32}.
 +                // Basically we dump the data onto the stack then memcpy it.
 +                //
 +                // Other approaches I tried:
 +                // - Casting rust ret pointer to the foreign type and using Store
 +                //   is (a) unsafe if size of foreign type > size of rust type and
 +                //   (b) runs afoul of strict aliasing rules, yielding invalid
 +                //   assembly under -O (specifically, the store gets removed).
 +                // - Truncating foreign type to correct integral type and then
 +                //   bitcasting to the struct type yields invalid cast errors.
 +
 +                // We instead thus allocate some scratch space...
 +                let scratch_size = cast.size(bx);
 +                let scratch_align = cast.align(bx);
 +                let llscratch = bx.alloca(cast.gcc_type(bx), scratch_align);
 +                bx.lifetime_start(llscratch, scratch_size);
 +
 +                // ... where we first store the value...
 +                bx.store(val, llscratch, scratch_align);
 +
 +                // ... and then memcpy it to the intended destination.
 +                bx.memcpy(
 +                    dst.llval,
 +                    self.layout.align.abi,
 +                    llscratch,
 +                    scratch_align,
 +                    bx.const_usize(self.layout.size.bytes()),
 +                    MemFlags::empty(),
 +                );
 +
 +                bx.lifetime_end(llscratch, scratch_size);
 +            }
 +        }
 +        else {
 +            OperandValue::Immediate(val).store(bx, dst);
 +        }
 +    }
 +
 +    fn store_fn_arg<'a>(&self, bx: &mut Builder<'a, 'gcc, 'tcx>, idx: &mut usize, dst: PlaceRef<'tcx, RValue<'gcc>>) {
 +        let mut next = || {
 +            let val = bx.current_func().get_param(*idx as i32);
 +            *idx += 1;
 +            val.to_rvalue()
 +        };
 +        match self.mode {
-             }
++            PassMode::Ignore => {},
 +            PassMode::Pair(..) => {
 +                OperandValue::Pair(next(), next()).store(bx, dst);
-             }
++            },
 +            PassMode::Indirect { extra_attrs: Some(_), .. } => {
 +                OperandValue::Ref(next(), Some(next()), self.layout.align.abi).store(bx, dst);
-                 self.store(bx, next_arg.to_rvalue(), dst);
-             }
++            },
 +            PassMode::Direct(_) | PassMode::Indirect { extra_attrs: None, .. } | PassMode::Cast(_) => {
 +                let next_arg = next();
-                 self.context.new_cast(None, value, typ)
++                self.store(bx, next_arg, dst);
++            },
 +        }
 +    }
 +}
 +
 +fn int_type_width_signed<'gcc, 'tcx>(ty: Ty<'tcx>, cx: &CodegenCx<'gcc, 'tcx>) -> Option<(u64, bool)> {
 +    match ty.kind() {
 +        ty::Int(t) => Some((
 +            match t {
 +                rustc_middle::ty::IntTy::Isize => u64::from(cx.tcx.sess.target.pointer_width),
 +                rustc_middle::ty::IntTy::I8 => 8,
 +                rustc_middle::ty::IntTy::I16 => 16,
 +                rustc_middle::ty::IntTy::I32 => 32,
 +                rustc_middle::ty::IntTy::I64 => 64,
 +                rustc_middle::ty::IntTy::I128 => 128,
 +            },
 +            true,
 +        )),
 +        ty::Uint(t) => Some((
 +            match t {
 +                rustc_middle::ty::UintTy::Usize => u64::from(cx.tcx.sess.target.pointer_width),
 +                rustc_middle::ty::UintTy::U8 => 8,
 +                rustc_middle::ty::UintTy::U16 => 16,
 +                rustc_middle::ty::UintTy::U32 => 32,
 +                rustc_middle::ty::UintTy::U64 => 64,
 +                rustc_middle::ty::UintTy::U128 => 128,
 +            },
 +            false,
 +        )),
 +        _ => None,
 +    }
 +}
 +
 +impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
 +    fn bit_reverse(&mut self, width: u64, value: RValue<'gcc>) -> RValue<'gcc> {
 +        let result_type = value.get_type();
 +        let typ = result_type.to_unsigned(self.cx);
 +
 +        let value =
 +            if result_type.is_signed(self.cx) {
-                     let sixty_four = self.context.new_rvalue_from_long(typ, 64);
-                     let high = self.context.new_cast(None, value >> sixty_four, self.u64_type);
-                     let low = self.context.new_cast(None, value, self.u64_type);
++                self.gcc_int_cast(value, typ)
 +            }
 +            else {
 +                value
 +            };
 +
 +        let context = &self.cx.context;
 +        let result =
 +            match width {
 +                8 => {
 +                    // First step.
 +                    let left = self.and(value, context.new_rvalue_from_int(typ, 0xF0));
 +                    let left = self.lshr(left, context.new_rvalue_from_int(typ, 4));
 +                    let right = self.and(value, context.new_rvalue_from_int(typ, 0x0F));
 +                    let right = self.shl(right, context.new_rvalue_from_int(typ, 4));
 +                    let step1 = self.or(left, right);
 +
 +                    // Second step.
 +                    let left = self.and(step1, context.new_rvalue_from_int(typ, 0xCC));
 +                    let left = self.lshr(left, context.new_rvalue_from_int(typ, 2));
 +                    let right = self.and(step1, context.new_rvalue_from_int(typ, 0x33));
 +                    let right = self.shl(right, context.new_rvalue_from_int(typ, 2));
 +                    let step2 = self.or(left, right);
 +
 +                    // Third step.
 +                    let left = self.and(step2, context.new_rvalue_from_int(typ, 0xAA));
 +                    let left = self.lshr(left, context.new_rvalue_from_int(typ, 1));
 +                    let right = self.and(step2, context.new_rvalue_from_int(typ, 0x55));
 +                    let right = self.shl(right, context.new_rvalue_from_int(typ, 1));
 +                    let step3 = self.or(left, right);
 +
 +                    step3
 +                },
 +                16 => {
 +                    // First step.
 +                    let left = self.and(value, context.new_rvalue_from_int(typ, 0x5555));
 +                    let left = self.shl(left, context.new_rvalue_from_int(typ, 1));
 +                    let right = self.and(value, context.new_rvalue_from_int(typ, 0xAAAA));
 +                    let right = self.lshr(right, context.new_rvalue_from_int(typ, 1));
 +                    let step1 = self.or(left, right);
 +
 +                    // Second step.
 +                    let left = self.and(step1, context.new_rvalue_from_int(typ, 0x3333));
 +                    let left = self.shl(left, context.new_rvalue_from_int(typ, 2));
 +                    let right = self.and(step1, context.new_rvalue_from_int(typ, 0xCCCC));
 +                    let right = self.lshr(right, context.new_rvalue_from_int(typ, 2));
 +                    let step2 = self.or(left, right);
 +
 +                    // Third step.
 +                    let left = self.and(step2, context.new_rvalue_from_int(typ, 0x0F0F));
 +                    let left = self.shl(left, context.new_rvalue_from_int(typ, 4));
 +                    let right = self.and(step2, context.new_rvalue_from_int(typ, 0xF0F0));
 +                    let right = self.lshr(right, context.new_rvalue_from_int(typ, 4));
 +                    let step3 = self.or(left, right);
 +
 +                    // Fourth step.
 +                    let left = self.and(step3, context.new_rvalue_from_int(typ, 0x00FF));
 +                    let left = self.shl(left, context.new_rvalue_from_int(typ, 8));
 +                    let right = self.and(step3, context.new_rvalue_from_int(typ, 0xFF00));
 +                    let right = self.lshr(right, context.new_rvalue_from_int(typ, 8));
 +                    let step4 = self.or(left, right);
 +
 +                    step4
 +                },
 +                32 => {
 +                    // TODO(antoyo): Refactor with other implementations.
 +                    // First step.
 +                    let left = self.and(value, context.new_rvalue_from_long(typ, 0x55555555));
 +                    let left = self.shl(left, context.new_rvalue_from_long(typ, 1));
 +                    let right = self.and(value, context.new_rvalue_from_long(typ, 0xAAAAAAAA));
 +                    let right = self.lshr(right, context.new_rvalue_from_long(typ, 1));
 +                    let step1 = self.or(left, right);
 +
 +                    // Second step.
 +                    let left = self.and(step1, context.new_rvalue_from_long(typ, 0x33333333));
 +                    let left = self.shl(left, context.new_rvalue_from_long(typ, 2));
 +                    let right = self.and(step1, context.new_rvalue_from_long(typ, 0xCCCCCCCC));
 +                    let right = self.lshr(right, context.new_rvalue_from_long(typ, 2));
 +                    let step2 = self.or(left, right);
 +
 +                    // Third step.
 +                    let left = self.and(step2, context.new_rvalue_from_long(typ, 0x0F0F0F0F));
 +                    let left = self.shl(left, context.new_rvalue_from_long(typ, 4));
 +                    let right = self.and(step2, context.new_rvalue_from_long(typ, 0xF0F0F0F0));
 +                    let right = self.lshr(right, context.new_rvalue_from_long(typ, 4));
 +                    let step3 = self.or(left, right);
 +
 +                    // Fourth step.
 +                    let left = self.and(step3, context.new_rvalue_from_long(typ, 0x00FF00FF));
 +                    let left = self.shl(left, context.new_rvalue_from_long(typ, 8));
 +                    let right = self.and(step3, context.new_rvalue_from_long(typ, 0xFF00FF00));
 +                    let right = self.lshr(right, context.new_rvalue_from_long(typ, 8));
 +                    let step4 = self.or(left, right);
 +
 +                    // Fifth step.
 +                    let left = self.and(step4, context.new_rvalue_from_long(typ, 0x0000FFFF));
 +                    let left = self.shl(left, context.new_rvalue_from_long(typ, 16));
 +                    let right = self.and(step4, context.new_rvalue_from_long(typ, 0xFFFF0000));
 +                    let right = self.lshr(right, context.new_rvalue_from_long(typ, 16));
 +                    let step5 = self.or(left, right);
 +
 +                    step5
 +                },
 +                64 => {
 +                    // First step.
 +                    let left = self.shl(value, context.new_rvalue_from_long(typ, 32));
 +                    let right = self.lshr(value, context.new_rvalue_from_long(typ, 32));
 +                    let step1 = self.or(left, right);
 +
 +                    // Second step.
 +                    let left = self.and(step1, context.new_rvalue_from_long(typ, 0x0001FFFF0001FFFF));
 +                    let left = self.shl(left, context.new_rvalue_from_long(typ, 15));
 +                    let right = self.and(step1, context.new_rvalue_from_long(typ, 0xFFFE0000FFFE0000u64 as i64)); // TODO(antoyo): transmute the number instead?
 +                    let right = self.lshr(right, context.new_rvalue_from_long(typ, 17));
 +                    let step2 = self.or(left, right);
 +
 +                    // Third step.
 +                    let left = self.lshr(step2, context.new_rvalue_from_long(typ, 10));
 +                    let left = self.xor(step2, left);
 +                    let temp = self.and(left, context.new_rvalue_from_long(typ, 0x003F801F003F801F));
 +
 +                    let left = self.shl(temp, context.new_rvalue_from_long(typ, 10));
 +                    let left = self.or(temp, left);
 +                    let step3 = self.xor(left, step2);
 +
 +                    // Fourth step.
 +                    let left = self.lshr(step3, context.new_rvalue_from_long(typ, 4));
 +                    let left = self.xor(step3, left);
 +                    let temp = self.and(left, context.new_rvalue_from_long(typ, 0x0E0384210E038421));
 +
 +                    let left = self.shl(temp, context.new_rvalue_from_long(typ, 4));
 +                    let left = self.or(temp, left);
 +                    let step4 = self.xor(left, step3);
 +
 +                    // Fifth step.
 +                    let left = self.lshr(step4, context.new_rvalue_from_long(typ, 2));
 +                    let left = self.xor(step4, left);
 +                    let temp = self.and(left, context.new_rvalue_from_long(typ, 0x2248884222488842));
 +
 +                    let left = self.shl(temp, context.new_rvalue_from_long(typ, 2));
 +                    let left = self.or(temp, left);
 +                    let step5 = self.xor(left, step4);
 +
 +                    step5
 +                },
 +                128 => {
 +                    // TODO(antoyo): find a more efficient implementation?
-                     let new_low = self.context.new_cast(None, reversed_high, typ);
-                     let new_high = self.context.new_cast(None, reversed_low, typ) << sixty_four;
++                    let sixty_four = self.gcc_int(typ, 64);
++                    let right_shift = self.gcc_lshr(value, sixty_four);
++                    let high = self.gcc_int_cast(right_shift, self.u64_type);
++                    let low = self.gcc_int_cast(value, self.u64_type);
 +
 +                    let reversed_high = self.bit_reverse(64, high);
 +                    let reversed_low = self.bit_reverse(64, low);
 +
-                     new_low | new_high
++                    let new_low = self.gcc_int_cast(reversed_high, typ);
++                    let new_high = self.shl(self.gcc_int_cast(reversed_low, typ), sixty_four);
 +
-         self.context.new_cast(None, result, result_type)
++                    self.gcc_or(new_low, new_high)
 +                },
 +                _ => {
 +                    panic!("cannot bit reverse with width = {}", width);
 +                },
 +            };
 +
-     fn count_leading_zeroes(&self, width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
++        self.gcc_int_cast(result, result_type)
 +    }
 +
-                 let sixty_four = self.context.new_rvalue_from_long(arg_type, 64);
-                 let high = self.context.new_cast(None, arg >> sixty_four, self.u64_type);
-                 let low = self.context.new_cast(None, arg, self.u64_type);
++    fn count_leading_zeroes(&mut self, width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): use width?
 +        let arg_type = arg.get_type();
 +        let count_leading_zeroes =
++            // TODO(antoyo): write a new function Type::is_compatible_with(&Type) and use it here
++            // instead of using is_uint().
 +            if arg_type.is_uint(&self.cx) {
 +                "__builtin_clz"
 +            }
 +            else if arg_type.is_ulong(&self.cx) {
 +                "__builtin_clzl"
 +            }
 +            else if arg_type.is_ulonglong(&self.cx) {
 +                "__builtin_clzll"
 +            }
 +            else if width == 128 {
 +                // Algorithm from: https://stackoverflow.com/a/28433850/389119
 +                let array_type = self.context.new_array_type(None, arg_type, 3);
 +                let result = self.current_func()
 +                    .new_local(None, array_type, "count_loading_zeroes_results");
 +
-                 let first_value = self.context.new_cast(None, self.context.new_call(None, clzll, &[high]), arg_type);
++                let sixty_four = self.const_uint(arg_type, 64);
++                let shift = self.lshr(arg, sixty_four);
++                let high = self.gcc_int_cast(shift, self.u64_type);
++                let low = self.gcc_int_cast(arg, self.u64_type);
 +
 +                let zero = self.context.new_rvalue_zero(self.usize_type);
 +                let one = self.context.new_rvalue_one(self.usize_type);
 +                let two = self.context.new_rvalue_from_long(self.usize_type, 2);
 +
 +                let clzll = self.context.get_builtin_function("__builtin_clzll");
 +
 +                let first_elem = self.context.new_array_access(None, result, zero);
-                 let second_value = self.context.new_cast(None, self.context.new_call(None, clzll, &[low]), arg_type) + sixty_four;
++                let first_value = self.gcc_int_cast(self.context.new_call(None, clzll, &[high]), arg_type);
 +                self.llbb()
 +                    .add_assignment(None, first_elem, first_value);
 +
 +                let second_elem = self.context.new_array_access(None, result, one);
-                 let third_value = self.context.new_rvalue_from_long(arg_type, 128);
++                let cast = self.gcc_int_cast(self.context.new_call(None, clzll, &[low]), arg_type);
++                let second_value = self.add(cast, sixty_four);
 +                self.llbb()
 +                    .add_assignment(None, second_elem, second_value);
 +
 +                let third_elem = self.context.new_array_access(None, result, two);
-                 return self.context.new_cast(None, res, arg_type);
++                let third_value = self.const_uint(arg_type, 128);
 +                self.llbb()
 +                    .add_assignment(None, third_elem, third_value);
 +
 +                let not_high = self.context.new_unary_op(None, UnaryOp::LogicalNegate, self.u64_type, high);
 +                let not_low = self.context.new_unary_op(None, UnaryOp::LogicalNegate, self.u64_type, low);
 +                let not_low_and_not_high = not_low & not_high;
 +                let index = not_high + not_low_and_not_high;
 +                // NOTE: the following cast is necessary to avoid a GIMPLE verification failure in
 +                // gcc.
 +                // TODO(antoyo): do the correct verification in libgccjit to avoid an error at the
 +                // compilation stage.
 +                let index = self.context.new_cast(None, index, self.i32_type);
 +
 +                let res = self.context.new_array_access(None, result, index);
 +
-                 let count_leading_zeroes = self.context.get_builtin_function("__builtin_clz");
-                 let arg = self.context.new_cast(None, arg, self.uint_type);
-                 let diff = self.int_width(self.uint_type) - self.int_width(arg_type);
-                 let diff = self.context.new_rvalue_from_long(self.int_type, diff);
++                return self.gcc_int_cast(res.to_rvalue(), arg_type);
 +            }
 +            else {
-     fn count_trailing_zeroes(&self, _width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
++                let count_leading_zeroes = self.context.get_builtin_function("__builtin_clzll");
++                let arg = self.context.new_cast(None, arg, self.ulonglong_type);
++                let diff = self.ulonglong_type.get_size() as i64 - arg_type.get_size() as i64;
++                let diff = self.context.new_rvalue_from_long(self.int_type, diff * 8);
 +                let res = self.context.new_call(None, count_leading_zeroes, &[arg]) - diff;
 +                return self.context.new_cast(None, res, arg_type);
 +            };
 +        let count_leading_zeroes = self.context.get_builtin_function(count_leading_zeroes);
 +        let res = self.context.new_call(None, count_leading_zeroes, &[arg]);
 +        self.context.new_cast(None, res, arg_type)
 +    }
 +
-                 self.context.new_cast(None, arg, new_type)
++    fn count_trailing_zeroes(&mut self, _width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
 +        let result_type = arg.get_type();
 +        let arg =
 +            if result_type.is_signed(self.cx) {
 +                let new_type = result_type.to_unsigned(self.cx);
-                 let sixty_four = self.context.new_rvalue_from_long(arg_type, 64);
-                 let high = self.context.new_cast(None, arg >> sixty_four, self.u64_type);
-                 let low = self.context.new_cast(None, arg, self.u64_type);
++                self.gcc_int_cast(arg, new_type)
 +            }
 +            else {
 +                arg
 +            };
 +        let arg_type = arg.get_type();
 +        let (count_trailing_zeroes, expected_type) =
++            // TODO(antoyo): write a new function Type::is_compatible_with(&Type) and use it here
++            // instead of using is_uint().
 +            if arg_type.is_uchar(&self.cx) || arg_type.is_ushort(&self.cx) || arg_type.is_uint(&self.cx) {
 +                // NOTE: we don't need to & 0xFF for uchar because the result is undefined on zero.
 +                ("__builtin_ctz", self.cx.uint_type)
 +            }
 +            else if arg_type.is_ulong(&self.cx) {
 +                ("__builtin_ctzl", self.cx.ulong_type)
 +            }
 +            else if arg_type.is_ulonglong(&self.cx) {
 +                ("__builtin_ctzll", self.cx.ulonglong_type)
 +            }
 +            else if arg_type.is_u128(&self.cx) {
 +                // Adapted from the algorithm to count leading zeroes from: https://stackoverflow.com/a/28433850/389119
 +                let array_type = self.context.new_array_type(None, arg_type, 3);
 +                let result = self.current_func()
 +                    .new_local(None, array_type, "count_loading_zeroes_results");
 +
-                 let first_value = self.context.new_cast(None, self.context.new_call(None, ctzll, &[low]), arg_type);
++                let sixty_four = self.gcc_int(arg_type, 64);
++                let shift = self.gcc_lshr(arg, sixty_four);
++                let high = self.gcc_int_cast(shift, self.u64_type);
++                let low = self.gcc_int_cast(arg, self.u64_type);
 +
 +                let zero = self.context.new_rvalue_zero(self.usize_type);
 +                let one = self.context.new_rvalue_one(self.usize_type);
 +                let two = self.context.new_rvalue_from_long(self.usize_type, 2);
 +
 +                let ctzll = self.context.get_builtin_function("__builtin_ctzll");
 +
 +                let first_elem = self.context.new_array_access(None, result, zero);
-                 let second_value = self.context.new_cast(None, self.context.new_call(None, ctzll, &[high]), arg_type) + sixty_four;
++                let first_value = self.gcc_int_cast(self.context.new_call(None, ctzll, &[low]), arg_type);
 +                self.llbb()
 +                    .add_assignment(None, first_elem, first_value);
 +
 +                let second_elem = self.context.new_array_access(None, result, one);
-                 let third_value = self.context.new_rvalue_from_long(arg_type, 128);
++                let second_value = self.gcc_add(self.gcc_int_cast(self.context.new_call(None, ctzll, &[high]), arg_type), sixty_four);
 +                self.llbb()
 +                    .add_assignment(None, second_elem, second_value);
 +
 +                let third_elem = self.context.new_array_access(None, result, two);
-                 return self.context.new_cast(None, res, result_type);
++                let third_value = self.gcc_int(arg_type, 128);
 +                self.llbb()
 +                    .add_assignment(None, third_elem, third_value);
 +
 +                let not_low = self.context.new_unary_op(None, UnaryOp::LogicalNegate, self.u64_type, low);
 +                let not_high = self.context.new_unary_op(None, UnaryOp::LogicalNegate, self.u64_type, high);
 +                let not_low_and_not_high = not_low & not_high;
 +                let index = not_low + not_low_and_not_high;
 +                // NOTE: the following cast is necessary to avoid a GIMPLE verification failure in
 +                // gcc.
 +                // TODO(antoyo): do the correct verification in libgccjit to avoid an error at the
 +                // compilation stage.
 +                let index = self.context.new_cast(None, index, self.i32_type);
 +
 +                let res = self.context.new_array_access(None, result, index);
 +
-                 unimplemented!("count_trailing_zeroes for {:?}", arg_type);
++                return self.gcc_int_cast(res.to_rvalue(), result_type);
 +            }
 +            else {
-     fn int_width(&self, typ: Type<'gcc>) -> i64 {
-         self.cx.int_width(typ) as i64
-     }
-     fn pop_count(&self, value: RValue<'gcc>) -> RValue<'gcc> {
++                let count_trailing_zeroes = self.context.get_builtin_function("__builtin_ctzll");
++                let arg_size = arg_type.get_size();
++                let casted_arg = self.context.new_cast(None, arg, self.ulonglong_type);
++                let byte_diff = self.ulonglong_type.get_size() as i64 - arg_size as i64;
++                let diff = self.context.new_rvalue_from_long(self.int_type, byte_diff * 8);
++                let mask = self.context.new_rvalue_from_long(arg_type, -1); // To get the value with all bits set.
++                let masked = mask & self.context.new_unary_op(None, UnaryOp::BitwiseNegate, arg_type, arg);
++                let cond = self.context.new_comparison(None, ComparisonOp::Equals, masked, mask);
++                let diff = diff * self.context.new_cast(None, cond, self.int_type);
++                let res = self.context.new_call(None, count_trailing_zeroes, &[casted_arg]) - diff;
++                return self.context.new_cast(None, res, result_type);
 +            };
 +        let count_trailing_zeroes = self.context.get_builtin_function(count_trailing_zeroes);
 +        let arg =
 +            if arg_type != expected_type {
 +                self.context.new_cast(None, arg, expected_type)
 +            }
 +            else {
 +                arg
 +            };
 +        let res = self.context.new_call(None, count_trailing_zeroes, &[arg]);
 +        self.context.new_cast(None, res, result_type)
 +    }
 +
-                 self.context.new_cast(None, value, value_type)
++    fn pop_count(&mut self, value: RValue<'gcc>) -> RValue<'gcc> {
 +        // TODO(antoyo): use the optimized version with fewer operations.
 +        let result_type = value.get_type();
 +        let value_type = result_type.to_unsigned(self.cx);
 +
 +        let value =
 +            if result_type.is_signed(self.cx) {
-             let sixty_four = self.context.new_rvalue_from_long(value_type, 64);
-             let high = self.context.new_cast(None, value >> sixty_four, self.cx.ulonglong_type);
++                self.gcc_int_cast(value, value_type)
 +            }
 +            else {
 +                value
 +            };
 +
 +        if value_type.is_u128(&self.cx) {
 +            // TODO(antoyo): implement in the normal algorithm below to have a more efficient
 +            // implementation (that does not require a call to __popcountdi2).
 +            let popcount = self.context.get_builtin_function("__builtin_popcountll");
-             let low = self.context.new_cast(None, value, self.cx.ulonglong_type);
++            let sixty_four = self.gcc_int(value_type, 64);
++            let right_shift = self.gcc_lshr(value, sixty_four);
++            let high = self.gcc_int_cast(right_shift, self.cx.ulonglong_type);
 +            let high = self.context.new_call(None, popcount, &[high]);
-             return self.context.new_cast(None, res, result_type);
++            let low = self.gcc_int_cast(value, self.cx.ulonglong_type);
 +            let low = self.context.new_call(None, popcount, &[low]);
 +            let res = high + low;
-         let max = self.context.new_rvalue_from_long(shift.get_type(), width as i64);
-         let shift = shift % max;
++            return self.gcc_int_cast(res, result_type);
 +        }
 +
 +        // First step.
 +        let mask = self.context.new_rvalue_from_long(value_type, 0x5555555555555555);
 +        let left = value & mask;
 +        let shifted = value >> self.context.new_rvalue_from_int(value_type, 1);
 +        let right = shifted & mask;
 +        let value = left + right;
 +
 +        // Second step.
 +        let mask = self.context.new_rvalue_from_long(value_type, 0x3333333333333333);
 +        let left = value & mask;
 +        let shifted = value >> self.context.new_rvalue_from_int(value_type, 2);
 +        let right = shifted & mask;
 +        let value = left + right;
 +
 +        // Third step.
 +        let mask = self.context.new_rvalue_from_long(value_type, 0x0F0F0F0F0F0F0F0F);
 +        let left = value & mask;
 +        let shifted = value >> self.context.new_rvalue_from_int(value_type, 4);
 +        let right = shifted & mask;
 +        let value = left + right;
 +
 +        if value_type.is_u8(&self.cx) {
 +            return self.context.new_cast(None, value, result_type);
 +        }
 +
 +        // Fourth step.
 +        let mask = self.context.new_rvalue_from_long(value_type, 0x00FF00FF00FF00FF);
 +        let left = value & mask;
 +        let shifted = value >> self.context.new_rvalue_from_int(value_type, 8);
 +        let right = shifted & mask;
 +        let value = left + right;
 +
 +        if value_type.is_u16(&self.cx) {
 +            return self.context.new_cast(None, value, result_type);
 +        }
 +
 +        // Fifth step.
 +        let mask = self.context.new_rvalue_from_long(value_type, 0x0000FFFF0000FFFF);
 +        let left = value & mask;
 +        let shifted = value >> self.context.new_rvalue_from_int(value_type, 16);
 +        let right = shifted & mask;
 +        let value = left + right;
 +
 +        if value_type.is_u32(&self.cx) {
 +            return self.context.new_cast(None, value, result_type);
 +        }
 +
 +        // Sixth step.
 +        let mask = self.context.new_rvalue_from_long(value_type, 0x00000000FFFFFFFF);
 +        let left = value & mask;
 +        let shifted = value >> self.context.new_rvalue_from_int(value_type, 32);
 +        let right = shifted & mask;
 +        let value = left + right;
 +
 +        self.context.new_cast(None, value, result_type)
 +    }
 +
 +    // Algorithm from: https://blog.regehr.org/archives/1063
 +    fn rotate_left(&mut self, value: RValue<'gcc>, shift: RValue<'gcc>, width: u64) -> RValue<'gcc> {
-                 self.context.new_unary_op(None, UnaryOp::Minus, shift.get_type(), shift),
-                 self.context.new_rvalue_from_long(shift.get_type(), width as i64 - 1),
++        let max = self.const_uint(shift.get_type(), width);
++        let shift = self.urem(shift, max);
 +        let lhs = self.shl(value, shift);
++        let result_neg = self.neg(shift);
 +        let result_and =
 +            self.and(
-         let max = self.context.new_rvalue_from_long(shift.get_type(), width as i64);
-         let shift = shift % max;
++                result_neg,
++                self.const_uint(shift.get_type(), width - 1),
 +            );
 +        let rhs = self.lshr(value, result_and);
 +        self.or(lhs, rhs)
 +    }
 +
 +    // Algorithm from: https://blog.regehr.org/archives/1063
 +    fn rotate_right(&mut self, value: RValue<'gcc>, shift: RValue<'gcc>, width: u64) -> RValue<'gcc> {
-                 self.context.new_unary_op(None, UnaryOp::Minus, shift.get_type(), shift),
-                 self.context.new_rvalue_from_long(shift.get_type(), width as i64 - 1),
++        let max = self.const_uint(shift.get_type(), width);
++        let shift = self.urem(shift, max);
 +        let lhs = self.lshr(value, shift);
++        let result_neg = self.neg(shift);
 +        let result_and =
 +            self.and(
-             // expect, the current blocks in the state need to be updated.
-             *self.current_block.borrow_mut() = Some(after_block);
-             self.block = Some(after_block);
++                result_neg,
++                self.const_uint(shift.get_type(), width - 1),
 +            );
 +        let rhs = self.shl(value, result_and);
 +        self.or(lhs, rhs)
 +    }
 +
 +    fn saturating_add(&mut self, lhs: RValue<'gcc>, rhs: RValue<'gcc>, signed: bool, width: u64) -> RValue<'gcc> {
 +        let func = self.current_func.borrow().expect("func");
 +
 +        if signed {
 +            // Algorithm from: https://stackoverflow.com/a/56531252/389119
 +            let after_block = func.new_block("after");
 +            let func_name =
 +                match width {
 +                    8 => "__builtin_add_overflow",
 +                    16 => "__builtin_add_overflow",
 +                    32 => "__builtin_sadd_overflow",
 +                    64 => "__builtin_saddll_overflow",
 +                    128 => "__builtin_add_overflow",
 +                    _ => unreachable!(),
 +                };
 +            let overflow_func = self.context.get_builtin_function(func_name);
 +            let result_type = lhs.get_type();
 +            let res = func.new_local(None, result_type, "saturating_sum");
 +            let overflow = self.overflow_call(overflow_func, &[lhs, rhs, res.get_address(None)], None);
 +
 +            let then_block = func.new_block("then");
 +
 +            let unsigned_type = self.context.new_int_type(width as i32 / 8, false);
 +            let shifted = self.context.new_cast(None, lhs, unsigned_type) >> self.context.new_rvalue_from_int(unsigned_type, width as i32 - 1);
 +            let uint_max = self.context.new_unary_op(None, UnaryOp::BitwiseNegate, unsigned_type,
 +                self.context.new_rvalue_from_int(unsigned_type, 0)
 +            );
 +            let int_max = uint_max >> self.context.new_rvalue_one(unsigned_type);
 +            then_block.add_assignment(None, res, self.context.new_cast(None, shifted + int_max, result_type));
 +            then_block.end_with_jump(None, after_block);
 +
 +            self.llbb().end_with_conditional(None, overflow, then_block, after_block);
 +
 +            // NOTE: since jumps were added in a place rustc does not
-             let func_name =
-                 match width {
-                     8 => "__builtin_sub_overflow",
-                     16 => "__builtin_sub_overflow",
-                     32 => "__builtin_ssub_overflow",
-                     64 => "__builtin_ssubll_overflow",
-                     128 => "__builtin_sub_overflow",
-                     _ => unreachable!(),
-                 };
-             let overflow_func = self.context.get_builtin_function(func_name);
++            // expect, the current block in the state need to be updated.
++            self.switch_to_block(after_block);
 +
 +            res.to_rvalue()
 +        }
 +        else {
 +            // Algorithm from: http://locklessinc.com/articles/sat_arithmetic/
 +            let res = lhs + rhs;
 +            let res_type = res.get_type();
 +            let cond = self.context.new_comparison(None, ComparisonOp::LessThan, res, lhs);
 +            let value = self.context.new_unary_op(None, UnaryOp::Minus, res_type, self.context.new_cast(None, cond, res_type));
 +            res | value
 +        }
 +    }
 +
 +    // Algorithm from: https://locklessinc.com/articles/sat_arithmetic/
 +    fn saturating_sub(&mut self, lhs: RValue<'gcc>, rhs: RValue<'gcc>, signed: bool, width: u64) -> RValue<'gcc> {
 +        if signed {
 +            // Also based on algorithm from: https://stackoverflow.com/a/56531252/389119
-             let overflow = self.overflow_call(overflow_func, &[lhs, rhs, res.get_address(None)], None);
 +            let result_type = lhs.get_type();
 +            let func = self.current_func.borrow().expect("func");
 +            let res = func.new_local(None, result_type, "saturating_diff");
-             let unsigned_type = self.context.new_int_type(width as i32 / 8, false);
-             let shifted = self.context.new_cast(None, lhs, unsigned_type) >> self.context.new_rvalue_from_int(unsigned_type, width as i32 - 1);
-             let uint_max = self.context.new_unary_op(None, UnaryOp::BitwiseNegate, unsigned_type,
-                 self.context.new_rvalue_from_int(unsigned_type, 0)
-             );
-             let int_max = uint_max >> self.context.new_rvalue_one(unsigned_type);
-             then_block.add_assignment(None, res, self.context.new_cast(None, shifted + int_max, result_type));
++            let supports_native_type = self.is_native_int_type(result_type);
++            let overflow =
++                if supports_native_type {
++                    let func_name =
++                        match width {
++                            8 => "__builtin_sub_overflow",
++                            16 => "__builtin_sub_overflow",
++                            32 => "__builtin_ssub_overflow",
++                            64 => "__builtin_ssubll_overflow",
++                            128 => "__builtin_sub_overflow",
++                            _ => unreachable!(),
++                        };
++                    let overflow_func = self.context.get_builtin_function(func_name);
++                    self.overflow_call(overflow_func, &[lhs, rhs, res.get_address(None)], None)
++                }
++                else {
++                    let func_name =
++                        match width {
++                            128 => "__rust_i128_subo",
++                            _ => unreachable!(),
++                        };
++                    let param_a = self.context.new_parameter(None, result_type, "a");
++                    let param_b = self.context.new_parameter(None, result_type, "b");
++                    let result_field = self.context.new_field(None, result_type, "result");
++                    let overflow_field = self.context.new_field(None, self.bool_type, "overflow");
++                    let return_type = self.context.new_struct_type(None, "result_overflow", &[result_field, overflow_field]);
++                    let func = self.context.new_function(None, FunctionType::Extern, return_type.as_type(), &[param_a, param_b], func_name, false);
++                    let result = self.context.new_call(None, func, &[lhs, rhs]);
++                    let overflow = result.access_field(None, overflow_field);
++                    let int_result = result.access_field(None, result_field);
++                    self.llbb().add_assignment(None, res, int_result);
++                    overflow
++                };
 +
 +            let then_block = func.new_block("then");
 +            let after_block = func.new_block("after");
 +
-             // expect, the current blocks in the state need to be updated.
-             *self.current_block.borrow_mut() = Some(after_block);
-             self.block = Some(after_block);
++            // NOTE: convert the type to unsigned to have an unsigned shift.
++            let unsigned_type = result_type.to_unsigned(&self.cx);
++            let shifted = self.gcc_lshr(self.gcc_int_cast(lhs, unsigned_type), self.gcc_int(unsigned_type, width as i64 - 1));
++            let uint_max = self.gcc_not(self.gcc_int(unsigned_type, 0));
++            let int_max = self.gcc_lshr(uint_max, self.gcc_int(unsigned_type, 1));
++            then_block.add_assignment(None, res, self.gcc_int_cast(self.gcc_add(shifted, int_max), result_type));
 +            then_block.end_with_jump(None, after_block);
 +
 +            self.llbb().end_with_conditional(None, overflow, then_block, after_block);
 +
 +            // NOTE: since jumps were added in a place rustc does not
-     if bx.sess().panic_strategy() == PanicStrategy::Abort {
++            // expect, the current block in the state need to be updated.
++            self.switch_to_block(after_block);
 +
 +            res.to_rvalue()
 +        }
 +        else {
 +            let res = lhs - rhs;
 +            let comparison = self.context.new_comparison(None, ComparisonOp::LessThanEquals, res, lhs);
 +            let comparison = self.context.new_cast(None, comparison, lhs.get_type());
 +            let unary_op = self.context.new_unary_op(None, UnaryOp::Minus, comparison.get_type(), comparison);
 +            self.and(res, unary_op)
 +        }
 +    }
 +}
 +
 +fn try_intrinsic<'gcc, 'tcx>(bx: &mut Builder<'_, 'gcc, 'tcx>, try_func: RValue<'gcc>, data: RValue<'gcc>, _catch_func: RValue<'gcc>, dest: RValue<'gcc>) {
++    // NOTE: the `|| true` here is to use the panic=abort strategy with panic=unwind too
++    if bx.sess().panic_strategy() == PanicStrategy::Abort || true {
++        // TODO(bjorn3): Properly implement unwinding and remove the `|| true` once this is done.
 +        bx.call(bx.type_void(), try_func, &[data], None);
 +        // Return 0 unconditionally from the intrinsic call;
 +        // we can never unwind.
 +        let ret_align = bx.tcx.data_layout.i32_align.abi;
 +        bx.store(bx.const_i32(0), dest, ret_align);
 +    }
 +    else if wants_msvc_seh(bx.sess()) {
 +        unimplemented!();
 +    }
 +    else {
 +        unimplemented!();
 +    }
 +}
index aff27f71d91c464612577e5a275c452a65a674d2,0000000000000000000000000000000000000000..7d7811c878219ebc3b9403e3a13b81f56e6b9594
mode 100644,000000..100644
--- /dev/null
@@@ -1,167 -1,0 +1,188 @@@
 +use gccjit::{RValue, Type};
 +use rustc_codegen_ssa::base::compare_simd_types;
 +use rustc_codegen_ssa::common::{TypeKind, span_invalid_monomorphization_error};
 +use rustc_codegen_ssa::mir::operand::OperandRef;
 +use rustc_codegen_ssa::traits::{BaseTypeMethods, BuilderMethods};
 +use rustc_hir as hir;
 +use rustc_middle::span_bug;
 +use rustc_middle::ty::layout::HasTyCtxt;
 +use rustc_middle::ty::{self, Ty};
 +use rustc_span::{Span, Symbol, sym};
 +
 +use crate::builder::Builder;
 +
 +pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, name: Symbol, callee_ty: Ty<'tcx>, args: &[OperandRef<'tcx, RValue<'gcc>>], ret_ty: Ty<'tcx>, llret_ty: Type<'gcc>, span: Span) -> Result<RValue<'gcc>, ()> {
 +    // macros for error handling:
 +    macro_rules! emit_error {
 +        ($msg: tt) => {
 +            emit_error!($msg, )
 +        };
 +        ($msg: tt, $($fmt: tt)*) => {
 +            span_invalid_monomorphization_error(
 +                bx.sess(), span,
 +                &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
 +                         name, $($fmt)*));
 +        }
 +    }
 +
 +    macro_rules! return_error {
 +        ($($fmt: tt)*) => {
 +            {
 +                emit_error!($($fmt)*);
 +                return Err(());
 +            }
 +        }
 +    }
 +
 +    macro_rules! require {
 +        ($cond: expr, $($fmt: tt)*) => {
 +            if !$cond {
 +                return_error!($($fmt)*);
 +            }
 +        };
 +    }
 +
 +    macro_rules! require_simd {
 +        ($ty: expr, $position: expr) => {
 +            require!($ty.is_simd(), "expected SIMD {} type, found non-SIMD `{}`", $position, $ty)
 +        };
 +    }
 +
 +    let tcx = bx.tcx();
 +    let sig =
 +        tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), callee_ty.fn_sig(tcx));
 +    let arg_tys = sig.inputs();
 +    let name_str = name.as_str();
 +
 +    // every intrinsic below takes a SIMD vector as its first argument
 +    require_simd!(arg_tys[0], "input");
 +    let in_ty = arg_tys[0];
 +
 +    let comparison = match name {
 +        sym::simd_eq => Some(hir::BinOpKind::Eq),
 +        sym::simd_ne => Some(hir::BinOpKind::Ne),
 +        sym::simd_lt => Some(hir::BinOpKind::Lt),
 +        sym::simd_le => Some(hir::BinOpKind::Le),
 +        sym::simd_gt => Some(hir::BinOpKind::Gt),
 +        sym::simd_ge => Some(hir::BinOpKind::Ge),
 +        _ => None,
 +    };
 +
 +    let (in_len, in_elem) = arg_tys[0].simd_size_and_type(bx.tcx());
 +    if let Some(cmp_op) = comparison {
 +        require_simd!(ret_ty, "return");
 +
 +        let (out_len, out_ty) = ret_ty.simd_size_and_type(bx.tcx());
 +        require!(
 +            in_len == out_len,
 +            "expected return type with length {} (same as input type `{}`), \
 +             found `{}` with length {}",
 +            in_len,
 +            in_ty,
 +            ret_ty,
 +            out_len
 +        );
 +        require!(
 +            bx.type_kind(bx.element_type(llret_ty)) == TypeKind::Integer,
 +            "expected return type with integer elements, found `{}` with non-integer `{}`",
 +            ret_ty,
 +            out_ty
 +        );
 +
 +        return Ok(compare_simd_types(
 +            bx,
 +            args[0].immediate(),
 +            args[1].immediate(),
 +            in_elem,
 +            llret_ty,
 +            cmp_op,
 +        ));
 +    }
 +
 +    if let Some(stripped) = name_str.strip_prefix("simd_shuffle") {
 +        let n: u64 = stripped.parse().unwrap_or_else(|_| {
 +            span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?")
 +        });
 +
 +        require_simd!(ret_ty, "return");
 +
 +        let (out_len, out_ty) = ret_ty.simd_size_and_type(bx.tcx());
 +        require!(
 +            out_len == n,
 +            "expected return type of length {}, found `{}` with length {}",
 +            n,
 +            ret_ty,
 +            out_len
 +        );
 +        require!(
 +            in_elem == out_ty,
 +            "expected return element type `{}` (element of input `{}`), \
 +             found `{}` with element type `{}`",
 +            in_elem,
 +            in_ty,
 +            ret_ty,
 +            out_ty
 +        );
 +
 +        let vector = args[2].immediate();
 +
 +        return Ok(bx.shuffle_vector(
 +            args[0].immediate(),
 +            args[1].immediate(),
 +            vector,
 +        ));
 +    }
 +
 +    macro_rules! arith_binary {
 +        ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
 +            $(if name == sym::$name {
 +                match in_elem.kind() {
 +                    $($(ty::$p(_))|* => {
 +                        return Ok(bx.$call(args[0].immediate(), args[1].immediate()))
 +                    })*
 +                    _ => {},
 +                }
 +                require!(false,
 +                         "unsupported operation on `{}` with element `{}`",
 +                         in_ty,
 +                         in_elem)
 +            })*
 +        }
 +    }
 +
 +    arith_binary! {
 +        simd_add: Uint, Int => add, Float => fadd;
 +        simd_sub: Uint, Int => sub, Float => fsub;
 +        simd_mul: Uint, Int => mul, Float => fmul;
 +        simd_div: Uint => udiv, Int => sdiv, Float => fdiv;
 +        simd_rem: Uint => urem, Int => srem, Float => frem;
 +        simd_shl: Uint, Int => shl;
 +        simd_shr: Uint => lshr, Int => ashr;
 +        simd_and: Uint, Int => and;
 +        simd_or: Uint, Int => or; // FIXME(antoyo): calling `or` might not work on vectors.
 +        simd_xor: Uint, Int => xor;
 +    }
 +
++    macro_rules! arith_unary {
++        ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
++            $(if name == sym::$name {
++                match in_elem.kind() {
++                    $($(ty::$p(_))|* => {
++                        return Ok(bx.$call(args[0].immediate()))
++                    })*
++                    _ => {},
++                }
++                require!(false,
++                         "unsupported operation on `{}` with element `{}`",
++                         in_ty,
++                         in_elem)
++            })*
++        }
++    }
++
++    arith_unary! {
++        simd_neg: Int => neg, Float => fneg;
++    }
++
 +    unimplemented!("simd {}", name);
 +}
index ef95dfb6e2e0079c0c182ba19769b681d7929880,0000000000000000000000000000000000000000..eac4a06226cf8aaf6d7ace95fbf54f176c0fc2ae
mode 100644,000000..100644
--- /dev/null
@@@ -1,285 -1,0 +1,308 @@@
-  * TODO(antoyo): support LTO.
 +/*
++ * TODO(antoyo): implement equality in libgccjit based on https://zpz.github.io/blog/overloading-equality-operator-in-cpp-class-hierarchy/ (for type equality?)
 + * TODO(antoyo): support #[inline] attributes.
- use std::sync::Arc;
++ * TODO(antoyo): support LTO (gcc's equivalent to Thin LTO is enabled by -fwhopr: https://stackoverflow.com/questions/64954525/does-gcc-have-thin-lto).
 + *
 + * TODO(antoyo): remove the patches.
 + */
 +
 +#![feature(rustc_private, decl_macro, associated_type_bounds, never_type, trusted_len)]
 +#![allow(broken_intra_doc_links)]
 +#![recursion_limit="256"]
 +#![warn(rust_2018_idioms)]
 +#![warn(unused_lifetimes)]
 +
 +extern crate rustc_ast;
 +extern crate rustc_codegen_ssa;
 +extern crate rustc_data_structures;
 +extern crate rustc_errors;
 +extern crate rustc_hir;
 +extern crate rustc_metadata;
 +extern crate rustc_middle;
 +extern crate rustc_session;
 +extern crate rustc_span;
 +extern crate rustc_target;
++extern crate tempfile;
 +
 +// This prevents duplicating functions and statics that are already part of the host rustc process.
 +#[allow(unused_extern_crates)]
 +extern crate rustc_driver;
 +
 +mod abi;
 +mod allocator;
 +mod archive;
 +mod asm;
 +mod back;
 +mod base;
 +mod builder;
 +mod callee;
 +mod common;
 +mod consts;
 +mod context;
 +mod coverageinfo;
 +mod debuginfo;
 +mod declare;
++mod int;
 +mod intrinsic;
 +mod mono_item;
 +mod type_;
 +mod type_of;
 +
 +use std::any::Any;
- use gccjit::{Context, OptimizationLevel};
++use std::sync::{Arc, Mutex};
 +
- pub struct GccCodegenBackend;
++use gccjit::{Context, OptimizationLevel, CType};
 +use rustc_ast::expand::allocator::AllocatorKind;
 +use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
 +use rustc_codegen_ssa::base::codegen_crate;
 +use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryFn};
 +use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
 +use rustc_codegen_ssa::target_features::supported_target_features;
 +use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, ModuleBufferMethods, ThinBufferMethods, WriteBackendMethods};
 +use rustc_data_structures::fx::FxHashMap;
 +use rustc_errors::{ErrorGuaranteed, Handler};
 +use rustc_metadata::EncodedMetadata;
 +use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
 +use rustc_middle::ty::TyCtxt;
++use rustc_middle::ty::query::Providers;
 +use rustc_session::config::{Lto, OptLevel, OutputFilenames};
 +use rustc_session::Session;
 +use rustc_span::Symbol;
 +use rustc_span::fatal_error::FatalError;
++use tempfile::TempDir;
 +
 +pub struct PrintOnPanic<F: Fn() -> String>(pub F);
 +
 +impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
 +    fn drop(&mut self) {
 +        if ::std::thread::panicking() {
 +            println!("{}", (self.0)());
 +        }
 +    }
 +}
 +
 +#[derive(Clone)]
-         base::compile_codegen_unit(tcx, cgu_name)
++pub struct GccCodegenBackend {
++    supports_128bit_integers: Arc<Mutex<bool>>,
++}
 +
 +impl CodegenBackend for GccCodegenBackend {
 +    fn init(&self, sess: &Session) {
 +        if sess.lto() != Lto::No {
 +            sess.warn("LTO is not supported. You may get a linker error.");
 +        }
++
++        let temp_dir = TempDir::new().expect("cannot create temporary directory");
++        let temp_file = temp_dir.into_path().join("result.asm");
++        let check_context = Context::default();
++        check_context.set_print_errors_to_stderr(false);
++        let _int128_ty = check_context.new_c_type(CType::UInt128t);
++        // NOTE: we cannot just call compile() as this would require other files than libgccjit.so.
++        check_context.compile_to_file(gccjit::OutputKind::Assembler, temp_file.to_str().expect("path to str"));
++        *self.supports_128bit_integers.lock().expect("lock") = check_context.get_last_error() == Ok(None);
++    }
++
++    fn provide(&self, providers: &mut Providers) {
++        // FIXME(antoyo) compute list of enabled features from cli flags
++        providers.global_backend_features = |_tcx, ()| vec![];
 +    }
 +
 +    fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>, metadata: EncodedMetadata, need_metadata_module: bool) -> Box<dyn Any> {
 +        let target_cpu = target_cpu(tcx.sess);
 +        let res = codegen_crate(self.clone(), tcx, target_cpu.to_string(), metadata, need_metadata_module);
 +
 +        Box::new(res)
 +    }
 +
 +    fn join_codegen(&self, ongoing_codegen: Box<dyn Any>, sess: &Session, _outputs: &OutputFilenames) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
 +        let (codegen_results, work_products) = ongoing_codegen
 +            .downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>()
 +            .expect("Expected GccCodegenBackend's OngoingCodegen, found Box<Any>")
 +            .join(sess);
 +
 +        Ok((codegen_results, work_products))
 +    }
 +
 +    fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) -> Result<(), ErrorGuaranteed> {
 +        use rustc_codegen_ssa::back::link::link_binary;
 +
 +        link_binary::<crate::archive::ArArchiveBuilder<'_>>(
 +            sess,
 +            &codegen_results,
 +            outputs,
 +        )
 +    }
 +
 +    fn target_features(&self, sess: &Session) -> Vec<Symbol> {
 +        target_features(sess)
 +    }
 +}
 +
 +impl ExtraBackendMethods for GccCodegenBackend {
 +    fn new_metadata<'tcx>(&self, _tcx: TyCtxt<'tcx>, _mod_name: &str) -> Self::Module {
 +        GccContext {
 +            context: Context::default(),
 +        }
 +    }
 +
 +    fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, mods: &mut Self::Module, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
 +        unsafe { allocator::codegen(tcx, mods, module_name, kind, has_alloc_error_handler) }
 +    }
 +
 +    fn compile_codegen_unit<'tcx>(&self, tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen<Self::Module>, u64) {
-     Box::new(GccCodegenBackend)
++        base::compile_codegen_unit(tcx, cgu_name, *self.supports_128bit_integers.lock().expect("lock"))
 +    }
 +
 +    fn target_machine_factory(&self, _sess: &Session, _opt_level: OptLevel, _features: &[String]) -> TargetMachineFactoryFn<Self> {
 +        // TODO(antoyo): set opt level.
 +        Arc::new(|_| {
 +            Ok(())
 +        })
 +    }
 +
 +    fn target_cpu<'b>(&self, _sess: &'b Session) -> &'b str {
 +        unimplemented!();
 +    }
 +
 +    fn tune_cpu<'b>(&self, _sess: &'b Session) -> Option<&'b str> {
 +        None
 +        // TODO(antoyo)
 +    }
 +}
 +
 +pub struct ModuleBuffer;
 +
 +impl ModuleBufferMethods for ModuleBuffer {
 +    fn data(&self) -> &[u8] {
 +        unimplemented!();
 +    }
 +}
 +
 +pub struct ThinBuffer;
 +
 +impl ThinBufferMethods for ThinBuffer {
 +    fn data(&self) -> &[u8] {
 +        unimplemented!();
 +    }
 +}
 +
 +pub struct GccContext {
 +    context: Context<'static>,
 +}
 +
 +unsafe impl Send for GccContext {}
 +// FIXME(antoyo): that shouldn't be Sync. Parallel compilation is currently disabled with "-Zno-parallel-llvm". Try to disable it here.
 +unsafe impl Sync for GccContext {}
 +
 +impl WriteBackendMethods for GccCodegenBackend {
 +    type Module = GccContext;
 +    type TargetMachine = ();
 +    type ModuleBuffer = ModuleBuffer;
 +    type Context = ();
 +    type ThinData = ();
 +    type ThinBuffer = ThinBuffer;
 +
 +    fn run_fat_lto(_cgcx: &CodegenContext<Self>, mut modules: Vec<FatLTOInput<Self>>, _cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<LtoModuleCodegen<Self>, FatalError> {
 +        // TODO(antoyo): implement LTO by sending -flto to libgccjit and adding the appropriate gcc linker plugins.
 +        // NOTE: implemented elsewhere.
 +        // TODO: what is implemented elsewhere ^ ?
 +        let module =
 +            match modules.remove(0) {
 +                FatLTOInput::InMemory(module) => module,
 +                FatLTOInput::Serialized { .. } => {
 +                    unimplemented!();
 +                }
 +            };
 +        Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: vec![] })
 +    }
 +
 +    fn run_thin_lto(_cgcx: &CodegenContext<Self>, _modules: Vec<(String, Self::ThinBuffer)>, _cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
 +        unimplemented!();
 +    }
 +
 +    fn print_pass_timings(&self) {
 +        unimplemented!();
 +    }
 +
 +    unsafe fn optimize(_cgcx: &CodegenContext<Self>, _diag_handler: &Handler, module: &ModuleCodegen<Self::Module>, config: &ModuleConfig) -> Result<(), FatalError> {
 +        module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));
 +        Ok(())
 +    }
 +
 +    unsafe fn optimize_thin(_cgcx: &CodegenContext<Self>, _thin: &mut ThinModule<Self>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
 +        unimplemented!();
 +    }
 +
 +    unsafe fn codegen(cgcx: &CodegenContext<Self>, diag_handler: &Handler, module: ModuleCodegen<Self::Module>, config: &ModuleConfig) -> Result<CompiledModule, FatalError> {
 +        back::write::codegen(cgcx, diag_handler, module, config)
 +    }
 +
 +    fn prepare_thin(_module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
 +        unimplemented!();
 +    }
 +
 +    fn serialize_module(_module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
 +        unimplemented!();
 +    }
 +
 +    fn run_lto_pass_manager(_cgcx: &CodegenContext<Self>, _module: &ModuleCodegen<Self::Module>, _config: &ModuleConfig, _thin: bool) -> Result<(), FatalError> {
 +        // TODO(antoyo)
 +        Ok(())
 +    }
 +
 +    fn run_link(cgcx: &CodegenContext<Self>, diag_handler: &Handler, modules: Vec<ModuleCodegen<Self::Module>>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
 +        back::write::link(cgcx, diag_handler, modules)
 +    }
 +}
 +
 +/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
 +#[no_mangle]
 +pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
++    Box::new(GccCodegenBackend {
++        supports_128bit_integers: Arc::new(Mutex::new(false)),
++    })
 +}
 +
 +fn to_gcc_opt_level(optlevel: Option<OptLevel>) -> OptimizationLevel {
 +    match optlevel {
 +        None => OptimizationLevel::None,
 +        Some(level) => {
 +            match level {
 +                OptLevel::No => OptimizationLevel::None,
 +                OptLevel::Less => OptimizationLevel::Limited,
 +                OptLevel::Default => OptimizationLevel::Standard,
 +                OptLevel::Aggressive => OptimizationLevel::Aggressive,
 +                OptLevel::Size | OptLevel::SizeMin => OptimizationLevel::Limited,
 +            }
 +        },
 +    }
 +}
 +
 +fn handle_native(name: &str) -> &str {
 +    if name != "native" {
 +        return name;
 +    }
 +
 +    unimplemented!();
 +}
 +
 +pub fn target_cpu(sess: &Session) -> &str {
 +    let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
 +    handle_native(name)
 +}
 +
 +pub fn target_features(sess: &Session) -> Vec<Symbol> {
 +    supported_target_features(sess)
 +        .iter()
 +        .filter_map(
 +            |&(feature, gate)| {
 +                if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
 +            },
 +        )
 +        .filter(|_feature| {
 +            // TODO(antoyo): implement a way to get enabled feature in libgccjit.
 +            false
 +        })
 +        .map(|feature| Symbol::intern(feature))
 +        .collect()
 +}
index 28e2adc492bbeb49f86be0c3037610c3d0fd53a2,0000000000000000000000000000000000000000..e95058085216e94aa9482e579ad4b21097e29b33
mode 100644,000000..100644
--- /dev/null
@@@ -1,282 -1,0 +1,270 @@@
- use crate::common::TypeReflection;
 +use std::convert::TryInto;
 +
 +use gccjit::{RValue, Struct, Type};
 +use rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods};
 +use rustc_codegen_ssa::common::TypeKind;
 +use rustc_middle::bug;
 +use rustc_middle::ty::layout::TyAndLayout;
 +use rustc_target::abi::{AddressSpace, Align, Integer, Size};
 +
-         if typ.is_integral() {
 +use crate::context::CodegenCx;
 +use crate::type_of::LayoutGccExt;
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
 +    pub fn type_ix(&self, num_bits: u64) -> Type<'gcc> {
 +        // gcc only supports 1, 2, 4 or 8-byte integers.
 +        // FIXME(antoyo): this is misleading to use the next power of two as rustc_codegen_ssa
 +        // sometimes use 96-bit numbers and the following code will give an integer of a different
 +        // size.
 +        let bytes = (num_bits / 8).next_power_of_two() as i32;
 +        match bytes {
 +            1 => self.i8_type,
 +            2 => self.i16_type,
 +            4 => self.i32_type,
 +            8 => self.i64_type,
 +            16 => self.i128_type,
 +            _ => panic!("unexpected num_bits: {}", num_bits),
 +        }
 +    }
 +
 +    pub fn type_void(&self) -> Type<'gcc> {
 +        self.context.new_type::<()>()
 +    }
 +
 +    pub fn type_size_t(&self) -> Type<'gcc> {
 +        self.context.new_type::<usize>()
 +    }
 +
 +    pub fn type_u8(&self) -> Type<'gcc> {
 +        self.u8_type
 +    }
 +
 +    pub fn type_u16(&self) -> Type<'gcc> {
 +        self.u16_type
 +    }
 +
 +    pub fn type_u32(&self) -> Type<'gcc> {
 +        self.u32_type
 +    }
 +
 +    pub fn type_u64(&self) -> Type<'gcc> {
 +        self.u64_type
 +    }
 +
 +    pub fn type_u128(&self) -> Type<'gcc> {
 +        self.u128_type
 +    }
 +
 +    pub fn type_pointee_for_align(&self, align: Align) -> Type<'gcc> {
 +        // FIXME(eddyb) We could find a better approximation if ity.align < align.
 +        let ity = Integer::approximate_align(self, align);
 +        self.type_from_integer(ity)
 +    }
 +}
 +
 +impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    fn type_i1(&self) -> Type<'gcc> {
 +        self.bool_type
 +    }
 +
 +    fn type_i8(&self) -> Type<'gcc> {
 +        self.i8_type
 +    }
 +
 +    fn type_i16(&self) -> Type<'gcc> {
 +        self.i16_type
 +    }
 +
 +    fn type_i32(&self) -> Type<'gcc> {
 +        self.i32_type
 +    }
 +
 +    fn type_i64(&self) -> Type<'gcc> {
 +        self.i64_type
 +    }
 +
 +    fn type_i128(&self) -> Type<'gcc> {
 +        self.i128_type
 +    }
 +
 +    fn type_isize(&self) -> Type<'gcc> {
 +        self.isize_type
 +    }
 +
 +    fn type_f32(&self) -> Type<'gcc> {
 +        self.context.new_type::<f32>()
 +    }
 +
 +    fn type_f64(&self) -> Type<'gcc> {
 +        self.context.new_type::<f64>()
 +    }
 +
 +    fn type_func(&self, params: &[Type<'gcc>], return_type: Type<'gcc>) -> Type<'gcc> {
 +        self.context.new_function_pointer_type(None, return_type, params, false)
 +    }
 +
 +    fn type_struct(&self, fields: &[Type<'gcc>], _packed: bool) -> Type<'gcc> {
 +        let types = fields.to_vec();
 +        if let Some(typ) = self.struct_types.borrow().get(fields) {
 +            return typ.clone();
 +        }
 +        let fields: Vec<_> = fields.iter().enumerate()
 +            .map(|(index, field)| self.context.new_field(None, *field, &format!("field{}_TODO", index)))
 +            .collect();
 +        // TODO(antoyo): use packed.
 +        let typ = self.context.new_struct_type(None, "struct", &fields).as_type();
 +        self.struct_types.borrow_mut().insert(types, typ);
 +        typ
 +    }
 +
 +    fn type_kind(&self, typ: Type<'gcc>) -> TypeKind {
-         if typ.is_i8(self) || typ.is_u8(self) {
-             8
-         }
-         else if typ.is_i16(self) || typ.is_u16(self) {
-             16
-         }
-         else if typ.is_i32(self) || typ.is_u32(self) {
-             32
-         }
-         else if typ.is_i64(self) || typ.is_u64(self) {
-             64
-         }
-         else if typ.is_i128(self) || typ.is_u128(self) {
-             128
-         }
-         else {
-             panic!("Cannot get width of int type {:?}", typ);
-         }
++        if self.is_int_type_or_bool(typ) {
 +            TypeKind::Integer
 +        }
++        else if typ.is_compatible_with(self.float_type) {
++            TypeKind::Float
++        }
++        else if typ.is_compatible_with(self.double_type) {
++            TypeKind::Double
++        }
 +        else if typ.dyncast_vector().is_some() {
 +            TypeKind::Vector
 +        }
 +        else {
 +            // TODO(antoyo): support other types.
 +            TypeKind::Void
 +        }
 +    }
 +
 +    fn type_ptr_to(&self, ty: Type<'gcc>) -> Type<'gcc> {
 +        ty.make_pointer()
 +    }
 +
 +    fn type_ptr_to_ext(&self, ty: Type<'gcc>, _address_space: AddressSpace) -> Type<'gcc> {
 +        // TODO(antoyo): use address_space
 +        ty.make_pointer()
 +    }
 +
 +    fn element_type(&self, ty: Type<'gcc>) -> Type<'gcc> {
 +        if let Some(typ) = ty.dyncast_array() {
 +            typ
 +        }
 +        else if let Some(vector_type) = ty.dyncast_vector() {
 +            vector_type.get_element_type()
 +        }
 +        else if let Some(typ) = ty.get_pointee() {
 +            typ
 +        }
 +        else {
 +            unreachable!()
 +        }
 +    }
 +
 +    fn vector_length(&self, _ty: Type<'gcc>) -> usize {
 +        unimplemented!();
 +    }
 +
 +    fn float_width(&self, typ: Type<'gcc>) -> usize {
 +        let f32 = self.context.new_type::<f32>();
 +        let f64 = self.context.new_type::<f64>();
 +        if typ == f32 {
 +            32
 +        }
 +        else if typ == f64 {
 +            64
 +        }
 +        else {
 +            panic!("Cannot get width of float type {:?}", typ);
 +        }
 +        // TODO(antoyo): support other sizes.
 +    }
 +
 +    fn int_width(&self, typ: Type<'gcc>) -> u64 {
++        self.gcc_int_width(typ)
 +    }
 +
 +    fn val_ty(&self, value: RValue<'gcc>) -> Type<'gcc> {
 +        value.get_type()
 +    }
 +}
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
 +    pub fn type_padding_filler(&self, size: Size, align: Align) -> Type<'gcc> {
 +        let unit = Integer::approximate_align(self, align);
 +        let size = size.bytes();
 +        let unit_size = unit.size().bytes();
 +        assert_eq!(size % unit_size, 0);
 +        self.type_array(self.type_from_integer(unit), size / unit_size)
 +    }
 +
 +    pub fn set_struct_body(&self, typ: Struct<'gcc>, fields: &[Type<'gcc>], _packed: bool) {
 +        // TODO(antoyo): use packed.
 +        let fields: Vec<_> = fields.iter().enumerate()
 +            .map(|(index, field)| self.context.new_field(None, *field, &format!("field_{}", index)))
 +            .collect();
 +        typ.set_fields(None, &fields);
 +    }
 +
 +    pub fn type_named_struct(&self, name: &str) -> Struct<'gcc> {
 +        self.context.new_opaque_struct_type(None, name)
 +    }
 +
 +    pub fn type_array(&self, ty: Type<'gcc>, mut len: u64) -> Type<'gcc> {
 +        if let Some(struct_type) = ty.is_struct() {
 +            if struct_type.get_field_count() == 0 {
 +                // NOTE: since gccjit only supports i32 for the array size and libcore's tests uses a
 +                // size of usize::MAX in test_binary_search, we workaround this by setting the size to
 +                // zero for ZSTs.
 +                // FIXME(antoyo): fix gccjit API.
 +                len = 0;
 +            }
 +        }
 +
 +        // NOTE: see note above. Some other test uses usize::MAX.
 +        if len == u64::MAX {
 +            len = 0;
 +        }
 +
 +        let len: i32 = len.try_into().expect("array len");
 +
 +        self.context.new_array_type(None, ty, len)
 +    }
 +}
 +
 +pub fn struct_fields<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>) -> (Vec<Type<'gcc>>, bool) {
 +    let field_count = layout.fields.count();
 +
 +    let mut packed = false;
 +    let mut offset = Size::ZERO;
 +    let mut prev_effective_align = layout.align.abi;
 +    let mut result: Vec<_> = Vec::with_capacity(1 + field_count * 2);
 +    for i in layout.fields.index_by_increasing_offset() {
 +        let target_offset = layout.fields.offset(i as usize);
 +        let field = layout.field(cx, i);
 +        let effective_field_align =
 +            layout.align.abi.min(field.align.abi).restrict_for_offset(target_offset);
 +        packed |= effective_field_align < field.align.abi;
 +
 +        assert!(target_offset >= offset);
 +        let padding = target_offset - offset;
 +        let padding_align = prev_effective_align.min(effective_field_align);
 +        assert_eq!(offset.align_to(padding_align) + padding, target_offset);
 +        result.push(cx.type_padding_filler(padding, padding_align));
 +
 +        result.push(field.gcc_type(cx, !field.ty.is_any_ptr())); // FIXME(antoyo): might need to check if the type is inside another, like Box<Type>.
 +        offset = target_offset + field.size;
 +        prev_effective_align = effective_field_align;
 +    }
 +    if !layout.is_unsized() && field_count > 0 {
 +        if offset > layout.size {
 +            bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
 +        }
 +        let padding = layout.size - offset;
 +        let padding_align = prev_effective_align;
 +        assert_eq!(offset.align_to(padding_align) + padding, layout.size);
 +        result.push(cx.type_padding_filler(padding, padding_align));
 +        assert_eq!(result.len(), 1 + field_count * 2);
 +    }
 +
 +    (result, packed)
 +}
index 649ffc16249a68c13a03e73977299e0ea7633b1a,0000000000000000000000000000000000000000..ed8f0445ca3e20d07dc67de469b089e6b7cab5a5
mode 100644,000000..100644
--- /dev/null
@@@ -1,359 -1,0 +1,361 @@@
-             ty::Adt(def, _) if def.is_box() => {
 +use std::fmt::Write;
 +
 +use gccjit::{Struct, Type};
 +use crate::rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods};
 +use rustc_middle::bug;
 +use rustc_middle::ty::{self, Ty, TypeFoldable};
 +use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
 +use rustc_middle::ty::print::with_no_trimmed_paths;
 +use rustc_target::abi::{self, Abi, F32, F64, FieldsShape, Int, Integer, Pointer, PointeeInfo, Size, TyAbiInterface, Variants};
 +use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
 +
 +use crate::abi::{FnAbiGccExt, GccType};
 +use crate::context::CodegenCx;
 +use crate::type_::struct_fields;
 +
 +impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
 +    fn type_from_unsigned_integer(&self, i: Integer) -> Type<'gcc> {
 +        use Integer::*;
 +        match i {
 +            I8 => self.type_u8(),
 +            I16 => self.type_u16(),
 +            I32 => self.type_u32(),
 +            I64 => self.type_u64(),
 +            I128 => self.type_u128(),
 +        }
 +    }
 +}
 +
 +pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>, defer: &mut Option<(Struct<'gcc>, TyAndLayout<'tcx>)>) -> Type<'gcc> {
 +    match layout.abi {
 +        Abi::Scalar(_) => bug!("handled elsewhere"),
 +        Abi::Vector { ref element, count } => {
 +            let element = layout.scalar_gcc_type_at(cx, element, Size::ZERO);
 +            return cx.context.new_vector_type(element, count);
 +        },
 +        Abi::ScalarPair(..) => {
 +            return cx.type_struct(
 +                &[
 +                    layout.scalar_pair_element_gcc_type(cx, 0, false),
 +                    layout.scalar_pair_element_gcc_type(cx, 1, false),
 +                ],
 +                false,
 +            );
 +        }
 +        Abi::Uninhabited | Abi::Aggregate { .. } => {}
 +    }
 +
 +    let name = match layout.ty.kind() {
 +        // FIXME(eddyb) producing readable type names for trait objects can result
 +        // in problematically distinct types due to HRTB and subtyping (see #47638).
 +        // ty::Dynamic(..) |
 +        ty::Adt(..) | ty::Closure(..) | ty::Foreign(..) | ty::Generator(..) | ty::Str
 +            if !cx.sess().fewer_names() =>
 +        {
 +            let mut name = with_no_trimmed_paths!(layout.ty.to_string());
 +            if let (&ty::Adt(def, _), &Variants::Single { index }) =
 +                (layout.ty.kind(), &layout.variants)
 +            {
 +                if def.is_enum() && !def.variants().is_empty() {
 +                    write!(&mut name, "::{}", def.variant(index).name).unwrap();
 +                }
 +            }
 +            if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
 +                (layout.ty.kind(), &layout.variants)
 +            {
 +                write!(&mut name, "::{}", ty::GeneratorSubsts::variant_name(index)).unwrap();
 +            }
 +            Some(name)
 +        }
 +        ty::Adt(..) => {
 +            // If `Some` is returned then a named struct is created in LLVM. Name collisions are
 +            // avoided by LLVM (with increasing suffixes). If rustc doesn't generate names then that
 +            // can improve perf.
 +            // FIXME(antoyo): I don't think that's true for libgccjit.
 +            Some(String::new())
 +        }
 +        _ => None,
 +    };
 +
 +    match layout.fields {
 +        FieldsShape::Primitive | FieldsShape::Union(_) => {
 +            let fill = cx.type_padding_filler(layout.size, layout.align.abi);
 +            let packed = false;
 +            match name {
 +                None => cx.type_struct(&[fill], packed),
 +                Some(ref name) => {
 +                    let gcc_type = cx.type_named_struct(name);
 +                    cx.set_struct_body(gcc_type, &[fill], packed);
 +                    gcc_type.as_type()
 +                },
 +            }
 +        }
 +        FieldsShape::Array { count, .. } => cx.type_array(layout.field(cx, 0).gcc_type(cx, true), count),
 +        FieldsShape::Arbitrary { .. } =>
 +            match name {
 +                None => {
 +                    let (gcc_fields, packed) = struct_fields(cx, layout);
 +                    cx.type_struct(&gcc_fields, packed)
 +                },
 +                Some(ref name) => {
 +                    let gcc_type = cx.type_named_struct(name);
 +                    *defer = Some((gcc_type, layout));
 +                    gcc_type.as_type()
 +                },
 +            },
 +    }
 +}
 +
 +pub trait LayoutGccExt<'tcx> {
 +    fn is_gcc_immediate(&self) -> bool;
 +    fn is_gcc_scalar_pair(&self) -> bool;
 +    fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, set_fields: bool) -> Type<'gcc>;
 +    fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
 +    fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>;
 +    fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc>;
 +    fn gcc_field_index(&self, index: usize) -> u64;
 +    fn pointee_info_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, offset: Size) -> Option<PointeeInfo>;
 +}
 +
 +impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
 +    fn is_gcc_immediate(&self) -> bool {
 +        match self.abi {
 +            Abi::Scalar(_) | Abi::Vector { .. } => true,
 +            Abi::ScalarPair(..) => false,
 +            Abi::Uninhabited | Abi::Aggregate { .. } => self.is_zst(),
 +        }
 +    }
 +
 +    fn is_gcc_scalar_pair(&self) -> bool {
 +        match self.abi {
 +            Abi::ScalarPair(..) => true,
 +            Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } | Abi::Aggregate { .. } => false,
 +        }
 +    }
 +
 +    /// Gets the GCC type corresponding to a Rust type, i.e., `rustc_middle::ty::Ty`.
 +    /// The pointee type of the pointer in `PlaceRef` is always this type.
 +    /// For sized types, it is also the right LLVM type for an `alloca`
 +    /// containing a value of that type, and most immediates (except `bool`).
 +    /// Unsized types, however, are represented by a "minimal unit", e.g.
 +    /// `[T]` becomes `T`, while `str` and `Trait` turn into `i8` - this
 +    /// is useful for indexing slices, as `&[T]`'s data pointer is `T*`.
 +    /// If the type is an unsized struct, the regular layout is generated,
 +    /// with the inner-most trailing unsized field using the "minimal unit"
 +    /// of that field's type - this is useful for taking the address of
 +    /// that field and ensuring the struct has the right alignment.
 +    //TODO(antoyo): do we still need the set_fields parameter?
 +    fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, set_fields: bool) -> Type<'gcc> {
 +        if let Abi::Scalar(ref scalar) = self.abi {
 +            // Use a different cache for scalars because pointers to DSTs
 +            // can be either fat or thin (data pointers of fat pointers).
 +            if let Some(&ty) = cx.scalar_types.borrow().get(&self.ty) {
 +                return ty;
 +            }
 +            let ty =
 +                match *self.ty.kind() {
 +                    ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
 +                        cx.type_ptr_to(cx.layout_of(ty).gcc_type(cx, set_fields))
 +                    }
 +                    ty::Adt(def, _) if def.is_box() => {
 +                        cx.type_ptr_to(cx.layout_of(self.ty.boxed_ty()).gcc_type(cx, true))
 +                    }
 +                    ty::FnPtr(sig) => cx.fn_ptr_backend_type(&cx.fn_abi_of_fn_ptr(sig, ty::List::empty())),
 +                    _ => self.scalar_gcc_type_at(cx, scalar, Size::ZERO),
 +                };
 +            cx.scalar_types.borrow_mut().insert(self.ty, ty);
 +            return ty;
 +        }
 +
 +        // Check the cache.
 +        let variant_index =
 +            match self.variants {
 +                Variants::Single { index } => Some(index),
 +                _ => None,
 +            };
 +        let cached_type = cx.types.borrow().get(&(self.ty, variant_index)).cloned();
 +        if let Some(ty) = cached_type {
 +            let type_to_set_fields = cx.types_with_fields_to_set.borrow_mut().remove(&ty);
 +            if let Some((struct_type, layout)) = type_to_set_fields {
 +                // Since we might be trying to generate a type containing another type which is not
 +                // completely generated yet, we deferred setting the fields until now.
 +                let (fields, packed) = struct_fields(cx, layout);
 +                cx.set_struct_body(struct_type, &fields, packed);
 +            }
 +            return ty;
 +        }
 +
 +        assert!(!self.ty.has_escaping_bound_vars(), "{:?} has escaping bound vars", self.ty);
 +
 +        // Make sure lifetimes are erased, to avoid generating distinct LLVM
 +        // types for Rust types that only differ in the choice of lifetimes.
 +        let normal_ty = cx.tcx.erase_regions(self.ty);
 +
 +        let mut defer = None;
 +        let ty =
 +            if self.ty != normal_ty {
 +                let mut layout = cx.layout_of(normal_ty);
 +                if let Some(v) = variant_index {
 +                    layout = layout.for_variant(cx, v);
 +                }
 +                layout.gcc_type(cx, true)
 +            }
 +            else {
 +                uncached_gcc_type(cx, *self, &mut defer)
 +            };
 +
 +        cx.types.borrow_mut().insert((self.ty, variant_index), ty);
 +
 +        if let Some((ty, layout)) = defer {
 +            let (fields, packed) = struct_fields(cx, layout);
 +            cx.set_struct_body(ty, &fields, packed);
 +        }
 +
 +        ty
 +    }
 +
 +    fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
 +        if let Abi::Scalar(ref scalar) = self.abi {
 +            if scalar.is_bool() {
 +                return cx.type_i1();
 +            }
 +        }
 +        self.gcc_type(cx, true)
 +    }
 +
 +    fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc> {
 +        match scalar.value {
 +            Int(i, true) => cx.type_from_integer(i),
 +            Int(i, false) => cx.type_from_unsigned_integer(i),
 +            F32 => cx.type_f32(),
 +            F64 => cx.type_f64(),
 +            Pointer => {
 +                // If we know the alignment, pick something better than i8.
 +                let pointee =
 +                    if let Some(pointee) = self.pointee_info_at(cx, offset) {
 +                        cx.type_pointee_for_align(pointee.align)
 +                    }
 +                    else {
 +                        cx.type_i8()
 +                    };
 +                cx.type_ptr_to(pointee)
 +            }
 +        }
 +    }
 +
 +    fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
 +        // TODO(antoyo): remove llvm hack:
 +        // HACK(eddyb) special-case fat pointers until LLVM removes
 +        // pointee types, to avoid bitcasting every `OperandRef::deref`.
 +        match self.ty.kind() {
 +            ty::Ref(..) | ty::RawPtr(_) => {
 +                return self.field(cx, index).gcc_type(cx, true);
 +            }
++            // only wide pointer boxes are handled as pointers
++            // thin pointer boxes with scalar allocators are handled by the general logic below
++            ty::Adt(def, substs) if def.is_box() && cx.layout_of(substs.type_at(1)).is_zst() => {
 +                let ptr_ty = cx.tcx.mk_mut_ptr(self.ty.boxed_ty());
 +                return cx.layout_of(ptr_ty).scalar_pair_element_gcc_type(cx, index, immediate);
 +            }
 +            _ => {}
 +        }
 +
 +        let (a, b) = match self.abi {
 +            Abi::ScalarPair(ref a, ref b) => (a, b),
 +            _ => bug!("TyAndLayout::scalar_pair_element_llty({:?}): not applicable", self),
 +        };
 +        let scalar = [a, b][index];
 +
 +        // Make sure to return the same type `immediate_gcc_type` would when
 +        // dealing with an immediate pair.  This means that `(bool, bool)` is
 +        // effectively represented as `{i8, i8}` in memory and two `i1`s as an
 +        // immediate, just like `bool` is typically `i8` in memory and only `i1`
 +        // when immediate.  We need to load/store `bool` as `i8` to avoid
 +        // crippling LLVM optimizations or triggering other LLVM bugs with `i1`.
 +        // TODO(antoyo): this bugs certainly don't happen in this case since the bool type is used instead of i1.
 +        if scalar.is_bool() {
 +            return cx.type_i1();
 +        }
 +
 +        let offset =
 +            if index == 0 {
 +                Size::ZERO
 +            }
 +            else {
 +                a.value.size(cx).align_to(b.value.align(cx).abi)
 +            };
 +        self.scalar_gcc_type_at(cx, scalar, offset)
 +    }
 +
 +    fn gcc_field_index(&self, index: usize) -> u64 {
 +        match self.abi {
 +            Abi::Scalar(_) | Abi::ScalarPair(..) => {
 +                bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
 +            }
 +            _ => {}
 +        }
 +        match self.fields {
 +            FieldsShape::Primitive | FieldsShape::Union(_) => {
 +                bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
 +            }
 +
 +            FieldsShape::Array { .. } => index as u64,
 +
 +            FieldsShape::Arbitrary { .. } => 1 + (self.fields.memory_index(index) as u64) * 2,
 +        }
 +    }
 +
 +    fn pointee_info_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, offset: Size) -> Option<PointeeInfo> {
 +        if let Some(&pointee) = cx.pointee_infos.borrow().get(&(self.ty, offset)) {
 +            return pointee;
 +        }
 +
 +        let result = Ty::ty_and_layout_pointee_info_at(*self, cx, offset);
 +
 +        cx.pointee_infos.borrow_mut().insert((self.ty, offset), result);
 +        result
 +    }
 +}
 +
 +impl<'gcc, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
 +    fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
 +        layout.gcc_type(self, true)
 +    }
 +
 +    fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
 +        layout.immediate_gcc_type(self)
 +    }
 +
 +    fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
 +        layout.is_gcc_immediate()
 +    }
 +
 +    fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
 +        layout.is_gcc_scalar_pair()
 +    }
 +
 +    fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
 +        layout.gcc_field_index(index)
 +    }
 +
 +    fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
 +        layout.scalar_pair_element_gcc_type(self, index, immediate)
 +    }
 +
 +    fn cast_backend_type(&self, ty: &CastTarget) -> Type<'gcc> {
 +        ty.gcc_type(self)
 +    }
 +
 +    fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
 +        fn_abi.ptr_to_gcc_type(self)
 +    }
 +
 +    fn reg_backend_type(&self, _ty: &Reg) -> Type<'gcc> {
 +        unimplemented!();
 +    }
 +
 +    fn fn_decl_backend_type(&self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
 +        // FIXME(antoyo): return correct type.
 +        self.type_void()
 +    }
 +}
index b9aeee795504780d4a1355019eeee0bfa5832243,0000000000000000000000000000000000000000..1beeee136df3106401aed9afdbde9919c8238fd8
mode 100755,000000..100755
--- /dev/null
@@@ -1,217 -1,0 +1,254 @@@
- if [ -f ./gcc_path ]; then 
 +#!/bin/bash
 +
 +# TODO(antoyo): rewrite to cargo-make (or just) or something like that to only rebuild the sysroot when needed?
 +
 +set -e
 +
-     CARGO_INCREMENTAL=1 cargo rustc --release
++if [ -f ./gcc_path ]; then
 +    export GCC_PATH=$(cat gcc_path)
 +else
 +    echo 'Please put the path to your custom build of libgccjit in the file `gcc_path`, see Readme.md for details'
 +    exit 1
 +fi
 +
 +export LD_LIBRARY_PATH="$GCC_PATH"
 +export LIBRARY_PATH="$GCC_PATH"
 +
++features=
++
++if [[ "$1" == "--features" ]]; then
++    shift
++    features="--features $1"
++    shift
++fi
++
 +if [[ "$1" == "--release" ]]; then
 +    export CHANNEL='release'
-     cargo rustc
++    CARGO_INCREMENTAL=1 cargo rustc --release $features
 +    shift
 +else
 +    echo $LD_LIBRARY_PATH
 +    export CHANNEL='debug'
-     rust_toolchain=$(cat rust-toolchain)
++    cargo rustc $features
++fi
++
++if [[ "$1" == "--build" ]]; then
++    exit
 +fi
 +
 +source config.sh
 +
 +function clean() {
 +    rm -r target/out || true
 +    mkdir -p target/out/gccjit
 +}
 +
 +function mini_tests() {
 +    echo "[BUILD] mini_core"
 +    $RUSTC example/mini_core.rs --crate-name mini_core --crate-type lib,dylib --target $TARGET_TRIPLE
 +
 +    echo "[BUILD] example"
 +    $RUSTC example/example.rs --crate-type lib --target $TARGET_TRIPLE
 +
 +    echo "[AOT] mini_core_hello_world"
 +    $RUSTC example/mini_core_hello_world.rs --crate-name mini_core_hello_world --crate-type bin -g --target $TARGET_TRIPLE
 +    $RUN_WRAPPER ./target/out/mini_core_hello_world abc bcd
 +}
 +
 +function build_sysroot() {
 +    echo "[BUILD] sysroot"
 +    time ./build_sysroot/build_sysroot.sh
 +}
 +
 +function std_tests() {
 +    echo "[AOT] arbitrary_self_types_pointers_and_wrappers"
 +    $RUSTC example/arbitrary_self_types_pointers_and_wrappers.rs --crate-name arbitrary_self_types_pointers_and_wrappers --crate-type bin --target $TARGET_TRIPLE
 +    $RUN_WRAPPER ./target/out/arbitrary_self_types_pointers_and_wrappers
 +
 +    echo "[AOT] alloc_system"
 +    $RUSTC example/alloc_system.rs --crate-type lib --target "$TARGET_TRIPLE"
 +
 +    echo "[AOT] alloc_example"
 +    $RUSTC example/alloc_example.rs --crate-type bin --target $TARGET_TRIPLE
 +    $RUN_WRAPPER ./target/out/alloc_example
 +
 +    echo "[AOT] dst_field_align"
 +    # FIXME(antoyo): Re-add -Zmir-opt-level=2 once rust-lang/rust#67529 is fixed.
 +    $RUSTC example/dst-field-align.rs --crate-name dst_field_align --crate-type bin --target $TARGET_TRIPLE
 +    $RUN_WRAPPER ./target/out/dst_field_align || (echo $?; false)
 +
 +    echo "[AOT] std_example"
 +    $RUSTC example/std_example.rs --crate-type bin --target $TARGET_TRIPLE
 +    $RUN_WRAPPER ./target/out/std_example --target $TARGET_TRIPLE
 +
 +    echo "[AOT] subslice-patterns-const-eval"
 +    $RUSTC example/subslice-patterns-const-eval.rs --crate-type bin -Cpanic=abort --target $TARGET_TRIPLE
 +    $RUN_WRAPPER ./target/out/subslice-patterns-const-eval
 +
 +    echo "[AOT] track-caller-attribute"
 +    $RUSTC example/track-caller-attribute.rs --crate-type bin -Cpanic=abort --target $TARGET_TRIPLE
 +    $RUN_WRAPPER ./target/out/track-caller-attribute
 +
 +    echo "[BUILD] mod_bench"
 +    $RUSTC example/mod_bench.rs --crate-type bin --target $TARGET_TRIPLE
 +}
 +
 +# FIXME(antoyo): linker gives multiple definitions error on Linux
 +#echo "[BUILD] sysroot in release mode"
 +#./build_sysroot/build_sysroot.sh --release
 +
 +# TODO(antoyo): uncomment when it works.
 +#pushd simple-raytracer
 +#if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
 +    #echo "[BENCH COMPILE] ebobby/simple-raytracer"
 +    #hyperfine --runs ${RUN_RUNS:-10} --warmup 1 --prepare "rm -r target/*/debug || true" \
 +    #"RUSTFLAGS='' cargo build --target $TARGET_TRIPLE" \
 +    #"../cargo.sh build"
 +
 +    #echo "[BENCH RUN] ebobby/simple-raytracer"
 +    #cp ./target/*/debug/main ./raytracer_cg_gccjit
 +    #hyperfine --runs ${RUN_RUNS:-10} ./raytracer_cg_llvm ./raytracer_cg_gccjit
 +#else
 +    #echo "[BENCH COMPILE] ebobby/simple-raytracer (skipped)"
 +    #echo "[COMPILE] ebobby/simple-raytracer"
 +    #../cargo.sh build
 +    #echo "[BENCH RUN] ebobby/simple-raytracer (skipped)"
 +#fi
 +#popd
 +
 +function test_libcore() {
 +    pushd build_sysroot/sysroot_src/library/core/tests
 +    echo "[TEST] libcore"
 +    rm -r ./target || true
 +    ../../../../../cargo.sh test
 +    popd
 +}
 +
 +# TODO(antoyo): uncomment when it works.
 +#pushd regex
 +#echo "[TEST] rust-lang/regex example shootout-regex-dna"
 +#../cargo.sh clean
 +## Make sure `[codegen mono items] start` doesn't poison the diff
 +#../cargo.sh build --example shootout-regex-dna
 +#cat examples/regexdna-input.txt | ../cargo.sh run --example shootout-regex-dna | grep -v "Spawned thread" > res.txt
 +#diff -u res.txt examples/regexdna-output.txt
 +
 +#echo "[TEST] rust-lang/regex tests"
 +#../cargo.sh test --tests -- --exclude-should-panic --test-threads 1 -Zunstable-options
 +#popd
 +
 +#echo
 +#echo "[BENCH COMPILE] mod_bench"
 +
 +#COMPILE_MOD_BENCH_INLINE="$RUSTC example/mod_bench.rs --crate-type bin -Zmir-opt-level=3 -O --crate-name mod_bench_inline"
 +#COMPILE_MOD_BENCH_LLVM_0="rustc example/mod_bench.rs --crate-type bin -Copt-level=0 -o target/out/mod_bench_llvm_0 -Cpanic=abort"
 +#COMPILE_MOD_BENCH_LLVM_1="rustc example/mod_bench.rs --crate-type bin -Copt-level=1 -o target/out/mod_bench_llvm_1 -Cpanic=abort"
 +#COMPILE_MOD_BENCH_LLVM_2="rustc example/mod_bench.rs --crate-type bin -Copt-level=2 -o target/out/mod_bench_llvm_2 -Cpanic=abort"
 +#COMPILE_MOD_BENCH_LLVM_3="rustc example/mod_bench.rs --crate-type bin -Copt-level=3 -o target/out/mod_bench_llvm_3 -Cpanic=abort"
 +
 +## Use 100 runs, because a single compilations doesn't take more than ~150ms, so it isn't very slow
 +#hyperfine --runs ${COMPILE_RUNS:-100} "$COMPILE_MOD_BENCH_INLINE" "$COMPILE_MOD_BENCH_LLVM_0" "$COMPILE_MOD_BENCH_LLVM_1" "$COMPILE_MOD_BENCH_LLVM_2" "$COMPILE_MOD_BENCH_LLVM_3"
 +
 +#echo
 +#echo "[BENCH RUN] mod_bench"
 +#hyperfine --runs ${RUN_RUNS:-10} ./target/out/mod_bench{,_inline} ./target/out/mod_bench_llvm_*
 +
 +function test_rustc() {
 +    echo
 +    echo "[TEST] rust-lang/rust"
 +
-     rm -r src/test/ui/{abi*,extern/,llvm-asm/,panic-runtime/,panics/,unsized-locals/,proc-macro/,threads-sendsync/,thinlto/,simd*,borrowck/,test*,*lto*.rs} || true
++    rust_toolchain=$(cat rust-toolchain | grep channel | sed 's/channel = "\(.*\)"/\1/')
 +
 +    git clone https://github.com/rust-lang/rust.git || true
 +    cd rust
 +    git fetch
 +    git checkout $(rustc -V | cut -d' ' -f3 | tr -d '(')
 +    export RUSTFLAGS=
 +
++    git apply - <<EOF
++diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs
++index 887d27fd6dca4..2c2239f2b83d1 100644
++--- a/src/tools/compiletest/src/header.rs
+++++ b/src/tools/compiletest/src/header.rs
++@@ -806,8 +806,8 @@ pub fn make_test_description<R: Read>(
++     cfg: Option<&str>,
++ ) -> test::TestDesc {
++     let mut ignore = false;
++     #[cfg(not(bootstrap))]
++-    let ignore_message: Option<String> = None;
+++    let ignore_message: Option<&str> = None;
++     let mut should_fail = false;
++
++     let rustc_has_profiler_support = env::var_os("RUSTC_PROFILER_SUPPORT").is_some();
++
++EOF
++
 +    rm config.toml || true
 +
 +    cat > config.toml <<EOF
 +[rust]
 +codegen-backends = []
 +deny-warnings = false
 +
 +[build]
 +cargo = "$(which cargo)"
 +local-rebuild = true
 +rustc = "$HOME/.rustup/toolchains/$rust_toolchain-$TARGET_TRIPLE/bin/rustc"
 +EOF
 +
 +    rustc -V | cut -d' ' -f3 | tr -d '('
 +    git checkout $(rustc -V | cut -d' ' -f3 | tr -d '(') src/test
 +
 +    for test in $(rg -i --files-with-matches "//(\[\w+\])?~|// error-pattern:|// build-fail|// run-fail|-Cllvm-args" src/test/ui); do
 +      rm $test
 +    done
 +
 +    git checkout -- src/test/ui/issues/auxiliary/issue-3136-a.rs # contains //~ERROR, but shouldn't be removed
 +
-     rm src/test/ui/llvm-asm/llvm-asm-in-out-operand.rs || true # TODO(antoyo): Enable back this test if I ever implement the llvm_asm! macro.
++    rm -r src/test/ui/{abi*,extern/,panic-runtime/,panics/,unsized-locals/,proc-macro/,threads-sendsync/,thinlto/,simd*,borrowck/,test*,*lto*.rs} || true
 +    for test in $(rg --files-with-matches "catch_unwind|should_panic|thread|lto" src/test/ui); do
 +      rm $test
 +    done
 +    git checkout src/test/ui/type-alias-impl-trait/auxiliary/cross_crate_ice.rs
 +    git checkout src/test/ui/type-alias-impl-trait/auxiliary/cross_crate_ice2.rs
 +
 +    RUSTC_ARGS="-Zpanic-abort-tests -Csymbol-mangling-version=v0 -Zcodegen-backend="$(pwd)"/../target/"$CHANNEL"/librustc_codegen_gcc."$dylib_ext" --sysroot "$(pwd)"/../build_sysroot/sysroot -Cpanic=abort"
 +
 +    echo "[TEST] rustc test suite"
 +    COMPILETEST_FORCE_STAGE0=1 ./x.py test --run always --stage 0 src/test/ui/ --rustc-args "$RUSTC_ARGS"
 +}
 +
 +function clean_ui_tests() {
 +    find rust/build/x86_64-unknown-linux-gnu/test/ui/ -name stamp -exec rm -rf {} \;
 +}
 +
 +case $1 in
 +    "--test-rustc")
 +        test_rustc
 +        ;;
 +
 +    "--test-libcore")
 +        test_libcore
 +        ;;
 +
 +    "--clean-ui-tests")
 +        clean_ui_tests
 +        ;;
 +
++    "--std-tests")
++        std_tests
++        ;;
++
++    "--build-sysroot")
++        build_sysroot
++        ;;
++
 +    *)
 +        clean
 +        mini_tests
 +        build_sysroot
 +        std_tests
 +        test_libcore
 +        test_rustc
 +        ;;
 +esac
index cc8647006ca639e846ad70347239aff21f98df7a,0000000000000000000000000000000000000000..eb38a8a383575587c6880ecf37160fcf133ba499
mode 100644,000000..100644
--- /dev/null
@@@ -1,153 -1,0 +1,153 @@@
-         pub static STDOUT: *mut i32;
 +// Compiler:
 +//
 +// Run-time:
 +//   stdout: 2
 +//     7 8
 +//     10
 +
 +#![allow(unused_attributes)]
 +#![feature(auto_traits, lang_items, no_core, start, intrinsics, track_caller)]
 +
 +#![no_std]
 +#![no_core]
 +
 +/*
 + * Core
 + */
 +
 +// Because we don't have core yet.
 +#[lang = "sized"]
 +pub trait Sized {}
 +
 +#[lang = "copy"]
 +trait Copy {
 +}
 +
 +impl Copy for isize {}
 +impl Copy for *mut i32 {}
 +impl Copy for usize {}
 +impl Copy for u8 {}
 +impl Copy for i8 {}
 +impl Copy for i32 {}
 +
 +#[lang = "receiver"]
 +trait Receiver {
 +}
 +
 +#[lang = "freeze"]
 +pub(crate) unsafe auto trait Freeze {}
 +
 +#[lang = "panic_location"]
 +struct PanicLocation {
 +    file: &'static str,
 +    line: u32,
 +    column: u32,
 +}
 +
 +mod libc {
 +    #[link(name = "c")]
 +    extern "C" {
 +        pub fn puts(s: *const u8) -> i32;
 +        pub fn fflush(stream: *mut i32) -> i32;
 +        pub fn printf(format: *const i8, ...) -> i32;
 +
-         libc::fflush(libc::STDOUT);
++        pub static stdout: *mut i32;
 +    }
 +}
 +
 +mod intrinsics {
 +    extern "rust-intrinsic" {
 +        pub fn abort() -> !;
 +    }
 +}
 +
 +#[lang = "panic"]
 +#[track_caller]
 +#[no_mangle]
 +pub fn panic(_msg: &str) -> ! {
 +    unsafe {
 +        libc::puts("Panicking\0" as *const str as *const u8);
++        libc::fflush(libc::stdout);
 +        intrinsics::abort();
 +    }
 +}
 +
 +#[lang = "add"]
 +trait Add<RHS = Self> {
 +    type Output;
 +
 +    fn add(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Add for u8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i32 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for usize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for isize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +/*
 + * Code
 + */
 +
 +fn inc_ref(num: &mut isize) -> isize {
 +    *num = *num + 5;
 +    *num + 1
 +}
 +
 +fn inc(num: isize) -> isize {
 +    num + 1
 +}
 +
 +
 +#[start]
 +fn main(mut argc: isize, _argv: *const *const u8) -> isize {
 +    argc = inc(argc);
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
 +    }
 +
 +    let b = inc_ref(&mut argc);
 +    unsafe {
 +        libc::printf(b"%ld %ld\n\0" as *const u8 as *const i8, argc, b);
 +    }
 +
 +    argc = 10;
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
 +    }
 +    0
 +}
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..49376012c40e79e34ca715aabbb6c1e90c271d52
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,153 @@@
++// Compiler:
++//
++// Run-time:
++//   status: 0
++
++#![feature(arbitrary_self_types, auto_traits, core_intrinsics, lang_items, start, intrinsics)]
++
++#![no_std]
++
++mod intrinsics {
++    extern "rust-intrinsic" {
++        pub fn abort() -> !;
++    }
++}
++
++/*
++ * Core
++ */
++
++mod libc {
++    #[link(name = "c")]
++    extern "C" {
++        pub fn puts(s: *const u8) -> i32;
++    }
++}
++
++#[panic_handler]
++fn panic_handler(_: &core::panic::PanicInfo) -> ! {
++    unsafe {
++        core::intrinsics::abort();
++    }
++}
++
++/*
++ * Code
++ */
++
++#[start]
++fn main(argc: isize, _argv: *const *const u8) -> isize {
++    let var = 134217856_u128;
++    let var2 = 10475372733397991552_u128;
++    let var3 = 193236519889708027473620326106273939584_u128;
++    let var4 = 123236519889708027473620326106273939584_u128;
++    let var5 = 153236519889708027473620326106273939584_u128;
++    let var6 = 18446744073709551616_i128;
++    let var7 = 170141183460469231731687303715884105728_u128;
++
++    // Shifts.
++    assert_eq!(var << (argc as u128 - 1), var);
++    assert_eq!(var << argc as u128, 268435712);
++    assert_eq!(var << (argc + 32) as u128, 1152922604118474752);
++    assert_eq!(var << (argc + 48) as u128, 75557935783508361347072);
++    assert_eq!(var << (argc + 60) as u128, 309485304969250248077606912);
++    assert_eq!(var << (argc + 62) as u128, 1237941219877000992310427648);
++    assert_eq!(var << (argc + 63) as u128, 2475882439754001984620855296);
++    assert_eq!(var << (argc + 80) as u128, 324518863143436548128224745357312);
++
++    assert_eq!(var2 << argc as u128, 20950745466795983104);
++    assert_eq!(var2 << (argc as u128 - 1), var2);
++    assert_eq!(var2 << (argc + 32) as u128, 89982766606709001335848566784);
++    assert_eq!(var2 << (argc + 48) as u128, 5897110592337281111546171672756224);
++    assert_eq!(var2 << (argc + 60) as u128, 24154564986213503432893119171609493504);
++    assert_eq!(var2 << (argc + 62) as u128, 96618259944854013731572476686437974016);
++    assert_eq!(var2 << (argc + 63) as u128, 193236519889708027463144953372875948032);
++
++    assert_eq!(var3 << argc as u128, 46190672858477591483866044780779667712);
++    assert_eq!(var3 << (argc as u128 - 1), var3);
++    assert_eq!(var3 << (argc + 32) as u128, 21267668304951024224840338247585366016);
++    assert_eq!(var3 << (argc + 48) as u128, 1335125106377253154015353231953100800);
++    assert_eq!(var3 << (argc + 60) as u128, 24154564986213503432893119171609493504);
++    assert_eq!(var3 << (argc + 62) as u128, 96618259944854013731572476686437974016);
++    assert_eq!(var3 << (argc + 63) as u128, 193236519889708027463144953372875948032);
++
++    assert_eq!((2220326408_u32 + argc as u32) >> (32 - 6), 33);
++
++    assert_eq!(var >> (argc as u128 - 1), var);
++    assert_eq!(var >> argc as u128, 67108928);
++    assert_eq!(var >> (argc + 32) as u128, 0);
++    assert_eq!(var >> (argc + 48) as u128, 0);
++    assert_eq!(var >> (argc + 60) as u128, 0);
++    assert_eq!(var >> (argc + 62) as u128, 0);
++    assert_eq!(var >> (argc + 63) as u128, 0);
++
++    assert_eq!(var2 >> argc as u128, 5237686366698995776);
++    assert_eq!(var2 >> (argc as u128 - 1), var2);
++    assert_eq!(var2 >> (argc + 32) as u128, 1219493888);
++    assert_eq!(var2 >> (argc + 48) as u128, 18608);
++    assert_eq!(var2 >> (argc + 60) as u128, 4);
++    assert_eq!(var2 >> (argc + 62) as u128, 1);
++    assert_eq!(var2 >> (argc + 63) as u128, 0);
++
++    assert_eq!(var3 >> (argc as u128 - 1), var3);
++    assert_eq!(var3 >> argc as u128, 96618259944854013736810163053136969792);
++    assert_eq!(var3 >> (argc + 32) as u128, 22495691651677250335181635584);
++    assert_eq!(var3 >> (argc + 48) as u128, 343257013727985387194544);
++    assert_eq!(var3 >> (argc + 60) as u128, 83802981867183932420);
++    assert_eq!(var3 >> (argc + 62) as u128, 20950745466795983105);
++    assert_eq!(var3 >> (argc + 63) as u128, 10475372733397991552);
++    assert_eq!(var3 >> (argc + 80) as u128, 79920751444992);
++
++    assert_eq!(var6 >> argc as u128, 9223372036854775808);
++    assert_eq!((var6 - 1) >> argc as u128, 9223372036854775807);
++    assert_eq!(var7 >> argc as u128, 85070591730234615865843651857942052864);
++
++    // Casts
++    assert_eq!((var >> (argc + 32) as u128) as u64, 0);
++    assert_eq!((var >> argc as u128) as u64, 67108928);
++
++    // Addition.
++    assert_eq!(var + argc as u128, 134217857);
++
++    assert_eq!(var2 + argc as u128, 10475372733397991553);
++    assert_eq!(var2 + (var2 + argc as u128) as u128, 20950745466795983105);
++
++    assert_eq!(var3 + argc as u128, 193236519889708027473620326106273939585);
++
++    // Subtraction
++    assert_eq!(var - argc as u128, 134217855);
++
++    assert_eq!(var2 - argc as u128, 10475372733397991551);
++
++    assert_eq!(var3 - argc as u128, 193236519889708027473620326106273939583);
++
++    // Multiplication
++    assert_eq!(var * (argc + 1) as u128, 268435712);
++    assert_eq!(var * (argc as u128 + var2), 1405982069077538020949770368);
++
++    assert_eq!(var2 * (argc + 1) as u128, 20950745466795983104);
++    assert_eq!(var2 * (argc as u128 + var2), 109733433903618109003204073240861360256);
++
++    assert_eq!(var3 * argc as u128, 193236519889708027473620326106273939584);
++
++    assert_eq!(var4 * (argc + 1) as u128, 246473039779416054947240652212547879168);
++
++    assert_eq!(var5 * (argc + 1) as u128, 306473039779416054947240652212547879168);
++
++    // Division.
++    assert_eq!(var / (argc + 1) as u128, 67108928);
++    assert_eq!(var / (argc + 2) as u128, 44739285);
++
++    assert_eq!(var2 / (argc + 1) as u128, 5237686366698995776);
++    assert_eq!(var2 / (argc + 2) as u128, 3491790911132663850);
++
++    assert_eq!(var3 / (argc + 1) as u128, 96618259944854013736810163053136969792);
++    assert_eq!(var3 / (argc + 2) as u128, 64412173296569342491206775368757979861);
++    assert_eq!(var3 / (argc as u128 + var4), 1);
++    assert_eq!(var3 / (argc as u128 + var2), 18446744073709551615);
++
++    assert_eq!(var4 / (argc + 1) as u128, 61618259944854013736810163053136969792);
++    assert_eq!(var4 / (argc + 2) as u128, 41078839963236009157873442035424646528);
++
++    0
++}
index 7111703ca2532096c497313bdff6e3122cfda055,0000000000000000000000000000000000000000..6477b83982804646fbb7bc9c597e9dcd8a09a3d4
mode 100644,000000..100644
--- /dev/null
@@@ -1,129 -1,0 +1,129 @@@
-         pub static STDOUT: *mut i32;
 +// Compiler:
 +//
 +// Run-time:
 +//   stdout: Panicking
 +//   status: signal
 +
 +#![allow(unused_attributes)]
 +#![feature(auto_traits, lang_items, no_core, start, intrinsics)]
 +
 +#![no_std]
 +#![no_core]
 +
 +/*
 + * Core
 + */
 +
 +// Because we don't have core yet.
 +#[lang = "sized"]
 +pub trait Sized {}
 +
 +#[lang = "copy"]
 +trait Copy {
 +}
 +
 +impl Copy for isize {}
 +impl Copy for *mut i32 {}
 +impl Copy for usize {}
 +impl Copy for i32 {}
 +impl Copy for u8 {}
 +impl Copy for i8 {}
 +
 +#[lang = "receiver"]
 +trait Receiver {
 +}
 +
 +#[lang = "freeze"]
 +pub(crate) unsafe auto trait Freeze {}
 +
 +#[lang = "panic_location"]
 +struct PanicLocation {
 +    file: &'static str,
 +    line: u32,
 +    column: u32,
 +}
 +
 +mod libc {
 +    #[link(name = "c")]
 +    extern "C" {
 +        pub fn puts(s: *const u8) -> i32;
 +        pub fn fflush(stream: *mut i32) -> i32;
 +
-         libc::fflush(libc::STDOUT);
++        pub static stdout: *mut i32;
 +    }
 +}
 +
 +mod intrinsics {
 +    extern "rust-intrinsic" {
 +        pub fn abort() -> !;
 +    }
 +}
 +
 +#[lang = "panic"]
 +#[track_caller]
 +#[no_mangle]
 +pub fn panic(_msg: &str) -> ! {
 +    unsafe {
 +        libc::puts("Panicking\0" as *const str as *const u8);
++        libc::fflush(libc::stdout);
 +        intrinsics::abort();
 +    }
 +}
 +
 +#[lang = "add"]
 +trait Add<RHS = Self> {
 +    type Output;
 +
 +    fn add(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Add for u8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i32 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for usize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for isize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +/*
 + * Code
 + */
 +
 +#[start]
 +fn main(mut argc: isize, _argv: *const *const u8) -> isize {
 +    let int = 9223372036854775807isize;
 +    let int = int + argc;
 +    int
 +}
index e8876009cc61043eea0ac5d1624eeb3e76d2fa1e,0000000000000000000000000000000000000000..52de20021f3e0c16c506573e7cf2f896027d317c
mode 100644,000000..100644
--- /dev/null
@@@ -1,165 -1,0 +1,165 @@@
-         pub static STDOUT: *mut i32;
 +
 +// Compiler:
 +//
 +// Run-time:
 +//   stdout: 2
 +//     7
 +//     6
 +//     11
 +
 +#![allow(unused_attributes)]
 +#![feature(auto_traits, lang_items, no_core, start, intrinsics, track_caller)]
 +
 +#![no_std]
 +#![no_core]
 +
 +/*
 + * Core
 + */
 +
 +// Because we don't have core yet.
 +#[lang = "sized"]
 +pub trait Sized {}
 +
 +#[lang = "copy"]
 +trait Copy {
 +}
 +
 +impl Copy for isize {}
 +impl Copy for *mut i32 {}
 +impl Copy for usize {}
 +impl Copy for u8 {}
 +impl Copy for i8 {}
 +impl Copy for i32 {}
 +
 +#[lang = "receiver"]
 +trait Receiver {
 +}
 +
 +#[lang = "freeze"]
 +pub(crate) unsafe auto trait Freeze {}
 +
 +#[lang = "panic_location"]
 +struct PanicLocation {
 +    file: &'static str,
 +    line: u32,
 +    column: u32,
 +}
 +
 +mod libc {
 +    #[link(name = "c")]
 +    extern "C" {
 +        pub fn puts(s: *const u8) -> i32;
 +        pub fn fflush(stream: *mut i32) -> i32;
 +        pub fn printf(format: *const i8, ...) -> i32;
 +
-         libc::fflush(libc::STDOUT);
++        pub static stdout: *mut i32;
 +    }
 +}
 +
 +mod intrinsics {
 +    extern "rust-intrinsic" {
 +        pub fn abort() -> !;
 +    }
 +}
 +
 +#[lang = "panic"]
 +#[track_caller]
 +#[no_mangle]
 +pub fn panic(_msg: &str) -> ! {
 +    unsafe {
 +        libc::puts("Panicking\0" as *const str as *const u8);
++        libc::fflush(libc::stdout);
 +        intrinsics::abort();
 +    }
 +}
 +
 +#[lang = "add"]
 +trait Add<RHS = Self> {
 +    type Output;
 +
 +    fn add(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Add for u8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i32 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for usize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for isize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +/*
 + * Code
 + */
 +
 +struct Test {
 +    field: isize,
 +}
 +
 +fn test(num: isize) -> Test {
 +    Test {
 +        field: num + 1,
 +    }
 +}
 +
 +fn update_num(num: &mut isize) {
 +    *num = *num + 5;
 +}
 +
 +#[start]
 +fn main(mut argc: isize, _argv: *const *const u8) -> isize {
 +    let mut test = test(argc);
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, test.field);
 +    }
 +    update_num(&mut test.field);
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, test.field);
 +    }
 +
 +    update_num(&mut argc);
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
 +    }
 +
 +    let refe = &mut argc;
 +    *refe = *refe + 5;
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
 +    }
 +
 +    0
 +}
index 4dc375309e42695933ade915d98720048a4e0340,0000000000000000000000000000000000000000..e078b37b4aba9ed247fe3849b74fdb4d649ed7b5
mode 100644,000000..100644
--- /dev/null
@@@ -1,221 -1,0 +1,221 @@@
-         pub static STDOUT: *mut i32;
 +// Compiler:
 +//
 +// Run-time:
 +//   stdout: 41
 +//     39
 +//     10
 +
 +#![allow(unused_attributes)]
 +#![feature(auto_traits, lang_items, no_core, start, intrinsics, arbitrary_self_types)]
 +
 +#![no_std]
 +#![no_core]
 +
 +/*
 + * Core
 + */
 +
 +// Because we don't have core yet.
 +#[lang = "sized"]
 +pub trait Sized {}
 +
 +#[lang = "copy"]
 +trait Copy {
 +}
 +
 +impl Copy for isize {}
 +impl Copy for *mut i32 {}
 +impl Copy for usize {}
 +impl Copy for u8 {}
 +impl Copy for i8 {}
 +impl Copy for i16 {}
 +impl Copy for i32 {}
 +
 +#[lang = "deref"]
 +pub trait Deref {
 +    type Target: ?Sized;
 +
 +    fn deref(&self) -> &Self::Target;
 +}
 +
 +#[lang = "receiver"]
 +trait Receiver {
 +}
 +
 +#[lang = "freeze"]
 +pub(crate) unsafe auto trait Freeze {}
 +
 +#[lang = "panic_location"]
 +struct PanicLocation {
 +    file: &'static str,
 +    line: u32,
 +    column: u32,
 +}
 +
 +mod libc {
 +    #[link(name = "c")]
 +    extern "C" {
 +        pub fn printf(format: *const i8, ...) -> i32;
 +        pub fn puts(s: *const u8) -> i32;
 +        pub fn fflush(stream: *mut i32) -> i32;
 +
-         libc::fflush(libc::STDOUT);
++        pub static stdout: *mut i32;
 +    }
 +}
 +
 +mod intrinsics {
 +    extern "rust-intrinsic" {
 +        pub fn abort() -> !;
 +    }
 +}
 +
 +#[lang = "panic"]
 +#[track_caller]
 +#[no_mangle]
 +pub fn panic(_msg: &str) -> ! {
 +    unsafe {
 +        libc::puts("Panicking\0" as *const str as *const u8);
++        libc::fflush(libc::stdout);
 +        intrinsics::abort();
 +    }
 +}
 +
 +#[lang = "add"]
 +trait Add<RHS = Self> {
 +    type Output;
 +
 +    fn add(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Add for u8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i8 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for i32 {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for usize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +impl Add for isize {
 +    type Output = Self;
 +
 +    fn add(self, rhs: Self) -> Self {
 +        self + rhs
 +    }
 +}
 +
 +#[lang = "sub"]
 +pub trait Sub<RHS = Self> {
 +    type Output;
 +
 +    fn sub(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Sub for usize {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +impl Sub for isize {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +impl Sub for u8 {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +impl Sub for i8 {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +impl Sub for i16 {
 +    type Output = Self;
 +
 +    fn sub(self, rhs: Self) -> Self {
 +        self - rhs
 +    }
 +}
 +
 +#[lang = "mul"]
 +pub trait Mul<RHS = Self> {
 +    type Output;
 +
 +    #[must_use]
 +    fn mul(self, rhs: RHS) -> Self::Output;
 +}
 +
 +impl Mul for u8 {
 +    type Output = Self;
 +
 +    fn mul(self, rhs: Self) -> Self::Output {
 +        self * rhs
 +    }
 +}
 +
 +impl Mul for usize {
 +    type Output = Self;
 +
 +    fn mul(self, rhs: Self) -> Self::Output {
 +        self * rhs
 +    }
 +}
 +
 +impl Mul for isize {
 +    type Output = Self;
 +
 +    fn mul(self, rhs: Self) -> Self::Output {
 +        self * rhs
 +    }
 +}
 +
 +/*
 + * Code
 + */
 +
 +#[start]
 +fn main(mut argc: isize, _argv: *const *const u8) -> isize {
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, 40 + argc);
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, 40 - argc);
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, 10 * argc);
 +    }
 +    0
 +}
index ab89f6aff4b50798aadf5191f823e4200110472b,0000000000000000000000000000000000000000..294add968449a4c6104e553cc99f5644442274e4
mode 100644,000000..100644
--- /dev/null
@@@ -1,106 -1,0 +1,112 @@@
 +// Compiler:
 +//
 +// Run-time:
 +//   status: 0
 +//   stdout: 10
 +//      14
 +//      1
 +//      12
 +//      12
 +//      1
 +
 +#![feature(auto_traits, lang_items, no_core, start, intrinsics)]
 +
 +#![no_std]
 +#![no_core]
 +
 +/*
 + * Core
 + */
 +
 +// Because we don't have core yet.
 +#[lang = "sized"]
 +pub trait Sized {}
 +
++#[lang = "destruct"]
++pub trait Destruct {}
++
++#[lang = "drop"]
++pub trait Drop {}
++
 +#[lang = "copy"]
 +trait Copy {
 +}
 +
 +impl Copy for isize {}
 +
 +#[lang = "receiver"]
 +trait Receiver {
 +}
 +
 +#[lang = "freeze"]
 +pub(crate) unsafe auto trait Freeze {}
 +
 +mod intrinsics {
 +    use super::Sized;
 +
 +    extern "rust-intrinsic" {
 +        pub fn abort() -> !;
 +    }
 +}
 +
 +mod libc {
 +    #[link(name = "c")]
 +    extern "C" {
 +        pub fn printf(format: *const i8, ...) -> i32;
 +    }
 +}
 +
 +#[lang = "structural_peq"]
 +pub trait StructuralPartialEq {}
 +
 +#[lang = "structural_teq"]
 +pub trait StructuralEq {}
 +
 +#[lang = "drop_in_place"]
 +#[allow(unconditional_recursion)]
 +pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
 +    // Code here does not matter - this is replaced by the
 +    // real drop glue by the compiler.
 +    drop_in_place(to_drop);
 +}
 +
 +/*
 + * Code
 + */
 +
 +struct Test {
 +    field: isize,
 +}
 +
 +struct WithRef {
 +    refe: &'static Test,
 +}
 +
 +static mut CONSTANT: isize = 10;
 +
 +static mut TEST: Test = Test {
 +    field: 12,
 +};
 +
 +static mut TEST2: Test = Test {
 +    field: 14,
 +};
 +
 +static mut WITH_REF: WithRef = WithRef {
 +    refe: unsafe { &TEST },
 +};
 +
 +#[start]
 +fn main(mut argc: isize, _argv: *const *const u8) -> isize {
 +    unsafe {
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, CONSTANT);
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, TEST2.field);
 +        TEST2.field = argc;
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, TEST2.field);
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, WITH_REF.refe.field);
 +        WITH_REF.refe = &TEST2;
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, TEST.field);
 +        libc::printf(b"%ld\n\0" as *const u8 as *const i8, WITH_REF.refe.field);
 +    }
 +    0
 +}