@@ -4,11 +4,11 @@ Halcyon is a work-in-progress compiler for a large subset of C, written in Haske
44
55## Current Status
66
7- The compiler currently handles the simplest subset of C programs: functions that return integer constants. For example:
7+ The compiler currently handles C programs with unary operators and integer constants. For example:
88
99``` c
1010int main (void) {
11- return 42 ;
11+ return ~ (-42) ;
1212}
1313```
1414
@@ -18,8 +18,9 @@ The compiler processes source code through the following stages:
1818
19191. **Lexical Analysis**: Breaks source code into a sequence of tokens
20202. **Parsing**: Converts tokens into an Abstract Syntax Tree (AST)
21- 3. **Code Generation**: Transforms AST into x86_64 assembly
22- 4. **Code Emission**: Outputs the assembly code to an executable
21+ 3. **TACKY Generation**: Transforms AST into TACKY intermediate representation
22+ 4. **Code Generation**: Transforms AST into x86_64 assembly
23+ 5. **Code Emission**: Outputs the assembly code to an executable
2324
2425### Internal Representations
2526
@@ -33,10 +34,24 @@ Programs are represented internally using a series of increasingly lower-level d
3334 , body :: Statement
3435 }
3536 data Statement = Return Expr
36- data Expr = Constant Int
37+ data Expr = Constant Int | Unary UnaryOp Expr
38+ data UnaryOp = Complement | Negate
3739 ```
3840
39- 2 . ** Assembly AST** :
41+ 2 . ** TACKY IR** :
42+ ``` haskell
43+ data Program = Program FunctionDef
44+ data FunctionDef = Function
45+ { name :: Text
46+ , body :: [Instruction ]
47+ }
48+ data Instruction
49+ = Return TackyVal
50+ | Unary UnaryOp TackyVal TackyVal
51+ data TackyVal = Constant Int | Var Text
52+ ```
53+
54+ 3 . ** Assembly AST **:
4055 ```haskell
4156 data Program = Program FunctionDef
4257 data FunctionDef = Function
@@ -47,26 +62,42 @@ Programs are represented internally using a series of increasingly lower-level d
4762 data Operand = Imm Int | Register
4863 ```
4964
65+
5066## Project Structure
5167
5268```
53- lib/
54- ├── Halcyon/
55- │ ├── Backend/ # Code generation and emission
56- │ │ ├── Codegen.hs # AST to Assembly conversion
57- │ │ └── Emit.hs # Assembly to text output
58- │ ├── Core/ # Core data types and utilities
59- │ │ ├── Assembly.hs # Assembly representation
60- │ │ ├── Ast.hs # C language AST
61- │ │ ├── Monad.hs # Compiler monad stack
62- │ │ └── Settings.hs # Compiler settings and types
63- │ ├── Driver/ # Compiler driver
64- │ │ ├── Cli.hs # Command line interface
65- │ │ └── Pipeline.hs # Compilation pipeline
66- │ └── Frontend/ # Parsing and analysis
67- │ ├── Lexer.hs # Lexical analysis
68- │ ├── Parse.hs # Parsing
69- │ └── Tokens.hs # Token definitions
69+ .
70+ ├── app/ # Application entry point
71+ │ └── Main.hs
72+ ├── bin/ # Binary outputs
73+ ├── lib/ # Main library code
74+ │ ├── Halcyon.hs # Library entry point
75+ │ └── Halcyon/ # Core modules
76+ │ ├── Backend/ # Code generation and emission
77+ │ │ ├── Codegen.hs # TACKY to Assembly conversion
78+ │ │ ├── Emit.hs # Assembly to text output
79+ │ │ └── ReplacePseudos.hs # Register/stack allocation
80+ │ ├── Core/ # Core data types and utilities
81+ │ │ ├── Assembly.hs # Assembly representation
82+ │ │ ├── Ast.hs # C language AST
83+ │ │ ├── Monad.hs # Compiler monad stack
84+ │ │ ├── Settings.hs # Compiler settings and types
85+ │ │ ├── Tacky.hs # TACKY IR definition
86+ │ │ └── TackyGen.hs # AST to TACKY transformation
87+ │ ├── Driver/ # Compiler driver
88+ │ │ ├── Cli.hs # Command line interface
89+ │ │ └── Pipeline.hs # Compilation pipeline
90+ │ └── Frontend/ # Parsing and analysis
91+ │ ├── Lexer.hs # Lexical analysis
92+ │ ├── Parse.hs # Parsing
93+ │ └── Tokens.hs # Token definitions
94+ ├── test/ # Test suite
95+ │ └── Main.hs
96+ ├── CHANGELOG.md # Version history
97+ ├── LICENSE # Project license
98+ ├── README.md # Project documentation
99+ ├── flake.nix # Nix build configuration
100+ └── halcyon.cabal # Cabal build configuration
70101```
71102
72103### Architecture
@@ -95,6 +126,7 @@ Options:
95126 --lex Run lexical analysis only
96127 --parse Run parsing only
97128 --codegen Run through code generation
129+ --tacky Run through TACKY generation
98130 -S Stop after assembly generation
99131 -h,--help Show help text
100132```
0 commit comments