omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / compiler / osc_scope.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Visible OSC names are indexed by spelling. Declaration IDs live for the |
| 3 | note whole function; leaving a block unlinks its entries in reverse order. |
| 4 | note The zero bucket sentinel is distinct from symbol zero. |
| 5 | note bridge c-type scopetab OscCheckedScope |
| 6 | note bridge c-type declsite OscDeclSite |
| 7 | om2 |
| 8 | |
| 9 | layout declsite 20 |
| 10 | field id uint at 0 little |
| 11 | field line uint at 4 little |
| 12 | field node uint at 8 little |
| 13 | field loop uint at 12 little |
| 14 | field kind uchar at 16 little |
| 15 | field mut uchar at 17 little |
| 16 | end |
| 17 | |
| 18 | layout scopetab 3672 |
| 19 | field head ushort 256 at 0 little |
| 20 | field prior ushort 480 at 512 little |
| 21 | field bucket ushort 480 at 1472 little |
| 22 | field visible ushort 480 at 2432 little |
| 23 | field start ushort 136 at 3392 little |
| 24 | field count uint at 3664 little |
| 25 | field depth uint at 3668 little |
| 26 | end |
| 27 | |
| 28 | fn name_bucket name view uchar * to ulong |
| 29 | hash = ulong 5381 |
| 30 | for i ulong len name |
| 31 | hash = xor mul hash 33 ulong get name i |
| 32 | end |
| 33 | ret and hash 255 |
| 34 | end |
| 35 | |
| 36 | export fn osc_scope_reset table edit scopetab to void |
| 37 | for i ulong 256 |
| 38 | put table head i 0 |
| 39 | end |
| 40 | put table count 0 |
| 41 | put table depth 0 |
| 42 | end |
| 43 | |
| 44 | export fn osc_scope_find table view scopetab symbols view uchar * stride ulong name view uchar * to long |
| 45 | if or ge len name 64 ge len name stride |
| 46 | ret -1 |
| 47 | end |
| 48 | entry = ulong get table head name_bucket name |
| 49 | while ne entry 0 bound 480 |
| 50 | id = sub entry 1 |
| 51 | base = mul id stride |
| 52 | if eq get symbols add base len name 0 |
| 53 | equal = true |
| 54 | for i ulong len name |
| 55 | if ne get symbols add base i get name i |
| 56 | equal = false |
| 57 | break |
| 58 | end |
| 59 | end |
| 60 | if equal |
| 61 | ret long id |
| 62 | end |
| 63 | end |
| 64 | entry = ulong get table prior id |
| 65 | end |
| 66 | ret -1 |
| 67 | end |
| 68 | |
| 69 | export fn osc_scope_bind table edit scopetab id ulong name view uchar * to void |
| 70 | requires lt id 480 |
| 71 | requires le len name 63 |
| 72 | slot = name_bucket name |
| 73 | put table prior id get table head slot |
| 74 | put table bucket id ushort slot |
| 75 | put table head slot ushort add id 1 |
| 76 | n = ulong get table count |
| 77 | put table visible n ushort id |
| 78 | put table count uint add n 1 |
| 79 | end |
| 80 | |
| 81 | export fn osc_scope_push table edit scopetab to ulong |
| 82 | d = ulong get table depth |
| 83 | if eq d 136 |
| 84 | ret 0 |
| 85 | end |
| 86 | put table start d ushort get table count |
| 87 | put table depth uint add d 1 |
| 88 | ret 1 |
| 89 | end |
| 90 | |
| 91 | export fn osc_scope_pop table edit scopetab to void |
| 92 | d = sub ulong get table depth 1 |
| 93 | first = ulong get table start d |
| 94 | n = ulong get table count |
| 95 | while gt n first bound 480 |
| 96 | n = sub n 1 |
| 97 | id = ulong get table visible n |
| 98 | put table head ulong get table bucket id get table prior id |
| 99 | end |
| 100 | put table depth uint d |
| 101 | put table count uint first |
| 102 | end |
| 103 | |
| 104 | note Search declaration order so a duplicate and a forward call both report |
| 105 | note the first matching declaration. Names use the checker's 64-byte spelling. |
| 106 | fn same_record_name records view uchar * name view uchar * base ulong to bool |
| 107 | for j ulong len name |
| 108 | ch = get name j |
| 109 | if or eq ch 0 ne get records add base j ch |
| 110 | ret false |
| 111 | end |
| 112 | end |
| 113 | ret eq get records add base len name 0 |
| 114 | end |
| 115 | |
| 116 | note Struct, field and function records all start with a 64-byte name. |
| 117 | note Reverse lookup preserves the semantic checker's last-match convention. |
| 118 | export fn osc_record_name records view uchar * name view uchar * stride ulong newest ulong to long |
| 119 | requires ge stride 64 |
| 120 | requires eq rem len records stride 0 |
| 121 | if gt len name 63 |
| 122 | ret -1 |
| 123 | end |
| 124 | count = div len records stride |
| 125 | for step ulong count |
| 126 | index = step |
| 127 | if ne newest 0 |
| 128 | index = sub sub count 1 step |
| 129 | end |
| 130 | if same_record_name records name mul index stride |
| 131 | ret long index |
| 132 | end |
| 133 | end |
| 134 | ret -1 |
| 135 | end |
| 136 | |
| 137 | export fn osc_find_function names view uchar * name view uchar * to long |
| 138 | requires le len name 63 |
| 139 | requires eq rem len names 64 0 |
| 140 | ret osc_record_name names name 64 0 |
| 141 | end |
| 142 | |
| 143 | note The checker supplies the declaration site. The scoped index and symbol |
| 144 | note record are installed together after duplicate and capacity checks. |
| 145 | fn symbol_word symbols edit uchar 76800 pos ulong value uint to void |
| 146 | for j ulong 4 |
| 147 | put symbols add pos j uchar and shr value mul j 8 255 |
| 148 | end |
| 149 | end |
| 150 | |
| 151 | export fn osc_declare_symbol table edit scopetab symbols edit uchar 76800 name view uchar * ty view uchar 16 site view declsite to long |
| 152 | requires le len name 63 |
| 153 | previous = osc_scope_find table symbols 160 name |
| 154 | if ge previous 0 |
| 155 | ret previous |
| 156 | end |
| 157 | id = ulong get site id |
| 158 | if ge id 480 |
| 159 | ret -2 |
| 160 | end |
| 161 | base = mul id 160 |
| 162 | for j ulong 160 |
| 163 | put symbols add base j 0 |
| 164 | end |
| 165 | for j ulong len name |
| 166 | put symbols add base j get name j |
| 167 | end |
| 168 | for j ulong 16 |
| 169 | put symbols add add base 72 j get ty j |
| 170 | end |
| 171 | symbol_word symbols add base 64 get site line |
| 172 | put symbols add base 68 get site kind |
| 173 | put symbols add base 69 get site mut |
| 174 | symbol_word symbols add base 88 get site loop |
| 175 | symbol_word symbols add base 92 4294967295 |
| 176 | symbol_word symbols add base 96 4294967295 |
| 177 | symbol_word symbols add base 152 get site node |
| 178 | osc_scope_bind table id name |
| 179 | ret -1 |
| 180 | end |