~ehmry/sigil

ref: post-mortem sigil/lib/compile-boot.dhall -rw-r--r-- 4.0 KiB
2b1c9d71Emery Hemingway Link the write-up (or write-off) of the project 11 months ago
                                                                                
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
let Sigil =
        env:DHALL_SIGIL
      ? https://git.sr.ht/~ehmry/dhall-sigil/blob/trunk/package.dhall

let Prelude = Sigil.Prelude

let BootModules = Sigil.BootModules

let RomEntry = Prelude.Map.Entry Text BootModules.ROM.Type

let compile =
      λ(addressType : Text) →
      λ(boot : Sigil.Boot.Type) →
      λ(out : Text) →
        let NaturalIndex = { index : Natural, value : Text }

        let TextIndex = { index : Text, value : Text }

        let moduleKeys =
              Prelude.Map.keys Text BootModules.ROM.Type boot.rom # [ "config" ]

        let moduleValues =
              let f =
                    λ(e : RomEntry) →
                      merge
                        { RomText = λ(text : Text) → ".ascii ${Text/show text}"
                        , RomPath = λ(path : Text) → ".incbin ${Text/show path}"
                        }
                        e.mapValue

              in    Prelude.List.map RomEntry Text f boot.rom
                  # [ ".incbin \"${out}/config\"" ]

        let map =
              λ(list : List Text) →
              λ(f : TextIndex → Text) →
                let indexedNatural = Prelude.List.indexed Text list

                let indexed =
                      Prelude.List.map
                        NaturalIndex
                        TextIndex
                        ( λ(x : NaturalIndex) →
                            { index = Prelude.Natural.show x.index
                            , value = x.value
                            }
                        )
                        indexedNatural

                let texts = Prelude.List.map TextIndex Text f indexed

                in  Prelude.Text.concatSep "\n" texts

        let mapNames = map moduleKeys

        let mapValues = map moduleValues

        let asm =
                  ''
                  .set MIN_PAGE_SIZE_LOG2, 12
                  .set DATA_ACCESS_ALIGNM_LOG2, 3

                  .section .data

                  .p2align DATA_ACCESS_ALIGNM_LOG2
                  .global _boot_modules_headers_begin
                  _boot_modules_headers_begin:

                  ''
              ++  mapNames
                    ( λ(m : TextIndex) →
                        ''
                        ${addressType} _boot_module_${m.index}_name
                        ${addressType} _boot_module_${m.index}_begin
                        ${addressType} _boot_module_${m.index}_end - _boot_module_${m.index}_begin
                        ''
                    )
              ++  ''
                  .global _boot_modules_headers_end
                  _boot_modules_headers_end:

                  ''
              ++  mapNames
                    ( λ(m : TextIndex) →
                        ''
                        .p2align DATA_ACCESS_ALIGNM_LOG2
                        _boot_module_${m.index}_name:
                        .string "${m.value}"
                        .byte 0

                        ''
                    )
              ++  ''
                  .section .data.boot_modules_binaries

                  .global _boot_modules_binaries_begin
                  _boot_modules_binaries_begin:

                  ''
              ++  mapValues
                    ( λ(m : TextIndex) →
                        ''
                        .p2align MIN_PAGE_SIZE_LOG2
                        _boot_module_${m.index}_begin:
                        ${m.value}
                        _boot_module_${m.index}_end:
                        ''
                    )
              ++  ''
                  .p2align MIN_PAGE_SIZE_LOG2
                  .global _boot_modules_binaries_end
                  _boot_modules_binaries_end:
                  ''

        in  { config = Sigil.Init.render boot.config
            , modules_asm = asm
            , stats =
                let sum = Sigil.Init.resources boot.config

                in  "RAM=${Prelude.Natural.show sum.ram}"
            }

let funcs = { to32bitImage = compile ".long", to64bitImage = compile ".quad" }

in  funcs.to64bitImage