-
Notifications
You must be signed in to change notification settings - Fork 0
/
MJMain.hs
50 lines (46 loc) · 1.37 KB
/
MJMain.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Main where
import MJLex
import MJParse
import MJSymbolTreeMaker
import MJTypeChecker
import MJTranslate
import Backend.Names
import Backend.Tree
import Backend.MachineSpecifics
import Backend.DummyMachine
import Backend.Cmm
import Backend.Canonicalize
import Backend.X86Assem
import Backend.X86Machine
import Backend.InstructionSelection
import Backend.Liveness
import Backend.RegisterAllocation
import Control.Monad
import Control.Monad.Identity
import System.IO
import System.Environment
import System.Exit
main = do
args <- getArgs
input <- readFile (args !! 0)
-- 1. Lex:
let tokens = alexScanTokens input
-- 2. Parse:
let abstractSyntaxTree = parse tokens
-- 3. Create symbol tree:
let symbolTable = symbolize abstractSyntaxTree
-- 4. Run type checker:
let t = typecheck' abstractSyntaxTree symbolTable
if not t then exitFailure else return ()
let assemString = runIdentity . withX86Machine $ do
-- 5. Translate program to intermediate language fragments:
ilFragments <- translate' abstractSyntaxTree symbolTable
-- 6. Canonicalize all fragments (includes blocking and tracing)
canFragments <- mapM canonicalize ilFragments
-- 7. Translate to x86 assembly
x86Fragments <- mapM codeGen canFragments
-- 8. Register Allocation
finalFragments <- mapM regAlloc x86Fragments
-- 9. Code emission
printAssembly finalFragments
putStrLn assemString