Skip to content

Commit d7c84e3

Browse files
authored
Merge pull request #3 from 0xpantera/refactor/core-types
Refactor core types
2 parents 498833c + f0b69d8 commit d7c84e3

File tree

13 files changed

+141
-113
lines changed

13 files changed

+141
-113
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,13 @@
4646
- TACKY generation tests
4747
- Assembly generation tests
4848
- Full pipeline integration tests
49-
- Test utilities and helper functions in Test.Common
49+
- Test utilities and helper functions in Test.Common
50+
51+
## 0.2.2.0 -- 2024-11-25
52+
53+
### Changed
54+
- Improved type definitions across compiler stages
55+
- Added record syntax for multi-field data constructors
56+
- Standardized naming conventions in core types
57+
- Simplified token type names
58+
- Made qualified imports consistent across modules

README.md

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,53 @@ The compiler processes source code through the following stages:
2727
Programs are represented internally using a series of increasingly lower-level data structures:
2828
2929
1. **Abstract Syntax Tree (AST)**:
30-
```haskell
31-
data Program = Program FunctionDef
32-
data FunctionDef = Function
33-
{ name :: Text
34-
, body :: Statement
35-
}
36-
data Statement = Return Expr
37-
data Expr = Constant Int | Unary UnaryOp Expr
38-
data UnaryOp = Complement | Negate
39-
```
30+
```haskell
31+
data Program = Program Function
32+
data Function = Function
33+
{ name :: Text
34+
, body :: Statement
35+
}
36+
data Statement = Return Expr
37+
data Expr
38+
= Constant { value :: Int }
39+
| Unary { operator :: UnaryOp, operand :: Expr }
40+
data UnaryOp = Complement | Negate
41+
```
4042

4143
2. **TACKY IR**:
4244
```haskell
43-
data Program = Program FunctionDef
44-
data FunctionDef = Function
45+
data Program = Program Function
46+
data Function = Function
4547
{ name :: Text
4648
, body :: [Instruction]
4749
}
48-
data Instruction
49-
= Return TackyVal
50-
| Unary UnaryOp TackyVal TackyVal
51-
data TackyVal = Constant Int | Var Text
50+
data Instruction
51+
= Return { value :: Val }
52+
| Unary { operator :: UnaryOp, src :: Val, dst :: Val }
53+
data Val = Constant Int | Var Text
54+
data UnaryOp = Complement | Negate
5255
```
5356

5457
3. **Assembly AST**:
55-
```haskell
56-
data Program = Program FunctionDef
57-
data FunctionDef = Function
58-
{ name :: Text
59-
, instructions :: [Instruction]
60-
}
61-
data Instruction = Mov Operand Operand | Ret
62-
data Operand = Imm Int | Register
63-
```
58+
```haskell
59+
data Program = Program Function
60+
data Function = Function
61+
{ name :: Text
62+
, instructions :: [Instruction]
63+
}
64+
data Instruction
65+
= Mov { src :: Operand, dst :: Operand }
66+
| Unary { operator :: UnaryOp, operand :: Operand }
67+
| AllocateStack { bytes :: Int }
68+
| Ret
69+
data Operand
70+
= Imm Int
71+
| Register Reg
72+
| Pseudo Text
73+
| Stack Int
74+
data UnaryOp = Neg | Not
75+
data Reg = Ax | R10
76+
```
6477

6578

6679
## Project Structure

halcyon.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.0
22
name: halcyon
3-
version: 0.2.1.0
3+
version: 0.2.2.0
44
-- synopsis:
55
-- description:
66
license: BSD-3-Clause

lib/Halcyon/Backend/Codegen.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Halcyon.Backend.Codegen where
44
import qualified Halcyon.Core.Assembly as Asm
55
import qualified Halcyon.Core.Tacky as Tacky
66

7-
convertVal :: Tacky.TackyVal -> Asm.Operand
7+
convertVal :: Tacky.Val -> Asm.Operand
88
convertVal (Tacky.Constant int) = Asm.Imm int
99
convertVal (Tacky.Var identifier) = Asm.Pseudo identifier
1010

@@ -25,7 +25,7 @@ convertInstruction (Tacky.Unary op src dest) =
2525
asmOp = convertOp op
2626
in [Asm.Mov asmSrc asmDest, Asm.Unary asmOp asmDest]
2727

28-
convertFunction :: Tacky.FunctionDef -> Asm.FunctionDef
28+
convertFunction :: Tacky.Function -> Asm.Function
2929
convertFunction Tacky.Function{..} =
3030
let instructions = concatMap convertInstruction body
3131
in Asm.Function {name, instructions}

lib/Halcyon/Backend/Emit.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ emitInstruction (Asm.Unary op operand) =
3131
emitInstruction Asm.Ret =
3232
"\tmovq %rbp, %rsp\n\tpopq %rbp\n\tret\n"
3333

34-
emitFunction :: Asm.FunctionDef -> Text
34+
emitFunction :: Asm.Function -> Text
3535
emitFunction Asm.Function{..} =
3636
let
3737
header =

lib/Halcyon/Backend/ReplacePseudos.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ replacePseudoInst = \case
6363
Asm.Ret -> pure Asm.Ret
6464
Asm.AllocateStack _ -> throwError UnexpectedAllocateStack
6565

66-
replacePseudoFunc :: MonadReplace m => Asm.FunctionDef -> m Asm.FunctionDef
66+
replacePseudoFunc :: MonadReplace m => Asm.Function -> m Asm.Function
6767
replacePseudoFunc func@Asm.Function{..} = do
6868
fixedInstructions <- traverse replacePseudoInst instructions
6969
pure $ func { Asm.instructions = fixedInstructions }
@@ -85,7 +85,7 @@ fixupInstruction (Asm.Mov (Asm.Stack src) (Asm.Stack dst)) =
8585
, (Asm.Mov (Asm.Register Asm.R10) (Asm.Stack dst)) ]
8686
fixupInstruction other = [ other ]
8787

88-
fixupFunction :: Asm.FunctionDef -> Int ->Asm.FunctionDef
88+
fixupFunction :: Asm.Function -> Int -> Asm.Function
8989
fixupFunction (Asm.Function{..}) lastStackSlot = Asm.Function
9090
{ name
9191
, instructions =

lib/Halcyon/Core/Assembly.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ module Halcyon.Core.Assembly where
22

33
import Data.Text ( Text )
44

5-
--TODO: use record syntax to name some of the product types
6-
-- e.i. Mov Operand Operand -> Mov {src: Operand, dest: Operand}
7-
data Program = Program FunctionDef
5+
6+
data Program = Program Function
87
deriving (Eq, Show)
98

10-
data FunctionDef = Function
9+
data Function = Function
1110
{ name :: Text
1211
, instructions :: [Instruction]
1312
} deriving (Eq, Show)
1413

15-
data Instruction
16-
= Mov Operand Operand
17-
| Unary UnaryOp Operand
18-
| AllocateStack Int
14+
data Instruction
15+
= Mov { src :: Operand, dst :: Operand }
16+
| Unary { operator :: UnaryOp, operand :: Operand }
17+
| AllocateStack { bytes :: Int }
1918
| Ret
2019
deriving (Eq, Show)
2120

lib/Halcyon/Core/Ast.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ module Halcyon.Core.Ast where
22

33
import Data.Text ( Text )
44

5-
data Program = Program FunctionDef
5+
data Program = Program Function
66
deriving (Eq, Show)
77

8-
data FunctionDef = Function
8+
data Function = Function
99
{ name :: Text
1010
, body :: Statement
1111
} deriving (Eq, Show)
1212

1313
data Statement = Return Expr
1414
deriving (Eq, Show)
1515

16-
data Expr = Constant Int | Unary UnaryOp Expr
16+
data Expr
17+
= Constant { value :: Int }
18+
| Unary { operator :: UnaryOp, operand :: Expr }
1719
deriving (Eq, Show)
1820

1921
data UnaryOp = Complement | Negate

lib/Halcyon/Core/Tacky.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ module Halcyon.Core.Tacky where
22

33
import Data.Text ( Text )
44

5-
data Program = Program FunctionDef
5+
data Program = Program Function
66
deriving (Eq, Show)
77

8-
data FunctionDef = Function
8+
data Function = Function
99
{ name :: Text
1010
, body :: [Instruction]
1111
} deriving (Eq, Show)
1212

13-
data Instruction
14-
= Return TackyVal
15-
| Unary UnaryOp TackyVal TackyVal
13+
data Instruction
14+
= Return { value :: Val }
15+
| Unary
16+
{ operator :: UnaryOp
17+
, src :: Val
18+
, dst :: Val
19+
}
1620
deriving (Eq, Show)
1721

18-
data TackyVal
22+
data Val
1923
= Constant Int
2024
| Var Text
2125
deriving (Eq, Show)

lib/Halcyon/Core/TackyGen.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ newtype TackyGenT m a = TackyGenT
1818

1919
data TackyGenRes = TackyGenRes
2020
{ instructions :: [Tacky.Instruction]
21-
, resultVal :: Tacky.TackyVal
21+
, resultVal :: Tacky.Val
2222
} deriving (Show, Eq)
2323

2424
-- Helper to generate unique names
@@ -51,7 +51,7 @@ emitTackyForStmnt (Ast.Return e) = do
5151
TackyGenRes{..} <- emitTackyForExpr e
5252
pure $ TackyGenRes {..}
5353

54-
emitTackyForFunc :: Monad m => Ast.FunctionDef -> TackyGenT m Tacky.FunctionDef
54+
emitTackyForFunc :: Monad m => Ast.Function -> TackyGenT m Tacky.Function
5555
emitTackyForFunc Ast.Function{..} = do
5656
TackyGenRes{..} <- emitTackyForStmnt body
5757
let finalInstrs = instructions ++ [Tacky.Return resultVal]

0 commit comments

Comments
 (0)