5676 Commits

Author SHA1 Message Date
Andrew Kelley
077b8d3def stage2: introduce new ZIR instruction: arg
* AstGen: LocalVal and LocalPtr use string table indexes for their
   names. This is more efficient because local variable declarations do
   need to include the variable names so that semantic analysis can emit
   a compile error if a declaration is shadowed. So we take advantage of
   this fact by comparing string table indexes when resolving names.

 * The arg ZIR instructions are needed for the above reasoning, as well
   as to emit equivalent AIR instructions for debug info.
   Now that we have these arg instructions, get rid of the special
   `Zir.Inst.Ref` range for parameters. ZIR instructions now refer
   to the arg instructions for parameters.

 * Move identAsString and strLitAsString from Module.GenZir to AstGen
   where they belong.
2021-04-30 21:43:18 -07:00
Andrew Kelley
db7acd83d2 Sema: implement function declarations 2021-04-30 14:36:02 -07:00
Andrew Kelley
2d8d681b5e stage2: un-tangle memory management of Decl and Namespace
Before there was this "top_decl" and "tmp_namespace" stack values that
were kludgy and buggy. Now Sema is slightly reworked so that files which
are structs are analyzed with their own Decl and Namespace already set
up.

After this commit there are no memory leaks for a successful build-obj.
2021-04-30 11:07:31 -07:00
Andrew Kelley
5d696b0706 stage2: fix File incorrectly freeing its Namespace 2021-04-29 20:49:31 -07:00
Andrew Kelley
8ab4a003c8 stage2: properly free Decl name
Two problems solved:

 * The Decl name may be allocated with gpa or it may be a reference to
   the ZIR string table.

 * The main update() function was freeing the ZIR when we still had
   Decl objects referencing it.
2021-04-29 20:34:33 -07:00
Andrew Kelley
8944240aec AstGen: represent global variables directly
Rather than with `block_inline_var`. This matches how function
declarations work and how extern variables work.
2021-04-29 19:56:01 -07:00
Andrew Kelley
86d564eed8 AstGen: implement extern variables 2021-04-29 19:44:51 -07:00
Andrew Kelley
ba9b9cb38d AstGen: implement function prototypes with alignment exprs 2021-04-29 18:25:25 -07:00
Andrew Kelley
2eef83e85f AstGen: fix comptime compile error source location 2021-04-29 17:43:07 -07:00
Andrew Kelley
47585cbe12 Sema: rename TZIR to AIR
Analyzed Intermediate Representation
2021-04-29 17:26:21 -07:00
Andrew Kelley
478dac5346 Sema: skip analysis of empty enum blocks
For these, the ZIR code has an empty block rather than a block with a
single `break_inline` instruction.
2021-04-29 17:25:10 -07:00
Andrew Kelley
9e49a65e1b AstGen: implement anytype struct fields 2021-04-29 17:13:18 -07:00
Andrew Kelley
55e86b724a AstGen: implement comptime struct fields 2021-04-29 16:57:13 -07:00
Andrew Kelley
d36a31ba75 stage2: fix scanDecls not advancing the field bits
Also fix `semaFile` not handling compile errors properly.
2021-04-28 23:16:13 -07:00
Andrew Kelley
0c71d2fdc1 stage2: implement semantic analysis for functions and global vars
* AstGen: add missing `break_inline` for comptime blocks.
 * Module: call getTree() in byteOffset(). This generates the AST when
   using cached ZIR and compile errors need to be reported.
 * Scope.File: distinguish between successful ZIR generation and AIR
   generation (when Decls in scope have been scanned).
   - `semaFile` correctly avoids doing work twice.
 * Implement first pass at `lookupInNamespace`. It has various TODOs
   left, such as `usingnamespace`, and setting up Decl dependencies.
2021-04-28 22:43:26 -07:00
Andrew Kelley
3462193d30 stage2: prepare for mainining Decl references to ZIR indexes 2021-04-28 16:57:01 -07:00
Andrew Kelley
f86469bc5e stage2: semaDecl properly analyzes the decl block
Also flattened out Decl TypedValue fields into
ty, val, has_tv
and add relevant fields to Decl for alignment and link section.
2021-04-28 16:57:01 -07:00
Andrew Kelley
fa6bb4b662 Sema: do not analyze test decls when not in test mode
We do this by reserving string table indexes 0 and 1 in ZIR to be
special. Decls now have 0 to mean comptime or usingnamespace, and 1 to
mean an unnamed test decl.
2021-04-28 16:57:01 -07:00
jacob gw
2354cbafdb stage2: implement #8364 2021-04-28 19:54:04 -04:00
Andrew Kelley
d3ffacb55c AstGen: hook up hex float parsing to float literals
Thanks @LemonBoy!
2021-04-28 15:13:43 -07:00
Andrew Kelley
9db5b2c5c6 AstGen: function prototypes can have alignment 2021-04-28 15:02:52 -07:00
Andrew Kelley
df24ce52b1 Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
In particular I wanted to take advantage of the new hex float parsing
code.
2021-04-28 14:57:38 -07:00
Andrew Kelley
2c9ed6daee
Merge pull request #8472 from sreehax/master
callconv: add SysV
2021-04-28 13:11:40 -04:00
joachimschmidt557
bc06e19828 stage2 riscv64: cleanup code and add tests 2021-04-28 07:20:45 +02:00
Andrew Kelley
eb9c29eb81 AstGen: fix function src hash not including body 2021-04-27 22:20:25 -07:00
Andrew Kelley
3d9e4edb57 link: fix compile error from previous commit 2021-04-27 14:46:55 -07:00
Andrew Kelley
bfded492f0 stage2: rewire the frontend driver to whole-file-zir
* Remove some unused imports in AstGen.zig. I think it would make sense
   to start decoupling AstGen from the rest of the compiler code,
   similar to how the tokenizer and parser are decoupled.
 * AstGen: For decls, move the block_inline instructions to the top of
   the function so that they get lower ZIR instruction indexes. With
   this, the block_inline instruction index combined with its corresponding
   break_inline instruction index can be used to form a ZIR instruction
   range. This is useful for allocating an array to map ZIR instructions
   to semantically analyzed instructions.

 * Module: extract emit-h functionality into a struct, and only allocate
   it when emit-h is activated.
 * Module: remove the `decl_table` field. This previously was a table of
   all Decls in the entire Module. A "name hash" strategy was used to
   find decls within a given namespace, using this global table. Now,
   each Namespace has its own map of name to children Decls.
   - Additionally, there were 3 places that relied on iterating over
     decl_table in order to function:
     - C backend and SPIR-V backend. These now have their own decl_table
       that they keep populated when `updateDecl` and `removeDecl` are
       called.
     - emit-h. A `decl_table` field has been added to the new GlobalEmitH
       struct which is only allocated when emit-h is activated.
 * Module: fix ZIR serialization/deserialization bug in debug mode having
   to do with the secret safety tag for untagged unions. There is still an
   open TODO to investigate a friendlier solution to this problem with
   the language.
 * Module: improve deserialization of ZIR to allocate only exactly as
   much capacity as length in the instructions array so as to not waste
   space.
 * Module: move `srcHashEql` to `std.zig` to live next to the definition
   of `SrcHash` itself.
 * Module: re-introduce the logic for scanning top level declarations
   within a namespace.

 * Compilation: add an `analyze_pkg` Job which is used to kick off the
   start of semantic analysis by doing the equivalent of
   `_ = @import("std");`. The `analyze_pkg` job is unconditionally added
   to the work queue on every update(), with pkg set to the std lib pkg.

 * Rename TZIR to AIR in a few places. A more comprehensive rename will
   come later.
2021-04-26 20:41:07 -07:00
Andrew Kelley
91c317bb9a AstGen: improved handling of declarations
* Every decl provides a 16 byte source hash which can be used to detect
   if the source code for any particular decl has changed.
 * Include comptime decls, test decls, and usingnamespace decls in the
   decls list of namespaces.
   - Tests are encoded as extended functions with is_test bit set.
2021-04-26 17:36:28 -07:00
Andrew Kelley
95b42f9e6b
Merge pull request #8604 from hoanga/haiku-support-sysdefs
further haiku support system definitions
2021-04-26 16:12:23 -04:00
Andrew Kelley
646eb1fa93 AstGen: implement opaque decls
Also move the decls to the beginning in ZIR encoding because in Sema we
want to create the namespace with the decls before evaluating the
fields.
2021-04-26 12:49:02 -07:00
Andrew Kelley
2fc6b347ec
Merge pull request #8616 from LemonBoy/fn-align
Function pointer alignment
2021-04-25 19:12:06 -04:00
LemonBoy
82f1d592fa stage1: Use correct alignment for asyncCall frame 2021-04-25 20:41:49 +02:00
Andrew Kelley
56226449d2 stage2: pre-open ZIR cache dir handles
So that we do not needlessly open and close the ZIR cache dir handles in
each AstGen operation.
2021-04-25 10:43:07 -07:00
Andrew Kelley
6f61594692
Merge pull request #8496 from xackus/isError
std.meta: add isError
2021-04-25 13:27:44 -04:00
joachimschmidt557
3a55cda14d stage2 register manager: Use an array instead of a hashmap for tracking
allocated registers
2021-04-25 13:20:53 -04:00
LemonBoy
ae15022406 translate-c: Fix casting of function pointers
The @ptrCast(X, @alignCast(@alignOf(T), Y)) pattern is only correct if T
is not a function type or a pointer, in that case the @alignOf refers to
the pointer itself and not to the pointee type.
2021-04-25 16:50:41 +02:00
LemonBoy
50a8124f45 stage1: Change how the Frame alignment is computed
The code would previously assume every function would start at addresses
being multiples of 16, this is not true beside some specific cases.
Moreover LLVM picks different alignment values depending on whether it's
trying to generate dense or fast code.

Let's use the minimum guaranteed alignment as base value, computed
according to how big the opcodes are.

The alignment of function pointers is always 1, a safe value that won't
cause any error at runtime. Note that this was already the case before
this commit, here we're making this choice explicit.

Let the 'alignment' field for TypeInfo of fn types reflect the ABI
alignment used by the compiler, make this field behave similarly to the
'alignment' one for pointers.
2021-04-25 16:40:41 +02:00
Andrew Kelley
015cd79f89 stage2: implement caching for ZIR code
Notably this exposed an issue with the language having to do with the
secret safety tag on untagged unions. How can we have our cake and eat
it too? Not solved in this commit. I will file a language proposal to
tackle this issue soon.

Fixes a compile error in `std.fs.File.readvAll`.
2021-04-25 00:02:58 -07:00
Andrew Kelley
ff2ec0dc5a AstGen: implement @Vector 2021-04-24 17:44:07 -07:00
Andrew Kelley
e018e64a53 stage2: move overflow builtin ZIR instructions to Extended
make some more room in our ZIR enum tag space
2021-04-24 17:31:15 -07:00
Andrew Kelley
6951889f6e
Merge pull request #8608 from LemonBoy/fix-8602
translate-c: Prevent mistranslation of fp literals
2021-04-24 19:55:13 -04:00
Andrew Kelley
15e891823e AstGen: parser ensures all suspend have blocks
See #8603.
2021-04-24 14:41:27 -07:00
Andrew Kelley
1592206965 AstGen: implement await and resume
based on @Vexu's code from before
2021-04-24 14:39:44 -07:00
Andrew Kelley
e86cee258c Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
In particular I wanted the change that makes `suspend;` illegal in the
parser.
2021-04-24 10:44:41 -07:00
LemonBoy
7c3896e6cd translate-c: Prevent mistranslation of fp literals
When trying to retrieve 80bit fp values from clang using
getValueAsApproximateDouble we'd eventually hit the ceiling value and
return infinity, an invalid value for a fp literal.

Add some logic to prevent this error and warn the user.

Closes #8602
2021-04-24 18:42:23 +02:00
LemonBoy
0aede1a8fc stage1: Require a block after suspend
Closes #8603
2021-04-24 10:25:43 +02:00
Andrew Kelley
224fbb23c4 stage2: better error reporting for LazySrcLoc.entire_file
instead of crashing the compilation, uses a plain error message
2021-04-23 23:51:27 -07:00
Andrew Kelley
27fa4bc2be AstGen: support struct init with ref result location 2021-04-23 23:40:10 -07:00
Jakub Konka
4ec6d174ad zld: new approach at handling static inits 2021-04-24 08:13:53 +02:00
Jakub Konka
b00d08b667 zld: add temp debug info fix by setting mtime=0 2021-04-24 08:13:53 +02:00