71 Commits

Author SHA1 Message Date
Andrew Kelley
b75a51f94b stage2: implement function calling convention for calls 2020-07-13 00:08:21 -07:00
Vexu
e85fe13e44
run zig fmt on std lib and self hosted 2020-07-11 20:41:19 +03:00
Andrew Kelley
7bd0500589 Merge remote-tracking branch 'origin/master' into register-allocation 2020-07-08 20:46:06 -07:00
Andrew Kelley
8e425c0c8d stage2: if AST=>ZIR 2020-07-08 20:33:33 -07:00
Andrew Kelley
5e60872060 stage2 misc fixes 2020-07-08 06:56:20 +00:00
Andrew Kelley
ab9df5b04b stage2: machine code for condbr jumps 2020-07-08 05:35:41 +00:00
Noam Preil
b4c571301b
Stage2: Refactor in preparation for C backend 2020-07-07 14:55:44 -04:00
Andrew Kelley
b55d0193e4 stage2: progress towards Block and CondBr codegen 2020-07-07 08:01:54 +00:00
Andrew Kelley
12737c9a30 stage2: codegen skeleton for cmp and sub 2020-07-06 09:21:57 +00:00
Andrew Kelley
8be8ebd698 stage2: skeleton codegen for x64 ADD
also rework Module to take advantage of the new hash map implementation.
2020-07-06 06:10:44 +00:00
Andrew Kelley
8fb392dbb4 stage2: implement liveness analysis 2020-07-05 23:20:08 +00:00
Andrew Kelley
3a89f214aa update more HashMap API usage 2020-07-05 21:11:42 +00:00
Andrew Kelley
1eed0cf0f3 zig fmt and delete unused type 2020-06-28 19:45:10 -04:00
Andrew Kelley
aa92446365 stage2: implement function parameters
In codegen.zig, the std.Target.Cpu.Arch is now generally available as a
comptime value where needed. This is a tradeoff that causes the compiler
binary to be more bloated, but gives us higher performance, since the
optimizer can optimize per architecture (which is usually how compilers
are designed anyway, with different code per-architecture), and it also
allows us to use per-architecture types, such as a Register enum that is
specific to the comptime-known architecture.

Adds abiSize method to Type.
2020-06-28 19:45:10 -04: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
c9a0ec25e0 self-hosted: add Tracy integration
This tool helps give an intuitive picture of performance. This will help
us understand where to improve the code.
2020-06-18 21:55:37 -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
Noam Preil
834e8ac2dc [Stage2/Codegen] Extract REX 2020-05-29 13:33:09 -04:00
Noam Preil
07472fb453 [Stage2/Codegen] Properly handle arch in genCall 2020-05-24 20:49:29 -04:00
Noam Preil
70239802c9 [Stage2/Codegen] Fix a typo 2020-05-19 22:27:22 -04:00
Andrew Kelley
8d3cca7fc2 stage2: function calls using the global offset table
so far they don't support parameters or return values
2020-05-19 13:51:46 -04:00
Andrew Kelley
8d812dba30 stage2: set up a trampoline table for functions
However there does not appear to be an x86 encoding for calling an
immediate address. So there's no point of setting this up. We should
just emit an indirect call to the got addr.
2020-05-19 13:33:36 -04:00
Andrew Kelley
88c8ff6e37 move some files around 2020-05-17 12:08:47 -04:00
Noam Preil
e2196a458f Minor cleanup 2020-05-17 12:03:01 -04:00
Noam Preil
13ea698a40 rework x64 genSetReg 2020-05-17 12:03:01 -04:00
Andrew Kelley
f2feb4e47a move Module to its own file 2020-05-15 21:44:33 -04:00
Andrew Kelley
ebb81ebe59 fix the global offset table code and updating decl exports 2020-05-15 01:22:04 -04:00
Andrew Kelley
81a01bd481 fix codegen of sentinel-terminated arrays and .got alignment
we now have an exit(0) program working
2020-05-14 16:34:04 -04:00
Andrew Kelley
0986dcf1cf self-hosted: fix codegen and resolve some analysis bugs 2020-05-14 13:20:27 -04:00
Andrew Kelley
6a2425c38c self-hosted: fix the rest of the compile errors 2020-05-13 22:12:38 -04:00
Andrew Kelley
080022f6c6 self-hosted: fix compile errors, except for codegen.zig 2020-05-13 20:06:01 -04:00
Andrew Kelley
e3a0fac1a7 self-hosted: link: global offset table support for decls 2020-05-12 20:11:47 -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
751903ba8f zir: add breakpoint() instruction and object file ability 2020-05-01 06:47:20 -04:00
Andrew Kelley
f89dbe6c4e link: introduce the concept of output mode and link mode 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
058937e44d bug fixes to make it work 2020-04-24 02:09:30 -04:00
Andrew Kelley
2d35f71fa9 codegen rdx set immediate 2020-04-23 20:30:20 -04:00
Andrew Kelley
757d13d784 codegen supports embedded-in-code constants
also coerce no longer requires a bitcast
2020-04-23 20:23:16 -04:00
Andrew Kelley
0ac502f372 codegen for setting rax and rdi registers 2020-04-23 19:40:17 -04:00
Andrew Kelley
63b54bcf51 codegen for inline assembly 2020-04-23 18:58:47 -04:00
Andrew Kelley
99ec614b71 codegen for const ints and string literals 2020-04-23 17:46:01 -04:00
Andrew Kelley
24a01eed90 basics of writing ELF and machine code generation 2020-04-23 16:41:20 -04:00
Andrew Kelley
2e806682f4
(breaking) std.Buffer => std.ArrayListSentineled(u8, 0)
This new name (and the fact that it is a function returning a type) will
make it more clear which use cases are better suited for ArrayList and
which are better suited for ArrayListSentineled.

Also for consistency with ArrayList,
 * `append` => `appendSlice`
 * `appendByte` => `append`

Thanks daurnimator for pointing out the confusion of std.Buffer.
2020-04-01 13:30:07 -04:00
Andrew Kelley
9e7ae06249
std lib API deprecations for the upcoming 0.6.0 release
See #3811
2020-03-30 14:23:22 -04:00
Michaël Larouche
9b11e5e1f1 Add InstallRawStep to Zig build system that does a similar job to llvm-objcopy. To use it, do 'exe.installRaw("kernel.bin");' where exe is a LibExeObjStep
Part of #2826
2020-02-03 15:07:34 -05:00
Andrew Kelley
7c1dbfab72
self-hosted: manually parse args 2019-12-11 02:08:33 -05:00
Andrew Kelley
fe4963412f
update self-hosted compiler to new format API 2019-12-08 23:17:03 -05:00
Vexu
36849d8a7b
fixes and cleanup in self hosted 2019-11-26 11:52:12 +02:00
Andrew Kelley
34b1ebefaa
Merge remote-tracking branch 'origin/master' into null-terminated-pointers 2019-11-24 20:28:46 -05:00