14867 Commits

Author SHA1 Message Date
Jakub Konka
eba153f88f
Merge pull request #9501 from ziglang/macho-objc-cleanup
Add standalone Objective-C enabled on macOS only
2021-08-02 08:01:15 +02:00
Andrew Kelley
dae4c18aa7 stage2: ZIR encodes comptime parameters
`func_extended` ZIR instructions now have a one of the unused flags used
as a `has_comptime_bits` boolean. When set, it means 1 or more
parameters are `comptime`. In this case, there is a u32 per every 32
parameters (usually just 1 u32) with each bit indicating whether the
corresponding parameter is `comptime`.

Sema uses this information to correctly mark generic functions as
generic. There is now a TODO compile error in place in case a generic
function call happens. A future commit will do the generic function call
implementation.
2021-08-01 22:04:18 -07:00
Andrew Kelley
d5f173d28f
Merge pull request #9496 from Luukdegram/stage2-wasm
stage2: wasm - Wrapping, intcast and optionals
2021-08-01 20:33:55 -04:00
Andrew Kelley
ddf14323ea stage2: implement @truncate 2021-08-01 16:13:58 -07:00
Jakub Konka
0ce54a1416 add standalone Objective-C enabled on macOS only 2021-08-01 22:48:39 +02:00
Andrew Kelley
6ae0825e7f Sema: implement comptime variables
Sema now properly handles alloc_inferred and alloc_inferred_mut ZIR
instructions inside a comptime execution context. In this case it
creates Decl objects and points to them with the new `decl_ref_mut`
Value Tag. `storePtr` is updated to mutate such Decl types and values.
In this case it destroys the old arena and makes a new one, preventing
memory growth during comptime code execution.

Additionally:

 * Fix `storePtr` to emit a compile error for a pointer comptime-known
   to be undefined.
 * Fix `storePtr` to emit runtime instructions for all the cases that a
   pointer is comptime-known but does not support comptime
   dereferencing, such as `@intToPtr` on a hard-coded address, or an
   extern function.
 * Fix `ret_coerce` not coercing inside inline function call context.
2021-08-01 12:36:04 -07:00
Luuk de Gram
6e139d124b
wasm: Resolve feedback (wrapping arbitrary int sizes)
- This ensures we honor the user's integer size when performing wrapping operations.
- Also, instead of using ensureCapacity, we now use ensureUnusedCapacity.
2021-08-01 21:30:06 +02:00
Jakub Konka
d794f4cd2a macho: add runaway section id when sorting sections 2021-08-01 18:05:07 +02:00
Meghan
7e52a096db
langref- fix packed struct error code 2021-08-01 12:42:48 +03:00
Meghan
32069d2330
langref- fix use after block error code 2021-08-01 12:42:05 +03:00
Luuk de Gram
a861b7d160
wasm: Test cases for optionals 2021-08-01 11:07:23 +02:00
Luuk de Gram
61de59e121
wasm: Implement optionals
This uses the same approach as error unions,
meaning it's a `WValue` with its tag set to `multi_value`.
The initial index of the multi_value will contain the null-tag, used to check if the value
is null or not. The other values will be the payload.

To support the `.?` shorthand syntax, we save the result from checking the null-tag
into a new local, which can then be loaded later in the block to either hit `unreachable` or
set the actual payload value.

Currently, it seems `.?` and `orelse unreachable` results in different AIR structure.
TODO: Is this expected?
2021-08-01 11:07:23 +02:00
Luuk de Gram
e58976542b
wasm: Test cases for wrap+intcast instructions 2021-08-01 11:07:23 +02:00
Luuk de Gram
5667ab7dcd
wasm: Implement wrapping operands, add opcodes to wasm.zig
- Some opcodes have the incorrect value set in std.
- Some opcodes were missing and have now been added to std.
- Adding wrapping operands for add,sub and mul.
- Implement intCast which either extends or shortens the type.
2021-08-01 11:07:23 +02:00
Žiga Željko
5589edf45c fix help for ast-check command 2021-08-01 03:57:34 -04:00
joachimschmidt557
0d09c6aed8 stage2 ARM: fix stack alignment
Acording to the AAPCS32, the stack alignment at public interfaces
should be 8, not 4.
2021-08-01 03:57:08 -04:00
Jakub Konka
0ce56f9305 macho: fix Trie and CodeSignature unit tests
after the cleanup.
2021-08-01 09:06:56 +02:00
Jakub Konka
58bc713c17 macho: make Trie accept allocator as a param
instead of storing it as a member of Trie struct.
2021-08-01 09:06:56 +02:00
Jakub Konka
d19fdf09ae macho: make CodeSignature accept allocator as param
instead storing it within the struct.
2021-08-01 09:06:56 +02:00
Jakub Konka
2e30bf23aa macho: cleanup extracting objects from archives 2021-08-01 09:06:56 +02:00
Jakub Konka
0b15ba8334 macho: don't allocate Dylib on the heap
instead, immediately transfer ownership to MachO struct. Also, revert
back to try-ok-fail parsing approach of objects, archives, and dylibs.
It seems easier to try and fail than check if the file *is* of a
certain type given that a dylib may be a stub and parsing yaml
twice in a row seems very wasteful.

Hint for the future: if we optimise yaml/TAPI parsing, this approach
may be rethought!
2021-08-01 09:06:56 +02:00
Jakub Konka
f023cdad7c macho: don't allocate Archives on the heap
instead, transfer ownership directly to MachO struct.
2021-08-01 09:06:56 +02:00
Jakub Konka
06396ddd7d macho: don't allocate Objects on the heap
instead, ownership is transferred to MachO. This makes Object
management align closer with data-oriented design.
2021-08-01 09:06:56 +02:00
Jakub Konka
e73777333d macho: don't store allocator in Dylib instance
instead pass it in as an arg to a function that requires it.
2021-08-01 09:06:56 +02:00
Jakub Konka
586a19e3db macho: don't store allocator in Archive
instead pass it in functions as an arg when required.
2021-08-01 09:06:56 +02:00
Jakub Konka
c30cc4dbbf macho: don't store allocator in Object
instead, pass it in functions that require it. Also, when parsing
relocs, make Object part of the context struct where we pass in
additional goodies such as `*MachO` or `*Allocator`.
2021-08-01 09:06:56 +02:00
Andrew Kelley
1f95c50d9a codegen: cmp lowering treats bools the same as unsigned int
fixes a crash when lowering `a == b` and they are of type bool.
I'm not worried about floats; I think we will probably add separate AIR
instructions for floats.
2021-07-30 17:48:24 -07:00
Andrew Kelley
6e78c007df Sema: improved AIR when one operand of bool cmp is known
When doing `x == true` or `x == false` it is now lowered as either a
no-op or a not, respectively, rather than a cmp instruction.

This commit also extracts a zirCmpEq function out from zirCmp, reducing
the amount of branching (on is_equality_cmp) in both functions.
2021-07-30 17:40:30 -07:00
Andrew Kelley
507dc1f2e7 stage2: fix hashing and comparison design flaw with Value
* `Value.toType` accepts a buffer parameter instead of an allocator
   parameter and can no longer fail.
 * Module: remove the unused `mod: *Module` parameter from various
   functions.
 * `Value.compare` now accepts a `Type` parameter which indicates the
   type of both operands. There is also a `Value.compareHetero` which
   accepts only Value parameters and supports comparing mixed types.
   Likewise, `Value.eql` requires a `Type` parameter.
 * `Value.hash` is removed; instead the hash map context structs now
   have a `ty: Type` field, and the hash function lives there, where it
   has access to a Value's Type when it computes a hash.
   - This allowed the hash function to be greatly simplified and sound
     in the sense that the same Values, even with different
     representations, always hash to the same thing.
 * Sema: Fix source location of zirCmp when an operand is runtime known
   but needs to be comptime known.
 * Remove unused target parameter from `Value.floatCast`.
2021-07-30 16:17:59 -07:00
joachimschmidt557
84039a57e4 stage2 codegen: Implement genTypedValue for enums 2021-07-30 17:53:33 -04:00
Koakuma
7aaea20e7e Add freeAndExit() implementation for Linux/SPARCv9 2021-07-30 13:23:41 -04:00
joachimschmidt557
f6b1fa9e29 stage2 codegen: genTypedValue for error unions and error sets 2021-07-30 13:22:33 -04:00
Andrew Kelley
e5e6ceda6a
Merge pull request #9486 from ziglang/comptime-pointers
stage2: more principled approach to comptime pointers and garbage collection of unused anon decls
2021-07-30 01:40:32 -04:00
Andrew Kelley
040c6eaaa0 stage2: garbage collect unused anon decls
After this change, the frontend and backend cooperate to keep track of
which Decls are actually emitted into the machine code. When any backend
sees a `decl_ref` Value, it must mark the corresponding Decl `alive`
field to true.

This prevents unused comptime data from spilling into the output object
files. For example, if you do an `inline for` loop, previously, any
intermediate value calculations would have gone into the object file.
Now they are garbage collected immediately after the owner Decl has its
machine code generated.

In the frontend, when it is time to send a Decl to the linker, if it has
not been marked "alive" then it is deleted instead.

Additional improvements:
 * Resolve type ABI layouts after successful semantic analysis of a
   Decl. This is needed so that the backend has access to struct fields.
 * Sema: fix incorrect logic in resolveMaybeUndefVal. It should return
   "not comptime known" instead of a compile error for global variables.
 * `Value.pointerDeref` now returns `null` in the case that the pointer
   deref cannot happen at compile-time. This is true for global
   variables, for example. Another example is if a comptime known
   pointer has a hard coded address value.
 * Binary arithmetic sets the requireRuntimeBlock source location to the
   lhs_src or rhs_src as appropriate instead of on the operator node.
 * Fix LLVM codegen for slice_elem_val which had the wrong logic for
   when the operand was not a pointer.

As noted in the comment in the implementation of deleteUnusedDecl, a
future improvement will be to rework the frontend/linker interface to
remove the frontend's responsibility of calling allocateDeclIndexes.

I discovered some issues with the plan9 linker backend that are related
to this, and worked around them for now.
2021-07-29 19:30:37 -07:00
Andrew Kelley
a5c6e51f03 stage2: more principled approach to comptime references
* AIR no longer has a `variables` array. Instead of the `varptr`
   instruction, Sema emits a constant with a `decl_ref`.
 * AIR no longer has a `ref` instruction. There is no longer any
   instruction that takes a value and returns a pointer to it. If this
   is desired, Sema must either create an anynomous Decl and return a
   constant `decl_ref`, or in the case of a runtime value, emit an
   `alloc` instruction, `store` the value to it, and then return the
   `alloc`.
 * The `ref_val` Value Tag is eliminated. `decl_ref` should be used
   instead. Also added is `eu_payload_ptr` which points to the payload
   of an error union, given an error union pointer.

In general, Sema should avoid calling `analyzeRef` if it can be helped.
For example in the case of field_val and elem_val, there should never be
a reason to create a temporary (alloc or decl). Recent previous commits
made progress along that front.

There is a new abstraction in Sema, which looks like this:

    var anon_decl = try block.startAnonDecl();
    defer anon_decl.deinit();
    // here 'anon_decl.arena()` may be used
    const decl = try anon_decl.finish(ty, val);
    // decl is typically now used with `decl_ref`.

This pattern is used to upgrade `ref_val` usages to `decl_ref` usages.

Additional improvements:

 * Sema: fix source location resolution for calling convention
   expression.
 * Sema: properly report "unable to resolve comptime value" for loads of
   global variables. There is now a set of functions which can be
   called if the callee wants to obtain the Value even if the tag is
   `variable` (indicating comptime-known address but runtime-known value).
 * Sema: `coerce` resolves builtin types before checking equality.
 * Sema: fix `u1_type` missing from `addType`, making this type have a
   slightly more efficient representation in AIR.
 * LLVM backend: fix `genTypedValue` for tags `decl_ref` and `variable`
   to properly do an LLVMConstBitCast.
 * Remove unused parameter from `Value.toEnum`.

After this commit, some test cases are no longer passing. This is due to
the more principled approach to comptime references causing more
anonymous decls to get sent to the linker for codegen. However, in all
these cases the decls are not actually referenced by the runtime machine
code. A future commit in this branch will implement garbage collection
of decls so that unused decls do not get sent to the linker for codegen.
This will make the tests go back to passing.
2021-07-29 15:59:51 -07:00
Andrew Kelley
192b5d24cb
Merge pull request #8570 from vrischmann/thread-name
add Thread.setName and Thread.getName
2021-07-29 13:25:13 -04:00
Vincent Rischmann
df4fe87163 thread: implement setName/getName 2021-07-29 12:02:12 +02:00
Vincent Rischmann
accde7fe2d windows: add wrappers for LocalFree, SetThreadDescription and GetThreadDescription 2021-07-29 10:57:08 +02:00
Vincent Rischmann
0be1e74902 windows/kernel32: add LocalFree, SetThreadDescription and GetThreadDescription 2021-07-29 10:57:08 +02:00
Vincent Rischmann
c0f40a5fe6 std/c: add pthread_setname_np and pthread_getname_np 2021-07-29 10:57:08 +02:00
Michael Dusan
ed174b7386 stage1: fix anon struct naming in certain cases
- generic "struct:L:C" naming if rloc is NodeTypeStructValueField
- generic "struct:L:C" naming if rloc is NodeTypeFnCallExpr
- move some tests from test/behavior/misc to test/behavior/typename

closes #4330
closes #9339
2021-07-28 18:17:11 -04:00
Mahdi Khanalizadeh
15bec20b00 linux: add mknod and mknodat syscalls 2021-07-28 18:15:41 -04:00
Belhorma Bendebiche
f5d9d739d7 stage1: Expand SysV C ABI support for small structs
While the SysV ABI is not that complicated, LLVM does not allow us
direct access to enforce it. By mimicking the IR generated by clang,
we can trick LLVM into doing the right thing. This involves two main
additions:

1. `AGG` ABI class
This is not part of the spec, but since we have to track class per
eightbyte and not per struct, the current enum is not enough. I
considered adding multiple classes like: `INTEGER_INTEGER`,
`INTEGER_SSE`, `SSE_INTEGER`. However, all of those cases would trigger
the same code path so it's simpler to collapse into one. This class is
only used on SysV.

2. LLVM C ABI type
Clang uses different types in C ABI function signatures than the
original structs passed in, and does conversion. For example, this
struct: `{ i8, i8, float }` would use `{ i16, float }` at ABI boundaries.
When passed as an argument, it is instead split into two arguments `i16`
and `float`. Therefore, for every struct that passes ABI boundaries we
need to keep track of its corresponding ABI type. Here are some more
examples:

```
| Struct             | ABI equivalent |
| { i8, i8, i8, i8 } | i32            |
| { float, float }   | double         |
| { float, i32, i8 } | { float, i64 } |
```

Then, we must update function calls, returns, parameter lists and inits
to properly convert back and forth as needed.
2021-07-28 18:13:17 -04:00
Matt Knight
2f9e498c6f add build dependency when linkerscript is a generated file 2021-07-28 18:10:53 -04:00
Zach Banks
9c028b2078 std/ArrayList: Allow ArrayList(u0) to be created
Enable creating ArrayList with zero-sized types.
This type still tracks length, but does not allocate
additional memory.
2021-07-28 18:05:40 -04:00
Evan Haas
8028e46f01 translate-c: handle signed array subscripts
A rather complicated workaround for handling signed array subscripts.
Once `[*]T + isize` is allowed, this can be removed.

Fixes #8556
2021-07-28 17:46:02 -04:00
Andrew Kelley
c47b46fc8d CI: enable stage2 behavior test coverage 2021-07-28 16:00:49 -04:00
Ominitay
c1285a1bbe Move fs.Walker to fs.Dir.Walker
fs.Walker has been replaced with fs.Dir.Walker. Paths of entries
returned are relative to the Dir.
2021-07-28 21:39:36 +03:00
Veikka Tuominen
f476463cd2
Merge pull request #9468 from ehaas/translate-c-inf-nan
translate-c: handle NAN and INFINITY macros
2021-07-28 21:37:46 +03:00
Evan Haas
d4d3a9dcc9
translate-c: handle floating point NAN and INFINITY macros 2021-07-28 08:06:22 -07:00