4 # Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer,
12 # without modification.
13 # 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 # similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 # redistribution must be conditioned upon including a substantially
16 # similar Disclaimer requirement for further binary redistribution.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 # THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 # THE POSSIBILITY OF SUCH DAMAGES.
40 print "usage: bhnd_nvram_map.awk <input map> [-hd] [-o output file]"
50 # Probe awk implementation's hex digit handling
51 if ("0xA" + 0 != 10) {
57 OUT_T_HEADER = "HEADER"
61 # Tab width to use when calculating output alignment
74 for (_i = 1; _i < ARGC; _i++) {
75 if (ARGV[_i] == "--debug") {
77 } else if (ARGV[_i] == "-d" && OUT_T == null) {
79 } else if (ARGV[_i] == "-h" && OUT_T == null) {
81 } else if (ARGV[_i] == "-v") {
83 } else if (ARGV[_i] == "-o") {
88 OUTPUT_FILE = ARGV[_i]
89 } else if (ARGV[_i] == "--") {
92 } else if (ARGV[_i] !~ /^-/) {
95 print "unknown option " ARGV[_i]
103 print("error: one of -d or -h required")
107 if (FILENAME == null) {
108 print("error: no input file specified")
112 if (OUTPUT_FILE == "-") {
113 OUTPUT_FILE = "/dev/stdout"
114 } else if (OUTPUT_FILE == null) {
115 OUTPUT_FILE_IDX = split(FILENAME, _g_output_path, "/")
116 OUTPUT_FILE = _g_output_path[OUTPUT_FILE_IDX]
118 if (OUTPUT_FILE !~ /^bhnd_/)
119 OUTPUT_FILE = "bhnd_" OUTPUT_FILE
121 if (OUT_T == OUT_T_HEADER)
122 OUTPUT_FILE = OUTPUT_FILE ".h"
124 OUTPUT_FILE = OUTPUT_FILE "_data.h"
128 UINT_REGEX = "^(0|[1-9][0-9]*)$"
129 HEX_REGEX = "^(0x[A-Fa-f0-9]+)$"
130 OFF_REGEX = "^(0|[1-9][0-9]*)|^(0x[A-Fa-f0-9]+)"
131 REL_OFF_REGEX = "^\\+(0|[1-9][0-9]*)|^\\+(0x[A-Fa-f0-9]+)"
133 ARRAY_REGEX = "\\[(0|[1-9][0-9]*)\\]"
134 TYPES_REGEX = "^(((u|i)(8|16|32))|char)("ARRAY_REGEX")?$"
136 IDENT_REGEX = "[A-Za-z_][A-Za-z0-9_]*"
137 SVAR_IDENT_REGEX = "^<"IDENT_REGEX">{?$" # <var> identifiers
138 VAR_IDENT_REGEX = "^"IDENT_REGEX"{?$" # var identifiers
140 VACCESS_REGEX = "^(private|internal)$"
142 # Property array keys
146 # Prop path array keys
147 PPATH_HEAD = "ppath_head"
148 PPATH_TAIL = "ppath_tail"
151 OBJ_IS_CLS = "o_is_cls"
152 OBJ_SUPER = "o_super"
156 CLS_NAME = "cls_name"
157 CLS_PROP = "cls_prop"
159 # C SPROM binding opcodes/opcode flags
160 SPROM_OPCODE_EOF = "SPROM_OPCODE_EOF"
161 SPROM_OPCODE_NELEM = "SPROM_OPCODE_NELEM"
162 SPROM_OPCODE_VAR_END = "SPROM_OPCODE_VAR_END"
163 SPROM_OPCODE_VAR_IMM = "SPROM_OPCODE_VAR_IMM"
164 SPROM_OPCODE_VAR_REL_IMM = "SPROM_OPCODE_VAR_REL_IMM"
165 SPROM_OPCODE_VAR = "SPROM_OPCODE_VAR"
166 SPROM_OPCODE_REV_IMM = "SPROM_OPCODE_REV_IMM"
167 SPROM_OPCODE_REV_RANGE = "SPROM_OPCODE_REV_RANGE"
168 SPROM_OP_REV_START_MASK = "SPROM_OP_REV_START_MASK"
169 SPROM_OP_REV_START_SHIFT = "SPROM_OP_REV_START_SHIFT"
170 SPROM_OP_REV_END_MASK = "SPROM_OP_REV_END_MASK"
171 SPROM_OP_REV_END_SHIFT = "SPROM_OP_REV_END_SHIFT"
172 SPROM_OPCODE_MASK_IMM = "SPROM_OPCODE_MASK_IMM"
173 SPROM_OPCODE_MASK = "SPROM_OPCODE_MASK"
174 SPROM_OPCODE_SHIFT_IMM = "SPROM_OPCODE_SHIFT_IMM"
175 SPROM_OPCODE_SHIFT = "SPROM_OPCODE_SHIFT"
176 SPROM_OPCODE_OFFSET_REL_IMM = "SPROM_OPCODE_OFFSET_REL_IMM"
177 SPROM_OPCODE_OFFSET = "SPROM_OPCODE_OFFSET"
178 SPROM_OPCODE_TYPE = "SPROM_OPCODE_TYPE"
179 SPROM_OPCODE_TYPE_IMM = "SPROM_OPCODE_TYPE_IMM"
180 SPROM_OPCODE_DO_BINDN_IMM = "SPROM_OPCODE_DO_BINDN_IMM"
181 SPROM_OPCODE_DO_BIND = "SPROM_OPCODE_DO_BIND"
182 SPROM_OPCODE_DO_BINDN = "SPROM_OPCODE_DO_BINDN"
183 SPROM_OP_BIND_SKIP_IN_MASK = "SPROM_OP_BIND_SKIP_IN_MASK"
184 SPROM_OP_BIND_SKIP_IN_SHIFT = "SPROM_OP_BIND_SKIP_IN_SHIFT"
185 SPROM_OP_BIND_SKIP_IN_SIGN = "SPROM_OP_BIND_SKIP_IN_SIGN"
186 SPROM_OP_BIND_SKIP_OUT_MASK = "SPROM_OP_BIND_SKIP_OUT_MASK"
187 SPROM_OP_BIND_SKIP_OUT_SHIFT = "SPROM_OP_BIND_SKIP_OUT_SHIFT"
189 SPROM_OP_DATA_U8 = "SPROM_OP_DATA_U8"
190 SPROM_OP_DATA_U8_SCALED = "SPROM_OP_DATA_U8_SCALED"
191 SPROM_OP_DATA_U16 = "SPROM_OP_DATA_U16"
192 SPROM_OP_DATA_U32 = "SPROM_OP_DATA_U32"
193 SPROM_OP_DATA_I8 = "SPROM_OP_DATA_I8"
195 SPROM_OP_BIND_SKIP_IN_MAX = 3 # maximum SKIP_IN value
196 SPROM_OP_BIND_SKIP_IN_MIN = -3 # minimum SKIP_IN value
197 SPROM_OP_BIND_SKIP_OUT_MAX = 1 # maximum SKIP_OUT value
198 SPROM_OP_BIND_SKIP_OUT_MIN = 0 # minimum SKIP_OUT value
199 SPROM_OP_IMM_MAX = 15 # maximum immediate value
200 SPROM_OP_REV_RANGE_MAX = 15 # maximum SROM rev range value
202 # SPROM opcode encoding state
203 SromOpStream = class_new("SromOpStream")
204 class_add_prop(SromOpStream, p_layout, "layout")
205 class_add_prop(SromOpStream, p_revisions, "revisions")
206 class_add_prop(SromOpStream, p_vid, "vid")
207 class_add_prop(SromOpStream, p_offset, "offset")
208 class_add_prop(SromOpStream, p_type, "type")
209 class_add_prop(SromOpStream, p_nelem, "nelem")
210 class_add_prop(SromOpStream, p_mask, "mask")
211 class_add_prop(SromOpStream, p_shift, "shift")
212 class_add_prop(SromOpStream, p_bind_total, "bind_total")
213 class_add_prop(SromOpStream, p_pending_bind, "pending_bind")
215 # SROM pending bind operation
216 SromOpBind = class_new("SromOpBind")
217 class_add_prop(SromOpBind, p_segment, "segment")
218 class_add_prop(SromOpBind, p_count, "count")
219 class_add_prop(SromOpBind, p_offset, "offset")
220 class_add_prop(SromOpBind, p_width, "width")
221 class_add_prop(SromOpBind, p_skip_in, "skip_in")
222 class_add_prop(SromOpBind, p_skip_out, "skip_out")
223 class_add_prop(SromOpBind, p_buffer, "buffer")
225 # Map class definition
226 Map = class_new("Map")
228 # Array class definition
229 Array = class_new("Array")
230 class_add_prop(Array, p_count, "count")
232 # MacroType class definition
233 # Used to define a set of known macro types that may be generated
234 MacroType = class_new("MacroType")
235 class_add_prop(MacroType, p_name, "name")
236 class_add_prop(MacroType, p_const_suffix, "const_suffix")
238 MTypeVarName = macro_type_new("name", "") # var name
239 MTypeVarID = macro_type_new("id", "_ID") # var unique ID
240 MTypeVarMaxLen = macro_type_new("len", "_MAXLEN") # var max array length
242 # Preprocessor Constant
243 MacroDefine = class_new("MacroDefine")
244 class_add_prop(MacroDefine, p_name, "name")
245 class_add_prop(MacroDefine, p_value, "value")
247 # ParseState definition
248 ParseState = class_new("ParseState")
249 class_add_prop(ParseState, p_ctx, "ctx")
250 class_add_prop(ParseState, p_is_block, "is_block")
251 class_add_prop(ParseState, p_line, "line")
254 Fmt = class_new("Fmt")
255 class_add_prop(Fmt, p_name, "name")
256 class_add_prop(Fmt, p_symbol, "symbol")
257 class_add_prop(Fmt, p_array_fmt, "array_fmt")
259 FmtHex = fmt_new("hex", "bhnd_nvram_val_bcm_hex_fmt")
260 FmtDec = fmt_new("decimal", "bhnd_nvram_val_bcm_decimal_fmt")
261 FmtMAC = fmt_new("macaddr", "bhnd_nvram_val_bcm_macaddr_fmt")
262 FmtLEDDC = fmt_new("leddc", "bhnd_nvram_val_bcm_leddc_fmt")
263 FmtCharArray = fmt_new("char_array", "bhnd_nvram_val_char_array_fmt")
264 FmtChar = fmt_new("char", "bhnd_nvram_val_char_array_fmt",
266 FmtStr = fmt_new("string", "bhnd_nvram_val_bcm_string_fmt")
268 # User-specifiable value formats
269 ValueFormats = map_new()
270 map_set(ValueFormats, get(FmtHex, p_name), FmtHex)
271 map_set(ValueFormats, get(FmtDec, p_name), FmtDec)
272 map_set(ValueFormats, get(FmtMAC, p_name), FmtMAC)
273 map_set(ValueFormats, get(FmtLEDDC, p_name), FmtLEDDC)
274 map_set(ValueFormats, get(FmtStr, p_name), FmtStr)
277 Type = class_new("Type")
278 class_add_prop(Type, p_name, "name")
279 class_add_prop(Type, p_width, "width")
280 class_add_prop(Type, p_signed, "signed")
281 class_add_prop(Type, p_const, "const")
282 class_add_prop(Type, p_const_val, "const_val")
283 class_add_prop(Type, p_array_const, "array_const")
284 class_add_prop(Type, p_array_const_val, "array_const_val")
285 class_add_prop(Type, p_default_fmt, "default_fmt")
286 class_add_prop(Type, p_mask, "mask")
288 ArrayType = class_new("ArrayType", AST)
289 class_add_prop(ArrayType, p_type, "type")
290 class_add_prop(ArrayType, p_count, "count")
294 UInt32Max = 4294967295
299 Int32Min = -2147483648
300 Int32Max = 2147483648
304 UInt8 = type_new("u8", 1, 0, "BHND_NVRAM_TYPE_UINT8",
305 "BHND_NVRAM_TYPE_UINT8_ARRAY", FmtHex, UInt8Max, 0, 16)
307 UInt16 = type_new("u16", 2, 0, "BHND_NVRAM_TYPE_UINT16",
308 "BHND_NVRAM_TYPE_UINT16_ARRAY", FmtHex, UInt16Max, 1, 17)
310 UInt32 = type_new("u32", 4, 0, "BHND_NVRAM_TYPE_UINT32",
311 "BHND_NVRAM_TYPE_UINT32_ARRAY", FmtHex, UInt32Max, 2, 18)
313 Int8 = type_new("i8", 1, 1, "BHND_NVRAM_TYPE_INT8",
314 "BHND_NVRAM_TYPE_INT8_ARRAY", FmtDec, UInt8Max, 4, 20)
316 Int16 = type_new("i16", 2, 1, "BHND_NVRAM_TYPE_INT16",
317 "BHND_NVRAM_TYPE_INT16_ARRAY", FmtDec, UInt16Max, 5, 21)
319 Int32 = type_new("i32", 4, 1, "BHND_NVRAM_TYPE_INT32",
320 "BHND_NVRAM_TYPE_INT32_ARRAY", FmtDec, UInt32Max, 6, 22)
322 Char = type_new("char", 1, 1, "BHND_NVRAM_TYPE_CHAR",
323 "BHND_NVRAM_TYPE_CHAR_ARRAY", FmtChar, UInt8Max, 8, 24)
325 BaseTypes = map_new()
326 map_set(BaseTypes, get(UInt8, p_name), UInt8)
327 map_set(BaseTypes, get(UInt16, p_name), UInt16)
328 map_set(BaseTypes, get(UInt32, p_name), UInt32)
329 map_set(BaseTypes, get(Int8, p_name), Int8)
330 map_set(BaseTypes, get(Int16, p_name), Int16)
331 map_set(BaseTypes, get(Int32, p_name), Int32)
332 map_set(BaseTypes, get(Char, p_name), Char)
334 BaseTypesArray = map_to_array(BaseTypes)
335 BaseTypesCount = array_size(BaseTypesArray)
338 VFlag = class_new("VFlag")
339 class_add_prop(VFlag, p_name, "name")
340 class_add_prop(VFlag, p_const, "const")
342 VFlagPrivate = vflag_new("private", "BHND_NVRAM_VF_MFGINT")
343 VFlagIgnoreAll1 = vflag_new("ignall1", "BHND_NVRAM_VF_IGNALL1")
345 # Variable Access Type Constants
346 VAccess = class_new("VAccess")
347 VAccessPublic = obj_new(VAccess) # Public
348 VAccessPrivate = obj_new(VAccess) # MFG Private
349 VAccessInternal = obj_new(VAccess) # Implementation-Internal
354 AST = class_new("AST")
355 class_add_prop(AST, p_line, "line")
357 SymbolContext = class_new("SymbolContext", AST)
358 class_add_prop(SymbolContext, p_vars, "vars")
360 # NVRAM root parser context
361 NVRAM = class_new("NVRAM", SymbolContext)
362 class_add_prop(NVRAM, p_var_groups, "var_groups")
363 class_add_prop(NVRAM, p_srom_layouts, "srom_layouts")
364 class_add_prop(NVRAM, p_srom_table, "srom_table")
367 VarGroup = class_new("VarGroup", SymbolContext)
368 class_add_prop(VarGroup, p_name, "name")
371 RevRange = class_new("RevRange", AST)
372 class_add_prop(RevRange, p_start, "start")
373 class_add_prop(RevRange, p_end, "end")
376 StringConstant = class_new("StringConstant", AST)
377 class_add_prop(StringConstant, p_value, "value") # string
378 class_add_prop(StringConstant, p_continued, "continued") # bool
380 # Variable Declaration
381 Var = class_new("Var", AST)
382 class_add_prop(Var, p_access, "access") # VAccess
383 class_add_prop(Var, p_name, "name") # string
384 class_add_prop(Var, p_desc, "desc") # StringConstant
385 class_add_prop(Var, p_help, "help") # StringConstant
386 class_add_prop(Var, p_type, "type") # AbstractType
387 class_add_prop(Var, p_fmt, "fmt") # Fmt
388 class_add_prop(Var, p_ignall1, "ignall1") # bool
389 # ID is assigned once all variables are sorted
390 class_add_prop(Var, p_vid, "vid") # int
392 # Common interface inherited by parser contexts that support
393 # registration of SROM variable entries
394 SromContext = class_new("SromContext", AST)
395 class_add_prop(SromContext, p_revisions, "revisions")
398 SromLayout = class_new("SromLayout", SromContext)
399 class_add_prop(SromLayout, p_entries, "entries") # Array<SromEntry>
400 class_add_prop(SromLayout, p_revmap, "revmap") # Map<(string,int), SromEntry>
401 class_add_prop(SromLayout, p_output_var_counts, # Map<int, int> (rev->count)
404 # SROM Layout Filter Node
405 # Represents a filter over a parent SromLayout's revisions
406 SromLayoutFilter = class_new("SromLayoutFilter", SromContext)
407 class_add_prop(SromLayoutFilter, p_parent, "parent")
409 # SROM variable entry
410 SromEntry = class_new("SromEntry", AST)
411 class_add_prop(SromEntry, p_var, "var")
412 class_add_prop(SromEntry, p_revisions, "revisions")
413 class_add_prop(SromEntry, p_base_offset, "base_offset")
414 class_add_prop(SromEntry, p_type, "type")
415 class_add_prop(SromEntry, p_offsets, "offsets")
417 # SROM variable offset
418 SromOffset = class_new("SromOffset", AST)
419 class_add_prop(SromOffset, p_segments, "segments")
421 # SROM variable offset segment
422 SromSegment = class_new("SromSegment", AST)
423 class_add_prop(SromSegment, p_offset, "offset")
424 class_add_prop(SromSegment, p_type, "type")
425 class_add_prop(SromSegment, p_mask, "mask")
426 class_add_prop(SromSegment, p_shift, "shift")
427 class_add_prop(SromSegment, p_value, "value")
429 # Create the parse state stack
430 _g_parse_stack_depth = 0
431 _g_parse_stack[0] = null
433 # Push the root parse state
434 parser_state_push(nvram_new(), 0)
437 function at_exit(_block_start, _state, _output_vars, _noutput_vars, _name, _var,
440 # Skip completion handling if exiting from an error
444 # Check for complete block closure
445 if (!in_parser_context(NVRAM)) {
446 _state = parser_state_get()
447 _block_start = get(_state, p_line)
448 errorx("missing '}' for block opened on line " _block_start "")
451 # Apply lexicographical sorting to our variable names. To support more
452 # effecient table searching, we guarantee a stable sort order (using C
455 # This also has a side-effect of generating a unique monotonic ID
456 # for all variables, which we will emit as a #define and can use as a
457 # direct index into the C variable table
458 _output_vars = array_new()
459 for (_name in _g_var_names) {
460 _var = _g_var_names[_name]
462 # Don't include internal variables in the output
463 if (var_is_internal(_var))
466 array_append(_output_vars, _var)
469 # Sort by variable name
470 array_sort(_output_vars, prop_to_path(p_name))
472 # Set all variable ID properties to their newly assigned ID value
473 _noutput_vars = array_size(_output_vars)
474 for (_i = 0; _i < _noutput_vars; _i++) {
475 _var = array_get(_output_vars, _i)
479 # Truncate output file and write common header
480 printf("") > OUTPUT_FILE
482 emit(" * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n")
484 emit(" * generated from nvram map: " FILENAME "\n")
488 # Emit all variable definitions
489 if (OUT_T == OUT_T_DATA) {
490 write_data(_output_vars)
491 } else if (OUT_T == OUT_T_HEADER) {
492 write_header(_output_vars)
495 printf("%u variable records written to %s\n", array_size(_output_vars),
496 OUTPUT_FILE) >> "/dev/stderr"
500 # Write the public header (output type HEADER)
501 function write_header(output_vars, _noutput_vars, _var,
502 _tab_align, _macro, _macros, _num_macros, _i)
504 # Produce our array of #defines
506 _noutput_vars = array_size(output_vars)
507 for (_i = 0; _i < _noutput_vars; _i++) {
508 _var = array_get(output_vars, _i)
511 _macro = var_get_macro(_var, MTypeVarName, \
512 "\"" get(_var, p_name) "\"")
513 _macros[_num_macros++] = _macro
515 # Variable array length
516 if (var_has_array_type(_var)) {
517 _macro = var_get_macro(_var, MTypeVarMaxLen,
518 var_get_array_len(_var))
519 _macros[_num_macros++] = _macro
523 # Calculate value tab alignment position for our macros
524 _tab_align = macros_get_tab_alignment(_macros, _num_macros)
527 for (_i = 0; _i < _num_macros; _i++)
528 write_macro_define(_macros[_i], _tab_align)
531 # Write the private data header (output type DATA)
532 function write_data(output_vars, _noutput_vars, _var, _nvram, _layouts,
533 _nlayouts, _layout, _revs, _rev, _rev_start, _rev_end, _base_type,
534 _srom_table, _nsrom_table, _i, _j)
536 _nvram = parser_state_get_context(NVRAM)
537 _layouts = get(_nvram, p_srom_layouts)
538 _nlayouts = array_size(_layouts)
540 _noutput_vars = array_size(output_vars)
542 # Write all our private NVAR_ID defines
543 write_data_defines(output_vars)
545 # Write all layout binding opcodes, and build an array
546 # mapping SROM revision to corresponding SROM layout
547 _srom_table = array_new()
548 for (_i = 0; _i < _nlayouts; _i++) {
549 _layout = array_get(_layouts, _i)
551 # Write binding opcode table to our output file
552 write_srom_bindings(_layout)
554 # Add entries to _srom_table for all covered revisions
555 _revs = get(_layout, p_revisions)
556 _rev_start = get(_revs, p_start)
557 _rev_end = get(_revs, p_end)
559 for (_j = _rev_start; _j <= _rev_end; _j++)
560 array_append(_srom_table, _j)
563 # Sort in ascending order, by SROM revision
564 array_sort(_srom_table)
565 _nsrom_table = array_size(_srom_table)
567 # Write the variable definitions
568 emit("/* Variable definitions */\n")
569 emit("const struct bhnd_nvram_vardefn " \
570 "bhnd_nvram_vardefns[] = {\n")
572 for (_i = 0; _i < _noutput_vars; _i++) {
573 write_data_nvram_vardefn(array_get(output_vars, _i))
577 emit("const size_t bhnd_nvram_num_vardefns = " _noutput_vars ";\n")
579 # Write static asserts for raw type constant values that must be kept
580 # synchronized with the code
581 for (_i = 0; _i < BaseTypesCount; _i++) {
582 _base_type = array_get(BaseTypesArray, _i)
584 emit(sprintf("_Static_assert(%s == %u, \"%s\");\n",
585 type_get_const(_base_type), type_get_const_val(_base_type),
586 "type constant out of sync"))
588 emit(sprintf("_Static_assert(%s == %u, \"%s\");\n",
589 get(_base_type, p_array_const),
590 get(_base_type, p_array_const_val),
591 "array type constant out of sync"))
594 # Write all top-level bhnd_sprom_layout entries
595 emit("/* SPROM layouts */\n")
596 emit("const struct bhnd_sprom_layout bhnd_sprom_layouts[] = {\n")
598 for (_i = 0; _i < _nsrom_table; _i++) {
599 _rev = array_get(_srom_table, _i)
600 _layout = nvram_get_srom_layout(_nvram, _rev)
601 write_data_srom_layout(_layout, _rev)
605 emit("const size_t bhnd_sprom_num_layouts = " _nsrom_table ";\n")
608 # Write a bhnd_nvram_vardef entry for the given variable
609 function write_data_nvram_vardefn(v, _desc, _help, _type, _fmt) {
610 obj_assert_class(v, Var)
612 _desc = get(v, p_desc)
613 _help = get(v, p_help)
614 _type = get(v, p_type)
615 _fmt = var_get_fmt(v)
619 emit(sprintf(".name = \"%s\",\n", get(v, p_name)))
622 emit(sprintf(".desc = \"%s\",\n", get(_desc, p_value)))
624 emit(".desc = NULL,\n")
627 emit(sprintf(".help = \"%s\",\n", get(_help, p_value)))
629 emit(".help = NULL,\n")
631 emit(".type = " type_get_const(_type) ",\n")
632 emit(".nelem = " var_get_array_len(v) ",\n")
633 emit(".fmt = &" get(_fmt, p_symbol) ",\n")
634 emit(".flags = " gen_var_flags(v) ",\n")
640 # Write a top-level bhnd_sprom_layout entry for the given revision
641 # and layout definition
642 function write_data_srom_layout(layout, revision, _flags, _size,
643 _sromcrc, _crc_seg, _crc_off,
644 _sromsig, _sig_seg, _sig_offset, _sig_value,
645 _sromrev, _rev_seg, _rev_off,
650 # Calculate the size; it always follows the internal CRC variable
651 _sromcrc = srom_layout_find_entry(layout, "<sromcrc>", revision)
652 if (_sromcrc == null) {
653 errorx("missing '<sromcrc>' entry for '"revision"' layout, " \
654 "cannot compute total size")
656 _crc_seg = srom_entry_get_single_segment(_sromcrc)
657 _crc_off = get(_crc_seg, p_offset)
659 _size += get(get(_crc_seg, p_type), p_width)
662 # Fetch signature definition
663 _sromsig = srom_layout_find_entry(layout, "<sromsig>", revision)
664 if (_sromsig == null) {
665 array_append(_flags, "SPROM_LAYOUT_MAGIC_NONE")
667 _sig_seg = srom_entry_get_single_segment(_sromsig)
669 _sig_offset = get(_sig_seg, p_offset)
670 _sig_value = get(_sig_seg, p_value)
671 if (_sig_value == "")
672 errorc(get(_sromsig, p_line), "missing signature value")
675 # Fetch sromrev definition
676 _sromrev = srom_layout_find_entry(layout, "sromrev", revision)
677 if (_sromrev == null) {
678 errorx("missing 'sromrev' entry for '"revision"' layout, " \
679 "cannot determine offset")
682 if (!type_equal(get(_sromrev, p_type), UInt8)) {
683 errorx("'sromrev' entry has non-u8 type '" \
684 type_to_string(get(_sromrev, p_type)))
687 _rev_seg = srom_entry_get_single_segment(_sromrev)
688 _rev_off = get(_rev_seg, p_offset)
694 emit(".size = "_size",\n")
695 emit(".rev = "revision",\n")
697 if (array_size(_flags) > 0) {
698 emit(".flags = " array_join(_flags, "|") ",\n")
700 emit(".flags = 0,\n")
703 emit(".srev_offset = " _rev_off ",\n")
705 if (_sromsig != null) {
706 emit(".magic_offset = " _sig_offset ",\n")
707 emit(".magic_value = " _sig_value ",\n")
709 emit(".magic_offset = 0,\n")
710 emit(".magic_value = 0,\n")
713 emit(".crc_offset = " _crc_off ",\n")
715 emit(".bindings = " srom_layout_get_variable_name(layout) ",\n")
716 emit(".bindings_size = nitems(" \
717 srom_layout_get_variable_name(layout) "),\n")
719 emit(".num_vars = " srom_layout_num_output_vars(layout, revision) ",\n")
727 # Create a new opstream encoding state instance for the given layout
728 function srom_ops_new(layout, _obj) {
729 obj_assert_class(layout, SromLayout)
731 _obj = obj_new(SromOpStream)
732 set(_obj, p_layout, layout)
733 set(_obj, p_revisions, get(layout, p_revisions))
735 set(_obj, p_offset, 0)
736 set(_obj, p_type, null)
737 set(_obj, p_mask, null)
738 set(_obj, p_shift, null)
743 # Return the current type width, or throw an error if no type is currently
745 function srom_ops_get_type_width(opstream, _type)
747 obj_assert_class(opstream, SromOpStream)
749 _type = get(opstream, p_type)
751 errorx("no type value set")
753 return (get(type_get_base(_type), p_width))
756 # Write a string to the SROM opcode stream, either buffering the write,
757 # or emitting it directly.
758 function srom_ops_emit(opstream, string, _pending_bind, _buffer) {
759 obj_assert_class(opstream, SromOpStream)
762 if ((_pending_bind = get(opstream, p_pending_bind)) != null) {
763 _buffer = get(_pending_bind, p_buffer)
764 array_append(_buffer, string)
772 # Emit a SROM opcode followed by up to four optional bytes
773 function srom_ops_emit_opcode(opstream, opcode, arg0, arg1, arg2, arg3) {
774 obj_assert_class(opstream, SromOpStream)
776 srom_ops_emit(opstream, opcode",\n")
777 if (arg0 != "") srom_ops_emit(opstream, arg0",\n")
778 if (arg1 != "") srom_ops_emit(opstream, arg1",\n")
779 if (arg2 != "") srom_ops_emit(opstream, arg2",\n")
780 if (arg3 != "") srom_ops_emit(opstream, arg3",\n")
783 # Emit a SROM opcode and associated integer value, choosing the best
784 # SROM_OP_DATA variant for encoding the value.
786 # opc: The standard opcode for non-IMM encoded data, or null if none
787 # opc_imm: The IMM opcode, or null if none
788 # value: The value to encode
789 # svalue: Symbolic representation of value to include in output, or null
790 function srom_ops_emit_int_opcode(opstream, opc, opc_imm, value, svalue,
791 _width, _offset, _delta)
793 obj_assert_class(opstream, SromOpStream)
795 # Fetch current type width
796 _width = srom_ops_get_type_width(opstream)
799 if (opc_imm == SPROM_OPCODE_SHIFT_IMM) {
800 # SHIFT_IMM -- the imm value must be positive and divisible by
801 # two (shift/2) to use the IMM form.
802 if (value >= 0 && value % 2 == 0) {
808 } else if (opc_imm == SPROM_OPCODE_OFFSET_REL_IMM) {
809 # OFFSET_REL_IMM -- the imm value must be positive, divisible
810 # by the type width, and relative to the last offset to use
813 # Assert that callers correctly flushed any pending bind before
814 # attempting to set a relative offset
815 if (get(opstream, p_pending_bind) != null)
816 errorx("can't set relative offset with a pending bind")
818 # Fetch current offset, calculate relative value and determine
819 # whether we can issue an IMM opcode
820 _offset = get(opstream, p_offset)
821 _delta = value - _offset
823 _delta % _width == 0 &&
824 (_delta/_width) <= SPROM_OP_IMM_MAX)
826 srom_ops_emit(opstream,
827 sprintf("/* %#x + %#x -> %#x */\n", _offset,
829 value = (_delta / _width)
836 # If no symbolic representation provided, write the raw value
840 # Try to encode as IMM value?
841 if (opc_imm != null && value >= 0 && value <= SPROM_OP_IMM_MAX) {
842 srom_ops_emit_opcode(opstream, "("opc_imm"|"svalue")")
846 # Can't encode as immediate; do we have a non-immediate form?
848 errorx("can't encode '" value "' as immediate, and no " \
849 "non-immediate form was provided")
851 # Determine and emit minimal encoding
852 # We let the C compiler perform the bit operations, rather than
853 # trying to wrestle awk's floating point arithmetic
857 errorx("cannot int8 encode '" value "'")
859 srom_ops_emit_opcode(opstream,
860 "("opc"|"SPROM_OP_DATA_I8")", svalue)
862 } else if (value <= UInt8Max) {
864 srom_ops_emit_opcode(opstream,
865 "("opc"|"SPROM_OP_DATA_U8")", svalue)
867 } else if (value % _width == 0 && (value / _width) <= UInt8Max) {
869 srom_ops_emit_opcode(opstream,
870 "("opc"|"SPROM_OP_DATA_U8_SCALED")", svalue / _width)
872 } else if (value <= UInt16Max) {
874 srom_ops_emit_opcode(opstream,
875 "("opc"|"SPROM_OP_DATA_U16")",
879 } else if (value <= UInt32Max) {
881 srom_ops_emit_opcode(opstream,
882 "("opc"|"SPROM_OP_DATA_U32")",
884 "(("svalue" >> 8) & 0xFF)",
885 "(("svalue" >> 16) & 0xFF)",
886 "(("svalue" >> 24) & 0xFF)")
889 errorx("can't encode '" value "' (too large)")
893 # Emit initial OPCODE_VAR opcode and update opstream state
894 function srom_ops_reset_var(opstream, var, _vid_prev, _vid, _vid_name,
897 obj_assert_class(opstream, SromOpStream)
898 obj_assert_class(var, Var)
900 # Flush any pending bind for the previous variable
901 srom_ops_flush_bind(opstream, 1)
903 # Fetch current state
904 _vid_prev = get(opstream, p_vid)
906 _vid = get(var, p_vid)
907 _vid_name = var_get_macro_name(var, MTypeVarID)
910 _type = get(var, p_type)
911 set(opstream, p_vid, _vid)
912 set(opstream, p_type, type_get_base(_type))
913 set(opstream, p_nelem, var_get_array_len(var))
914 set(opstream, p_mask, type_get_default_mask(_type))
915 set(opstream, p_shift, 0)
916 set(opstream, p_bind_total, 0)
918 # Always provide a human readable comment
919 srom_ops_emit(opstream, sprintf("/* %s (%#x) */\n", get(var, p_name),
920 get(opstream, p_offset)))
922 # Prefer a single VAR_IMM byte
923 if (_vid_prev == 0 || _vid <= SPROM_OP_IMM_MAX) {
924 srom_ops_emit_int_opcode(opstream,
925 null, SPROM_OPCODE_VAR_IMM,
930 # Try encoding as a single VAR_REL_IMM byte
931 if (_vid_prev <= _vid && (_vid - _vid_prev) <= SPROM_OP_IMM_MAX) {
932 srom_ops_emit_int_opcode(opstream,
933 null, SPROM_OPCODE_VAR_REL_IMM,
934 _vid - _vid_prev, null)
938 # Fall back on a multibyte encoding
939 srom_ops_emit_int_opcode(opstream, SPROM_OPCODE_VAR, null, _vid,
943 # Emit OPCODE_REV/OPCODE_REV_RANGE (if necessary) for a new revision range
944 function srom_ops_emit_revisions(opstream, revisions, _prev_revs,
947 obj_assert_class(opstream, SromOpStream)
948 _prev_revs = get(opstream, p_revisions)
950 if (revrange_equal(_prev_revs, revisions))
953 # Update stream state
954 set(opstream, p_revisions, revisions)
956 _start = get(revisions, p_start)
957 _end = get(revisions, p_end)
959 # Sanity-check range values
960 if (_start < 0 || _end < 0)
961 errorx("invalid range: " revrange_to_string(revisions))
963 # If range covers a single revision, and can be encoded within
964 # SROM_OP_IMM_MAX, we can use the single byte encoding
965 if (_start == _end && _start <= SPROM_OP_IMM_MAX) {
966 srom_ops_emit_int_opcode(opstream,
967 null, SPROM_OPCODE_REV_IMM, _start)
971 # Otherwise, we need to use the two byte range encoding
972 if (_start > SPROM_OP_REV_RANGE_MAX || _end > SPROM_OP_REV_RANGE_MAX) {
973 errorx(sprintf("cannot encode range values %s (>= %u)",
974 revrange_to_string(revisions), SPROM_OP_REV_RANGE_MAX))
977 srom_ops_emit_opcode(opstream,
978 SPROM_OPCODE_REV_RANGE,
979 sprintf("(%u << %s) | (%u << %s)",
980 _start, SPROM_OP_REV_START_SHIFT,
981 _end, SPROM_OP_REV_END_SHIFT))
984 # Emit OPCODE_OFFSET (if necessary) for a new offset
985 function srom_ops_emit_offset(opstream, offset, _prev_offset, _rel_offset,
988 obj_assert_class(opstream, SromOpStream)
990 # Flush any pending bind before adjusting the offset
991 srom_ops_flush_bind(opstream, 0)
993 # Fetch current offset
994 _prev_offset = get(opstream, p_offset)
995 if (_prev_offset == offset)
998 # Encode (possibly a relative, 1-byte form) of the offset opcode
999 srom_ops_emit_int_opcode(opstream, SPROM_OPCODE_OFFSET,
1000 SPROM_OPCODE_OFFSET_REL_IMM, offset, null)
1003 set(opstream, p_offset, offset)
1006 # Emit OPCODE_TYPE (if necessary) for a new type value; this also
1007 # resets the mask to the type default.
1008 function srom_ops_emit_type(opstream, type, _base_type, _prev_type, _prev_mask,
1011 obj_assert_class(opstream, SromOpStream)
1012 if (!obj_is_instanceof(type, ArrayType))
1013 obj_assert_class(type, Type)
1015 _default_mask = type_get_default_mask(type)
1016 _base_type = type_get_base(type)
1018 # If state already matches the requested type, nothing to be
1020 _prev_type = get(opstream, p_type)
1021 _prev_mask = get(opstream, p_mask)
1022 if (type_equal(_prev_type, _base_type) && _prev_mask == _default_mask)
1026 set(opstream, p_type, _base_type)
1027 set(opstream, p_mask, _default_mask)
1030 if (type_get_const_val(_base_type) <= SPROM_OP_IMM_MAX) {
1031 # Single byte IMM encoding
1032 srom_ops_emit_opcode(opstream,
1033 SPROM_OPCODE_TYPE_IMM "|" type_get_const(_base_type))
1036 srom_ops_emit_opcode(opstream, SPROM_OPCODE_TYPE,
1037 type_get_const(_base_type))
1041 # Emit OPCODE_MASK (if necessary) for a new mask value
1042 function srom_ops_emit_mask(opstream, mask, _prev_mask) {
1043 obj_assert_class(opstream, SromOpStream)
1044 _prev_mask = get(opstream, p_mask)
1046 if (_prev_mask == mask)
1049 set(opstream, p_mask, mask)
1050 srom_ops_emit_int_opcode(opstream,
1051 SPROM_OPCODE_MASK, SPROM_OPCODE_MASK_IMM,
1052 mask, sprintf("0x%x", mask))
1055 # Emit OPCODE_SHIFT (if necessary) for a new shift value
1056 function srom_ops_emit_shift(opstream, shift, _prev_shift) {
1057 obj_assert_class(opstream, SromOpStream)
1058 _prev_shift = get(opstream, p_shift)
1060 if (_prev_shift == shift)
1063 set(opstream, p_shift, shift)
1064 srom_ops_emit_int_opcode(opstream,
1065 SPROM_OPCODE_SHIFT, SPROM_OPCODE_SHIFT_IMM,
1069 # Return true if a valid BIND/BINDN encoding exists for the given SKIP_IN
1070 # value, false if the skip values exceed the limits of the bind opcode
1072 function srom_ops_can_encode_skip_in(skip_in) {
1073 return (skip_in >= SPROM_OP_BIND_SKIP_IN_MIN &&
1074 skip_in <= SPROM_OP_BIND_SKIP_IN_MAX)
1077 # Return true if a valid BIND/BINDN encoding exists for the given SKIP_OUT
1078 # value, false if the skip values exceed the limits of the bind opcode
1080 function srom_ops_can_encode_skip_out(skip_out) {
1081 return (skip_in >= SPROM_OP_BIND_SKIP_IN_MIN &&
1082 skip_in <= SPROM_OP_BIND_SKIP_IN_MAX)
1085 # Return true if a valid BIND/BINDN encoding exists for the given skip
1086 # values, false if the skip values exceed the limits of the bind opcode
1088 function srom_ops_can_encode_skip(skip_in, skip_out) {
1089 return (srom_ops_can_encode_skip_in(skip_in) &&
1090 srom_ops_can_encode_skip_out(skip_out))
1093 # Create a new SromOpBind instance for the given segment
1094 function srom_opbind_new(segment, skip_in, skip_out, _obj, _type, _width,
1097 obj_assert_class(segment, SromSegment)
1099 # Verify that an encoding exists for the skip values
1100 if (!srom_ops_can_encode_skip_in(skip_in)) {
1101 errorx(sprintf("cannot encode SKIP_IN=%d; maximum supported " \
1102 "range %d-%d", skip_in,
1103 SPROM_OP_BIND_SKIP_IN_MIN, SPROM_OP_BIND_SKIP_IN_MAX))
1106 if (!srom_ops_can_encode_skip_out(skip_out)) {
1107 errorx(sprintf("cannot encode SKIP_OUT=%d; maximum supported " \
1108 "range %d-%d", skip_out,
1109 SPROM_OP_BIND_SKIP_OUT_MIN, SPROM_OP_BIND_SKIP_OUT_MAX))
1112 # Fetch basic segment info
1113 _offset = get(segment, p_offset)
1114 _type = srom_segment_get_base_type(segment)
1115 _width = get(_type, p_width)
1117 # Construct new instance
1118 _obj = obj_new(SromOpBind)
1120 set(_obj, p_segment, segment)
1121 set(_obj, p_count, 1)
1122 set(_obj, p_offset, _offset)
1123 set(_obj, p_width, _width)
1124 set(_obj, p_skip_in, skip_in)
1125 set(_obj, p_skip_out, skip_out)
1126 set(_obj, p_buffer, array_new())
1131 # Try to coalesce a BIND for the given segment with an existing bind request,
1132 # returning true on success, or false if the two segments cannot be coalesced
1133 # into the existing request
1134 function srom_opbind_append(bind, segment, skip_out, _bind_seg, _bind_off,
1135 _width, _count, _skip_in, _seg_offset, _delta)
1137 obj_assert_class(bind, SromOpBind)
1138 obj_assert_class(segment, SromSegment)
1140 # Are the segments compatible?
1141 _bind_seg = get(bind, p_segment)
1142 if (!srom_segment_attributes_equal(_bind_seg, segment))
1145 # Are the output skip values compatible?
1146 if (get(bind, p_skip_out) != skip_out)
1149 # Find bind offset/count/width/skip
1150 _bind_off = get(bind, p_offset)
1151 _count = get(bind, p_count)
1152 _skip_in = get(bind, p_skip_in)
1153 _width = get(bind, p_width)
1155 # Fetch new segment's offset
1156 _seg_offset = get(segment, p_offset)
1158 # If there's only one segment in the bind op, we ned to compute the
1159 # skip value to be used for all later segments (including the
1160 # segment we're attempting to append)
1162 # If there's already multiple segments, we just need to verify that
1163 # the bind_offset + (count * width * skip_in) produces the new
1166 # Determine the delta between the two segment offsets. This
1167 # must be a multiple of the type width to be encoded
1169 _delta = _seg_offset - _bind_off
1170 if ((_delta % _width) != 0)
1173 # The skip byte count is calculated as (type width * skip)
1174 _skip_in = _delta / _width
1176 # Is the skip encodable?
1177 if (!srom_ops_can_encode_skip_in(_skip_in))
1180 # Save required skip
1181 set(bind, p_skip_in, _skip_in)
1182 } else if (_count > 1) {
1183 # Get the final offset of the binding if we were to add
1184 # one additional segment
1185 _bind_off = _bind_off + (_width * _skip_in * (_count + 1))
1187 # If it doesn't match our segment's offset, we can't
1188 # append this segment
1189 if (_bind_off != _seg_offset)
1193 # Success! Increment the bind count in the existing bind
1194 set(bind, p_count, _count + 1)
1198 # Return true if the given binding operation can be omitted from the output
1199 # if it would be immediately followed by a VAR, VAR_REL_IMM, or EOF opcode.
1201 # The bind operatin must be configured with default count, skip_in, and
1202 # skip_out values of 1, and must contain no buffered post-BIND opcodes
1203 function srom_opbind_is_implicit_encodable(bind) {
1204 obj_assert_class(bind, SromOpBind)
1206 if (get(bind, p_count) != 1)
1209 if (get(bind, p_skip_in) != 1)
1212 if (get(bind, p_skip_out) != 1)
1215 if (array_size(get(bind, p_buffer)) != 0)
1222 # Encode all segment settings for a single offset segment, followed by a bind
1225 # opstream: Opcode stream
1226 # segment: Segment to be written
1227 # continued: If this segment's value should be OR'd with the value of a
1229 function srom_ops_emit_segment(opstream, segment, continued, _value,
1230 _bind, _skip_in, _skip_out)
1232 obj_assert_class(opstream, SromOpStream)
1233 obj_assert_class(segment, SromSegment)
1235 # Determine basic bind parameters
1238 _skip_out = continued ? 0 : 1
1240 # Try to coalesce with a pending binding
1241 if ((_bind = get(opstream, p_pending_bind)) != null) {
1242 if (srom_opbind_append(_bind, segment, _skip_out))
1246 # Otherwise, flush any pending bind and enqueue our own
1247 srom_ops_flush_bind(opstream, 0)
1248 if (get(opstream, p_pending_bind))
1249 errorx("bind not flushed!")
1252 _value = get(segment, p_type)
1253 srom_ops_emit_type(opstream, _value)
1256 _value = get(segment, p_offset)
1257 srom_ops_emit_offset(opstream, _value)
1260 _value = get(segment, p_mask)
1261 srom_ops_emit_mask(opstream, _value)
1264 _value = get(segment, p_shift)
1265 srom_ops_emit_shift(opstream, _value)
1267 # Enqueue binding with opstream
1268 _bind = srom_opbind_new(segment, _skip_in, _skip_out)
1269 set(opstream, p_pending_bind, _bind)
1272 # (private) Adjust the stream's input offset by applying the given bind
1273 # operation's skip_in * width * count.
1274 function _srom_ops_apply_bind_offset(opstream, bind, _count, _offset, _width,
1275 _skip_in, _opstream_offset)
1277 obj_assert_class(opstream, SromOpStream)
1278 obj_assert_class(bind, SromOpBind)
1280 _opstream_offset = get(opstream, p_offset)
1281 _offset = get(bind, p_offset)
1282 if (_opstream_offset != _offset)
1283 errorx("stream/bind offset state mismatch")
1285 _count = get(bind, p_count)
1286 _width = get(bind, p_width)
1287 _skip_in = get(bind, p_skip_in)
1289 set(opstream, p_offset,
1290 _opstream_offset + ((_width * _skip_in) * _count))
1293 # (private) Write a bind instance and all buffered opcodes
1294 function _srom_ops_emit_bind(opstream, bind, _count, _skip_in, _skip_out,
1295 _off_start, _width, _si_signbit, _written, _nbuffer, _buffer)
1297 obj_assert_class(opstream, SromOpStream)
1298 obj_assert_class(bind, SromOpBind)
1300 # Assert that any pending bind state has already been cleared
1301 if (get(opstream, p_pending_bind) != null)
1302 errorx("cannot flush bind with an existing pending_bind active")
1304 # Fetch (and assert valid) our skip values
1305 _skip_in = get(bind, p_skip_in)
1306 _skip_out = get(bind, p_skip_out)
1308 if (!srom_ops_can_encode_skip(_skip_in, _skip_out))
1309 errorx("invalid skip values in buffered bind")
1311 # Determine SKIP_IN sign bit
1314 _si_signbit = SPROM_OP_BIND_SKIP_IN_SIGN
1316 # Emit BIND/BINDN opcodes until the full count is encoded
1317 _count = get(bind, p_count)
1318 while (_count > 0) {
1319 if (_count > 1 && _count <= SPROM_OP_IMM_MAX &&
1320 _skip_in == 1 && _skip_out == 1)
1322 # The one-byte BINDN form requires encoding the count
1323 # as a IMM, and has an implicit in/out skip of 1.
1324 srom_ops_emit_opcode(opstream,
1325 "("SPROM_OPCODE_DO_BINDN_IMM"|"_count")")
1328 } else if (_count > 1) {
1329 # The two byte BINDN form can encode skip values and a
1331 _written = min(_count, UInt8Max)
1333 srom_ops_emit_opcode(opstream,
1334 sprintf("(%s|%s|(%u<<%s)|(%u<<%s))",
1335 SPROM_OPCODE_DO_BINDN,
1337 abs(_skip_in), SPROM_OP_BIND_SKIP_IN_SHIFT,
1338 _skip_out, SPROM_OP_BIND_SKIP_OUT_SHIFT),
1343 # The 1-byte BIND form can encode the same SKIP values
1344 # as the 2-byte BINDN, with a implicit count of 1
1345 srom_ops_emit_opcode(opstream,
1346 sprintf("(%s|%s|(%u<<%s)|(%u<<%s))",
1347 SPROM_OPCODE_DO_BIND,
1349 abs(_skip_in), SPROM_OP_BIND_SKIP_IN_SHIFT,
1350 _skip_out, SPROM_OP_BIND_SKIP_OUT_SHIFT))
1355 # Update the stream's input offset
1356 _srom_ops_apply_bind_offset(opstream, bind)
1358 # Write any buffered post-BIND opcodes
1359 _buffer = get(bind, p_buffer)
1360 _nbuffer = array_size(_buffer)
1361 for (_i = 0; _i < _nbuffer; _i++)
1362 srom_ops_emit(opstream, array_get(_buffer, _i))
1365 # Flush any buffered binding
1366 function srom_ops_flush_bind(opstream, allow_implicit, _bind, _bind_total)
1368 obj_assert_class(opstream, SromOpStream)
1370 # If no pending bind, nothing to flush
1371 if ((_bind = get(opstream, p_pending_bind)) == null)
1374 # Check the per-variable bind count to determine whether
1375 # we can encode an implicit bind.
1377 # If there have been any explicit bind statements, implicit binding
1379 _bind_total = get(opstream, p_bind_total)
1380 if (allow_implicit && _bind_total > 0) {
1381 # Disable implicit encoding; explicit bind statements have
1382 # been issued for this variable previously.
1386 # Increment bind count
1387 set(opstream, p_bind_total, _bind_total + 1)
1389 # Clear the property value
1390 set(opstream, p_pending_bind, null)
1392 # If a pending bind operation can be encoded as an implicit bind,
1393 # emit a descriptive comment and update the stream state.
1395 # Otherwise, emit the full set of bind opcode(s)
1396 _base_off = get(opstream, p_offset)
1397 if (allow_implicit && srom_opbind_is_implicit_encodable(_bind)) {
1398 # Update stream's input offset
1399 _srom_ops_apply_bind_offset(opstream, _bind)
1401 _srom_ops_emit_bind(opstream, _bind)
1404 # Provide bind information as a comment
1405 srom_ops_emit(opstream,
1406 sprintf("/* bind (%s @ %#x -> %#x) */\n",
1407 type_to_string(get(opstream, p_type)),
1408 _base_off, get(opstream, p_offset)))
1414 # Write OPCODE_EOF after flushing any buffered writes
1415 function srom_ops_emit_eof(opstream) {
1416 obj_assert_class(opstream, SromOpStream)
1418 # Flush any buffered writes
1419 srom_ops_flush_bind(opstream, 1)
1421 # Emit an explicit VAR_END opcode for the last entry
1422 srom_ops_emit_opcode(opstream, SPROM_OPCODE_VAR_END)
1425 srom_ops_emit_opcode(opstream, SPROM_OPCODE_EOF)
1428 # Write the SROM offset segment bindings to the opstream
1429 function write_srom_offset_bindings(opstream, offsets,
1430 _noffsets, _offset, _segs, _nsegs, _segment, _cont,
1433 _noffsets = array_size(offsets)
1434 for (_i = 0; _i < _noffsets; _i++) {
1435 # Encode each segment in this offset
1436 _offset = array_get(offsets, _i)
1437 _segs = get(_offset, p_segments)
1438 _nsegs = array_size(_segs)
1440 for (_j = 0; _j < _nsegs; _j++) {
1441 _segment = array_get(_segs, _j)
1444 # Should this value be OR'd with the next segment?
1449 srom_ops_emit_segment(opstream, _segment, _cont)
1454 # Write the SROM entry stream for a SROM entry to the output file
1455 function write_srom_entry_bindings(entry, opstream, _var, _vid,
1456 _var_type, _entry_type, _offsets, _noffsets)
1458 _var = get(entry, p_var)
1459 _vid = get(_var, p_vid)
1461 # Encode revision switch. This resets variable state, so must
1462 # occur before any variable definitions to which it applies
1463 srom_ops_emit_revisions(opstream, get(entry, p_revisions))
1465 # Encode variable ID
1466 srom_ops_reset_var(opstream, _var, _vid)
1469 # Write entry-specific array length (SROM layouts may define array
1470 # mappings with fewer elements than in the variable definition)
1471 if (srom_entry_has_array_type(entry)) {
1472 _var_type = get(_var, p_type)
1473 _entry_type = get(entry, p_type)
1475 # If the array length differs from the variable default,
1476 # write an OPCODE_EXT_NELEM entry
1477 if (type_get_nelem(_var_type) != type_get_nelem(_entry_type)) {
1478 srom_ops_emit_opcode(opstream, SPROM_OPCODE_NELEM,
1479 srom_entry_get_array_len(entry))
1483 # Write offset segment bindings
1484 _offsets = get(entry, p_offsets)
1485 write_srom_offset_bindings(opstream, _offsets)
1489 # Write a SROM layout binding opcode table to the output file
1490 function write_srom_bindings(layout, _varname, _var, _all_entries,
1491 _nall_entries, _entries, _nentries, _entry, _opstream, _i)
1493 _varname = srom_layout_get_variable_name(layout)
1494 _all_entries = get(layout, p_entries)
1495 _opstream = srom_ops_new(layout)
1498 # Collect all entries to be included in the output, and then
1499 # sort by their variable's assigned ID (ascending).
1501 # The variable IDs were previously assigned in lexigraphical sort
1502 # order; since the variable *offsets* tend to match this order, this
1503 # works out well for our compact encoding, allowing us to make use of
1504 # compact relative encoding of both variable IDs and variable offsets.
1506 _entries = array_new()
1507 _nall_entries = array_size(_all_entries)
1508 for (_i = 0; _i < _nall_entries; _i++) {
1509 _entry = array_get(_all_entries, _i)
1510 _var = get(_entry, p_var)
1512 # Skip internal variables
1513 if (var_is_internal(_var))
1516 # Sanity check variable ID assignment
1517 if (get(_var, p_vid) == "")
1518 errorx("missing variable ID for " obj_to_string(_var))
1520 array_append(_entries, _entry)
1523 # Sort entries by (variable ID, revision range), ascending
1524 array_sort(_entries, prop_path_create(p_var, p_vid),
1525 prop_path_create(p_revisions, p_start),
1526 prop_path_create(p_revisions, p_end))
1528 # Emit all entry binding opcodes
1529 emit("static const uint8_t " _varname "[] = {\n")
1532 _nentries = array_size(_entries)
1533 for (_i = 0; _i < _nentries; _i++) {
1534 _entry = array_get(_entries, _i)
1535 write_srom_entry_bindings(_entry, _opstream)
1538 # Flush and write EOF
1539 srom_ops_emit_eof(_opstream)
1544 obj_delete(_opstream)
1545 obj_delete(_entries)
1548 # Write the BHND_NVAR_<NAME>_ID #defines to the output file
1549 function write_data_defines(output_vars, _noutput_vars, _tab_align, _var,
1550 _macro, _macros, _num_macros, _i)
1552 # Produce our array of #defines
1554 _noutput_vars = array_size(output_vars)
1555 for (_i = 0; _i < _noutput_vars; _i++) {
1556 _var = array_get(output_vars, _i)
1559 _macro = var_get_macro(_var, MTypeVarID, get(_var, p_vid))
1560 _macros[_num_macros++] = _macro
1563 # Calculate value tab alignment position for our macros
1564 _tab_align = macros_get_tab_alignment(_macros, _num_macros)
1566 # Write the #defines
1567 emit("/* ID constants provide an index into the variable array */\n")
1568 for (_i = 0; _i < _num_macros; _i++)
1569 write_macro_define(_macros[_i], _tab_align)
1573 # Calculate the common tab alignment to be used with a set of prefix strings
1574 # with the given maximum length
1575 function tab_alignment(max_len, _tab_align) {
1576 _tab_align = max_len
1577 _tab_align += (TAB_WIDTH - (_tab_align % TAB_WIDTH)) % TAB_WIDTH
1578 _tab_align /= TAB_WIDTH
1583 # Generate and return a tab string that can be appended to a string of
1584 # `strlen` to pad the column out to `align_to`
1586 # Note: If the string from which strlen was derived contains tabs, the result
1588 function tab_str(strlen, align_to, _lead, _pad, _result, _i) {
1590 _lead -= (_lead % TAB_WIDTH);
1593 # Determine required padding to reach the desired alignment
1594 if (align_to >= _lead)
1595 _pad = align_to - _lead;
1599 for (_i = 0; _i < _pad; _i++)
1600 _result = _result "\t"
1606 # Write a MacroDefine constant, padding the constant out to `align_to`
1607 function write_macro_define(macro, align_to, _tabstr, _i) {
1608 # Determine required padding to reach the desired alignment
1609 _tabstr = tab_str(length(get(macro, p_name)), align_to)
1611 emit("#define\t" get(macro, p_name) _tabstr get(macro, p_value) "\n")
1614 # Calculate the tab alignment to be used with a given integer-indexed array
1615 # of Macro instances.
1616 function macros_get_tab_alignment(macros, macros_len, _macro, _max_len, _i) {
1618 for (_i = 0; _i < macros_len; _i++) {
1620 _max_len = max(_max_len, length(get(_macro, p_name)))
1623 return (tab_alignment(_max_len))
1626 # Variable group block
1627 $1 == "group" && in_parser_context(NVRAM) {
1628 parse_variable_group()
1631 # Variable definition
1632 (($1 ~ VACCESS_REGEX && $2 ~ TYPES_REGEX) || $1 ~ TYPES_REGEX) &&
1633 in_parser_context(SymbolContext) \
1635 parse_variable_defn()
1638 # Variable "fmt" parameter
1639 $1 == "fmt" && in_parser_context(Var) {
1640 parse_variable_param($1)
1644 # Variable "all1" parameter
1645 $1 == "all1" && in_parser_context(Var) {
1646 parse_variable_param($1)
1650 # Variable desc/help parameters
1651 ($1 == "desc" || $1 == "help") && in_parser_context(Var) {
1652 parse_variable_param($1)
1657 $1 == "srom" && in_parser_context(NVRAM) {
1662 # SROM layout revision filter block
1663 $1 == "srom" && in_parser_context(SromLayout) {
1664 parse_srom_layout_filter()
1667 # SROM layout variable entry
1668 $1 ~ "("OFF_REGEX"):$" && \
1669 (in_parser_context(SromLayout) || in_parser_context(SromLayoutFilter)) \
1671 parse_srom_variable_entry()
1675 # SROM entry segment
1676 $1 ~ "("REL_OFF_REGEX"|"OFF_REGEX")[:,|]?" && in_parser_context(SromEntry) {
1677 parse_srom_entry_segments()
1680 # Skip comments and blank lines
1681 /^[ \t]*#/ || /^$/ {
1686 /}/ && !in_parser_context(NVRAM) {
1687 while (!in_parser_context(NVRAM) && $0 ~ "}") {
1688 parser_state_close_block();
1693 # Report unbalanced '}'
1694 /}/ && in_parser_context(NVRAM) {
1698 # Invalid variable type
1699 $1 && in_parser_context(SymbolContext) {
1700 error("unknown type '" $1 "'")
1703 # Generic parse failure
1705 error("unrecognized statement")
1708 # Create a class instance with the given name
1709 function class_new(name, superclass, _class) {
1711 errorx("class_get() must be called with one or two arguments")
1713 # Look for an existing class instance
1714 if (name in _g_class_names)
1715 errorx("redefining class: " name)
1717 # Create and register the class object
1718 _class = obj_new(superclass)
1719 _g_class_names[name] = _class
1720 _g_obj[_class,OBJ_IS_CLS] = 1
1721 _g_obj[_class,CLS_NAME] = name
1726 # Return the class instance with the given name
1727 function class_get(name) {
1728 if (name in _g_class_names)
1729 return (_g_class_names[name])
1731 errorx("no such class " name)
1734 # Return the name of cls
1735 function class_get_name(cls) {
1737 warnx("class_get_name() called with null class")
1741 if (!obj_is_class(cls))
1742 errorx(cls " is not a class object")
1744 return (_g_obj[cls,CLS_NAME])
1747 # Return true if the given property property ID is defined on class
1748 function class_has_prop_id(class, prop_id, _super) {
1750 errorx("class_has_prop_id() must be called with two arguments")
1755 if (prop_id == null)
1758 # Check class<->prop cache
1759 if ((class, prop_id) in _g_class_prop_cache)
1762 # Otherwise, slow path
1763 if (!obj_is_class(class))
1764 errorx(class " is not a class object")
1767 errorx("class_has_prop_id() must be called with two arguments")
1769 for (_super = class; _super != null; _super = obj_get_class(_super)) {
1770 if (!((_super,CLS_PROP,prop_id) in _g_obj))
1773 # Found; add to class<->prop cache
1774 _g_class_prop_cache[class,prop_id] = 1
1781 # Return true if the given property prop is defined on class
1782 function class_has_property(class, prop) {
1783 if (!(PROP_ID in prop))
1786 return (class_has_prop_id(class, prop[PROP_ID]))
1789 # Define a `prop` on `class` with the given `name` string
1790 function class_add_prop(class, prop, name, _prop_id) {
1791 if (_prop_id != null)
1792 errorx("class_add_prop() must be called with three arguments")
1794 # Check for duplicate property definition
1795 if (class_has_property(class, prop))
1796 errorx("property " prop[PROP_NAME] " already defined on " \
1797 class_get_name(class))
1800 if (_g_prop_ids == null)
1803 # Get (or create) new property entry
1804 if (name in _g_prop_names) {
1805 _prop_id = _g_prop_names[name]
1807 _prop_id = _g_prop_ids++
1808 _g_prop_names[name] = _prop_id
1809 _g_props[_prop_id] = name
1811 prop[PROP_NAME] = name
1812 prop[PROP_ID] = _prop_id
1815 # Add to class definition
1816 _g_obj[class,CLS_PROP,prop[PROP_ID]] = name
1820 # Return the property ID for a given class-defined property
1821 function class_get_prop_id(class, prop) {
1823 errorx("class_get_prop_id() on null class")
1825 if (!class_has_property(class, prop)) {
1826 errorx("requested undefined property '" prop[PROP_NAME] "on " \
1827 class_get_name(class))
1830 return (prop[PROP_ID])
1833 # Return the property ID for a given class-defined property name
1834 function class_get_named_prop_id(class, name, _prop_id) {
1836 errorx("class_get_prop_id() on null class")
1838 if (!(name in _g_prop_names))
1839 errorx("requested undefined property '" name "'")
1841 _prop_id = _g_prop_names[name]
1843 if (!class_has_prop_id(class, _prop_id)) {
1844 errorx("requested undefined property '" _g_props[_prop_id] \
1845 "' on " class_get_name(class))
1851 # Create a new instance of the given class
1852 function obj_new(class, _obj) {
1854 errorx("obj_new() must be called with one argument")
1856 if (_g_obj_ids == null)
1859 # Assign ID and set superclass
1861 _g_obj[_obj,OBJ_SUPER] = class
1866 # obj_delete() support for Map instances
1867 function _obj_delete_map(obj, _prefix, _key) {
1868 obj_assert_class(obj, Map)
1869 _prefix = "^" obj SUBSEP
1870 for (_key in _g_maps) {
1871 if (!match(_key, _prefix) && _key != obj)
1873 delete _g_maps[_key]
1877 # obj_delete() support for Array instances
1878 function _obj_delete_array(obj, _size, _i) {
1879 obj_assert_class(obj, Array)
1880 _size = array_size(obj)
1882 for (_i = 0; _i < _size; _i++)
1883 delete _g_arrays[obj,OBJ_PROP,_i]
1886 # Destroy all metadata associated with the given object
1887 function obj_delete(obj, _prop_id, _prop_name, _prefix, _key, _size, _i) {
1888 if (obj_is_class(obj))
1889 errorx("cannot delete class objects")
1891 # Handle classes that use external global array storage
1893 if (obj_is_instanceof(obj, Map)) {
1894 _obj_delete_map(obj)
1895 } else if (obj_is_instanceof(obj, Array)) {
1896 _obj_delete_array(obj)
1899 # Delete all object properties
1900 for (_prop_name in _g_prop_names) {
1901 if (!obj_has_prop_id(obj, _prop_id))
1904 _prop_id = _g_prop_names[_prop_name]
1905 delete _g_obj[obj,OBJ_PROP,_prop_id]
1906 delete _g_obj_nr[obj,OBJ_PROP,_prop_id]
1909 # Delete instance state
1910 delete _g_obj[obj,OBJ_IS_CLS]
1911 delete _g_obj[obj,OBJ_SUPER]
1914 # Print an object's unique ID, class, and properties to
1916 function obj_dump(obj, _pname, _prop_id, _prop_val) {
1917 print(class_get_name(obj_get_class(obj)) "<" obj ">:")
1919 # Dump all properties
1920 for (_pname in _g_prop_names) {
1921 _prop_id = _g_prop_names[_pname]
1923 if (!obj_has_prop_id(obj, _prop_id))
1926 _prop_val = prop_get(obj, _prop_id)
1927 printf("\t%s: %s\n", _pname, _prop_val)
1931 # Return true if obj is a class object
1932 function obj_is_class(obj) {
1933 return (_g_obj[obj,OBJ_IS_CLS] == 1)
1936 # Return the class of obj, if any.
1937 function obj_get_class(obj) {
1939 errorx("obj_get_class() on null object")
1940 return (_g_obj[obj,OBJ_SUPER])
1943 # Return true if obj is an instance of the given class
1944 function obj_is_instanceof(obj, class, _super) {
1946 errorx("obj_is_instanceof() must be called with two arguments")
1948 if (!obj_is_class(class))
1949 errorx(class " is not a class object")
1952 errorx("obj_is_instanceof() called with null obj (class " \
1953 class_get_name(class) ")")
1956 for (_super = obj_get_class(obj); _super != null;
1957 _super = obj_get_class(_super))
1959 if (_super == class)
1966 # Default object shallow equality implementation. Returns true if the two
1967 # objects share a common superclass and have identity equality across all defined
1969 function obj_trivially_equal(lhs, rhs, _class, _pname, _prop_id) {
1974 # Must share a common superclass
1975 _class = obj_get_class(lhs)
1976 if (_class != obj_get_class(rhs))
1979 # Compare all properties
1981 for (_pname in _g_prop_names) {
1982 _prop_id = _g_prop_names[_pname]
1984 if (!class_has_prop_id(_class, _prop_id))
1987 if (prop_get(lhs, _prop_id) != prop_get(rhs, _prop_id))
1991 # All properties are trivially equal
1996 # Return a debug string representation of an object's unique ID, class, and
1998 function obj_to_string(obj, _pname, _prop_id, _prop_val, _prop_count, _result) {
1999 _result = class_get_name(obj_get_class(obj)) "<" obj ">: { "
2001 # Fetch all properties
2003 for (_pname in _g_prop_names) {
2004 _prop_id = _g_prop_names[_pname]
2006 if (!obj_has_prop_id(obj, _prop_id))
2009 if (_prop_count >= 0)
2010 _result = _result ", "
2012 _result = _result sprintf("\t%s: %s\n", _pname, _prop_val)
2016 return (_result " }")
2019 # Assert that obj is an instance of the given class
2020 function obj_assert_class(obj, class) {
2021 if (!obj_is_instanceof(obj, class)) {
2022 errorx(class_get_name(obj_get_class(obj)) "<" obj "> is not " \
2023 "an instance of " class_get_name(class))
2027 # Return true if the given property prop is defined by the object's superclass
2028 function obj_has_property(obj, prop, _class) {
2030 errorx("obj_has_property() on null object")
2032 _class = obj_get_class(obj)
2033 return (class_has_property(_class, prop))
2036 # Return true if the given property ID is defined by the object's superclass
2037 function obj_has_prop_id(obj, prop_id, _class) {
2039 errorx("obj_has_prop_id() on null object")
2041 _class = obj_get_class(obj)
2042 return (class_has_prop_id(_class, prop_id))
2045 # Return the line (NR) at which a given property ID was set on the object
2046 # Will throw an error if the property has not been set on obj
2047 function obj_get_prop_id_nr(obj, prop_id) {
2049 errorx("obj_get_prop_id_nr() on null object")
2051 if (!obj_has_prop_id(obj, prop_id)) {
2052 errorx("requested undefined property '" _g_props[prop_id] \
2053 "' (" prop_id ") on " obj_to_string(obj))
2057 if ((obj,OBJ_PROP,prop_id) in _g_obj_nr)
2058 return (_g_obj_nr[obj,OBJ_PROP,prop_id])
2060 errorx("property '" _g_props[prop_id] "' (" prop_id ") not " \
2061 "previously set on " obj_to_string(obj))
2064 # Return the line (NR) at which a given property was set on the object
2065 # Will throw an error if the property has not been set on obj
2066 function obj_get_prop_nr(obj, prop) {
2067 return (obj_get_prop_id_nr(obj, prop[PROP_ID]))
2070 # Return an abstract property ID for a given property
2071 function obj_get_prop_id(obj, prop) {
2073 errorx("obj_get_prop_id() on null object")
2075 return (class_get_prop_id(obj_get_class(obj), prop))
2079 # Return the property ID for a given property name
2080 function obj_get_named_prop_id(obj, name) {
2082 errorx("obj_get_named_prop_id() on null object")
2084 return (class_get_named_prop_id(obj_get_class(obj), name))
2087 # Set a property on obj
2088 function set(obj, prop, value, _class) {
2089 return (prop_set(obj, prop[PROP_ID], value))
2092 # Get a property value defined on obj
2093 function get(obj, prop, _class) {
2094 return (prop_get(obj, prop[PROP_ID]))
2097 # Set a property on obj, using a property ID returned by obj_get_prop_id() or
2098 # class_get_prop_id()
2099 function prop_set(obj, prop_id, value, _class) {
2101 errorx("setting property '" _g_props[prop_id] \
2105 _class = obj_get_class(obj)
2107 errorx(obj " has no superclass")
2109 if (!class_has_prop_id(_class, prop_id)) {
2110 errorx("requested undefined property '" _g_props[prop_id] \
2111 "' (" prop_id ") on " class_get_name(_class))
2114 # Track the line on which the property was set
2115 _g_obj_nr[obj,OBJ_PROP,prop_id] = NR
2116 _g_obj[obj,OBJ_PROP,prop_id] = value
2119 # Convert a property ID to a property path.
2120 function prop_id_to_path(prop_id) {
2121 if (!(prop_id in _g_props))
2122 errorx("'" prop_id "' is not a property ID")
2124 # Convert to path string representation
2128 # Convert a property to a property path.
2129 function prop_to_path(prop) {
2130 if (!(PROP_ID in prop))
2131 errorx("prop_to_path() called with non-property head")
2133 return (prop_id_to_path(prop[PROP_ID]))
2136 # Create a property path from head and tail properties
2137 # Additional properties may be appended via prop_path_append() or
2138 # prop_path_append_id()
2139 function prop_path_create(head, tail) {
2140 if (!(PROP_ID in head))
2141 errorx("prop_path() called with non-property head")
2143 if (!(PROP_ID in tail))
2144 errorx("prop_path() called with non-property tail")
2146 return (head[PROP_ID] SUBSEP tail[PROP_ID])
2149 # Append a property to the given property path
2150 function prop_path_append(path, tail) {
2151 if (!(PROP_ID in tail))
2152 errorx("prop_path_append() called with non-property tail")
2154 return (prop_path_append_id(path, tail[PROP_ID]))
2157 # Append a property ID to the given property path
2158 function prop_path_append_id(path, tail_id) {
2159 if (!(tail_id in _g_props))
2160 errorx("'" tail_id "' is not a property ID")
2162 return (path SUBSEP tail_id)
2165 # Fetch a value from obj using a property path previously returned by
2166 # prop_path_create(), prop_to_path(), etc.
2167 function prop_get_path(obj, prop_path, _class, _prop_ids, _nprop_ids, _next,
2168 _prop_head, _prop_len, _prop_tail)
2171 errorx("requested property path '" \
2172 gsub(SUBSEP, ".", prop_path) "' on null object")
2175 # Try the cache first
2176 _class = obj_get_class(obj)
2177 if ((_class,prop_path,PPATH_HEAD) in _g_ppath_cache) {
2178 _prop_head = _g_ppath_cache[_class,prop_path,PPATH_HEAD]
2179 _next = prop_get(obj, _prop_head)
2181 if ((_class,prop_path,PPATH_TAIL) in _g_ppath_cache) {
2182 _prop_tail = _g_ppath_cache[_class,prop_path,PPATH_TAIL]
2183 return (prop_get_path(_next, _prop_tail))
2189 # Parse the head/tail of the property path and add to cache
2190 _nprop_ids = split(prop_path, _prop_ids, SUBSEP)
2191 if (_nprop_ids == 0)
2192 errorx("empty property path")
2193 _prop_head = _prop_ids[1]
2194 _g_ppath_cache[_class,prop_path,PPATH_HEAD] = _prop_head
2196 if (_nprop_ids > 1) {
2197 _prop_len = length(_prop_head)
2198 _prop_tail = substr(prop_path, _prop_len+2)
2201 _g_ppath_cache[_class,prop_path,PPATH_TAIL] = _prop_tail
2204 # Recursively call out implementation, this time fetching from
2206 return (prop_get_path(obj, prop_path))
2209 # Fetch a value property value from obj, using a property ID returned by
2210 # obj_get_prop_id() or class_get_prop_id()
2211 function prop_get(obj, prop_id, _class) {
2213 errorx("requested property '" _g_props[prop_id] \
2217 _class = obj_get_class(obj)
2219 errorx(obj " has no superclass")
2221 if (!class_has_prop_id(_class, prop_id)) {
2222 errorx("requested undefined property '" _g_props[prop_id] \
2223 "' (" prop_id ") on " class_get_name(_class))
2226 return (_g_obj[obj,OBJ_PROP,prop_id])
2229 # Create a new MacroType instance
2230 function macro_type_new(name, const_suffix, _obj) {
2231 _obj = obj_new(MacroType)
2233 set(_obj, p_name, name)
2234 set(_obj, p_const_suffix, const_suffix)
2239 # Create a new MacroDefine instance
2240 function macro_new(name, value, _obj) {
2241 _obj = obj_new(MacroDefine)
2242 set(_obj, p_name, name)
2243 set(_obj, p_value, value)
2248 # Create an empty array; this uses _g_arrays to store integer
2249 # keys/values under the object's property prefix.
2250 function array_new(_obj) {
2251 _obj = obj_new(Array)
2252 set(_obj, p_count, 0)
2257 # Return the number of elements in the array
2258 function array_size(array) {
2259 obj_assert_class(array, Array)
2260 return (get(array, p_count))
2263 # Return true if the array is empty
2264 function array_empty(array) {
2265 return (array_size(array) == 0)
2268 # Append a value to the array
2269 function array_append(array, value, _i) {
2270 obj_assert_class(array, Array)
2272 _i = get(array, p_count)
2273 _g_arrays[array,OBJ_PROP,_i] = value
2274 set(array, p_count, _i+1)
2277 # Set an array value
2278 # An error will be thrown if the idx is outside array bounds
2279 function array_set(array, idx, value) {
2280 obj_assert_class(array, Array)
2282 if (!((array,OBJ_PROP,idx) in _g_arrays))
2283 errorx(idx " out of range of array " obj_to_string(array))
2285 _g_arrays[array,OBJ_PROP,idx] = value
2288 # Return value at the given index from the array
2289 # An error will be thrown if 'idx' is outside the array bounds
2290 function array_get(array, idx) {
2291 obj_assert_class(array, Array)
2293 if (!((array,OBJ_PROP,idx) in _g_arrays))
2294 errorx(idx " out of range of array " obj_to_string(array))
2296 return (_g_arrays[array,OBJ_PROP,idx])
2301 # Sort an array, using standard awk comparison operators over its values.
2303 # If `prop_path*` is non-NULL, the corresponding property path (or property ID)
2304 # will be fetched from each array element and used as the sorting value.
2306 # If multiple property paths are specified, the array is first sorted by
2307 # the first path, and then any equal values are sorted by the second path,
2310 function array_sort(array, prop_path0, prop_path1, prop_path2, _size) {
2311 obj_assert_class(array, Array)
2314 errorx("no more than three property paths may be specified")
2316 _size = array_size(array)
2320 _qsort(array, prop_path0, prop_path1, prop_path2, 0, _size-1)
2323 function _qsort_get_key(array, idx, prop_path, _v) {
2324 _v = array_get(array, idx)
2326 if (prop_path == null)
2329 return (prop_get_path(_v, prop_path))
2332 function _qsort_compare(array, lhs_idx, rhs_val, ppath0, ppath1, ppath2,
2333 _lhs_val, _rhs_prop_val)
2335 _lhs_val = _qsort_get_key(array, lhs_idx, ppath0)
2337 _rhs_prop_val = rhs_val
2339 _rhs_prop_val = prop_get_path(rhs_val, ppath0)
2341 if (_lhs_val == _rhs_prop_val && ppath1 != null) {
2342 _lhs_val = _qsort_get_key(array, lhs_idx, ppath1)
2343 _rhs_prop_val = prop_get_path(rhs_val, ppath1)
2345 if (_lhs_val == _rhs_prop_val && ppath2 != null) {
2346 _lhs_val = _qsort_get_key(array, lhs_idx, ppath2)
2347 _rhs_prop_val = prop_get_path(rhs_val, ppath2)
2351 if (_lhs_val < _rhs_prop_val)
2353 else if (_lhs_val > _rhs_prop_val)
2359 function _qsort(array, ppath0, ppath1, ppath2, first, last, _qpivot,
2360 _qleft, _qleft_val, _qright, _qright_val)
2365 # select pivot element
2366 _qpivot = int(first + int((last-first+1) * rand()))
2370 _qpivot_val = array_get(array, _qpivot)
2373 while (_qleft <= _qright) {
2374 while (_qsort_compare(array, _qleft, _qpivot_val, ppath0, ppath1,
2380 while (_qsort_compare(array, _qright, _qpivot_val, ppath0, ppath1,
2387 if (_qleft <= _qright) {
2388 _qleft_val = array_get(array, _qleft)
2389 _qright_val = array_get(array, _qright)
2391 array_set(array, _qleft, _qright_val)
2392 array_set(array, _qright, _qleft_val)
2399 # sort the partitions
2400 _qsort(array, ppath0, ppath1, ppath2, first, _qright)
2401 _qsort(array, ppath0, ppath1, ppath2, _qleft, last)
2406 # Join all array values with the given separator
2408 # If `prop_path` is non-NULL, the corresponding property path (or property ID)
2409 # will be fetched from each array value and included in the result, rather than
2410 # immediate array value
2412 function array_join(array, sep, prop_path, _i, _size, _value, _result) {
2413 obj_assert_class(array, Array)
2416 _size = array_size(array)
2417 for (_i = 0; _i < _size; _i++) {
2418 # Fetch the value (and optionally, a target property)
2419 _value = array_get(array, _i)
2420 if (prop_path != null)
2421 _value = prop_get_path(_value, prop_path)
2424 _result = _result _value sep
2426 _result = _result _value
2432 # Return the first value in the array, or null if empty
2433 function array_first(array) {
2434 obj_assert_class(array, Array)
2436 if (array_size(array) == 0)
2439 return (array_get(array, 0))
2442 # Return the last value in the array, or null if empty
2443 function array_tail(list, _size) {
2444 obj_assert_class(array, Array)
2446 _size = array_size(array)
2450 return (array_get(array, _size-1))
2453 # Create an empty hash table; this uses the _g_maps array to store arbitrary
2454 # keys/values under the object's property prefix.
2455 function map_new(_obj) {
2460 # Add `key` with `value` to `map`
2461 function map_set(map, key, value) {
2462 obj_assert_class(map, Map)
2463 _g_maps[map,OBJ_PROP,key] = value
2466 # Remove `key` from the map
2467 function map_remove(map, key) {
2468 obj_assert_class(map, Map)
2469 delete _g_maps[map,OBJ_PROP,key]
2472 # Return true if `key` is found in `map`, false otherwise
2473 function map_contains(map, key) {
2474 obj_assert_class(map, Map)
2475 return ((map,OBJ_PROP,key) in _g_maps)
2478 # Fetch the value of `key` from the map. Will throw an error if the
2479 # key does not exist
2480 function map_get(map, key) {
2481 obj_assert_class(map, Map)
2482 return _g_maps[map,OBJ_PROP,key]
2485 # Create and return a new list containing all defined values in `map`
2486 function map_to_array(map, _key, _prefix, _values) {
2487 obj_assert_class(map, Map)
2489 _values = array_new()
2490 _prefix = "^" map SUBSEP OBJ_PROP SUBSEP
2491 for (_key in _g_maps) {
2492 if (!match(_key, _prefix))
2495 array_append(_values, _g_maps[_key])
2501 # Create a new Type instance
2502 function type_new(name, width, signed, constant, array_constant, fmt, mask,
2503 constant_value, array_constant_value, _obj)
2505 obj_assert_class(fmt, Fmt)
2507 _obj = obj_new(Type)
2508 set(_obj, p_name, name)
2509 set(_obj, p_width, width)
2510 set(_obj, p_signed, signed)
2511 set(_obj, p_const, constant)
2512 set(_obj, p_const_val, constant_value)
2513 set(_obj, p_array_const, array_constant)
2514 set(_obj, p_array_const_val, array_constant_value)
2515 set(_obj, p_default_fmt, fmt)
2516 set(_obj, p_mask, mask)
2521 # Return true if two types are equal
2522 function type_equal(lhs, rhs) {
2527 # Must share a common class
2528 if (obj_get_class(lhs) != obj_get_class(rhs))
2531 # Handle ArrayType equality
2532 if (obj_is_instanceof(lhs, ArrayType)) {
2533 # Size must be equal
2534 if (get(lhs, p_count) != get(rhs, p_count))
2537 # The base types must be equal
2538 return (type_equal(type_get_base(lhs), type_get_base(rhs)))
2541 # Handle Type equality -- we just check for trivial identity
2542 # equality of all members
2543 obj_assert_class(lhs, Type)
2544 return (obj_trivially_equal(lhs, rhs))
2547 # Return the type's default value mask. If the type is an array type,
2548 # the default mask of the base type will be returned.
2549 function type_get_default_mask(type) {
2550 if (obj_is_instanceof(type, ArrayType))
2551 return (type_get_default_mask(type_get_base(type)))
2553 obj_assert_class(type, Type)
2554 return (get(type, p_mask))
2557 # Return the type's C constant representation
2558 function type_get_const(type) {
2559 if (obj_is_instanceof(type, ArrayType))
2560 return (get(type_get_base(type), p_array_const))
2562 obj_assert_class(type, Type)
2563 return (get(type, p_const))
2566 # Return the type's C constant integer value
2567 function type_get_const_val(type) {
2568 if (obj_is_instanceof(type, ArrayType))
2569 return (get(type_get_base(type), p_array_const_val))
2571 obj_assert_class(type, Type)
2572 return (get(type, p_const_val))
2575 # Return an array type's element count, or 1 if the type is not
2577 function type_get_nelem(type) {
2578 if (obj_is_instanceof(type, ArrayType))
2579 return (get(type, p_count))
2581 obj_assert_class(type, Type)
2585 # Return the base type for a given type instance.
2586 function type_get_base(type) {
2587 if (obj_is_instanceof(type, ArrayType))
2588 return (type_get_base(get(type, p_type)))
2590 obj_assert_class(type, Type)
2594 # Return the default fmt for a given type instance
2595 function type_get_default_fmt(type, _base, _fmt, _array_fmt) {
2596 _base = type_get_base(type)
2597 _fmt = get(_base, p_default_fmt)
2599 if (obj_is_instanceof(type, ArrayType)) {
2600 _array_fmt = get(_fmt, p_array_fmt)
2601 if (_array_fmt != null)
2608 # Return a string representation of the given type
2609 function type_to_string(type, _base_type) {
2610 if (obj_is_instanceof(type, ArrayType)) {
2611 _base_type = type_get_base(type)
2612 return (type_to_string(_base_type) "[" get(type, p_count) "]")
2614 return get(type, p_name)
2617 # Return true if type `rhs` is can be coerced to type `lhs` without data
2619 function type_can_represent(lhs, rhs) {
2620 # Must be of the same class (Type or ArrayType)
2621 if (obj_get_class(lhs) != obj_get_class(rhs))
2624 if (obj_is_instanceof(lhs, ArrayType)) {
2625 # The base types must have a representable relationship
2626 if (!type_can_represent(type_get_base(lhs), type_get_base(rhs)))
2629 # The lhs type must be able to represent -at least- as
2630 # many elements as the RHS type
2631 if (get(lhs, p_count) < get(rhs, p_count))
2637 # A signed type could represent the full range of a smaller unsigned
2638 # type, but we don't bother; the two should agree when used in a SROM
2639 # layout. Instead simply assert that both are signed or unsigned.
2640 if (get(lhs, p_signed) != get(rhs, p_signed))
2643 # The `rhs` type must be equal or smaller in width to the `lhs` type
2644 if (get(lhs, p_width) < get(rhs, p_width))
2650 # Create a new ArrayType instance
2651 function array_type_new(type, count, _obj) {
2652 _obj = obj_new(ArrayType)
2653 set(_obj, p_type, type)
2654 set(_obj, p_count, count)
2660 # Parse a type string to either the Type, ArrayType, or null if
2661 # the type is not recognized.
2663 function parse_type_string(str, _base, _count) {
2664 if (match(str, ARRAY_REGEX"$") > 0) {
2665 # Extract count and base type
2666 _count = substr(str, RSTART+1, RLENGTH-2)
2667 sub(ARRAY_REGEX"$", "", str)
2669 # Look for base type
2670 if ((_base = type_named(str)) == null)
2673 return (array_type_new(_base, int(_count)))
2675 return (type_named(str))
2680 # Parse a variable name in the form of 'name' or 'name[len]', returning
2681 # either the provided base_type if no array specifiers are found, or
2682 # the fully parsed ArrayType.
2684 function parse_array_type_specifier(str, base_type, _count) {
2685 if (match(str, ARRAY_REGEX"$") > 0) {
2687 _count = substr(str, RSTART+1, RLENGTH-2)
2688 return (array_type_new(base_type, int(_count)))
2694 # Return the type constant for `name`, if any
2695 function type_named(name, _n, _type) {
2697 errorx("called type_named() with null name")
2699 if (map_contains(BaseTypes, name))
2700 return (map_get(BaseTypes, name))
2705 # Create a new Fmt instance
2706 function fmt_new(name, symbol, array_fmt, _obj) {
2708 set(_obj, p_name, name)
2709 set(_obj, p_symbol, symbol)
2711 if (array_fmt != null)
2712 set(_obj, p_array_fmt, array_fmt)
2718 # Return the Fmt constant for `name`, if any
2719 function fmt_named(name, _n, _fmt) {
2720 if (map_contains(ValueFormats, name))
2721 return (map_get(ValueFormats, name))
2726 # Create a new VFlag instance
2727 function vflag_new(name, constant, _obj) {
2728 _obj = obj_new(VFlag)
2729 set(_obj, p_name, name)
2730 set(_obj, p_const, constant)
2735 # Create a new StringConstant AST node
2736 function stringconstant_new(value, continued, _obj) {
2737 _obj = obj_new(StringConstant)
2738 set(_obj, p_value, value)
2739 set(_obj, p_continued, continued)
2740 set(_obj, p_line, NR)
2745 # Create an empty StringConstant AST node to which additional lines
2747 function stringconstant_empty(_obj) {
2748 return (stringconstant_new("", 1))
2751 # Parse an input string and return a new string constant
2753 function stringconstant_parse_line(line, _obj) {
2754 _obj = stringconstant_empty()
2755 stringconstant_append_line(_obj, line)
2759 # Parse and apend an additional line to this string constant
2760 function stringconstant_append_line(str, line, _cont, _strbuf, _regex, _eol) {
2761 obj_assert_class(str, StringConstant)
2763 # Must be marked for continuation
2764 if (!get(str, p_continued)) {
2765 errorx("can't append to non-continuation string '" \
2766 get(str, p_value) "'")
2769 _strbuf = get(str, p_value)
2771 # If start of string, look for (and remove) initial double quote
2772 if (_strbuf == null) {
2773 _regex = "^[ \t]*\""
2774 if (!sub(_regex, "", line)) {
2775 error("expected quoted string")
2779 # Look for a terminating double quote
2780 _regex = "([^\"\\\\]*(\\\\.[^\"\\\\]*)*)\""
2782 _eol = match(line, _regex)
2784 # Drop everything following the terminating quote
2785 line = substr(line, 1, RLENGTH-1)
2788 # No terminating quote found, continues on next line
2792 # Trim leading and trailing whitespace
2793 sub(/(^[ \t]+|[ \t]+$)/, "", line)
2795 # Append to existing buffer
2796 if ((_strbuf = get(str, p_value)) == NULL)
2797 set(str, p_value, line)
2799 set(str, p_value, _strbuf " " line)
2801 # Update line continuation setting
2802 set(str, p_continued, _cont)
2805 # Create a new RevRange instance
2806 function revrange_new(start, end, _obj) {
2807 _obj = obj_new(RevRange)
2808 set(_obj, p_start, start)
2809 set(_obj, p_end, end)
2810 set(_obj, p_line, NR)
2815 # Return true if the two revision ranges are equal
2816 function revrange_equal(lhs, rhs) {
2817 if (get(lhs, p_start) != get(rhs, p_start))
2820 if (get(lhs, p_end) != get(rhs, p_end))
2826 # Return true if the requested rev is covered by revrange, false otherwise
2827 function revrange_contains(range, rev) {
2828 obj_assert_class(range, RevRange)
2830 if (rev < get(range, p_start))
2832 else if (rev > get(range, p_end)) {
2840 # Return a string representation of the given revision range
2842 function revrange_to_string(revs, _start, _end) {
2843 obj_assert_class(revs, RevRange)
2845 _start = get(revs, p_start)
2846 _end = get(revs, p_end)
2850 else if (_end == REV_MAX)
2851 return (">= " _start)
2853 return (_start "-" _end)
2856 # Create a new VarGroup instance
2857 function var_group_new(name, _obj) {
2858 _obj = obj_new(VarGroup)
2859 set(_obj, p_name, name)
2860 set(_obj, p_vars, array_new())
2861 set(_obj, p_line, NR)
2866 # Create a new NVRAM instance
2867 function nvram_new(_obj, _vars, _v) {
2868 _obj = obj_new(NVRAM)
2870 set(_obj, p_vars, _vars)
2871 set(_obj, p_var_groups, array_new())
2872 set(_obj, p_srom_layouts, array_new())
2873 set(_obj, p_srom_table, map_new())
2876 # Register our implicit variable definitions
2879 # SROM signature offset
2880 _v = var_new(VAccessInternal, "<sromsig>", UInt16)
2881 array_append(_vars, _v)
2882 _g_var_names[get(_v, p_name)] = _v
2885 _v = var_new(VAccessInternal, "<sromcrc>", UInt8)
2886 array_append(_vars, _v)
2887 _g_var_names[get(_v, p_name)] = _v
2892 # Register a new SROM layout instance
2893 # An error will be thrown if the layout overlaps any revisions covered
2894 # by an existing instance.
2895 function nvram_add_srom_layout(nvram, layout, _table, _revs, _start, _end, _i) {
2896 obj_assert_class(nvram, NVRAM)
2897 obj_assert_class(layout, SromLayout)
2899 # revision:layout hash table
2900 _table = get(nvram, p_srom_table)
2902 # register the layout's revisions
2903 _revs = get(layout, p_revisions)
2904 _start = get(_revs, p_start)
2905 _end = get(_revs, p_end)
2907 for (_i = _start; _i <= _end; _i++) {
2908 if (map_contains(_table, _i)) {
2909 error("SROM layout redeclares layout for revision '" \
2910 _i "' (originally declared on line " \
2911 get(map_get(_table, _i), p_line) ")")
2914 map_set(_table, _i, layout)
2917 # append to srom_layouts
2918 array_append(get(nvram, p_srom_layouts), layout)
2921 # Return the first SROM layout registered for a given SROM revision,
2922 # or null if no matching layout is found
2923 function nvram_get_srom_layout(nvram, revision, _layouts, _nlayouts, _layout,
2926 obj_assert_class(nvram, NVRAM)
2928 _layouts = get(nvram, p_srom_layouts)
2929 _nlayouts = array_size(_layouts)
2930 for (_i = 0; _i < _nlayouts; _i++) {
2931 _layout = array_get(_layouts, _i)
2933 if (srom_layout_has_rev(_layout, revision))
2941 # Create a new Var instance
2942 function var_new(access, name, type, _obj) {
2943 obj_assert_class(access, VAccess)
2945 # Validate the variable identifier
2947 # The access modifier dictates the permitted identifier format.
2948 # VAccessInternal: <ident>
2949 # VAccess(Public|Private): ident
2950 if (access != VAccessInternal && name ~ SVAR_IDENT_REGEX) {
2951 error("invalid identifier '"name"'; did you mean to " \
2952 "mark this variable as internal?")
2953 } else if (access == VAccessInternal) {
2954 if (name !~ SVAR_IDENT_REGEX)
2955 error("invalid identifier '"name"' for internal " \
2956 "variable; did you mean '<" name ">'?")
2957 } else if (name !~ VAR_IDENT_REGEX) {
2958 error("invalid identifier '"name"'")
2962 set(_obj, p_access, access)
2963 set(_obj, p_name, name)
2964 set(_obj, p_type, type)
2965 set(_obj, p_line, NR)
2970 # Return true if var is internal-only, and should not be included
2971 # in any output (e.g. has an access specifier of VAccessInternal).
2972 function var_is_internal(var) {
2973 return (get(var, p_access) == VAccessInternal)
2976 # Return true if `var` has an array type
2977 function var_has_array_type(var, _vtype) {
2978 obj_assert_class(var, Var)
2979 _vtype = get(var, p_type)
2980 return (obj_is_instanceof(_vtype, ArrayType))
2983 # Return the number of array elements defined by this variable's type,
2984 # or 1 if the variable does not have an array type.
2985 function var_get_array_len(var) {
2986 obj_assert_class(var, Var)
2987 return (type_get_nelem(get(var, p_type)))
2990 # Return the fmt for var. If not explicitly set on var, will return then
2991 # return of calling type_get_default_fmt() with the variable's type
2992 function var_get_fmt(var, _fmt) {
2993 obj_assert_class(var, Var)
2995 # If defined on var, return it
2996 if ((_fmt = get(var, p_fmt)) != null)
2999 # Fall back on the type's default
3000 return (type_get_default_fmt(get(var, p_type)))
3003 # Return a new MacroDefine instance for the given variable, macro type,
3005 function var_get_macro(var, macro_type, value, _macro) {
3006 obj_assert_class(var, Var)
3007 obj_assert_class(macro_type, MacroType)
3009 return (macro_new(var_get_macro_name(var, macro_type), value))
3012 # Return the preprocessor constant name to be used with `var` for the given
3014 function var_get_macro_name(var, macro_type, _var_name, _suffix) {
3015 obj_assert_class(var, Var)
3016 obj_assert_class(macro_type, MacroType)
3018 _var_name = get(var, p_name)
3019 _suffix = get(macro_type, p_const_suffix)
3021 return("BHND_NVAR_" toupper(_var_name) _suffix)
3024 # Create a new SromLayout instance
3025 function srom_layout_new(rev_desc, _obj)
3027 _obj = obj_new(SromLayout)
3028 set(_obj, p_revisions, rev_desc)
3029 set(_obj, p_entries, array_new())
3030 set(_obj, p_revmap, map_new())
3031 set(_obj, p_output_var_counts, map_new())
3032 set(_obj, p_line, NR)
3037 # Register a new entry with the srom layout
3038 function srom_layout_add_entry(layout, entry, _revmap, _name, _rev_start,
3039 _rev_end, _var, _prev_entry, _count, _i)
3041 obj_assert_class(layout, SromLayout)
3042 obj_assert_class(entry, SromEntry)
3044 _layout_revmap = get(layout, p_revmap)
3045 _layout_var_count = get(layout, p_output_var_counts)
3047 _var = get(entry, p_var)
3048 _name = get(_var, p_name)
3050 # Add to revision array
3051 array_append(get(layout, p_entries), entry)
3053 # Add to the revision map tables
3054 _rev_start = get(get(entry, p_revisions), p_start)
3055 _rev_end = get(get(entry, p_revisions), p_end)
3057 for (_i = _rev_start; _i <= _rev_end; _i++) {
3058 # Check for existing entry
3059 _prev_entry = srom_layout_find_entry(layout, _name, _i)
3060 if (_prev_entry != null) {
3061 error("redefinition of variable '" _name "' for SROM " \
3062 "revision " _i " (previously defined on line " \
3063 get(_prev_entry, p_line) ")")
3066 # Add to the (varname,revision) map
3067 map_set(_layout_revmap, (_name SUBSEP _i), entry)
3069 # If an output variable, set or increment the output variable
3071 if (!srom_entry_should_output(entry, _i))
3074 if (!map_contains(_layout_var_count, _i)) {
3075 map_set(_layout_var_count, _i, 1)
3077 _count = map_get(_layout_var_count, _i)
3078 map_set(_layout_var_count, _i, _count + 1)
3084 # Return the variable name to be used when emitting C declarations
3085 # for this SROM layout
3087 # The name is gauranteed to be unique across SROM layouts with non-overlapping
3089 function srom_layout_get_variable_name(layout, _revs) {
3090 obj_assert_class(layout, SromLayout)
3092 _revs = get(layout, p_revisions)
3094 return ("bhnd_sprom_layout_r" get(_revs, p_start) \
3095 "_r" get(_revs, p_end))
3098 # Return true if the given SROM revision is defined by the layout, false
3100 function srom_layout_has_rev(layout, rev) {
3101 obj_assert_class(layout, SromLayout)
3102 return (revrange_contains(get(layout, p_revisions), rev))
3106 # Return the total number of output variables (variables to be included
3107 # in the SROM layout bindings) for the given SROM revision
3108 function srom_layout_num_output_vars(layout, rev, _counts)
3110 obj_assert_class(layout, SromLayout)
3112 _counts = get(layout, p_output_var_counts)
3113 if (!map_contains(_counts, rev))
3116 return (map_get(_counts, rev))
3119 # Return the SromEntry defined for the given variable name and SROM revision,
3121 function srom_layout_find_entry(layout, vname, revision, _key, _srom_revmap) {
3122 obj_assert_class(layout, SromLayout)
3124 _srom_revmap = get(layout, p_revmap)
3126 # SromEntry are mapped by name,revision composite keys
3127 _key = vname SUBSEP revision
3128 if (!map_contains(_srom_revmap, _key))
3131 return (map_get(_srom_revmap, _key))
3135 # Create a new SromLayoutFilter instance, checking that `revs`
3136 # falls within the parent's revision range
3137 function srom_layout_filter_new(parent, revs, _obj, _start, _end, _parent_revs) {
3138 obj_assert_class(parent, SromLayout)
3139 obj_assert_class(revs, RevRange)
3141 # Fetch our parent's revision range, confirm that we're
3143 _start = get(revs, p_start)
3144 _end = get(revs, p_end)
3145 _parent_revs = get(parent, p_revisions)
3147 if (!revrange_contains(_parent_revs, _start))
3148 error("'" _start "' is outside of parent range")
3150 if (!revrange_contains(_parent_revs, _end))
3151 error("'" _end "' is outside of parent range")
3153 if (revrange_equal(revs, _parent_revs)) {
3154 error("srom range '" revrange_to_string(revs) "' is " \
3155 "identical to parent range of '" \
3156 revrange_to_string(_parent_revs) "'")
3159 # Construct and return new filter instance
3160 _obj = obj_new(SromLayoutFilter)
3161 set(_obj, p_parent, parent)
3162 set(_obj, p_revisions, revs)
3163 set(_obj, p_line, NR)
3169 # Create a new SromEntry instance
3171 # var: The variable referenced by this entry
3172 # revisions: The SROM revisions to which this entry applies
3173 # base_offset: The SROM entry offset; any relative segment offsets will be
3174 # calculated relative to the base offset
3175 # type: The SROM's value type; this may be a subtype of the variable
3176 # type, and defines the data (width, sign, etc) to be read from
3179 function srom_entry_new(var, revisions, base_offset, type, _obj) {
3180 obj_assert_class(var, Var)
3181 if (revisions != null)
3182 obj_assert_class(revisions, RevRange)
3184 _obj = obj_new(SromEntry)
3185 set(_obj, p_var, var)
3186 set(_obj, p_revisions, revisions)
3187 set(_obj, p_base_offset, base_offset)
3188 set(_obj, p_type, type)
3189 set(_obj, p_offsets, array_new())
3190 set(_obj, p_line, NR)
3195 # Return true if the SromEntry has an array type
3196 function srom_entry_has_array_type(entry) {
3197 obj_assert_class(entry, SromEntry)
3199 return (obj_is_instanceof(get(entry, p_type), ArrayType))
3202 # Return the number of array elements defined by this SromEntry's type,
3203 # or 1 if the entry does not have an array type.
3204 function srom_entry_get_array_len(entry, _type) {
3205 obj_assert_class(entry, SromEntry)
3207 return (type_get_nelem(get(entry, p_type)))
3211 # Return true if the given entry should be included in the output bindings
3212 # generated for the given revision, false otherwise.
3214 function srom_entry_should_output(entry, rev, _var, _revs)
3216 obj_assert_class(entry, SromEntry)
3218 _var = get(entry, p_var)
3219 _revs = get(entry, p_revisions)
3221 # Exclude internal variables
3222 if (var_is_internal(_var))
3225 # Exclude inapplicable entry revisions
3226 if (!revrange_contains(_revs, rev))
3233 # Return the single, non-shifted, non-masked offset/segment for the given
3234 # SromEntry, or throw an error if the entry contains multiple offsets/segments.
3236 # This is used to fetch special-cased variable definitions that are required
3237 # to present a single simple offset.
3239 function srom_entry_get_single_segment(entry, _offsets, _segments, _seg,
3240 _base_type, _default_mask)
3242 obj_assert_class(entry, SromEntry)
3244 # Fetch the single offset's segment list
3245 _offsets = get(entry, p_offsets)
3246 if (array_size(_offsets) != 1)
3247 errorc(get(entry, p_line), "unsupported offset count")
3249 _segments = get(array_first(_offsets), p_segments)
3250 if (array_size(_segments) != 1)
3251 errorc(get(entry, p_line), "unsupported segment count")
3253 # Fetch the single segment
3254 _seg = array_first(_segments)
3255 _base_type = srom_segment_get_base_type(_seg)
3256 _default_mask = get(_base_type, p_mask)
3258 # Must not be shifted/masked
3259 if (get(_seg, p_shift) != 0)
3260 errorc(obj_get_prop_nr(_seg, p_mask), "shift unsupported")
3262 if (get(_seg, p_mask) != _default_mask)
3263 errorc(obj_get_prop_nr(_seg, p_mask), "mask unsupported")
3268 # Create a new SromOffset instance
3269 function srom_offset_new(_obj) {
3270 _obj = obj_new(SromOffset)
3271 set(_obj, p_segments, array_new())
3272 set(_obj, p_line, NR)
3277 # Return the number of SromSegment instances defined by this offset.
3278 function srom_offset_segment_count(offset) {
3279 obj_assert_class(offset, SromOffset)
3280 return (array_size(get(offset, p_segments)))
3283 # Return the idx'th segment. Will throw an error if idx falls outside
3284 # the number of available segments.
3285 function srom_offset_get_segment(offset, idx, _segments, _seg) {
3286 obj_assert_class(offset, SromOffset)
3288 return (array_get(get(offset, p_segments), idx))
3291 # Create a new SromSegment instance
3292 function srom_segment_new(offset, type, mask, shift, value, _obj) {
3293 _obj = obj_new(SromSegment)
3294 set(_obj, p_offset, offset)
3295 set(_obj, p_type, type)
3296 set(_obj, p_mask, mask)
3297 set(_obj, p_shift, shift)
3298 set(_obj, p_value, value)
3299 set(_obj, p_line, NR)
3304 # Return true if the segment has an array type
3305 function srom_segment_has_array_type(seg, _type) {
3306 _type = srom_segment_get_type(seg)
3307 return (obj_is_instanceof(_type, ArrayType))
3310 # Return the array count of the segment, or 1 if the segment does not have
3312 function srom_segment_get_array_len(seg, _type) {
3313 if (!srom_segment_has_array_type(seg))
3316 _type = srom_segment_get_type(seg)
3317 return (get(_type, p_count))
3320 # Return the type of the segment
3321 function srom_segment_get_type(seg) {
3322 obj_assert_class(seg, SromSegment)
3323 return (get(seg, p_type))
3327 # Return the base type of the segment
3328 function srom_segment_get_base_type(seg) {
3329 return (type_get_base(srom_segment_get_type(seg)))
3332 # Return true if the two segments have identical types and attributes (i.e.
3333 # differing only by offset)
3334 function srom_segment_attributes_equal(lhs, rhs) {
3335 obj_assert_class(lhs, SromSegment)
3336 obj_assert_class(rhs, SromSegment)
3339 if (!type_equal(get(lhs, p_type), get(rhs, p_type)))
3343 if (get(lhs, p_mask) != get(rhs, p_mask))
3347 if (get(lhs, p_shift) != get(rhs, p_shift))
3351 if (get(lhs, p_value) != get(rhs, p_value))
3357 # Return a human-readable representation of a Segment instance
3358 function segment_to_string(seg, _str, _t, _m, _s, _attrs, _attr_str) {
3359 _attrs = array_new()
3361 # include type (if specified)
3362 if ((_t = get(seg, p_type)) != null)
3363 _str = (type_to_string(_t) " ")
3366 _str = (_str sprintf("0x%X", get(seg, p_offset)))
3368 # append list of attributes
3369 if ((_m = get(seg, p_mask)) != null)
3370 array_append(_attrs, ("&" _m))
3372 if ((_s = get(seg, p_shift)) != null) {
3377 array_append(_attrs, _s)
3380 _attr_str = array_join(_attrs, ", ")
3383 if (_attr_str == "")
3386 return (_str " (" _attr_str ")")
3389 # return the flag definition for variable `v`
3390 function gen_var_flags(v, _type, _flags, _flag, _str)
3393 _type = get(v, p_type)
3394 _flags = array_new()
3397 if (get(v, p_access) == VAccessPrivate)
3398 array_append(_flags, VFlagPrivate)
3401 if (get(v, p_ignall1))
3402 array_append(_flags, VFlagIgnoreAll1)
3404 # If empty, return empty flag value
3405 if (array_size(_flags) == 0) {
3410 # Join all flag constants with |
3411 _str = array_join(_flags, "|", class_get_prop_id(VFlag, p_const))
3420 # Return the absolute value
3423 return (i < 0 ? -i : i)
3427 # Return the minimum of two values
3429 function min(lhs, rhs) {
3430 return (lhs < rhs ? lhs : rhs)
3434 # Return the maximum of two values
3436 function max(lhs, rhs) {
3437 return (lhs > rhs ? lhs : rhs)
3441 # Parse a hex string
3443 function parse_hex_string(str, _hex_pstate, _out, _p, _count) {
3444 if (!AWK_REQ_HEX_PARSING)
3447 # Populate hex parsing lookup table on-demand
3448 if (!("F" in _g_hex_table)) {
3449 for (_p = 0; _p < 16; _p++) {
3450 _g_hex_table[sprintf("%X", _p)] = _p
3451 _g_hex_table[sprintf("%x", _p)] = _p
3455 # Split input into an array
3456 _count = split(toupper(str), _hex_pstate, "")
3460 if (_count >= 2 && _hex_pstate[1] == "0") {
3461 if (_hex_pstate[2] == "x" || _hex_pstate[2] == "X")
3465 # Parse the hex_digits
3467 for (; _p <= _count; _p++)
3468 _out = (_out * 16) + _g_hex_table[_hex_pstate[_p]]
3474 # Return the integer representation of an unsigned decimal, hexadecimal, or
3477 function parse_uint_string(str) {
3478 if (str ~ UINT_REGEX)
3480 else if (str ~ HEX_REGEX)
3481 return (parse_hex_string(str))
3483 error("invalid integer value: '" str "'")
3487 # Parse an offset string, stripping any leading '+' or trailing ':' or ','
3494 function parse_uint_offset(str) {
3495 # Drop any leading '+'
3498 # Drop any trailing ':', ',', or '|'
3499 sub("[,|:]$", "", str)
3501 # Parse the cleaned up string
3502 return (parse_uint_string(str))
3506 # Print msg to output file, without indentation
3508 function emit_ni(msg) {
3509 printf("%s", msg) >> OUTPUT_FILE
3513 # Print msg to output file, indented for the current `output_depth`
3515 function emit(msg, _ind) {
3516 for (_ind = 0; _ind < output_depth; _ind++)
3523 # Print a warning to stderr
3525 function warn(msg) {
3526 print "warning:", msg, "at", FILENAME, "line", NR > "/dev/stderr"
3530 # Print an warning message without including the source line information
3532 function warnx(msg) {
3533 print "warning:", msg > "/dev/stderr"
3537 # Print a compiler error to stderr with a caller supplied
3540 function errorc(line, msg) {
3541 errorx(msg " at " FILENAME " line " line)
3545 # Print a compiler error to stderr
3547 function error(msg) {
3548 errorx(msg " at " FILENAME " line " NR ":\n\t" $0)
3552 # Print an error message without including the source line information
3554 function errorx(msg) {
3555 print "error:", msg > "/dev/stderr"
3561 # Print a debug output message
3563 function debug(msg, _i) {
3566 for (_i = 1; _i < _g_parse_stack_depth; _i++)
3567 printf("\t") > "/dev/stderr"
3568 print msg > "/dev/stderr"
3572 # Advance to the next non-comment input record
3574 function next_line(_result) {
3577 } while (_result > 0 && $0 ~ /^[ \t]*#.*/) # skip comment lines
3582 # Advance to the next input record and verify that it matches @p regex
3584 function getline_matching(regex, _result) {
3585 _result = next_line()
3596 # Shift the current fields left by `n`.
3598 # If all fields are consumed and the optional do_getline argument is true,
3599 # read the next line.
3601 function shiftf(n, do_getline, _i) {
3603 error("shift past end of line")
3606 # If shifting the entire line, just reset the line value
3609 for (_i = 1; _i <= NF-n; _i++) {
3615 if (NF == 0 && do_getline)
3619 # Push a new parser state.
3620 function parser_state_push(ctx, is_block, _state) {
3621 _state = obj_new(ParseState)
3622 set(_state, p_ctx, ctx)
3623 set(_state, p_is_block, is_block)
3624 set(_state, p_line, NR)
3626 _g_parse_stack_depth++
3627 _g_parse_stack[_g_parse_stack_depth] = _state
3630 # Fetch the current parser state
3631 function parser_state_get() {
3632 if (_g_parse_stack_depth == 0)
3633 errorx("parser_state_get() called with empty parse stack")
3635 return (_g_parse_stack[_g_parse_stack_depth])
3638 # Pop the current parser state
3639 function parser_state_pop(_block_state, _closes_block) {
3640 if (_g_parse_stack_depth == 0)
3641 errorx("parser_state_pop() called with empty parse stack")
3643 _closes_block = get(parser_state_get(), p_is_block)
3645 delete _g_parse_stack[_g_parse_stack_depth]
3646 _g_parse_stack_depth--
3652 # Fetch the current context object associated with this parser state
3653 # The object will be asserted as being an instance of the given class.
3654 function parser_state_get_context(class, _ctx_obj) {
3655 _ctx_obj = get(parser_state_get(), p_ctx)
3656 obj_assert_class(_ctx_obj, class)
3661 # Walk the parser state stack until a context object of the given class
3662 # is found. If the top of the stack is reached without finding a context object
3663 # of the requested type, an error will be thrown.
3664 function parser_state_find_context(class, _state, _ctx, _i) {
3666 errorx("parser_state_find_context() called with null class")
3668 # Find the first context instance inheriting from `class`
3669 for (_i = 0; _i < _g_parse_stack_depth; _i++) {
3670 _state = _g_parse_stack[_g_parse_stack_depth - _i]
3671 _ctx = get(_state, p_ctx)
3674 if (obj_is_instanceof(_ctx, class))
3679 errorx("no context instance of type '" class_get_name(class) "' " \
3680 "found in parse stack")
3684 # Find opening brace and push a new parser state for a brace-delimited block.
3686 function parser_state_open_block(ctx) {
3687 if ($0 ~ "{" || getline_matching("^[ \t]*{") > 0) {
3688 parser_state_push(ctx, 1)
3689 sub("^[^{]*{", "", $0)
3693 error("found '"$1 "' instead of expected '{'")
3697 # Find closing brace and pop parser states until the first
3698 # brace-delimited block is discarded.
3700 function parser_state_close_block(_next_state, _found_block) {
3702 error("internal error - no closing brace")
3704 # pop states until we exit the first enclosing block
3706 _next_state = parser_state_get()
3707 _found_block = get(_next_state, p_is_block)
3709 } while (!_found_block)
3711 # strip everything prior to the block closure
3712 sub("^[^}]*}", "", $0)
3715 # Evaluates to true if the current parser state is defined with a context of
3717 function in_parser_context(class, _ctx) {
3719 errorx("called in_parser_context() with null class")
3721 _ctx = get(parser_state_get(), p_ctx)
3722 return (obj_is_instanceof(_ctx, class))
3726 # Parse and return a revision range from the current line.
3729 # 4-10 # revisions 4-10, inclusive
3735 function parse_revrange(_start, _end, _robj) {
3739 if ($2 ~ "[0-9]*-[0-9*]") {
3740 split($2, _g_rev_range, "[ \t]*-[ \t]*")
3741 _start = int(_g_rev_range[1])
3742 _end = int(_g_rev_range[2])
3743 } else if ($2 ~ "(>|>=|<|<=)" && $3 ~ "[1-9][0-9]*") {
3747 } else if ($2 == ">=") {
3750 } else if ($2 == "<" && int($3) > 0) {
3753 } else if ($2 == "<=") {
3757 error("invalid revision descriptor")
3759 } else if ($2 ~ "[1-9][0-9]*") {
3763 error("invalid revision descriptor")
3766 return (revrange_new(_start, _end))
3770 # Parse a variable group block starting at the current line
3772 # group "Group Name" {
3779 function parse_variable_group(_ctx, _groups, _group, _group_name) {
3780 _ctx = parser_state_get_context(NVRAM)
3782 # Seek to the start of the name string
3785 # Parse the first line
3786 _group_name = stringconstant_parse_line($0)
3788 # Incrementally parse line continuations
3789 while (get(_group_name, p_continued)) {
3791 stringconstant_append_line(_group_name, $0)
3794 debug("group \"" get(_group_name, p_value) "\" {")
3796 # Register the new variable group
3797 _groups = get(_ctx, p_var_groups)
3798 _group = var_group_new(_group_name)
3799 array_append(_groups, _group)
3801 # Push our variable group block
3802 parser_state_open_block(_group)
3807 # Parse a variable definition block starting at the current line
3814 function parse_variable_defn(_ctx, _vaccess, _type, _name, _fmt, _var,
3817 _ctx = parser_state_get_context(SymbolContext)
3819 # Check for access modifier
3820 if ($1 == "private") {
3821 _vaccess = VAccessPrivate
3823 } else if ($1 == "internal") {
3824 _vaccess = VAccessInternal
3827 _vaccess = VAccessPublic
3830 # Find the base type
3831 if ((_type = type_named($1)) == null)
3832 error("unknown type '" $1 "'")
3834 # Parse (and trim) any array specifier from the variable name
3836 _type = parse_array_type_specifier(_name, _type)
3837 sub(ARRAY_REGEX"$", "", _name)
3839 # Look for an existing variable definition
3840 if (_name in _g_var_names) {
3841 error("variable identifier '" _name "' previously defined at " \
3842 "line " get(_g_var_names[_name], p_line))
3845 # Construct new variable instance
3846 _var = var_new(_vaccess, _name, _type)
3847 debug((_private ? "private " : "") type_to_string(_type) " " _name " {")
3849 # Register in global name table
3850 _g_var_names[_name] = _var
3852 # Add to our parent context
3853 _var_list = get(_ctx, p_vars)
3854 array_append(_var_list, _var)
3856 # Push our variable definition block
3857 parser_state_open_block(_var)
3862 # Return a string containing the human-readable list of valid Fmt names
3864 function fmt_get_human_readable_list(_result, _fmts, _fmt, _nfmts, _i)
3866 # Build up a string listing the valid formats
3867 _fmts = map_to_array(ValueFormats)
3870 _nfmts = array_size(_fmts)
3871 for (_i = 0; _i < _nfmts; _i++) {
3872 _fmt = array_get(_fmts, _i)
3874 _result = _result "or "
3876 _result = _name_str \
3877 "'" get(_fmt, p_name) "'"
3880 _result = _result ", "
3888 # Parse a variable parameter from the current line
3890 # fmt (decimal|hex|macaddr|...)
3892 # desc "quoted string"
3893 # help "quoted string"
3895 function parse_variable_param(param_name, _var, _vprops, _prop_id, _pval) {
3896 _var = parser_state_get_context(Var)
3898 if (param_name == "fmt") {
3901 # Check for an existing definition
3902 if ((_pval = get(_var, p_fmt)) != null) {
3903 error("fmt previously specified on line " \
3904 obj_get_prop_nr(_var, p_fmt))
3907 # Validate arguments
3909 error("'" $1 "' requires a single parameter value of " \
3910 fmt_get_human_readable_list())
3913 if ((_pval = fmt_named($2)) == null) {
3914 error("'" $1 "' value '" $2 "' unrecognized. Must be " \
3915 "one of " fmt_get_human_readable_list())
3919 set(_var, p_fmt, _pval)
3920 } else if (param_name == "all1") {
3923 # Check for an existing definition
3924 if ((_pval = get(_var, p_ignall1)) != null) {
3925 error("all1 previously specified on line " \
3926 obj_get_prop_nr(_var, p_ignall1))
3931 error("'" $1 "'requires a single 'ignore' argument")
3932 else if ($2 != "ignore")
3933 error("unknown "$1" value '"$2"', expected 'ignore'")
3935 # Set variable property
3936 set(_var, p_ignall1, 1)
3937 } else if (param_name == "desc" || param_name == "help") {
3938 # Fetch an indirect property reference for either the 'desc'
3939 # or 'help' property
3940 _prop_id = obj_get_named_prop_id(_var, param_name)
3942 # Check for an existing definition
3943 if ((_pval = prop_get(_var, _prop_id)) != null) {
3944 error(get(_var, p_name) " '" $1 "' redefined " \
3945 "(previously defined on line " \
3946 obj_get_prop_id_nr(_var, _prop_id) ")")
3949 # Seek to the start of the desc/help string
3952 # Parse the first line
3953 _pval = stringconstant_parse_line($0)
3955 # Incrementally parse line continuations
3956 while (get(_pval, p_continued)) {
3958 stringconstant_append_line(_pval, $0)
3961 debug(param_name " \"" get(_pval, p_value) "\"")
3963 # Add to the var object
3964 prop_set(_var, _prop_id, _pval)
3966 error("unknown variable property type: '" param_name "'")
3972 # Parse a top-level SROM layout block starting at the current line
3978 function parse_srom_layout(_nvram, _srom_layouts, _revs, _layout) {
3979 _nvram = parser_state_get_context(NVRAM)
3980 _srom_layouts = get(_nvram, p_srom_layouts)
3982 # Parse revision descriptor and register SROM
3984 _revs = parse_revrange()
3985 _layout = srom_layout_new(_revs)
3986 nvram_add_srom_layout(_nvram, _layout)
3988 debug("srom " revrange_to_string(_revs) " {")
3990 # Push new SROM parser state
3991 parser_state_open_block(_layout)
3996 # Parse a nested srom range filter block starting at the current line
4004 function parse_srom_layout_filter(_parent, _revs, _filter) {
4005 _parent = parser_state_get_context(SromLayout)
4007 # Parse revision descriptor
4008 _revs = parse_revrange()
4010 # Construct the filter (which also validates the revision range)
4011 _filter = srom_layout_filter_new(_parent, _revs)
4013 debug("srom " revrange_to_string(_revs) " {")
4015 # Push new SROM parser state
4016 parser_state_open_block(_filter)
4021 # Parse a SROM offset segment's attribute list from the current line
4024 # (&0xF0, >>4, =0x5340)
4027 # Attribute designators:
4028 # &0xF Mask value with 0xF
4029 # <<4 Shift left 4 bits
4030 # >>4 Shift right 4 bits
4031 # =0x53 The parsed value must be equal to this constant value
4033 # May be followed by a | indicating that this segment should be OR'd with the
4034 # segment that follows, or a terminating , indicating that a new offset's
4035 # list of segments may follow.
4037 function parse_srom_segment_attributes(offset, type, _attrs, _num_attr, _attr,
4038 _mask, _shift, _value, _i)
4040 # seek to offset (attributes...) or end of the offset expr (|,)
4041 sub("^[^,(|){}]+", "", $0)
4044 _mask = type_get_default_mask(type)
4049 # extract attribute list
4050 if (match($0, /\([^|\(\)]*\)/) <= 0)
4051 error("expected attribute list")
4053 _attrs = substr($0, RSTART+1, RLENGTH-2)
4055 # drop attribute list from the input line
4056 $0 = substr($0, RSTART+RLENGTH, length($0) - RSTART+RLENGTH)
4059 _num_attr = split(_attrs, _g_attrs, ",[ \t]*")
4060 for (_i = 1; _i <= _num_attr; _i++) {
4061 _attr = _g_attrs[_i]
4063 if (sub("^&[ \t]*", "", _attr) > 0) {
4064 _mask = parse_uint_string(_attr)
4065 } else if (sub("^<<[ \t]*", "", _attr) > 0) {
4066 _shift = - parse_uint_string(_attr)
4067 } else if (sub("^>>[ \t]*", "", _attr) > 0) {
4068 _shift = parse_uint_string(_attr)
4069 } else if (sub("^=[ \t]*", "", _attr) > 0) {
4072 error("unknown attribute '" _attr "'")
4077 return (srom_segment_new(offset, type, _mask, _shift, _value))
4081 # Parse a SROM offset's segment declaration from the current line
4083 # +0x0: u8 (&0xF0, >>4) # read 8 bits at +0x0 (relative to srom entry
4084 # # offset, apply 0xF0 mask, shift >> 4
4085 # 0x10: u8 (&0xF0, >>4) # identical to above, but perform the read at
4086 # # absolute offset 0x10
4088 # +0x0: u8 # no attributes
4091 # +0x0 # simplified forms denoted by lack of ':'; the
4092 # 0x0 # type is inherited from the parent SromEntry
4095 function parse_srom_segment(base_offset, base_type, _simple, _type, _type_str,
4096 _offset, _attrs, _num_attr, _attr, _mask, _shift, _off_desc)
4098 # Fetch the offset value
4101 # Offset string must be one of:
4102 # simplified entry: <offset|+reloff>
4103 # Provides only the offset, with the type inherited
4104 # from the original variable definition
4105 # standard entry: <offset|+reloff>:
4106 # Provides the offset, followed by a type
4108 # We differentiate the two by looking for (and simultaneously removing)
4110 if (!sub(/:$/, "", _offset))
4113 # The offset may either be absolute (e.g. 0x180) or relative (e.g.
4116 # If we find a relative offset definition, we must trim the leading '+'
4117 # and then add the base offset
4118 if (sub(/^\+/, "", _offset)) {
4119 _offset = base_offset + parse_uint_offset(_offset)
4122 _offset = parse_uint_offset(_offset)
4125 # If simplified form, use the base type of the SROM entry. Otherwise,
4126 # we need to parse the type.
4131 sub(/,$/, "", _type_str) # trim trailing ',', if any
4133 if ((_type = parse_type_string(_type_str)) == null)
4134 error("unknown type '" _type_str "'")
4137 # Parse the trailing (... attributes ...), if any
4138 return (parse_srom_segment_attributes(_offset, _type))
4142 # Parse a SROM variable entry from the current line
4143 # <offset>: <type> <varname><array spec> ...
4145 function parse_srom_variable_entry(_srom, _srom_revs, _rev_start, _rev_end,
4146 _srom_entries, _srom_revmap, _prev_entry, _ctx, _base_offset, _name,
4147 _stype, _var, _entry, _offset, _seg, _i)
4149 # Fetch our parent context
4150 _ctx = parser_state_get_context(SromContext)
4151 _srom_revs = get(_ctx, p_revisions)
4152 _rev_start = get(_srom_revs, p_start)
4153 _rev_end = get(_srom_revs, p_end)
4155 # Locate our enclosing layout
4156 _srom = parser_state_find_context(SromLayout)
4157 _srom_entries = get(_srom, p_entries)
4158 _srom_revmap = get(_srom, p_revmap)
4160 # Verify argument count
4162 error("unrecognized srom entry syntax; must specify at " \
4163 "least \"<offset>: <type> <variable name>\"")
4166 # Parse the base offset
4167 _base_offset = parse_uint_offset($1)
4169 # Parse the base type
4170 if ((_stype = type_named($2)) == null)
4171 error("unknown type '" $2 "'")
4173 # Parse (and trim) any array specifier from the variable name
4175 _stype = parse_array_type_specifier(_name, _stype)
4176 sub(ARRAY_REGEX"$", "", _name)
4178 # Locate the variable definition
4179 if (!(_name in _g_var_names))
4180 error("no definition found for variable '" _name "'")
4181 _var = _g_var_names[_name]
4183 # The SROM entry type must be a subtype of the variable's declared
4185 if (!type_can_represent(get(_var, p_type), _stype)) {
4186 error("'" type_to_string(_stype) "' SROM value cannot be " \
4187 "coerced to '" type_to_string(get(_var, p_type)) " " _name \
4191 # Create and register our new offset entry
4192 _entry = srom_entry_new(_var, _srom_revs, _base_offset, _stype)
4193 srom_layout_add_entry(_srom, _entry)
4195 # Seek to either the block start ('{'), or the attributes to be
4196 # used for a single offset/segment entry at `offset`
4199 # Using the block syntax? */
4201 debug(sprintf("0x%03x: %s %s {", _base_offset,
4202 type_to_string(_stype), _name))
4203 parser_state_open_block(_entry)
4205 # Otherwise, we're using the simplified syntax -- create and
4206 # register our implicit SromOffset
4207 _offset = srom_offset_new()
4208 array_append(get(_entry, p_offsets), _offset)
4210 # Parse and register simplified segment syntax
4211 _seg = parse_srom_segment_attributes(_base_offset, _stype)
4212 array_append(get(_offset, p_segments), _seg)
4214 debug(sprintf("0x%03x: %s %s { %s }", _base_offset,
4215 type_to_string(_stype), _name, segment_to_string(_seg)))
4220 # Parse all SromSegment entry segments readable starting at the current line
4222 # <offset|+reloff>[,|]?
4223 # <offset|+reloff>: <type>[,|]?
4224 # <offset|+reloff>: <type> (<attributes>)[,|]?
4226 function parse_srom_entry_segments(_entry, _base_off, _base_type, _offs,
4227 _offset, _segs, _seg, _more_seg, _more_vals)
4229 _entry = parser_state_get_context(SromEntry)
4230 _base_off = get(_entry, p_base_offset)
4231 _offs = get(_entry, p_offsets)
4233 _base_type = get(_entry, p_type)
4234 _base_type = type_get_base(_base_type)
4238 # Create a SromOffset
4239 _offset = srom_offset_new()
4240 _segs = get(_offset, p_segments)
4242 array_append(_offs, _offset)
4244 # Parse all segments
4246 _seg = parse_srom_segment(_base_off, _base_type)
4247 array_append(_segs, _seg)
4249 # Do more segments follow?
4250 _more_seg = ($1 == "|")
4255 debug(segment_to_string(_seg) " |")
4257 debug(segment_to_string(_seg))
4260 # Do more offsets follow?
4261 _more_vals = ($1 == ",")
4264 } while (_more_vals)