Commit Graph

14811 Commits

Author SHA1 Message Date
Andrew Kelley
dc88864c97 stage2: implement @boolToInt
This is the first commit in which some behavior tests are passing for
both stage1 and stage2.
2021-07-27 17:08:37 -07:00
Andrew Kelley
66e5920dc3 llvm backend: LLVMGetNamedGlobalAlias requires a null terminated string 2021-07-27 15:47:25 -07:00
Andrew Kelley
a2eb91c422 stage2: add deinit for test_functions 2021-07-27 15:44:30 -07:00
Andrew Kelley
1eeafc3967 stage2: move call to populateTestFunctions() outside performAllTheWork()
Before calling populateTestFunctions() we want to check
totalErrorCount() but that will read from some tables that might get
populated by the thread pool for C compilation tasks. So we wait until
all those tasks are finished before proceeding.
2021-07-27 15:10:49 -07:00
Andrew Kelley
a8e964eadd stage2: zig test now works with the LLVM backend
Frontend improvements:

 * When compiling in `zig test` mode, put a task on the work queue to
   analyze the main package root file. Normally, start code does
   `_ = import("root");` to make Zig analyze the user's code, however in
   the case of `zig test`, the root source file is the test runner.
   Without this change, no tests are picked up.
 * In the main pipeline, once semantic analysis is finished, if there
   are no compile errors, populate the `test_functions` Decl with the
   set of test functions picked up from semantic analysis.
 * Value: add `array` and `slice` Tags.

LLVM backend improvements:

 * Fix incremental updates of globals. Previously the
   value of a global would not get replaced with a new value.
 * Fix LLVM type of arrays. They were incorrectly sending
   the ABI size as the element count.
 * Remove the FuncGen parameter from genTypedValue. This function is for
   generating global constants and there is no function available when
   it is being called.
   - The `ref_val` case is now commented out. I'd like to eliminate
     `ref_val` as one of the possible Value Tags. Instead it should
     always be done via `decl_ref`.
 * Implement constant value generation for slices, arrays, and structs.
 * Constant value generation for functions supports the `decl_ref` tag.
2021-07-27 14:19:53 -07:00
Jarred Sumner
ba71b96fe6 Add zld.id to list of files to skip 2021-07-27 07:54:25 +02:00
Andrew Kelley
31a59c229c stage2: improvements towards zig test
* Add AIR instruction: struct_field_val
   - This is part of an effort to eliminate the AIR instruction `ref`.
   - It's implemented for C backend and LLVM backend so far.
 * Rename `resolvePossiblyUndefinedValue` to `resolveMaybeUndefVal` just
   to save some columns on long lines.
 * Sema: add `fieldVal` alongside `fieldPtr` (renamed from
   `namedFieldPtr`). This is part of an effort to eliminate the AIR
   instruction `ref`. The idea is to avoid unnecessary loads, stores,
   stack usage, and IR instructions, by paying a DRY cost.

LLVM backend improvements:

 * internal linkage vs exported linkage is implemented, along with
   aliases. There is an issue with incremental updates due to missing
   LLVM API for deleting aliases; see the relevant comment in this commit.
   - `updateDeclExports` is hooked up to the LLVM backend now.
 * Fix usage of `Type.tag() == .noreturn` rather than calling `isNoReturn()`.
 * Properly mark global variables as mutable/constant.
 * Fix llvm type generation of function pointers
 * Fix codegen for calls of function pointers
 * Implement llvm type generation of error unions and error sets.
 * Implement AIR instructions: addwrap, subwrap, mul, mulwrap, div,
   bit_and, bool_and, bit_or, bool_or, xor, struct_field_ptr,
   struct_field_val, unwrap_errunion_err, add for floats, sub for
   floats.

After this commit, `zig test` on a file with `test "example" {}`
correctly generates and executes a test binary. However the
`test_functions` slice is undefined and just happens to be going into
the .bss section, causing the length to be 0. The next step towards
`zig test` will be replacing the `test_functions` Decl Value with the
set of test function pointers, before it is sent to linker/codegen.
2021-07-26 19:27:49 -07:00
Robin Voetter
cdeea3b094 minimum/maximum builtins 2021-07-26 20:41:00 -04:00
Robin Voetter
50a29f7c21 Add @select
@select(
    comptime T: type,
    pred: std.meta.Vector(len, bool),
    a: std.meta.Vector(len, T),
    b: std.meta.Vector(len, T)
) std.meta.Vector(len, T)

Constructs a vector from a & b, based on the values in the predicate vector. For indices where the predicate value is true, the corresponding
element from the a vector is selected, and otherwise from b.
2021-07-26 20:05:48 -04:00
Andrew Kelley
fc105f2681
Merge pull request #9458 from SuperAuguste/popcount-ctz-clz
Vector support for `@popCount`, `@ctz`, and `@clz`
2021-07-26 19:15:27 -04:00
Takeshi Yoneda
a9a4fd3200 Wasm,libc: fix wasm-ld failure in matching libc symbols.
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2021-07-26 17:28:36 -04:00
fn ⌃ ⌥
24bfd7bddd Use -isysroot on Mojave too 2021-07-26 17:46:11 +02:00
Andrew Kelley
14d8a1c10d stage2 llvm backend improvements working towards zig test
* properly set global variables to const if they are not a global
   variable.
 * implement global variable initializations.
 * initial implementation of llvmType() for structs and functions.
 * implement genTypedValue for variable tags
 * implement more AIR instructions: varptr, slice_ptr, slice_len,
   slice_elem_val, ptr_slice_elem_val, unwrap_errunion_payload,
   unwrap_errunion_payload_ptr, unwrap_errunion_err,
   unwrap_errunion_err_ptr.
2021-07-25 22:38:50 -07:00
Auguste Rame
dd796154be Doc fixes for clz + ctz 2021-07-25 22:58:58 -04:00
Auguste Rame
c619b85f67 ctz + clz 2021-07-25 22:43:52 -04:00
Andrew Kelley
c3d10dbda1 stage2 llvm backend: implement llvmType for error union and slices 2021-07-25 19:06:48 -07:00
Andrew Kelley
5b4885fe89 stage2 llvm backend: DeclGen and DeclFn have context field
instead of a context() accessor method.
2021-07-25 18:49:25 -07:00
Andrew Kelley
66986dd248 stage2 llvm backend: rename getLLVMType to llvmType 2021-07-25 18:46:19 -07:00
Auguste Rame
ecca829bcb Add vector support for @popCount 2021-07-25 20:35:55 -04:00
Andrew Kelley
b87105c921 stage2 llvm backend: implement assembly and ptrtoint
These AIR instructions are the next blockers for `zig test` to work for
this backend.

After this commit, the "hello world" x86_64 test case passes for the
LLVM backend as well.
2021-07-24 23:54:20 -07:00
Andrew Kelley
653c851e62
Merge pull request #9446 from Luukdegram/stage2-air-wasm
stage2: wasm - Use `br_table` when possible for switch
2021-07-24 21:06:52 -04:00
Luuk de Gram
30376a82b2
Re-enable switch test cases and fix regressions 2021-07-24 20:05:41 +02:00
Luuk de Gram
5d98abd570
Support multi-value prongs 2021-07-24 19:49:25 +02:00
Luuk de Gram
72149ae7e4
Allow negative values 2021-07-24 19:49:25 +02:00
Luuk de Gram
cb41f0e58d
switchbr: When prongs are sparse values, use if/else-chain 2021-07-24 19:49:25 +02:00
Luuk de Gram
ad38fc1147
wasm: Rewrite switch_br to use br_table instead
This is an initial version, todo:
- Also make this work for u64 values, as the table must be indexed by u32.
- Add support for signed integers.
- Add support for enums.
2021-07-24 19:49:24 +02:00
Andrew Kelley
7b8cb881df stage2: improvements towards zig test
* There is now a main_pkg in addition to root_pkg. They are usually the
   same. When using `zig test`, main_pkg is the user's source file and
   root_pkg has the test runner.
 * scanDecl no longer looks for test decls outside the package being
   tested. honoring `--test-filter` is still TODO.
 * test runner main function has a void return value rather than
   `anyerror!void`
 * Sema is improved to generate better AIR for for loops on slices.
 * Sema: fix incorrect capacity calculation in zirBoolBr
 * Sema: add compile errors for trying to use slice fields as an lvalue.
 * Sema: fix type coercion for error unions
 * Sema: fix analyzeVarRef generating garbage AIR
 * C codegen: fix renderValue for error unions with 0 bit payload
 * C codegen: implement function pointer calls
 * CLI: fix usage text

 Adds 4 new AIR instructions:

  * slice_len, slice_ptr: to get the ptr and len fields of a slice.
  * slice_elem_val, ptr_slice_elem_val: to get the element value of
    a slice, and a pointer to a slice.

AstGen gains a new functionality:

 * One of the unused flags of struct decls is now used to indicate
   structs that are known to have non-zero size based on the AST alone.
2021-07-23 22:42:31 -07:00
Jakub Konka
f9798108f8
Merge pull request #9387 from ziglang/zld-incremental-2
macho+zld: bringing zld to stage2 MachO backend - simplicity is key!
2021-07-23 20:03:15 +02:00
Jakub Konka
5533f77054 Merge remote-tracking branch 'origin/master' into zld-incremental-2 2021-07-23 17:06:19 +02:00
Jakub Konka
1beda818e1 macho: re-enable parsing sections into atoms
However, make it default only when building in release modes since
it's a prelude to advanced dead code stripping not very useful in
debug.
2021-07-23 16:55:19 +02:00
David May
e5b476209a
Docs fix array/pointer/slice type coercion section (#9392)
* removed deprecated coercion: [X]T => [] const T

* Fixed tests and added desc for first test

* Improved heading
2021-07-23 09:32:20 +03:00
Veikka Tuominen
8ad23d7beb
Merge pull request #9265 from ehaas/translate-c-macro-matching
translate-c: add framework for special-casing macros
2021-07-23 09:29:25 +03:00
Andrew Kelley
e3fe3acce0
Merge pull request #9440 from ziglang/emit-bc
add -femit-llvm-bc CLI option and implement it, and improve -fcompiler-rt support
2021-07-23 02:22:23 -04:00
Alex Rønne Petersen
a38a691487 zig: -rdynamic now implies -fdll-export-fns unless the latter is explicitly set.
Fixes #9340.
2021-07-22 22:59:05 -04:00
Andrew Kelley
c56b30f565
Merge pull request #9378 from g-w1/loop-shadowing
astgen: errors for shadowing in captures
2021-07-22 22:57:12 -04:00
Andrew Kelley
80ba9f060d fix double linking of compiler-rt symbols on wasm
The include_compiler_rt stored in the bin file options means that we need
compiler-rt symbols *somehow*. However, in the context of using the stage1 backend
we need to tell stage1 to include compiler-rt only if stage1 is the place that
needs to provide those symbols. Otherwise the stage2 infrastructure will take care
of it in the linker, by putting compiler_rt.o into a static archive, or linking
compiler_rt.a against an executable. In other words we only want to set this flag
for stage1 if we are using build-obj.
2021-07-22 19:51:32 -07:00
Andrew Kelley
7c25390c95 support -fcompiler-rt in conjunction with build-obj
When using `build-exe` or `build-lib -dynamic`, `-fcompiler-rt` means building
compiler-rt into a static library and then linking it into the executable.

When using `build-lib`, `-fcompiler-rt` means building compiler-rt into an
object file and then adding it into the static archive.

Before this commit, when using `build-obj`, zig would build compiler-rt
into an object file, and then on ELF, use `lld -r` to merge it into the
main object file. Other linker backends of LLD do not support `-r` to
merge objects, so this failed with error messages for those targets.

Now, `-fcompiler-rt` when used with `build-obj` acts as if the user puts
`_ = @import("compiler_rt");` inside their root source file. The symbols
of compiler-rt go into the same compilation unit as the root source file.

This is hooked up for stage1 only for now. Once stage2 is capable of
building compiler-rt, it should be hooked up there as well.
2021-07-22 19:51:32 -07:00
Andrew Kelley
a5fb28070f add -femit-llvm-bc CLI option and implement it
* Added doc comments for `std.Target.ObjectFormat` enum
 * `std.Target.oFileExt` is removed because it is incorrect for Plan-9
   targets. Instead, use `std.Target.ObjectFormat.fileExt` and pass a
   CPU architecture.
 * Added `Compilation.Directory.joinZ` for when a null byte is desired.
 * Improvements to `Compilation.create` logic for computing `use_llvm`
   and reporting errors in contradictory flags. `-femit-llvm-ir` and
   `-femit-llvm-bc` will now imply `-fLLVM`.
 * Fix compilation when passing `.bc` files on the command line.
 * Improvements to the stage2 LLVM backend:
   - cleaned up error messages and error reporting. Properly bubble up
     some errors rather than dumping to stderr; others turn into panics.
   - properly call ZigLLVMCreateTargetMachine and
     ZigLLVMTargetMachineEmitToFile and implement calculation of the
     respective parameters (cpu features, code model, abi name, lto,
     tsan, etc).
   - LLVM module verification only runs in debug builds of the compiler
   - use LLVMDumpModule rather than printToString because in the case
     that we incorrectly pass a null pointer to LLVM it may crash during
     dumping the module and having it partially printed is helpful in
     this case.
   - support -femit-asm, -fno-emit-bin, -femit-llvm-ir, -femit-llvm-bc
   - Support LLVM backend when used with Mach-O and WASM linkers.
2021-07-22 19:51:32 -07:00
Jakub Konka
a4feb97cdf macho: assign and cache section ordinals upon creation
then, when sorting sections within segments, clear and redo the
ordinals since we re-apply them to symbols anyway. It is vital
to have the ordinals consistent with parsing and resolving relocs
however.
2021-07-22 23:13:13 +02:00
Evan Haas
b33efa3739
translate-c: Handle ambiguous cast or call macro
Fixes #9425
2021-07-22 11:50:12 -07:00
Evan Haas
dc4fa83dd7
translate-c: add framework for special-casing macros
Some macros (for example any macro that uses token pasting) cannot be
directly translated to Zig, but may nevertheless still admit a Zig
implementation. This provides a mechanism for matching macros against
templates and mapping them to functions implemented in c_translation.zig.

A macro matches a template if it contains the same sequence of tokens, except
that the name and parameters may be renamed. No attempt is made to
semantically analyze the macro. For example the following two macros are
considered equivalent:

```C
```

But the following two are not:

```C
```
2021-07-22 08:09:46 -07:00
Jakub Konka
4fd0cb7618 macho: sort nlists within object before filtering by type
Previously, we'd filter the nlists assuming they were correctly
ordered by type: local < extern defined < undefined within the
object's symbol table but this doesn't seem to be guaranteed,
therefore, we sort by type and address in one go, and filter
defined from undefined afterwards.
2021-07-22 16:02:31 +02:00
Jakub Konka
773863150a macho: fix incorrect prealloc in traditional path 2021-07-22 14:50:06 +02:00
Jakub Konka
ca90efe88e macho: fix memory leaks when emptying TextBlocks
This happens on every call to `TextBlock.empty` by the `Module`.
2021-07-22 14:05:12 +02:00
Jakub Konka
def1359187 Merge remote-tracking branch 'origin/master' into zld-incremental-2 2021-07-22 09:34:44 +02:00
Michal Ziulek
18b8738069
Fixed compile error: 'bMenu' needs to casted. (#9426) 2021-07-22 01:28:21 +02:00
Biolunar
093e10b66e
linux stdlib: fix definition of RW flags (#9428) 2021-07-22 01:27:59 +02:00
Frank Denis
680fa880d6
std.crypto: handle the top bit in 25519.field.fromBytes64() (#9435)
The only known use case for this is the hash-to-curve operation where the top bit is always cleared.

But the function is public, so let's make it work as one would expect in the general case.

Also fix the comment by the way.
2021-07-22 01:27:42 +02:00
Jakub Konka
d0edd37f69 macho: fix bug when freeing Decl
Take into account that an already freed Decl will no longer be
available as `decl.link.macho` causing a potential "inactive union field"
panic.
2021-07-21 23:38:20 +02:00
Jakub Konka
e05b1e0e07 macho: fix reloc generation for stubs and GOT entries
The current approach is somewhat hacky, however, works well for
one-off self-hosted linking.
2021-07-21 23:05:03 +02:00