6260 Commits

Author SHA1 Message Date
Andrew Kelley
ea902ffe8f Sema: reimplement runtime switch
Now supports multiple items pointing to the same body. This is a common
pattern even when using a jump table, with multiple cases pointing to
the same block of code.

In the case of a range specified, the items are moved to branches in the
else body. A future improvement may make it possible to have jump table
items as well as ranges pointing to the same block of code.
2021-07-20 12:19:17 -07:00
Luuk de Gram
caa0de545e Resolve regressions
- Get correct types in wasm backend.
- `arg` is already a `Ref`, therefore simply use `@intToEnum`.
- Fix regression in `zirBoolBr, where the order of insertion was incorrect.
2021-07-20 12:19:17 -07:00
Luuk de Gram
1150fc13dc wasm: Resolve regressions, add intcast support 2021-07-20 12:19:16 -07:00
Andrew Kelley
95756299af stage2: fix compile errors in LLVM backend 2021-07-20 12:19:16 -07:00
Lewis Gaul
bf8e347b1b Get codegen.zig to compile - use '{d}' format for Air.Inst.Index values 2021-07-20 12:19:16 -07:00
Lewis Gaul
7381431e68 Get register_manager.zig tests to compile - use value '1' as mock Air.Inst.Index 2021-07-20 12:19:16 -07:00
Luuk de Gram
1bc3bfc04b Implement switch_br dump 2021-07-20 12:19:16 -07:00
Luuk de Gram
480242b78a Debug info - Implement more instructions:
- bin_op
- un_op
- block
- struct_field_ptr
- br
- condbr

Also updates constant to write the actual Type, rather than the enum tag of the `Ref`.
2021-07-20 12:19:16 -07:00
Andrew Kelley
44fe9c52e1 stage2: wasm backend: update to latest naming convention 2021-07-20 12:19:16 -07:00
Andrew Kelley
934ebbe900 stage2: fix AIR not instruction (see prev commit) 2021-07-20 12:19:16 -07:00
Jacob G-W
414b144257 cbe: fix not (it is a ty_op, not un_op) 2021-07-20 12:19:16 -07:00
Andrew Kelley
4a0f38bb76 stage2: update LLVM backend to new AIR memory layout
Also fix compile errors when not using -Dskip-non-native
2021-07-20 12:19:16 -07:00
Andrew Kelley
33aab2c1bb stage2: ELF linking: avoid crashing for stupidly large functions 2021-07-20 12:19:16 -07:00
Andrew Kelley
b2733a36f8 Sema: memoize decl_val instructions when result is constant 2021-07-20 12:19:16 -07:00
Andrew Kelley
761f36ff93 stage2: rework C backend for new AIR memory layout 2021-07-20 12:19:16 -07:00
Jacob G-W
a804de13c8 plan9 linker: fix after testing
* exports get rendered properly in symbol table
* global offset table is at the start of data section
  instead of after symtab
* various null use fixes
2021-07-20 12:19:16 -07:00
Andrew Kelley
d17f492017 stage2: miscellaneous fixes for the branch
* Breaking language change: inline assembly must use string literal
   syntax. This is in preparation for inline assembly improvements that
   involve more integration with the Zig language. This means we cannot
   rely on text substitution.
 * Liveness: properly handle inline assembly and function calls with
   more than 3 operands.
   - More than 35 operands is not yet supported. This is a low priority
     to implement.
   - This required implementation in codegen.zig as well.
 * Liveness: fix bug causing incorrect tomb bits.
 * Sema: enable switch expressions that are evaluated at compile-time.
   - Runtime switch instructions still need to be reworked in this
     branch. There was a TODO left here (by me) with a suggestion to do
     some bigger changes as part of the AIR memory reworking. Now that
     time has come and I plan to honor the suggestion in a future commit
     before merging this branch.
 * AIR printing: fix missing ')' on alive instructions.

We're back to "hello world" working for the x86_64 backend.
2021-07-20 12:19:16 -07:00
Luuk de Gram
2438f61f1c Refactor entire wasm-backend to use new AIR memory layout 2021-07-20 12:19:16 -07:00
Luuk de Gram
424f260f85 Fix wasm-related compile errors:
- Update `fail()` to not require a `srcLoc`.
This brings it in line with other backends, and we were always passing 'node_offset = 0', anyway.
- Fix unused local due to change of architecture wrt function/decl generation.
- Replace all old instructions to indexes within the function signatures.
2021-07-20 12:19:16 -07:00
Andrew Kelley
8082660118 stage2: codegen.zig updated to new AIR memory layout 2021-07-20 12:19:16 -07:00
Andrew Kelley
eadbee2041 stage2: first pass at printing AIR/Liveness to text
* some instructions are not implemented yet
 * fix off-by-1 in Air.getMainBody
 * Compilation: use `@import("builtin")` rather than `std.builtin`
   for the values that are different for different build configurations.
 * Sema: avoid calling `addType` in between
   air_instructions.ensureUnusedCapacity and corresponding
   appendAssumeCapacity because it can possibly add an instruction.
 * Value: functions print their names
2021-07-20 12:19:16 -07:00
Andrew Kelley
12c10139e3 Sema: finish reworking for AIR memory layout except switch 2021-07-20 12:19:16 -07:00
Andrew Kelley
0da6633909 Sema: fix implementation of getTypeOf
and rename it to typeOf
2021-07-20 12:19:16 -07:00
Andrew Kelley
1294ebe1f5 Sema: AIR memory layout reworking for noreturn instructions 2021-07-20 12:19:16 -07:00
Andrew Kelley
c020a30296 Sema: remove br_block_flat AIR instruction
Thanks to the new AIR memory layout, we can do this by turning a br
operand into a block, rather than having this special purpose
instruction.
2021-07-20 12:19:16 -07:00
Andrew Kelley
27be4f3140 Sema: more AIR memory layout reworking progress
Additionally: ZIR encoding for floats now supports float literals up to
f64, not only f32. This is because we no longer need a source location
for this instruction.
2021-07-20 12:19:16 -07:00
Andrew Kelley
7bb2d13a09 stage2: remove ZIR instructions bool_and and bool_or
These were unused. I believe this happened with the introduction of
bool_br_and and bool_br_or instructions.
2021-07-20 12:19:16 -07:00
Andrew Kelley
3c5927fb87 Sema: add a strategy for handling costly source locations
Now you can pass `.unneeded` for a `LazySrcLoc` and if there ended up
being a compile error that needed it, you'll get
`error.NeededSourceLocation`.

Callsites can now exploit this error to do the expensive computation
to produce a source location object and then repeat the operation.
2021-07-20 12:19:16 -07:00
Andrew Kelley
dbd3529d1f Sema: first pass reworking for AIR memory layout 2021-07-20 12:19:16 -07:00
Andrew Kelley
f17a05bfb7 CLI: add plan9 -ofmt help text 2021-07-20 12:19:16 -07:00
Jacob G-W
91b1896184 plan9 linker: make more incremental
The incrementalness is now roughly the same as the c backend
rather than the spirv backend before.
2021-07-20 12:19:16 -07:00
Jacob G-W
3a41e4430e codegen: add FnResult type which is a Result that removes externally_managed 2021-07-20 12:19:16 -07:00
Andrew Kelley
c09b973ec2 stage2: compile error fixes for AIR memory layout branch
Now the branch is compiling again, provided that one uses
`-Dskip-non-native`, but many code paths are disabled. The code paths
can now be re-enabled one at a time and updated to conform to the new
AIR memory layout.
2021-07-20 12:19:16 -07:00
Andrew Kelley
0f38f68696 stage2: Air and Liveness are passed ephemerally
to the link infrastructure, instead of being stored with Module.Fn. This
moves towards a strategy to make more efficient use of memory by not
storing Air or Liveness data in the Fn struct, but computing it on
demand, immediately sending it to the backend, and then immediately
freeing it.

Backends which want to defer codegen until flush() such as SPIR-V
must move the Air/Liveness data upon `updateFunc` being called and keep
track of that data in the backend implementation itself.
2021-07-20 12:19:16 -07:00
Andrew Kelley
913393fd3b stage2: first pass over Module.zig for AIR memory layout 2021-07-20 12:19:16 -07:00
Andrew Kelley
ee6432537e stage2: first pass over codegen.zig for AIR memory layout 2021-07-20 12:19:16 -07:00
Andrew Kelley
ef7080aed1 stage2: update Liveness, SPIR-V for new AIR memory layout
also do the inline assembly instruction
2021-07-20 12:19:16 -07:00
Andrew Kelley
9918a5fbe3 AstGen: remove unneeded field ref_start_index
Previously, this field was used because the Zir.Inst.Ref encoding
supported the concept of references to function parameters. However now
thanks to whole-file-astgen, the implementations of indexToRef and
refToIndex are trivial addition/subtraction of a comptime const integer.
2021-07-20 12:19:14 -07:00
Andrew Kelley
3c3abaf390 stage2: update liveness analysis to new AIR memory layout
It's pretty compact, with each AIR instruction only taking up 4 bits,
plus a sparse table for special instructions such as conditional branch,
switch branch, and function calls with more than 2 arguments.
2021-07-20 12:18:14 -07:00
Andrew Kelley
5d6f7b44c1 stage2: rework AIR memory layout
This commit changes the AIR file and the documentation of the memory
layout. The actual work of modifying the surrounding code (in Sema and
codegen) is not yet done.
2021-07-20 12:18:14 -07:00
Loris Cro
e807020679 Fixed wrong "unable to load" error for non-existing import files
- Changed ZIR encoding of `import` metadata from having instruction
  indexes to storing token indexes.
2021-07-19 23:23:42 -04:00
Evan Haas
3e67ef5c9f translate-c: Handle underscore when used as an identifier
Use `@` syntax to escape `_` when used as an identifier.

Remove the stage1 astgen prohibition against assigning from `_`

Note: there a few stage1 bugs preventing `_` from being used as an identifier
for a local variable or function parameter; these will be fixed by stage2.
They are unlikely to arise in real C code since identifiers starting with
underscore are reserved for the implementation.
2021-07-19 12:56:23 +03:00
Jay Petacat
9979741bff stage1: Fix OsOther by adding missing OsPlan9 2021-07-16 19:35:25 -04:00
Daniele Cocca
b009aca38a src: return a null-terminated slice 2021-07-15 15:12:44 -04:00
leesongun
132b18e2b3
Fix bigint_shl (#9305) 2021-07-13 10:16:57 +03:00
Jacob G-W
74972531b5 fix doc comment in translate_c 2021-07-13 10:12:31 +03:00
Andrew Kelley
28dd9d478d C backend: TypedefMap is now ArrayHashMap
The C backend depends on insertion order into this map so that type
definitions will be declared before they are used.
2021-07-12 12:40:32 -07:00
Martin Wickham
bd1689ae89 Remove Stage1AirInst::owner_bb, use zir owner instead. 2021-07-11 19:16:57 -05:00
Andrew Kelley
7ef8546826
Merge pull request #9352 from g-w1/fix-9346
stage2 astgen: error for return outside of function scope
2021-07-11 15:11:28 -04:00
Jacob G-W
b0b9c3c2dc stage2: remove redundancy from error message
invalid 'try' outside function scope ->
'try' outside function scope
2021-07-11 10:03:47 -04:00