interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / compiler / osc_scope.om

180 lines · 4954 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Visible OSC names are indexed by spelling. Declaration IDs live for the
3note whole function; leaving a block unlinks its entries in reverse order.
4note The zero bucket sentinel is distinct from symbol zero.
5note bridge c-type scopetab OscCheckedScope
6note bridge c-type declsite OscDeclSite
7om2
8
9layout declsite 20
10field id uint at 0 little
11field line uint at 4 little
12field node uint at 8 little
13field loop uint at 12 little
14field kind uchar at 16 little
15field mut uchar at 17 little
16end
17
18layout scopetab 3672
19field head ushort 256 at 0 little
20field prior ushort 480 at 512 little
21field bucket ushort 480 at 1472 little
22field visible ushort 480 at 2432 little
23field start ushort 136 at 3392 little
24field count uint at 3664 little
25field depth uint at 3668 little
26end
27
28fn 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
34end
35
36export 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
42end
43
44export 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
67end
68
69export fn osc_scope_bind table edit scopetab id ulong name view uchar * to void
70requires lt id 480
71requires 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
79end
80
81export 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
89end
90
91export 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
102end
103
104note Search declaration order so a duplicate and a forward call both report
105note the first matching declaration. Names use the checker's 64-byte spelling.
106fn 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
114end
115
116note Struct, field and function records all start with a 64-byte name.
117note Reverse lookup preserves the semantic checker's last-match convention.
118export fn osc_record_name records view uchar * name view uchar * stride ulong newest ulong to long
119requires ge stride 64
120requires 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
135end
136
137export fn osc_find_function names view uchar * name view uchar * to long
138requires le len name 63
139requires eq rem len names 64 0
140 ret osc_record_name names name 64 0
141end
142
143note The checker supplies the declaration site. The scoped index and symbol
144note record are installed together after duplicate and capacity checks.
145fn 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
149end
150
151export fn osc_declare_symbol table edit scopetab symbols edit uchar 76800 name view uchar * ty view uchar 16 site view declsite to long
152requires 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
180end