Commit Graph

3539 Commits (5e345ff0ee9eb70d7f5f7167227f6a1e8903f428)

Author SHA1 Message Date
Andrew Kelley 10e172b1d7
add `other` OS and fix regression in build-exe for freestanding 2019-12-04 14:42:40 -05:00
Andrew Kelley e3ebaab3c7
fix `zig builtin` 2019-12-03 13:57:13 -05:00
Andrew Kelley a91f552393
WinMainCRTStartup implies defaulting to console subsystem 2019-12-03 12:52:28 -05:00
Andrew Kelley 6a046c1bcd
activate start code when pub main exists
and rename LinkType->LinkMode, OutType->OutputMode
2019-12-03 12:50:42 -05:00
Vexu 20bcdab462
correct caching and add test for missing fn name 2019-12-03 12:50:41 -05:00
Vexu a0ca30ce01
move more startup code to std lib 2019-12-03 12:50:18 -05:00
LemonBoy fd7c7be33c Pick up WinMain with proper CC 2019-12-03 12:49:08 -05:00
LemonBoy 26c8930b95 Accept comptime-known expression for asm 2019-12-02 19:53:06 -05:00
Andrew Kelley ad214c7aa0
bring your own OS layer in the std lib
closes #3784
2019-12-02 15:02:17 -05:00
Andrew Kelley dcbd5ad155
remove upstream support for Zen hobby OS
The new plan to support hobby operating systems is #3784.

And what kind of name is "Zen" anyway? There's already a
[Zen programming language](http://zenlang.sourceforge.net/)
and that's just confusing.
2019-12-02 12:01:49 -05:00
Andrew Kelley 6d8550a7df
fix crash assigning optional struct with anon literal
closes #3827
2019-12-02 01:09:06 -05:00
Andrew Kelley e7ee6647a1
fix invalid check for fn_inline property 2019-12-01 23:56:28 -05:00
Andrew Kelley 37caa56fbc
fix docs regressions 2019-12-01 21:27:55 -05:00
Andrew Kelley c32e50f505
fix regressions in compile error tests 2019-12-01 20:53:24 -05:00
Andrew Kelley 080316cd4f
fix assigning to an unwrapped optional field in an inline loop 2019-12-01 18:55:35 -05:00
Andrew Kelley c2cee40aec
add advanced IR debugging support
and use it to improve copy_const_val with regards to parent backrefs
2019-12-01 17:09:11 -05:00
Andrew Kelley 78811ff4ac
Merge remote-tracking branch 'origin/kill-ConstGlobalRefs' into remove-array-type-coercion 2019-12-01 09:58:32 -05:00
Andrew Kelley b36c07a95a
Merge remote-tracking branch 'origin/master' into remove-array-type-coercion 2019-12-01 09:56:01 -05:00
Andrew Kelley 4b6740e19d
sometimes free stuff from Zig IR pass 1
Total bytes used in stage1 std lib tests:
3.418 -> 3.198 GiB (saving 225 MiB)

There's still this from pass 1 not getting freed:
Const: 6909049 items, 72 bytes each, total 474.407 MiB

This is due to 2 things hanging on to references to IrAnalyze pointers:
 * ZigVar->owner_exec->analysis
 * LazyValue->ira

The LazyValue one could be solved by memoizing the results after the
lazy value is resolved, and then it could unref the IrAnalyze.

ZigVars that are determined to be comptime const, could have their
const_value set to that value, instead of using the mem_slot_index
mechanism. This would prevent an IrAnalyze ref in some cases.
2019-12-01 01:13:21 -05:00
Andrew Kelley b5df18c8fd
inline ConstGlobalRefs into ZigValue
Having ConstGlobalRefs be a pointer in ZigValue was a hack that caused
plenty of bugs. It was used to work around difficulties in type coercing
array values into slices.

However, after #3787 is merged, array values no longer type coerce into
slices, and so this provided an opportunity to clean up the code.

This has the nice effect of reducing stage1 peak RAM usage during the
std lib tests from 3.443 GiB to 3.405 GiB (saving 39 MiB).

There is one behavior test failing in this branch, which I plan to debug
after merging #3787.
2019-12-01 00:29:16 -05:00
Andrew Kelley 5026b1aad5
free IrAnalyze sometimes
Total bytes used in stage1 std lib tests:
3.443 GiB -> 3.418 GiB
2019-11-30 23:57:48 -05:00
Andrew Kelley 85e1e3b95f
Merge pull request #3284 from Sahnvour/export_variables
Improved support for exporting variables
2019-11-30 00:25:52 -05:00
Andrew Kelley f980c29306
fix typo in error note for integer casting 2019-11-29 22:33:55 -05:00
Andrew Kelley 7278c51ddd
fix empty result location for parameters not working 2019-11-29 21:36:12 -05:00
Andrew Kelley 559bd27b08
fix `@bitCast` result coercing to error union by returning 2019-11-29 19:58:00 -05:00
Andrew Kelley 815b4cfd9d
fix return result loc as peer result loc in inferred error set function 2019-11-29 18:21:21 -05:00
Andrew Kelley bcdb3a9006
more progress 2019-11-28 00:02:53 -05:00
Andrew Kelley bf3ac66150
remove type coercion from array values to references
* Implements #3768. This is a sweeping breaking change that requires
   many (trivial) edits to Zig source code. Array values no longer
   coerced to slices; however one may use `&` to obtain a reference to
   an array value, which may then be coerced to a slice.

 * Adds `IrInstruction::dump`, for debugging purposes. It's useful to
   call to inspect the instruction when debugging Zig IR.

 * Fixes bugs with result location semantics. See the new behavior test
   cases, and compile error test cases.

 * Fixes bugs with `@typeInfo` not properly resolving const values.

 * Behavior tests are passing but std lib tests are not yet. There
   is more work to do before merging this branch.
2019-11-27 03:37:50 -05:00
Andrew Kelley 8ecd6c4d8c
add compiler note for bad int coercion
closes #3724
2019-11-26 19:50:52 -05:00
Vexu 379d547603
add missing cast to generic function call result 2019-11-26 14:32:37 -05:00
Vexu 0e405c5fc5
add missing cast to call result type 2019-11-26 14:32:34 -05:00
Michael Dusan d20df7df09
stage1: fix bad intern counting 2019-11-25 20:41:17 -05:00
Michael Dusan 6b7e1085e3
Merge pull request #3774 from mikdusan/stage1-intern-housekeeping
stage1: consolodate interning
2019-11-25 19:19:32 -05:00
Andrew Kelley 4261fa3c49
move logic to the appropriate layers; add new compile error 2019-11-25 18:46:17 -05:00
Michael Dusan 6c89f96df1
stage1: consolodate interning
- merge const_void_val → intern.x_void
- move const_zero_byte → intern.zero_byte
- wrap intern access
2019-11-25 17:18:56 -05:00
Michael Dusan a647a88dfc
const interning for 1-possible-value types 2019-11-25 15:04:49 -05:00
Michael Dusan 8f3e972da6
unembed ZigValue from IrInstruction 2019-11-25 15:04:39 -05:00
Michael Dusan acd95546b7
rename ConstExprValue → ZigValue 2019-11-25 15:04:29 -05:00
Andrew Kelley 29e438fd1f
more sentinel-terminated pointers std lib integration
See #3767
2019-11-25 00:43:36 -05:00
Andrew Kelley 15d415e10b
make std.mem.toSlice use null terminated pointers
and fix the fallout
2019-11-24 21:21:05 -05:00
Andrew Kelley 34b1ebefaa
Merge remote-tracking branch 'origin/master' into null-terminated-pointers 2019-11-24 20:28:46 -05:00
Andrew Kelley b9f88c3552
fix compile errors for array sentinels mismatching 2019-11-24 20:25:14 -05:00
Andrew Kelley 7eb5acdc80
fix casting `[N:x]T` to `[N]T` memcpying too many bytes 2019-11-24 18:57:07 -05:00
Andrew Kelley 217a5090ff
fix compile error regressions 2019-11-24 18:10:31 -05:00
Andrew Kelley c96d565166
add compile error for incompatible pointer sentinels 2019-11-24 17:02:56 -05:00
Andrew Kelley 09ec720dab
fix comptime `@ptrCast` of pointers to arrays 2019-11-24 16:25:26 -05:00
Sahnvour 6ebd26f3db removed unnecessary backslashes in windows' libc paths 2019-11-24 21:33:58 +01:00
Sahnvour 696567d9d7 export: check variable type
also fixed existing occurrences
2019-11-24 21:33:58 +01:00
Sahnvour 1bde3b6821 gen-h: register the need for stdbool.h and stdint.h when exploring types recursively
otherwise they wouldn't be included when only a struct contains them
2019-11-24 21:33:58 +01:00
Sahnvour 49156e3980 gen-h: add a pass for exported variables 2019-11-24 21:33:58 +01:00
Sahnvour 104b6f1629 refactored gen_h_file to improve maintainability and output
- extracted functions
- factorised extern "C" into a block containing all function prototypes instead of writing macros all over the place
- using intermediate buffers instead of writing directly to the output file
2019-11-24 21:33:58 +01:00
Andrew Kelley 44b1dc6db8
add type coercion: [:x]T to [*:x]T 2019-11-24 15:17:55 -05:00
Andrew Kelley 4c7b52503b
all tests passing 2019-11-24 02:14:21 -05:00
Andrew Kelley f25182f46d
structs can have fields with type `var`
behavior tests passing now
2019-11-23 17:51:37 -05:00
Vexu 89310dad56
Merge branch 'master' into modernize-stage2 2019-11-23 23:01:28 +02:00
Michael Dusan 29d7b5a80c remove UTF BOM 2019-11-23 14:17:55 -05:00
Vexu 03cc81665b
Merge branch 'master' into modernize-stage2 2019-11-23 19:13:48 +02:00
Andrew Kelley 7597735bad
update the stage1 implementation to the new proposal
See #3731
2019-11-23 04:45:35 -05:00
Vexu 4d9318cee0
fix missing implicit cast in return instruction 2019-11-21 23:21:51 -05:00
Vexu 0b63573674
improve broken llvm module error message 2019-11-21 23:21:14 -05:00
Timon Kruiper 94299d16d1 Stage1: Add compile error for an empty switch on a integer 2019-11-22 04:00:47 +00:00
Andrew Kelley fd6020c4e2
update tests, better error messages, update self-hosted tokenizer 2019-11-21 20:43:41 -05:00
Andrew Kelley cf2fe2536e
better error message when null termination does not match 2019-11-21 20:43:41 -05:00
Andrew Kelley 47f06be369
string literals are now null terminated
this also deletes C string literals from the language, and then makes
the std lib changes and compiler changes necessary to get the behavior
tests and std lib tests passing again.
2019-11-21 20:43:41 -05:00
Andrew Kelley 21f344b3b9
add null terminated pointers and arrays to self-hosted
as well as `@typeInfo` and `@Type`
2019-11-21 20:43:41 -05:00
Andrew Kelley 1aa978f32e
implement null terminated pointers 2019-11-21 20:43:41 -05:00
daurnimator ed956b5812 translate-c: add support for MacroQualified definitions 2019-11-19 01:05:17 +00:00
Andrew Kelley 57b8614a5a
Merge pull request #3697 from Vexu/container-docs
Implement container level doc comments
2019-11-17 22:31:12 +00:00
daurnimator 4e28d7a5f7 fix bug on empty error union 2019-11-17 22:29:49 +00:00
LemonBoy 924dd674e2 Catch invalid type from peer resolution
Fixes #3703
2019-11-17 22:25:14 +00:00
Vexu 6cddf9d723 properly parse anon literal in array 2019-11-17 22:24:21 +00:00
Vexu 977b613881
add container doc comments to generated docs 2019-11-15 15:17:59 +02:00
Vexu e509d21f39
implemented container doc comments in stage 1 2019-11-15 15:02:51 +02:00
LemonBoy a11da37734 Update discriminant value also for zero-sized unions
Fixes #3681
2019-11-14 17:07:20 +00:00
Andrew Kelley d89f39d719
rework layout of struct type fields
This removes the remaining hack in the implementation of anonymous
struct literals, and they can now therefore now have greater than 16
fields/elements.
2019-11-14 03:52:39 -05:00
Andrew Kelley f2f698a888
rework comptime struct value layout, removing 1/2 hacks
in the implementation of anonymous struct literals
2019-11-14 03:52:36 -05:00
Vexu b83ce08a3b add compile error for @atomicRmw on enum not being an .Xchg 2019-11-13 18:48:31 +00:00
Andrew Kelley 8bae70454d
Merge pull request #3675 from Vexu/atomic-store
Add @atomicStore builtin
2019-11-13 03:06:55 +00:00
Andrew Kelley 32b37e695a
fix anonymous struct literal assigned to variable
closes #3667
2019-11-12 21:57:25 -05:00
Andrew Kelley 37318bf151
fn parameters participate in result location semantics
See #3665
2019-11-12 18:55:17 -05:00
Vexu 41914321b4
fix comptime atomicStore and add tests 2019-11-13 01:32:16 +02:00
Vexu 110ef2e528
add @atomicStore builtin 2019-11-13 00:25:44 +02:00
Andrew Kelley e32b4829f4
Merge pull request #3670 from Vexu/atomics-enum
Support atomic operations with enums
2019-11-12 17:45:29 +00:00
Andrew Kelley 710ccacfa3
fix assertion failure in ptrToInt
found by building pluto
2019-11-12 12:21:20 -05:00
Vexu 7e5b234b8b
support atomic operations with enums 2019-11-12 17:13:33 +02:00
Andrew Kelley 45d2fd9b9d
fix assertion failure in init_const_undefined
found this from building my tetris game
2019-11-11 22:11:28 -05:00
Andrew Kelley bf8870a60b
fix unresolved type making it to codegen
found this trying to build oxid
2019-11-11 22:11:22 -05:00
Andrew Kelley b9482fc32d
implement fully anonymous list literals 2019-11-11 19:00:39 -05:00
Andrew Kelley ca2a788a24
fully anonymous struct literals 2019-11-11 17:50:41 -05:00
Andrew Kelley 1bca8e693d
fix anon literal used with return result loc 2019-11-11 13:18:16 -05:00
Andrew Kelley 5b27943498
implement anon struct literal syntax
This implements stage1 parser support for anonymous struct literal
syntax (see #685), as well as semantic analysis support for anonymous
struct literals and anonymous list literals (see #208). The semantic
analysis works when there is a type coercion in the result location;
inferring the struct type based on the values in the literal is not
implemented yet. Also remaining to do is zig fmt support for this new
syntax and documentation updates.
2019-11-11 13:11:58 -05:00
Andrew Kelley de30438ed2
stage1 parser code for anon container lit 2019-11-11 13:11:58 -05:00
Andrew Kelley ae0a219d1f
stop accepting deprecated `use` keyword
closes #2591
2019-11-11 13:02:39 -05:00
LemonBoy d18b5f8b53 Fix initialization of union references
Fixes #3532
2019-11-09 12:20:34 -05:00
Andrew Kelley 3cf5c2c62b
fix regressed tests and update docs to use "type coercion" 2019-11-08 15:57:25 -05:00
Andrew Kelley 3834d3dac0
passing std lib tests 2019-11-08 15:57:25 -05:00
Andrew Kelley a2acc27872
behavior tests passing 2019-11-08 15:57:25 -05:00
Andrew Kelley 8954a1bae5
more regressions fixed 2019-11-08 15:57:25 -05:00
Andrew Kelley fa34dfcce7
fix result loc of cast not finding parent 2019-11-08 15:57:24 -05:00
Andrew Kelley 2a6fbbd8fb
introduce `@as` builtin for type coercion
This commit also hooks up type coercion (previously called implicit
casting) into the result location mechanism, and additionally hooks up
variable declarations, maintaining the property that:

    var a: T = b;

is semantically equivalent to:

    var a = @as(T, b);

See #1757
2019-11-08 15:57:24 -05:00
Vexu 8edf27343f
self hosted compiler: fix zig_llvm.h function signature 2019-11-07 10:29:44 +02:00
Shawn Landden 2e52fafac5 correctly use llvm undef in release modes 2019-11-07 02:51:04 -05:00
LemonBoy 528908a06e
Fix ptrCast of array references to fn
Closes #3607
2019-11-07 02:46:10 -05:00
Andrew Kelley 6ee3cabe5c
allow type coercion from *[0]T to E![]const T
This is an unambiguous, safe cast.
2019-11-06 20:44:15 -05:00
Vexu ac6f0d245d fix NodeErrorSetDecl rendering 2019-11-06 18:15:39 -05:00
Brendan Hansknecht c1e8fdf812 add token for parsing pointer dereference 2019-11-06 14:03:21 -05:00
Vexu 6b61fcddfa fix translate-c arrow deref 2019-11-06 13:58:55 -05:00
Andrew Kelley 9a08de93b6
fix incorrect type name on bit field pointers 2019-11-05 13:37:43 -05:00
Andrew Kelley 9b4a529164
fix initialization of vector in a struct field 2019-11-05 12:36:39 -05:00
Andrew Kelley cbaa10fc3b
implement storing vector elements via runtime index 2019-11-05 12:11:57 -05:00
Andrew Kelley 70be308c43
implement loading vector elements via runtime index 2019-11-05 12:11:40 -05:00
Andrew Kelley 76d188551e
implement store of vector element with comptime index 2019-11-05 12:11:18 -05:00
Andrew Kelley ece8d6c2fa
comptime load of vector element 2019-11-05 12:11:18 -05:00
Andrew Kelley 55e54d98c4
runtime load vector element with comptime index 2019-11-05 12:11:16 -05:00
LemonBoy 71e209521a Mark type fields as CT
Fixes #3276
2019-11-05 12:05:01 -05:00
LemonBoy 9170dcb73f Fix #3558
Finding a suitable test case is left as an exercise to the reader.
2019-11-04 20:33:41 +01:00
LemonBoy c47211cc60 Prevent crash when slicing undefined ptr to slice
Fixes #3534
2019-11-04 20:33:41 +01:00
LemonBoy 2b4bf1e7ce Fix crash in #3483
The bytesToSlice is still not evaluated at comptime but at least it
doesn't crash anymore.
2019-11-04 20:33:40 +01:00
LemonBoy 61825062aa Correctly process errors for invalid types in fn call
Fixes #3544
2019-11-04 20:33:40 +01:00
LemonBoy 2f740fa19f Fix cmpxchg trying to execute at CT
Fixes #3582
2019-11-04 09:54:13 +01:00
Michael Dusan 10046f9a52 stage1: add linux XDG Base Directory support
- define zig global cache based on XDG spec:

    if env XDG_CACHE_HOME {
        "$XDG_CACHE_HOME/zig"
    } else {
        "$HOME/.cache/zig"
    }

- old definition "$HOME/.local/share/zig" is retired
- closes #3573
2019-11-02 18:30:07 -04:00
Andrew Kelley 790d439ce2
util.cpp: canonicalize the order of includes 2019-10-31 13:47:20 -04:00
Andrew Kelley 61d5a0bf48
Merge branch 'std.net' 2019-10-30 21:30:16 -04:00
Tse 33cc204481 DragonFlyBSD support 2019-10-30 21:21:58 -04:00
Andrew Kelley b3c80418a8
fix regression in behavior tests 2019-10-30 00:40:17 -04:00
Andrew Kelley c3d816a98e
std lib networking improvements, especially non-blocking I/O
* delete the std/event/net directory
 * `std.event.Loop.waitUntilFdReadable` and related functions
   no longer have possibility of failure. On Linux, they fall
   back to poll() and then fall back to sleep().
 * add some missing `noasync` decorations in `std.event.Loop`
 * redo the `std.net.Server` API. it's quite nice now, but
   shutdown does not work cleanly. There is a race condition with
   close() that I am actively working on.
 * move `std.io.OutStream` to its own file to match `std.io.InStream`.
   I started working on making `write` integrated with evented I/O,
   but it got tricky so I backed off and filed #3557. However
   I did integrate `std.os.writev` and `std.os.pwritev` with evented I/O.
 * add `std.Target.stack_align`
 * move networking tests to `lib/std/net/test.zig`
 * add `std.net.tcpConnectToHost` and `std.net.tcpConnectToAddress`.
 * rename `error.UnknownName` to `error.UnknownHostName` within the
   context of DNS resolution.
 * add `std.os.readv`, which is integrated with evented I/O.
 * `std.os.preadv`, is now integrated with evented I/O.
 * `std.os.accept4` now asserts that ENOTSOCK and EOPNOTSUPP never
    occur (misuse of API), instead of returning errors.
 * `std.os.connect` is now integrated with evented I/O.
   `std.os.connect_async` is gone. Just use `std.os.connect`.
 * fix false positive dependency loop regarding async function frames
 * add more compile notes to help when dependency loops occur
   in determining whether a function is async.
 * ir: change an assert to ir_assert to make it easier to find
   workarounds for when such an assert is triggered. In this case
   it was trying to parse an IPv4 address at comptime.
2019-10-29 22:59:30 -04:00
Andrew Kelley 4f594527c0
detect async fn recursion and emit compile error 2019-10-28 15:10:48 -04:00
Andrew Kelley f2d0d9820d
synchronize the target features for compiling C code
d91fc0fdd8 changed zig's behavior to
disable the SSE feature when cross compiling for i386-freestanding.

This commit does the same when compiling C Code.
2019-10-28 14:37:32 -04:00
Noam Preil d91fc0fdd8 Don't use SSE on freestanding 2019-10-26 16:46:43 -04:00
Noam Preil b8305b5648 Don't save/restore stack on newStackCall to noreturn function 2019-10-26 16:45:46 -04:00
Timon Kruiper d04c58816d Translate-c: Fix a segfault when to many errors are emitted
This was already fixed when doing `@cImport`, but not yet when
running `zig translate-c`.
2019-10-26 13:09:23 -04:00
Michael Dusan 3aeb1b115d stage1: fix compile error on macOS Xcode 11.2
src/codegen.cpp:7713:33: error: unused variable 'global_linkage_values' [-Werror,-Wunused-const-variable]
static const GlobalLinkageValue global_linkage_values[] = {
2019-10-25 17:49:01 -04:00
Andrew Kelley e219f0c786
fix regression in `zig run`
changes in 1dcf540426 added a "double
free" of the progress node, causing a segfault for `zig run` when there
is a terminal attached. fixed.
2019-10-25 13:03:12 -04:00
Andrew Kelley ad438a95c5
avoid passing -march=native when not supported
Clang does not support -march=native for all targets.
Arguably it should always work, but in reality it gives:
error: the clang compiler does not support '-march=native'
If we move CPU detection logic into Zig itelf, we will not need this,
instead we will always pass target features and CPU configuration explicitly.
For now, we simply avoid passing the flag when it is known to not be
supported.
2019-10-24 19:43:56 -04:00
Andrew Kelley f8bd1cd3b1
implement partial C ABI support for aarch64 2019-10-24 19:13:21 -04:00
Andrew Kelley 32c89531b1
Add missing compile error when ptr used instead of array
in array literals.

Fixes #3524
2019-10-24 16:23:33 -04:00
Andrew Kelley 17eb24a7e4
move types from builtin to std
* All the data types from `@import("builtin")` are moved to
  `@import("std").builtin`. The target-related types are moved
  to `std.Target`. This allows the data types to have methods, such as
  `std.Target.current.isDarwin()`.
 * `std.os.windows.subsystem` is moved to
   `std.Target.current.subsystem`.
 * Remove the concept of the panic package from the compiler
   implementation. Instead, `std.builtin.panic` is always the panic
   function. It checks for `@hasDecl(@import("root"), "panic")`,
   or else provides a default implementation.

This is an important step for multibuilds (#3028). Without this change,
the types inside the builtin namespace look like different types, when
trying to merge builds with different target settings. With this change,
Zig can figure out that, e.g., `std.builtin.Os` (the enum type) from one
compilation and `std.builtin.Os` from another compilation are the same
type, even if the target OS value differs.
2019-10-23 19:09:49 -04:00
Andrew Kelley e98e5dda52
implement safety for resuming non-suspended function
closes #3469
2019-10-22 23:43:27 -04:00
Andrew Kelley 1dcf540426
main.cpp: fix not ending progress node for zig build 2019-10-22 23:32:06 -04:00
Andrew Kelley f65b1d4680
integrate stage1 progress display with semantic analysis 2019-10-22 17:52:12 -04:00
Vexu 2550cb4638 remove pub syntax for container fields 2019-10-21 23:04:19 -04:00
LemonBoy ddb87237de Fix crash with peer type resolution & error unions
Fixes #3353
2019-10-20 22:03:39 -04:00
LemonBoy 771dafbaba Resolve lazy slice start/end values
Fixes #3489
2019-10-20 14:49:25 -04:00
LemonBoy 6f7939a452 Prevent too eager constant-folding of switch expression
A pointer was wrongly assumed to be comptime-available causing the
analysis pass to assume its initial value was constant.

Fixes #3481
2019-10-19 13:48:26 -04:00
LemonBoy bab93e7561 Fix crash when generating constant unions with single field 2019-10-19 13:47:49 -04:00
Andrew Kelley 4e98512344
reduce the size of IrInstruction by 8 bytes on 64 bit targets
This brings the std lib tests down from 3.51 GiB memory usage
to 3.41 GiB, by making two fields that were 64 bits 32 bits.
This is a small thing; the bigger wins will come from the strategy
outlined in the previous commit.
2019-10-19 03:51:04 -04:00
Andrew Kelley fa9f1d2396
add -DZIG_ENABLE_MEM_PROFILE option and -fmem-report flag
This can be used to find what's taking up memory in the compiler.
Here's an example of how to use it:

```
./zig test ../lib/std/std.zig --cache off -fmem-report
```

And here's the output I get for this on x86_64-linux-gnu today:

```
Const: 6462833 items, 152 bytes each, total 936.84 MiB
ConstGlobalRefs: 17236534 items, 24 bytes each, total 394.51 MiB
ResetResult: 1698108 items, 160 bytes each, total 259.11 MiB
ConstExprValue: 3118299 items, 80 bytes each, total 237.91 MiB
EndExpr: 1345395 items, 168 bytes each, total 215.56 MiB
Unknown_8: 27370821 items, 8 bytes each, total 208.82 MiB
VarPtr: 1127866 items, 168 bytes each, total 180.70 MiB
IrBasicBlock: 794834 items, 120 bytes each, total 90.96 MiB
LoadPtr: 554024 items, 160 bytes each, total 84.54 MiB
Unknown_64: 1245715 items, 64 bytes each, total 76.03 MiB
Unknown_40: 1879218 items, 40 bytes each, total 71.69 MiB
Unknown_72: 989117 items, 72 bytes each, total 67.92 MiB
Return: 423783 items, 160 bytes each, total 64.66 MiB
Unknown_168: 332480 items, 168 bytes each, total 53.27 MiB
Unknown_152: 336890 items, 152 bytes each, total 48.84 MiB
AddImplicitReturnType: 230819 items, 168 bytes each, total 36.98 MiB
Br: 217835 items, 168 bytes each, total 34.90 MiB
Unknown_184: 179529 items, 184 bytes each, total 31.50 MiB
FieldPtr: 179388 items, 184 bytes each, total 31.48 MiB
BinOp: 171004 items, 176 bytes each, total 28.70 MiB
LoadPtrGen: 173287 items, 168 bytes each, total 27.76 MiB
CondBr: 137864 items, 192 bytes each, total 25.24 MiB
Unknown_720: 30918 items, 720 bytes each, total 21.23 MiB
CallSrc: 99320 items, 216 bytes each, total 20.46 MiB
Unknown_160: 129243 items, 160 bytes each, total 19.72 MiB
Unknown_1: 19339456 items, 1 bytes each, total 18.44 MiB
CheckStatementIsVoid: 119838 items, 160 bytes each, total 18.29 MiB
Unknown_48: 371178 items, 48 bytes each, total 16.99 MiB
TestComptime: 101443 items, 160 bytes each, total 15.48 MiB
DeclVarSrc: 72578 items, 184 bytes each, total 12.74 MiB
StorePtr: 72776 items, 176 bytes each, total 12.22 MiB
ZigVar: 79201 items, 160 bytes each, total 12.09 MiB
Unknown_16: 770643 items, 16 bytes each, total 11.76 MiB
Phi: 60482 items, 184 bytes each, total 10.61 MiB
TestErrSrc: 66177 items, 168 bytes each, total 10.60 MiB
Unknown_240: 45164 items, 240 bytes each, total 10.34 MiB
ElemPtr: 58232 items, 184 bytes each, total 10.22 MiB
AllocaSrc: 60053 items, 176 bytes each, total 10.08 MiB
CallGen: 44873 items, 224 bytes each, total 9.59 MiB
SaveErrRetAddr: 63787 items, 152 bytes each, total 9.25 MiB
Unknown_112: 82283 items, 112 bytes each, total 8.79 MiB
AllocaGen: 51909 items, 176 bytes each, total 8.71 MiB
Unknown_24: 373599 items, 24 bytes each, total 8.55 MiB
ResultLocPeer: 113683 items, 72 bytes each, total 7.81 MiB
DeclRef: 36343 items, 168 bytes each, total 5.82 MiB
UnwrapErrPayload: 34603 items, 168 bytes each, total 5.54 MiB
Ref: 33414 items, 168 bytes each, total 5.35 MiB
Unknown_104: 53882 items, 104 bytes each, total 5.34 MiB
DeclVarGen: 32540 items, 168 bytes each, total 5.21 MiB
StructFieldPtr: 30449 items, 176 bytes each, total 5.11 MiB
UnwrapErrCode: 31508 items, 168 bytes each, total 5.05 MiB
Unknown_56: 90256 items, 56 bytes each, total 4.82 MiB
SpillBegin: 28722 items, 168 bytes each, total 4.60 MiB
SpillEnd: 28722 items, 160 bytes each, total 4.38 MiB
ResultLocReturn: 64573 items, 48 bytes each, total 2.96 MiB
PtrType: 14702 items, 184 bytes each, total 2.58 MiB
SliceType: 15005 items, 176 bytes each, total 2.52 MiB
Unknown_176: 13326 items, 176 bytes each, total 2.24 MiB
RefGen: 12881 items, 168 bytes each, total 2.06 MiB
UnOp: 12102 items, 176 bytes each, total 2.03 MiB
SwitchBr: 9453 items, 200 bytes each, total 1.80 MiB
TestErrGen: 11143 items, 160 bytes each, total 1.70 MiB
Unknown_32: 52359 items, 32 bytes each, total 1.60 MiB
CheckSwitchProngs: 9094 items, 184 bytes each, total 1.60 MiB
TypeOf: 9259 items, 160 bytes each, total 1.41 MiB
IntCast: 8772 items, 168 bytes each, total 1.41 MiB
OptionalUnwrapPtr: 8755 items, 168 bytes each, total 1.40 MiB
SwitchTarget: 9094 items, 160 bytes each, total 1.39 MiB
Cast: 8198 items, 176 bytes each, total 1.38 MiB
WidenOrShorten: 8448 items, 160 bytes each, total 1.29 MiB
ErrorUnion: 7613 items, 176 bytes each, total 1.28 MiB
SliceSrc: 6249 items, 192 bytes each, total 1.14 MiB
ErrWrapCode: 7133 items, 168 bytes each, total 1.14 MiB
TypeName: 7328 items, 160 bytes each, total 1.12 MiB
ImplicitCast: 5480 items, 176 bytes each, total 941.88 KiB
ResolveResult: 5638 items, 168 bytes each, total 924.98 KiB
ResultLocInstruction: 22696 items, 40 bytes each, total 886.56 KiB
BitCastSrc: 4947 items, 168 bytes each, total 811.62 KiB
CompileErr: 5148 items, 160 bytes each, total 804.38 KiB
ReturnPtr: 5305 items, 152 bytes each, total 787.46 KiB
Unreachable: 5038 items, 152 bytes each, total 747.83 KiB
TestNonNull: 4716 items, 160 bytes each, total 736.88 KiB
BitCastGen: 4431 items, 160 bytes each, total 692.34 KiB
PtrToInt: 4289 items, 160 bytes each, total 670.16 KiB
SliceGen: 3573 items, 192 bytes each, total 669.94 KiB
ArrayType: 4081 items, 168 bytes each, total 669.54 KiB
IntType: 3868 items, 168 bytes each, total 634.59 KiB
Unknown_88: 7213 items, 88 bytes each, total 619.87 KiB
Truncate: 3771 items, 168 bytes each, total 618.68 KiB
TypeInfo: 3740 items, 160 bytes each, total 584.38 KiB
SwitchVar: 3385 items, 176 bytes each, total 581.80 KiB
ContainerInitFields: 3223 items, 184 bytes each, total 579.13 KiB
ContainerInitList: 2309 items, 192 bytes each, total 432.94 KiB
PtrCastGen: 2626 items, 168 bytes each, total 430.83 KiB
BoolNot: 2457 items, 160 bytes each, total 383.91 KiB
FnProto: 2054 items, 184 bytes each, total 369.08 KiB
MergeErrSets: 1927 items, 176 bytes each, total 331.20 KiB
Unknown_136: 2486 items, 136 bytes each, total 330.17 KiB
Unknown_80: 4059 items, 80 bytes each, total 317.11 KiB
Bswap: 1670 items, 168 bytes each, total 273.98 KiB
TypeId: 1680 items, 160 bytes each, total 262.50 KiB
PtrCastSrc: 1371 items, 176 bytes each, total 235.64 KiB
ErrName: 1193 items, 160 bytes each, total 186.41 KiB
UnionTag: 1120 items, 160 bytes each, total 175.00 KiB
TagName: 1050 items, 160 bytes each, total 164.06 KiB
SizeOf: 942 items, 160 bytes each, total 147.19 KiB
MemberName: 871 items, 168 bytes each, total 142.90 KiB
Import: 881 items, 160 bytes each, total 137.66 KiB
PtrOfArrayToSlice: 758 items, 168 bytes each, total 124.36 KiB
UnionFieldPtr: 710 items, 176 bytes each, total 122.03 KiB
EnumToInt: 778 items, 160 bytes each, total 121.56 KiB
CheckRuntimeScope: 700 items, 168 bytes each, total 114.84 KiB
FieldParentPtr: 632 items, 184 bytes each, total 113.56 KiB
BoolToInt: 719 items, 160 bytes each, total 112.34 KiB
ResultLocPeerParent: 904 items, 104 bytes each, total 91.81 KiB
IntToPtr: 537 items, 168 bytes each, total 88.10 KiB
AlignOf: 561 items, 160 bytes each, total 87.66 KiB
AtomicRmw: 356 items, 208 bytes each, total 72.31 KiB
MemberCount: 441 items, 160 bytes each, total 68.91 KiB
Memset: 342 items, 176 bytes each, total 58.78 KiB
PopCount: 321 items, 168 bytes each, total 52.66 KiB
AlignCast: 251 items, 168 bytes each, total 41.18 KiB
IrInstruction *: 5230 items, 8 bytes each, total 40.86 KiB
IrBasicBlock *: 5230 items, 8 bytes each, total 40.86 KiB
TagType: 261 items, 160 bytes each, total 40.78 KiB
HasDecl: 234 items, 168 bytes each, total 38.39 KiB
OverflowOp: 191 items, 200 bytes each, total 37.30 KiB
Export: 209 items, 176 bytes each, total 35.92 KiB
SetCold: 219 items, 160 bytes each, total 34.22 KiB
ReturnAddress: 216 items, 152 bytes each, total 32.06 KiB
FromBytes: 178 items, 176 bytes each, total 30.59 KiB
SetRuntimeSafety: 188 items, 160 bytes each, total 29.38 KiB
OptionalWrap: 151 items, 168 bytes each, total 24.77 KiB
Clz: 143 items, 168 bytes each, total 23.46 KiB
ResizeSlice: 135 items, 168 bytes each, total 22.15 KiB
UnionInitNamedField: 106 items, 184 bytes each, total 19.05 KiB
Panic: 102 items, 160 bytes each, total 15.94 KiB
SwitchElseVar: 93 items, 168 bytes each, total 15.26 KiB
ToBytes: 89 items, 168 bytes each, total 14.60 KiB
IntToFloat: 78 items, 168 bytes each, total 12.80 KiB
Unknown_4360: 3 items, 4360 bytes each, total 12.77 KiB
ErrWrapPayload: 72 items, 168 bytes each, total 11.81 KiB
FloatOp: 62 items, 176 bytes each, total 10.66 KiB
FloatToInt: 47 items, 168 bytes each, total 7.71 KiB
FloatCast: 46 items, 168 bytes each, total 7.55 KiB
ErrToInt: 47 items, 160 bytes each, total 7.34 KiB
Asm: 33 items, 216 bytes each, total 6.96 KiB
ErrSetCast: 40 items, 168 bytes each, total 6.56 KiB
Memcpy: 34 items, 176 bytes each, total 5.84 KiB
AtomicLoad: 17 items, 184 bytes each, total 3.05 KiB
AwaitSrc: 16 items, 168 bytes each, total 2.62 KiB
Resume: 14 items, 160 bytes each, total 2.19 KiB
AwaitGen: 12 items, 176 bytes each, total 2.06 KiB
ArgType: 12 items, 168 bytes each, total 1.97 KiB
AnyFrameType: 12 items, 160 bytes each, total 1.88 KiB
SuspendFinish: 10 items, 160 bytes each, total 1.56 KiB
SuspendBegin: 10 items, 160 bytes each, total 1.56 KiB
Ctz: 9 items, 168 bytes each, total 1.48 KiB
FrameHandle: 8 items, 152 bytes each, total 1.19 KiB
SetEvalBranchQuota: 7 items, 160 bytes each, total 1.09 KiB
AssertZero: 7 items, 160 bytes each, total 1.09 KiB
UndeclaredIdent: 7 items, 160 bytes each, total 1.09 KiB
CmpxchgSrc: 5 items, 216 bytes each, total 1.05 KiB
CmpxchgGen: 5 items, 200 bytes each, total 1000.00 bytes
IntToEnum: 4 items, 168 bytes each, total 672.00 bytes
VectorType: 4 items, 168 bytes each, total 672.00 bytes
ErrorReturnTrace: 2 items, 160 bytes each, total 320.00 bytes
Breakpoint: 2 items, 152 bytes each, total 304.00 bytes
FrameAddress: 2 items, 152 bytes each, total 304.00 bytes
Unknown_4: 61 items, 4 bytes each, total 244.00 bytes
VectorToArray: 1 items, 168 bytes each, total 168.00 bytes
SetAlignStack: 1 items, 160 bytes each, total 160.00 bytes
Unknown_12: 2 items, 12 bytes each, total 24.00 bytes
ErrorTableEntry *: 0 items, 8 bytes each, total 0.00 bytes
AstNode *: 0 items, 8 bytes each, total 0.00 bytes
Total bytes used: 3.51 GiB
```

You can see that most of the memory is taken up by IR instructions,
as well as comptime values. This points toward 2 changes which will
greatly reduce memory usage:

 * Rework semantic analysis so that IR instructions can be freed.
   Currently the comptime value struct (ConstExprValue) is embedded
   directly into the IrInstruction struct. If this is made to be
   separate, at the very least pass 1 IR instructions can be freed.
   This includes `Const` which is the largest usage of memory currently.

 * Rework the ConstExprValue struct to no longer be a tagged union.
   For example, there's no need for an integer comptime value to be
   80 bytes.

From this you can also see that this eliminates some things from being
the culprit. Before doing this analysis, I considered whether doing
string interning would help. From the above output, you can see that all
strings in the compiler account for only 18 MiB, so string interning
would have been a dead end.
2019-10-19 03:23:23 -04:00
Andrew Kelley 2d5b2bf1c9
improve progress reporting
* use erase rest of line escape code.
 * use `stderr.supportsAnsiEscapeCodes` rather than `isTty`.
 * respect `--color off`
 * avoid unnecessary recursion
 * add `Progress.log`
 * disable the progress std lib test since it's noisy and uses
   `time.sleep()`.
 * enable/integrate progress printing with the default test runner
2019-10-17 21:55:49 -04:00
Andrew Kelley 299991019d
rework the progress module and integrate with stage1 2019-10-17 20:20:22 -04:00
Andrew Kelley 88b9579488
update musl src to v1.1.24 2019-10-17 11:55:43 -04:00