Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ check: embcc $(if $(call built,x86),libc-x86_64 libcxx-x86_64)
.PHONY: check-self
check-self: embcc
@n=0; bad=0; for f in $(SRCS); do n=$$((n + 1)); \
./embcc -std=c99 -Wall -Wextra -Werror -O2 -Isrc -c -o /dev/null $$f || \
./embcc --target=x86_64-elf -std=c99 -Wall -Wextra -Werror -O2 -Isrc -c -o /dev/null $$f || \
{ echo "check-self: $$f"; bad=$$((bad + 1)); }; done; \
echo "check-self: EmbCC compiles $$((n - bad)) of its $$n sources"; [ $$bad -eq 0 ]

Expand Down Expand Up @@ -1246,7 +1246,7 @@ libcxx-x86_64: embcc embar
@mkdir -p $(BUILD)/libcxx/x86_64
@for f in $(LIBCXX_SRCS); do \
o=$(BUILD)/libcxx/x86_64/$$(basename $$f .cc).o; \
./embcc -c -O2 -x c++ $(LIBCXX_INC) $$f -o $$o || exit 1; \
./embcc --target=x86_64-elf -c -O2 -x c++ $(LIBCXX_INC) $$f -o $$o || exit 1; \
done
@rm -f $(BUILD)/libcxx/x86_64/libcxx.a
@$${EMBCC_X86_AR:-./embar} rcs $(BUILD)/libcxx/x86_64/libcxx.a \
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,17 @@ described in [Getting started](docs/manual/getting-started.md).

EmbCC is also a compiler for operating systems and hosted programs:
x86-64 and AArch64 kernels (EmbLinkOS among them), and Linux, macOS and
Windows objects. On Linux x86-64, `make libc-linux-x86_64` builds the C
library, and `./embcc --target=x86_64-linux-gnu -O2 hello.c -o hello`
compiles and links a static executable. On macOS, EmbCC writes Mach-O
objects for the system linker:
Windows objects. With no `--target=`, it compiles for the machine it
runs on, as GCC and clang do:

```sh
./embcc --target=aarch64-apple-darwin --opt=speed -c hello.c -o hello.o
cc hello.o -o hello
./embcc -O2 hello.c -o hello && ./hello
```

On Linux that is a static executable against EmbCC's own C library,
which `make` builds. On macOS it is a Mach-O program linked by the
system linker. A board or a kernel is always named with `--target=`.

`x86_64-elf` and `aarch64-elf` build freestanding kernels; `embld
--embx` writes EmbLinkOS images.

Expand Down
5 changes: 3 additions & 2 deletions docs/manual/embedded.md
Original file line number Diff line number Diff line change
Expand Up @@ -1576,8 +1576,9 @@ instruction. Fences compile to nothing.

## x86-64 kernels and EmbLinkOS

`x86_64-elf` is the freestanding x86-64 target and the default when no
`--target=` is given. `x86_64-emblink` is the same code generator with
`x86_64-elf` is the freestanding x86-64 target; name it with
`--target=x86_64-elf`, since the default is the machine EmbCC runs on.
`x86_64-emblink` is the same code generator with
`__EmbLinkOS__`, `__emblink__` and `__emblink` predefined, for EmbLinkOS
userland.

Expand Down
2 changes: 1 addition & 1 deletion docs/manual/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ a warning and no debug information for the Darwin targets. See

### Bare-metal x86-64 and EmbLinkOS

`x86_64-elf` (the default target) and `x86_64-emblink` are freestanding:
`x86_64-elf` and `x86_64-emblink` are freestanding:
`embcc` links them only when the program defines its own `_start`.
Normally you compile with `-c` and link with [`embld`](tools/embld.md)
together with your start-up code, as described in
Expand Down
16 changes: 13 additions & 3 deletions docs/manual/invoking.md
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,19 @@ With no `--target=`, the target is chosen in this order:
2. the `DEFAULT_TARGET` the compiler was built with
(`make DEFAULT_TARGET=riscv32-unknown-elf`; see
[Getting started](getting-started.md#choosing-the-default-target));
3. `x86_64-elf`, or in a compiler built without x86 (`make TARGETS=`; see
[Choosing the targets](getting-started.md#choosing-the-targets)) the
first family it has: `thumbv7m-none-eabi` for `TARGETS=arm`.
3. the machine the compiler runs on, as GCC and clang do:
`aarch64-apple-darwin` on an arm64 Mac, `x86_64-apple-darwin` on an
Intel one, `x86_64-linux-gnu` or `aarch64-linux-gnu` on Linux,
`x86_64-windows-gnu` on Windows, `x86_64-emblink` on EmbLinkOS. So
`embcc hello.c -o hello` builds a program that runs here;
4. where the compiler was built without that machine's family
(`make TARGETS=`; see
[Choosing the targets](getting-started.md#choosing-the-targets)) or
on a system it has no hosted target for, `x86_64-elf`, or without x86
the first family it has: `thumbv7m-none-eabi` for `TARGETS=arm`.

A build that compiles for a board or a kernel says so with `--target=`,
as it would with clang.

`--target=` on the command line overrides both. A default that is not an
accepted triple stops every invocation, naming where it came from:
Expand Down
7 changes: 4 additions & 3 deletions docs/manual/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ embcc: x86_64
...
```

With no `--target=`, EmbCC compiles for `x86_64-elf`, unless the compiler
was built with `make DEFAULT_TARGET=TRIPLE` or the environment sets
`EMBCC_DEFAULT_TARGET`.
With no `--target=`, EmbCC compiles for the machine it runs on, as GCC
and clang do, unless the compiler was built with `make
DEFAULT_TARGET=TRIPLE` or the environment sets `EMBCC_DEFAULT_TARGET`
([The default target](invoking.md#the-default-target)).

### `EMBCC_DEFAULT_TARGET`

Expand Down
9 changes: 4 additions & 5 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ feeling. "Today" is the state at 0.0.1-beta (October 2026).
| Coverage | Every construct EmbCC refuses is either implemented or listed in the manual as permanent, with the reason and a workaround. At least 99% of the GCC torture files that GCC accepts are accepted. | 110 distinct backend refusals and 134 front-end and driver refusals in the source. 95.4% of the torture files GCC accepts are accepted. |
| Code size | `-Os`, object by object against clang: at most 1.10 on Cortex-M and RISC-V, 1.25 on AVR. | Cortex-M4 1.14, Cortex-M0 1.17, RV32 1.28, RV64 1.28, AVR 1.53, x86-64 1.70, AArch64 1.49; without math, Cortex-M4 1.08 and RISC-V 1.12 (`tools/scoreboard.sh`, 2026-10-24). |
| Debugging | `-g` changes no code (already true). A variable held in a register is shown by the debugger, not reported as optimized out. Breakpoints, stepping and locals are checked through GDB on every board family EmbSim or QEMU runs. | `-g` changes no code on any target. No location lists yet, so register-held variables show as `<optimized out>`. `-g` is refused for Windows. |
| Stable interface | EmbCC's own option names are final. GCC spellings are accepted by one compatibility layer. Removing anything follows the deprecation policy below. The default target is settled. | EmbCC's own option vocabulary is in place. The GCC compatibility layer is still spread through the driver. The default target is `x86_64-elf`. |
| Stable interface | EmbCC's own option names are final. GCC spellings are accepted by one compatibility layer. Removing anything follows the deprecation policy below. The default target is settled. | EmbCC's own option vocabulary is in place. The GCC compatibility layer is still spread through the driver. The default target is the machine EmbCC runs on, as with GCC and clang (decided November 2026). |
| Real hardware | Programs built by EmbCC verified on at least one real board per major family: Cortex-M0+, Cortex-M3/M4, AVR, RISC-V, PIC32. `embflash` checked with a real probe. | Verified on QEMU and EmbSim only. |
| Installing | Linux and macOS (x86-64 and arm64) packages from every release, a Homebrew tap, and `install.sh` with no arguments installing the latest release. A Windows host build. | Linux and macOS tarballs and `.deb` files from the release workflow. No Homebrew tap repository yet. No Windows host build. |
| Documentation | The manual and the status page are re-audited against the compiler before each release. A getting-started page for each EmbSim board. Migration guides from `arm-none-eabi-gcc`, `avr-gcc` and XC32. | The manual is complete, but the status page has drifted (it still lists C++ as refused on Cortex-M). The general [Migrating from GCC](manual/migrating-from-gcc.md) page exists; none yet for a particular board toolchain. |
Expand Down Expand Up @@ -80,9 +80,8 @@ not at all.
- The GCC compatibility layer: the driver reads options by identity
from one table; every GCC spelling is an alias in one module,
documented on a migration page.
- The default target: decide whether `embcc` with no `--target` builds
for the host (as clang does) or stays `x86_64-elf`, and ship the
decision with a warning period.
- The default target: done. `embcc` with no `--target` builds for the
machine it runs on, as clang does.
- The source layout: the planned move to `compiler/`, `targets/`,
`runtime/`, `sim/`, `tools/` happens now or is dropped. Either way,
the build and install interface (`make`, `./embcc`, `embld`) stays.
Expand Down Expand Up @@ -175,7 +174,7 @@ Each of these changes the plan, and none can be made by code alone:

| Decision | Needed by | Options |
|---|---|---|
| The default target | 0.2.0 | The host, as clang does; or `x86_64-elf`, as now. EmbLinkOS's build relies on today's default and would need `--target=x86_64-elf` first. |
| The default target | done | The host, as clang does (November 2026). A build for a board or a kernel names its target. EmbLinkOS's `mathself` recipe relied on the old default and needs `--target=x86_64-elf`. |
| The source layout | 0.2.0 | Move to the planned layout before 1.0, or keep `src/`. |
| Which refusals are permanent | 0.5.0 | Nested functions, C++ on AVR, GCC options for other compilers' ABIs (`-mabicalls`, RX `-m64bit-doubles`, ...). |
| Hardware for validation | 0.9.0 | The six boards above and a debug probe (a CMSIS-DAP or J-Link, and a PICkit or MPLAB Snap for PIC32). |
Expand Down
42 changes: 38 additions & 4 deletions src/arch/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,44 @@ const char *target_default_name(void)
return t && *t ? t : NULL;
}

/* The machine this compiler runs on, as a target: what GCC and clang
* build for when no --target= is given. Read from the compiler that built
* this one, so a compiler built on an arm64 Mac targets arm64 macOS and
* one built on Linux targets Linux, and a compiler EmbCC builds for
* EmbLinkOS targets EmbLinkOS. NULL for a host EmbCC has no hosted target
* for, which then falls back as before. */
static const char *host_triple(void)
{
#if defined(__emblink__) || defined(__EmbLinkOS__)
# if defined(__aarch64__)
return "aarch64-emblink";
# else
return "x86_64-emblink";
# endif
#elif defined(__APPLE__) && defined(__aarch64__)
return "aarch64-apple-darwin";
#elif defined(__APPLE__) && defined(__x86_64__)
return "x86_64-apple-darwin";
#elif defined(__linux__) && defined(__x86_64__)
return "x86_64-linux-gnu";
#elif defined(__linux__) && defined(__aarch64__)
return "aarch64-linux-gnu";
#elif defined(_WIN32) && (defined(__x86_64__) || defined(_M_X64))
return "x86_64-windows-gnu";
#else
return NULL;
#endif
}

const char *target_builtin_default(void)
{
/* the host, where its family is built in (make TARGETS=) */
const char *h = host_triple();
enum target_arch a;
enum target_os os;
enum target_fmt fmt;
if (h && target_from_triple(h, &a, &os, &fmt) && target_arch_built(a))
return h;
if (FAMILY_BUILT(x86))
return "x86_64-elf";
for (int i = 0; i < target_family_count(); i++)
Expand Down Expand Up @@ -240,10 +276,8 @@ int target_apply_default(const char **bad)
enum target_os os;
enum target_fmt fmt;

/* the built-in default stands -- x86_64-elf, unless this compiler was
* built without x86 (make TARGETS=), and then its first family's */
if (!t && FAMILY_BUILT(x86))
return 1;
/* else the built-in default: the machine this compiler runs on, as
* GCC and clang build for it (target_builtin_default) */
if (!t)
t = target_builtin_default();
if (!target_from_triple(t, &a, &os, &fmt)) {
Expand Down
20 changes: 15 additions & 5 deletions tests/golden/default-target.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/sh
# The CONFIGURED default target: `embcc main.c` compiles for the board.
# The default target: the machine EmbCC runs on, as with GCC and clang,
# and the CONFIGURED one, with which `embcc main.c` compiles for the board.
#
# A person whose work is one machine should not type --target= a hundred
# times a day, and a Makefile that inherited no environment should still
Expand Down Expand Up @@ -41,9 +42,18 @@ fail() { echo "$@"; exit 1; }
mach() { "$EMBCC" "$@" -dumpmachine 2>&1; }

# ---- 1. the order -----------------------------------------------------
# With nothing configured, the machine this runs on, as GCC and clang
# build for it (`embcc hello.c -o hello` makes a program for here)
case "$(uname -s)-$(uname -m)" in
Darwin-arm64) host=aarch64-apple-darwin ;;
Darwin-x86_64) host=x86_64-apple-darwin ;;
Linux-x86_64) host=x86_64-linux-gnu ;;
Linux-aarch64) host=aarch64-linux-gnu ;;
*) host=x86_64-elf ;;
esac
got=$(mach)
[ "$got" = x86_64-elf ] ||
fail "with nothing configured the default is '$got', not x86_64-elf"
[ "$got" = "$host" ] ||
fail "with nothing configured the default is '$got', not this host's $host"

got=$(EMBCC_DEFAULT_TARGET=riscv32-unknown-elf mach)
[ "$got" = riscv32-unknown-elf ] ||
Expand All @@ -52,7 +62,7 @@ got=$(EMBCC_DEFAULT_TARGET=riscv32-unknown-elf mach)
got=$(EMBCC_DEFAULT_TARGET=riscv32-unknown-elf mach --target=aarch64-elf)
[ "$got" = aarch64-elf ] ||
fail "--target= did not override the environment: got '$got'"
echo " --target= > EMBCC_DEFAULT_TARGET > x86_64-elf"
echo " --target= > EMBCC_DEFAULT_TARGET > the host ($host)"

# ---- 2. the refusal ---------------------------------------------------
# In a subshell: `VAR=val func` leaves VAR set in the CALLING shell when
Expand Down Expand Up @@ -130,7 +140,7 @@ echo " and a default makes the code --target= makes"
# now, around this very script, so if the driver ever reads it the suite
# retargets itself.
got=$(EMBCC_TARGET=riscv64-unknown-elf mach)
[ "$got" = x86_64-elf ] ||
[ "$got" = "$host" ] ||
fail "the driver read EMBCC_TARGET (got '$got') -- that name belongs to
tests/lib.sh, and honouring it retargets the whole suite"
echo " EMBCC_TARGET is the harness's, and the driver leaves it alone"
6 changes: 6 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ fi
cd "$(dirname "$0")/.."
EMBCC=${EMBCC_ARG:-$PWD/embcc}
export EMBCC
# The suite's tests were written against x86_64-elf as the target nothing
# names, and name every other one: they compile for it on any host. The
# compiler's own default is the machine it runs on (default-target.sh,
# which unsets this, tests that), and a person's EMBCC_DEFAULT_TARGET for
# a board must not retarget the suite.
export EMBCC_DEFAULT_TARGET=x86_64-elf

# Turn on the optimizer's IR verifier for the whole suite: every -O1/-O2 compile
# (optimizer.sh, regalloc-O2.sh, the on-metal kernel build) then checks that no
Expand Down
2 changes: 1 addition & 1 deletion tools/gen-selfhost-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SRCS=$(make -pn 2>/dev/null | sed -n 's/^SRCS := //p' | head -1)
[ -n "$SRCS" ] || { echo "gen-selfhost-ref: cannot read SRCS from the Makefile" >&2; exit 1; }
rm -rf ref; mkdir -p ref
for f in $SRCS; do
./embcc -c "$f" $INCS -o "ref/$(echo "${f#src/}" | tr / _ | sed 's/\.c$//').o"
./embcc --target=x86_64-elf -c "$f" $INCS -o "ref/$(echo "${f#src/}" | tr / _ | sed 's/\.c$//').o"
done
echo "wrote $(ls ref/*.o | wc -l) reference objects"
./embld -o "$OS_BUILD/embcc.elf" "$OS_BUILD/crt0.o" "$OS_BUILD/syscalls.o" \
Expand Down
2 changes: 1 addition & 1 deletion tools/os-stage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ELF="$OUT/$NAME.elf"
rm -f "$OBJ" "$ELF"

echo "embcc -c $SRC -o $OBJ"
./embcc -c "$SRC" -o "$OBJ"
./embcc --target=x86_64-elf -c "$SRC" -o "$OBJ"

# -nostartfiles: crt0.o provides _start, there are no crtX files here.
# -static: the kernel binds a shared libembk.so only for UI apps; an M1 program
Expand Down
Loading