154 Commits

Author SHA1 Message Date
Andrew Kelley
193ad413f0 stage2: compiling C objects with clang
* add target_util.zig which has ported code from src/target.cpp
 * Module gains an arena that owns memory used during initialization
   that has the same lifetime as the Module. Useful for constructing
   file paths and lists of strings that have mixed lifetimes.
   - The Module memory itself is allocated in this arena. init/deinit
     are modified to be create/destroy.
   - root_name moves to the arena and no longer needs manual free
 * implement the ability to invoke `zig clang` as a subprocess
   - there are lots of TODOs that should be solved before merging
 * Module now requires a Random object and zig_lib_dir
 * Module now requires a path to its own executable or any zig
   executable that can do `zig clang`.
 * Wire up more CLI options.
 * Module creates "zig-cache" directory and "tmp" and "o" subdirectories
   ("h" is created by the cache_hash)
 * stubbed out some of the things linker code needs to do with TODO
   prints
 * delete dead code for computing compiler id. the previous commit
   eliminated the need for it.
 * add `zig translate-c` CLI option but it's not fully hooked up yet.
   It should be possible for this to be fully wired up before merging
   this branch.
 * `zig targets` now uses canonical data for available_libcs
2020-09-09 09:28:05 -07:00
Andrew Kelley
c99e34a00e stage2: eliminate the "compiler id" concept
Instead, append a "dirty suffix" to the version string when there are
dirty git changes and use the version string as the compiler id.

This avoids a dependency on the cache hash system, and saves time on
first invocation of the compiler since it does not have to compute its
compiler id. It also saves time by not having to check the cache for a
saved compiler id.
2020-09-09 09:28:05 -07:00
Andrew Kelley
35f334ae0f organize some TODO comments 2020-09-09 09:28:05 -07:00
Andrew Kelley
4056bb92e6 stage2: more progress moving zig cc to stage2
* std.cache_hash exposes Hasher type
 * std.cache_hash makes hasher_init a global const
 * std.cache_hash supports cloning so that clones can share the same
   open manifest dir handle as well as fork from shared hasher state
 * start to populate the cache_hash for stage2 builds
 * remove a footgun from std.cache_hash add function
 * get rid of std.Target.ObjectFormat.unknown
 * rework stage2 logic for resolving output artifact names by adding
   object_format as an optional parameter to std.zig.binNameAlloc
 * support -Denable-llvm in stage2 tests
 * Module supports the use case when there are no .zig files
 * introduce c_object_table and failed_c_objects to Module
 * propagate many new kinds of data from CLI into Module and into
   linker.Options
 * introduce -fLLVM, -fLLD, -fClang and their -fno- counterparts.
   closes #6251.
   - add logic for choosing when to use LLD or zig's self-hosted linker
 * stub code for implementing invoking Clang to build C objects
 * add -femit-h, -femit-h=foo, and -fno-emit-h CLI options
2020-09-09 09:28:05 -07:00
Andrew Kelley
d7268cbb24
Merge pull request #6232 from LemonBoy/fix-readall
std: Don't trust stat() size in readAllAlloc fns
2020-09-06 19:19:57 -04:00
LemonBoy
90743881cf std: Minor changes to the fs module
* Add a size_hint parameter to the read{toEnd,File}AllocOptions fns
* Rename readAllAlloc{,Options} to readToEndAlloc{,Options} as they
  don't rewind the file before reading
* Fix missing rewind in test case
2020-09-04 10:17:00 +02:00
Alexandros Naskos
e9b137f23a Completed basic PE linker for stage2
Added std.coff.MachineType
Added image characteristic and section flag valued to std.coff
Added std.Target.Cpu.Arch.toCoffMachine
Fixed stage2 --watch flag on windows
2020-09-04 05:15:03 +03:00
Alexandros Naskos
fac9a4e286 Start working on PE/COFF linking. 2020-09-04 05:12:26 +03:00
Andrew Kelley
17f36566de stage2: upgrade Scope.Container decls from ArrayList to HashMap 2020-09-03 15:02:38 -07:00
Vexu
6f0126e957
stage2: split Scope.Container from Scope.File 2020-09-03 15:06:45 +03:00
Vexu
6ab0ac161e
stage2: slice return type analysis 2020-09-03 15:05:47 +03:00
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
Andrew Kelley
4c13d020db stage2: proper split of requireRuntimeBlock and requireFunctionBlock
* improve the ZIR generated of variable decls
   - utilize the same ZIR for the type and init value when possible
   - init value gets a result location with the variable type.
     no manual coercion is required.
 * no longer use return instructions to extract values out of comptime
   blocks. Instead run the analysis and then look at the corresponding
   analyzed instruction, relying on the comptime mechanism to report
   errors when something could not be comptime evaluated.
2020-09-01 12:39:47 -07:00
Andrew Kelley
717b0e8275 stage2: introduce the ability for Scope.Block to be comptime
This gives zir_sema analysis the ability to check if the current scope
is expected to be comptime.
2020-08-31 23:34:58 -07:00
Tadeo Kondrak
d3e5105ecc
std.zig.ast: make getTrailer/setTrailer private and add getters/setters 2020-08-30 17:18:25 -06:00
Tadeo Kondrak
3c87872dc5
stage2: Update to new std.meta.TrailerFlags API 2020-08-27 15:21:19 -06:00
Andrew Kelley
b0995cb9f9 stage2: correct logic for analyzeIsNull 2020-08-26 19:53:36 -07:00
Vexu
fb28349349 stage2: astgen catch 2020-08-26 19:50:56 -07:00
Andrew Kelley
16d54c70eb stage2: getErrorValue takes advantage of HashMap getOrPut API 2020-08-24 15:41:59 -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
Andrew Kelley
f54b2e2da6 add missing mutability check in simplePtrType 2020-08-20 17:08:01 -07:00
Vexu
6a053ffcc8 stage2: comptime decl 2020-08-20 16:42:48 -07:00
Vexu
d312d64c9a stage2: slice types 2020-08-20 16:42:48 -07:00
Vexu
9568248450 stage2: complex pointer types 2020-08-20 16:42:48 -07:00
Vexu
ebfe723f3c stage2: implement rest of simple pointer types 2020-08-20 16:42:48 -07:00
Vexu
d25674a51e
disallow extern variables with initializers 2020-08-20 11:35:33 +03:00
Andrew Kelley
9ec9c0f5e5 optimize the memory layout of Module.Fn and Module.Var
`is_pub` added to `Fn` would cost us an additional 8
bytes of memory per function, which is a real bummer
since it's only 1 bit of information.

If we wanted to really remove this, I suspect we could
make this a function isPub() which looks at the AST of
the corresponding Decl and finds if the FnProto AST node
has the pub token. However I saw an easier approach -

The data of whether something is pub or not is actually
a property of a Decl anyway, not a function, so we can
look at moving the field into Decl. Indeed, doing this,
we see that Decl already has deletion_flag: bool which
is hiding in the padding bytes between the enum (1 byte)
and the following u32 field (generation). So if we put
the is_pub bool there, it actually will take up no
additional space, with 1 byte of padding remaining.

This was an easy reworking of the code since any
func.is_pub could be changed simply to func.owner_decl.is_pub.

I also modified `Var` to make the init value non-optional
and moved the optional bit to a has_init: bool field. This is worse from
the perspective of control flow and safety, however it makes
`@sizeOf(Var)` go from 32 bytes to 24 bytes. The more code we can fit
into memory at once, the more justified we are in using the compiler as
a long-running process that does incremental updates.
2020-08-19 17:52:22 -07:00
Vexu
9801047bdb stage2: handle var attributes 2020-08-19 16:12:29 -07:00
Vexu
338a495648 stage2: implement global variables 2020-08-19 16:12:29 -07:00
Andrew Kelley
dc35b8641b stage2: Module enumerates the possible top level decls 2020-08-18 23:11:56 -07:00
Vexu
7c15c9428e
stage2: array types 2020-08-18 12:36:00 +03:00
Isaac Freund
60fb50ee5a
stage2/wasm: write exports on flush, cleanup
Exports now have a dirty flag and are rewritten on flush if this flag
has been set.

A couple other minor changes have been made based on Andrew's review.
2020-08-18 01:01:13 +02:00
Isaac Freund
3370b5f109
stage2/wasm: implement basic container generation
Thus far, we only generate the type, function, export, and code
sections. These are sufficient to generate and export simple functions.

Codegen is currently hardcoded to `i32.const 42`, the main goal of this
commit is to create infrastructure for the container format which will
work with incremental compilation.
2020-08-18 00:32:58 +02:00
Veikka Tuominen
d8fb377e2a
Merge pull request #6060 from Vexu/stage2
Stage2: more optionals stuff
2020-08-18 00:15:36 +03:00
Andrew Kelley
624e643872
Merge pull request #6046 from heidezomp/std-log-scoped-part2
std.log: (breaking) remove scope parameter from logging functions
2020-08-17 16:59:27 -04:00
Vexu
13b2f1e90b
address review feedback 2020-08-17 19:21:03 +03:00
Jakub Konka
e23fc3905f Add skeleton for MachO support in stage2
This commit adds an empty skeleton for MachO format support in stage2.
2020-08-17 18:16:29 +02:00
Vexu
012fac255f
stage2: fix optimization causing wrong optional child types 2020-08-17 14:26:32 +03:00
Noam Preil
93619a5e4e
Module: panic when encountering unimplemented node 2020-08-16 20:32:51 -04:00
Andrew Kelley
0f3f96c850 stage2: astgen for labeled blocks and labeled breaks 2020-08-15 00:52:25 -07:00
Andrew Kelley
b49d3672f3 stage2 astgen for LabeledBlock 2020-08-14 22:50:00 -04:00
Andrew Kelley
9a5a1013a8 std.zig.ast: extract out Node.LabeledBlock from Node.Block
This is part of an ongoing effort to reduce size of in-memory AST. This
enum flattening pattern is widespread throughout the self-hosted
compiler.

This is a API breaking change for consumers of the self-hosted parser.
2020-08-14 22:50:00 -04:00
Andrew Kelley
7a39a038db stage2: proper semantic analysis of improper returning of implicit void 2020-08-14 13:08:55 -07:00
Andrew Kelley
5f7c7191ab stage2: astgen for non-labeled blocks 2020-08-14 11:28:40 -07:00
Andrew Kelley
28a9da8bfc stage2: implement while loops (bool condition)
* introduce a dump() function on Module.Fn which helpfully prints to
   stderr the ZIR representation of a function (can be called before
   attempting to codegen it). This is a debugging tool.
 * implement x86 codegen for loops
 * liveness: fix analysis of conditional branches. The logic was buggy
   in a couple ways:
   - it never actually saved the results into the IR instruction (fixed now)
   - it incorrectly labeled operands as dying when their true death was
     after the conditional branch ended (fixed now)
 * zir rendering is enhanced to show liveness analysis results. this
   helps when debugging liveness analysis.
 * fix bug in zir rendering not numbering instructions correctly

closes #6021
2020-08-13 20:32:32 -07:00
Veikka Tuominen
f5b99abc93
Merge pull request #6045 from Vexu/block
Add error for unused/duplicate labels.
2020-08-14 01:04:05 +03:00
Andrew Kelley
ec4953504a stage2: implement safety checks at the zir_sema level 2020-08-13 10:04:46 -07:00