Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Aro as a C frontend #10795

Closed
wants to merge 3 commits into from
Closed

add Aro as a C frontend #10795

wants to merge 3 commits into from

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Feb 5, 2022

Problems to solve in this branch before merging:

  • Liveness and AIR printing want a ZIR instance. This issue is
    addressed by make AIR instruction for assembly not reference ZIR #10784.
  • linker: updateFunc / updateDecl want a Module instance which
    currently represents only Zig code. These functions should be changed
    to accept a Compilation, not a Module. The relevant state that it
    needed to touch on Module should be moved to Compilation instead.
  • Decl needs to be shared by both the C frontend and the Zig
    frontend. It currently has Zig-only fields and the functions to
    create Decl objects don't really fit the API that the C frontend
    needs.
  • A lot of the incremental compilation infrastructure doesn't quite
    match up, for example Decl objects being owned by Namespaces which is
    a Zig concept. Maybe there could be 1 Namespace globally across all C
    files and 1 more Namespace per C file for functions and globals
    declared static.
  • There is common code to be extracted from Sema that is shared with
    Aro frontend. All the helper functions having to do with the
    air_instructions and air_extra arrays.

There are many instances of calling @panic("TODO") that need to be
cleaned up.

There is a lot of compiler options that Zig is not communicating to Aro,
for example we do not pass the -D switches that we pass to Clang yet.
Need to audit addCCArgs and do the equivalent things for setting up
the Aro Compilation.

The Aro code is copied from https://github.com/Vexu/arocc, commit
7a0e54227e1ea2b2df8aa7bd2b7075f735109317. The only patches are to delete
Codegen and replace it with our own, and to delete main.zig, and add
Type and Value to lib.zig.

I believe the current plan is for main Aro development to happen
upstream on the arocc repository and periodically sync it downstream
with the Zig repository. It's up to @Vexu whether to keep that process
or change it in the future.

Problems to solve in this branch before merging:

 * Liveness and AIR printing want a ZIR instance. This issue is
   addressed by #10784.
 * linker: updateFunc / updateDecl want a Module instance which
   currently represents only Zig code. These functions should be changed
   to accept a Compilation, not a Module. The relevant state that it
   needed to touch on Module should be moved to Compilation instead.
 * `Decl` needs to be shared by both the C frontend and the Zig
   frontend. It currently has Zig-only fields and the functions to
   create Decl objects don't really fit the API that the C frontend
   needs.
 * A lot of the incremental compilation infrastructure doesn't quite
   match up, for example Decl objects being owned by Namespaces which is
   a Zig concept. Maybe there could be 1 Namespace globally across all C
   files and 1 more Namespace per C file for functions and globals
   declared `static`.
 * There is common code to be extracted from Sema that is shared with
   Aro frontend. All the helper functions having to do with the
   air_instructions and air_extra arrays.

There are many instances of calling `@panic("TODO")` that need to be
cleaned up.

There is a lot of compiler options that Zig is not communicating to Aro,
for example we do not pass the -D switches that we pass to Clang yet.
Need to audit `addCCArgs` and do the equivalent things for setting up
the Aro Compilation.

The Aro code is copied from https://github.com/Vexu/arocc, commit
7a0e54227e1ea2b2df8aa7bd2b7075f735109317. The only patches are to delete
Codegen and replace it with our own, and to delete main.zig, and add
Type and Value to lib.zig.

I believe the current plan is for main Aro development to happen
upstream on the arocc repository and periodically sync it downstream
with the Zig repository. It's up to @Vexu whether to keep that process
or change it in the future.
next problem is there needs to be a namespace because it's needed for
getting the file, for debug info source location purposes.
@andrewrk
Copy link
Member Author

andrewrk commented Feb 7, 2022

Current status on hello world:

$ ./zig-out/bin/zig build-exe hello.c -lc empty.zig --verbose-air
# Begin Function AIR: main:
# Total AIR+Liveness bytes: 257B
# AIR Instructions:         9 (81B)
# AIR Extra Data:           6 (24B)
# AIR Values Bytes:         4 (32B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      0 (0B)
# Liveness special table:   0 (0B)
  %0 = const_ty(*const fn([*c]i8, ...) callconv(.C) c_int)
  %1 = constant(*const fn([*c]i8, ...) callconv(.C) c_int, (decl ref))
  %2 = const_ty([15]u8)
  %3 = constant([15]u8, "Hello, World!\n\x00")
  %4 = const_ty(*const [15:0]u8)
  %5 = constant(*const [15:0]u8, (decl ref))
  %7 = constant(c_int, 0)

  %6!= call(%1!, [%5!])
  %8!= ret(%7!)
# End Function AIR: main

thread 489784 panic: Segmentation fault at address 0x8
/home/andy/dev/zig/src/Module.zig:751:34: 0x3faba9 in Module.Decl.getFileScope (zig)
        return decl.src_namespace.file_scope;
                                 ^
/home/andy/dev/zig/src/Module.zig:614:44: 0x59e852 in Module.Decl.nodeOffsetSrcLoc (zig)
            .file_scope = decl.getFileScope(),
                                           ^
/home/andy/dev/zig/src/Module.zig:609:37: 0x59a205 in Module.Decl.srcLoc (zig)
        return decl.nodeOffsetSrcLoc(0);
                                    ^
/home/andy/dev/zig/src/link/Elf.zig:2690:69: 0x706c57 in link.Elf.updateFunc (zig)
    const res = try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .{
                                                                    ^
/home/andy/dev/zig/src/link.zig:453:77: 0x6fde29 in link.File.updateFunc (zig)
            .elf   => return @fieldParentPtr(Elf,   "base", base).updateFunc(module, func, air, liveness),
                                                                            ^
/home/andy/dev/zig/src/aro/Codegen.zig:314:26: 0x739e16 in aro.Codegen.genFn (zig)
    c.bin_file.updateFunc(mod, module_fn, air, liveness) catch |err| switch (err) {
                         ^
/home/andy/dev/zig/src/aro/Codegen.zig:84:27: 0x7108b1 in aro.Codegen.generateTree (zig)
            => try c.genFn(decl_node),
                          ^
/home/andy/dev/zig/src/Compilation.zig:3222:33: 0x6fea91 in Compilation.compileWithAro (zig)
    try aro.Codegen.generateTree(comp, &aro_comp, tree, arena);
                                ^
/home/andy/dev/zig/src/Compilation.zig:2832:32: 0x58df44 in Compilation.processOneJob (zig)
            comp.compileWithAro(c_source_file) catch |err| switch (err) {
                               ^
/home/andy/dev/zig/src/Compilation.zig:2691:30: 0x57a716 in Compilation.performAllTheWork (zig)
            try processOneJob(self, work_item, main_progress_node);
                             ^

I'm starting to wonder... should we perhaps lower to ZIR instead of AIR? The integration would be a lot less messy, and it's much more obvious how to parallelize it. One .c file would produce exactly one corresponding ZIR file. 🤔 🤔 🤔

@andrewrk
Copy link
Member Author

Keeping the branch alive but closing the PR as I don't intend to merge this any time soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant