16261 Commits

Author SHA1 Message Date
joachimschmidt557
c0ae9647f9 stage2 ARM: implement slice_elem_val for types with size <= 4 2021-12-28 20:38:37 -05:00
Veikka Tuominen
4f4f0bc6f0 stage1: fix access of slice sentinel at comptime 2021-12-28 14:44:46 +02:00
Andrew Kelley
4b9b9e7257 stage2: LLVM backend: fix lowering of union constants
Comment from this commit reproduced here:

LLVM does not allow us to change the type of globals. So we must
create a new global with the correct type, copy all its attributes,
and then update all references to point to the new global,
delete the original, and rename the new one to the old one's name.
This is necessary because LLVM does not support const bitcasting
a struct with padding bytes, which is needed to lower a const union value
to LLVM, when a field other than the most-aligned is active. Instead,
we must lower to an unnamed struct, and pointer cast at usage sites
of the global. Such an unnamed struct is the cause of the global type
mismatch, because we don't have the LLVM type until the *value* is created,
whereas the global needs to be created based on the type alone, because
lowering the value may reference the global as a pointer.
2021-12-28 01:53:58 -07:00
Andrew Kelley
232f8a291d stage2: fix build on 32-bit targets
Regressed in 85d4c8620f602726b159efe1fe2ea0e07e3c5b59.
2021-12-28 01:15:36 -07:00
Andrew Kelley
85d4c8620f Sema: implement array coercion 2021-12-27 22:06:23 -07:00
Ali Chraghi
042b770d62
std: Skip comptime struct fields in mem.zeroes() (#10406)
closes #9934
2021-12-27 23:33:11 -05:00
Andrew Kelley
6ed7850972 Sema: fix anytype parameters whose types require comptime 2021-12-27 19:39:28 -07:00
Andrew Kelley
fc1a5cd9e7 Sema: implement @tagName for enum literals 2021-12-27 18:10:24 -07:00
Andrew Kelley
9dd4fb4130 stage2: fix 0-bit function parameters
Before this commit, Zig would incorrectly emit `arg` AIR instructions
for parameters whose types were 0-bit.
2021-12-27 17:56:33 -07:00
Andrew Kelley
886df772f0 stage2: LLVM backend: fix const packed structs
When doing LLVM const bit shifting we must make sure the integer bit
sizes are wide enough or else LLVM gives us a poison result.
2021-12-27 16:59:26 -07:00
Frank Denis
3abe464b06 crypto/edwards25519: faster point decompression
Make recovery of the x-coordinate slightly faster.

See https://mailarchive.ietf.org/arch/msg/cfrg/qlKpMBqxXZYmDpXXIx6LO3Oznv4/
for details.
2021-12-27 14:42:58 -08:00
Matthew Hall
4266795743 stage2: make tests/behaviour/void.zig work with c backend
* fix initialisation of void* fields of structs (initialises to 0xaa.. rather than {})
* don't generate struct fields when the field type does not have codegen bits
* in airAlloc generate a void* literal if the element type does not have codegen bits
2021-12-27 14:42:25 -08:00
Jan Philipp Hafer
17046674a7 compiler_rt: add __negvsi2, __negvdi2, __negvti2
- neg can only overflow, if a == MIN
- case `-0` is properly handled by hardware, so overflow check by comparing
  `a == MIN` is sufficient
- tests: MIN, MIN+1, MIN+4, -42, -7, -1, 0, 1, 7..

See #1290
2021-12-27 14:35:45 -08:00
Andrew Kelley
70894d5c2f AstGen: fix loop result locations
The main problem was that the loop body was treated as an expression
that was one of the peer result values of a loop, when in reality the
loop body is noreturn and only the `break` operands are the result
values of loops.

This was solved by introducing an override that prevents rvalue() from
emitting a store to result location instruction for loop bodies.

An orthogonal change also included in this commit is switching
`elem_val` index expressions to using `coerced_ty` and doing the
coercion to `usize` inside `Sema`, resulting in smaller ZIR (since the
cast becomes implied).

I also changed the break operand expression to use `reachableExpr`,
introducing a new compile error for double break.

This makes a few more behavior tests pass for `while` and `for` loops.
2021-12-27 15:30:31 -07:00
daurnimator
2c23699594
Bcrypt pbkdf (#10331)
* Make bcrypt State struct public

This is useful to implement the various protocols outside of the standard library

* Implement bcrypt pbkdf

This variant is used in e.g. SSH
The OpenBSD implementation was used as a reference
2021-12-27 21:59:32 +01:00
Andrew Kelley
c8fb36b36c stage2: LLVM backend: implement @tagName for enums
Introduced a new AIR instruction: `tag_name`. Reasons to do this
instead of lowering it in Sema to a switch, function call, array
lookup, or if-else tower:
 * Sema is a bottleneck; do less work in Sema whenever possible.
 * If any optimization passes run, and the operand to becomes
   comptime-known, then it could change to have a comptime result
   value instead of lowering to a function or array or something which
   would then have to be garbage-collected.
 * Backends may want to choose to use a function and a switch branch,
   or they may want to use a different strategy.

Codegen for `@tagName` is implemented for the LLVM backend but not any
others yet.

Introduced some new `Type` tags:
 * `const_slice_u8_sentinel_0`
 * `manyptr_const_u8_sentinel_0`

The motivation for this was to make typeof() on the tag_name AIR
instruction non-allocating.

A bunch more enum tests are passing now.
2021-12-27 01:14:50 -07:00
Andrew Kelley
f41b9cdb6d Sema: fix enum tag type not initialized when 0 fields 2021-12-26 21:36:12 -07:00
Andrew Kelley
629a54c711 Sema: improve non-exhaustive enum support
* remove false positive "all prongs handled" compile error for
   non-exhaustive enums.
 * implement `@TypeInfo` for enums, except enums which have any
   declarations is still TODO.
 * `getBuiltin` uses nomespaceLookup/analyzeDeclVal rather than
   namespaceLookupRef/analyzeLoad. Avoids a detour through an
   unnecessary type, and adds a detour through a caching mechanism.
 * `Value.eql`: add missing code to handle enum comparisons for
   non-exhaustive enums. It works by converting the enum tags to numeric
   values and comparing those.
2021-12-26 21:07:16 -07:00
Jan Philipp Hafer
405ff911da compiler_rt: add __absvsi2, __absvdi2, __absvti2
- abs can only overflow, if a == MIN
- comparing the sign change from wrapping addition is branchless
- tests: MIN, MIN+1,..MIN+4, -42, -7, -1, 0, 1, 7..

See #1290
2021-12-26 13:21:18 -08:00
Andrew Kelley
71923d7e40
Merge pull request #10414 from joachimschmidt557/stage2-arm
stage2 ARM: Implement airLoad for slices
2021-12-26 13:18:55 -08:00
joachimschmidt557
8a0e86cd5c
stage2 ARM: implement load for types with size 8 (e.g. slices) 2021-12-26 17:05:08 +01:00
joachimschmidt557
15f0f9240d
stage2 codegen: Implement generateSymbol for undefined values 2021-12-26 16:40:51 +01:00
Andrew Kelley
6b8e33d14c stage2: LLVM: fix lowering of packed structs
* ensure enough capacity when building the LLVM type and value.
 * add explicit padding field and populate it to ensure proper
   alignment.
2021-12-24 02:37:54 -07:00
Andrew Kelley
5b171f446f stage2: initial implementation of packed structs
Layout algorithm: all `align(0)` fields are squished together as if they
were a single integer with a number of bits equal to `@bitSizeOf` each
field added together. Then the natural ABI alignment of that integer is
used for that pseudo-field.
2021-12-23 23:57:02 -07:00
Andrew Kelley
3763580e1e add missing files to CMakeLists 2021-12-23 17:48:58 -07:00
Andrew Kelley
0049c7180f
Merge pull request #10394 from ziglang/stage2-x86_64-mir-intel-syntax
stage2: rewrite MIR -> Isel layer for x86_64
2021-12-23 16:39:51 -08:00
Andrew Kelley
4c11986650
MIR: remove unnecessary TODO comment
it wouldn't save any bytes in the MIR, and we can just check the range of the value when lowering the MIR to machine code.
2021-12-23 16:39:28 -08:00
Jakub Konka
35fe088e0e stage2: add lowering of RMI encoding
Example includes imul with 3 operands such as imul r64, r/m64, imm32.
2021-12-23 21:14:14 +01:00
Jakub Konka
dba5df64ea stage2: use lowerToRmEnc to lower two-operand imul
Fix mismatched register sizes in codegen.
2021-12-23 20:51:48 +01:00
Jakub Konka
c50bb2b80f stage2: lower jcc and setcc conditional jump/set instructions 2021-12-23 20:29:34 +01:00
Jakub Konka
8c664d3f6a stage2: support multibyte opcodes and refactor 1byte opcode changes 2021-12-23 18:49:40 +01:00
Jakub Konka
d23a1487bd stage2: add lowering of ZO encoding
ZO (probably) stands for zero operands encoding which is effectively
only the opcode.
2021-12-23 18:49:40 +01:00
Jakub Konka
a70b4068c6 stage2: add lowering to I encoding
Examples include push imm32.
2021-12-23 10:22:23 +01:00
Jakub Konka
b657956c44 stage2: add lowering to O encoding
Example includes push/pop register.
2021-12-23 09:47:38 +01:00
Andrew Kelley
303bad9989 behavior tests: stage2 is not yet passing this test
Looks like I repeated the same mistake, which last time was addressed in
1e0addcf73ee71d23a41b744995848bcca38e8d3.
2021-12-22 20:48:53 -07:00
Andrew Kelley
cc937369fb stage2: Type.hasCodeGenBits asserts structs and unions have fields
Previously, this function would return an incorrect result for structs
and unions which did not have their fields resolved yet.

This required introducing more logic in Sema to resolve types before
doing certain things such as creating an anonmyous Decl and emitting
function call AIR.

As a result a couple more struct tests pass.

Oh, and I implemented the language change to make sizeOf for pointers
always return pointer size bytes even if the element type is 0 bits.
2021-12-22 20:29:26 -07:00
Luuk de Gram
e061d75cdf wasm-linker: Implement symbol names emitting
The linker will now emit names for all function, global and data segment symbols.
This increases the ability to debug wasm modules tremendously as tools like wasm2wat
can use this information to generate named functions, globals etc, rather than placeholders such as $f1.
2021-12-23 01:47:45 +01:00
Jakub Konka
603f826779 stage2: migrate push/pop r/m64 to new lowering mechanism 2021-12-23 01:30:25 +01:00
Jakub Konka
9078cb0197 stage2: add lowering of M encoding
Examples include jmp / call near with memory or register operand
like `jmp [rax]`, or even RIP-relative `call [rip + 0x10]`.
2021-12-23 01:22:07 +01:00
Jakub Konka
1167e248ef stage2: add lowering of D encoding
Example of such encoding includes a near/far call and jmp instructions.
2021-12-23 00:43:38 +01:00
Jakub Konka
362eccf539 stage2: handle RIP relative addressing in MI, RM and MR 2021-12-22 23:07:46 +01:00
Jakub Konka
b40e5adf54 stage2: add lowering for FD/TD encodings 2021-12-22 22:36:56 +01:00
Jakub Konka
b9a6f81d1a stage2: add lowering fn for OI encoding
Implement movabs using OI generic encoding.
2021-12-22 19:01:43 +01:00
Jakub Konka
2b5de9403d stage2: create generic lowering fns for MI, RM, and MR encodings
This way I am hopeful they can be reused for every MIR lowering
function which follows a given encoding. Currently, support MI,
RM and MR encodings without SIB scaling.
2021-12-22 18:10:52 +01:00
Dante Catalfamo
3bd0575cfe
Add BSD Authentication constants (#10376) 2021-12-22 00:59:53 -05:00
Andrew Kelley
6fdf7ce0af Sema: simplify coercion logic
Instead of a separate function, `coerceNum` for handling comptime-known
number coercion, outside of the main switch, the `coerce` function now
has a single big switch statement that decides the control flow based on
the zig type tag.
2021-12-21 22:35:24 -07:00
Andrew Kelley
5d6380b38d
Merge pull request #10384 from joachimschmidt557/stage2-arm-optionals
stage2 ARM: basic implementation of optionals and error unions
2021-12-21 21:06:08 -08:00
Andrew Kelley
88be5bd81d Sema: fix empty struct init
* Extract common logic between `zirStructInitEmpty` and
   `zirStructInit`.
 * `resolveTypeFields` additionally sets status to `have_layout` if the
   total number of fields is 0.
2021-12-21 20:34:27 -07:00
Andrew Kelley
06d751dbb3 link/wasm: fix regression of wrong assertion
Fixes typo introduced in 2cbeb85a96af25f2718a604aa2bec4f76dd85018.
2021-12-21 18:43:19 -07:00
Stephen Lumenta
efab3b1e6d fix expectStringEndsWith error output.
before it started outputting the actual starting, not ending characters.
2021-12-21 17:33:04 -08:00