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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
-- Helper function to flatten the instruction hierarchy
local function flatten_instructions(insts, prefix, result)
prefix = prefix or ""
result = result or {}
for _, entry in ipairs(insts) do
for name, data in pairs(entry) do
local full_name = prefix == "" and name or (prefix .. "." .. name)
-- If it's a table with numeric indices, it has children
if type(data) == "table" and #data > 0 then
flatten_instructions(data, full_name, result)
else
-- Add the current instruction
table.insert(result, full_name)
end
end
end
return result
end
-- Load instruction definitions
local function load_instructions(filename)
local chunk, err = loadfile(filename)
if not chunk then
error("Failed to load instruction file: " .. filename .. " - " .. (err or "unknown error"))
end
-- Just execute it normally
local result = chunk()
-- If the file sets a global 'insts', use that
if result.insts then
return result.insts
end
error("Instruction file must return a table with 'insts' entry")
end
-- Load stable names table
local function load_stable_names(filename)
local file = io.open(filename, "r")
if not file then
-- File doesn't exist, return empty table
return {}
end
file:close()
local chunk, err = loadfile(filename)
if not chunk then
error("Failed to load stable names file: " .. filename .. " - " .. (err or "unknown error"))
end
local result = chunk()
-- Validate structure
if type(result) ~= "table" then
error("Stable names file must return a table")
end
for name, id in pairs(result) do
if type(name) ~= "string" then
error(string.format("Invalid key: expected string, got %s", type(name)))
end
if type(id) ~= "number" then
error(string.format("Invalid value for '%s': expected number, got %s", name, type(id)))
end
end
return result
end
-- Save stable names table
local function save_stable_names(filename, stable_names)
local file, err = io.open(filename, "w")
if not file then
error("Failed to open file for writing: " .. filename .. " - " .. (err or "unknown error"))
end
file:write("-- This file is machine generated! any entries written below will be preserved,\n")
file:write("-- but things like comments or anything outside the schema won't be preserved\n")
file:write("return {\n")
-- Sort by ID for consistent output
local sorted_entries = {}
for name, id in pairs(stable_names) do
table.insert(sorted_entries, { name = name, id = id })
end
table.sort(sorted_entries, function(a, b)
return a.id < b.id
end)
for _, entry in ipairs(sorted_entries) do
-- Escape quotes in name
local escaped_name = entry.name:gsub('"', '\\"')
file:write(string.format('\t["%s"] = %d,\n', escaped_name, entry.id))
end
file:write("}\n")
file:close()
end
-- Check for unique IDs
local function check_unique_ids(stable_names)
local seen_ids = {}
local duplicates = {}
for name, id in pairs(stable_names) do
if seen_ids[id] then
if not duplicates[id] then
duplicates[id] = { seen_ids[id] }
end
table.insert(duplicates[id], name)
else
seen_ids[id] = name
end
end
return duplicates
end
-- Check bijection
local function check_bijection(inst_names, stable_names)
local missing_from_stable = {}
local extra_in_stable = {}
-- Check for instructions missing from stable names
for _, name in ipairs(inst_names) do
if stable_names[name] == nil then
table.insert(missing_from_stable, name)
end
end
-- Check for stable names not in instructions
local inst_name_set = {}
for _, name in ipairs(inst_names) do
inst_name_set[name] = true
end
for name, _ in pairs(stable_names) do
if not inst_name_set[name] then
table.insert(extra_in_stable, name)
end
end
return missing_from_stable, extra_in_stable
end
-- Get next available ID
local function get_next_id(stable_names)
local max_id = -1
for _, id in pairs(stable_names) do
if id > max_id then
max_id = id
end
end
return max_id + 1
end
-- Print usage
local function print_usage()
print("Usage: lua check_instructions.lua check|update [inst_file] [stable_file]")
print("Commands:")
print(" check - Check bijection and uniqueness (default)")
print(" update - Add missing instructions to stable names")
end
-- Main program
local function main(args)
local command = args[1] or "check"
local inst_file = args[2] or "source/slang/slang-ir-insts.lua"
local stable_file = args[3] or "source/slang/slang-ir-insts-stable-names.lua"
-- Validate command
local valid_commands = { check = true, update = true }
if not valid_commands[command] then
print("ERROR: Invalid command: " .. command)
print_usage()
return 1
end
-- Load data with error handling
local ok, insts_or_err = pcall(load_instructions, inst_file)
if not ok then
print("ERROR: " .. insts_or_err)
return 1
end
local insts = insts_or_err
ok, stable_names = pcall(load_stable_names, stable_file)
if not ok then
print("ERROR: " .. stable_names)
return 1
end
-- Flatten instruction hierarchy
local inst_names = flatten_instructions(insts)
local has_errors = false
if command == "check" or command == "all" then
print("=== Checking stable names ===")
-- Check unique IDs
local duplicate_ids = check_unique_ids(stable_names)
if next(duplicate_ids) then
has_errors = true
print("ERROR: Duplicate IDs found:")
for id, names in pairs(duplicate_ids) do
print(string.format(" - ID %d used by: %s", id, table.concat(names, ", ")))
end
else
print("✓ All IDs are unique")
end
-- Check bijection
local missing, extra = check_bijection(inst_names, stable_names)
if #missing > 0 then
has_errors = true
print(string.format("ERROR: %d instructions missing from stable names:", #missing))
for _, name in ipairs(missing) do
print(" - " .. name)
end
else
print("✓ All instructions have stable names")
end
if #extra > 0 then
print(string.format("WARNING: %d extra entries in stable names (not in instructions):", #extra))
for _, name in ipairs(extra) do
print(" - " .. name)
end
else
print("✓ No extra entries in stable names")
end
if not has_errors and #extra == 0 then
print("✓ Is a bijection")
end
end
if command == "update" or command == "all" then
print("=== Updating stable names ===")
-- Don't update if there are errors
if has_errors then
print("ERROR: Cannot update due to errors in existing stable names")
return 1
end
local missing, _ = check_bijection(inst_names, stable_names)
if #missing > 0 then
-- Add missing instructions
local next_id = get_next_id(stable_names)
for _, name in ipairs(missing) do
stable_names[name] = next_id
next_id = next_id + 1
end
-- Save updated file
local ok, err = pcall(save_stable_names, stable_file, stable_names)
if not ok then
print("ERROR: Failed to save: " .. err)
return 1
end
print(string.format("Added %d new instructions to %s", #missing, stable_file))
else
print("No missing instructions to add")
end
end
return has_errors and 1 or 0
end
-- Run the program
os.exit(main(arg) or 0)
|