aboutsummaryrefslogtreecommitdiff
path: root/src-self-hosted/ir.zig
AgeCommit message (Collapse)Author
2020-09-21rename src-self-hosted/ to src/Andrew Kelley
2020-09-13stage2: rename ZigModule to ModuleAndrew Kelley
2020-09-13stage2: caching system integration & Module/Compilation splittingAndrew Kelley
* update to the new cache hash API * std.Target defaultVersionRange moves to std.Target.Os.Tag * std.Target.Os gains getVersionRange which returns a tagged union * start the process of splitting Module into Compilation and "zig module". - The parts of Module having to do with only compiling zig code are extracted into ZigModule.zig. - Next step is to rename Module to Compilation. - After that rename ZigModule back to Module. * implement proper cache hash usage when compiling C objects, and properly manage the file lock of the build artifacts. * make versions optional to match recent changes to master branch. * proper cache hash integration for compiling zig code * proper cache hash integration for linking even when not compiling zig code. * ELF LLD linking integrates with the caching system. A comment from the source code: Here we want to determine whether we can save time by not invoking LLD when the output is unchanged. None of the linker options or the object files that are being linked are in the hash that namespaces the directory we are outputting to. Therefore, we must hash those now, and the resulting digest will form the "id" of the linking job we are about to perform. After a successful link, we store the id in the metadata of a symlink named "id.txt" in the artifact directory. So, now, we check if this symlink exists, and if it matches our digest. If so, we can skip linking. Otherwise, we proceed with invoking LLD. * implement disable_c_depfile option * add tracy to a few more functions
2020-08-31stage2: introduce the ability for Scope.Block to be comptimeAndrew Kelley
This gives zir_sema analysis the ability to check if the current scope is expected to be comptime.
2020-08-19stage2: implement global variablesVexu
2020-08-18stage2 codegen: proper abstraction for re-using dying operandsAndrew Kelley
closes #6064
2020-08-17stage2: astgen for if and while with error unionsVexu
2020-08-17stage2: fix optimization causing wrong optional child typesVexu
2020-08-13stage2: implement while loops (bool condition)Andrew Kelley
* 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-13stage2: implement safety checks at the zir_sema levelAndrew Kelley
2020-08-13stage2: split unwrap_optional to safe and unsafe verionsVexu
2020-08-13stage2: implement unwrap optionalVexu
2020-08-12stage2: astgen for while loopsAndrew Kelley
See #6021
2020-08-11stage2: basic support for parameters .debug_infoAndrew Kelley
see #6014
2020-08-02codegen: emit .debug_line ops for IR instructionsAndrew Kelley
2020-07-29stage2: codegen handles undefined valuesAndrew Kelley
* `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-29stage2: more progress towards mutable local variablesAndrew Kelley
* implement sema for runtime deref, store pointer, coerce_to_ptr_elem, and store * identifiers support being lvalues, except for decls is still TODO * codegen supports load, store, ref, alloc * introduce more MCValue union tags to support pointers * add load, ref, store typed IR instructions * add Type.isVolatilePtr
2020-07-28self-hosted: beginnings of stack allocationAndrew Kelley
Comment out non-x86_64 architectures for now in codegen.zig, because they all have compile errors for their codepaths anyway, and it was bloating the compilation speed and memory usage when stage1 tried to build self-hosted. Here's the panic message: "Backend architectures that don't have good support yet are commented out, to improve compilation performance. If you are interested in one of these other backends feel free to uncomment them. Eventually these will be completed, but stage1 is slow and a memory hog." This is a workaround to lower the time it takes to build self-hosted with stage1 as well as use less memory. It should fix the CI. Additionally: * Add `single_mut_pointer` support to `Type` * Trivial implementation of stack allocation in codegen.zig. It does not deal with freeing yet, and it's missing the stack pointer adjustment prologue. * Add the `alloc` IR instruction and semantic analysis for `alloc` ZIR instruction.
2020-07-21stage2: add floatCast to zir and irVexu
2020-07-21stage2: implement some casts for numbersVexu
2020-07-20stage2: register allocator processes operand deathsAndrew Kelley
also rework the IR data structures
2020-07-20stage2: fix implementation of liveness operandDies()Andrew Kelley
2020-07-15stage2: local constsAndrew Kelley
These are now supported enough that this example code hits the limitations of the register allocator: fn add(a: u32, b: u32) void { const c = a + b; // 7 const d = a + c; // 10 const e = d + b; // 14 assert(e == 14); } // error: TODO implement copyToNewRegister So now the next step is to implement register allocation as planned.
2020-07-13stage2: fix liveness analysis of Call instructionsAndrew Kelley
2020-07-13stage2: add ZIR support for BoolNotAndrew Kelley
2020-07-13stage2: add implicit return void where applicableAndrew Kelley
2020-07-11run zig fmt on std lib and self hostedVexu
2020-07-08stage2: `if` AST=>ZIRAndrew Kelley
2020-07-08stage2: machine code for condbr jumpsAndrew Kelley
2020-07-07stage2: progress towards Block and CondBr codegenAndrew Kelley
2020-07-06stage2: codegen skeleton for cmp and subAndrew Kelley
2020-07-06stage2: skeleton codegen for x64 ADDAndrew Kelley
also rework Module to take advantage of the new hash map implementation.
2020-07-05stage2: implement liveness analysisAndrew Kelley
2020-06-26self-hosted: working towards conditional branching test caseAndrew Kelley
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-18self-hosted: implement Decl lookupAndrew Kelley
* 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-05-15move Module to its own fileAndrew Kelley
2020-05-15fix memory leaks of one of the ZIR test casesAndrew Kelley
2020-05-15self-hosted: update main.zigAndrew Kelley
After this commit there are no more bit rotted files. The testing program that was in ir.zig has been moved to main.zig Unsupported command line options have been deleted, or error messages added. The compiler repl is available from the build-exe, build-lib, build-obj commands with the --watch option. The main zig build script now builds the self-hosted compiler unconditionally. Linking against LLVM is behind a -Denable-llvm flag that defaults to off.
2020-05-15fix the global offset table code and updating decl exportsAndrew Kelley
2020-05-14fix codegen of sentinel-terminated arrays and .got alignmentAndrew Kelley
we now have an exit(0) program working
2020-05-14self-hosted: fix codegen and resolve some analysis bugsAndrew Kelley
2020-05-13work around stage1 compiler bugAndrew Kelley
breaking from inside the block with defers in scope triggered broken LLVM module found: Terminator found in the middle of a basic block!
2020-05-13self-hosted: fix compile errors, except for codegen.zigAndrew Kelley
2020-05-12self-hosted: ir: implement separated analysis of Decl and FnAndrew Kelley
2020-05-12self-hosted: link: global offset table support for declsAndrew Kelley
2020-05-12self-hosted: rework the memory layout of ir.Module and related typesAndrew Kelley
* 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-10rework self-hosted compiler for incremental buildsAndrew Kelley
* 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-01rework std.math.big.IntAndrew Kelley
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-01zir: add breakpoint() instruction and object file abilityAndrew Kelley
2020-05-01link: introduce the concept of output mode and link modeAndrew Kelley