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

stage2: blaze the trail for std lib integration #8470

Merged
merged 17 commits into from Apr 9, 2021
Merged

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Apr 9, 2021

Based on work by @FireFox317 in #8447.

This branch adds "builtin" and "std" to the import table when using the
self-hosted backend.

"builtin" gains one additional item:

pub const zig_is_stage2 = true; // false when using stage1 backend

This allows the std lib to do conditional compilation based on detecting
which backend is being used. This will be removed from builtin as soon
as self-hosted catches up to feature parity with stage1.
Keep a sharp eye out - people are going to be tempted to abuse this.
The general rule of thumb is do not use builtin.zig_is_stage2. However
this commit breaks the rule so that we can gain limited start.zig support
as we incrementally improve the self-hosted compiler.

This commit also implements fullyQualifiedNameHash and related
functionality, which effectively puts all Decls in their proper
namespaces. fullyQualifiedName is not yet implemented.

Stop printing "todo" log messages for test decls unless we are in test
mode.

Add "previous definition here" error notes for Decl name collisions.

This commit does not bring us yet to a newly passing test case.

Here's what I'm working towards:

const std = @import("std");

export fn main() c_int {
    const a = std.fs.base64_alphabet[0];
    return a - 'A';
}

Current output:

$ ./zig-cache/bin/zig build-exe test.zig
test.zig:3:1: error: TODO implement more analyze elemptr
zig-cache/lib/zig/std/start.zig:38:46: error: TODO implement structInitExpr ty

So the next steps are clear:

  • Sema: improve elemptr
  • AstGen: implement structInitExpr

FireFox317 and others added 9 commits April 8, 2021 14:20
A global comptime block did not end with a break_inline instruction
which caused an assertion to be hit.
std.builtin.ExportOptions is not yet supported, thus the second argument
of export is now a simple string that specifies the exported symbol
name.
We currently emit a simplified builtin2.zig that is able to be parsed
and correctly interpreted in stage2.
Fix some infinite recursions, because the code assumed that packages
cannot point to each other. But this assumption does not hold anymore.
This adds a simplified start2.zig that the current stage2 compiler is
able to generate code for.
The following code caused an assertion to be hit:
```
pub fn main() void {
    var e: anyerror!c_int = error.Foo;
    const i = e catch 69;
    assert(69 - i == 0);
}
```
Reverting most of the code from the previous commits in this branch.
Will pull in the code with modifications bit by bit.
This branch adds "builtin" and "std" to the import table when using the
self-hosted backend.

"builtin" gains one additional item:

```
pub const zig_is_stage2 = true; // false when using stage1 backend
```

This allows the std lib to do conditional compilation based on detecting
which backend is being used. This will be removed from builtin as soon
as self-hosted catches up to feature parity with stage1.
Keep a sharp eye out - people are going to be tempted to abuse this.
The general rule of thumb is do not use `builtin.zig_is_stage2`. However
this commit breaks the rule so that we can gain limited start.zig support
as we incrementally improve the self-hosted compiler.

This commit also implements `fullyQualifiedNameHash` and related
functionality, which effectively puts all Decls in their proper
namespaces. `fullyQualifiedName` is not yet implemented.

Stop printing "todo" log messages for test decls unless we are in test
mode.

Add "previous definition here" error notes for Decl name collisions.

This commit does not bring us yet to a newly passing test case.

Here's what I'm working towards:

```zig
const std = @import("std");

export fn main() c_int {
    const a = std.fs.base64_alphabet[0];
    return a - 'A';
}
```

Current output:

```
$ ./zig-cache/bin/zig build-exe test.zig
test.zig:3:1: error: TODO implement more analyze elemptr
zig-cache/lib/zig/std/start.zig:38:46: error: TODO implement structInitExpr ty
```

So the next steps are clear:
 * Sema: improve elemptr
 * AstGen: implement structInitExpr
@andrewrk
Copy link
Member Author

andrewrk commented Apr 9, 2021

Here's where we're at after the latest commits:

$ ./zig-cache/bin/zig build-exe test.zig -ofmt=c
error(module): TODO: analyze usingnamespace decl
/home/andy/dev/zig/build/zig-cache/o/3bede4785089a42c29ed3ce42a91b97f/builtin.zig:12:25: error: use of undeclared identifier 'OutputMode'

I'll go ahead and implement usingnamespace now, and then I think this should work!

g-w1 and others added 2 commits April 9, 2021 02:11
Remove -femit-zir as we aren't going to need it. Also remove zir test code
This removes a TODO that asserts the file is not zir.
@andrewrk andrewrk marked this pull request as ready for review April 9, 2021 17:13
@andrewrk andrewrk merged commit f75cdd1 into master Apr 9, 2021
@andrewrk andrewrk deleted the stage2-start branch April 9, 2021 17:15
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.

None yet

3 participants