Commit Graph

2974 Commits

Author SHA1 Message Date
joachimschmidt557
1b657e6e41 stage2 codegen: Make sure function return value is in a callee
preserved register
2021-03-31 23:27:50 +02:00
Frank Denis
6993087edc Remove the base64 unsafe decoder 2021-03-28 14:32:34 +02:00
Frank Denis
b8c019ef49 std/base64: cleanups & support url-safe and other non-padded variants
This makes a few changes to the base64 codecs.

* The padding character is optional. The common "URL-safe" variant, in
particular, is generally not used with padding. This is also the case for
password hashes, so having this will avoid code duplication with bcrypt,
scrypt and other functions.
* The URL-safe variant is added. Instead of having individual constants
for each parameter of each variant, we are now grouping these in a
struct. So, `standard_pad_char` just becomes `standard.pad_char`.
* Types are not `snake_case`'d any more. So, `standard_encoder` becomes
`standard.Encoder`, as it is a type.
* Creating a decoder with ignored characters required the alphabet and
padding. Now, `standard.decoderWithIgnore(<ignored chars>)` returns a
decoder with the standard parameters and the set of ignored chars.
* Whatever applies to `standard.*` obviously also works with `url_safe.*`
* the `calcSize()` interface was inconsistent, taking a length in the
encoder, and a slice in the encoder. Rename the variant that takes a
slice to `calcSizeForSlice()`.
* In the decoder with ignored characters, add `calcSizeUpperBound()`,
which is more useful than the one that takes a slice in order to size
a fixed buffer before we have the data.
* Return `error.InvalidCharacter` when the input actually contains
characters that are neither padding nor part of the alphabet. If we
hit a padding issue (which includes extra bits at the end),
consistently return `error.InvalidPadding`.
* Don't keep the `char_in_alphabet` array permanently in a decoder;
it is only required for sanity checks during initialization.
* Tests are unchanged, but now cover both the standard (padded) and
the url-safe (non-padded) variants.
* Add an error set, rename `OutputTooSmallError` to `NoSpaceLeft`
to match the `hex2bin` equivalent.
2021-03-28 14:32:34 +02:00
Evan Haas
ab9324e604 translate-c: intcast compound assignment operand if different-sized integer
Use transCCast to cast the RHS of compound assignment if necessary.
2021-03-28 15:21:12 +03:00
Veikka Tuominen
df0f7f4692 translate-c: stop creating unnamed decls for typedefs child types 2021-03-25 16:59:26 +02:00
Luuk de Gram
4b854b75d2
Fix getNot and add test cases 2021-03-22 19:56:38 +01:00
Evan Haas
dce612ac2b translate-c: Ensure assignments are within a block when necessary
Ensures that if an assignment statement is the sole statement within a
C if statement, for loop, do loop, or do while loop, then when translated
it resides within a block, even though it does not in the original C.

Fixes the following invalid translation:

`if (1) if (1) 2;` -> `if (true) if (true) _ = @as(c_int, 2);`

To this:
```zig
    if (true) if (true) {
        _ = @as(c_int, 2);
    };
```

Fixes #8159
2021-03-22 10:48:08 +02:00
Veikka Tuominen
ac7217e1f5 translate-c: preserve zero fractional part in float literals 2021-03-18 22:46:00 +02:00
jacob gw
7c6eb41619 cbe tests: fix test on windows
"\n" -> std.cstr.line_sep
2021-03-18 22:45:01 +02:00
Jakub Konka
17c066e925
Merge pull request #8282 from kubkon/zld
macho: upstream zld linker
2021-03-18 19:14:17 +01:00
Evan Haas
b54514d9dd
translate-c: Use [N:0] arrays when initializer is a string literal (#8264)
* translate-c: Use [N:0] arrays when initializer is a string literal

Translate incomplete arrays as [N:0] when initialized by a string literal.
This preserves a bit more of the type information from the original C program.

Fixes #8215
2021-03-18 14:41:04 +02:00
Jakub Konka
f3b4f79c7f zld: temporarily disable testing shared lib linking 2021-03-18 11:10:31 +01:00
Ersikan
36db4b7cc4 test-cli: Remove temporary directory after tests 2021-03-17 10:27:41 +01:00
Ersikan
a354000090 zig fmt: fix non-UTF-8 encoding #2820
Fixes #2820

After reading the source code, the first two bytes are inspected, and
if they correspond to a UTF-16 BOM in little-endian order, the source
code is converted to UTF-8.
2021-03-17 10:26:47 +01:00
Evan Haas
715370a10a translate-c: demote usage of un-implemented builtins 2021-03-17 09:06:47 +02:00
Evan Haas
c558a1ae26 translate-c: Implement generic selection expressions
Enables translation of C code that uses the `_Generic` keyword
2021-03-17 00:07:33 +02:00
LemonBoy
27d07c6c4d std: Replace testing fns for floating-point values
Beside handling NaNs and other non-numeric values better we finally
offer the same pair of testing predicates in math and testing.
2021-03-14 17:23:47 -04:00
Andrew Kelley
ce14bc7176
Merge pull request #8152 from LemonBoy/fix-riscv-ret
stage1: Follow the C ABI for return types
2021-03-12 20:12:36 -05:00
LemonBoy
36c4037144 stage1: Add tests for C ABI integer return types 2021-03-12 11:50:56 +01:00
Andrew Kelley
e9a038c33b
Merge pull request #7934 from Vexu/stage2-cbe
Stage2 cbe: optionals and errors
2021-03-11 22:02:35 -05:00
Andrew Kelley
4fc6f631e0
Merge pull request #8126 from xackus/translate_c_int_literal_promotion
translate-c: promote int literals to bigger types
2021-03-11 14:32:37 -05:00
Evan Haas
c760532be0 translate-c: Add compound literal support 2021-03-08 10:09:12 +02:00
Evan Haas
d01bb21173 translate-c: Explicitly cast decayed array to pointer with @ptrCast
This enables translation of code that uses pointer arithmetic with arrays
2021-03-08 00:35:17 +02:00
jacob gw
30ffa052f2
stage2 cbe: add error union and error union operations 2021-03-08 00:33:59 +02:00
jacob gw
6467ef6d3b
cbe: add error comparison support 2021-03-08 00:33:59 +02:00
Veikka Tuominen
0a7be71bc2
stage2 cbe: non pointer optionals 2021-03-08 00:33:56 +02:00
Veikka Tuominen
ca20d0ea26
stage2 cbe: pointer like optionals 2021-03-08 00:32:52 +02:00
Evan Haas
6d69a29d75 translate-c: Support compound assignment of pointer and signed int
This handles `ptr += idx` and `ptr -= idx` when `idx` is a
signed integer expression.
2021-03-07 14:54:04 +02:00
Evan Haas
874c63f89c translate-c: translate align attribute for block scoped variables 2021-03-07 14:53:33 +02:00
Veikka Tuominen
8c6e7fb2c7
stage2: implement var args 2021-03-06 15:55:29 +02:00
xackus
eda1b53723 strip the leading zero from octal literals 2021-03-05 21:21:23 +01:00
xackus
eee43a65ae add tests 2021-03-05 21:21:23 +01:00
xackus
b4ef6fa09d fix test-translate-c 2021-03-05 21:04:27 +01:00
Evan Haas
291edafa1b translate-c: enable pointer arithmetic with signed integer operand
Given a pointer operand `ptr` and a signed integer operand `idx`

`ptr + idx` and `idx + ptr` -> ptr + @bitCast(usize, @intCast(isize, idx))
`ptr - idx` -> ptr - @bitCast(usize, @intCast(isize, idx))

Thanks @LemonBoy for pointing out that we can take advantage of wraparound
to dramatically simplify the code.
2021-03-05 14:16:40 +02:00
Veikka Tuominen
904f774563 translate-c: fix c tokenizer giving invalid tokens 2021-03-03 11:33:14 -08:00
g-w1
8b100792eb stage2: error set merging with tests
I had to come up with creative tests because we don't have error set type equality yet.
2021-03-02 23:53:05 -07:00
Andrew Kelley
713f113822 stage2: improve orelse implementation
* Now it supports being an lvalue (see additional lines in the test
   case).
 * Properly handles a pointer result location (see additional lines in
   the test case that assign the result of the orelse to a variable
   rather than a const).
 * Properly sets the result location type when possible, so that type
   inference of an `orelse` operand expression knows its result type.
2021-03-02 21:59:23 -07:00
Timon Kruiper
ed6757ece6 stage2: add a test for for loops in LLVM backend 2021-03-02 19:02:55 -07:00
Timon Kruiper
d4ec0279d3 stage2: add support for optionals in the LLVM backend
We can now codegen optionals! This includes the following instructions:
- is_null
- is_null_ptr
- is_non_null
- is_non_null_ptr
- optional_payload
- optional_payload_ptr
- br_void

Also includes a test for optionals.
2021-03-02 19:02:55 -07:00
Andrew Kelley
84f3b3dff2 re-enable behavior tests: translate-c macros
These were temporarily disabled in
dd973fb365 and we forgot re-enable
them until now.
2021-03-02 16:32:52 -07:00
Andrew Kelley
f296c95599
Merge pull request #8120 from joachimschmidt557/stage2-arm
stage2 ARM: implement basic integer multiplication
2021-03-01 19:11:37 -08:00
joachimschmidt557
278bd60732
stage2 ARM: Add tests for basic integer multiplication 2021-03-02 00:34:41 +01:00
Evan Haas
45d220cac6 translate-c: add <assert.h> support
Implement __builtin_expect so C code that uses assert() can be translated.
2021-03-01 10:34:23 +02:00
Evan Haas
294ee1bbc9 translate-c: add limited OffsetOfExpr support
Add support for OffsetOfExpr that contain exactly 1 component, when that component
is a field.

For example, given:

```c
struct S {
  float f;
  double d;
};
struct T {
  long l;
  int i;
  struct S s[10];
};
```

Then:
```c
offsetof(struct T, i)       // supported
offsetof(struct T, s[2].d)  // not supported currently
```
2021-02-28 21:56:57 +02:00
Michael Dusan
e65b6d99ac std.zig.fmtEscapes: update impacted test 2021-02-27 12:53:49 -07:00
Evan Haas
0816981561 translate-c: add typeof support 2021-02-25 22:33:42 -08:00
g-w1
153c97ac9e improve stage2 to allow catch at comptime:
* add error_union value tag.
* add analyzeIsErr
* add Value.isError
* add TZIR wrap_errunion_payload and wrap_errunion_err for
  wrapping from T -> E!T and E -> E!T
* add anlyzeInstUnwrapErrCode and analyzeInstUnwrapErr
* add analyzeInstEnsureErrPayloadVoid:
* add wrapErrorUnion
* add comptime error comparison for tests
* tests!
2021-02-25 16:41:16 -08:00
Evan Haas
4f11a88b9f translate-c: Add support for pointer subtraction
When two pointers are subtracted, both shall point to elements of the
same array object, or one past the last element of the array object;
the result is the difference of the subscripts of the two array elements.

The size of the result is implementation-defined, and its type
(a signed integer type) is ptrdiff_t defined in the <stddef.h> header.
If the result is not representable in an object of that type,
the behavior is undefined.

See C Standard, §6.5.6 [ISO/IEC 9899:2011]

Fixes #7216
2021-02-25 22:24:11 +02:00
Andrew Kelley
8e6c2b7a47 Merge remote-tracking branch 'origin/master' into ast-memory-layout 2021-02-24 15:08:23 -07:00
Veikka Tuominen
1f62e87031
fix formatting in translate-c test case 2021-02-23 10:53:25 +02:00