# Legalize: implement soft-float legalizations · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/bc78d8efdb90c77670c30474071942f093a2427f)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: bc78d8efdb90c77670c30474071942f093a2427f

Requested commit: bc78d8efdb90c77670c30474071942f093a2427f

Commit: bc78d8efdb90c77670c30474071942f093a2427f

Tree: 21038abbc15d9e4c037b5e9981274d741659a332

Author: Matthew Lugg

Committer: Matthew Lugg

## Message

```
Legalize: implement soft-float legalizations

A new `Legalize.Feature` tag is introduced for each float bit width
(16/32/64/80/128). When e.g. `soft_f16` is enabled, all arithmetic and
comparison operations on `f16` are converted to calls to the appropriate
compiler_rt function using the new AIR tag `.legalize_compiler_rt_call`.
This includes casts where the source *or* target type is `f16`, or
integer<=>float conversions to or from `f16`. Occasionally, operations
are legalized to blocks because there is extra code required; for
instance, legalizing `@floatFromInt` where the integer type is larger
than 64 bits requires calling an arbitrary-width integer conversion
function which accepts a pointer to the integer, so we need to use
`alloc` to create such a pointer, and store the integer there (after
possibly zero-extending or sign-extending it).

No backend currently uses these new legalizations (and as such, no
backend currently needs to implement `.legalize_compiler_rt_call`).
However, for testing purposes, I tried modifying the self-hosted x86_64
backend to enable all of the soft-float features (and implement the AIR
instruction). This modified backend was able to pass all of the behavior
tests (except for one `@mod` test where the LLVM backend has a bug
resulting in incorrect compiler-rt behavior!), including the tests
specific to the self-hosted x86_64 backend.

`f16` and `f80` legalizations are likely of particular interest to
backend developers, because most architectures do not have instructions
to operate on these types. However, enabling *all* of these legalization
passes can be useful when developing a new backend to hit the ground
running and pass a good amount of tests more easily.

```

## Parents

- [9c45a87490dab538ee480549d3194e946f2ae6b2](https://git.cafe/gitcafe/zig/commit/9c45a87490dab538ee480549d3194e946f2ae6b2?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/bc78d8efdb90c77670c30474071942f093a2427f?format=markdown)
