# AstGen: avoid redundant "ref" instructions · gitcafe/zig

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

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

Visibility: public

Requested revision: f4d5fcde727d93da9ebe17a27cc62270545ccda6

Requested commit: f4d5fcde727d93da9ebe17a27cc62270545ccda6

Commit: f4d5fcde727d93da9ebe17a27cc62270545ccda6

Tree: 1bb04bf9a99acb6619fae6f2d550672c1249e55c

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
AstGen: avoid redundant "ref" instructions

Whenever a `ref` instruction is needed, it is created and saved in
`AstGen.ref_table` instead of being immediately appended to the current
block body. Then, when the referenced instruction is being added to the
parent block (e.g. from setBlockBody), if it has a ref_table entry, then
the ref instruction is added directly after the instruction being referenced.
This makes sure two properties are upheld:
1. All pointers to the same locals return the same address. This is required
   to be compliant with the language specification.
2. `ref` instructions will dominate their uses. This is a required property
   of ZIR.

A complication arises when a ref instruction refs another ref
instruction. The logic in appendBodyWithFixups must take this into
account, recursively handling ref refs.

```

## Parents

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

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