1260 Commits

Author SHA1 Message Date
Andrew Kelley
7d70774fde stage2: fix memory leak with exported decl name 2020-08-04 12:15:47 -07:00
Andrew Kelley
0d696a48da stage2 .debug_line: handle Decl line numbers changing 2020-08-04 00:22:11 -07:00
Andrew Kelley
30ee08dfc2 stage2: stop needlessly re-analyzing unchanged functions 2020-08-03 23:47:54 -07:00
Andrew Kelley
cb25d8e4bc stage2 .debug_line: handle Decl deletes and updates 2020-08-03 23:13:54 -07:00
Andrew Kelley
9b3a70c8aa stage2: move link.File.ELF.SrcFn field from Module.Fn to Module.Decl
SrcFn represents the function in the linked output file, if the
`Decl` is a function. This is stored here and not in `Fn` because `Decl`
survives across updates but `Fn` does not.

TODO Look into making `Fn` a longer lived structure and moving this field there
to save on memory usage.
2020-08-03 22:22:47 -07:00
Andrew Kelley
0c598100d8 stage2: fix use-after-free of export symbol name 2020-08-03 22:07:21 -07:00
Andrew Kelley
edfede575c self-hosted: add build option for log scopes
Now you can enable a set of log scopes by passing -Dlog=<scope>
2020-08-03 21:56:21 -07:00
Andrew Kelley
a33efc74ed stage2 codegen: revert the unneeded is_stmt stuff 2020-08-03 21:09:58 -07:00
Andrew Kelley
d624bf8059 stage2 .debug_line stepping with gdb is working 2020-08-03 21:01:06 -07:00
Andrew Kelley
ac10841fa9 stage2 .debug_line: simpler strategy for incremental compilation
See #5963
2020-08-03 19:14:09 -07:00
Vexu
4ab2f947f9
translate-c: recognize other type trait expressions
Closes #5979
2020-08-04 00:48:29 +03:00
Andrew Kelley
4e023c6fa8 stage2: dwarf: fix standard opcode length of LNS_fixed_advance_pc 2020-08-03 00:54:30 -07:00
Andrew Kelley
eccbb03063 downgrade .debug_info to DWARFv4
gdb, notice me senpai
2020-08-03 00:42:08 -07:00
Andrew Kelley
bf85d3db3f downgrade .debug_line to DWARFv4
apparently gdb 8.3.1 which is still a commonly distributed version of gdb,
does not support v5.
2020-08-03 00:34:12 -07:00
Andrew Kelley
659603c621 codegen: emit .debug_line ops for IR instructions 2020-08-02 21:28:06 -07:00
Andrew Kelley
42d331b58a .debug_line: avoid DW_FORM_strp to work around readelf/gdb
These tools do not support DWARFv5 yet apparently.
2020-08-02 20:22:41 -07:00
Andrew Kelley
1ce6e201aa .debug_line: don't rely on header_length field
Empirically, debug info consumers do not respect this field, or otherwise
consider it to be an error when it does not point exactly to the end of the header.
Therefore we rely on the NOP jump at the beginning of the Line Number Program for
padding rather than this field.

llvm-dwarfdump says the line number data is fine; gdb and
binutils-readelf crap out.
2020-08-02 19:57:42 -07:00
Andrew Kelley
1a3f250f19 .debug_line incremental compilation initial support
Supports writing the first function. Still TODO is:
 * handling the .debug_line header growing too large
 * adding a new file to an existing compilation
 * adding an additional function to an existing file
 * handling incremental updates
 * adding the main IR debug ops for IR instructions

There are also issues to work out:
 * readelf --debug-dump=rawline is saying there is no .debug_str section
   even though there is
 * readelf --debug-dump=decodedline is saying the file index 0 is bad
   and reporting some other kind of corruption.
2020-08-02 19:25:26 -07:00
Andrew Kelley
ba6e5cbfd2 stage2: add the .debug_line header and associated data types
* the .debug_line header is written properly
 * link.File.Elf gains:
   - SrcFn, which is now a field in Module.Fn
   - SrcFile, which is now a field in Module.Scope.File
 * link.File.Elf gets a whole *Package field rather than only
   root_src_dir_path.
 * the fields first_dbg_line_file and last_dbg_line_file tell where the
   Line Number Program begins and ends, which alows moving files when
   the header gets too big, and allows appending files to the end.
 * codegen is passed a buffer for emitting .debug_line
   Line Number Program opcodes for functions.

See #5963

There is some work-in-progress code here, but I need to go make some
experimental changes to changing how to represent source locations and I
want to do that in a separate commit.
2020-08-02 12:02:43 -07:00
Isaac Freund
6123201f06 stage2: move format-specific code to link.File.X
This makes the code outside of link.File.Elf less elf-specific and will
allow for easier implementation of other formats such as wasm.
2020-08-02 18:07:10 +00:00
Andrew Kelley
0962cc5a32 stage2: implement .debug_aranges DWARF 2020-07-31 01:16:34 -07:00
Andrew Kelley
acd0dabab2 stage2: add some tracy calls to link.zig functions 2020-07-30 23:51:14 -07:00
Andrew Kelley
f1c1b8c02d stage2: add DWARF info for the main compilation unit 2020-07-31 06:48:17 +00:00
Vexu
1f8f434b9c
compiler_rt: add floatditf 2020-07-29 18:03:44 +03:00
Andrew Kelley
8899e6e334 stage2: codegen: fix off-by-one stack variable offsets 2020-07-29 02:29:37 -07:00
Andrew Kelley
606f157a6b stage2: register-aliasing-aware codegen
* unify duplicated register allocation codepath
 * support the x86_64 concept of register aliasing
 * slightly improved memset codegen, supports sizes 1, 2, 4, 8
2020-07-29 02:29:37 -07:00
Andrew Kelley
1bbfa36b76 stage2: improved codegen
* multiple returns jump to one canonical function exitlude. This is in
   preparation for the defer feature.
 * simple elision of trivial jump relocs.
 * omit prelude/exitlude for naked calling convention functions.
 * fix not switching on arch for prelude/exitlude
 * fix swapped registers when setting stack mem from a register
2020-07-29 02:29:37 -07:00
Andrew Kelley
64a1a280ef stage2: fix superfluous returnvoid ZIR 2020-07-29 02:29:37 -07:00
Andrew Kelley
4beff80b2f stage2: codegen handles undefined values
* `optimize_mode` is passed to `link.File` and stored there
 * improve the debugging function `Module.dumpInst`
 * get rid of `Value.the_one_possible_value` in favor of a few more
   specific values for different types. This is less buggy, one less
   footgun.
 * `Type.onePossibleValue` now returns a `?Value` instead of `bool`.
 * codegen handles undefined values. `undef` is a new `MCValue` tag.
   It uses 0xaa values depending on optimization mode. However
   optimization mode does not yet support scope overrides.
 * link.zig: move the `Options` field from `File.Elf` and `File.C` to
   the base struct.
   - fix the Tag enum to adhere to style conventions
 * ZIR now supports emitting undefined values.
 * Fix the logic of comptime math to properly compare against zero using
   the `compareWithZero` function.
2020-07-29 02:29:37 -07:00
Andrew Kelley
2b8e7deeda stage2: add ZIR emitType support for simple pointer types 2020-07-29 02:29:37 -07:00
Andrew Kelley
99d2d9bf64 stage2: fix format() of mutable pointer types 2020-07-29 02:29:37 -07:00
Andrew Kelley
cb3e8e323d stage2: x86_64 codegen for movs to/from stack variables 2020-07-29 02:29:37 -07:00
Andrew Kelley
5ccee4c986 stage2: more progress towards mutable local variables
* implement sema for runtime deref, store pointer, coerce_to_ptr_elem,
   and store
 * identifiers support being lvalues, except for decls is still TODO
 * codegen supports load, store, ref, alloc
 * introduce more MCValue union tags to support pointers
 * add load, ref, store typed IR instructions
 * add Type.isVolatilePtr
2020-07-29 02:29:36 -07:00
Andrew Kelley
d726c2a2d3 self-hosted: beginnings of stack allocation
Comment out non-x86_64 architectures for now in codegen.zig, because
they all have compile errors for their codepaths anyway, and it was
bloating the compilation speed and memory usage when stage1 tried to
build self-hosted. Here's the panic message:

"Backend architectures that don't have good support yet are commented
out, to improve compilation performance. If you are interested in one
of these other backends feel free to uncomment them. Eventually these
will be completed, but stage1 is slow and a memory hog."

This is a workaround to lower the time it takes to build self-hosted
with stage1 as well as use less memory. It should fix the CI.

Additionally:
 * Add `single_mut_pointer` support to `Type`
 * Trivial implementation of stack allocation in codegen.zig. It does
   not deal with freeing yet, and it's missing the stack pointer
   adjustment prologue.
 * Add the `alloc` IR instruction and semantic analysis for `alloc` ZIR
   instruction.
2020-07-28 01:43:04 -07:00
Andrew Kelley
0965724e31 self-hosted: refactor some code out of Module.zig into zir_sema.zig
This makes sense from an organizational point of view, as explained by
this new doc comment at the top of the new file:

//! Semantic analysis of ZIR instructions.
//! This file operates on a `Module` instance, transforming untyped ZIR
//! instructions into semantically-analyzed IR instructions. It does type
//! checking, comptime control flow, and safety-check generation. This is the
//! the heart of the Zig compiler.
//! When deciding if something goes into this file or into Module, here is a
//! guiding principle: if it has to do with (untyped) ZIR instructions, it goes
//! here. If the analysis operates on typed IR instructions, it goes in Module.

Before:
   4009 src-self-hosted/Module.zig

After:
   2776 src-self-hosted/Module.zig
   1128 src-self-hosted/zir_sema.zig

This should be sufficient to avoid the situation we have in stage1 where
ir.cpp is 32,516 lines.
2020-07-27 22:44:18 -07:00
Andrew Kelley
b8e22d2002 stage2: implement integer return values 2020-07-27 18:59:13 -07:00
Andrew Kelley
3e0a46281c stage2: fix function calls always having void return type 2020-07-27 18:59:13 -07:00
Andrew Kelley
488df7f1d1 stage2: astgen for all arithmetic and assignments 2020-07-27 17:09:47 -07:00
Vexu
e7007fa7bd
translate-c: use ArrayList for macro tokens 2020-07-27 15:38:56 +03:00
Henrik Laxhuber
9f6401c692 Fix a use of appendToken to appendIdentifier in translate-c 2020-07-27 13:43:49 +03:00
Henrik Laxhuber
442025481c Fix parsing of unsigned in translate-c.
Previously, `unsigned` was parsed as the shorthand for `unsigned int`.
This commit introduces code to parse `unsigned short`, `unsigned int`,
`unsigned long`, and `unsigned long long`.

There is a comment in the code about std.c.parse` - Im not
familiar with zig internals, but it seems like this is a separate
C parsing implementation. In the long run, it probably makes
sense to merge both implementations, so this commit should be
regarded as a quick fix that doesn't address an apparently
underlying issue.
2020-07-27 13:43:49 +03:00
Andrew Kelley
09b0ad494b stage2: remove superfluous else => unreachable 2020-07-25 23:32:30 -07:00
Andrew Kelley
a36772ee64
Merge pull request #5693 from antlilja/switch-unreachable-else
Add error message for unreachable else prong in switch
2020-07-26 05:46:18 +00:00
Michael Dusan
3b26e50863 macOS: macho ld64.lld fixes
* bring `construct_linker_job_macho` to parity with
   `construct_linker_job_elf`
 * macho now sets `-error-limit`
 * macho on macOS now sets `-macosx_version_min` and `-sdk_version`
   to `10.13` when running `zig0`
 * macho now detects when `-l` prefix is not needed
 * macho on macOS detects system libraries in a case-insensitive manner
 * macho now ads user-specified libraries to linker command-line args
   when condition `is_native_os != true`
 * re-ordered some macho args positions to match elf positions

closes #5059
closes #5067
2020-07-24 20:01:18 +00:00
Luuk de Gram
3019ab9391
Fix resolvepeertype() int signess and feedback improvements 2020-07-24 17:51:24 +02:00
Luuk de Gram
470264a4f5
Restructuring and f32/f64 support 2020-07-24 17:16:48 +02:00
Luuk de Gram
97e92d868e
Rebase and skeleton for float support 2020-07-24 17:16:48 +02:00
Luuk de Gram
ee7fbb9548
Restructured arithmetic operations 2020-07-24 17:16:48 +02:00
Andrew Kelley
aac6e8c418 self-hosted: AST flattening, astgen improvements, result locations, and more
* AST: flatten ControlFlowExpression into Continue, Break, and Return.
 * AST: unify identifiers and literals into the same AST type: OneToken
 * AST: ControlFlowExpression uses TrailerFlags to optimize storage
   space.
 * astgen: support `var` as well as `const` locals, and support
   explicitly typed locals. Corresponding Module and codegen code is not
   implemented yet.
 * astgen: support result locations.
 * ZIR: add the following instructions (see the corresponding doc
   comments for explanations of semantics):
   - alloc
   - alloc_inferred
   - bitcast_result_ptr
   - coerce_result_block_ptr
   - coerce_result_ptr
   - coerce_to_ptr_elem
   - ensure_result_used
   - ensure_result_non_error
   - ret_ptr
   - ret_type
   - store
   - param_type
 * the skeleton structure for result locations is set up. It's looking
   pretty clean so far.
 * add compile error for unused result and compile error for discarding
   errors.
 * astgen: split builtin calls up to implemented manually, and implement
   `@as`, `@bitCast` (and others) with respect to result locations.
 * add CLI support for hex and raw object formats. They are not
   supported by the self-hosted compiler yet, and emit errors.
 * rename `--c` CLI to `-ofmt=[objectformat]` which can be any of the
   object formats. Only ELF and C are supported so far. Also added missing
   help to the help text.
 * Remove hard tabs from C backend test cases. Shame on you Noam, you
   are grounded, you should know better, etc. Bad boy.
 * Delete C backend code and test case that relied on comptime_int
   incorrectly making it all the way to codegen.
2020-07-23 23:05:26 -07:00
Vexu
dd89297388
stage2: actually implement float casting 2020-07-21 22:34:14 +03:00