> ## Content Index
> Fetch the complete content index at: https://jahans.blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# The Layered Aggregate, or Accessor Aggregate pattern
- URL: https://jahans.blog/the-layered-aggregate-or-accessor-aggregate-pattern/
- Published: 2026-11-22T04:54:31.000Z
- Updated: 2026-11-22T23:38:39.000Z
- Author: Jahan Addison
- Tags: C++, compiler theory, compilers, computer science, credence

The backend in Credence – the compiler of a systems language [I've now given a name](https://github.com/jahan-addison/Brocoli?ref=jahans.blog) – that powers its ARM64 and x86-64 code generation, instruction selection, and memory access is some of the most profound code I've ever written. But before I talk about the machine model I designed to access memory addresses in the backends, I want to start with a simple example:  
  
A common scenario in parsing is very large visitor classes or dispatch methods over each node type in an AST. [The C language is no exception](https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-parser.cc?ref=jahans.blog). These can be huge, spanning thousands of lines and translation units. We all know that "god" classes are a big no-no, and should try to avoid them.  
  
Suppose we want to build a parser for a language, but we don't want one big Parser class with a function to handle each node type. Moreover, one part of the parser may depend on a branch of another - and each contains the shared context or state. You might have a line that is a bare expression like `x ;` or a statement that includes expressions such as `print x ;` . In this case, an expression needs only the token stream, but a statement needs to parse expressions.  
  
Let's say our language has a primitive way to declare statements and expressions with basic arithmetic:

```
 let x = 1 + 2 * 3 ;
 print x ;
```

Here, each line is a statement, and each statement may have an expression. These are our two constructs for building sections of the Parser.

The shared state would be our [token stream](https://en.wikipedia.org/wiki/Lexical%5Fanalysis?ref=jahans.blog) and our position within the tokens:  

```C++
struct Token_Stream
{
    std::vector<std::string> tokens{};
    std::size_t position{ 0 };

    std::string const& current() const;
    std::string const& advance();
    bool match(std::string const& token);
};
```

Both a statement parser and the expression parser would need this `Token_Stream` \- each would `advance` the stream as we parse the source code. An expression would only depend on context, so let's do that first:

```C++
class Expression_Parser
{
  public:
    explicit Expression_Parser(Token_Stream& tokens);

    std::string parse_expression();

  private:
    std::string parse_term();

  private:
    Token_Stream& tokens_;
};
```

  
Pretty straightforward, right? This would deal with expressions such as `5 + 5` or `1 + 2 * 3`. Next our `Statement_Parser`:

```C++
class Statement_Parser
{
  public:
    Statement_Parser(Token_Stream& tokens, Expression_Parser& expressions);

    std::string parse_statement();

  private:
    Token_Stream& tokens_;
    Expression_Parser& expressions_;
};
```

Here is where things get interesting. A statement needs the token stream content, but it also needs to parse expressions. This is where the "layering" comes in or a *dependency in one direction*.

## A building's floors

  
In the Layered Aggregate, each part may lean on the parts below it, but **never** on the parts above. Think of floors in a building: the second floor rests on the first, and the third on the second, but no floor rests on the one above it.  
In a parser, the statement parser uses the expression parser, because a statement contains expressions. The expression parser never calls back into the statement parser. Both rest on the ground floor, the token stream.  
  
Finally, here's what the "Aggregate" that glues it all together looks like:

```C++
class Parser
{
  public:
    explicit Parser(std::string const& source);
    // No copy or move
    Parser(Parser const&) = delete;
    Parser& operator=(Parser const&) = delete;
    Parser(Parser&&) = delete;
    Parser& operator=(Parser&&) = delete;

    std::vector<std::string> parse_program();

  private:
    // Declaration order is layer order - members are constructed top to
    // bottom, so each part refers only to the members above it
    Token_Stream tokens_;
    Expression_Parser expressions_{ tokens_ };
    Statement_Parser statements_{ tokens_, expressions_ };
};
```

We see now that our parser has two layers or "floors." First, the layer that can parse statements. Then, the expression parser. The parent layer may use the layer below as a utility. This allows us to use references to a single instance of each type! 

## The Accessor Aggregate

The example above is simple and straightforward, but that's not what I created the pattern for. In fact, my parser in Credence is still a big god class, admittedly. What I really needed this for was the one-way dependency layering in a machine model of an architecture's storage devices:

![diagram of Credence's ARM64 memory accessor floors](https://jahans.blog/content/images/2026/11/image.png)

During code generation, to accurately grab the storage device of a memory address, I needed all registers currently in use, the stack, the current stack frame, and much more. Maybe the storage device was a pointer in a vector; maybe it was currently on the `x8` register; or maybe it's in the machine's accumulator! This **Layered Aggregate** (or Accessor Aggregate) lets me query the entire machine model to find where an address was stored, with each part single-purpose and easy to extend.  
  
The context includes the general code generation tools: both the current IR instruction and our position in instruction generation, the current stack layout, the current stack frame, and any signals on the signal register.

The layers:

```C++
detail::Flag_Accessor flag_accessor{}; // no dependencies
detail::Table_Accessor table_accessor{ table_, instructions_ }; // context only
detail::Accumulator_Accessor accumulator_accessor{ &signal_register }; // context only
detail::Vector_Accessor vector_accessor{ table_, get_frame_in_memory() };
detail::Register_Accessor register_accessor{ &signal_register };
detail::Address_Accessor address_accessor{ table_, stack,
    flag_accessor, register_accessor, get_frame_in_memory() }; // + flags, registers
detail::Device_Accessor device_accessor{ address_accessor,
    table_accessor, get_frame_in_memory(), stack }; // + addresses, table
```

  
The Aggregate:

```C++
class Memory_Accessor final : public ARM64_Memory_Accessor
{
  public:
    Memory_Accessor() = delete;

    // the accessors refer to members of this object, so a copy or a move
    // would refer to the members of its source
    Memory_Accessor(Memory_Accessor const&) = delete;
    Memory_Accessor& operator=(Memory_Accessor const&) = delete;
    Memory_Accessor(Memory_Accessor&&) = delete;
    Memory_Accessor& operator=(Memory_Accessor&&) = delete;

    explicit Memory_Accessor(Table_Pointer table,
        ir::Instructions_PTR instructions,
        Stack_Pointer stack_pointer)
        : ARM64_Memory_Accessor(table)
        , table_(std::move(table))
        , instructions_(std::move(instructions))
        , stack(std::move(stack_pointer))
    {
        instruction_accessor = std::make_shared<detail::Instruction_Accessor>();
    }

  // ...

  public:
    bool is_lvalue_allocated_in_memory(LValue const& lvalue);
    Device get_operand_rvalue_device(RValue const& rvalue);
    Device get_device_by_lvalue(LValue const& lvalue);     
    Register get_available_storage_register(Operand_Size size);
    void insert_lvalue_to_device(LValue const& lvalue, Operand_Size size);
    void insert_lvalue_to_device(LValue const& lvalue, SET_INLINE_DEBUG);
    void set_vector_offset_to_storage_space(LValue const& lvalue);
    bool is_address_taken(LValue const& lvalue);
    Size get_size_from_rvalue_reference(RValue const& rvalue);
    Size get_size_from_typed_rvalue(LValue const& lvalue,
        Immediate const& rvalue);
        
  // ...

  private:
    Register signal_register = assembly::Register::wzr;
    Table_Pointer table_;
    ir::Instructions_PTR instructions_;  
    Stack_Pointer stack;    
    detail::Flag_Accessor flag_accessor{};
    detail::Table_Accessor table_accessor{ table_, instructions_ };
    detail::Accumulator_Accessor accumulator_accessor{ &signal_register };
    detail::Vector_Accessor vector_accessor{ table_, get_frame_in_memory() };
    detail::Register_Accessor register_accessor{ &signal_register };
    detail::Address_Accessor address_accessor{ table_,
        stack,
        flag_accessor,
        register_accessor,
        get_frame_in_memory() };
    detail::Device_Accessor device_accessor{ address_accessor,
        table_accessor,
        get_frame_in_memory(),
        stack };
    Instruction_Pointer instruction_accessor{};
};
```

  
And behold, a type that lets me query the storage of any address in memory. The memory accessors are not the only place I used this pattern. In a follow-up post, I may write about the other types, such as the `Inserters`. For more, see the [ARM64 memory accessor in Credence](https://github.com/jahan-addison/Brocoli/blob/master/credence/target/arm64/memory.h?ref=jahans.blog). You should also check out how I made an interface and used the pattern across ARM64 and x86-64 [here](https://github.com/jahan-addison/Brocoli/blob/master/credence/target/common/accessor.h?ref=jahans.blog).