/gcc-auto-deref

GCC with auto-dereferencing of struct/union pointers when accessing a field.

Primary LanguageCMIT LicenseMIT

gcc-auto-deref

This repo contains a patch of the GNU Compiler Collection which allows for a pointer to a struct or union to be dereferenced using the . operator. This effectively removes the need for explicitly dereferencing using the -> operator.

This is not a breaking change, so existing code-bases should be fine using this compiler instead of some other one. Actually, this change allows for the same code to be valid in even more contexts.

Example

test.c contains an example of the extended feature set of this patched compiler.

Attempting to compile test.c with a non-patched compiler will result in an error detailing a pointer to a struct or union needs to be dereferenced using ->.

Build & Install

As always, the GNU documentation is the best source for accurate information: https://gcc.gnu.org/install/

OSDev wiki has a few related articles that contain great examples.

Use Linux, or WSL. Native Windows is not able to compile GCC.

First, install dependencies.

sudo apt install build-essential make texinfo

Get the source code of binutils, as well as a supported version of GCC.

wget https://ftp.gnu.org/gnu/binutils/binutils-2.38.tar.xz
wget https://ftp.gnu.org/gnu/gcc/gcc-11.3.0/gcc-11.3.0.tar.xz

[binutils] [GCC]

Extract the downloaded archives into their own folders.

tar -xf binutils-2.38.tar.xz binutils-2.38
tar -xf gcc-11.3.0.tar.xz gcc-11.3.0

Download GCC prerequisites.

cd gcc-11.3.0
./contrib/download_prerequisites
cd ..

Here is where we apply the patch to GCC.

patch -p 0 -u -i gcc.patch

Next, create out-of-source build directories for both binutils and GCC.

mkdir binutils-build
mkdir gcc-build

Now we must specify where to install the final executables and libraries. I choose a subdirectory of this repository named install. To do this, we will set the shell environment variable PREFIX.

export PREFIX="/absolute/path/to/this/repository/install"

WARNING: If you do not specify a prefix, your system compiler may be replaced when installing. This breaks things, so specify a prefix!

We can now configure and build binutils.

cd binutils-build
../binutils-2.38/configure --prefix="$PREFIX" --disable-nls --disable-werror
make -j
make install
cd ..
  • --prefix – Set installation destination.
  • --disable-nls – Disable native langauge support (English-only diagnostics).
  • --disable-werror – Disable warnings treated as errors (non-fatal warnings).

And with binutils built and installed, we can move on to GCC.
This step takes a while; make some tea, and go touch grass :^).

cd gcc-build
../gcc-11.3.0/configure --prefix="$PREFIX" --enable-languages=c,c++ --disable-nls --disable-werror --disable-multilib --disable-bootstrap
make -j all-gcc
make -j all-target-libgcc
make install-gcc
make install-target-libgcc
cd ..
  • --prefix – Set installation destination.
  • --disable-nls – Disable native langauge support (English-only diagnostics).
  • --disable-werror – Disable warnings treated as errors (non-fatal warnings).

Only if NOT cross-compiling:

  • --disable-multilib – Disable multiple targets (build native compiler).

Only if current compiler version matches patched compiler version:

  • --disable-bootstrap – Disable building bootstrap compiler (use native compiler to save time).

At this point, there should be an install directory containing bin, lib, etc.
Use the new patched compiler to build test.c and see if it works!

Inspiration

Special thanks to Tsoding for the idea and implementation in TCC.