81 Commits

Author SHA1 Message Date
Vexu
9a59cdcd41
stage2: various small type fixes 2020-09-03 15:05:10 +03:00
Sahnvour
575fbd5e35 hash_map: rename to ArrayHashMap and add new HashMap implementation 2020-09-02 00:17:50 +02:00
Vexu
b1aa2857ff stage2: astgen for loops 2020-08-26 19:50:56 -07:00
Vexu
5de9aac749 stage2: error set types 2020-08-24 15:36:42 -07:00
Vexu
1520e084cb stage2: implement accessing error values 2020-08-24 15:36:42 -07:00
Vexu
e9b15ac9a0 stage2: error set declarations 2020-08-24 15:36:42 -07:00
Vexu
16d7db59ed stage2: anyframe and error union types 2020-08-24 15:36:42 -07:00
Vexu
5fdcb1a792 stage2: emit zir variable fix, array type and enum literal support 2020-08-19 16:12:29 -07:00
Vexu
338a495648 stage2: implement global variables 2020-08-19 16:12:29 -07:00
Vexu
e0b01bd4a9
stage2: enum literals 2020-08-18 14:28:33 +03:00
Isaac Freund
fc850aad61 stage2: fix signed <-> unsigned Value casts 2020-08-17 01:40:46 -04:00
Noam Preil
1eb5aaa4b5 CBE: renderValue pays attention to Type, not Tag 2020-08-12 21:58:21 -07:00
Vexu
1f8f434b9c
compiler_rt: add floatditf 2020-07-29 18:03:44 +03:00
Andrew Kelley
4beff80b2f stage2: codegen handles undefined values
* `optimize_mode` is passed to `link.File` and stored there
 * improve the debugging function `Module.dumpInst`
 * get rid of `Value.the_one_possible_value` in favor of a few more
   specific values for different types. This is less buggy, one less
   footgun.
 * `Type.onePossibleValue` now returns a `?Value` instead of `bool`.
 * codegen handles undefined values. `undef` is a new `MCValue` tag.
   It uses 0xaa values depending on optimization mode. However
   optimization mode does not yet support scope overrides.
 * link.zig: move the `Options` field from `File.Elf` and `File.C` to
   the base struct.
   - fix the Tag enum to adhere to style conventions
 * ZIR now supports emitting undefined values.
 * Fix the logic of comptime math to properly compare against zero using
   the `compareWithZero` function.
2020-07-29 02:29:37 -07:00
Vexu
dd89297388
stage2: actually implement float casting 2020-07-21 22:34:14 +03:00
Vexu
c29c79b17a
stage2: remove some dead code, fix build on aarch64 2020-07-21 22:34:14 +03:00
Vexu
7e7d1df4da
stage2: add floatCast to zir and ir 2020-07-21 22:34:12 +03:00
Vexu
da217fadeb
stage2: astgen for floats and other primitive literals 2020-07-21 22:29:28 +03:00
Vexu
e77ca6af70
stage2: add float values 2020-07-21 22:29:28 +03:00
Andrew Kelley
a92990f993 stage2: implement enough for assert() function to codegen 2020-07-14 02:24:12 -07:00
Vexu
e85fe13e44
run zig fmt on std lib and self hosted 2020-07-11 20:41:19 +03:00
Andrew Kelley
130c7fd23b self-hosted: working towards conditional branching test case
New features:
 * Functions can have parameters in semantic analysis. Codegen
   is not implemented yet.
 * Support for i8, i16, i32, i64, u8, u16, u32, u64 primitive
   identifiers.
 * New ZIR instructions: arg, block, and breakvoid

Implementation details:

 * Move Module.Body to ir.Body
 * Scope.Block gains a parent field and an optional Label field
 * Fix bug in integer type equality comparison.

Here's the test case I'm working towards:

```
@void = primitive(void)
@i32 = primitive(i32)
@fnty = fntype([@i32, @i32], @void)

@0 = str("entry")
@1 = export(@0, "entry")

@entry = fn(@fnty, {
  %0 = arg(0)
  %1 = arg(1)
  %2 = add(%0, %1)
  %3 = int(7)
  %4 = block("if", {
    %neq = cmp(%2, neq, %3)
    %5 = condbr(%neq, {
      %6 = unreachable()
    }, {
      %7 = breakvoid("if")
    })
  })
  %11 = returnvoid()
})
```

$ ./zig-cache/bin/zig build-obj test.zir
test.zir:9:12: error: TODO implement function parameters for Arch.x86_64

That's where I left off.
2020-06-26 02:30:14 -04:00
Andrew Kelley
7e58c56ca7 self-hosted: implement Decl lookup
* Take advantage of coercing anonymous struct literals to struct types.
 * Reworks Module to favor Zig source as the primary use case.
   Breaks ZIR compilation, which will have to be restored in a future commit.
 * Decl uses src_index rather then src, pointing to an AST Decl node
   index, or ZIR Module Decl index, rather than a byte offset.
 * ZIR instructions have an `analyzed_inst` field instead of Module
   having a hash table.
 * Module.Fn loses the `fn_type` field since it is redundant with
   its `owner_decl` `TypedValue` type.
 * Implement Type and Value copying. A ZIR Const instruction's TypedValue
   is copied to the Decl arena during analysis, which allows freeing the
   ZIR text instructions post-analysis.
 * Don't flush the ELF file if there are compilation errors.
 * Function return types allow arbitrarily complex expressions.
 * AST->ZIR for function calls and return statements.
2020-06-18 17:12:56 -04:00
Andrew Kelley
b4eac0414a stage2: hook up Zig AST to ZIR
* Introduce the concept of anonymous Decls
 * Primitive Hello, World with inline asm works
 * There is still an unsolved problem of how to manage ZIR instructions
   memory when generating from AST. Currently it leaks.
2020-06-18 17:12:56 -04:00
Andrew Kelley
cf654b52d6 stage2: -femit-zir respects decl names and supports cycles 2020-06-08 15:16:40 -04:00
Andrew Kelley
0bd89979fd stage2: handle deletions and better dependency resolution
* Deleted decls are deleted; unused decls are also detected as deleted.
   Cycles are not yet detected.
 * Re-analysis is smarter and will not cause a re-analysis of dependants
   when only a function body is changed.
2020-05-28 22:42:13 -04:00
Andrew Kelley
f2feb4e47a move Module to its own file 2020-05-15 21:44:33 -04:00
Andrew Kelley
0986dcf1cf self-hosted: fix codegen and resolve some analysis bugs 2020-05-14 13:20:27 -04:00
Andrew Kelley
080022f6c6 self-hosted: fix compile errors, except for codegen.zig 2020-05-13 20:06:01 -04:00
Andrew Kelley
a3da584248 self-hosted: ir: implement separated analysis of Decl and Fn 2020-05-12 23:59:46 -04:00
Andrew Kelley
619159cf48 self-hosted: rework the memory layout of ir.Module and related types
* add TypedValue.Managed which represents a Type, a Value, and some
   kind of memory management strategy.
 * introduce an analysis queue
 * flesh out how incremental compilation works with respect to exports
 * ir.text.Module is only capable of one error message during parsing
 * link.zig no longer has a decl table map and instead has structs that
   exist directly on ir.Module.Decl and ir.Module.Export
 * implement primitive .text block allocation
 * implement linker code for updating Decls and Exports
 * implement null Type

Some supporting std lib changes:
 * add std.ArrayList.appendSliceAssumeCapacity
 * add std.fs.File.copyRange and copyRangeAll
 * fix std.HashMap having modification safety on in ReleaseSmall builds
 * add std.HashMap.putAssumeCapacityNoClobber
2020-05-12 01:02:48 -04:00
Andrew Kelley
a32d3a85d2 rework self-hosted compiler for incremental builds
* introduce std.ArrayListUnmanaged for when you have the allocator
   stored elsewhere
 * move std.heap.ArenaAllocator implementation to its own file. extract
   the main state into std.heap.ArenaAllocator.State, which can be
   stored as an alternative to storing the entire ArenaAllocator, saving
   24 bytes per ArenaAllocator on 64 bit targets.
 * std.LinkedList.Node pointer field now defaults to being null
   initialized.
 * Rework self-hosted compiler Package API
 * Delete almost all the bitrotted self-hosted compiler code. The only bit
   rotted code left is in main.zig and compilation.zig
 * Add call instruction to ZIR
 * self-hosted compiler ir API and link API are reworked to support
   a long-running compiler that incrementally updates declarations
 * Introduce the concept of scopes to ZIR semantic analysis
 * ZIR text format supports referencing named decls that are declared
   later in the file
 * Figure out how memory management works for the long-running compiler
   and incremental compilation. The main roots are top level
   declarations. There is a table of decls. The key is a cryptographic
   hash of the fully qualified decl name. Each decl has an arena
   allocator where all of the memory related to that decl is stored.
   Each code block has its own arena allocator for the lifetime of
   the block. Values that want to survive when going out of scope in
   a block must get copied into the outer block. Finally, values must
   get copied into the Decl arena to be long-lived.
 * Delete the unused MemoryCell struct. Instead, comptime pointers are
   based on references to Decl structs.
 * Figure out how caching works. Each Decl will store a set of other
   Decls which must be recompiled when it changes.

This branch is still work-in-progress; this commit breaks the build.
2020-05-10 02:05:54 -04:00
Andrew Kelley
8766821157 rework std.math.big.Int
Now there are 3 types:
 * std.math.big.int.Const
   - the memory is immutable, only stores limbs and is_positive
   - all methods operating on constant data go here
 * std.math.big.int.Mutable
   - the memory is mutable, stores capacity in addition to limbs and
     is_positive
   - methods here have some Mutable parameters and some Const
     parameters. These methods expect callers to pre-calculate the
     amount of resources required, and asserts that the resources are
     available.
 * std.math.big.int.Managed
   - the memory is mutable and additionally stores an allocator.
   - methods here perform the resource calculations for the programmer.
   - this is the high level abstraction from before

Each of these 3 types can be converted to the other ones.

You can see the use case for this in the self-hosted compiler, where we
only store limbs, and construct the big ints as needed.

This gets rid of the hack where the allocator was optional and the
notion of "fixed" versions of the struct. Such things are now modeled
with the `big.int.Const` type.
2020-05-01 06:47:56 -04:00
Andrew Kelley
28729efe29 ZIR: implement return instruction 2020-05-01 06:47:20 -04:00
Andrew Kelley
6b0f7de247 ZIR: add cmp and condbr instructions 2020-05-01 06:47:20 -04:00
Andrew Kelley
d44c9bdbd9 ir: elemptr and add instructions 2020-04-26 01:20:58 -04:00
Andrew Kelley
24a01eed90 basics of writing ELF and machine code generation 2020-04-23 16:41:20 -04:00
Andrew Kelley
d58233b361 ir: improve ZIR emission enough to emit hello world 2020-04-22 03:08:50 -04:00
Andrew Kelley
993e654554 emit zir skeleton 2020-04-22 00:04:52 -04:00
Andrew Kelley
2e6ccec100 ir: analyze asm instruction 2020-04-21 22:19:32 -04:00
Andrew Kelley
8d3e4147d5 ir: analyze deref instruction 2020-04-21 21:33:55 -04:00
Andrew Kelley
2c11acf807 ir: analyze fieldptr instruction 2020-04-21 21:14:56 -04:00
Andrew Kelley
2cdbb5f472 ir: analyze int casting 2020-04-21 19:48:59 -04:00
Andrew Kelley
0746028a2a ir: analyze int instruction 2020-04-21 17:54:00 -04:00
Andrew Kelley
8671e8d6d4 ir: analyze fntype instruction 2020-04-21 17:06:09 -04:00
Andrew Kelley
22e7ca5613 ir: analysis of fn instruction 2020-04-21 16:06:15 -04:00
Andrew Kelley
8e0bcaca9b ir: analyze str instruction 2020-04-21 01:20:01 -04:00
Andrew Kelley
4c7507cceb ir: semantic analysis skeleton 2020-04-21 00:56:30 -04:00
Andrew Kelley
e74c5a7c24 ir: nice rendering of string literal constants 2020-04-19 20:44:18 -04:00
Andrew Kelley
ded6e0326d ir: rendering skeleton 2020-04-19 20:04:11 -04:00