LLVM
Table of Contents
Used by clang, Rust, Swift, Julia, Ruby, CUDA, and others.
- Lexer and Parser needs to be implemented seperately.
1. Structure
1.1. Frontend
- It provides tools for converting custom abstract syntax tree(AST) into intermediate representation(IR)
codegen()
1.2. Middleend
- It analyzes IR and optimize it.
1.3. Backend
- It convert IR into the machine code.
2. Intermediate Representation
- A statement must contain one of the op codes.
- No semicolons!
- Variables are immutable
- The inline dereferencing is not possible.
- No "unsigned" type exists, because there's no distintion.
- A basic block must end either with a terminating operation:
brorret.
2.1. Types
i1boolean, flagiNinteger,uNunsigned integer.Ncan be any number between 1 and 232 - 1.<ty>*pointer,ptrcan be used to automatically infer the pointer type
2.2. Instructions
Global variables and functions start with @.
Local variables and unnamed variables start with %.
<ty> *alloca <ty>define <ty> <fn>(<ty> <var>, ...) { ... }define functiondeclare <ty> <fn>(<ty>, ...)external functionsalloca <ty>[, <ty> <NumElements>][, align <alignment>]load <ty>, <ty*> <val>load from memorystore <ty> <val>, <ty*> <ptr>add <wrap> <ty> <val1>, <val2>submul…
icmp <cmp>, <ty> <val1>, <val2>br i1 <cond>, label <val1>, label <val2>,br label <val>call <ty> <fn>(...)ret <ty> <val>getelementptr <ty>, <ty> *<ptr>, <ty> <pos>, <ty> <offset>
Error handling using libunwind
resume <ty> <val>landingpad <ty> <val> [cleanup] [catch <ty> <val>] [filter <ty> <val>]
2.3. Tokens
- If the name is a number they must be in order starting from
0which representing the entry point. @varglobal collapsed:: true- Function name must start with
@?
- Function name must start with
%varlocal variablelabel:label#attrattribute of a function- The attributes are specified at the end of the
.llfile, and it is assigned to a function by adding it at the end of the function declaration:define <ty> <fn>(...) #attr { ... }
- The attributes are specified at the end of the
!flagflag
3. libunwind
Unwind (stack tracing) library developed as part of LLVM project.
In general error is handled in three main pathways:
- explicit error invocation within the source code -> unwind stack
- runtime error (if runtime exists) triggers error -> unwind stack
- os error -> signal received by the process -> either the signal handler triggers unwind, or OS perform default operation (core dump)